| 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 | // The utilities in this file help sanitize data from the user's arrow |
| 16 | // library into the types expected by vectordb's arrow library. Node |
| 17 | // generally allows for mulitple versions of the same library (and sometimes |
| 18 | // even multiple copies of the same version) to be installed at the same |
| 19 | // time. However, arrow-js uses instanceof which expected that the input |
| 20 | // comes from the exact same library instance. This is not always the case |
| 21 | // and so we must sanitize the input to ensure that it is compatible. |
| 22 | |
| 23 | import { |
| 24 | Field, |
| 25 | Utf8, |
| 26 | FixedSizeBinary, |
| 27 | FixedSizeList, |
| 28 | Schema, |
| 29 | List, |
| 30 | Struct, |
| 31 | Float, |
| 32 | Bool, |
| 33 | Date_, |
| 34 | Decimal, |
| 35 | type DataType, |
| 36 | Dictionary, |
| 37 | Binary, |
| 38 | Float32, |
| 39 | Interval, |
| 40 | Map_, |
| 41 | Duration, |
| 42 | Union, |
| 43 | Time, |
| 44 | Timestamp, |
| 45 | Type, |
| 46 | Null, |
| 47 | Int, |
| 48 | type Precision, |
| 49 | type DateUnit, |
| 50 | Int8, |
| 51 | Int16, |
| 52 | Int32, |
| 53 | Int64, |
| 54 | Uint8, |
| 55 | Uint16, |
| 56 | Uint32, |
| 57 | Uint64, |
| 58 | Float16, |
| 59 | Float64, |
| 60 | DateDay, |
| 61 | DateMillisecond, |
| 62 | DenseUnion, |
| 63 | SparseUnion, |
| 64 | TimeNanosecond, |
| 65 | TimeMicrosecond, |
| 66 | TimeMillisecond, |
| 67 | TimeSecond, |
| 68 | TimestampNanosecond, |
| 69 | TimestampMicrosecond, |
| 70 | TimestampMillisecond, |
| 71 | TimestampSecond, |
| 72 | IntervalDayTime, |
| 73 | IntervalYearMonth, |
| 74 | DurationNanosecond, |
| 75 | DurationMicrosecond, |
| 76 | DurationMillisecond, |
| 77 | DurationSecond |
| 78 | } from "apache-arrow"; |
| 79 | import type { IntBitWidth, TimeBitWidth } from "apache-arrow/type"; |
| 80 | |
| 81 | function sanitizeMetadata( |
| 82 | metadataLike?: unknown |
| 83 | ): Map<string, string> | undefined { |
| 84 | if (metadataLike === undefined || metadataLike === null) { |
| 85 | return undefined; |
| 86 | } |
| 87 | if (!(metadataLike instanceof Map)) { |
| 88 | throw Error("Expected metadata, if present, to be a Map<string, string>"); |
| 89 | } |
| 90 | for (const item of metadataLike) { |
| 91 | if (!(typeof item[0] === "string" || !(typeof item[1] === "string"))) { |
| 92 | throw Error( |
| 93 | "Expected metadata, if present, to be a Map<string, string> but it had non-string keys or values" |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | return metadataLike as Map<string, string>; |
| 98 | } |
| 99 | |
| 100 | function sanitizeInt(typeLike: object) { |
| 101 | if ( |
| 102 | !("bitWidth" in typeLike) || |
| 103 | typeof typeLike.bitWidth !== "number" || |
| 104 | !("isSigned" in typeLike) || |
| 105 | typeof typeLike.isSigned !== "boolean" |
| 106 | ) { |
| 107 | throw Error( |
| 108 | "Expected an Int Type to have a `bitWidth` and `isSigned` property" |
| 109 | ); |
| 110 | } |
| 111 | return new Int(typeLike.isSigned, typeLike.bitWidth as IntBitWidth); |
| 112 | } |
| 113 | |
| 114 | function sanitizeFloat(typeLike: object) { |
| 115 | if (!("precision" in typeLike) || typeof typeLike.precision !== "number") { |
| 116 | throw Error("Expected a Float Type to have a `precision` property"); |
| 117 | } |
| 118 | return new Float(typeLike.precision as Precision); |
| 119 | } |
| 120 | |
| 121 | function sanitizeDecimal(typeLike: object) { |
| 122 | if ( |
| 123 | !("scale" in typeLike) || |
| 124 | typeof typeLike.scale !== "number" || |
| 125 | !("precision" in typeLike) || |
| 126 | typeof typeLike.precision !== "number" || |
| 127 | !("bitWidth" in typeLike) || |
| 128 | typeof typeLike.bitWidth !== "number" |
| 129 | ) { |
| 130 | throw Error( |
| 131 | "Expected a Decimal Type to have `scale`, `precision`, and `bitWidth` properties" |
| 132 | ); |
| 133 | } |
| 134 | return new Decimal(typeLike.scale, typeLike.precision, typeLike.bitWidth); |
| 135 | } |
| 136 | |
| 137 | function sanitizeDate(typeLike: object) { |
| 138 | if (!("unit" in typeLike) || typeof typeLike.unit !== "number") { |
| 139 | throw Error("Expected a Date type to have a `unit` property"); |
| 140 | } |
| 141 | return new Date_(typeLike.unit as DateUnit); |
| 142 | } |
| 143 | |
| 144 | function sanitizeTime(typeLike: object) { |
| 145 | if ( |
| 146 | !("unit" in typeLike) || |
| 147 | typeof typeLike.unit !== "number" || |
| 148 | !("bitWidth" in typeLike) || |
| 149 | typeof typeLike.bitWidth !== "number" |
| 150 | ) { |
| 151 | throw Error( |
| 152 | "Expected a Time type to have `unit` and `bitWidth` properties" |
| 153 | ); |
| 154 | } |
| 155 | return new Time(typeLike.unit, typeLike.bitWidth as TimeBitWidth); |
| 156 | } |
| 157 | |
| 158 | function sanitizeTimestamp(typeLike: object) { |
| 159 | if (!("unit" in typeLike) || typeof typeLike.unit !== "number") { |
| 160 | throw Error("Expected a Timestamp type to have a `unit` property"); |
| 161 | } |
| 162 | let timezone = null; |
| 163 | if ("timezone" in typeLike && typeof typeLike.timezone === "string") { |
| 164 | timezone = typeLike.timezone; |
| 165 | } |
| 166 | return new Timestamp(typeLike.unit, timezone); |
| 167 | } |
| 168 | |
| 169 | function sanitizeTypedTimestamp( |
| 170 | typeLike: object, |
| 171 | Datatype: |
| 172 | | typeof TimestampNanosecond |
| 173 | | typeof TimestampMicrosecond |
| 174 | | typeof TimestampMillisecond |
| 175 | | typeof TimestampSecond |
| 176 | ) { |
| 177 | let timezone = null; |
| 178 | if ("timezone" in typeLike && typeof typeLike.timezone === "string") { |
| 179 | timezone = typeLike.timezone; |
| 180 | } |
| 181 | return new Datatype(timezone); |
| 182 | } |
| 183 | |
| 184 | function sanitizeInterval(typeLike: object) { |
| 185 | if (!("unit" in typeLike) || typeof typeLike.unit !== "number") { |
| 186 | throw Error("Expected an Interval type to have a `unit` property"); |
| 187 | } |
| 188 | return new Interval(typeLike.unit); |
| 189 | } |
| 190 | |
| 191 | function sanitizeList(typeLike: object) { |
| 192 | if (!("children" in typeLike) || !Array.isArray(typeLike.children)) { |
| 193 | throw Error( |
| 194 | "Expected a List type to have an array-like `children` property" |
| 195 | ); |
| 196 | } |
| 197 | if (typeLike.children.length !== 1) { |
| 198 | throw Error("Expected a List type to have exactly one child"); |
| 199 | } |
| 200 | return new List(sanitizeField(typeLike.children[0])); |
| 201 | } |
| 202 | |
| 203 | function sanitizeStruct(typeLike: object) { |
| 204 | if (!("children" in typeLike) || !Array.isArray(typeLike.children)) { |
| 205 | throw Error( |
| 206 | "Expected a Struct type to have an array-like `children` property" |
| 207 | ); |
| 208 | } |
| 209 | return new Struct(typeLike.children.map((child) => sanitizeField(child))); |
| 210 | } |
| 211 | |
| 212 | function sanitizeUnion(typeLike: object) { |
| 213 | if ( |
| 214 | !("typeIds" in typeLike) || |
| 215 | !("mode" in typeLike) || |
| 216 | typeof typeLike.mode !== "number" |
| 217 | ) { |
| 218 | throw Error( |
| 219 | "Expected a Union type to have `typeIds` and `mode` properties" |
| 220 | ); |
| 221 | } |
| 222 | if (!("children" in typeLike) || !Array.isArray(typeLike.children)) { |
| 223 | throw Error( |
| 224 | "Expected a Union type to have an array-like `children` property" |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | return new Union( |
| 229 | typeLike.mode, |
| 230 | typeLike.typeIds as any, |
| 231 | typeLike.children.map((child) => sanitizeField(child)) |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | function sanitizeTypedUnion( |
| 236 | typeLike: object, |
| 237 | UnionType: typeof DenseUnion | typeof SparseUnion |
| 238 | ) { |
| 239 | if (!("typeIds" in typeLike)) { |
| 240 | throw Error( |
| 241 | "Expected a DenseUnion/SparseUnion type to have a `typeIds` property" |
| 242 | ); |
| 243 | } |
| 244 | if (!("children" in typeLike) || !Array.isArray(typeLike.children)) { |
| 245 | throw Error( |
| 246 | "Expected a DenseUnion/SparseUnion type to have an array-like `children` property" |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | return new UnionType( |
| 251 | typeLike.typeIds as any, |
| 252 | typeLike.children.map((child) => sanitizeField(child)) |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | function sanitizeFixedSizeBinary(typeLike: object) { |
| 257 | if (!("byteWidth" in typeLike) || typeof typeLike.byteWidth !== "number") { |
| 258 | throw Error( |
| 259 | "Expected a FixedSizeBinary type to have a `byteWidth` property" |
| 260 | ); |
| 261 | } |
| 262 | return new FixedSizeBinary(typeLike.byteWidth); |
| 263 | } |
| 264 | |
| 265 | function sanitizeFixedSizeList(typeLike: object) { |
| 266 | if (!("listSize" in typeLike) || typeof typeLike.listSize !== "number") { |
| 267 | throw Error("Expected a FixedSizeList type to have a `listSize` property"); |
| 268 | } |
| 269 | if (!("children" in typeLike) || !Array.isArray(typeLike.children)) { |
| 270 | throw Error( |
| 271 | "Expected a FixedSizeList type to have an array-like `children` property" |
| 272 | ); |
| 273 | } |
| 274 | if (typeLike.children.length !== 1) { |
| 275 | throw Error("Expected a FixedSizeList type to have exactly one child"); |
| 276 | } |
| 277 | return new FixedSizeList( |
| 278 | typeLike.listSize, |
| 279 | sanitizeField(typeLike.children[0]) |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | function sanitizeMap(typeLike: object) { |
| 284 | if (!("children" in typeLike) || !Array.isArray(typeLike.children)) { |
| 285 | throw Error( |
| 286 | "Expected a Map type to have an array-like `children` property" |
| 287 | ); |
| 288 | } |
| 289 | if (!("keysSorted" in typeLike) || typeof typeLike.keysSorted !== "boolean") { |
| 290 | throw Error("Expected a Map type to have a `keysSorted` property"); |
| 291 | } |
| 292 | return new Map_( |
| 293 | typeLike.children.map((field) => sanitizeField(field)) as any, |
| 294 | typeLike.keysSorted |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | function sanitizeDuration(typeLike: object) { |
| 299 | if (!("unit" in typeLike) || typeof typeLike.unit !== "number") { |
| 300 | throw Error("Expected a Duration type to have a `unit` property"); |
| 301 | } |
| 302 | return new Duration(typeLike.unit); |
| 303 | } |
| 304 | |
| 305 | function sanitizeDictionary(typeLike: object) { |
| 306 | if (!("id" in typeLike) || typeof typeLike.id !== "number") { |
| 307 | throw Error("Expected a Dictionary type to have an `id` property"); |
| 308 | } |
| 309 | if (!("indices" in typeLike) || typeof typeLike.indices !== "object") { |
| 310 | throw Error("Expected a Dictionary type to have an `indices` property"); |
| 311 | } |
| 312 | if (!("dictionary" in typeLike) || typeof typeLike.dictionary !== "object") { |
| 313 | throw Error("Expected a Dictionary type to have an `dictionary` property"); |
| 314 | } |
| 315 | if (!("isOrdered" in typeLike) || typeof typeLike.isOrdered !== "boolean") { |
| 316 | throw Error("Expected a Dictionary type to have an `isOrdered` property"); |
| 317 | } |
| 318 | return new Dictionary( |
| 319 | sanitizeType(typeLike.dictionary), |
| 320 | sanitizeType(typeLike.indices) as any, |
| 321 | typeLike.id, |
| 322 | typeLike.isOrdered |
| 323 | ); |
| 324 | } |
| 325 | |
| 326 | function sanitizeType(typeLike: unknown): DataType<any> { |
| 327 | if (typeof typeLike !== "object" || typeLike === null) { |
| 328 | throw Error("Expected a Type but object was null/undefined"); |
| 329 | } |
| 330 | if (!("typeId" in typeLike) || !(typeof typeLike.typeId !== "function")) { |
| 331 | throw Error("Expected a Type to have a typeId function"); |
| 332 | } |
| 333 | let typeId: Type; |
| 334 | if (typeof typeLike.typeId === "function") { |
| 335 | typeId = (typeLike.typeId as () => unknown)() as Type; |
| 336 | } else if (typeof typeLike.typeId === "number") { |
| 337 | typeId = typeLike.typeId as Type; |
| 338 | } else { |
| 339 | throw Error("Type's typeId property was not a function or number"); |
| 340 | } |
| 341 | |
| 342 | switch (typeId) { |
| 343 | case Type.NONE: |
| 344 | throw Error("Received a Type with a typeId of NONE"); |
| 345 | case Type.Null: |
| 346 | return new Null(); |
| 347 | case Type.Int: |
| 348 | return sanitizeInt(typeLike); |
| 349 | case Type.Float: |
| 350 | return sanitizeFloat(typeLike); |
| 351 | case Type.Binary: |
| 352 | return new Binary(); |
| 353 | case Type.Utf8: |
| 354 | return new Utf8(); |
| 355 | case Type.Bool: |
| 356 | return new Bool(); |
| 357 | case Type.Decimal: |
| 358 | return sanitizeDecimal(typeLike); |
| 359 | case Type.Date: |
| 360 | return sanitizeDate(typeLike); |
| 361 | case Type.Time: |
| 362 | return sanitizeTime(typeLike); |
| 363 | case Type.Timestamp: |
| 364 | return sanitizeTimestamp(typeLike); |
| 365 | case Type.Interval: |
| 366 | return sanitizeInterval(typeLike); |
| 367 | case Type.List: |
| 368 | return sanitizeList(typeLike); |
| 369 | case Type.Struct: |
| 370 | return sanitizeStruct(typeLike); |
| 371 | case Type.Union: |
| 372 | return sanitizeUnion(typeLike); |
| 373 | case Type.FixedSizeBinary: |
| 374 | return sanitizeFixedSizeBinary(typeLike); |
| 375 | case Type.FixedSizeList: |
| 376 | return sanitizeFixedSizeList(typeLike); |
| 377 | case Type.Map: |
| 378 | return sanitizeMap(typeLike); |
| 379 | case Type.Duration: |
| 380 | return sanitizeDuration(typeLike); |
| 381 | case Type.Dictionary: |
| 382 | return sanitizeDictionary(typeLike); |
| 383 | case Type.Int8: |
| 384 | return new Int8(); |
| 385 | case Type.Int16: |
| 386 | return new Int16(); |
| 387 | case Type.Int32: |
| 388 | return new Int32(); |
| 389 | case Type.Int64: |
| 390 | return new Int64(); |
| 391 | case Type.Uint8: |
| 392 | return new Uint8(); |
| 393 | case Type.Uint16: |
| 394 | return new Uint16(); |
| 395 | case Type.Uint32: |
| 396 | return new Uint32(); |
| 397 | case Type.Uint64: |
| 398 | return new Uint64(); |
| 399 | case Type.Float16: |
| 400 | return new Float16(); |
| 401 | case Type.Float32: |
| 402 | return new Float32(); |
| 403 | case Type.Float64: |
| 404 | return new Float64(); |
| 405 | case Type.DateMillisecond: |
| 406 | return new DateMillisecond(); |
| 407 | case Type.DateDay: |
| 408 | return new DateDay(); |
| 409 | case Type.TimeNanosecond: |
| 410 | return new TimeNanosecond(); |
| 411 | case Type.TimeMicrosecond: |
| 412 | return new TimeMicrosecond(); |
| 413 | case Type.TimeMillisecond: |
| 414 | return new TimeMillisecond(); |
| 415 | case Type.TimeSecond: |
| 416 | return new TimeSecond(); |
| 417 | case Type.TimestampNanosecond: |
| 418 | return sanitizeTypedTimestamp(typeLike, TimestampNanosecond); |
| 419 | case Type.TimestampMicrosecond: |
| 420 | return sanitizeTypedTimestamp(typeLike, TimestampMicrosecond); |
| 421 | case Type.TimestampMillisecond: |
| 422 | return sanitizeTypedTimestamp(typeLike, TimestampMillisecond); |
| 423 | case Type.TimestampSecond: |
| 424 | return sanitizeTypedTimestamp(typeLike, TimestampSecond); |
| 425 | case Type.DenseUnion: |
| 426 | return sanitizeTypedUnion(typeLike, DenseUnion); |
| 427 | case Type.SparseUnion: |
| 428 | return sanitizeTypedUnion(typeLike, SparseUnion); |
| 429 | case Type.IntervalDayTime: |
| 430 | return new IntervalDayTime(); |
| 431 | case Type.IntervalYearMonth: |
| 432 | return new IntervalYearMonth(); |
| 433 | case Type.DurationNanosecond: |
| 434 | return new DurationNanosecond(); |
| 435 | case Type.DurationMicrosecond: |
| 436 | return new DurationMicrosecond(); |
| 437 | case Type.DurationMillisecond: |
| 438 | return new DurationMillisecond(); |
| 439 | case Type.DurationSecond: |
| 440 | return new DurationSecond(); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | function sanitizeField(fieldLike: unknown): Field { |
| 445 | if (fieldLike instanceof Field) { |
| 446 | return fieldLike; |
| 447 | } |
| 448 | if (typeof fieldLike !== "object" || fieldLike === null) { |
| 449 | throw Error("Expected a Field but object was null/undefined"); |
| 450 | } |
| 451 | if ( |
| 452 | !("type" in fieldLike) || |
| 453 | !("name" in fieldLike) || |
| 454 | !("nullable" in fieldLike) |
| 455 | ) { |
| 456 | throw Error( |
| 457 | "The field passed in is missing a `type`/`name`/`nullable` property" |
| 458 | ); |
| 459 | } |
| 460 | const type = sanitizeType(fieldLike.type); |
| 461 | const name = fieldLike.name; |
| 462 | if (!(typeof name === "string")) { |
| 463 | throw Error("The field passed in had a non-string `name` property"); |
| 464 | } |
| 465 | const nullable = fieldLike.nullable; |
| 466 | if (!(typeof nullable === "boolean")) { |
| 467 | throw Error("The field passed in had a non-boolean `nullable` property"); |
| 468 | } |
| 469 | let metadata; |
| 470 | if ("metadata" in fieldLike) { |
| 471 | metadata = sanitizeMetadata(fieldLike.metadata); |
| 472 | } |
| 473 | return new Field(name, type, nullable, metadata); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Convert something schemaLike into a Schema instance |
| 478 | * |
| 479 | * This method is often needed even when the caller is using a Schema |
| 480 | * instance because they might be using a different instance of apache-arrow |
| 481 | * than lancedb is using. |
| 482 | */ |
| 483 | export function sanitizeSchema(schemaLike: unknown): Schema { |
| 484 | if (schemaLike instanceof Schema) { |
| 485 | return schemaLike; |
| 486 | } |
| 487 | if (typeof schemaLike !== "object" || schemaLike === null) { |
| 488 | throw Error("Expected a Schema but object was null/undefined"); |
| 489 | } |
| 490 | if (!("fields" in schemaLike)) { |
| 491 | throw Error( |
| 492 | "The schema passed in does not appear to be a schema (no 'fields' property)" |
| 493 | ); |
| 494 | } |
| 495 | let metadata; |
| 496 | if ("metadata" in schemaLike) { |
| 497 | metadata = sanitizeMetadata(schemaLike.metadata); |
| 498 | } |
| 499 | if (!Array.isArray(schemaLike.fields)) { |
| 500 | throw Error( |
| 501 | "The schema passed in had a 'fields' property but it was not an array" |
| 502 | ); |
| 503 | } |
| 504 | const sanitizedFields = schemaLike.fields.map((field) => |
| 505 | sanitizeField(field) |
| 506 | ); |
| 507 | return new Schema(sanitizedFields, metadata); |
| 508 | } |