Elffuss BraidHub

kikocis/lancedb public

f371856ec79a · 3.3 KiBRenderedRawPermalink
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
11An enhanced version of the makeTable function from Apache Arrow
12that supports nested fields and embeddings columns.
13
14(typically you do not need to call this function. It will be called automatically
15when creating a table or adding data to it)
16
17This function converts an array of Record<String, any> (row-major JS objects)
18to an Arrow Table (a columnar structure)
19
20Note that it currently does not support nulls.
21
22If a schema is provided then it will be used to determine the resulting array
23types. Fields will also be reordered to fit the order defined by the schema.
24
25If a schema is not provided then the types will be inferred and the field order
26will be controlled by the order of properties in the first record. If a type
27is inferred it will always be nullable.
28
29If the input is empty then a schema must be provided to create an empty table.
30
31When a schema is not specified then data types will be inferred. The inference
32rules 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`&lt;`string`, `unknown`&gt;[]
44
45• **options?**: `Partial`&lt;[`MakeArrowTableOptions`](../classes/MakeArrowTableOptions.md)&gt;
46
47• **metadata?**: `Map`&lt;`string`, `string`&gt;
48
49## Returns
50
51`ArrowTable`
52
53## Example
54
55import { fromTableToBuffer, makeArrowTable } from "../arrow";
56import { Field, FixedSizeList, Float16, Float32, Int32, Schema } from "apache-arrow";
57
58const 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
70By default it assumes that the column named `vector` is a vector column
71and it will be converted into a fixed size list array of type float32.
72The `vectorColumns` option can be used to support other vector column
73names and data types.
74
75```ts
76
77const 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
93You can specify the vector column types and names using the options as well
94
95```typescript
96
97const 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 ]);
103const 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 }
113assert.deepEqual(table.schema, schema)
114```