@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.
Files changed (164) hide show
  1. package/LICENSE +41 -0
  2. package/README.md +9 -0
  3. package/dist/dist.dev.js +14269 -0
  4. package/dist/dist.min.js +18 -0
  5. package/dist/index.cjs +2436 -0
  6. package/dist/index.cjs.map +7 -0
  7. package/dist/index.d.ts +28 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +73 -0
  10. package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.d.ts +11 -0
  11. package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.d.ts.map +1 -0
  12. package/dist/lib/arrow-utils/arrow-fixed-size-list-utils.js +40 -0
  13. package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.d.ts +11 -0
  14. package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.d.ts.map +1 -0
  15. package/dist/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.js +32 -0
  16. package/dist/lib/mesh/convert-mesh-to-table.d.ts +18 -0
  17. package/dist/lib/mesh/convert-mesh-to-table.d.ts.map +1 -0
  18. package/dist/lib/mesh/convert-mesh-to-table.js +60 -0
  19. package/dist/lib/mesh/convert-table-to-mesh.d.ts +7 -0
  20. package/dist/lib/mesh/convert-table-to-mesh.d.ts.map +1 -0
  21. package/dist/lib/mesh/convert-table-to-mesh.js +40 -0
  22. package/dist/lib/mesh/deduce-mesh-schema.d.ts +23 -0
  23. package/dist/lib/mesh/deduce-mesh-schema.d.ts.map +1 -0
  24. package/dist/lib/mesh/deduce-mesh-schema.js +62 -0
  25. package/dist/lib/mesh/mesh-utils.d.ts +23 -0
  26. package/dist/lib/mesh/mesh-utils.d.ts.map +1 -0
  27. package/dist/lib/mesh/mesh-utils.js +46 -0
  28. package/dist/lib/schema/convert-arrow-schema.d.ts +23 -0
  29. package/dist/lib/schema/convert-arrow-schema.d.ts.map +1 -0
  30. package/dist/lib/schema/convert-arrow-schema.js +280 -0
  31. package/dist/lib/schema/data-type.d.ts +16 -0
  32. package/dist/lib/schema/data-type.d.ts.map +1 -0
  33. package/dist/lib/schema/data-type.js +88 -0
  34. package/dist/lib/schema/deduce-table-schema.d.ts +14 -0
  35. package/dist/lib/schema/deduce-table-schema.d.ts.map +1 -0
  36. package/dist/lib/schema/deduce-table-schema.js +100 -0
  37. package/dist/lib/table/arrow-api/arrow-like-field.d.ts +19 -0
  38. package/dist/lib/table/arrow-api/arrow-like-field.d.ts.map +1 -0
  39. package/dist/lib/table/arrow-api/arrow-like-field.js +36 -0
  40. package/dist/lib/table/arrow-api/arrow-like-interface.d.ts +43 -0
  41. package/dist/lib/table/arrow-api/arrow-like-interface.d.ts.map +1 -0
  42. package/dist/lib/table/arrow-api/arrow-like-interface.js +4 -0
  43. package/dist/lib/table/arrow-api/arrow-like-schema.d.ts +12 -0
  44. package/dist/lib/table/arrow-api/arrow-like-schema.d.ts.map +1 -0
  45. package/dist/lib/table/arrow-api/arrow-like-schema.js +81 -0
  46. package/dist/lib/table/arrow-api/arrow-like-table.d.ts +28 -0
  47. package/dist/lib/table/arrow-api/arrow-like-table.d.ts.map +1 -0
  48. package/dist/lib/table/arrow-api/arrow-like-table.js +63 -0
  49. package/dist/lib/table/arrow-api/arrow-like-type.d.ts +177 -0
  50. package/dist/lib/table/arrow-api/arrow-like-type.d.ts.map +1 -0
  51. package/dist/lib/table/arrow-api/arrow-like-type.js +442 -0
  52. package/dist/lib/table/arrow-api/enum.d.ts +92 -0
  53. package/dist/lib/table/arrow-api/enum.d.ts.map +1 -0
  54. package/dist/lib/table/arrow-api/enum.js +97 -0
  55. package/dist/lib/table/arrow-api/get-type-info.d.ts +13 -0
  56. package/dist/lib/table/arrow-api/get-type-info.d.ts.map +1 -0
  57. package/dist/lib/table/arrow-api/get-type-info.js +27 -0
  58. package/dist/lib/table/arrow-api/index.d.ts +5 -0
  59. package/dist/lib/table/arrow-api/index.d.ts.map +1 -0
  60. package/dist/lib/table/arrow-api/index.js +7 -0
  61. package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.d.ts +10 -0
  62. package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.d.ts.map +1 -0
  63. package/dist/lib/table/batch-builder/arrow-table-batch-aggregator.js +67 -0
  64. package/dist/lib/table/batch-builder/arrow-table-builder.d.ts +25 -0
  65. package/dist/lib/table/batch-builder/arrow-table-builder.d.ts.map +1 -0
  66. package/dist/lib/table/batch-builder/arrow-table-builder.js +99 -0
  67. package/dist/lib/table/batch-builder/base-table-batch-aggregator.d.ts +19 -0
  68. package/dist/lib/table/batch-builder/base-table-batch-aggregator.d.ts.map +1 -0
  69. package/dist/lib/table/batch-builder/base-table-batch-aggregator.js +63 -0
  70. package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.d.ts +20 -0
  71. package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.d.ts.map +1 -0
  72. package/dist/lib/table/batch-builder/columnar-table-batch-aggregator.js +83 -0
  73. package/dist/lib/table/batch-builder/row-table-batch-aggregator.d.ts +21 -0
  74. package/dist/lib/table/batch-builder/row-table-batch-aggregator.d.ts.map +1 -0
  75. package/dist/lib/table/batch-builder/row-table-batch-aggregator.js +85 -0
  76. package/dist/lib/table/batch-builder/table-batch-aggregator.d.ts +24 -0
  77. package/dist/lib/table/batch-builder/table-batch-aggregator.d.ts.map +1 -0
  78. package/dist/lib/table/batch-builder/table-batch-aggregator.js +4 -0
  79. package/dist/lib/table/batch-builder/table-batch-builder.d.ts +52 -0
  80. package/dist/lib/table/batch-builder/table-batch-builder.d.ts.map +1 -0
  81. package/dist/lib/table/batch-builder/table-batch-builder.js +148 -0
  82. package/dist/lib/table/batches/convert-batches.d.ts +10 -0
  83. package/dist/lib/table/batches/convert-batches.d.ts.map +1 -0
  84. package/dist/lib/table/batches/convert-batches.js +45 -0
  85. package/dist/lib/table/batches/make-arrow-batch-iterator.d.ts +17 -0
  86. package/dist/lib/table/batches/make-arrow-batch-iterator.d.ts.map +1 -0
  87. package/dist/lib/table/batches/make-arrow-batch-iterator.js +57 -0
  88. package/dist/lib/table/batches/make-table-batch-iterator.d.ts +15 -0
  89. package/dist/lib/table/batches/make-table-batch-iterator.d.ts.map +1 -0
  90. package/dist/lib/table/batches/make-table-batch-iterator.js +21 -0
  91. package/dist/lib/table/batches/make-table-from-batches.d.ts +9 -0
  92. package/dist/lib/table/batches/make-table-from-batches.d.ts.map +1 -0
  93. package/dist/lib/table/batches/make-table-from-batches.js +62 -0
  94. package/dist/lib/table/tables/convert-arrow-table.d.ts +24 -0
  95. package/dist/lib/table/tables/convert-arrow-table.d.ts.map +1 -0
  96. package/dist/lib/table/tables/convert-arrow-table.js +150 -0
  97. package/dist/lib/table/tables/convert-table.d.ts +43 -0
  98. package/dist/lib/table/tables/convert-table.d.ts.map +1 -0
  99. package/dist/lib/table/tables/convert-table.js +120 -0
  100. package/dist/lib/table/tables/make-table.d.ts +67 -0
  101. package/dist/lib/table/tables/make-table.d.ts.map +1 -0
  102. package/dist/lib/table/tables/make-table.js +101 -0
  103. package/dist/lib/table/tables/row-utils.d.ts +15 -0
  104. package/dist/lib/table/tables/row-utils.d.ts.map +1 -0
  105. package/dist/lib/table/tables/row-utils.js +49 -0
  106. package/dist/lib/table/tables/table-accessors.d.ts +68 -0
  107. package/dist/lib/table/tables/table-accessors.d.ts.map +1 -0
  108. package/dist/lib/table/tables/table-accessors.js +350 -0
  109. package/dist/lib/table/tables/table-column.d.ts +7 -0
  110. package/dist/lib/table/tables/table-column.d.ts.map +1 -0
  111. package/dist/lib/table/tables/table-column.js +39 -0
  112. package/dist/lib/table/tables/table-types.d.ts +12 -0
  113. package/dist/lib/table/tables/table-types.d.ts.map +1 -0
  114. package/dist/lib/table/tables/table-types.js +23 -0
  115. package/dist/lib/utils/assert.d.ts +2 -0
  116. package/dist/lib/utils/assert.d.ts.map +1 -0
  117. package/dist/lib/utils/assert.js +8 -0
  118. package/dist/lib/utils/async-queue.d.ts +18 -0
  119. package/dist/lib/utils/async-queue.d.ts.map +1 -0
  120. package/dist/lib/utils/async-queue.js +90 -0
  121. package/dist/types/types.d.ts +15 -0
  122. package/dist/types/types.d.ts.map +1 -0
  123. package/dist/types/types.js +4 -0
  124. package/package.json +52 -0
  125. package/src/index.ts +134 -0
  126. package/src/lib/arrow-utils/arrow-fixed-size-list-utils.ts +63 -0
  127. package/src/lib/arrow-utils/arrow-list-of-fixed-size-list-utils.ts +48 -0
  128. package/src/lib/mesh/convert-mesh-to-table.ts +77 -0
  129. package/src/lib/mesh/convert-table-to-mesh.ts +49 -0
  130. package/src/lib/mesh/deduce-mesh-schema.ts +75 -0
  131. package/src/lib/mesh/mesh-utils.ts +64 -0
  132. package/src/lib/schema/convert-arrow-schema.ts +301 -0
  133. package/src/lib/schema/data-type.ts +103 -0
  134. package/src/lib/schema/deduce-table-schema.ts +119 -0
  135. package/src/lib/table/arrow-api/arrow-like-field.ts +53 -0
  136. package/src/lib/table/arrow-api/arrow-like-interface.ts +84 -0
  137. package/src/lib/table/arrow-api/arrow-like-schema.ts +101 -0
  138. package/src/lib/table/arrow-api/arrow-like-table.ts +85 -0
  139. package/src/lib/table/arrow-api/arrow-like-type.ts +502 -0
  140. package/src/lib/table/arrow-api/enum.ts +99 -0
  141. package/src/lib/table/arrow-api/get-type-info.ts +38 -0
  142. package/src/lib/table/arrow-api/index.ts +8 -0
  143. package/src/lib/table/batch-builder/arrow-table-batch-aggregator-builders.ts.disabled +190 -0
  144. package/src/lib/table/batch-builder/arrow-table-batch-aggregator.ts +82 -0
  145. package/src/lib/table/batch-builder/arrow-table-builder.ts +112 -0
  146. package/src/lib/table/batch-builder/base-table-batch-aggregator.ts +82 -0
  147. package/src/lib/table/batch-builder/columnar-table-batch-aggregator.ts +99 -0
  148. package/src/lib/table/batch-builder/row-table-batch-aggregator.ts +108 -0
  149. package/src/lib/table/batch-builder/table-batch-aggregator.ts +28 -0
  150. package/src/lib/table/batch-builder/table-batch-builder.ts +185 -0
  151. package/src/lib/table/batches/convert-batches.ts +83 -0
  152. package/src/lib/table/batches/make-arrow-batch-iterator.ts +75 -0
  153. package/src/lib/table/batches/make-table-batch-iterator.ts +25 -0
  154. package/src/lib/table/batches/make-table-from-batches.ts +84 -0
  155. package/src/lib/table/tables/convert-arrow-table.ts +198 -0
  156. package/src/lib/table/tables/convert-table.ts +151 -0
  157. package/src/lib/table/tables/make-table.ts +127 -0
  158. package/src/lib/table/tables/row-utils.ts +59 -0
  159. package/src/lib/table/tables/table-accessors.ts +417 -0
  160. package/src/lib/table/tables/table-column.ts +49 -0
  161. package/src/lib/table/tables/table-types.ts +37 -0
  162. package/src/lib/utils/assert.ts +8 -0
  163. package/src/lib/utils/async-queue.ts +104 -0
  164. package/src/types/types.ts +45 -0
@@ -0,0 +1,99 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ // This code is adapted from ArrowJS https://github.com/apache/arrow
6
+ // under Apache license http://www.apache.org/licenses/LICENSE-2.0
7
+
8
+ /**
9
+ * Main data type enumeration.
10
+ *
11
+ * Data types in this library are all *logical*. They can be expressed as
12
+ * either a primitive physical type (bytes or bits of some fixed size), a
13
+ * nested type consisting of other data types, or another data type (e.g. a
14
+ * timestamp encoded as an int64).
15
+ *
16
+ * **Note**: Only enum values 0-17 (NONE through Map) are written to an Arrow
17
+ * IPC payload.
18
+ *
19
+ * The rest of the values are specified here so TypeScript can narrow the type
20
+ * signatures further beyond the base Arrow Types. The Arrow DataTypes include
21
+ * metadata like `bitWidth` that impact the type signatures of the values we
22
+ * accept and return.
23
+ *
24
+ * For example, the `Int8Vector` reads 1-byte numbers from an `Int8Array`, an
25
+ * `Int32Vector` reads a 4-byte number from an `Int32Array`, and an `Int64Vector`
26
+ * reads a pair of 4-byte lo, hi 32-bit integers as a zero-copy slice from the
27
+ * underlying `Int32Array`.
28
+ *
29
+ * Library consumers benefit by knowing the narrowest type, since we can ensure
30
+ * the types across all public methods are propagated, and never bail to `any`.
31
+ * These values are _never_ used at runtime, and they will _never_ be written
32
+ * to the flatbuffers metadata of serialized Arrow IPC payloads.
33
+ */
34
+ export enum Type {
35
+ /** The default placeholder type */
36
+ NONE = 0,
37
+ /** A NULL type having no physical storage */
38
+ Null = 1,
39
+ /** Signed or unsigned 8, 16, 32, or 64-bit little-endian integer */
40
+ Int = 2,
41
+ /** 2, 4, or 8-byte floating point value */
42
+ Float = 3,
43
+ /** Variable-length bytes (no guarantee of UTF8-ness) */
44
+ Binary = 4,
45
+ /** UTF8 variable-length string as List<Char> */
46
+ Utf8 = 5,
47
+ /** Boolean as 1 bit, LSB bit-packed ordering */
48
+ Bool = 6,
49
+ /** Precision-and-scale-based decimal type. Storage type depends on the parameters. */
50
+ Decimal = 7,
51
+ /** int32_t days or int64_t milliseconds since the UNIX epoch */
52
+ Date = 8,
53
+ /** Time as signed 32 or 64-bit integer, representing either seconds, milliseconds, microseconds, or nanoseconds since midnight since midnight */
54
+ Time = 9,
55
+ /** Exact timestamp encoded with int64 since UNIX epoch (Default unit millisecond) */
56
+ Timestamp = 10,
57
+ /** YEAR_MONTH or DAY_TIME interval in SQL style */
58
+ Interval = 11,
59
+ /** A list of some logical data type */
60
+ List = 12,
61
+ /** Struct of logical types */
62
+ Struct = 13,
63
+ /** Union of logical types */
64
+ Union = 14,
65
+ /** Fixed-size binary. Each value occupies the same number of bytes */
66
+ FixedSizeBinary = 15,
67
+ /** Fixed-size list. Each value occupies the same number of bytes */
68
+ FixedSizeList = 16,
69
+ /** Map of named logical types */
70
+ Map = 17,
71
+
72
+ /** Dictionary aka Category type */
73
+ Dictionary = -1,
74
+ Int8 = -2,
75
+ Int16 = -3,
76
+ Int32 = -4,
77
+ Int64 = -5,
78
+ Uint8 = -6,
79
+ Uint16 = -7,
80
+ Uint32 = -8,
81
+ Uint64 = -9,
82
+ Float16 = -10,
83
+ Float32 = -11,
84
+ Float64 = -12,
85
+ DateDay = -13,
86
+ DateMillisecond = -14,
87
+ TimestampSecond = -15,
88
+ TimestampMillisecond = -16,
89
+ TimestampMicrosecond = -17,
90
+ TimestampNanosecond = -18,
91
+ TimeSecond = -19,
92
+ TimeMillisecond = -20,
93
+ TimeMicrosecond = -21,
94
+ TimeNanosecond = -22,
95
+ DenseUnion = -23,
96
+ SparseUnion = -24,
97
+ IntervalDayTime = -25,
98
+ IntervalYearMonth = -26
99
+ }
@@ -0,0 +1,38 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {Type} from './arrow-like-type';
6
+
7
+ /**
8
+ * Gets type information from an Arrow type object or "mock" Arrow type object
9
+ * @param arrowTypeLike Arrow Type or type object of similar shape
10
+ */
11
+ export function getTypeInfo(arrowTypeLike: any): {
12
+ typeId: Type;
13
+ ArrayType: ArrayLike<unknown>;
14
+ typeName: string;
15
+ typeEnumName?: string;
16
+ precision?: number;
17
+ } {
18
+ return {
19
+ typeId: arrowTypeLike.typeId,
20
+ ArrayType: arrowTypeLike.ArrayType,
21
+ typeName: arrowTypeLike.toString(),
22
+ typeEnumName: getTypeKey(arrowTypeLike.typeId),
23
+ precision: arrowTypeLike.precision
24
+ };
25
+ }
26
+
27
+ let ReverseType: {[key: string]: string} | null = null;
28
+
29
+ function getTypeKey(typeKey) {
30
+ if (!ReverseType) {
31
+ ReverseType = {};
32
+ for (const key in Type) {
33
+ ReverseType[Type[key]] = key;
34
+ }
35
+ }
36
+
37
+ return ReverseType[typeKey];
38
+ }
@@ -0,0 +1,8 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ export {ArrowLikeField as Field} from './arrow-like-field';
6
+ export {ArrowLikeSchema as Schema} from './arrow-like-schema';
7
+ export {ArrowLikeTable as Table} from './arrow-like-table';
8
+ export * from './arrow-like-type';
@@ -0,0 +1,190 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {Schema, ColumnarTableBatch, ArrowTableBatch} from '@loaders.gl/schema';
6
+ import {TableBatchAggregator} from './table-batch-aggregator';
7
+ import {convertSchemaToArrow} from '../../schema/convert-arrow-schema';
8
+ import * as arrow from 'apache-arrow';
9
+
10
+ /** @note Should be part of @loaders.gl/schema - kept separate to avoid hard arrow dependency */
11
+ export class ArrowTableBatchAggregator extends TableBatchAggregator {
12
+
13
+ constructor(schema: Schema, options) {
14
+ super(schema, options);
15
+ this.arrowSchema = null;
16
+ }
17
+
18
+ getBatch(): ArrowTableBatch | null {
19
+ const batch = super.getBatch();
20
+ if (batch) {
21
+ // Get the arrow schema
22
+ this.arrowSchema = this.arrowSchema || getArrowSchema(batch.schema);
23
+
24
+ // Get arrow format vectors
25
+ const arrowVectors = getArrowVectors(this.arrowSchema, batch.data);
26
+
27
+ // Create the record batch
28
+ const recordBatch = new arrow.RecordBatch(
29
+ this.arrowSchema,
30
+ arrow.makeData({
31
+ type: new arrow.Struct(this.arrowSchema.fields),
32
+ children: arrowVectors.map(({data}) => data[0])
33
+ })
34
+ );
35
+
36
+ return {
37
+ shape: 'arrow-table',
38
+ batchType: 'data',
39
+ data: new arrow.Table([recordBatch]),
40
+ length: batch.length
41
+ };
42
+ }
43
+
44
+ return null;
45
+ }
46
+ }
47
+
48
+ // Convert from a simple loaders.gl schema to an Arrow schema
49
+ function getArrowSchema(schema): arrow.Schema {
50
+ const arrowFields: arrow.Field[] = [];
51
+ for (const key in schema) {
52
+ const field = schema[key];
53
+ if (field.type === Float32Array) {
54
+ // TODO - just store the original field as metadata?
55
+ const metadata = new Map(); // field;
56
+ // arrow: new arrow.Field(name, nullable, metadata)
57
+ const arrowField = new arrow.Field(field.name, new arrow.Float32(), field.nullable, metadata);
58
+ arrowFields.push(arrowField);
59
+ }
60
+ }
61
+ if (arrowFields.length === 0) {
62
+ throw new Error('No arrow convertible fields');
63
+ }
64
+
65
+ return new arrow.Schema(arrowFields);
66
+ }
67
+
68
+ // Convert from simple loaders.gl arrays to arrow vectors
69
+ function getArrowVectors(arrowSchema, data): arrow.Vector[] {
70
+ const arrowVectors: any[] = [];
71
+ for (const field of arrowSchema.fields) {
72
+ const vector = data[field.name];
73
+ if (vector instanceof Float32Array) {
74
+ const arrowVector = arrow.makeVector(vector);
75
+ arrowVectors.push(arrowVector);
76
+ }
77
+ }
78
+ if (arrowSchema.fields.length !== arrowVectors.length) {
79
+ throw new Error('Some columns not arrow convertible');
80
+ }
81
+ return arrowVectors;
82
+ }
83
+
84
+ type ColumnarTableBatchOptions = {};
85
+
86
+ const DEFAULT_ROW_COUNT = 100;
87
+
88
+ export class ArrowBatchAggregator implements TableBatchAggregator {
89
+ schema: Schema;
90
+ length: number = 0;
91
+ allocated: number = 0;
92
+ columns: {[columnName: string]: any[]} = {};
93
+
94
+ arrowSchema: arrow.Schema | null;
95
+
96
+ constructor(schema: Schema, options: ColumnarTableBatchOptions) {
97
+ this.schema = schema;
98
+ this.arrowSchema = convertSchemaToArrow(schema);
99
+ this._reallocateColumns();
100
+ }
101
+
102
+ rowCount(): number {
103
+ return this.length;
104
+ }
105
+
106
+ addArrayRow(row: any[]) {
107
+ // If user keeps pushing rows beyond batch size, reallocate
108
+ this._reallocateColumns();
109
+ let i = 0;
110
+ // TODO what if no csv header, columns not populated?
111
+ for (const fieldName in this.columns) {
112
+ this.columns[fieldName][this.length] = row[i++];
113
+ }
114
+ this.length++;
115
+ }
116
+
117
+ addObjectRow(row: {[columnName: string]: any}): void {
118
+ // If user keeps pushing rows beyond batch size, reallocate
119
+ this._reallocateColumns();
120
+ for (const fieldName in row) {
121
+ this.columns[fieldName][this.length] = row[fieldName];
122
+ }
123
+ this.length++;
124
+ }
125
+
126
+ getBatch(): ColumnarTableBatch | ArrowTableBatch | null {
127
+ this._pruneColumns();
128
+ const columns = Array.isArray(this.schema) ? this.columns : {};
129
+
130
+ // schema is an array if there're no headers
131
+ // object if there are headers
132
+ // columns should match schema format
133
+ if (!Array.isArray(this.schema)) {
134
+ for (const fieldName in this.schema) {
135
+ const field = this.schema[fieldName];
136
+ columns[field.name] = this.columns[field.index];
137
+ }
138
+ }
139
+
140
+ this.columns = {};
141
+
142
+ const batch: ColumnarTableBatch = {
143
+ shape: 'columnar-table',
144
+ batchType: 'data',
145
+ data: columns,
146
+ schema: this.schema,
147
+ length: this.length
148
+ };
149
+
150
+ return batch;
151
+ }
152
+
153
+ // HELPERS
154
+
155
+ _reallocateColumns() {
156
+ if (this.length < this.allocated) {
157
+ return;
158
+ }
159
+
160
+ // @ts-ignore TODO
161
+ this.allocated = this.allocated > 0 ? (this.allocated *= 2) : DEFAULT_ROW_COUNT;
162
+ this.columns = {};
163
+
164
+ for (const fieldName in this.schema) {
165
+ const field = this.schema[fieldName];
166
+ const ArrayType = field.type || Float32Array;
167
+ const oldColumn = this.columns[field.index];
168
+
169
+ if (oldColumn && ArrayBuffer.isView(oldColumn)) {
170
+ // Copy the old data to the new array
171
+ const typedArray = new ArrayType(this.allocated);
172
+ typedArray.set(oldColumn);
173
+ this.columns[field.index] = typedArray;
174
+ } else if (oldColumn) {
175
+ // Plain array
176
+ oldColumn.length = this.allocated;
177
+ this.columns[field.index] = oldColumn;
178
+ } else {
179
+ // Create new
180
+ this.columns[field.index] = new ArrayType(this.allocated);
181
+ }
182
+ }
183
+ }
184
+
185
+ _pruneColumns() {
186
+ for (const [columnName, column] of Object.entries(this.columns)) {
187
+ this.columns[columnName] = column.slice(0, this.length);
188
+ }
189
+ }
190
+ }
@@ -0,0 +1,82 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {ArrowTableBatch} from '@loaders.gl/schema';
6
+ import {ColumnarTableBatchAggregator} from './columnar-table-batch-aggregator';
7
+ import * as arrow from 'apache-arrow';
8
+
9
+ /** @note Should be part of @loaders.gl/schema - kept separate to avoid hard arrow dependency */
10
+ export class ArrowTableBatchAggregator extends ColumnarTableBatchAggregator {
11
+ arrowSchema: arrow.Schema | null;
12
+
13
+ constructor(schema, options) {
14
+ super(schema, options);
15
+ this.arrowSchema = null;
16
+ }
17
+
18
+ getBatch(): ArrowTableBatch | null {
19
+ const batch = super.getBatch();
20
+ if (batch) {
21
+ // Get the arrow schema
22
+ this.arrowSchema = this.arrowSchema || getArrowSchema(batch.schema);
23
+
24
+ // Get arrow format vectors
25
+ const arrowVectors = getArrowVectors(this.arrowSchema, batch.data);
26
+
27
+ // Create the record batch
28
+ const recordBatch = new arrow.RecordBatch(
29
+ this.arrowSchema,
30
+ arrow.makeData({
31
+ type: new arrow.Struct(this.arrowSchema.fields),
32
+ children: arrowVectors.map(({data}) => data[0])
33
+ })
34
+ );
35
+
36
+ return {
37
+ shape: 'arrow-table',
38
+ batchType: 'data',
39
+ data: new arrow.Table([recordBatch]),
40
+ length: batch.length
41
+ };
42
+ }
43
+
44
+ return null;
45
+ }
46
+ }
47
+
48
+ // Convert from a simple loaders.gl schema to an Arrow schema
49
+ function getArrowSchema(schema): arrow.Schema {
50
+ const arrowFields: arrow.Field[] = [];
51
+ for (const key in schema) {
52
+ const field = schema[key];
53
+ if (field.type === Float32Array) {
54
+ // TODO - just store the original field as metadata?
55
+ const metadata = new Map(); // field;
56
+ // arrow: new arrow.Field(name, nullable, metadata)
57
+ const arrowField = new arrow.Field(field.name, new arrow.Float32(), field.nullable, metadata);
58
+ arrowFields.push(arrowField);
59
+ }
60
+ }
61
+ if (arrowFields.length === 0) {
62
+ throw new Error('No arrow convertible fields');
63
+ }
64
+
65
+ return new arrow.Schema(arrowFields);
66
+ }
67
+
68
+ // Convert from simple loaders.gl arrays to arrow vectors
69
+ function getArrowVectors(arrowSchema, data): arrow.Vector[] {
70
+ const arrowVectors: any[] = [];
71
+ for (const field of arrowSchema.fields) {
72
+ const vector = data[field.name];
73
+ if (vector instanceof Float32Array) {
74
+ const arrowVector = arrow.makeVector(vector);
75
+ arrowVectors.push(arrowVector);
76
+ }
77
+ }
78
+ if (arrowSchema.fields.length !== arrowVectors.length) {
79
+ throw new Error('Some columns not arrow convertible');
80
+ }
81
+ return arrowVectors;
82
+ }
@@ -0,0 +1,112 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {Schema, ArrowTable, ArrowTableBatch} from '@loaders.gl/schema';
6
+ import * as arrow from 'apache-arrow';
7
+ import {convertSchemaToArrow} from '@loaders.gl/schema-utils';
8
+
9
+ /** Builds an arrow table or batches */
10
+ export class ArrowTableBuilder {
11
+ schema: Schema;
12
+ arrowSchema: arrow.Schema;
13
+ arrowBuilders: arrow.Builder[];
14
+ length: number;
15
+
16
+ constructor(schema: Schema) {
17
+ this.schema = schema;
18
+ this.arrowSchema = convertSchemaToArrow(schema);
19
+ this.arrowBuilders = this.arrowSchema.fields.map((field) =>
20
+ arrow.makeBuilder({type: field.type, nullValues: [null]})
21
+ );
22
+ this.length = 0;
23
+ }
24
+
25
+ addObjectRow(row: {[key: string]: any}) {
26
+ for (let i = 0; i < this.arrowBuilders.length; i++) {
27
+ const columnName = this.schema.fields[i].name;
28
+ const value = row[columnName];
29
+ // if (this.schema.fields[i].type.toString() === 'bool') {
30
+ // debugger;
31
+ // }
32
+ this.arrowBuilders[i].append(value);
33
+ }
34
+ this.length++;
35
+ }
36
+
37
+ addArrayRow(row: any[]) {
38
+ for (let i = 0; i < this.arrowBuilders.length; i++) {
39
+ this.arrowBuilders[i].append(row[i]);
40
+ }
41
+ this.length++;
42
+ }
43
+
44
+ /** Makes sure that a first batch with schema is sent even if no rows */
45
+ firstBatch(): ArrowTableBatch | null {
46
+ const arrowRecordBatch = this._getArrowRecordBatch();
47
+ // If there is data, a batch will be sent later
48
+ if (arrowRecordBatch.numCols !== 0) {
49
+ return null;
50
+ }
51
+ return {
52
+ shape: 'arrow-table',
53
+ batchType: 'data',
54
+ length: arrowRecordBatch.numRows,
55
+ schema: this.schema,
56
+ data: new arrow.Table(arrowRecordBatch)
57
+ };
58
+ }
59
+
60
+ /** Flush the current batch if conditions are right */
61
+ flushBatch(): ArrowTableBatch | null {
62
+ const arrowRecordBatch = this._getArrowRecordBatch();
63
+ if (arrowRecordBatch.numCols === 0) {
64
+ return null;
65
+ }
66
+ return {
67
+ shape: 'arrow-table',
68
+ batchType: 'data',
69
+ length: arrowRecordBatch.numRows,
70
+ schema: this.schema,
71
+ data: new arrow.Table(arrowRecordBatch)
72
+ };
73
+ }
74
+
75
+ /** Get a last batch if any data is left */
76
+ finishBatch(): ArrowTableBatch | null {
77
+ const arrowRecordBatch = this._getArrowRecordBatch();
78
+ this.arrowBuilders.forEach((builder) => builder.finish());
79
+ if (arrowRecordBatch.numCols === 0) {
80
+ return null;
81
+ }
82
+ return {
83
+ shape: 'arrow-table',
84
+ batchType: 'data',
85
+ length: arrowRecordBatch.numRows,
86
+ schema: this.schema,
87
+ data: new arrow.Table(arrowRecordBatch)
88
+ };
89
+ }
90
+
91
+ /** Return a table with all the accumulated data */
92
+ finishTable(): ArrowTable {
93
+ const arrowRecordBatch = this._getArrowRecordBatch();
94
+ this.arrowBuilders.forEach((builder) => builder.finish());
95
+ return {
96
+ shape: 'arrow-table',
97
+ schema: this.schema,
98
+ data: new arrow.Table(arrowRecordBatch)
99
+ };
100
+ }
101
+
102
+ /** Extract a record batch flushing the currently accumulated data in the builders */
103
+ _getArrowRecordBatch(): arrow.RecordBatch {
104
+ const {arrowBuilders, arrowSchema} = this;
105
+ const arrowDatas = arrowBuilders.map((builder) => builder.flush());
106
+ const length = arrowDatas[0].length;
107
+ const structField = new arrow.Struct(arrowSchema.fields);
108
+ const arrowStructData = new arrow.Data(structField, 0, length, 0, undefined, arrowDatas);
109
+ const arrowRecordBatch = new arrow.RecordBatch(arrowSchema, arrowStructData);
110
+ return arrowRecordBatch;
111
+ }
112
+ }
@@ -0,0 +1,82 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {Schema, TableBatch} from '@loaders.gl/schema';
6
+ import {TableBatchAggregator, TableBatchOptions} from './table-batch-aggregator';
7
+
8
+ const DEFAULT_ROW_COUNT = 100;
9
+
10
+ export class BaseTableBatchAggregator implements TableBatchAggregator {
11
+ schema: Schema;
12
+ options: TableBatchOptions;
13
+
14
+ shape?: 'array-row-table' | 'object-row-table';
15
+ length: number = 0;
16
+ rows: any[] | null = null;
17
+ cursor: number = 0;
18
+ private _headers: string[] = [];
19
+
20
+ constructor(schema: Schema, options: TableBatchOptions) {
21
+ this.options = options;
22
+ this.schema = schema;
23
+
24
+ // schema is an array if there're no headers
25
+ // object if there are headers
26
+ if (!Array.isArray(schema)) {
27
+ this._headers = [];
28
+ for (const key in schema) {
29
+ this._headers[schema[key].index] = schema[key].name;
30
+ }
31
+ }
32
+ }
33
+
34
+ rowCount(): number {
35
+ return this.length;
36
+ }
37
+
38
+ addArrayRow(row: any[], cursor?: number): void {
39
+ if (Number.isFinite(cursor)) {
40
+ this.cursor = cursor as number;
41
+ }
42
+
43
+ this.shape = 'array-row-table';
44
+
45
+ this.rows = this.rows || new Array(DEFAULT_ROW_COUNT);
46
+ this.rows[this.length] = row;
47
+ this.length++;
48
+ }
49
+
50
+ addObjectRow(row: {[columnName: string]: any}, cursor?: number): void {
51
+ if (Number.isFinite(cursor)) {
52
+ this.cursor = cursor as number;
53
+ }
54
+
55
+ this.shape = 'object-row-table';
56
+
57
+ this.rows = this.rows || new Array(DEFAULT_ROW_COUNT);
58
+ this.rows[this.length] = row;
59
+ this.length++;
60
+ }
61
+
62
+ getBatch(): TableBatch | null {
63
+ let rows = this.rows;
64
+ if (!rows) {
65
+ return null;
66
+ }
67
+
68
+ rows = rows.slice(0, this.length);
69
+ this.rows = null;
70
+
71
+ const batch: TableBatch = {
72
+ shape: this.shape || 'array-row-table',
73
+ batchType: 'data',
74
+ data: rows,
75
+ length: this.length,
76
+ schema: this.schema,
77
+ cursor: this.cursor
78
+ };
79
+
80
+ return batch;
81
+ }
82
+ }