@loaders.gl/schema-utils 4.4.0-alpha.1
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.
- package/LICENSE +41 -0
- package/README.md +9 -0
- package/dist/dist.dev.js +14269 -0
- package/dist/dist.min.js +18 -0
- package/dist/index.cjs +2436 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +73 -0
- package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.d.ts +11 -0
- package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.d.ts.map +1 -0
- package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.js +40 -0
- package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.d.ts +11 -0
- package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.d.ts.map +1 -0
- package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.js +32 -0
- package/dist/lib/mesh/convert-mesh-to-table.d.ts +18 -0
- package/dist/lib/mesh/convert-mesh-to-table.d.ts.map +1 -0
- package/dist/lib/mesh/convert-mesh-to-table.js +60 -0
- package/dist/lib/mesh/convert-table-to-mesh.d.ts +7 -0
- package/dist/lib/mesh/convert-table-to-mesh.d.ts.map +1 -0
- package/dist/lib/mesh/convert-table-to-mesh.js +40 -0
- package/dist/lib/mesh/deduce-mesh-schema.d.ts +23 -0
- package/dist/lib/mesh/deduce-mesh-schema.d.ts.map +1 -0
- package/dist/lib/mesh/deduce-mesh-schema.js +62 -0
- package/dist/lib/mesh/mesh-utils.d.ts +23 -0
- package/dist/lib/mesh/mesh-utils.d.ts.map +1 -0
- package/dist/lib/mesh/mesh-utils.js +46 -0
- package/dist/lib/schema/convert-arrow-schema.d.ts +23 -0
- package/dist/lib/schema/convert-arrow-schema.d.ts.map +1 -0
- package/dist/lib/schema/convert-arrow-schema.js +280 -0
- package/dist/lib/schema/data-type.d.ts +16 -0
- package/dist/lib/schema/data-type.d.ts.map +1 -0
- package/dist/lib/schema/data-type.js +88 -0
- package/dist/lib/schema/deduce-table-schema.d.ts +14 -0
- package/dist/lib/schema/deduce-table-schema.d.ts.map +1 -0
- package/dist/lib/schema/deduce-table-schema.js +100 -0
- package/dist/lib/table/arrow-api/arrow-like-field.d.ts +19 -0
- package/dist/lib/table/arrow-api/arrow-like-field.d.ts.map +1 -0
- package/dist/lib/table/arrow-api/arrow-like-field.js +36 -0
- package/dist/lib/table/arrow-api/arrow-like-interface.d.ts +43 -0
- package/dist/lib/table/arrow-api/arrow-like-interface.d.ts.map +1 -0
- package/dist/lib/table/arrow-api/arrow-like-interface.js +4 -0
- package/dist/lib/table/arrow-api/arrow-like-schema.d.ts +12 -0
- package/dist/lib/table/arrow-api/arrow-like-schema.d.ts.map +1 -0
- package/dist/lib/table/arrow-api/arrow-like-schema.js +81 -0
- package/dist/lib/table/arrow-api/arrow-like-table.d.ts +28 -0
- package/dist/lib/table/arrow-api/arrow-like-table.d.ts.map +1 -0
- package/dist/lib/table/arrow-api/arrow-like-table.js +63 -0
- package/dist/lib/table/arrow-api/arrow-like-type.d.ts +177 -0
- package/dist/lib/table/arrow-api/arrow-like-type.d.ts.map +1 -0
- package/dist/lib/table/arrow-api/arrow-like-type.js +442 -0
- package/dist/lib/table/arrow-api/enum.d.ts +92 -0
- package/dist/lib/table/arrow-api/enum.d.ts.map +1 -0
- package/dist/lib/table/arrow-api/enum.js +97 -0
- package/dist/lib/table/arrow-api/get-type-info.d.ts +13 -0
- package/dist/lib/table/arrow-api/get-type-info.d.ts.map +1 -0
- package/dist/lib/table/arrow-api/get-type-info.js +27 -0
- package/dist/lib/table/arrow-api/index.d.ts +5 -0
- package/dist/lib/table/arrow-api/index.d.ts.map +1 -0
- package/dist/lib/table/arrow-api/index.js +7 -0
- package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.d.ts +10 -0
- package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.d.ts.map +1 -0
- package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.js +67 -0
- package/dist/lib/table/batch-builder/arrow-table-builder.d.ts +25 -0
- package/dist/lib/table/batch-builder/arrow-table-builder.d.ts.map +1 -0
- package/dist/lib/table/batch-builder/arrow-table-builder.js +99 -0
- package/dist/lib/table/batch-builder/base-table-batch-aggregator.d.ts +19 -0
- package/dist/lib/table/batch-builder/base-table-batch-aggregator.d.ts.map +1 -0
- package/dist/lib/table/batch-builder/base-table-batch-aggregator.js +63 -0
- package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.d.ts +20 -0
- package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.d.ts.map +1 -0
- package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.js +83 -0
- package/dist/lib/table/batch-builder/row-table-batch-aggregator.d.ts +21 -0
- package/dist/lib/table/batch-builder/row-table-batch-aggregator.d.ts.map +1 -0
- package/dist/lib/table/batch-builder/row-table-batch-aggregator.js +85 -0
- package/dist/lib/table/batch-builder/table-batch-aggregator.d.ts +24 -0
- package/dist/lib/table/batch-builder/table-batch-aggregator.d.ts.map +1 -0
- package/dist/lib/table/batch-builder/table-batch-aggregator.js +4 -0
- package/dist/lib/table/batch-builder/table-batch-builder.d.ts +52 -0
- package/dist/lib/table/batch-builder/table-batch-builder.d.ts.map +1 -0
- package/dist/lib/table/batch-builder/table-batch-builder.js +148 -0
- package/dist/lib/table/batches/convert-batches.d.ts +10 -0
- package/dist/lib/table/batches/convert-batches.d.ts.map +1 -0
- package/dist/lib/table/batches/convert-batches.js +45 -0
- package/dist/lib/table/batches/make-arrow-batch-iterator.d.ts +17 -0
- package/dist/lib/table/batches/make-arrow-batch-iterator.d.ts.map +1 -0
- package/dist/lib/table/batches/make-arrow-batch-iterator.js +57 -0
- package/dist/lib/table/batches/make-table-batch-iterator.d.ts +15 -0
- package/dist/lib/table/batches/make-table-batch-iterator.d.ts.map +1 -0
- package/dist/lib/table/batches/make-table-batch-iterator.js +21 -0
- package/dist/lib/table/batches/make-table-from-batches.d.ts +9 -0
- package/dist/lib/table/batches/make-table-from-batches.d.ts.map +1 -0
- package/dist/lib/table/batches/make-table-from-batches.js +62 -0
- package/dist/lib/table/tables/convert-arrow-table.d.ts +24 -0
- package/dist/lib/table/tables/convert-arrow-table.d.ts.map +1 -0
- package/dist/lib/table/tables/convert-arrow-table.js +150 -0
- package/dist/lib/table/tables/convert-table.d.ts +43 -0
- package/dist/lib/table/tables/convert-table.d.ts.map +1 -0
- package/dist/lib/table/tables/convert-table.js +120 -0
- package/dist/lib/table/tables/make-table.d.ts +67 -0
- package/dist/lib/table/tables/make-table.d.ts.map +1 -0
- package/dist/lib/table/tables/make-table.js +101 -0
- package/dist/lib/table/tables/row-utils.d.ts +15 -0
- package/dist/lib/table/tables/row-utils.d.ts.map +1 -0
- package/dist/lib/table/tables/row-utils.js +49 -0
- package/dist/lib/table/tables/table-accessors.d.ts +68 -0
- package/dist/lib/table/tables/table-accessors.d.ts.map +1 -0
- package/dist/lib/table/tables/table-accessors.js +350 -0
- package/dist/lib/table/tables/table-column.d.ts +7 -0
- package/dist/lib/table/tables/table-column.d.ts.map +1 -0
- package/dist/lib/table/tables/table-column.js +39 -0
- package/dist/lib/table/tables/table-types.d.ts +12 -0
- package/dist/lib/table/tables/table-types.d.ts.map +1 -0
- package/dist/lib/table/tables/table-types.js +23 -0
- package/dist/lib/utils/assert.d.ts +2 -0
- package/dist/lib/utils/assert.d.ts.map +1 -0
- package/dist/lib/utils/assert.js +8 -0
- package/dist/lib/utils/async-queue.d.ts +18 -0
- package/dist/lib/utils/async-queue.d.ts.map +1 -0
- package/dist/lib/utils/async-queue.js +90 -0
- package/dist/types/types.d.ts +15 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/types.js +4 -0
- package/package.json +52 -0
- package/src/index.ts +134 -0
- package/src/lib/arrow-utils/arrow-fixed-size-list-utils.ts +63 -0
- package/src/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.ts +48 -0
- package/src/lib/mesh/convert-mesh-to-table.ts +77 -0
- package/src/lib/mesh/convert-table-to-mesh.ts +49 -0
- package/src/lib/mesh/deduce-mesh-schema.ts +75 -0
- package/src/lib/mesh/mesh-utils.ts +64 -0
- package/src/lib/schema/convert-arrow-schema.ts +301 -0
- package/src/lib/schema/data-type.ts +103 -0
- package/src/lib/schema/deduce-table-schema.ts +119 -0
- package/src/lib/table/arrow-api/arrow-like-field.ts +53 -0
- package/src/lib/table/arrow-api/arrow-like-interface.ts +84 -0
- package/src/lib/table/arrow-api/arrow-like-schema.ts +101 -0
- package/src/lib/table/arrow-api/arrow-like-table.ts +85 -0
- package/src/lib/table/arrow-api/arrow-like-type.ts +502 -0
- package/src/lib/table/arrow-api/enum.ts +99 -0
- package/src/lib/table/arrow-api/get-type-info.ts +38 -0
- package/src/lib/table/arrow-api/index.ts +8 -0
- package/src/lib/table/batch-builder/arrow-table-batch-aggregator-builders.ts.disabled +190 -0
- package/src/lib/table/batch-builder/arrow-table-batch-aggregator.ts +82 -0
- package/src/lib/table/batch-builder/arrow-table-builder.ts +112 -0
- package/src/lib/table/batch-builder/base-table-batch-aggregator.ts +82 -0
- package/src/lib/table/batch-builder/columnar-table-batch-aggregator.ts +99 -0
- package/src/lib/table/batch-builder/row-table-batch-aggregator.ts +108 -0
- package/src/lib/table/batch-builder/table-batch-aggregator.ts +28 -0
- package/src/lib/table/batch-builder/table-batch-builder.ts +185 -0
- package/src/lib/table/batches/convert-batches.ts +83 -0
- package/src/lib/table/batches/make-arrow-batch-iterator.ts +75 -0
- package/src/lib/table/batches/make-table-batch-iterator.ts +25 -0
- package/src/lib/table/batches/make-table-from-batches.ts +84 -0
- package/src/lib/table/tables/convert-arrow-table.ts +198 -0
- package/src/lib/table/tables/convert-table.ts +151 -0
- package/src/lib/table/tables/make-table.ts +127 -0
- package/src/lib/table/tables/row-utils.ts +59 -0
- package/src/lib/table/tables/table-accessors.ts +417 -0
- package/src/lib/table/tables/table-column.ts +49 -0
- package/src/lib/table/tables/table-types.ts +37 -0
- package/src/lib/utils/assert.ts +8 -0
- package/src/lib/utils/async-queue.ts +104 -0
- package/src/types/types.ts +45 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {DataType, Field, Schema, SchemaMetadata} from '@loaders.gl/schema';
|
|
6
|
+
import * as arrow from 'apache-arrow';
|
|
7
|
+
|
|
8
|
+
/** Convert Apache Arrow Schema (class instance) to a serialized Schema (plain data) */
|
|
9
|
+
export function convertArrowToSchema(arrowSchema: arrow.Schema): Schema {
|
|
10
|
+
return serializeArrowSchema(arrowSchema);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Convert Apache Arrow Schema (class instance) to a serialized Schema (plain data) */
|
|
14
|
+
export function convertSchemaToArrow(schema: Schema): arrow.Schema {
|
|
15
|
+
return deserializeArrowSchema(schema);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Convert Apache Arrow Schema (class instance) to a serialized Schema (plain data) */
|
|
19
|
+
export function serializeArrowSchema(arrowSchema: arrow.Schema): Schema {
|
|
20
|
+
return {
|
|
21
|
+
fields: arrowSchema.fields.map((arrowField) => serializeArrowField(arrowField)),
|
|
22
|
+
metadata: serializeArrowMetadata(arrowSchema.metadata)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Convert a serialized Schema (plain data) to an Apache Arrow Schema (class instance) */
|
|
27
|
+
export function deserializeArrowSchema(schema: Schema): arrow.Schema {
|
|
28
|
+
return new arrow.Schema(
|
|
29
|
+
schema.fields.map((field) => deserializeArrowField(field)),
|
|
30
|
+
deserializeArrowMetadata(schema.metadata)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Convert Apache Arrow Schema metadata (Map<string, string>) to serialized metadata (Record<string, string> */
|
|
35
|
+
export function serializeArrowMetadata(arrowMetadata: Map<string, string>): SchemaMetadata {
|
|
36
|
+
return Object.fromEntries(arrowMetadata);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Convert serialized metadata (Record<string, string> to Apache Arrow Schema metadata (Map<string, string>) to */
|
|
40
|
+
export function deserializeArrowMetadata(metadata?: SchemaMetadata): Map<string, string> {
|
|
41
|
+
return metadata ? new Map(Object.entries(metadata)) : new Map<string, string>();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Convert Apache Arrow Field (class instance) to serialized Field (plain data) */
|
|
45
|
+
export function serializeArrowField(field: arrow.Field): Field {
|
|
46
|
+
return {
|
|
47
|
+
name: field.name,
|
|
48
|
+
type: serializeArrowType(field.type),
|
|
49
|
+
nullable: field.nullable,
|
|
50
|
+
metadata: serializeArrowMetadata(field.metadata)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Convert a serialized Field (plain data) to an Apache Arrow Field (class instance)*/
|
|
55
|
+
export function deserializeArrowField(field: Field): arrow.Field {
|
|
56
|
+
return new arrow.Field(
|
|
57
|
+
field.name,
|
|
58
|
+
deserializeArrowType(field.type),
|
|
59
|
+
field.nullable,
|
|
60
|
+
deserializeArrowMetadata(field.metadata)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Converts a serializable loaders.gl data type to hydrated arrow data type */
|
|
65
|
+
// eslint-disable-next-line complexity
|
|
66
|
+
export function serializeArrowType(arrowType: arrow.DataType): DataType {
|
|
67
|
+
switch (arrowType.constructor) {
|
|
68
|
+
case arrow.Null:
|
|
69
|
+
return 'null';
|
|
70
|
+
case arrow.Binary:
|
|
71
|
+
return 'binary';
|
|
72
|
+
case arrow.Bool:
|
|
73
|
+
return 'bool';
|
|
74
|
+
case arrow.Int:
|
|
75
|
+
const intType = arrowType as arrow.Int;
|
|
76
|
+
return `${intType.isSigned ? 'u' : ''}int${intType.bitWidth}`;
|
|
77
|
+
case arrow.Int8:
|
|
78
|
+
return 'int8';
|
|
79
|
+
case arrow.Int16:
|
|
80
|
+
return 'int16';
|
|
81
|
+
case arrow.Int32:
|
|
82
|
+
return 'int32';
|
|
83
|
+
case arrow.Int64:
|
|
84
|
+
return 'int64';
|
|
85
|
+
case arrow.Uint8:
|
|
86
|
+
return 'uint8';
|
|
87
|
+
case arrow.Uint16:
|
|
88
|
+
return 'uint16';
|
|
89
|
+
case arrow.Uint32:
|
|
90
|
+
return 'uint32';
|
|
91
|
+
case arrow.Uint64:
|
|
92
|
+
return 'uint64';
|
|
93
|
+
case arrow.Float:
|
|
94
|
+
const precision = (arrowType as arrow.Float).precision;
|
|
95
|
+
// return `float(precision + 1) * 16`;
|
|
96
|
+
switch (precision) {
|
|
97
|
+
case arrow.Precision.HALF:
|
|
98
|
+
return 'float16';
|
|
99
|
+
case arrow.Precision.SINGLE:
|
|
100
|
+
return 'float32';
|
|
101
|
+
case arrow.Precision.DOUBLE:
|
|
102
|
+
return 'float64';
|
|
103
|
+
default:
|
|
104
|
+
return 'float16';
|
|
105
|
+
}
|
|
106
|
+
case arrow.Float16:
|
|
107
|
+
return 'float16';
|
|
108
|
+
case arrow.Float32:
|
|
109
|
+
return 'float32';
|
|
110
|
+
case arrow.Float64:
|
|
111
|
+
return 'float64';
|
|
112
|
+
case arrow.Utf8:
|
|
113
|
+
return 'utf8';
|
|
114
|
+
case arrow.Decimal:
|
|
115
|
+
const decimal = arrowType as arrow.Decimal;
|
|
116
|
+
return {
|
|
117
|
+
type: 'decimal',
|
|
118
|
+
bitWidth: decimal.bitWidth,
|
|
119
|
+
precision: decimal.precision,
|
|
120
|
+
scale: decimal.scale
|
|
121
|
+
};
|
|
122
|
+
case arrow.Date_:
|
|
123
|
+
const dateUnit = (arrowType as arrow.Date_).unit;
|
|
124
|
+
return dateUnit === arrow.DateUnit.DAY ? 'date-day' : 'date-millisecond';
|
|
125
|
+
case arrow.DateDay:
|
|
126
|
+
return 'date-day';
|
|
127
|
+
case arrow.DateMillisecond:
|
|
128
|
+
return 'date-millisecond';
|
|
129
|
+
case arrow.Time:
|
|
130
|
+
const timeUnit = (arrowType as arrow.Time).unit;
|
|
131
|
+
switch (timeUnit) {
|
|
132
|
+
case arrow.TimeUnit.SECOND:
|
|
133
|
+
return 'time-second';
|
|
134
|
+
case arrow.TimeUnit.MILLISECOND:
|
|
135
|
+
return 'time-millisecond';
|
|
136
|
+
case arrow.TimeUnit.MICROSECOND:
|
|
137
|
+
return 'time-microsecond';
|
|
138
|
+
case arrow.TimeUnit.NANOSECOND:
|
|
139
|
+
return 'time-nanosecond';
|
|
140
|
+
default:
|
|
141
|
+
return 'time-second';
|
|
142
|
+
}
|
|
143
|
+
case arrow.TimeMillisecond:
|
|
144
|
+
return 'time-millisecond';
|
|
145
|
+
case arrow.TimeSecond:
|
|
146
|
+
return 'time-second';
|
|
147
|
+
case arrow.TimeMicrosecond:
|
|
148
|
+
return 'time-microsecond';
|
|
149
|
+
case arrow.TimeNanosecond:
|
|
150
|
+
return 'time-nanosecond';
|
|
151
|
+
case arrow.Timestamp:
|
|
152
|
+
const timeStampUnit = (arrowType as arrow.Timestamp).unit;
|
|
153
|
+
switch (timeStampUnit) {
|
|
154
|
+
case arrow.TimeUnit.SECOND:
|
|
155
|
+
return 'timestamp-second';
|
|
156
|
+
case arrow.TimeUnit.MILLISECOND:
|
|
157
|
+
return 'timestamp-millisecond';
|
|
158
|
+
case arrow.TimeUnit.MICROSECOND:
|
|
159
|
+
return 'timestamp-microsecond';
|
|
160
|
+
case arrow.TimeUnit.NANOSECOND:
|
|
161
|
+
return 'timestamp-nanosecond';
|
|
162
|
+
default:
|
|
163
|
+
return 'timestamp-second';
|
|
164
|
+
}
|
|
165
|
+
case arrow.TimestampSecond:
|
|
166
|
+
return 'timestamp-second';
|
|
167
|
+
case arrow.TimestampMillisecond:
|
|
168
|
+
return 'timestamp-millisecond';
|
|
169
|
+
case arrow.TimestampMicrosecond:
|
|
170
|
+
return 'timestamp-microsecond';
|
|
171
|
+
case arrow.TimestampNanosecond:
|
|
172
|
+
return 'timestamp-nanosecond';
|
|
173
|
+
case arrow.Interval:
|
|
174
|
+
const intervalUnit = (arrowType as arrow.Interval).unit;
|
|
175
|
+
switch (intervalUnit) {
|
|
176
|
+
case arrow.IntervalUnit.DAY_TIME:
|
|
177
|
+
return 'interval-daytime';
|
|
178
|
+
case arrow.IntervalUnit.YEAR_MONTH:
|
|
179
|
+
return 'interval-yearmonth';
|
|
180
|
+
default:
|
|
181
|
+
return 'interval-daytime';
|
|
182
|
+
}
|
|
183
|
+
case arrow.IntervalDayTime:
|
|
184
|
+
return 'interval-daytime';
|
|
185
|
+
case arrow.IntervalYearMonth:
|
|
186
|
+
return 'interval-yearmonth';
|
|
187
|
+
case arrow.Map_:
|
|
188
|
+
const mapType = arrowType as arrow.Map_;
|
|
189
|
+
return {
|
|
190
|
+
type: 'map',
|
|
191
|
+
keysSorted: mapType.keysSorted,
|
|
192
|
+
children: mapType.children.map((arrowField) => serializeArrowField(arrowField))
|
|
193
|
+
};
|
|
194
|
+
case arrow.List:
|
|
195
|
+
const listType = arrowType as arrow.List;
|
|
196
|
+
const listField = listType.valueField;
|
|
197
|
+
return {
|
|
198
|
+
type: 'list',
|
|
199
|
+
children: [serializeArrowField(listField)]
|
|
200
|
+
};
|
|
201
|
+
case arrow.FixedSizeList:
|
|
202
|
+
const fixedSizeList = arrowType as arrow.FixedSizeList;
|
|
203
|
+
return {
|
|
204
|
+
type: 'fixed-size-list',
|
|
205
|
+
listSize: fixedSizeList.listSize,
|
|
206
|
+
children: [serializeArrowField(fixedSizeList.children[0])]
|
|
207
|
+
};
|
|
208
|
+
case arrow.Struct:
|
|
209
|
+
const structType = arrowType as arrow.Struct;
|
|
210
|
+
return {
|
|
211
|
+
type: 'struct',
|
|
212
|
+
children: structType.children.map((arrowField) => serializeArrowField(arrowField))
|
|
213
|
+
};
|
|
214
|
+
default:
|
|
215
|
+
throw new Error(`arrow type not supported: ${arrowType.constructor.name}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Converts a serializable loaders.gl data type to hydrated arrow data type */
|
|
220
|
+
// eslint-disable-next-line complexity
|
|
221
|
+
export function deserializeArrowType(dataType: DataType): arrow.DataType {
|
|
222
|
+
if (typeof dataType === 'object') {
|
|
223
|
+
switch (dataType.type) {
|
|
224
|
+
case 'decimal':
|
|
225
|
+
return new arrow.Decimal(dataType.precision, dataType.scale, dataType.bitWidth);
|
|
226
|
+
case 'map':
|
|
227
|
+
let children = dataType.children.map((arrowField) => deserializeArrowField(arrowField));
|
|
228
|
+
return new arrow.Map_(children as any, dataType.keysSorted);
|
|
229
|
+
case 'list':
|
|
230
|
+
const field = deserializeArrowField(dataType.children[0]);
|
|
231
|
+
return new arrow.List(field);
|
|
232
|
+
case 'fixed-size-list':
|
|
233
|
+
const child = deserializeArrowField(dataType.children[0]);
|
|
234
|
+
return new arrow.FixedSizeList(dataType.listSize, child);
|
|
235
|
+
case 'struct':
|
|
236
|
+
children = dataType.children.map((arrowField) => deserializeArrowField(arrowField));
|
|
237
|
+
return new arrow.Struct(children);
|
|
238
|
+
default:
|
|
239
|
+
throw new Error('array type not supported');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
switch (dataType) {
|
|
244
|
+
case 'null':
|
|
245
|
+
return new arrow.Null();
|
|
246
|
+
case 'binary':
|
|
247
|
+
return new arrow.Binary();
|
|
248
|
+
case 'bool':
|
|
249
|
+
return new arrow.Bool();
|
|
250
|
+
case 'int8':
|
|
251
|
+
return new arrow.Int8();
|
|
252
|
+
case 'int16':
|
|
253
|
+
return new arrow.Int16();
|
|
254
|
+
case 'int32':
|
|
255
|
+
return new arrow.Int32();
|
|
256
|
+
case 'int64':
|
|
257
|
+
return new arrow.Int64();
|
|
258
|
+
case 'uint8':
|
|
259
|
+
return new arrow.Uint8();
|
|
260
|
+
case 'uint16':
|
|
261
|
+
return new arrow.Uint16();
|
|
262
|
+
case 'uint32':
|
|
263
|
+
return new arrow.Uint32();
|
|
264
|
+
case 'uint64':
|
|
265
|
+
return new arrow.Uint64();
|
|
266
|
+
case 'float16':
|
|
267
|
+
return new arrow.Float16();
|
|
268
|
+
case 'float32':
|
|
269
|
+
return new arrow.Float32();
|
|
270
|
+
case 'float64':
|
|
271
|
+
return new arrow.Float64();
|
|
272
|
+
case 'utf8':
|
|
273
|
+
return new arrow.Utf8();
|
|
274
|
+
case 'date-day':
|
|
275
|
+
return new arrow.DateDay();
|
|
276
|
+
case 'date-millisecond':
|
|
277
|
+
return new arrow.DateMillisecond();
|
|
278
|
+
case 'time-second':
|
|
279
|
+
return new arrow.TimeSecond();
|
|
280
|
+
case 'time-millisecond':
|
|
281
|
+
return new arrow.TimeMillisecond();
|
|
282
|
+
case 'time-microsecond':
|
|
283
|
+
return new arrow.TimeMicrosecond();
|
|
284
|
+
case 'time-nanosecond':
|
|
285
|
+
return new arrow.TimeNanosecond();
|
|
286
|
+
case 'timestamp-second':
|
|
287
|
+
return new arrow.TimestampSecond();
|
|
288
|
+
case 'timestamp-millisecond':
|
|
289
|
+
return new arrow.TimestampMillisecond();
|
|
290
|
+
case 'timestamp-microsecond':
|
|
291
|
+
return new arrow.TimestampMicrosecond();
|
|
292
|
+
case 'timestamp-nanosecond':
|
|
293
|
+
return new arrow.TimestampNanosecond();
|
|
294
|
+
case 'interval-daytime':
|
|
295
|
+
return new arrow.IntervalDayTime();
|
|
296
|
+
case 'interval-yearmonth':
|
|
297
|
+
return new arrow.IntervalYearMonth();
|
|
298
|
+
default:
|
|
299
|
+
throw new Error('array type not supported');
|
|
300
|
+
}
|
|
301
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {DataType, TypedArray, TypedArrayConstructor, ArrayType} from '@loaders.gl/schema';
|
|
6
|
+
|
|
7
|
+
/** Deduce column types from values */
|
|
8
|
+
export function getDataTypeFromValue(
|
|
9
|
+
value: unknown,
|
|
10
|
+
defaultNumberType: 'float32' = 'float32'
|
|
11
|
+
): DataType {
|
|
12
|
+
if (value instanceof Date) {
|
|
13
|
+
return 'date-millisecond';
|
|
14
|
+
}
|
|
15
|
+
if (value instanceof Number) {
|
|
16
|
+
return defaultNumberType;
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === 'string') {
|
|
19
|
+
return 'utf8';
|
|
20
|
+
}
|
|
21
|
+
if (value === null || value === 'undefined') {
|
|
22
|
+
return 'null';
|
|
23
|
+
}
|
|
24
|
+
return 'null';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Deduces a simple data type "descriptor from a typed array instance
|
|
29
|
+
*/
|
|
30
|
+
export function getDataTypeFromArray(array: ArrayType): {type: DataType; nullable: boolean} {
|
|
31
|
+
let type = getDataTypeFromTypedArray(array as TypedArray);
|
|
32
|
+
if (type !== 'null') {
|
|
33
|
+
return {type, nullable: false};
|
|
34
|
+
}
|
|
35
|
+
if (array.length > 0) {
|
|
36
|
+
type = getDataTypeFromValue(array[0]);
|
|
37
|
+
return {type, nullable: true};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {type: 'null', nullable: true};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Deduces a simple data type "descriptor from a typed array instance
|
|
45
|
+
*/
|
|
46
|
+
export function getDataTypeFromTypedArray(array: TypedArray): DataType {
|
|
47
|
+
switch (array.constructor) {
|
|
48
|
+
case Int8Array:
|
|
49
|
+
return 'int8';
|
|
50
|
+
case Uint8Array:
|
|
51
|
+
case Uint8ClampedArray:
|
|
52
|
+
return 'uint8';
|
|
53
|
+
case Int16Array:
|
|
54
|
+
return 'int16';
|
|
55
|
+
case Uint16Array:
|
|
56
|
+
return 'uint16';
|
|
57
|
+
case Int32Array:
|
|
58
|
+
return 'int32';
|
|
59
|
+
case Uint32Array:
|
|
60
|
+
return 'uint32';
|
|
61
|
+
case Float32Array:
|
|
62
|
+
return 'float32';
|
|
63
|
+
case Float64Array:
|
|
64
|
+
return 'float64';
|
|
65
|
+
default:
|
|
66
|
+
return 'null';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function getArrayTypeFromDataType(
|
|
71
|
+
type: DataType,
|
|
72
|
+
nullable: boolean | undefined
|
|
73
|
+
): TypedArrayConstructor | ArrayConstructor {
|
|
74
|
+
if (!nullable) {
|
|
75
|
+
switch (type) {
|
|
76
|
+
case 'int8':
|
|
77
|
+
return Int8Array;
|
|
78
|
+
case 'uint8':
|
|
79
|
+
return Uint8Array;
|
|
80
|
+
case 'int16':
|
|
81
|
+
return Int16Array;
|
|
82
|
+
case 'uint16':
|
|
83
|
+
return Uint16Array;
|
|
84
|
+
case 'int32':
|
|
85
|
+
return Int32Array;
|
|
86
|
+
case 'uint32':
|
|
87
|
+
return Uint32Array;
|
|
88
|
+
case 'float32':
|
|
89
|
+
return Float32Array;
|
|
90
|
+
case 'float64':
|
|
91
|
+
return Float64Array;
|
|
92
|
+
default:
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// if (typeof BigInt64Array !== 'undefined') {
|
|
98
|
+
// TYPED_ARRAY_TO_TYPE.BigInt64Array = new Int64();
|
|
99
|
+
// TYPED_ARRAY_TO_TYPE.BigUint64Array = new Uint64();
|
|
100
|
+
// }
|
|
101
|
+
|
|
102
|
+
return Array;
|
|
103
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
// Type deduction
|
|
6
|
+
import type {Schema, Field, ArrayType, Table} from '@loaders.gl/schema';
|
|
7
|
+
import {getDataTypeFromArray, getDataTypeFromValue} from './data-type';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* SCHEMA SUPPORT - AUTODEDUCTION
|
|
11
|
+
* @param {*} table
|
|
12
|
+
* @param {*} schema
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
export function deduceTableSchema(table: Table): Schema {
|
|
16
|
+
switch (table.shape) {
|
|
17
|
+
case 'array-row-table':
|
|
18
|
+
case 'object-row-table':
|
|
19
|
+
return deduceSchemaFromRows(table.data);
|
|
20
|
+
|
|
21
|
+
case 'geojson-table':
|
|
22
|
+
return deduceSchemaFromGeoJSON(table.features);
|
|
23
|
+
|
|
24
|
+
case 'columnar-table':
|
|
25
|
+
return deduceSchemaFromColumns(table.data);
|
|
26
|
+
|
|
27
|
+
case 'arrow-table':
|
|
28
|
+
default:
|
|
29
|
+
throw new Error('Deduce schema');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function deduceSchema(
|
|
34
|
+
data: unknown[][] | {[key: string]: unknown}[] | {[key: string]: unknown[]}
|
|
35
|
+
): Schema {
|
|
36
|
+
return Array.isArray(data) ? deduceSchemaFromRows(data) : deduceSchemaFromColumns(data);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Given an object with columnar arrays, try to deduce a schema */
|
|
40
|
+
function deduceSchemaFromColumns(columnarTable: {[key: string]: ArrayType}): Schema {
|
|
41
|
+
const fields: Field[] = [];
|
|
42
|
+
for (const [columnName, column] of Object.entries(columnarTable)) {
|
|
43
|
+
const field = deduceFieldFromColumn(column, columnName);
|
|
44
|
+
fields.push(field);
|
|
45
|
+
}
|
|
46
|
+
return {fields, metadata: {}};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Given an array of rows, try to deduce a schema */
|
|
50
|
+
function deduceSchemaFromRows(rowTable: unknown[][] | {[key: string]: unknown}[]): Schema {
|
|
51
|
+
if (!rowTable.length) {
|
|
52
|
+
throw new Error('deduce from empty table');
|
|
53
|
+
}
|
|
54
|
+
const fields: Field[] = [];
|
|
55
|
+
const row0 = rowTable[0];
|
|
56
|
+
// TODO - fields can be nullable, false detection...
|
|
57
|
+
// Could look at additional rows if nulls in first row
|
|
58
|
+
// TODO - if array, column names will be numbers
|
|
59
|
+
for (const [columnName, value] of Object.entries(row0)) {
|
|
60
|
+
fields.push(deduceFieldFromValue(value, columnName));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return {fields, metadata: {}};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Given a GeoJSON, try to deduce a schema */
|
|
67
|
+
function deduceSchemaFromGeoJSON(features: {properties: Record<string, number> | null}[]): Schema {
|
|
68
|
+
if (!features.length) {
|
|
69
|
+
throw new Error('deduce from empty table');
|
|
70
|
+
}
|
|
71
|
+
const fields: Field[] = [];
|
|
72
|
+
const row0 = features[0].properties || {};
|
|
73
|
+
// TODO - fields can be nullable, false detection...
|
|
74
|
+
// Could look at additional rows if nulls in first row
|
|
75
|
+
// TODO - if array, column names will be numbers
|
|
76
|
+
for (const [columnName, value] of Object.entries(row0)) {
|
|
77
|
+
fields.push(deduceFieldFromValue(value, columnName));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {fields, metadata: {}};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Given a column (i.e. array), attempt to deduce an appropriate `Field` */
|
|
84
|
+
function deduceFieldFromColumn(column: ArrayType, name: string): Field {
|
|
85
|
+
if (ArrayBuffer.isView(column)) {
|
|
86
|
+
const type = getDataTypeFromArray(column);
|
|
87
|
+
return {
|
|
88
|
+
name,
|
|
89
|
+
type: type.type || 'null',
|
|
90
|
+
nullable: type.nullable
|
|
91
|
+
// metadata: {}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (Array.isArray(column) && column.length > 0) {
|
|
96
|
+
const value = column[0];
|
|
97
|
+
const type = getDataTypeFromValue(value);
|
|
98
|
+
// TODO - support nested schemas?
|
|
99
|
+
return {
|
|
100
|
+
name,
|
|
101
|
+
type,
|
|
102
|
+
nullable: true
|
|
103
|
+
// metadata: {},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
throw new Error('empty table');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Given a value, attempt to deduce an appropriate `Field` */
|
|
111
|
+
function deduceFieldFromValue(value: unknown, name: string): Field {
|
|
112
|
+
const type = getDataTypeFromValue(value);
|
|
113
|
+
return {
|
|
114
|
+
name,
|
|
115
|
+
type,
|
|
116
|
+
nullable: true
|
|
117
|
+
// metadata: {}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {DataType} from './arrow-like-type';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ArrowJS `Field` API-compatible class for row-based tables
|
|
9
|
+
* https://loaders.gl/arrowjs/docs/api-reference/field
|
|
10
|
+
* A field holds name, nullable, and metadata information about a table "column"
|
|
11
|
+
* A Schema is essentially a list of fields
|
|
12
|
+
*/
|
|
13
|
+
export class ArrowLikeField {
|
|
14
|
+
name: string;
|
|
15
|
+
type: DataType;
|
|
16
|
+
nullable: boolean;
|
|
17
|
+
metadata: Map<string, string>;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
name: string,
|
|
21
|
+
type: DataType,
|
|
22
|
+
nullable = false,
|
|
23
|
+
metadata: Map<string, string> = new Map()
|
|
24
|
+
) {
|
|
25
|
+
this.name = name;
|
|
26
|
+
this.type = type;
|
|
27
|
+
this.nullable = nullable;
|
|
28
|
+
this.metadata = metadata;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get typeId(): number {
|
|
32
|
+
return this.type && this.type.typeId;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
clone(): ArrowLikeField {
|
|
36
|
+
return new ArrowLikeField(this.name, this.type, this.nullable, this.metadata);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
compareTo(other: this): boolean {
|
|
40
|
+
return (
|
|
41
|
+
this.name === other.name &&
|
|
42
|
+
this.type === other.type &&
|
|
43
|
+
this.nullable === other.nullable &&
|
|
44
|
+
this.metadata === other.metadata
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
toString(): string {
|
|
49
|
+
return `${JSON.stringify(this.type)}${this.nullable ? ', nullable' : ''}${
|
|
50
|
+
this.metadata ? `, metadata: ${JSON.stringify(this.metadata)}` : ''
|
|
51
|
+
}`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {DataType} from './arrow-like-type';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ArrowJS `Field` API-compatible class for row-based tables
|
|
9
|
+
* https://loaders.gl/arrowjs/docs/api-reference/field
|
|
10
|
+
* A field holds name, nullable, and metadata information about a table "column"
|
|
11
|
+
* A Schema is essentially a list of fields
|
|
12
|
+
*/
|
|
13
|
+
export interface Field {
|
|
14
|
+
name: string;
|
|
15
|
+
type: DataType;
|
|
16
|
+
nullable: boolean;
|
|
17
|
+
metadata: Map<string, string>;
|
|
18
|
+
|
|
19
|
+
// constructor(
|
|
20
|
+
// name: string,
|
|
21
|
+
// type: DataType,
|
|
22
|
+
// nullable?,
|
|
23
|
+
// metadata?
|
|
24
|
+
// );
|
|
25
|
+
|
|
26
|
+
typeId(): number;
|
|
27
|
+
|
|
28
|
+
clone(): Field;
|
|
29
|
+
|
|
30
|
+
compareTo(other: this): boolean;
|
|
31
|
+
|
|
32
|
+
toString();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface Schema {
|
|
36
|
+
fields: Field[];
|
|
37
|
+
metadata: Map<string, string>;
|
|
38
|
+
|
|
39
|
+
// constructor(
|
|
40
|
+
// fields: Field[] ,
|
|
41
|
+
// metadata: Map<string, string>
|
|
42
|
+
// )
|
|
43
|
+
|
|
44
|
+
// TODO - arrow only seems to compare fields, not metadata
|
|
45
|
+
compareTo(other: Schema): boolean;
|
|
46
|
+
|
|
47
|
+
select(...columnNames: string[]): Schema;
|
|
48
|
+
|
|
49
|
+
selectAt(...columnIndices: number[]): Schema;
|
|
50
|
+
|
|
51
|
+
assign(schemaOrFields: Schema | Field[]): Schema;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ArrowLikeVector {
|
|
55
|
+
table: Table;
|
|
56
|
+
columnName: string;
|
|
57
|
+
|
|
58
|
+
// constructor(table: Table, columnName: string);
|
|
59
|
+
|
|
60
|
+
get(rowIndex: number): unknown;
|
|
61
|
+
toArray(): ArrayLike<unknown>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Class that provides an API similar to Apache Arrow Table class
|
|
66
|
+
* Forwards methods directly if the underlying table is Arrow, otherwise calls accessor functions
|
|
67
|
+
*/
|
|
68
|
+
export interface Table {
|
|
69
|
+
schema: Schema;
|
|
70
|
+
|
|
71
|
+
// constructor(table: Table);
|
|
72
|
+
|
|
73
|
+
data: any;
|
|
74
|
+
|
|
75
|
+
numCols: number;
|
|
76
|
+
|
|
77
|
+
length(): number;
|
|
78
|
+
|
|
79
|
+
getChild(columnName: string): ArrowLikeVector;
|
|
80
|
+
|
|
81
|
+
// getChildAt(columnIndex: number): ArrowLikeVector {
|
|
82
|
+
// return
|
|
83
|
+
// }
|
|
84
|
+
}
|