|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.sql; |
| 7 | + |
| 8 | +import static org.hamcrest.Matchers.containsString; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import org.junit.Test; |
| 12 | +import org.opensearch.client.ResponseException; |
| 13 | +import org.opensearch.sql.legacy.SQLIntegTestCase; |
| 14 | +import org.opensearch.sql.legacy.TestsConstants; |
| 15 | + |
| 16 | +/** |
| 17 | + * Integration tests for vectorSearch SQL table function. These tests verify DSL push-down shape via |
| 18 | + * _explain and validation error paths. They do NOT require the k-NN plugin since _explain only |
| 19 | + * parses and plans the query without executing it against a knn index. |
| 20 | + */ |
| 21 | +public class VectorSearchIT extends SQLIntegTestCase { |
| 22 | + |
| 23 | + @Override |
| 24 | + protected void init() throws Exception { |
| 25 | + // _explain needs the index to exist for field resolution. |
| 26 | + loadIndex(Index.ACCOUNT); |
| 27 | + } |
| 28 | + |
| 29 | + private static final String TEST_INDEX = TestsConstants.TEST_INDEX_ACCOUNT; |
| 30 | + |
| 31 | + // ── DSL shape verification via _explain ─────────────────────────────── |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testExplainTopKProducesKnnQuery() throws IOException { |
| 35 | + String explain = |
| 36 | + explainQuery( |
| 37 | + "SELECT v._id, v._score " |
| 38 | + + "FROM vectorSearch(table='" |
| 39 | + + TEST_INDEX |
| 40 | + + "', field='embedding', " |
| 41 | + + "vector='[1.0, 2.0, 3.0]', option='k=5') AS v " |
| 42 | + + "LIMIT 5"); |
| 43 | + |
| 44 | + // WrapperQueryBuilder wraps the knn JSON — verify the wrapper is present |
| 45 | + // and track_scores is enabled for score preservation. |
| 46 | + assertTrue("Explain should contain wrapper query:\n" + explain, explain.contains("wrapper")); |
| 47 | + assertTrue( |
| 48 | + "Explain should contain track_scores:\n" + explain, explain.contains("track_scores")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testExplainRadialMaxDistanceProducesKnnQuery() throws IOException { |
| 53 | + String explain = |
| 54 | + explainQuery( |
| 55 | + "SELECT v._id, v._score " |
| 56 | + + "FROM vectorSearch(table='" |
| 57 | + + TEST_INDEX |
| 58 | + + "', field='embedding', " |
| 59 | + + "vector='[1.0, 2.0]', option='max_distance=10.5') AS v " |
| 60 | + + "LIMIT 100"); |
| 61 | + |
| 62 | + assertTrue("Explain should contain wrapper query:\n" + explain, explain.contains("wrapper")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testExplainRadialMinScoreProducesKnnQuery() throws IOException { |
| 67 | + String explain = |
| 68 | + explainQuery( |
| 69 | + "SELECT v._id, v._score " |
| 70 | + + "FROM vectorSearch(table='" |
| 71 | + + TEST_INDEX |
| 72 | + + "', field='embedding', " |
| 73 | + + "vector='[1.0, 2.0]', option='min_score=0.8') AS v " |
| 74 | + + "LIMIT 100"); |
| 75 | + |
| 76 | + assertTrue("Explain should contain wrapper query:\n" + explain, explain.contains("wrapper")); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testExplainPostFilterProducesBoolQuery() throws IOException { |
| 81 | + String explain = |
| 82 | + explainQuery( |
| 83 | + "SELECT v._id, v._score " |
| 84 | + + "FROM vectorSearch(table='" |
| 85 | + + TEST_INDEX |
| 86 | + + "', field='embedding', " |
| 87 | + + "vector='[1.0, 2.0, 3.0]', option='k=10') AS v " |
| 88 | + + "WHERE v.state = 'TX' " |
| 89 | + + "LIMIT 10"); |
| 90 | + |
| 91 | + assertTrue("Explain should contain bool query:\n" + explain, explain.contains("bool")); |
| 92 | + assertTrue( |
| 93 | + "Explain should contain must clause (knn in scoring context):\n" + explain, |
| 94 | + explain.contains("must")); |
| 95 | + assertTrue( |
| 96 | + "Explain should contain filter clause (WHERE in non-scoring context):\n" + explain, |
| 97 | + explain.contains("filter")); |
| 98 | + } |
| 99 | + |
| 100 | + // ── Validation error paths ──────────────────────────────────────────── |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testMutualExclusivityRejectsKAndMaxDistance() throws IOException { |
| 104 | + ResponseException ex = |
| 105 | + expectThrows( |
| 106 | + ResponseException.class, |
| 107 | + () -> |
| 108 | + executeQuery( |
| 109 | + "SELECT v._id FROM vectorSearch(table='t', field='f', " |
| 110 | + + "vector='[1.0]', option='k=5,max_distance=10') AS v")); |
| 111 | + |
| 112 | + assertThat(ex.getMessage(), containsString("Only one of")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testMutualExclusivityRejectsKAndMinScore() throws IOException { |
| 117 | + ResponseException ex = |
| 118 | + expectThrows( |
| 119 | + ResponseException.class, |
| 120 | + () -> |
| 121 | + executeQuery( |
| 122 | + "SELECT v._id FROM vectorSearch(table='t', field='f', " |
| 123 | + + "vector='[1.0]', option='k=5,min_score=0.5') AS v")); |
| 124 | + |
| 125 | + assertThat(ex.getMessage(), containsString("Only one of")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testKTooLargeRejects() throws IOException { |
| 130 | + ResponseException ex = |
| 131 | + expectThrows( |
| 132 | + ResponseException.class, |
| 133 | + () -> |
| 134 | + executeQuery( |
| 135 | + "SELECT v._id FROM vectorSearch(table='t', field='f', " |
| 136 | + + "vector='[1.0]', option='k=10001') AS v")); |
| 137 | + |
| 138 | + assertThat(ex.getMessage(), containsString("k must be between 1 and 10000")); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testKZeroRejects() throws IOException { |
| 143 | + ResponseException ex = |
| 144 | + expectThrows( |
| 145 | + ResponseException.class, |
| 146 | + () -> |
| 147 | + executeQuery( |
| 148 | + "SELECT v._id FROM vectorSearch(table='t', field='f', " |
| 149 | + + "vector='[1.0]', option='k=0') AS v")); |
| 150 | + |
| 151 | + assertThat(ex.getMessage(), containsString("k must be between 1 and 10000")); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testUnknownOptionKeyRejects() throws IOException { |
| 156 | + ResponseException ex = |
| 157 | + expectThrows( |
| 158 | + ResponseException.class, |
| 159 | + () -> |
| 160 | + executeQuery( |
| 161 | + "SELECT v._id FROM vectorSearch(table='t', field='f', " |
| 162 | + + "vector='[1.0]', option='k=5,method.ef_search=100') AS v")); |
| 163 | + |
| 164 | + assertThat(ex.getMessage(), containsString("Unknown option key")); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testEmptyVectorRejects() throws IOException { |
| 169 | + ResponseException ex = |
| 170 | + expectThrows( |
| 171 | + ResponseException.class, |
| 172 | + () -> |
| 173 | + executeQuery( |
| 174 | + "SELECT v._id FROM vectorSearch(table='t', field='f', " |
| 175 | + + "vector='[]', option='k=5') AS v")); |
| 176 | + |
| 177 | + assertThat(ex.getMessage(), containsString("must not be empty")); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testInvalidFieldNameRejects() throws IOException { |
| 182 | + ResponseException ex = |
| 183 | + expectThrows( |
| 184 | + ResponseException.class, |
| 185 | + () -> |
| 186 | + executeQuery( |
| 187 | + "SELECT v._id FROM vectorSearch(table='t', " |
| 188 | + + "field='field\\\"injection', vector='[1.0]', option='k=5') AS v")); |
| 189 | + |
| 190 | + assertThat(ex.getMessage(), containsString("Invalid field name")); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testMissingRequiredOptionRejects() throws IOException { |
| 195 | + ResponseException ex = |
| 196 | + expectThrows( |
| 197 | + ResponseException.class, |
| 198 | + () -> |
| 199 | + executeQuery( |
| 200 | + "SELECT v._id FROM vectorSearch(table='t', field='f', " |
| 201 | + + "vector='[1.0]', option='') AS v")); |
| 202 | + |
| 203 | + assertThat(ex.getMessage(), containsString("Missing required option")); |
| 204 | + } |
| 205 | +} |
0 commit comments