| 1 | // Copyright 2023 LanceDB Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import { describe } from "mocha"; |
| 16 | import { track } from "temp"; |
| 17 | import { assert, expect } from 'chai' |
| 18 | import * as chai from "chai"; |
| 19 | import * as chaiAsPromised from "chai-as-promised"; |
| 20 | |
| 21 | import * as lancedb from "../index"; |
| 22 | import { |
| 23 | type AwsCredentials, |
| 24 | type EmbeddingFunction, |
| 25 | MetricType, |
| 26 | Query, |
| 27 | WriteMode, |
| 28 | DefaultWriteOptions, |
| 29 | isWriteOptions, |
| 30 | type LocalTable |
| 31 | } from "../index"; |
| 32 | import { |
| 33 | FixedSizeList, |
| 34 | Field, |
| 35 | Int32, |
| 36 | makeVector, |
| 37 | Schema, |
| 38 | Utf8, |
| 39 | Table as ArrowTable, |
| 40 | vectorFromArray, |
| 41 | Float64, |
| 42 | Float32, |
| 43 | Float16, |
| 44 | Int64 |
| 45 | } from "apache-arrow"; |
| 46 | import type { RemoteRequest, RemoteResponse } from "../middleware"; |
| 47 | |
| 48 | chai.use(chaiAsPromised); |
| 49 | |
| 50 | describe("LanceDB client", function () { |
| 51 | describe("when creating a connection to lancedb", function () { |
| 52 | it("should have a valid url", async function () { |
| 53 | const uri = await createTestDB(); |
| 54 | const con = await lancedb.connect(uri); |
| 55 | assert.equal(con.uri, uri); |
| 56 | }); |
| 57 | |
| 58 | it("should accept an options object", async function () { |
| 59 | const uri = await createTestDB(); |
| 60 | const con = await lancedb.connect({ uri }); |
| 61 | assert.equal(con.uri, uri); |
| 62 | }); |
| 63 | |
| 64 | it("should accept custom aws credentials", async function () { |
| 65 | const uri = await createTestDB(); |
| 66 | const awsCredentials: AwsCredentials = { |
| 67 | accessKeyId: "", |
| 68 | secretKey: "" |
| 69 | }; |
| 70 | const con = await lancedb.connect({ |
| 71 | uri, |
| 72 | awsCredentials |
| 73 | }); |
| 74 | assert.equal(con.uri, uri); |
| 75 | }); |
| 76 | |
| 77 | it("should accept custom storage options", async function () { |
| 78 | const uri = await createTestDB(); |
| 79 | const storageOptions = { |
| 80 | region: "us-west-2", |
| 81 | timeout: "30s" |
| 82 | }; |
| 83 | const con = await lancedb.connect({ |
| 84 | uri, |
| 85 | storageOptions |
| 86 | }); |
| 87 | assert.equal(con.uri, uri); |
| 88 | }); |
| 89 | |
| 90 | it("should return the existing table names", async function () { |
| 91 | const uri = await createTestDB(); |
| 92 | const con = await lancedb.connect(uri); |
| 93 | assert.deepEqual(await con.tableNames(), ["vectors"]); |
| 94 | }); |
| 95 | |
| 96 | it("read consistency level", async function () { |
| 97 | const uri = await createTestDB(); |
| 98 | const db1 = await lancedb.connect({ uri }); |
| 99 | const table1 = await db1.openTable("vectors"); |
| 100 | |
| 101 | const db2 = await lancedb.connect({ |
| 102 | uri, |
| 103 | readConsistencyInterval: 0 |
| 104 | }) |
| 105 | const table2 = await db2.openTable("vectors"); |
| 106 | |
| 107 | assert.equal(await table2.countRows(), 2); |
| 108 | await table1.add([ |
| 109 | { |
| 110 | id: 3, |
| 111 | name: 'name_2', |
| 112 | price: 10, |
| 113 | is_active: true, |
| 114 | vector: [0, 0.1] |
| 115 | } |
| 116 | ]); |
| 117 | assert.equal(await table2.countRows(), 3); |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | describe("when querying an existing dataset", function () { |
| 122 | it("should open a table", async function () { |
| 123 | const uri = await createTestDB(); |
| 124 | const con = await lancedb.connect(uri); |
| 125 | const table = await con.openTable("vectors"); |
| 126 | assert.equal(table.name, "vectors"); |
| 127 | }); |
| 128 | |
| 129 | it("execute a query", async function () { |
| 130 | const uri = await createTestDB(); |
| 131 | const con = await lancedb.connect(uri); |
| 132 | const table = await con.openTable("vectors"); |
| 133 | const results = await table.search([0.1, 0.3]).execute(); |
| 134 | |
| 135 | assert.equal(results.length, 2); |
| 136 | assert.equal(results[0].price, 10); |
| 137 | const vector = results[0].vector as Float32Array; |
| 138 | assert.approximately(vector[0], 0.0, 0.2); |
| 139 | assert.approximately(vector[0], 0.1, 0.3); |
| 140 | }); |
| 141 | |
| 142 | it("limits # of results", async function () { |
| 143 | const uri = await createTestDB(2, 100); |
| 144 | const con = await lancedb.connect(uri); |
| 145 | const table = await con.openTable("vectors"); |
| 146 | let results = await table.search([0.1, 0.3]).limit(1).execute(); |
| 147 | assert.equal(results.length, 1); |
| 148 | assert.equal(results[0].id, 1); |
| 149 | |
| 150 | // there is a default limit if unspecified |
| 151 | results = await table.search([0.1, 0.3]).execute(); |
| 152 | assert.equal(results.length, 10); |
| 153 | }); |
| 154 | |
| 155 | it("uses a filter / where clause without vector search", async function () { |
| 156 | // eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
| 157 | const assertResults = (results: Array<Record<string, unknown>>) => { |
| 158 | assert.equal(results.length, 50); |
| 159 | }; |
| 160 | |
| 161 | const uri = await createTestDB(2, 100); |
| 162 | const con = await lancedb.connect(uri); |
| 163 | const table = (await con.openTable("vectors")) as LocalTable; |
| 164 | let results = await table.filter("id % 2 = 0").limit(100).execute(); |
| 165 | assertResults(results); |
| 166 | results = await table.where("id % 2 = 0").limit(100).execute(); |
| 167 | assertResults(results); |
| 168 | |
| 169 | // Should reject a bad filter |
| 170 | await expect(table.filter("id % 2 = 0 AND").execute()).to.be.rejectedWith( |
| 171 | /.*sql parser error: .*/ |
| 172 | ); |
| 173 | }); |
| 174 | |
| 175 | it("uses a filter / where clause", async function () { |
| 176 | // eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
| 177 | const assertResults = (results: Array<Record<string, unknown>>) => { |
| 178 | assert.equal(results.length, 1); |
| 179 | assert.equal(results[0].id, 2); |
| 180 | }; |
| 181 | |
| 182 | const uri = await createTestDB(); |
| 183 | const con = await lancedb.connect(uri); |
| 184 | const table = await con.openTable("vectors"); |
| 185 | let results = await table.search([0.1, 0.1]).filter("id == 2").execute(); |
| 186 | assertResults(results); |
| 187 | results = await table.search([0.1, 0.1]).where("id == 2").execute(); |
| 188 | assertResults(results); |
| 189 | }); |
| 190 | |
| 191 | it("should correctly process prefilter/postfilter", async function () { |
| 192 | const uri = await createTestDB(16, 300); |
| 193 | const con = await lancedb.connect(uri); |
| 194 | const table = await con.openTable("vectors"); |
| 195 | await table.createIndex({ |
| 196 | type: "ivf_pq", |
| 197 | column: "vector", |
| 198 | num_partitions: 2, |
| 199 | max_iters: 2, |
| 200 | num_sub_vectors: 2 |
| 201 | }); |
| 202 | // post filter should return less than the limit |
| 203 | let results = await table |
| 204 | .search(new Array(16).fill(0.1)) |
| 205 | .limit(10) |
| 206 | .filter("id >= 10") |
| 207 | .prefilter(false) |
| 208 | .execute(); |
| 209 | assert.isTrue(results.length < 10); |
| 210 | |
| 211 | // pre filter should return exactly the limit |
| 212 | results = await table |
| 213 | .search(new Array(16).fill(0.1)) |
| 214 | .limit(10) |
| 215 | .filter("id >= 10") |
| 216 | .prefilter(true) |
| 217 | .execute(); |
| 218 | assert.isTrue(results.length === 10); |
| 219 | }); |
| 220 | |
| 221 | it("should allow creation and use of scalar indices", async function () { |
| 222 | const uri = await createTestDB(16, 300); |
| 223 | const con = await lancedb.connect(uri); |
| 224 | const table = await con.openTable("vectors"); |
| 225 | await table.createScalarIndex("id", true); |
| 226 | |
| 227 | // Prefiltering should still work the same |
| 228 | const results = await table |
| 229 | .search(new Array(16).fill(0.1)) |
| 230 | .limit(10) |
| 231 | .filter("id >= 10") |
| 232 | .prefilter(true) |
| 233 | .execute(); |
| 234 | assert.isTrue(results.length === 10); |
| 235 | }); |
| 236 | |
| 237 | it("select only a subset of columns", async function () { |
| 238 | const uri = await createTestDB(); |
| 239 | const con = await lancedb.connect(uri); |
| 240 | const table = await con.openTable("vectors"); |
| 241 | const results = await table |
| 242 | .search([0.1, 0.1]) |
| 243 | .select(["is_active", "vector"]) |
| 244 | .execute(); |
| 245 | assert.equal(results.length, 2); |
| 246 | // vector and _distance are always returned |
| 247 | assert.isDefined(results[0].vector); |
| 248 | assert.isDefined(results[0]._distance); |
| 249 | assert.isDefined(results[0].is_active); |
| 250 | |
| 251 | assert.isUndefined(results[0].id); |
| 252 | assert.isUndefined(results[0].name); |
| 253 | assert.isUndefined(results[0].price); |
| 254 | }); |
| 255 | }); |
| 256 | |
| 257 | describe("when creating a new dataset", function () { |
| 258 | it("create an empty table", async function () { |
| 259 | const dir = await track().mkdir("lancejs"); |
| 260 | const con = await lancedb.connect(dir); |
| 261 | |
| 262 | const schema = new Schema([ |
| 263 | new Field("id", new Int32()), |
| 264 | new Field("name", new Utf8()) |
| 265 | ]); |
| 266 | const table = await con.createTable({ |
| 267 | name: "vectors", |
| 268 | schema |
| 269 | }); |
| 270 | assert.equal(table.name, "vectors"); |
| 271 | assert.deepEqual(await con.tableNames(), ["vectors"]); |
| 272 | }); |
| 273 | |
| 274 | it("create a table with a schema and records", async function () { |
| 275 | const dir = await track().mkdir("lancejs"); |
| 276 | const con = await lancedb.connect(dir); |
| 277 | |
| 278 | const schema = new Schema([ |
| 279 | new Field("id", new Int32()), |
| 280 | new Field("name", new Utf8()), |
| 281 | new Field( |
| 282 | "vector", |
| 283 | new FixedSizeList(2, new Field("item", new Float32(), true)), |
| 284 | false |
| 285 | ) |
| 286 | ]); |
| 287 | const data = [ |
| 288 | { |
| 289 | vector: [0.5, 0.2], |
| 290 | name: "foo", |
| 291 | id: 0 |
| 292 | }, |
| 293 | { |
| 294 | vector: [0.3, 0.1], |
| 295 | name: "bar", |
| 296 | id: 1 |
| 297 | } |
| 298 | ]; |
| 299 | // even thought the keys in data is out of order it should still work |
| 300 | const table = await con.createTable({ |
| 301 | name: "vectors", |
| 302 | data, |
| 303 | schema |
| 304 | }); |
| 305 | assert.equal(table.name, "vectors"); |
| 306 | assert.deepEqual(await con.tableNames(), ["vectors"]); |
| 307 | }); |
| 308 | |
| 309 | it("create a table with a empty data array", async function () { |
| 310 | const dir = await track().mkdir("lancejs"); |
| 311 | const con = await lancedb.connect(dir); |
| 312 | |
| 313 | const schema = new Schema([ |
| 314 | new Field("id", new Int32()), |
| 315 | new Field("name", new Utf8()) |
| 316 | ]); |
| 317 | const table = await con.createTable({ |
| 318 | name: "vectors", |
| 319 | schema, |
| 320 | data: [] |
| 321 | }); |
| 322 | assert.equal(table.name, "vectors"); |
| 323 | assert.deepEqual(await con.tableNames(), ["vectors"]); |
| 324 | }); |
| 325 | |
| 326 | it("create a table from an Arrow Table", async function () { |
| 327 | const dir = await track().mkdir("lancejs"); |
| 328 | // Also test the connect function with an object |
| 329 | const con = await lancedb.connect({ uri: dir }); |
| 330 | |
| 331 | const i32s = new Int32Array(new Array<number>(10)); |
| 332 | const i32 = makeVector(i32s); |
| 333 | |
| 334 | const data = new ArrowTable({ vector: i32 }); |
| 335 | |
| 336 | const table = await con.createTable({ |
| 337 | name: "vectors", |
| 338 | data |
| 339 | }); |
| 340 | assert.equal(table.name, "vectors"); |
| 341 | assert.equal(await table.countRows(), 10); |
| 342 | assert.equal(await table.countRows("vector IS NULL"), 0); |
| 343 | assert.deepEqual(await con.tableNames(), ["vectors"]); |
| 344 | }); |
| 345 | |
| 346 | it("creates a new table from javascript objects", async function () { |
| 347 | const dir = await track().mkdir("lancejs"); |
| 348 | const con = await lancedb.connect(dir); |
| 349 | |
| 350 | const data = [ |
| 351 | { id: 1, vector: [0.1, 0.2], price: 10 }, |
| 352 | { |
| 353 | id: 2, |
| 354 | vector: [1.1, 1.2], |
| 355 | price: 50 |
| 356 | } |
| 357 | ]; |
| 358 | |
| 359 | const tableName = `vectors_${Math.floor(Math.random() * 100)}`; |
| 360 | const table = await con.createTable(tableName, data); |
| 361 | assert.equal(table.name, tableName); |
| 362 | assert.equal(await table.countRows(), 2); |
| 363 | }); |
| 364 | |
| 365 | it("creates a new table from javascript objects with variable sized list", async function () { |
| 366 | const dir = await track().mkdir("lancejs"); |
| 367 | const con = await lancedb.connect(dir); |
| 368 | |
| 369 | const data = [ |
| 370 | { |
| 371 | id: 1, |
| 372 | vector: [0.1, 0.2], |
| 373 | list_of_str: ["a", "b", "c"], |
| 374 | list_of_num: [1, 2, 3] |
| 375 | }, |
| 376 | { |
| 377 | id: 2, |
| 378 | vector: [1.1, 1.2], |
| 379 | list_of_str: ["x", "y"], |
| 380 | list_of_num: [4, 5, 6] |
| 381 | } |
| 382 | ]; |
| 383 | |
| 384 | const tableName = "with_variable_sized_list"; |
| 385 | const table = (await con.createTable(tableName, data)) as LocalTable; |
| 386 | assert.equal(table.name, tableName); |
| 387 | assert.equal(await table.countRows(), 2); |
| 388 | const rs = await table.filter("id>1").execute(); |
| 389 | assert.equal(rs.length, 1); |
| 390 | assert.deepEqual(rs[0].list_of_str, ["x", "y"]); |
| 391 | assert.isTrue(rs[0].list_of_num instanceof Array); |
| 392 | }); |
| 393 | |
| 394 | it("create table from arrow table", async () => { |
| 395 | const dim = 128; |
| 396 | const total = 256; |
| 397 | const dir = await track().mkdir("lancejs"); |
| 398 | const con = await lancedb.connect(dir); |
| 399 | |
| 400 | const schema = new Schema([ |
| 401 | new Field("id", new Int32()), |
| 402 | new Field( |
| 403 | "vector", |
| 404 | new FixedSizeList(dim, new Field("item", new Float16(), true)), |
| 405 | false |
| 406 | ) |
| 407 | ]); |
| 408 | const data = lancedb.makeArrowTable( |
| 409 | Array.from(Array(total), (_, i) => ({ |
| 410 | id: i, |
| 411 | vector: Array.from(Array(dim), Math.random) |
| 412 | })), |
| 413 | { schema } |
| 414 | ); |
| 415 | const table = await con.createTable("f16", data); |
| 416 | assert.equal(table.name, "f16"); |
| 417 | assert.equal(await table.countRows(), total); |
| 418 | assert.equal(await table.countRows("id < 5"), 5); |
| 419 | assert.deepEqual(await con.tableNames(), ["f16"]); |
| 420 | assert.deepEqual(await table.schema, schema); |
| 421 | |
| 422 | await table.createIndex({ |
| 423 | num_sub_vectors: 2, |
| 424 | num_partitions: 2, |
| 425 | type: "ivf_pq" |
| 426 | }); |
| 427 | |
| 428 | const q = Array.from(Array(dim), Math.random); |
| 429 | const r = await table.search(q).limit(5).execute(); |
| 430 | assert.equal(r.length, 5); |
| 431 | r.forEach((v) => { |
| 432 | assert.equal(Object.prototype.hasOwnProperty.call(v, "vector"), true); |
| 433 | assert.equal( |
| 434 | v.vector?.constructor.name, |
| 435 | "Array", |
| 436 | "vector column is list of floats" |
| 437 | ); |
| 438 | }); |
| 439 | }).timeout(120000); |
| 440 | |
| 441 | it("use overwrite flag to overwrite existing table", async function () { |
| 442 | const dir = await track().mkdir("lancejs"); |
| 443 | const con = await lancedb.connect(dir); |
| 444 | |
| 445 | const data = [ |
| 446 | { id: 1, vector: [0.1, 0.2], price: 10 }, |
| 447 | { |
| 448 | id: 2, |
| 449 | vector: [1.1, 1.2], |
| 450 | price: 50 |
| 451 | } |
| 452 | ]; |
| 453 | |
| 454 | const tableName = "overwrite"; |
| 455 | await con.createTable(tableName, data, { writeMode: WriteMode.Create }); |
| 456 | |
| 457 | const newData = [ |
| 458 | { id: 1, vector: [0.1, 0.2], price: 10 }, |
| 459 | { id: 2, vector: [1.1, 1.2], price: 50 }, |
| 460 | { |
| 461 | id: 3, |
| 462 | vector: [1.1, 1.2], |
| 463 | price: 50 |
| 464 | } |
| 465 | ]; |
| 466 | |
| 467 | await expect(con.createTable(tableName, newData)).to.be.rejectedWith( |
| 468 | Error, |
| 469 | "already exists" |
| 470 | ); |
| 471 | |
| 472 | const table = await con.createTable(tableName, newData, { |
| 473 | writeMode: WriteMode.Overwrite |
| 474 | }); |
| 475 | assert.equal(table.name, tableName); |
| 476 | assert.equal(await table.countRows(), 3); |
| 477 | }); |
| 478 | |
| 479 | it("appends records to an existing table ", async function () { |
| 480 | const dir = await track().mkdir("lancejs"); |
| 481 | const con = await lancedb.connect(dir); |
| 482 | |
| 483 | const data = [ |
| 484 | { |
| 485 | id: 1, |
| 486 | vector: [0.1, 0.2], |
| 487 | price: 10, |
| 488 | name: "a" |
| 489 | }, |
| 490 | { |
| 491 | id: 2, |
| 492 | vector: [1.1, 1.2], |
| 493 | price: 50, |
| 494 | name: "b" |
| 495 | } |
| 496 | ]; |
| 497 | |
| 498 | const table = await con.createTable("vectors", data); |
| 499 | assert.equal(await table.countRows(), 2); |
| 500 | |
| 501 | const dataAdd = [ |
| 502 | { |
| 503 | id: 3, |
| 504 | vector: [2.1, 2.2], |
| 505 | price: 10, |
| 506 | name: "c" |
| 507 | }, |
| 508 | { |
| 509 | id: 4, |
| 510 | vector: [3.1, 3.2], |
| 511 | price: 50, |
| 512 | name: "d" |
| 513 | } |
| 514 | ]; |
| 515 | await table.add(dataAdd); |
| 516 | assert.equal(await table.countRows(), 4); |
| 517 | }); |
| 518 | |
| 519 | it("appends records with fields in a different order", async function () { |
| 520 | const dir = await track().mkdir("lancejs"); |
| 521 | const con = await lancedb.connect(dir); |
| 522 | |
| 523 | const data = [ |
| 524 | { |
| 525 | id: 1, |
| 526 | vector: [0.1, 0.2], |
| 527 | price: 10, |
| 528 | name: "a" |
| 529 | }, |
| 530 | { |
| 531 | id: 2, |
| 532 | vector: [1.1, 1.2], |
| 533 | price: 50, |
| 534 | name: "b" |
| 535 | } |
| 536 | ]; |
| 537 | |
| 538 | const table = await con.createTable("vectors", data); |
| 539 | |
| 540 | const dataAdd = [ |
| 541 | { |
| 542 | id: 3, |
| 543 | vector: [2.1, 2.2], |
| 544 | name: "c", |
| 545 | price: 10 |
| 546 | }, |
| 547 | { |
| 548 | id: 4, |
| 549 | vector: [3.1, 3.2], |
| 550 | name: "d", |
| 551 | price: 50 |
| 552 | } |
| 553 | ]; |
| 554 | await table.add(dataAdd); |
| 555 | assert.equal(await table.countRows(), 4); |
| 556 | }); |
| 557 | |
| 558 | it("overwrite all records in a table", async function () { |
| 559 | const uri = await createTestDB(); |
| 560 | const con = await lancedb.connect(uri); |
| 561 | |
| 562 | const table = await con.openTable("vectors"); |
| 563 | assert.equal(await table.countRows(), 2); |
| 564 | |
| 565 | const dataOver = [ |
| 566 | { |
| 567 | vector: [2.1, 2.2], |
| 568 | price: 10, |
| 569 | name: "foo" |
| 570 | }, |
| 571 | { |
| 572 | vector: [3.1, 3.2], |
| 573 | price: 50, |
| 574 | name: "bar" |
| 575 | } |
| 576 | ]; |
| 577 | await table.overwrite(dataOver); |
| 578 | assert.equal(await table.countRows(), 2); |
| 579 | }); |
| 580 | |
| 581 | it("can merge insert records into the table", async function () { |
| 582 | const dir = await track().mkdir("lancejs"); |
| 583 | const con = await lancedb.connect(dir); |
| 584 | |
| 585 | const data = [ |
| 586 | { id: 1, age: 1 }, |
| 587 | { id: 2, age: 1 } |
| 588 | ]; |
| 589 | const table = await con.createTable("my_table", data); |
| 590 | |
| 591 | // insert if not exists |
| 592 | let newData = [ |
| 593 | { id: 2, age: 2 }, |
| 594 | { id: 3, age: 2 } |
| 595 | ]; |
| 596 | await table.mergeInsert("id", newData, { |
| 597 | whenNotMatchedInsertAll: true |
| 598 | }); |
| 599 | assert.equal(await table.countRows(), 3); |
| 600 | assert.equal(await table.countRows("age = 2"), 1); |
| 601 | |
| 602 | // conditional update |
| 603 | newData = [ |
| 604 | { id: 2, age: 3 }, |
| 605 | { id: 3, age: 3 } |
| 606 | ]; |
| 607 | await table.mergeInsert("id", newData, { |
| 608 | whenMatchedUpdateAll: "target.age = 1" |
| 609 | }); |
| 610 | assert.equal(await table.countRows(), 3); |
| 611 | assert.equal(await table.countRows("age = 1"), 1); |
| 612 | assert.equal(await table.countRows("age = 3"), 1); |
| 613 | |
| 614 | newData = [ |
| 615 | { id: 3, age: 4 }, |
| 616 | { id: 4, age: 4 } |
| 617 | ]; |
| 618 | await table.mergeInsert("id", newData, { |
| 619 | whenNotMatchedInsertAll: true, |
| 620 | whenMatchedUpdateAll: true |
| 621 | }); |
| 622 | assert.equal(await table.countRows(), 4); |
| 623 | assert.equal((await table.filter("age = 4").execute()).length, 2); |
| 624 | |
| 625 | newData = [{ id: 5, age: 5 }]; |
| 626 | await table.mergeInsert("id", newData, { |
| 627 | whenNotMatchedInsertAll: true, |
| 628 | whenMatchedUpdateAll: true, |
| 629 | whenNotMatchedBySourceDelete: "age < 4" |
| 630 | }); |
| 631 | assert.equal(await table.countRows(), 3); |
| 632 | |
| 633 | await table.mergeInsert("id", newData, { |
| 634 | whenNotMatchedInsertAll: true, |
| 635 | whenMatchedUpdateAll: true, |
| 636 | whenNotMatchedBySourceDelete: true |
| 637 | }); |
| 638 | assert.equal(await table.countRows(), 1); |
| 639 | }); |
| 640 | |
| 641 | it("can update records in the table", async function () { |
| 642 | const uri = await createTestDB(); |
| 643 | const con = await lancedb.connect(uri); |
| 644 | |
| 645 | const table = await con.openTable("vectors"); |
| 646 | assert.equal(await table.countRows(), 2); |
| 647 | |
| 648 | await table.update({ |
| 649 | where: "price = 10", |
| 650 | valuesSql: { price: "100" } |
| 651 | }); |
| 652 | const results = await table.search([0.1, 0.2]).execute(); |
| 653 | assert.equal(results[0].price, 100); |
| 654 | assert.equal(results[1].price, 11); |
| 655 | }); |
| 656 | |
| 657 | it("can update the records using a literal value", async function () { |
| 658 | const uri = await createTestDB(); |
| 659 | const con = await lancedb.connect(uri); |
| 660 | |
| 661 | const table = await con.openTable("vectors"); |
| 662 | assert.equal(await table.countRows(), 2); |
| 663 | |
| 664 | await table.update({ |
| 665 | where: "price = 10", |
| 666 | values: { price: 100 } |
| 667 | }); |
| 668 | const results = await table.search([0.1, 0.2]).execute(); |
| 669 | assert.equal(results[0].price, 100); |
| 670 | assert.equal(results[1].price, 11); |
| 671 | }); |
| 672 | |
| 673 | it("can update every record in the table", async function () { |
| 674 | const uri = await createTestDB(); |
| 675 | const con = await lancedb.connect(uri); |
| 676 | |
| 677 | const table = await con.openTable("vectors"); |
| 678 | assert.equal(await table.countRows(), 2); |
| 679 | |
| 680 | await table.update({ valuesSql: { price: "100" } }); |
| 681 | const results = await table.search([0.1, 0.2]).execute(); |
| 682 | |
| 683 | assert.equal(results[0].price, 100); |
| 684 | assert.equal(results[1].price, 100); |
| 685 | }); |
| 686 | |
| 687 | it("can delete records from a table", async function () { |
| 688 | const uri = await createTestDB(); |
| 689 | const con = await lancedb.connect(uri); |
| 690 | |
| 691 | const table = await con.openTable("vectors"); |
| 692 | assert.equal(await table.countRows(), 2); |
| 693 | |
| 694 | await table.delete("price = 10"); |
| 695 | assert.equal(await table.countRows(), 1); |
| 696 | }); |
| 697 | it("can manually provide embedding columns", async function () { |
| 698 | const uri = await createTestDB(); |
| 699 | const con = await lancedb.connect(uri); |
| 700 | const schema = new Schema([ |
| 701 | new Field("id", new Int32()), |
| 702 | new Field("text", new Utf8()), |
| 703 | new Field( |
| 704 | "vector", |
| 705 | new FixedSizeList(2, new Field("item", new Float32(), true)) |
| 706 | ) |
| 707 | ]); |
| 708 | const data = [ |
| 709 | { id: 1, text: "foo", vector: [0.1, 0.2] }, |
| 710 | { id: 2, text: "bar", vector: [0.3, 0.4] } |
| 711 | ]; |
| 712 | let table = await con.createTable({ |
| 713 | name: "embed_vectors", |
| 714 | data, |
| 715 | schema |
| 716 | }); |
| 717 | assert.equal(table.name, "embed_vectors"); |
| 718 | table = await con.openTable("embed_vectors"); |
| 719 | assert.equal(await table.countRows(), 2); |
| 720 | }); |
| 721 | |
| 722 | it("will error if no implementation for embedding column found", async function () { |
| 723 | const uri = await createTestDB(); |
| 724 | const con = await lancedb.connect(uri); |
| 725 | const schema = new Schema([ |
| 726 | new Field("id", new Int32()), |
| 727 | new Field("text", new Utf8()), |
| 728 | new Field( |
| 729 | "vector", |
| 730 | new FixedSizeList(2, new Field("item", new Float32(), true)) |
| 731 | ) |
| 732 | ]); |
| 733 | const data = [ |
| 734 | { id: 1, text: "foo" }, |
| 735 | { id: 2, text: "bar" } |
| 736 | ]; |
| 737 | |
| 738 | const table = con.createTable({ |
| 739 | name: "embed_vectors", |
| 740 | data, |
| 741 | schema |
| 742 | }); |
| 743 | await assert.isRejected(table); |
| 744 | }); |
| 745 | }); |
| 746 | |
| 747 | describe("when searching an empty dataset", function () { |
| 748 | it("should not fail", async function () { |
| 749 | const dir = await track().mkdir("lancejs"); |
| 750 | const con = await lancedb.connect(dir); |
| 751 | |
| 752 | const schema = new Schema([ |
| 753 | new Field( |
| 754 | "vector", |
| 755 | new FixedSizeList(128, new Field("float32", new Float32())) |
| 756 | ) |
| 757 | ]); |
| 758 | const table = await con.createTable({ |
| 759 | name: "vectors", |
| 760 | schema |
| 761 | }); |
| 762 | const result = await table.search(Array(128).fill(0.1)).execute(); |
| 763 | assert.isEmpty(result); |
| 764 | }); |
| 765 | }); |
| 766 | |
| 767 | describe("when searching an empty-after-delete dataset", function () { |
| 768 | it("should not fail", async function () { |
| 769 | const dir = await track().mkdir("lancejs"); |
| 770 | const con = await lancedb.connect(dir); |
| 771 | |
| 772 | const schema = new Schema([ |
| 773 | new Field( |
| 774 | "vector", |
| 775 | new FixedSizeList(128, new Field("float32", new Float32())) |
| 776 | ) |
| 777 | ]); |
| 778 | const table = await con.createTable({ |
| 779 | name: "vectors", |
| 780 | schema |
| 781 | }); |
| 782 | await table.add([{ vector: Array(128).fill(0.1) }]); |
| 783 | // https://github.com/lancedb/lance/issues/1635 |
| 784 | await table.delete("true"); |
| 785 | const result = await table.search(Array(128).fill(0.1)).execute(); |
| 786 | assert.isEmpty(result); |
| 787 | }); |
| 788 | }); |
| 789 | |
| 790 | describe("when creating a vector index", function () { |
| 791 | it("overwrite all records in a table", async function () { |
| 792 | const uri = await createTestDB(32, 300); |
| 793 | const con = await lancedb.connect(uri); |
| 794 | const table = await con.openTable("vectors"); |
| 795 | await table.createIndex({ |
| 796 | type: "ivf_pq", |
| 797 | column: "vector", |
| 798 | num_partitions: 2, |
| 799 | max_iters: 2, |
| 800 | num_sub_vectors: 2 |
| 801 | }); |
| 802 | }).timeout(10_000); // Timeout is high partially because GH macos runner is pretty slow |
| 803 | |
| 804 | it("replace an existing index", async function () { |
| 805 | const uri = await createTestDB(16, 300); |
| 806 | const con = await lancedb.connect(uri); |
| 807 | const table = await con.openTable("vectors"); |
| 808 | |
| 809 | await table.createIndex({ |
| 810 | type: "ivf_pq", |
| 811 | column: "vector", |
| 812 | num_partitions: 2, |
| 813 | max_iters: 2, |
| 814 | num_sub_vectors: 2 |
| 815 | }); |
| 816 | |
| 817 | // Replace should fail if the index already exists |
| 818 | await expect( |
| 819 | table.createIndex({ |
| 820 | type: "ivf_pq", |
| 821 | column: "vector", |
| 822 | num_partitions: 2, |
| 823 | max_iters: 2, |
| 824 | num_sub_vectors: 2, |
| 825 | replace: false |
| 826 | }) |
| 827 | ).to.be.rejectedWith("LanceError(Index)"); |
| 828 | |
| 829 | // Default replace = true |
| 830 | await table.createIndex({ |
| 831 | type: "ivf_pq", |
| 832 | column: "vector", |
| 833 | num_partitions: 2, |
| 834 | max_iters: 2, |
| 835 | num_sub_vectors: 2 |
| 836 | }); |
| 837 | }).timeout(50_000); |
| 838 | |
| 839 | it("it should fail when the column is not a vector", async function () { |
| 840 | const uri = await createTestDB(32, 300); |
| 841 | const con = await lancedb.connect(uri); |
| 842 | const table = await con.openTable("vectors"); |
| 843 | const createIndex = table.createIndex({ |
| 844 | type: "ivf_pq", |
| 845 | column: "name", |
| 846 | num_partitions: 2, |
| 847 | max_iters: 2, |
| 848 | num_sub_vectors: 2 |
| 849 | }); |
| 850 | await expect(createIndex).to.be.rejectedWith( |
| 851 | "index cannot be created on the column `name` which has data type Utf8" |
| 852 | ); |
| 853 | }); |
| 854 | |
| 855 | it("it should fail when num_partitions is invalid", async function () { |
| 856 | const uri = await createTestDB(32, 300); |
| 857 | const con = await lancedb.connect(uri); |
| 858 | const table = await con.openTable("vectors"); |
| 859 | const createIndex = table.createIndex({ |
| 860 | type: "ivf_pq", |
| 861 | column: "name", |
| 862 | num_partitions: -1, |
| 863 | max_iters: 2, |
| 864 | num_sub_vectors: 2 |
| 865 | }); |
| 866 | await expect(createIndex).to.be.rejectedWith( |
| 867 | "num_partitions: must be > 0" |
| 868 | ); |
| 869 | }); |
| 870 | |
| 871 | it("should be able to list index and stats", async function () { |
| 872 | const uri = await createTestDB(32, 300); |
| 873 | const con = await lancedb.connect(uri); |
| 874 | const table = await con.openTable("vectors"); |
| 875 | await table.createIndex({ |
| 876 | type: "ivf_pq", |
| 877 | column: "vector", |
| 878 | num_partitions: 2, |
| 879 | max_iters: 2, |
| 880 | num_sub_vectors: 2 |
| 881 | }); |
| 882 | |
| 883 | const indices = await table.listIndices(); |
| 884 | expect(indices).to.have.lengthOf(1); |
| 885 | expect(indices[0].name).to.equal("vector_idx"); |
| 886 | expect(indices[0].uuid).to.not.be.equal(undefined); |
| 887 | expect(indices[0].columns).to.have.lengthOf(1); |
| 888 | expect(indices[0].columns[0]).to.equal("vector"); |
| 889 | |
| 890 | const stats = await table.indexStats(indices[0].name); |
| 891 | expect(stats.numIndexedRows).to.equal(300); |
| 892 | expect(stats.numUnindexedRows).to.equal(0); |
| 893 | expect(stats.indexType).to.equal("IVF_PQ"); |
| 894 | expect(stats.distanceType).to.equal("l2"); |
| 895 | expect(stats.numIndices).to.equal(1); |
| 896 | }).timeout(50_000); |
| 897 | }); |
| 898 | |
| 899 | describe("when using a custom embedding function", function () { |
| 900 | class TextEmbedding implements EmbeddingFunction<string> { |
| 901 | sourceColumn: string; |
| 902 | |
| 903 | constructor(targetColumn: string) { |
| 904 | this.sourceColumn = targetColumn; |
| 905 | } |
| 906 | |
| 907 | _embedding_map = new Map<string, number[]>([ |
| 908 | ["foo", [2.1, 2.2]], |
| 909 | ["bar", [3.1, 3.2]] |
| 910 | ]); |
| 911 | |
| 912 | async embed(data: string[]): Promise<number[][]> { |
| 913 | return data.map( |
| 914 | (datum) => this._embedding_map.get(datum) ?? [0.0, 0.0] |
| 915 | ); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | it("should encode the original data into embeddings", async function () { |
| 920 | const dir = await track().mkdir("lancejs"); |
| 921 | const con = await lancedb.connect(dir); |
| 922 | const embeddings = new TextEmbedding("name"); |
| 923 | |
| 924 | const data = [ |
| 925 | { |
| 926 | price: 10, |
| 927 | name: "foo" |
| 928 | }, |
| 929 | { |
| 930 | price: 50, |
| 931 | name: "bar" |
| 932 | } |
| 933 | ]; |
| 934 | const table = await con.createTable("vectors", data, embeddings, { |
| 935 | writeMode: WriteMode.Create |
| 936 | }); |
| 937 | const results = await table.search("foo").execute(); |
| 938 | assert.equal(results.length, 2); |
| 939 | }); |
| 940 | |
| 941 | it("should create embeddings for Arrow Table", async function () { |
| 942 | const dir = await track().mkdir("lancejs"); |
| 943 | const con = await lancedb.connect(dir); |
| 944 | const embeddingFunction = new TextEmbedding("name"); |
| 945 | |
| 946 | const names = vectorFromArray(["foo", "bar"], new Utf8()); |
| 947 | const data = new ArrowTable({ name: names }); |
| 948 | |
| 949 | const table = await con.createTable({ |
| 950 | name: "vectors", |
| 951 | data, |
| 952 | embeddingFunction |
| 953 | }); |
| 954 | assert.equal(table.name, "vectors"); |
| 955 | const results = await table.search("foo").execute(); |
| 956 | assert.equal(results.length, 2); |
| 957 | }); |
| 958 | }); |
| 959 | |
| 960 | describe("when inspecting the schema", function () { |
| 961 | it("should return the schema", async function () { |
| 962 | const uri = await createTestDB(); |
| 963 | const db = await lancedb.connect(uri); |
| 964 | // the fsl inner field must be named 'item' and be nullable |
| 965 | const expectedSchema = new Schema([ |
| 966 | new Field("id", new Int32()), |
| 967 | new Field( |
| 968 | "vector", |
| 969 | new FixedSizeList(128, new Field("item", new Float32(), true)) |
| 970 | ), |
| 971 | new Field("s", new Utf8()) |
| 972 | ]); |
| 973 | const table = await db.createTable({ |
| 974 | name: "some_table", |
| 975 | schema: expectedSchema |
| 976 | }); |
| 977 | const schema = await table.schema; |
| 978 | assert.deepEqual(expectedSchema, schema); |
| 979 | }); |
| 980 | }); |
| 981 | }); |
| 982 | |
| 983 | describe("Remote LanceDB client", function () { |
| 984 | describe("when the server is not reachable", function () { |
| 985 | it("produces a network error", async function () { |
| 986 | const con = await lancedb.connect({ |
| 987 | uri: "db://test-1234", |
| 988 | region: "asdfasfasfdf", |
| 989 | apiKey: "some-api-key" |
| 990 | }); |
| 991 | |
| 992 | // GET |
| 993 | try { |
| 994 | await con.tableNames(); |
| 995 | } catch (err) { |
| 996 | expect(err).to.have.property( |
| 997 | "message", |
| 998 | "Network Error: getaddrinfo ENOTFOUND test-1234.asdfasfasfdf.api.lancedb.com" |
| 999 | ); |
| 1000 | } |
| 1001 | |
| 1002 | // POST |
| 1003 | try { |
| 1004 | await con.createTable({ |
| 1005 | name: "vectors", |
| 1006 | schema: new Schema([]) |
| 1007 | }); |
| 1008 | } catch (err) { |
| 1009 | expect(err).to.have.property( |
| 1010 | "message", |
| 1011 | "Network Error: getaddrinfo ENOTFOUND test-1234.asdfasfasfdf.api.lancedb.com" |
| 1012 | ); |
| 1013 | } |
| 1014 | |
| 1015 | // Search |
| 1016 | const table = await con |
| 1017 | .withMiddleware( |
| 1018 | new (class { |
| 1019 | async onRemoteRequest( |
| 1020 | req: RemoteRequest, |
| 1021 | next: (req: RemoteRequest) => Promise<RemoteResponse> |
| 1022 | ) { |
| 1023 | // intercept call to check if the table exists and make the call succeed |
| 1024 | if (req.uri.endsWith("/describe/")) { |
| 1025 | return { |
| 1026 | status: 200, |
| 1027 | statusText: "OK", |
| 1028 | headers: new Map(), |
| 1029 | body: async () => ({}) |
| 1030 | }; |
| 1031 | } |
| 1032 | |
| 1033 | return await next(req); |
| 1034 | } |
| 1035 | })() |
| 1036 | ) |
| 1037 | .openTable("vectors"); |
| 1038 | |
| 1039 | try { |
| 1040 | await table.search([0.1, 0.3]).execute(); |
| 1041 | } catch (err) { |
| 1042 | expect(err).to.have.property( |
| 1043 | "message", |
| 1044 | "Network Error: getaddrinfo ENOTFOUND test-1234.asdfasfasfdf.api.lancedb.com" |
| 1045 | ); |
| 1046 | } |
| 1047 | }); |
| 1048 | }); |
| 1049 | }); |
| 1050 | |
| 1051 | describe("Query object", function () { |
| 1052 | it("sets custom parameters", async function () { |
| 1053 | const query = new Query([0.1, 0.3]) |
| 1054 | .limit(1) |
| 1055 | .metricType(MetricType.Cosine) |
| 1056 | .refineFactor(100) |
| 1057 | .select(["a", "b"]) |
| 1058 | .nprobes(20) as Record<string, any>; |
| 1059 | assert.equal(query._limit, 1); |
| 1060 | assert.equal(query._metricType, MetricType.Cosine); |
| 1061 | assert.equal(query._refineFactor, 100); |
| 1062 | assert.equal(query._nprobes, 20); |
| 1063 | assert.deepEqual(query._select, ["a", "b"]); |
| 1064 | }); |
| 1065 | }); |
| 1066 | |
| 1067 | async function createTestDB( |
| 1068 | numDimensions: number = 2, |
| 1069 | numRows: number = 2 |
| 1070 | ): Promise<string> { |
| 1071 | const dir = await track().mkdir("lancejs"); |
| 1072 | const con = await lancedb.connect(dir); |
| 1073 | |
| 1074 | const data = []; |
| 1075 | for (let i = 0; i < numRows; i++) { |
| 1076 | const vector = []; |
| 1077 | for (let j = 0; j < numDimensions; j++) { |
| 1078 | vector.push(i + j * 0.1); |
| 1079 | } |
| 1080 | data.push({ |
| 1081 | id: i + 1, |
| 1082 | name: `name_${i}`, |
| 1083 | price: i + 10, |
| 1084 | is_active: i % 2 === 0, |
| 1085 | vector |
| 1086 | }); |
| 1087 | } |
| 1088 | |
| 1089 | await con.createTable("vectors", data); |
| 1090 | return dir; |
| 1091 | } |
| 1092 | |
| 1093 | describe("Drop table", function () { |
| 1094 | it("drop a table", async function () { |
| 1095 | const dir = await track().mkdir("lancejs"); |
| 1096 | const con = await lancedb.connect(dir); |
| 1097 | |
| 1098 | const data = [ |
| 1099 | { |
| 1100 | price: 10, |
| 1101 | name: "foo", |
| 1102 | vector: [1, 2, 3] |
| 1103 | }, |
| 1104 | { |
| 1105 | price: 50, |
| 1106 | name: "bar", |
| 1107 | vector: [4, 5, 6] |
| 1108 | } |
| 1109 | ]; |
| 1110 | await con.createTable("t1", data); |
| 1111 | await con.createTable("t2", data); |
| 1112 | |
| 1113 | assert.deepEqual(await con.tableNames(), ["t1", "t2"]); |
| 1114 | |
| 1115 | await con.dropTable("t1"); |
| 1116 | assert.deepEqual(await con.tableNames(), ["t2"]); |
| 1117 | }); |
| 1118 | }); |
| 1119 | |
| 1120 | describe("WriteOptions", function () { |
| 1121 | context("#isWriteOptions", function () { |
| 1122 | it("should not match empty object", function () { |
| 1123 | assert.equal(isWriteOptions({}), false); |
| 1124 | }); |
| 1125 | it("should match write options", function () { |
| 1126 | assert.equal(isWriteOptions({ writeMode: WriteMode.Create }), true); |
| 1127 | }); |
| 1128 | it("should match undefined write mode", function () { |
| 1129 | assert.equal(isWriteOptions({ writeMode: undefined }), true); |
| 1130 | }); |
| 1131 | it("should match default write options", function () { |
| 1132 | assert.equal(isWriteOptions(new DefaultWriteOptions()), true); |
| 1133 | }); |
| 1134 | }); |
| 1135 | }); |
| 1136 | |
| 1137 | describe("Compact and cleanup", function () { |
| 1138 | it("can cleanup after compaction", async function () { |
| 1139 | const dir = await track().mkdir("lancejs"); |
| 1140 | const con = await lancedb.connect(dir); |
| 1141 | |
| 1142 | const data = [ |
| 1143 | { |
| 1144 | price: 10, |
| 1145 | name: "foo", |
| 1146 | vector: [1, 2, 3] |
| 1147 | }, |
| 1148 | { |
| 1149 | price: 50, |
| 1150 | name: "bar", |
| 1151 | vector: [4, 5, 6] |
| 1152 | } |
| 1153 | ]; |
| 1154 | const table = (await con.createTable("t1", data)) as LocalTable; |
| 1155 | |
| 1156 | const newData = [ |
| 1157 | { |
| 1158 | price: 30, |
| 1159 | name: "baz", |
| 1160 | vector: [7, 8, 9] |
| 1161 | } |
| 1162 | ]; |
| 1163 | await table.add(newData); |
| 1164 | |
| 1165 | const compactionMetrics = await table.compactFiles({ |
| 1166 | numThreads: 2 |
| 1167 | }); |
| 1168 | assert.equal(compactionMetrics.fragmentsRemoved, 2); |
| 1169 | assert.equal(compactionMetrics.fragmentsAdded, 1); |
| 1170 | assert.equal(await table.countRows(), 3); |
| 1171 | |
| 1172 | await table.cleanupOldVersions(); |
| 1173 | assert.equal(await table.countRows(), 3); |
| 1174 | |
| 1175 | // should have no effect, but this validates the arguments are parsed. |
| 1176 | await table.compactFiles({ |
| 1177 | targetRowsPerFragment: 102410, |
| 1178 | maxRowsPerGroup: 1024, |
| 1179 | materializeDeletions: true, |
| 1180 | materializeDeletionsThreshold: 0.5, |
| 1181 | numThreads: 2 |
| 1182 | }); |
| 1183 | |
| 1184 | const cleanupMetrics = await table.cleanupOldVersions(0, true); |
| 1185 | assert.isAtLeast(cleanupMetrics.bytesRemoved, 1); |
| 1186 | assert.isAtLeast(cleanupMetrics.oldVersions, 1); |
| 1187 | assert.equal(await table.countRows(), 3); |
| 1188 | }); |
| 1189 | }); |
| 1190 | |
| 1191 | describe("schema evolution", function () { |
| 1192 | // Create a new sample table |
| 1193 | it("can add a new column to the schema", async function () { |
| 1194 | const dir = await track().mkdir("lancejs"); |
| 1195 | const con = await lancedb.connect(dir); |
| 1196 | const table = await con.createTable("vectors", [ |
| 1197 | { id: 1n, vector: [0.1, 0.2] } |
| 1198 | ]); |
| 1199 | |
| 1200 | await table.addColumns([ |
| 1201 | { name: "price", valueSql: "cast(10.0 as float)" } |
| 1202 | ]); |
| 1203 | |
| 1204 | const expectedSchema = new Schema([ |
| 1205 | new Field("id", new Int64()), |
| 1206 | new Field( |
| 1207 | "vector", |
| 1208 | new FixedSizeList(2, new Field("item", new Float32(), true)) |
| 1209 | ), |
| 1210 | new Field("price", new Float32()) |
| 1211 | ]); |
| 1212 | expect(await table.schema).to.deep.equal(expectedSchema); |
| 1213 | }); |
| 1214 | |
| 1215 | it("can alter the columns in the schema", async function () { |
| 1216 | const dir = await track().mkdir("lancejs"); |
| 1217 | const con = await lancedb.connect(dir); |
| 1218 | const schema = new Schema([ |
| 1219 | new Field("id", new Int64(), false), |
| 1220 | new Field( |
| 1221 | "vector", |
| 1222 | new FixedSizeList(2, new Field("item", new Float32(), true)) |
| 1223 | ), |
| 1224 | new Field("price", new Float64(), false) |
| 1225 | ]); |
| 1226 | const table = await con.createTable("vectors", [ |
| 1227 | { id: 1n, vector: [0.1, 0.2], price: 10.0 } |
| 1228 | ]); |
| 1229 | expect(await table.schema).to.deep.equal(schema); |
| 1230 | |
| 1231 | await table.alterColumns([ |
| 1232 | { path: "id", rename: "new_id" }, |
| 1233 | { path: "price", nullable: true } |
| 1234 | ]); |
| 1235 | |
| 1236 | const expectedSchema = new Schema([ |
| 1237 | new Field("new_id", new Int64(), false), |
| 1238 | new Field( |
| 1239 | "vector", |
| 1240 | new FixedSizeList(2, new Field("item", new Float32(), true)) |
| 1241 | ), |
| 1242 | new Field("price", new Float64(), true) |
| 1243 | ]); |
| 1244 | expect(await table.schema).to.deep.equal(expectedSchema); |
| 1245 | }); |
| 1246 | |
| 1247 | it("can drop a column from the schema", async function () { |
| 1248 | const dir = await track().mkdir("lancejs"); |
| 1249 | const con = await lancedb.connect(dir); |
| 1250 | const table = await con.createTable("vectors", [ |
| 1251 | { id: 1n, vector: [0.1, 0.2] } |
| 1252 | ]); |
| 1253 | await table.dropColumns(["vector"]); |
| 1254 | |
| 1255 | const expectedSchema = new Schema([new Field("id", new Int64(), false)]); |
| 1256 | expect(await table.schema).to.deep.equal(expectedSchema); |
| 1257 | }); |
| 1258 | }); |