@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,101 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {SchemaMetadata, Field} from '@loaders.gl/schema';
6
+ import {ArrowLikeField} from './arrow-like-field';
7
+
8
+ export class ArrowLikeSchema {
9
+ fields: ArrowLikeField[];
10
+ metadata: Map<string, string>;
11
+
12
+ constructor(
13
+ fields: ArrowLikeField[] | Field[],
14
+ metadata: SchemaMetadata | Map<string, string> = new Map<string, string>()
15
+ ) {
16
+ // checkNames(fields);
17
+ // For kepler fields, create arrow compatible `Fields` that have kepler fields as `metadata`
18
+ this.fields = fields.map(
19
+ (field) => new ArrowLikeField(field.name, field.type, field.nullable, field.metadata)
20
+ );
21
+ this.metadata =
22
+ metadata instanceof Map ? metadata : new Map<string, string>(Object.entries(metadata));
23
+ }
24
+
25
+ // TODO - arrow only seems to compare fields, not metadata
26
+ compareTo(other: ArrowLikeSchema): boolean {
27
+ if (this.metadata !== other.metadata) {
28
+ return false;
29
+ }
30
+ if (this.fields.length !== other.fields.length) {
31
+ return false;
32
+ }
33
+ for (let i = 0; i < this.fields.length; ++i) {
34
+ if (!this.fields[i].compareTo(other.fields[i])) {
35
+ return false;
36
+ }
37
+ }
38
+ return true;
39
+ }
40
+
41
+ select(...columnNames: string[]): ArrowLikeSchema {
42
+ // Ensure column names reference valid fields
43
+ const nameMap = Object.create(null);
44
+ for (const name of columnNames) {
45
+ nameMap[name] = true;
46
+ }
47
+ const selectedFields = this.fields.filter((field) => nameMap[field.name]);
48
+ return new ArrowLikeSchema(selectedFields, this.metadata);
49
+ }
50
+
51
+ selectAt(...columnIndices: number[]): ArrowLikeSchema {
52
+ // Ensure column indices reference valid fields
53
+ const selectedFields = columnIndices.map((index) => this.fields[index]).filter(Boolean);
54
+ return new ArrowLikeSchema(selectedFields, this.metadata);
55
+ }
56
+
57
+ assign(schemaOrFields: ArrowLikeSchema | ArrowLikeField[]): ArrowLikeSchema {
58
+ let fields: ArrowLikeField[];
59
+ let metadata = this.metadata;
60
+
61
+ if (schemaOrFields instanceof ArrowLikeSchema) {
62
+ const otherArrowLikeSchema = schemaOrFields;
63
+ fields = otherArrowLikeSchema.fields;
64
+ metadata = mergeMaps(mergeMaps(new Map(), this.metadata), otherArrowLikeSchema.metadata);
65
+ } else {
66
+ fields = schemaOrFields;
67
+ }
68
+
69
+ // Create a merged list of fields, overwrite fields in place, new fields at end
70
+ const fieldMap: {[key: string]: ArrowLikeField} = Object.create(null);
71
+
72
+ for (const field of this.fields) {
73
+ fieldMap[field.name] = field;
74
+ }
75
+
76
+ for (const field of fields) {
77
+ fieldMap[field.name] = field;
78
+ }
79
+
80
+ const mergedFields = Object.values(fieldMap);
81
+
82
+ return new ArrowLikeSchema(mergedFields, metadata);
83
+ }
84
+ }
85
+
86
+ // Warn if any duplicated field names
87
+ // function checkNames(fields: Field[]): void {
88
+ // const usedNames: Record<string, boolean> = {};
89
+ // for (const field of fields) {
90
+ // if (usedNames[field.name]) {
91
+ // // eslint-disable-next-line
92
+ // console.warn('ArrowLikeSchema: duplicated field name', field.name, field);
93
+ // }
94
+ // usedNames[field.name] = true;
95
+ // }
96
+ // }
97
+
98
+ function mergeMaps<T>(m1: T, m2: T): T {
99
+ // @ts-ignore
100
+ return new Map([...(m1 || new Map()), ...(m2 || new Map())]);
101
+ }
@@ -0,0 +1,85 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {Table} from '@loaders.gl/schema';
6
+
7
+ import {ArrowLikeSchema} from './arrow-like-schema';
8
+ import {deduceTableSchema} from '../../schema/deduce-table-schema';
9
+
10
+ import {
11
+ getTableCell,
12
+ getTableLength,
13
+ getTableNumCols
14
+ // getTableCell,
15
+ // getTableRowShape,
16
+ // getTableColumnIndex,
17
+ // getTableColumnName,
18
+ // getTableRowAsObject,
19
+ // getTableRowAsArray
20
+ } from '../tables/table-accessors';
21
+
22
+ class ArrowLikeVector {
23
+ table: Table;
24
+ columnName: string;
25
+
26
+ constructor(table: Table, columnName: string) {
27
+ this.table = table;
28
+ this.columnName = columnName;
29
+ }
30
+
31
+ get(rowIndex: number): unknown {
32
+ return getTableCell(this.table, rowIndex, this.columnName);
33
+ }
34
+
35
+ toArray(): ArrayLike<unknown> {
36
+ switch (this.table.shape) {
37
+ case 'arrow-table':
38
+ const arrowTable = this.table.data as any;
39
+ return arrowTable.getChild(this.columnName)?.toArray();
40
+ case 'columnar-table':
41
+ return this.table.data[this.columnName];
42
+ default:
43
+ throw new Error(this.table.shape);
44
+ }
45
+ }
46
+ }
47
+
48
+ /**
49
+ * Class that provides an API similar to Apache Arrow Table class
50
+ * Forwards methods directly if the underlying table is Arrow, otherwise calls accessor functions
51
+ */
52
+ export class ArrowLikeTable {
53
+ schema: ArrowLikeSchema;
54
+ table: Table;
55
+
56
+ constructor(table: Table) {
57
+ const schema = table.schema || deduceTableSchema(table);
58
+ this.schema = new ArrowLikeSchema(schema.fields, schema.metadata);
59
+ this.table = {...table, schema};
60
+ }
61
+
62
+ // get schema() {
63
+ // return this.table.schema;
64
+ // }
65
+
66
+ get data() {
67
+ return this.table.shape === 'geojson-table' ? this.table.features : this.table.data;
68
+ }
69
+
70
+ get numCols(): number {
71
+ return getTableNumCols(this.table);
72
+ }
73
+
74
+ get length(): number {
75
+ return getTableLength(this.table);
76
+ }
77
+
78
+ getChild(columnName: string): ArrowLikeVector {
79
+ return new ArrowLikeVector(this.table, columnName);
80
+ }
81
+
82
+ // getChildAt(columnIndex: number): ArrowLikeVector {
83
+ // return
84
+ // }
85
+ }
@@ -0,0 +1,502 @@
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
+ import {Type} from './enum';
9
+
10
+ import {ArrowLikeField as Field} from './arrow-like-field';
11
+
12
+ export {Type} from './enum';
13
+
14
+ export type TypedIntArray =
15
+ | Int8Array
16
+ | Uint8Array
17
+ | Int16Array
18
+ | Uint16Array
19
+ | Int32Array
20
+ | Uint32Array
21
+ | Uint8ClampedArray;
22
+
23
+ export type TypedFloatArray = Float32Array | Float64Array;
24
+
25
+ export type TypedArray = TypedIntArray | TypedFloatArray;
26
+
27
+ export type AnyArrayType = Array<any> | TypedIntArray | TypedFloatArray;
28
+
29
+ /** ArrowLike DataType class */
30
+ export class DataType {
31
+ static isNull(x: any): boolean {
32
+ return x && x.typeId === Type.Null;
33
+ }
34
+ static isInt(x: any): boolean {
35
+ return x && x.typeId === Type.Int;
36
+ }
37
+ static isFloat(x: any): boolean {
38
+ return x && x.typeId === Type.Float;
39
+ }
40
+ static isBinary(x: any): boolean {
41
+ return x && x.typeId === Type.Binary;
42
+ }
43
+ static isUtf8(x: any): boolean {
44
+ return x && x.typeId === Type.Utf8;
45
+ }
46
+ static isBool(x: any): boolean {
47
+ return x && x.typeId === Type.Bool;
48
+ }
49
+ static isDecimal(x: any): boolean {
50
+ return x && x.typeId === Type.Decimal;
51
+ }
52
+ static isDate(x: any): boolean {
53
+ return x && x.typeId === Type.Date;
54
+ }
55
+ static isTime(x: any): boolean {
56
+ return x && x.typeId === Type.Time;
57
+ }
58
+ static isTimestamp(x: any): boolean {
59
+ return x && x.typeId === Type.Timestamp;
60
+ }
61
+ static isInterval(x: any): boolean {
62
+ return x && x.typeId === Type.Interval;
63
+ }
64
+ static isList(x: any): boolean {
65
+ return x && x.typeId === Type.List;
66
+ }
67
+ static isStruct(x: any): boolean {
68
+ return x && x.typeId === Type.Struct;
69
+ }
70
+ static isUnion(x: any): boolean {
71
+ return x && x.typeId === Type.Union;
72
+ }
73
+ static isFixedSizeBinary(x: any): boolean {
74
+ return x && x.typeId === Type.FixedSizeBinary;
75
+ }
76
+ static isFixedSizeList(x: any): boolean {
77
+ return x && x.typeId === Type.FixedSizeList;
78
+ }
79
+ static isMap(x: any): boolean {
80
+ return x && x.typeId === Type.Map;
81
+ }
82
+ static isDictionary(x: any): boolean {
83
+ return x && x.typeId === Type.Dictionary;
84
+ }
85
+
86
+ get typeId(): Type {
87
+ return Type.NONE;
88
+ }
89
+
90
+ // get ArrayType(): AnyArrayType {
91
+ // return Int8Array;
92
+ // }
93
+
94
+ // get ArrayType() { return Array; }
95
+ compareTo(other: DataType): boolean {
96
+ // TODO
97
+ return this === other; // comparer.visit(this, other);
98
+ }
99
+ }
100
+
101
+ // NULL
102
+
103
+ export class Null extends DataType {
104
+ get typeId(): Type {
105
+ return Type.Null;
106
+ }
107
+ get [Symbol.toStringTag](): string {
108
+ return 'Null';
109
+ }
110
+ toString(): string {
111
+ return 'Null';
112
+ }
113
+ }
114
+
115
+ // BOOLEANS
116
+
117
+ export class Bool extends DataType {
118
+ get typeId(): Type {
119
+ return Type.Bool;
120
+ }
121
+ // get ArrayType() {
122
+ // return Uint8Array;
123
+ // }
124
+ get [Symbol.toStringTag](): string {
125
+ return 'Bool';
126
+ }
127
+ toString(): string {
128
+ return 'Bool';
129
+ }
130
+ }
131
+
132
+ // INTS
133
+
134
+ export class Int extends DataType {
135
+ readonly isSigned: boolean;
136
+ readonly bitWidth: number;
137
+ constructor(isSigned, bitWidth) {
138
+ super();
139
+ this.isSigned = isSigned;
140
+ this.bitWidth = bitWidth;
141
+ }
142
+ get typeId(): Type {
143
+ return Type.Int;
144
+ }
145
+ // get ArrayType() {
146
+ // switch (this.bitWidth) {
147
+ // case 8:
148
+ // return this.isSigned ? Int8Array : Uint8Array;
149
+ // case 16:
150
+ // return this.isSigned ? Int16Array : Uint16Array;
151
+ // case 32:
152
+ // return this.isSigned ? Int32Array : Uint32Array;
153
+ // case 64:
154
+ // return this.isSigned ? Int32Array : Uint32Array;
155
+ // default:
156
+ // throw new Error(`Unrecognized ${this[Symbol.toStringTag]} type`);
157
+ // }
158
+ // }
159
+ get [Symbol.toStringTag](): string {
160
+ return 'Int';
161
+ }
162
+ toString(): string {
163
+ return `${this.isSigned ? 'I' : 'Ui'}nt${this.bitWidth}`;
164
+ }
165
+ }
166
+
167
+ export class Int8 extends Int {
168
+ constructor() {
169
+ super(true, 8);
170
+ }
171
+ }
172
+ export class Int16 extends Int {
173
+ constructor() {
174
+ super(true, 16);
175
+ }
176
+ }
177
+ export class Int32 extends Int {
178
+ constructor() {
179
+ super(true, 32);
180
+ }
181
+ }
182
+ export class Int64 extends Int {
183
+ constructor() {
184
+ super(true, 64);
185
+ }
186
+ }
187
+ export class Uint8 extends Int {
188
+ constructor() {
189
+ super(false, 8);
190
+ }
191
+ }
192
+ export class Uint16 extends Int {
193
+ constructor() {
194
+ super(false, 16);
195
+ }
196
+ }
197
+ export class Uint32 extends Int {
198
+ constructor() {
199
+ super(false, 32);
200
+ }
201
+ }
202
+ export class Uint64 extends Int {
203
+ constructor() {
204
+ super(false, 64);
205
+ }
206
+ }
207
+
208
+ // FLOATS
209
+
210
+ const Precision = {
211
+ HALF: 16,
212
+ SINGLE: 32,
213
+ DOUBLE: 64
214
+ };
215
+
216
+ export class Float extends DataType {
217
+ readonly precision: number;
218
+ constructor(precision) {
219
+ super();
220
+ this.precision = precision;
221
+ }
222
+ get typeId(): Type {
223
+ return Type.Float;
224
+ }
225
+ // get ArrayType() {
226
+ // switch (this.precision) {
227
+ // case Precision.HALF:
228
+ // return Uint16Array;
229
+ // case Precision.SINGLE:
230
+ // return Float32Array;
231
+ // case Precision.DOUBLE:
232
+ // return Float64Array;
233
+ // default:
234
+ // throw new Error(`Unrecognized ${this[Symbol.toStringTag]} type`);
235
+ // }
236
+ // }
237
+ get [Symbol.toStringTag](): string {
238
+ return 'Float';
239
+ }
240
+ toString(): string {
241
+ return `Float${this.precision}`;
242
+ }
243
+ }
244
+
245
+ export class Float16 extends Float {
246
+ constructor() {
247
+ super(Precision.HALF);
248
+ }
249
+ }
250
+ export class Float32 extends Float {
251
+ constructor() {
252
+ super(Precision.SINGLE);
253
+ }
254
+ }
255
+ export class Float64 extends Float {
256
+ constructor() {
257
+ super(Precision.DOUBLE);
258
+ }
259
+ }
260
+
261
+ export class Binary extends DataType {
262
+ constructor() {
263
+ super();
264
+ }
265
+ get typeId() {
266
+ return Type.Binary;
267
+ }
268
+ toString() {
269
+ return 'Binary';
270
+ }
271
+ get [Symbol.toStringTag]() {
272
+ return 'Binary';
273
+ }
274
+ }
275
+
276
+ // STRINGS
277
+
278
+ export class Utf8 extends DataType {
279
+ get typeId(): Type {
280
+ return Type.Utf8;
281
+ }
282
+ // get ArrayType() {
283
+ // return Uint8Array;
284
+ // }
285
+ get [Symbol.toStringTag](): string {
286
+ return 'Utf8';
287
+ }
288
+ toString(): string {
289
+ return 'Utf8';
290
+ }
291
+ }
292
+
293
+ // DATES, TIMES AND INTERVALS
294
+
295
+ const DateUnit = {
296
+ DAY: 0,
297
+ MILLISECOND: 1
298
+ };
299
+
300
+ export class Date extends DataType {
301
+ readonly unit: number;
302
+ constructor(unit) {
303
+ super();
304
+ this.unit = unit;
305
+ }
306
+ get typeId(): Type {
307
+ return Type.Date;
308
+ }
309
+ // get ArrayType() {
310
+ // return Int32Array;
311
+ // }
312
+ get [Symbol.toStringTag](): string {
313
+ return 'Date';
314
+ }
315
+ toString(): string {
316
+ return `Date${(this.unit + 1) * 32}<${DateUnit[this.unit]}>`;
317
+ }
318
+ }
319
+
320
+ export class DateDay extends Date {
321
+ constructor() {
322
+ super(DateUnit.DAY);
323
+ }
324
+ }
325
+ export class DateMillisecond extends Date {
326
+ constructor() {
327
+ super(DateUnit.MILLISECOND);
328
+ }
329
+ }
330
+
331
+ const TimeUnit = {
332
+ SECOND: 1,
333
+ MILLISECOND: 1e3,
334
+ MICROSECOND: 1e6,
335
+ NANOSECOND: 1e9
336
+ };
337
+
338
+ export class Time extends DataType {
339
+ readonly unit: any;
340
+ readonly bitWidth: number;
341
+
342
+ constructor(unit: any, bitWidth: number) {
343
+ super();
344
+ this.unit = unit;
345
+ this.bitWidth = bitWidth;
346
+ }
347
+ get typeId(): Type {
348
+ return Type.Time;
349
+ }
350
+ toString(): string {
351
+ return `Time${this.bitWidth}<${TimeUnit[this.unit]}>`;
352
+ }
353
+ get [Symbol.toStringTag](): string {
354
+ return 'Time';
355
+ }
356
+ // get ArrayType() {
357
+ // return Int32Array;
358
+ // }
359
+ }
360
+
361
+ export class TimeSecond extends Time {
362
+ constructor() {
363
+ super(TimeUnit.SECOND, 32);
364
+ }
365
+ }
366
+ export class TimeMillisecond extends Time {
367
+ constructor() {
368
+ super(TimeUnit.MILLISECOND, 32);
369
+ }
370
+ }
371
+ // export class TimeMicrosecond extends Time { constructor() { super(TimeUnit.MICROSECOND, 64); } }
372
+ // export class TimeNanosecond extends Time { constructor() { super(TimeUnit.NANOSECOND, 64); } }
373
+
374
+ export class Timestamp extends DataType {
375
+ readonly unit: any;
376
+ readonly timezone: any;
377
+
378
+ constructor(unit: any, timezone = null) {
379
+ super();
380
+ this.unit = unit;
381
+ this.timezone = timezone;
382
+ }
383
+ get typeId(): Type {
384
+ return Type.Timestamp;
385
+ }
386
+ // get ArrayType() {
387
+ // return Int32Array;
388
+ // }
389
+ get [Symbol.toStringTag](): string {
390
+ return 'Timestamp';
391
+ }
392
+ toString(): string {
393
+ return `Timestamp<${TimeUnit[this.unit]}${this.timezone ? `, ${this.timezone}` : ''}>`;
394
+ }
395
+ }
396
+
397
+ export class TimestampSecond extends Timestamp {
398
+ constructor(timezone = null) {
399
+ super(TimeUnit.SECOND, timezone);
400
+ }
401
+ }
402
+ export class TimestampMillisecond extends Timestamp {
403
+ constructor(timezone = null) {
404
+ super(TimeUnit.MILLISECOND, timezone);
405
+ }
406
+ }
407
+ export class TimestampMicrosecond extends Timestamp {
408
+ constructor(timezone = null) {
409
+ super(TimeUnit.MICROSECOND, timezone);
410
+ }
411
+ }
412
+ export class TimestampNanosecond extends Timestamp {
413
+ constructor(timezone = null) {
414
+ super(TimeUnit.NANOSECOND, timezone);
415
+ }
416
+ }
417
+
418
+ const IntervalUnit = {
419
+ DAY_TIME: 0,
420
+ YEAR_MONTH: 1
421
+ };
422
+
423
+ export class Interval extends DataType {
424
+ readonly unit: number;
425
+ constructor(unit: number) {
426
+ super();
427
+ this.unit = unit;
428
+ }
429
+ get typeId(): Type {
430
+ return Type.Interval;
431
+ }
432
+ // get ArrayType() {
433
+ // return Int32Array;
434
+ // }
435
+ get [Symbol.toStringTag](): string {
436
+ return 'Interval';
437
+ }
438
+ toString(): string {
439
+ return `Interval<${IntervalUnit[this.unit]}>`;
440
+ }
441
+ }
442
+
443
+ export class IntervalDayTime extends Interval {
444
+ constructor() {
445
+ super(IntervalUnit.DAY_TIME);
446
+ }
447
+ }
448
+ export class IntervalYearMonth extends Interval {
449
+ constructor() {
450
+ super(IntervalUnit.YEAR_MONTH);
451
+ }
452
+ }
453
+
454
+ export class FixedSizeList extends DataType {
455
+ readonly listSize: number;
456
+ readonly children: Field[];
457
+
458
+ constructor(listSize: number, child: Field) {
459
+ super();
460
+ this.listSize = listSize;
461
+ this.children = [child];
462
+ }
463
+ get typeId(): Type {
464
+ return Type.FixedSizeList;
465
+ }
466
+ get valueType() {
467
+ return this.children[0].type;
468
+ }
469
+ get valueField() {
470
+ return this.children[0];
471
+ }
472
+ // get ArrayType() {
473
+ // return this.valueType.ArrayType;
474
+ // }
475
+ get [Symbol.toStringTag](): string {
476
+ return 'FixedSizeList';
477
+ }
478
+ toString(): string {
479
+ return `FixedSizeList[${this.listSize}]<${JSON.stringify(this.valueType)}>`;
480
+ }
481
+ }
482
+
483
+ export class Struct extends DataType {
484
+ public readonly children: Field[];
485
+
486
+ constructor(children: Field[]) {
487
+ super();
488
+ this.children = children;
489
+ }
490
+
491
+ public get typeId() {
492
+ return Type.Struct;
493
+ }
494
+ public toString() {
495
+ return `Struct<{${this.children
496
+ .map((f) => `${f.name}:${JSON.stringify(f.type)}`)
497
+ .join(', ')}}>`;
498
+ }
499
+ get [Symbol.toStringTag](): string {
500
+ return 'Struct';
501
+ }
502
+ }