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