|
| 1 | +/*jshint globalstrict:false, strict:false, maxlen: 500 */ |
| 2 | +/*global fail, assertEqual, assertNotEqual, assertTrue, assertFalse */ |
| 3 | + |
| 4 | +// ////////////////////////////////////////////////////////////////////////////// |
| 5 | +// / DISCLAIMER |
| 6 | +// / |
| 7 | +// / Copyright 2014-2024 ArangoDB GmbH, Cologne, Germany |
| 8 | +// / Copyright 2004-2014 triAGENS GmbH, Cologne, Germany |
| 9 | +// / |
| 10 | +// / Licensed under the Business Source License 1.1 (the "License"); |
| 11 | +// / you may not use this file except in compliance with the License. |
| 12 | +// / You may obtain a copy of the License at |
| 13 | +// / |
| 14 | +// / https://github.com/arangodb/arangodb/blob/devel/LICENSE |
| 15 | +// / |
| 16 | +// / Unless required by applicable law or agreed to in writing, software |
| 17 | +// / distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +// / WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +// / See the License for the specific language governing permissions and |
| 20 | +// / limitations under the License. |
| 21 | +// / |
| 22 | +// / Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 23 | +// / |
| 24 | +/// @author Jure Bajic |
| 25 | +// ////////////////////////////////////////////////////////////////////////////// |
| 26 | + |
| 27 | +const internal = require("internal"); |
| 28 | +const jsunity = require("jsunity"); |
| 29 | +const arangodb = require("@arangodb"); |
| 30 | +const helper = require("@arangodb/aql-helper"); |
| 31 | +const errors = internal.errors; |
| 32 | +const db = internal.db; |
| 33 | +const { |
| 34 | + randomNumberGeneratorFloat, |
| 35 | + randomInteger, |
| 36 | +} = require("@arangodb/testutils/seededRandom"); |
| 37 | + |
| 38 | +const dbName = "vectorIndexHintDb"; |
| 39 | +const collName = "vectorIndexHintColl"; |
| 40 | + |
| 41 | +//////////////////////////////////////////////////////////////////////////////// |
| 42 | +/// @brief helper function to get the used vector index from a query plan |
| 43 | +//////////////////////////////////////////////////////////////////////////////// |
| 44 | +function getVectorIndexName(query, bindVars = {}) { |
| 45 | + const explain = db._createStatement({ query, bindVars }).explain(); |
| 46 | + const vectorNodes = explain.plan.nodes.filter( |
| 47 | + (node) => node.type === "EnumerateNearVectorNode" |
| 48 | + ); |
| 49 | + if (vectorNodes.length === 0) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + assertEqual(1, vectorNodes.length, "Expected exactly one EnumerateNearVectorNode"); |
| 53 | + return vectorNodes[0].index ? vectorNodes[0].index.name : null; |
| 54 | +} |
| 55 | + |
| 56 | +//////////////////////////////////////////////////////////////////////////////// |
| 57 | +/// @brief Test suite for vector index hints |
| 58 | +//////////////////////////////////////////////////////////////////////////////// |
| 59 | +function VectorIndexHintsSuite() { |
| 60 | + let collection; |
| 61 | + let randomPoint; |
| 62 | + const dimension = 128; |
| 63 | + const numberOfDocs = 100; |
| 64 | + const seed = randomInteger(); |
| 65 | + |
| 66 | + return { |
| 67 | + setUpAll: function () { |
| 68 | + db._createDatabase(dbName); |
| 69 | + db._useDatabase(dbName); |
| 70 | + |
| 71 | + collection = db._create(collName, { numberOfShards: 3 }); |
| 72 | + |
| 73 | + // Generate random vectors |
| 74 | + let docs = []; |
| 75 | + let gen = randomNumberGeneratorFloat(seed); |
| 76 | + for (let i = 0; i < numberOfDocs; ++i) { |
| 77 | + const vector = Array.from({ length: dimension }, () => gen()); |
| 78 | + const vectorCosine = Array.from({ length: dimension }, () => gen()); |
| 79 | + const vectorInnerProduct = Array.from({ length: dimension }, () => gen()); |
| 80 | + |
| 81 | + if (i === Math.floor(numberOfDocs / 2)) { |
| 82 | + randomPoint = vector; |
| 83 | + } |
| 84 | + |
| 85 | + docs.push({ |
| 86 | + _key: `doc${i}`, |
| 87 | + vector: vector, |
| 88 | + vectorCosine: vectorCosine, |
| 89 | + vectorInnerProduct: vectorInnerProduct, |
| 90 | + value: i, |
| 91 | + }); |
| 92 | + } |
| 93 | + collection.insert(docs); |
| 94 | + |
| 95 | + collection.ensureIndex({ |
| 96 | + name: "vector_l2", |
| 97 | + type: "vector", |
| 98 | + fields: ["vector"], |
| 99 | + params: { |
| 100 | + metric: "l2", |
| 101 | + dimension: dimension, |
| 102 | + nLists: 5, |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + collection.ensureIndex({ |
| 107 | + name: "vector_l2_secondary", |
| 108 | + type: "vector", |
| 109 | + fields: ["vector"], |
| 110 | + params: { |
| 111 | + metric: "l2", |
| 112 | + dimension: dimension, |
| 113 | + nLists: 3, |
| 114 | + }, |
| 115 | + }); |
| 116 | + }, |
| 117 | + |
| 118 | + tearDownAll: function () { |
| 119 | + db._useDatabase("_system"); |
| 120 | + db._dropDatabase(dbName); |
| 121 | + }, |
| 122 | + |
| 123 | + testVectorL2HintFirstIndex: function () { |
| 124 | + const query = ` |
| 125 | + FOR doc IN ${collName} OPTIONS { indexHint: "vector_l2" } |
| 126 | + SORT APPROX_NEAR_L2(doc.vector, @qp) |
| 127 | + LIMIT 5 |
| 128 | + RETURN doc |
| 129 | + `; |
| 130 | + const bindVars = { qp: randomPoint }; |
| 131 | + const indexName = getVectorIndexName(query, bindVars); |
| 132 | + |
| 133 | + assertEqual("vector_l2", indexName); |
| 134 | + }, |
| 135 | + |
| 136 | + testVectorL2HintSecondIndex: function () { |
| 137 | + const query = ` |
| 138 | + FOR doc IN ${collName} OPTIONS { indexHint: "vector_l2_secondary" } |
| 139 | + SORT APPROX_NEAR_L2(doc.vector, @qp) |
| 140 | + LIMIT 5 |
| 141 | + RETURN doc |
| 142 | + `; |
| 143 | + const bindVars = { qp: randomPoint }; |
| 144 | + const indexName = getVectorIndexName(query, bindVars); |
| 145 | + |
| 146 | + assertEqual("vector_l2_secondary", indexName); |
| 147 | + }, |
| 148 | + |
| 149 | + testVectorL2HintNonExistingIndex: function () { |
| 150 | + const query = ` |
| 151 | + FOR doc IN ${collName} OPTIONS { indexHint: "nonexistent_index" } |
| 152 | + SORT APPROX_NEAR_L2(doc.vector, @qp) |
| 153 | + LIMIT 5 |
| 154 | + RETURN doc |
| 155 | + `; |
| 156 | + const bindVars = { qp: randomPoint }; |
| 157 | + const indexName = getVectorIndexName(query, bindVars); |
| 158 | + |
| 159 | + assertTrue(["vector_l2_secondary", "vector_l2"].includes(indexName)); |
| 160 | + }, |
| 161 | + |
| 162 | + testVectorL2WithHintForced: function () { |
| 163 | + const query = ` |
| 164 | + FOR doc IN ${collName} OPTIONS { indexHint: "vector_l2_secondary", forceIndexHint: true } |
| 165 | + SORT APPROX_NEAR_L2(doc.vector, @qp) |
| 166 | + LIMIT 5 |
| 167 | + RETURN doc |
| 168 | + `; |
| 169 | + const bindVars = { qp: randomPoint }; |
| 170 | + const indexName = getVectorIndexName(query, bindVars); |
| 171 | + |
| 172 | + assertEqual("vector_l2_secondary", indexName); |
| 173 | + }, |
| 174 | + |
| 175 | + testVectorL2WithHintForcedNonExistingIndex: function () { |
| 176 | + const query = ` |
| 177 | + FOR doc IN ${collName} OPTIONS { indexHint: "nonexistent_index", forceIndexHint: true } |
| 178 | + SORT APPROX_NEAR_L2(doc.vector, @qp) |
| 179 | + LIMIT 5 |
| 180 | + RETURN doc |
| 181 | + `; |
| 182 | + const bindVars = { qp: randomPoint }; |
| 183 | + |
| 184 | + try { |
| 185 | + db._createStatement({ query, bindVars }).explain(); |
| 186 | + fail(); |
| 187 | + } catch (err) { |
| 188 | + assertEqual(errors.ERROR_QUERY_FORCED_INDEX_HINT_UNUSABLE.code, err.errorNum); |
| 189 | + } |
| 190 | + }, |
| 191 | + |
| 192 | + testVectorL2HintMultipleIndexes: function () { |
| 193 | + const query = ` |
| 194 | + FOR doc IN ${collName} OPTIONS { indexHint: ["non_existing_index", "vector_l2_secondary"] } |
| 195 | + SORT APPROX_NEAR_L2(doc.vector, @qp) |
| 196 | + LIMIT 5 |
| 197 | + RETURN doc |
| 198 | + `; |
| 199 | + const bindVars = { qp: randomPoint }; |
| 200 | + const indexName = getVectorIndexName(query, bindVars); |
| 201 | + |
| 202 | + assertEqual("vector_l2_secondary", indexName); |
| 203 | + }, |
| 204 | + }; |
| 205 | +} |
| 206 | + |
| 207 | +jsunity.run(VectorIndexHintsSuite); |
| 208 | + |
| 209 | +return jsunity.done(); |
0 commit comments