|
| 1 | +/* Copyright (c) 2025, Oracle and/or its affiliates. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * This software is dual-licensed to you under the Universal Permissive License |
| 6 | + * (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 7 | + * 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 8 | + * either license. |
| 9 | + * |
| 10 | + * If you elect to accept the software under the Apache License, Version 2.0, |
| 11 | + * the following applies: |
| 12 | + * |
| 13 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | + * you may not use this file except in compliance with the License. |
| 15 | + * You may obtain a copy of the License at |
| 16 | + * |
| 17 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 18 | + * |
| 19 | + * Unless required by applicable law or agreed to in writing, software |
| 20 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 21 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | + * See the License for the specific language governing permissions and |
| 23 | + * limitations under the License. |
| 24 | + * |
| 25 | + * NAME |
| 26 | + * vectortypesparse.js |
| 27 | + * |
| 28 | + * DESCRIPTION |
| 29 | + * Insert and query SPARSE VECTOR columns. |
| 30 | + * |
| 31 | + * |
| 32 | + *****************************************************************************/ |
| 33 | + |
| 34 | +'use strict'; |
| 35 | + |
| 36 | +Error.stackTraceLimit = 50; |
| 37 | + |
| 38 | +const oracledb = require('oracledb'); |
| 39 | +const assert = require('assert'); |
| 40 | +const dbConfig = require('./dbconfig.js'); |
| 41 | +const tableName = 'testvectorsparse'; |
| 42 | + |
| 43 | +if (process.env.NODE_ORACLEDB_DRIVER_MODE === 'thick') { |
| 44 | + let clientOpts = {}; |
| 45 | + // On Windows and macOS Intel platforms, set the environment |
| 46 | + // variable NODE_ORACLEDB_CLIENT_LIB_DIR to the Oracle Client library path |
| 47 | + if (process.platform === 'win32' || (process.platform === 'darwin' && process.arch === 'x64')) { |
| 48 | + clientOpts = { libDir: process.env.NODE_ORACLEDB_CLIENT_LIB_DIR }; |
| 49 | + } |
| 50 | + oracledb.initOracleClient(clientOpts); // enable node-oracledb Thick mode |
| 51 | +} |
| 52 | + |
| 53 | +oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT; |
| 54 | + |
| 55 | +async function run() { |
| 56 | + |
| 57 | + const connection = await oracledb.getConnection(dbConfig); |
| 58 | + |
| 59 | + try { |
| 60 | + let result; |
| 61 | + const serverVersion = connection.oracleServerVersion; |
| 62 | + if (serverVersion < 2306000000) { |
| 63 | + console.log(`DB version ${serverVersion} does not support VECTOR type`); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + console.log('Creating table'); |
| 68 | + await connection.execute(`DROP TABLE if exists ${tableName}`); |
| 69 | + await connection.execute(`CREATE TABLE ${tableName} (id NUMBER GENERATED ALWAYS AS IDENTITY, |
| 70 | + sparseF64 VECTOR(4, float64, SPARSE), sparseFlexF64 VECTOR(*, float64, SPARSE), |
| 71 | + denseF64 VECTOR(2, float64), denseFlexF64 VECTOR(*, float64))`); |
| 72 | + |
| 73 | + const arr = [39, -65]; |
| 74 | + const queryVector = new Float64Array([39, -65]); |
| 75 | + const float64arr1 = new Float64Array(arr); |
| 76 | + const float64arr2 = new Float64Array([-34, 23]); |
| 77 | + const float64arr3 = new Float64Array([-34, 23, 32, 12]); |
| 78 | + const sparseString = '[4, [1, 3], [39, -65]]'; // totalDims, indexArray, valueArray. |
| 79 | + let sparsevec = new oracledb.SparseVector({ values: float64arr1, indices: [1, 3], numDimensions: 4 }); |
| 80 | + |
| 81 | + const binds = { |
| 82 | + sparse: { type: oracledb.DB_TYPE_VECTOR, val: sparsevec }, |
| 83 | + dense: { type: oracledb.DB_TYPE_VECTOR, val: float64arr2 } |
| 84 | + }; |
| 85 | + |
| 86 | + const denseArray = sparsevec.dense(); |
| 87 | + console.log(' dense vector ', denseArray); |
| 88 | + |
| 89 | + console.log('Inserting SparseVector instance created from POJO'); |
| 90 | + result = await connection.execute(`insert into ${tableName} values(DEFAULT, :1, :2, :3, :4)`, |
| 91 | + [ |
| 92 | + sparsevec, |
| 93 | + sparsevec, |
| 94 | + float64arr1, |
| 95 | + float64arr1 |
| 96 | + ]); |
| 97 | + |
| 98 | + console.log('Inserting string data of sparse format'); |
| 99 | + result = await connection.execute(`insert into ${tableName} values(DEFAULT, :sparse, :sparse, :dense, :dense)`, |
| 100 | + [sparseString, sparseString, float64arr1, float64arr1]); |
| 101 | + |
| 102 | + console.log('Inserting SparseVector instance created from string'); |
| 103 | + sparsevec = new oracledb.SparseVector(sparseString); |
| 104 | + result = await connection.execute(`insert into ${tableName} values(DEFAULT, :1, :2, :3, :4)`, |
| 105 | + [ |
| 106 | + sparsevec, |
| 107 | + sparsevec, |
| 108 | + float64arr1, |
| 109 | + float64arr1 |
| 110 | + ]); |
| 111 | + |
| 112 | + console.log('Inserting SparseVector instance created from dense Array'); |
| 113 | + sparsevec = new oracledb.SparseVector(denseArray); |
| 114 | + result = await connection.execute(`insert into ${tableName} values(DEFAULT, :1, :2, :3, :4)`, |
| 115 | + [ |
| 116 | + sparsevec, |
| 117 | + sparsevec, |
| 118 | + float64arr1, |
| 119 | + float64arr1 |
| 120 | + ]); |
| 121 | + |
| 122 | + console.log('Inserting Dense vector into Sparse Flex dimensions column'); |
| 123 | + let sql = `insert into ${tableName} values(DEFAULT, :sparse, :dense, :dense, :dense)`; |
| 124 | + result = await connection.execute(sql, binds); |
| 125 | + |
| 126 | + console.log('Inserting Sparse vector into Dense Flex dimensions column'); |
| 127 | + sql = `insert into ${tableName} values(DEFAULT, :sparse, :sparse, :dense, :sparse)`; |
| 128 | + result = await connection.execute(sql, binds); |
| 129 | + |
| 130 | + console.log('Query Results:'); |
| 131 | + result = await connection.execute( |
| 132 | + `select * from ${tableName} ORDER BY id`); |
| 133 | + console.log("Query metadata:", result.metaData); |
| 134 | + for (const val of result.rows) { |
| 135 | + console.log("Query rows:", JSON.stringify(val)); |
| 136 | + } |
| 137 | + |
| 138 | + // Inserting Dense vector of different dimensions into Sparse Fixed dimensions column |
| 139 | + sql = `insert into ${tableName} values(DEFAULT, :dense, :sparse, :dense, :dense)`; |
| 140 | + await assert.rejects( |
| 141 | + async () => await connection.execute(sql, |
| 142 | + { |
| 143 | + sparse: { type: oracledb.DB_TYPE_VECTOR, val: sparsevec }, |
| 144 | + dense: { type: oracledb.DB_TYPE_VECTOR, val: float64arr2 } |
| 145 | + } |
| 146 | + ), |
| 147 | + /ORA-51803:/ |
| 148 | + ); |
| 149 | + |
| 150 | + // Inserting Dense vector of same dimensions into Sparse Fixed dimensions column |
| 151 | + sql = `insert into ${tableName} values(DEFAULT, :dense, :sparse, :dense, :dense)`; |
| 152 | + await assert.rejects( |
| 153 | + async () => await connection.execute(sql, |
| 154 | + { |
| 155 | + sparse: { type: oracledb.DB_TYPE_VECTOR, val: sparsevec }, |
| 156 | + dense: { type: oracledb.DB_TYPE_VECTOR, val: float64arr3 } |
| 157 | + } |
| 158 | + ), |
| 159 | + /ORA-51803:/ |
| 160 | + ); |
| 161 | + |
| 162 | + // Inserting Sparse vector into Dense Fixed dimensions column |
| 163 | + sql = `insert into ${tableName} values(DEFAULT, :sparse, :sparse, :sparse, :dense)`; |
| 164 | + await assert.rejects( |
| 165 | + async () => await connection.execute(sql, binds), |
| 166 | + /ORA-51803:/ |
| 167 | + ); |
| 168 | + |
| 169 | + const sparseQueryVec = new oracledb.SparseVector({ values: queryVector, indices: [2, 4], numDimensions: 4 }); |
| 170 | + console.log('vector distance with Query ', queryVector); |
| 171 | + console.log(await connection.execute(`select vector_distance (sparseF64, :1) from ${tableName}`, [sparseQueryVec])); |
| 172 | + |
| 173 | + } catch (err) { |
| 174 | + console.error(err); |
| 175 | + } finally { |
| 176 | + if (connection) { |
| 177 | + try { |
| 178 | + await connection.close(); |
| 179 | + } catch (err) { |
| 180 | + console.error(err); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +run(); |
0 commit comments