@dengxifeng/lancedb 0.26.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/AGENTS.md +13 -0
  2. package/CONTRIBUTING.md +76 -0
  3. package/README.md +37 -0
  4. package/dist/arrow.d.ts +279 -0
  5. package/dist/arrow.js +1316 -0
  6. package/dist/connection.d.ts +259 -0
  7. package/dist/connection.js +224 -0
  8. package/dist/embedding/embedding_function.d.ts +103 -0
  9. package/dist/embedding/embedding_function.js +192 -0
  10. package/dist/embedding/index.d.ts +27 -0
  11. package/dist/embedding/index.js +101 -0
  12. package/dist/embedding/openai.d.ts +16 -0
  13. package/dist/embedding/openai.js +93 -0
  14. package/dist/embedding/registry.d.ts +74 -0
  15. package/dist/embedding/registry.js +165 -0
  16. package/dist/embedding/transformers.d.ts +36 -0
  17. package/dist/embedding/transformers.js +122 -0
  18. package/dist/header.d.ts +162 -0
  19. package/dist/header.js +217 -0
  20. package/dist/index.d.ts +85 -0
  21. package/dist/index.js +106 -0
  22. package/dist/indices.d.ts +692 -0
  23. package/dist/indices.js +156 -0
  24. package/dist/merge.d.ts +80 -0
  25. package/dist/merge.js +92 -0
  26. package/dist/native.d.ts +585 -0
  27. package/dist/native.js +339 -0
  28. package/dist/permutation.d.ts +143 -0
  29. package/dist/permutation.js +184 -0
  30. package/dist/query.d.ts +581 -0
  31. package/dist/query.js +853 -0
  32. package/dist/rerankers/index.d.ts +5 -0
  33. package/dist/rerankers/index.js +19 -0
  34. package/dist/rerankers/rrf.d.ts +14 -0
  35. package/dist/rerankers/rrf.js +28 -0
  36. package/dist/sanitize.d.ts +32 -0
  37. package/dist/sanitize.js +473 -0
  38. package/dist/table.d.ts +581 -0
  39. package/dist/table.js +321 -0
  40. package/dist/util.d.ts +14 -0
  41. package/dist/util.js +77 -0
  42. package/license_header.txt +2 -0
  43. package/package.json +122 -0
@@ -0,0 +1,5 @@
1
+ import { RecordBatch } from "apache-arrow";
2
+ export * from "./rrf";
3
+ export interface Reranker {
4
+ rerankHybrid(query: string, vecResults: RecordBatch, ftsResults: RecordBatch): Promise<RecordBatch>;
5
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // SPDX-FileCopyrightText: Copyright The LanceDB Authors
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ __exportStar(require("./rrf"), exports);
@@ -0,0 +1,14 @@
1
+ import { RecordBatch } from "apache-arrow";
2
+ import { RrfReranker as NativeRRFReranker } from "../native";
3
+ /**
4
+ * Reranks the results using the Reciprocal Rank Fusion (RRF) algorithm.
5
+ *
6
+ * @hideconstructor
7
+ */
8
+ export declare class RRFReranker {
9
+ private inner;
10
+ /** @ignore */
11
+ constructor(inner: NativeRRFReranker);
12
+ static create(k?: number): Promise<RRFReranker>;
13
+ rerankHybrid(query: string, vecResults: RecordBatch, ftsResults: RecordBatch): Promise<RecordBatch>;
14
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // SPDX-FileCopyrightText: Copyright The LanceDB Authors
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.RRFReranker = void 0;
6
+ const arrow_1 = require("../arrow");
7
+ const native_1 = require("../native");
8
+ /**
9
+ * Reranks the results using the Reciprocal Rank Fusion (RRF) algorithm.
10
+ *
11
+ * @hideconstructor
12
+ */
13
+ class RRFReranker {
14
+ inner;
15
+ /** @ignore */
16
+ constructor(inner) {
17
+ this.inner = inner;
18
+ }
19
+ static async create(k = 60) {
20
+ return new RRFReranker(await native_1.RrfReranker.tryNew(new Float32Array([k])));
21
+ }
22
+ async rerankHybrid(query, vecResults, ftsResults) {
23
+ const buffer = await this.inner.rerankHybrid(query, await (0, arrow_1.fromRecordBatchToBuffer)(vecResults), await (0, arrow_1.fromRecordBatchToBuffer)(ftsResults));
24
+ const recordBatch = await (0, arrow_1.fromBufferToRecordBatch)(buffer);
25
+ return recordBatch;
26
+ }
27
+ }
28
+ exports.RRFReranker = RRFReranker;
@@ -0,0 +1,32 @@
1
+ import type { TKeys } from "apache-arrow/type";
2
+ import { DataType, Date_, Decimal, DenseUnion, Dictionary, Duration, Field, FixedSizeBinary, FixedSizeList, Float, Int, Interval, List, Map_, Schema, SchemaLike, SparseUnion, Struct, Table, TableLike, Time, Timestamp, TimestampMicrosecond, TimestampMillisecond, TimestampNanosecond, TimestampSecond, Type, Union } from "./arrow";
3
+ export declare function sanitizeMetadata(metadataLike?: unknown): Map<string, string> | undefined;
4
+ export declare function sanitizeInt(typeLike: object): Int<Type.Int | Type.Int8 | Type.Int16 | Type.Int32 | Type.Int64 | Type.Uint8 | Type.Uint16 | Type.Uint32 | Type.Uint64>;
5
+ export declare function sanitizeFloat(typeLike: object): Float<Type.Float | Type.Float16 | Type.Float32 | Type.Float64>;
6
+ export declare function sanitizeDecimal(typeLike: object): Decimal;
7
+ export declare function sanitizeDate(typeLike: object): Date_<import("apache-arrow/type").Dates>;
8
+ export declare function sanitizeTime(typeLike: object): Time<Type.Time | Type.TimeSecond | Type.TimeMillisecond | Type.TimeMicrosecond | Type.TimeNanosecond>;
9
+ export declare function sanitizeTimestamp(typeLike: object): Timestamp<Type.Timestamp | Type.TimestampSecond | Type.TimestampMillisecond | Type.TimestampMicrosecond | Type.TimestampNanosecond>;
10
+ export declare function sanitizeTypedTimestamp(typeLike: object, Datatype: typeof TimestampNanosecond | typeof TimestampMicrosecond | typeof TimestampMillisecond | typeof TimestampSecond): TimestampSecond | TimestampMillisecond | TimestampMicrosecond | TimestampNanosecond;
11
+ export declare function sanitizeInterval(typeLike: object): Interval<Type.Interval | Type.IntervalDayTime | Type.IntervalYearMonth>;
12
+ export declare function sanitizeList(typeLike: object): List<any>;
13
+ export declare function sanitizeStruct(typeLike: object): Struct<any>;
14
+ export declare function sanitizeUnion(typeLike: object): Union<Type.Union | Type.DenseUnion | Type.SparseUnion>;
15
+ export declare function sanitizeTypedUnion(typeLike: object, UnionType: typeof DenseUnion | typeof SparseUnion): SparseUnion | DenseUnion;
16
+ export declare function sanitizeFixedSizeBinary(typeLike: object): FixedSizeBinary;
17
+ export declare function sanitizeFixedSizeList(typeLike: object): FixedSizeList<any>;
18
+ export declare function sanitizeMap(typeLike: object): Map_<any, any>;
19
+ export declare function sanitizeDuration(typeLike: object): Duration<Type.Duration | Type.DurationSecond | Type.DurationMillisecond | Type.DurationMicrosecond | Type.DurationNanosecond>;
20
+ export declare function sanitizeDictionary(typeLike: object): Dictionary<DataType<any, any>, TKeys>;
21
+ export declare function sanitizeType(typeLike: unknown): DataType<any>;
22
+ export declare function sanitizeField(fieldLike: unknown): Field;
23
+ /**
24
+ * Convert something schemaLike into a Schema instance
25
+ *
26
+ * This method is often needed even when the caller is using a Schema
27
+ * instance because they might be using a different instance of apache-arrow
28
+ * than lancedb is using.
29
+ */
30
+ export declare function sanitizeSchema(schemaLike: SchemaLike): Schema;
31
+ export declare function sanitizeTable(tableLike: TableLike): Table;
32
+ export declare function dataTypeFromName(typeName: string): DataType;
@@ -0,0 +1,473 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // SPDX-FileCopyrightText: Copyright The LanceDB Authors
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.sanitizeMetadata = sanitizeMetadata;
6
+ exports.sanitizeInt = sanitizeInt;
7
+ exports.sanitizeFloat = sanitizeFloat;
8
+ exports.sanitizeDecimal = sanitizeDecimal;
9
+ exports.sanitizeDate = sanitizeDate;
10
+ exports.sanitizeTime = sanitizeTime;
11
+ exports.sanitizeTimestamp = sanitizeTimestamp;
12
+ exports.sanitizeTypedTimestamp = sanitizeTypedTimestamp;
13
+ exports.sanitizeInterval = sanitizeInterval;
14
+ exports.sanitizeList = sanitizeList;
15
+ exports.sanitizeStruct = sanitizeStruct;
16
+ exports.sanitizeUnion = sanitizeUnion;
17
+ exports.sanitizeTypedUnion = sanitizeTypedUnion;
18
+ exports.sanitizeFixedSizeBinary = sanitizeFixedSizeBinary;
19
+ exports.sanitizeFixedSizeList = sanitizeFixedSizeList;
20
+ exports.sanitizeMap = sanitizeMap;
21
+ exports.sanitizeDuration = sanitizeDuration;
22
+ exports.sanitizeDictionary = sanitizeDictionary;
23
+ exports.sanitizeType = sanitizeType;
24
+ exports.sanitizeField = sanitizeField;
25
+ exports.sanitizeSchema = sanitizeSchema;
26
+ exports.sanitizeTable = sanitizeTable;
27
+ exports.dataTypeFromName = dataTypeFromName;
28
+ // The utilities in this file help sanitize data from the user's arrow
29
+ // library into the types expected by vectordb's arrow library. Node
30
+ // generally allows for mulitple versions of the same library (and sometimes
31
+ // even multiple copies of the same version) to be installed at the same
32
+ // time. However, arrow-js uses instanceof which expected that the input
33
+ // comes from the exact same library instance. This is not always the case
34
+ // and so we must sanitize the input to ensure that it is compatible.
35
+ const apache_arrow_1 = require("apache-arrow");
36
+ const arrow_1 = require("./arrow");
37
+ function sanitizeMetadata(metadataLike) {
38
+ if (metadataLike === undefined || metadataLike === null) {
39
+ return undefined;
40
+ }
41
+ if (!(metadataLike instanceof Map)) {
42
+ throw Error("Expected metadata, if present, to be a Map<string, string>");
43
+ }
44
+ for (const item of metadataLike) {
45
+ if (!(typeof item[0] === "string" || !(typeof item[1] === "string"))) {
46
+ throw Error("Expected metadata, if present, to be a Map<string, string> but it had non-string keys or values");
47
+ }
48
+ }
49
+ return metadataLike;
50
+ }
51
+ function sanitizeInt(typeLike) {
52
+ if (!("bitWidth" in typeLike) ||
53
+ typeof typeLike.bitWidth !== "number" ||
54
+ !("isSigned" in typeLike) ||
55
+ typeof typeLike.isSigned !== "boolean") {
56
+ throw Error("Expected an Int Type to have a `bitWidth` and `isSigned` property");
57
+ }
58
+ return new arrow_1.Int(typeLike.isSigned, typeLike.bitWidth);
59
+ }
60
+ function sanitizeFloat(typeLike) {
61
+ if (!("precision" in typeLike) || typeof typeLike.precision !== "number") {
62
+ throw Error("Expected a Float Type to have a `precision` property");
63
+ }
64
+ return new arrow_1.Float(typeLike.precision);
65
+ }
66
+ function sanitizeDecimal(typeLike) {
67
+ if (!("scale" in typeLike) ||
68
+ typeof typeLike.scale !== "number" ||
69
+ !("precision" in typeLike) ||
70
+ typeof typeLike.precision !== "number" ||
71
+ !("bitWidth" in typeLike) ||
72
+ typeof typeLike.bitWidth !== "number") {
73
+ throw Error("Expected a Decimal Type to have `scale`, `precision`, and `bitWidth` properties");
74
+ }
75
+ return new arrow_1.Decimal(typeLike.scale, typeLike.precision, typeLike.bitWidth);
76
+ }
77
+ function sanitizeDate(typeLike) {
78
+ if (!("unit" in typeLike) || typeof typeLike.unit !== "number") {
79
+ throw Error("Expected a Date type to have a `unit` property");
80
+ }
81
+ return new arrow_1.Date_(typeLike.unit);
82
+ }
83
+ function sanitizeTime(typeLike) {
84
+ if (!("unit" in typeLike) ||
85
+ typeof typeLike.unit !== "number" ||
86
+ !("bitWidth" in typeLike) ||
87
+ typeof typeLike.bitWidth !== "number") {
88
+ throw Error("Expected a Time type to have `unit` and `bitWidth` properties");
89
+ }
90
+ return new arrow_1.Time(typeLike.unit, typeLike.bitWidth);
91
+ }
92
+ function sanitizeTimestamp(typeLike) {
93
+ if (!("unit" in typeLike) || typeof typeLike.unit !== "number") {
94
+ throw Error("Expected a Timestamp type to have a `unit` property");
95
+ }
96
+ let timezone = null;
97
+ if ("timezone" in typeLike && typeof typeLike.timezone === "string") {
98
+ timezone = typeLike.timezone;
99
+ }
100
+ return new arrow_1.Timestamp(typeLike.unit, timezone);
101
+ }
102
+ function sanitizeTypedTimestamp(typeLike,
103
+ // eslint-disable-next-line @typescript-eslint/naming-convention
104
+ Datatype) {
105
+ let timezone = null;
106
+ if ("timezone" in typeLike && typeof typeLike.timezone === "string") {
107
+ timezone = typeLike.timezone;
108
+ }
109
+ return new Datatype(timezone);
110
+ }
111
+ function sanitizeInterval(typeLike) {
112
+ if (!("unit" in typeLike) || typeof typeLike.unit !== "number") {
113
+ throw Error("Expected an Interval type to have a `unit` property");
114
+ }
115
+ return new arrow_1.Interval(typeLike.unit);
116
+ }
117
+ function sanitizeList(typeLike) {
118
+ if (!("children" in typeLike) || !Array.isArray(typeLike.children)) {
119
+ throw Error("Expected a List type to have an array-like `children` property");
120
+ }
121
+ if (typeLike.children.length !== 1) {
122
+ throw Error("Expected a List type to have exactly one child");
123
+ }
124
+ return new arrow_1.List(sanitizeField(typeLike.children[0]));
125
+ }
126
+ function sanitizeStruct(typeLike) {
127
+ if (!("children" in typeLike) || !Array.isArray(typeLike.children)) {
128
+ throw Error("Expected a Struct type to have an array-like `children` property");
129
+ }
130
+ return new arrow_1.Struct(typeLike.children.map((child) => sanitizeField(child)));
131
+ }
132
+ function sanitizeUnion(typeLike) {
133
+ if (!("typeIds" in typeLike) ||
134
+ !("mode" in typeLike) ||
135
+ typeof typeLike.mode !== "number") {
136
+ throw Error("Expected a Union type to have `typeIds` and `mode` properties");
137
+ }
138
+ if (!("children" in typeLike) || !Array.isArray(typeLike.children)) {
139
+ throw Error("Expected a Union type to have an array-like `children` property");
140
+ }
141
+ return new arrow_1.Union(typeLike.mode,
142
+ // biome-ignore lint/suspicious/noExplicitAny: skip
143
+ typeLike.typeIds, typeLike.children.map((child) => sanitizeField(child)));
144
+ }
145
+ function sanitizeTypedUnion(typeLike,
146
+ // eslint-disable-next-line @typescript-eslint/naming-convention
147
+ UnionType) {
148
+ if (!("typeIds" in typeLike)) {
149
+ throw Error("Expected a DenseUnion/SparseUnion type to have a `typeIds` property");
150
+ }
151
+ if (!("children" in typeLike) || !Array.isArray(typeLike.children)) {
152
+ throw Error("Expected a DenseUnion/SparseUnion type to have an array-like `children` property");
153
+ }
154
+ return new UnionType(typeLike.typeIds, typeLike.children.map((child) => sanitizeField(child)));
155
+ }
156
+ function sanitizeFixedSizeBinary(typeLike) {
157
+ if (!("byteWidth" in typeLike) || typeof typeLike.byteWidth !== "number") {
158
+ throw Error("Expected a FixedSizeBinary type to have a `byteWidth` property");
159
+ }
160
+ return new arrow_1.FixedSizeBinary(typeLike.byteWidth);
161
+ }
162
+ function sanitizeFixedSizeList(typeLike) {
163
+ if (!("listSize" in typeLike) || typeof typeLike.listSize !== "number") {
164
+ throw Error("Expected a FixedSizeList type to have a `listSize` property");
165
+ }
166
+ if (!("children" in typeLike) || !Array.isArray(typeLike.children)) {
167
+ throw Error("Expected a FixedSizeList type to have an array-like `children` property");
168
+ }
169
+ if (typeLike.children.length !== 1) {
170
+ throw Error("Expected a FixedSizeList type to have exactly one child");
171
+ }
172
+ return new arrow_1.FixedSizeList(typeLike.listSize, sanitizeField(typeLike.children[0]));
173
+ }
174
+ function sanitizeMap(typeLike) {
175
+ if (!("children" in typeLike) || !Array.isArray(typeLike.children)) {
176
+ throw Error("Expected a Map type to have an array-like `children` property");
177
+ }
178
+ if (!("keysSorted" in typeLike) || typeof typeLike.keysSorted !== "boolean") {
179
+ throw Error("Expected a Map type to have a `keysSorted` property");
180
+ }
181
+ return new arrow_1.Map_(
182
+ // biome-ignore lint/suspicious/noExplicitAny: skip
183
+ typeLike.children.map((field) => sanitizeField(field)), typeLike.keysSorted);
184
+ }
185
+ function sanitizeDuration(typeLike) {
186
+ if (!("unit" in typeLike) || typeof typeLike.unit !== "number") {
187
+ throw Error("Expected a Duration type to have a `unit` property");
188
+ }
189
+ return new arrow_1.Duration(typeLike.unit);
190
+ }
191
+ function sanitizeDictionary(typeLike) {
192
+ if (!("id" in typeLike) || typeof typeLike.id !== "number") {
193
+ throw Error("Expected a Dictionary type to have an `id` property");
194
+ }
195
+ if (!("indices" in typeLike) || typeof typeLike.indices !== "object") {
196
+ throw Error("Expected a Dictionary type to have an `indices` property");
197
+ }
198
+ if (!("dictionary" in typeLike) || typeof typeLike.dictionary !== "object") {
199
+ throw Error("Expected a Dictionary type to have an `dictionary` property");
200
+ }
201
+ if (!("isOrdered" in typeLike) || typeof typeLike.isOrdered !== "boolean") {
202
+ throw Error("Expected a Dictionary type to have an `isOrdered` property");
203
+ }
204
+ return new arrow_1.Dictionary(sanitizeType(typeLike.dictionary), sanitizeType(typeLike.indices), typeLike.id, typeLike.isOrdered);
205
+ }
206
+ // biome-ignore lint/suspicious/noExplicitAny: skip
207
+ function sanitizeType(typeLike) {
208
+ if (typeof typeLike === "string") {
209
+ return dataTypeFromName(typeLike);
210
+ }
211
+ if (typeof typeLike !== "object" || typeLike === null) {
212
+ throw Error("Expected a Type but object was null/undefined");
213
+ }
214
+ if (!("typeId" in typeLike) ||
215
+ !(typeof typeLike.typeId !== "function" ||
216
+ typeof typeLike.typeId !== "number")) {
217
+ throw Error("Expected a Type to have a typeId property");
218
+ }
219
+ let typeId;
220
+ if (typeof typeLike.typeId === "function") {
221
+ typeId = typeLike.typeId();
222
+ }
223
+ else if (typeof typeLike.typeId === "number") {
224
+ typeId = typeLike.typeId;
225
+ }
226
+ else {
227
+ throw Error("Type's typeId property was not a function or number");
228
+ }
229
+ switch (typeId) {
230
+ case arrow_1.Type.NONE:
231
+ throw Error("Received a Type with a typeId of NONE");
232
+ case arrow_1.Type.Null:
233
+ return new arrow_1.Null();
234
+ case arrow_1.Type.Int:
235
+ return sanitizeInt(typeLike);
236
+ case arrow_1.Type.Float:
237
+ return sanitizeFloat(typeLike);
238
+ case arrow_1.Type.Binary:
239
+ return new arrow_1.Binary();
240
+ case arrow_1.Type.Utf8:
241
+ return new arrow_1.Utf8();
242
+ case arrow_1.Type.Bool:
243
+ return new arrow_1.Bool();
244
+ case arrow_1.Type.Decimal:
245
+ return sanitizeDecimal(typeLike);
246
+ case arrow_1.Type.Date:
247
+ return sanitizeDate(typeLike);
248
+ case arrow_1.Type.Time:
249
+ return sanitizeTime(typeLike);
250
+ case arrow_1.Type.Timestamp:
251
+ return sanitizeTimestamp(typeLike);
252
+ case arrow_1.Type.Interval:
253
+ return sanitizeInterval(typeLike);
254
+ case arrow_1.Type.List:
255
+ return sanitizeList(typeLike);
256
+ case arrow_1.Type.Struct:
257
+ return sanitizeStruct(typeLike);
258
+ case arrow_1.Type.Union:
259
+ return sanitizeUnion(typeLike);
260
+ case arrow_1.Type.FixedSizeBinary:
261
+ return sanitizeFixedSizeBinary(typeLike);
262
+ case arrow_1.Type.FixedSizeList:
263
+ return sanitizeFixedSizeList(typeLike);
264
+ case arrow_1.Type.Map:
265
+ return sanitizeMap(typeLike);
266
+ case arrow_1.Type.Duration:
267
+ return sanitizeDuration(typeLike);
268
+ case arrow_1.Type.Dictionary:
269
+ return sanitizeDictionary(typeLike);
270
+ case arrow_1.Type.Int8:
271
+ return new arrow_1.Int8();
272
+ case arrow_1.Type.Int16:
273
+ return new arrow_1.Int16();
274
+ case arrow_1.Type.Int32:
275
+ return new arrow_1.Int32();
276
+ case arrow_1.Type.Int64:
277
+ return new arrow_1.Int64();
278
+ case arrow_1.Type.Uint8:
279
+ return new arrow_1.Uint8();
280
+ case arrow_1.Type.Uint16:
281
+ return new arrow_1.Uint16();
282
+ case arrow_1.Type.Uint32:
283
+ return new arrow_1.Uint32();
284
+ case arrow_1.Type.Uint64:
285
+ return new arrow_1.Uint64();
286
+ case arrow_1.Type.Float16:
287
+ return new arrow_1.Float16();
288
+ case arrow_1.Type.Float32:
289
+ return new arrow_1.Float32();
290
+ case arrow_1.Type.Float64:
291
+ return new arrow_1.Float64();
292
+ case arrow_1.Type.DateMillisecond:
293
+ return new arrow_1.DateMillisecond();
294
+ case arrow_1.Type.DateDay:
295
+ return new arrow_1.DateDay();
296
+ case arrow_1.Type.TimeNanosecond:
297
+ return new arrow_1.TimeNanosecond();
298
+ case arrow_1.Type.TimeMicrosecond:
299
+ return new arrow_1.TimeMicrosecond();
300
+ case arrow_1.Type.TimeMillisecond:
301
+ return new arrow_1.TimeMillisecond();
302
+ case arrow_1.Type.TimeSecond:
303
+ return new arrow_1.TimeSecond();
304
+ case arrow_1.Type.TimestampNanosecond:
305
+ return sanitizeTypedTimestamp(typeLike, arrow_1.TimestampNanosecond);
306
+ case arrow_1.Type.TimestampMicrosecond:
307
+ return sanitizeTypedTimestamp(typeLike, arrow_1.TimestampMicrosecond);
308
+ case arrow_1.Type.TimestampMillisecond:
309
+ return sanitizeTypedTimestamp(typeLike, arrow_1.TimestampMillisecond);
310
+ case arrow_1.Type.TimestampSecond:
311
+ return sanitizeTypedTimestamp(typeLike, arrow_1.TimestampSecond);
312
+ case arrow_1.Type.DenseUnion:
313
+ return sanitizeTypedUnion(typeLike, arrow_1.DenseUnion);
314
+ case arrow_1.Type.SparseUnion:
315
+ return sanitizeTypedUnion(typeLike, arrow_1.SparseUnion);
316
+ case arrow_1.Type.IntervalDayTime:
317
+ return new arrow_1.IntervalDayTime();
318
+ case arrow_1.Type.IntervalYearMonth:
319
+ return new arrow_1.IntervalYearMonth();
320
+ case arrow_1.Type.DurationNanosecond:
321
+ return new arrow_1.DurationNanosecond();
322
+ case arrow_1.Type.DurationMicrosecond:
323
+ return new arrow_1.DurationMicrosecond();
324
+ case arrow_1.Type.DurationMillisecond:
325
+ return new arrow_1.DurationMillisecond();
326
+ case arrow_1.Type.DurationSecond:
327
+ return new arrow_1.DurationSecond();
328
+ default:
329
+ throw new Error("Unrecognized type id in schema: " + typeId);
330
+ }
331
+ }
332
+ function sanitizeField(fieldLike) {
333
+ if (fieldLike instanceof arrow_1.Field) {
334
+ return fieldLike;
335
+ }
336
+ if (typeof fieldLike !== "object" || fieldLike === null) {
337
+ throw Error("Expected a Field but object was null/undefined");
338
+ }
339
+ if (!("type" in fieldLike) ||
340
+ !("name" in fieldLike) ||
341
+ !("nullable" in fieldLike)) {
342
+ throw Error("The field passed in is missing a `type`/`name`/`nullable` property");
343
+ }
344
+ let type;
345
+ try {
346
+ type = sanitizeType(fieldLike.type);
347
+ }
348
+ catch (error) {
349
+ throw Error(`Unable to sanitize type for field: ${fieldLike.name} due to error: ${error}`, { cause: error });
350
+ }
351
+ const name = fieldLike.name;
352
+ if (!(typeof name === "string")) {
353
+ throw Error("The field passed in had a non-string `name` property");
354
+ }
355
+ const nullable = fieldLike.nullable;
356
+ if (!(typeof nullable === "boolean")) {
357
+ throw Error("The field passed in had a non-boolean `nullable` property");
358
+ }
359
+ let metadata;
360
+ if ("metadata" in fieldLike) {
361
+ metadata = sanitizeMetadata(fieldLike.metadata);
362
+ }
363
+ return new arrow_1.Field(name, type, nullable, metadata);
364
+ }
365
+ /**
366
+ * Convert something schemaLike into a Schema instance
367
+ *
368
+ * This method is often needed even when the caller is using a Schema
369
+ * instance because they might be using a different instance of apache-arrow
370
+ * than lancedb is using.
371
+ */
372
+ function sanitizeSchema(schemaLike) {
373
+ if (schemaLike instanceof arrow_1.Schema) {
374
+ return schemaLike;
375
+ }
376
+ if (typeof schemaLike !== "object" || schemaLike === null) {
377
+ throw Error("Expected a Schema but object was null/undefined");
378
+ }
379
+ if (!("fields" in schemaLike)) {
380
+ throw Error("The schema passed in does not appear to be a schema (no 'fields' property)");
381
+ }
382
+ let metadata;
383
+ if ("metadata" in schemaLike) {
384
+ metadata = sanitizeMetadata(schemaLike.metadata);
385
+ }
386
+ if (!Array.isArray(schemaLike.fields)) {
387
+ throw Error("The schema passed in had a 'fields' property but it was not an array");
388
+ }
389
+ const sanitizedFields = schemaLike.fields.map((field) => sanitizeField(field));
390
+ return new arrow_1.Schema(sanitizedFields, metadata);
391
+ }
392
+ function sanitizeTable(tableLike) {
393
+ if (tableLike instanceof arrow_1.Table) {
394
+ return tableLike;
395
+ }
396
+ if (typeof tableLike !== "object" || tableLike === null) {
397
+ throw Error("Expected a Table but object was null/undefined");
398
+ }
399
+ if (!("schema" in tableLike)) {
400
+ throw Error("The table passed in does not appear to be a table (no 'schema' property)");
401
+ }
402
+ if (!("batches" in tableLike)) {
403
+ throw Error("The table passed in does not appear to be a table (no 'columns' property)");
404
+ }
405
+ const schema = sanitizeSchema(tableLike.schema);
406
+ const batches = tableLike.batches.map(sanitizeRecordBatch);
407
+ return new arrow_1.Table(schema, batches);
408
+ }
409
+ function sanitizeRecordBatch(batchLike) {
410
+ if (batchLike instanceof arrow_1.RecordBatch) {
411
+ return batchLike;
412
+ }
413
+ if (typeof batchLike !== "object" || batchLike === null) {
414
+ throw Error("Expected a RecordBatch but object was null/undefined");
415
+ }
416
+ if (!("schema" in batchLike)) {
417
+ throw Error("The record batch passed in does not appear to be a record batch (no 'schema' property)");
418
+ }
419
+ if (!("data" in batchLike)) {
420
+ throw Error("The record batch passed in does not appear to be a record batch (no 'data' property)");
421
+ }
422
+ const schema = sanitizeSchema(batchLike.schema);
423
+ const data = sanitizeData(batchLike.data);
424
+ return new arrow_1.RecordBatch(schema, data);
425
+ }
426
+ function sanitizeData(dataLike) {
427
+ if (dataLike instanceof apache_arrow_1.Data) {
428
+ return dataLike;
429
+ }
430
+ return new apache_arrow_1.Data(dataLike.type, dataLike.offset, dataLike.length, dataLike.nullCount, {
431
+ [apache_arrow_1.BufferType.OFFSET]: dataLike.valueOffsets,
432
+ [apache_arrow_1.BufferType.DATA]: dataLike.values,
433
+ [apache_arrow_1.BufferType.VALIDITY]: dataLike.nullBitmap,
434
+ [apache_arrow_1.BufferType.TYPE]: dataLike.typeIds,
435
+ });
436
+ }
437
+ const constructorsByTypeName = {
438
+ null: () => new arrow_1.Null(),
439
+ binary: () => new arrow_1.Binary(),
440
+ utf8: () => new arrow_1.Utf8(),
441
+ bool: () => new arrow_1.Bool(),
442
+ int8: () => new arrow_1.Int8(),
443
+ int16: () => new arrow_1.Int16(),
444
+ int32: () => new arrow_1.Int32(),
445
+ int64: () => new arrow_1.Int64(),
446
+ uint8: () => new arrow_1.Uint8(),
447
+ uint16: () => new arrow_1.Uint16(),
448
+ uint32: () => new arrow_1.Uint32(),
449
+ uint64: () => new arrow_1.Uint64(),
450
+ float16: () => new arrow_1.Float16(),
451
+ float32: () => new arrow_1.Float32(),
452
+ float64: () => new arrow_1.Float64(),
453
+ datemillisecond: () => new arrow_1.DateMillisecond(),
454
+ dateday: () => new arrow_1.DateDay(),
455
+ timenanosecond: () => new arrow_1.TimeNanosecond(),
456
+ timemicrosecond: () => new arrow_1.TimeMicrosecond(),
457
+ timemillisecond: () => new arrow_1.TimeMillisecond(),
458
+ timesecond: () => new arrow_1.TimeSecond(),
459
+ intervaldaytime: () => new arrow_1.IntervalDayTime(),
460
+ intervalyearmonth: () => new arrow_1.IntervalYearMonth(),
461
+ durationnanosecond: () => new arrow_1.DurationNanosecond(),
462
+ durationmicrosecond: () => new arrow_1.DurationMicrosecond(),
463
+ durationmillisecond: () => new arrow_1.DurationMillisecond(),
464
+ durationsecond: () => new arrow_1.DurationSecond(),
465
+ };
466
+ function dataTypeFromName(typeName) {
467
+ const normalizedTypeName = typeName.toLowerCase();
468
+ const _constructor = constructorsByTypeName[normalizedTypeName];
469
+ if (!_constructor) {
470
+ throw new Error("Unrecognized type name in schema: " + typeName);
471
+ }
472
+ return _constructor();
473
+ }