| 1 | [**@lancedb/lancedb**](../README.md) • **Docs** |
| 2 | |
| 3 | *** |
| 4 | |
| 5 | [@lancedb/lancedb](../globals.md) / makeArrowTable |
| 6 | |
| 7 | # Function: makeArrowTable() |
| 8 | |
| 9 | > **makeArrowTable**(`data`, `options`?, `metadata`?): `ArrowTable` |
| 10 | |
| 11 | An enhanced version of the makeTable function from Apache Arrow |
| 12 | that supports nested fields and embeddings columns. |
| 13 | |
| 14 | (typically you do not need to call this function. It will be called automatically |
| 15 | when creating a table or adding data to it) |
| 16 | |
| 17 | This function converts an array of Record<String, any> (row-major JS objects) |
| 18 | to an Arrow Table (a columnar structure) |
| 19 | |
| 20 | Note that it currently does not support nulls. |
| 21 | |
| 22 | If a schema is provided then it will be used to determine the resulting array |
| 23 | types. Fields will also be reordered to fit the order defined by the schema. |
| 24 | |
| 25 | If a schema is not provided then the types will be inferred and the field order |
| 26 | will be controlled by the order of properties in the first record. If a type |
| 27 | is inferred it will always be nullable. |
| 28 | |
| 29 | If the input is empty then a schema must be provided to create an empty table. |
| 30 | |
| 31 | When a schema is not specified then data types will be inferred. The inference |
| 32 | rules are as follows: |
| 33 | |
| 34 | - boolean => Bool |
| 35 | - number => Float64 |
| 36 | - String => Utf8 |
| 37 | - Buffer => Binary |
| 38 | - Record<String, any> => Struct |
| 39 | - Array<any> => List |
| 40 | |
| 41 | ## Parameters |
| 42 | |
| 43 | • **data**: `Record`<`string`, `unknown`>[] |
| 44 | |
| 45 | • **options?**: `Partial`<[`MakeArrowTableOptions`](../classes/MakeArrowTableOptions.md)> |
| 46 | |
| 47 | • **metadata?**: `Map`<`string`, `string`> |
| 48 | |
| 49 | ## Returns |
| 50 | |
| 51 | `ArrowTable` |
| 52 | |
| 53 | ## Example |
| 54 | |
| 55 | import { fromTableToBuffer, makeArrowTable } from "../arrow"; |
| 56 | import { Field, FixedSizeList, Float16, Float32, Int32, Schema } from "apache-arrow"; |
| 57 | |
| 58 | const schema = new Schema([ |
| 59 | new Field("a", new Int32()), |
| 60 | new Field("b", new Float32()), |
| 61 | new Field("c", new FixedSizeList(3, new Field("item", new Float16()))), |
| 62 | ]); |
| 63 | const table = makeArrowTable([ |
| 64 | { a: 1, b: 2, c: [1, 2, 3] }, |
| 65 | { a: 4, b: 5, c: [4, 5, 6] }, |
| 66 | { a: 7, b: 8, c: [7, 8, 9] }, |
| 67 | ], { schema }); |
| 68 | ``` |
| 69 | |
| 70 | By default it assumes that the column named `vector` is a vector column |
| 71 | and it will be converted into a fixed size list array of type float32. |
| 72 | The `vectorColumns` option can be used to support other vector column |
| 73 | names and data types. |
| 74 | |
| 75 | ```ts |
| 76 | |
| 77 | const schema = new Schema([ |
| 78 | new Field("a", new Float64()), |
| 79 | new Field("b", new Float64()), |
| 80 | new Field( |
| 81 | "vector", |
| 82 | new FixedSizeList(3, new Field("item", new Float32())) |
| 83 | ), |
| 84 | ]); |
| 85 | const table = makeArrowTable([ |
| 86 | { a: 1, b: 2, vector: [1, 2, 3] }, |
| 87 | { a: 4, b: 5, vector: [4, 5, 6] }, |
| 88 | { a: 7, b: 8, vector: [7, 8, 9] }, |
| 89 | ]); |
| 90 | assert.deepEqual(table.schema, schema); |
| 91 | ``` |
| 92 | |
| 93 | You can specify the vector column types and names using the options as well |
| 94 | |
| 95 | ```typescript |
| 96 | |
| 97 | const schema = new Schema([ |
| 98 | new Field('a', new Float64()), |
| 99 | new Field('b', new Float64()), |
| 100 | new Field('vec1', new FixedSizeList(3, new Field('item', new Float16()))), |
| 101 | new Field('vec2', new FixedSizeList(3, new Field('item', new Float16()))) |
| 102 | ]); |
| 103 | const table = makeArrowTable([ |
| 104 | { a: 1, b: 2, vec1: [1, 2, 3], vec2: [2, 4, 6] }, |
| 105 | { a: 4, b: 5, vec1: [4, 5, 6], vec2: [8, 10, 12] }, |
| 106 | { a: 7, b: 8, vec1: [7, 8, 9], vec2: [14, 16, 18] } |
| 107 | ], { |
| 108 | vectorColumns: { |
| 109 | vec1: { type: new Float16() }, |
| 110 | vec2: { type: new Float16() } |
| 111 | } |
| 112 | } |
| 113 | assert.deepEqual(table.schema, schema) |
| 114 | ``` |