Elffuss BraidHub

kikocis/lancedb public

main · 6.3 KiBRawPermalink
1// Copyright 2024 Lance 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
15import { readdirSync } from "fs";
16import { Field, Float64, Schema } from "apache-arrow";
17import * as tmp from "tmp";
18import { Connection, Table, connect } from "../lancedb";
19import { LocalTable } from "../lancedb/table";
20
21describe("when connecting", () => {
22 let tmpDir: tmp.DirResult;
23 beforeEach(() => {
24 tmpDir = tmp.dirSync({ unsafeCleanup: true });
25 });
26 afterEach(() => tmpDir.removeCallback());
27
28 it("should connect", async () => {
29 const db = await connect(tmpDir.name);
30 expect(db.display()).toBe(
31 `NativeDatabase(uri=${tmpDir.name}, read_consistency_interval=None)`,
32 );
33 });
34
35 it("should allow read consistency interval to be specified", async () => {
36 const db = await connect(tmpDir.name, { readConsistencyInterval: 5 });
37 expect(db.display()).toBe(
38 `NativeDatabase(uri=${tmpDir.name}, read_consistency_interval=5s)`,
39 );
40 });
41});
42
43describe("given a connection", () => {
44 let tmpDir: tmp.DirResult;
45 let db: Connection;
46 beforeEach(async () => {
47 tmpDir = tmp.dirSync({ unsafeCleanup: true });
48 db = await connect(tmpDir.name);
49 });
50 afterEach(() => tmpDir.removeCallback());
51
52 it("should raise an error if opening a non-existent table", async () => {
53 await expect(db.openTable("non-existent")).rejects.toThrow("was not found");
54 });
55
56 it("should raise an error if any operation is tried after it is closed", async () => {
57 expect(db.isOpen()).toBe(true);
58 await db.close();
59 expect(db.isOpen()).toBe(false);
60 await expect(db.tableNames()).rejects.toThrow("Connection is closed");
61 });
62 it("should be able to create a table from an object arg `createTable(options)`, or args `createTable(name, data, options)`", async () => {
63 let tbl = await db.createTable("test", [{ id: 1 }, { id: 2 }]);
64 await expect(tbl.countRows()).resolves.toBe(2);
65
66 tbl = await db.createTable({
67 name: "test",
68 data: [{ id: 3 }],
69 mode: "overwrite",
70 });
71
72 await expect(tbl.countRows()).resolves.toBe(1);
73 });
74
75 it("should fail if creating table twice, unless overwrite is true", async () => {
76 let tbl = await db.createTable("test", [{ id: 1 }, { id: 2 }]);
77 await expect(tbl.countRows()).resolves.toBe(2);
78 await expect(
79 db.createTable("test", [{ id: 1 }, { id: 2 }]),
80 ).rejects.toThrow();
81 tbl = await db.createTable("test", [{ id: 3 }], { mode: "overwrite" });
82 await expect(tbl.countRows()).resolves.toBe(1);
83 });
84
85 it("should respect limit and page token when listing tables", async () => {
86 const db = await connect(tmpDir.name);
87
88 await db.createTable("b", [{ id: 1 }]);
89 await db.createTable("a", [{ id: 1 }]);
90 await db.createTable("c", [{ id: 1 }]);
91
92 let tables = await db.tableNames();
93 expect(tables).toEqual(["a", "b", "c"]);
94
95 tables = await db.tableNames({ limit: 1 });
96 expect(tables).toEqual(["a"]);
97
98 tables = await db.tableNames({ limit: 1, startAfter: "a" });
99 expect(tables).toEqual(["b"]);
100
101 tables = await db.tableNames({ startAfter: "a" });
102 expect(tables).toEqual(["b", "c"]);
103 });
104
105 it("should create tables in v2 mode", async () => {
106 const db = await connect(tmpDir.name);
107 const data = [...Array(10000).keys()].map((i) => ({ id: i }));
108
109 // Create in v1 mode
110 let table = await db.createTable("test", data, { useLegacyFormat: true });
111
112 const isV2 = async (table: Table) => {
113 const data = await table.query().toArrow({ maxBatchLength: 100000 });
114 console.log(data.batches.length);
115 return data.batches.length < 5;
116 };
117
118 await expect(isV2(table)).resolves.toBe(false);
119
120 // Create in v2 mode
121 table = await db.createTable("test_v2", data);
122
123 await expect(isV2(table)).resolves.toBe(true);
124
125 await table.add(data);
126
127 await expect(isV2(table)).resolves.toBe(true);
128
129 // Create empty in v2 mode
130 const schema = new Schema([new Field("id", new Float64(), true)]);
131
132 table = await db.createEmptyTable("test_v2_empty", schema, {
133 useLegacyFormat: false,
134 });
135
136 await table.add(data);
137 await expect(isV2(table)).resolves.toBe(true);
138 });
139
140 it("should be able to create tables with V2 manifest paths", async () => {
141 const db = await connect(tmpDir.name);
142 let table = (await db.createEmptyTable(
143 "test_manifest_paths_v2_empty",
144 new Schema([new Field("id", new Float64(), true)]),
145 {
146 enableV2ManifestPaths: true,
147 },
148 )) as LocalTable;
149 expect(await table.usesV2ManifestPaths()).toBe(true);
150
151 let manifestDir =
152 tmpDir.name + "/test_manifest_paths_v2_empty.lance/_versions";
153 readdirSync(manifestDir).forEach((file) => {
154 expect(file).toMatch(/^\d{20}\.manifest$/);
155 });
156
157 table = (await db.createTable("test_manifest_paths_v2", [{ id: 1 }], {
158 enableV2ManifestPaths: true,
159 })) as LocalTable;
160 expect(await table.usesV2ManifestPaths()).toBe(true);
161 manifestDir = tmpDir.name + "/test_manifest_paths_v2.lance/_versions";
162 readdirSync(manifestDir).forEach((file) => {
163 expect(file).toMatch(/^\d{20}\.manifest$/);
164 });
165 });
166
167 it("should be able to migrate tables to the V2 manifest paths", async () => {
168 const db = await connect(tmpDir.name);
169 const table = (await db.createEmptyTable(
170 "test_manifest_path_migration",
171 new Schema([new Field("id", new Float64(), true)]),
172 {
173 enableV2ManifestPaths: false,
174 },
175 )) as LocalTable;
176
177 expect(await table.usesV2ManifestPaths()).toBe(false);
178
179 const manifestDir =
180 tmpDir.name + "/test_manifest_path_migration.lance/_versions";
181 readdirSync(manifestDir).forEach((file) => {
182 expect(file).toMatch(/^\d\.manifest$/);
183 });
184
185 await table.migrateManifestPathsV2();
186 expect(await table.usesV2ManifestPaths()).toBe(true);
187
188 readdirSync(manifestDir).forEach((file) => {
189 expect(file).toMatch(/^\d{20}\.manifest$/);
190 });
191 });
192});