df-script 1.4.0

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 (134) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +269 -0
  3. package/dist/api.d.ts +39 -0
  4. package/dist/api.js +18 -0
  5. package/dist/bundle.min.js +1 -0
  6. package/dist/columnExpressions/ColumnExpr.d.ts +368 -0
  7. package/dist/columnExpressions/ColumnExpr.js +23 -0
  8. package/dist/columnExpressions/ExprBase.d.ts +24 -0
  9. package/dist/columnExpressions/ExprBase.js +133 -0
  10. package/dist/columnExpressions/constants.d.ts +2 -0
  11. package/dist/columnExpressions/constants.js +5 -0
  12. package/dist/columnExpressions/functions/all.d.ts +5 -0
  13. package/dist/columnExpressions/functions/all.js +11 -0
  14. package/dist/columnExpressions/functions/coalesce.d.ts +3 -0
  15. package/dist/columnExpressions/functions/coalesce.js +40 -0
  16. package/dist/columnExpressions/functions/exclude.d.ts +5 -0
  17. package/dist/columnExpressions/functions/exclude.js +13 -0
  18. package/dist/columnExpressions/functions/lit.d.ts +5 -0
  19. package/dist/columnExpressions/functions/lit.js +22 -0
  20. package/dist/columnExpressions/functions/when.d.ts +24 -0
  21. package/dist/columnExpressions/functions/when.js +81 -0
  22. package/dist/columnExpressions/index.d.ts +19 -0
  23. package/dist/columnExpressions/index.js +81 -0
  24. package/dist/columnExpressions/mixins/AggregationExpr.d.ts +50 -0
  25. package/dist/columnExpressions/mixins/AggregationExpr.js +172 -0
  26. package/dist/columnExpressions/mixins/ArithmeticExpr.d.ts +67 -0
  27. package/dist/columnExpressions/mixins/ArithmeticExpr.js +143 -0
  28. package/dist/columnExpressions/mixins/ComparisonExpr.d.ts +60 -0
  29. package/dist/columnExpressions/mixins/ComparisonExpr.js +389 -0
  30. package/dist/columnExpressions/mixins/ListExpr.d.ts +63 -0
  31. package/dist/columnExpressions/mixins/ListExpr.js +248 -0
  32. package/dist/columnExpressions/mixins/LogicalExpr.d.ts +33 -0
  33. package/dist/columnExpressions/mixins/LogicalExpr.js +54 -0
  34. package/dist/columnExpressions/mixins/ManipulationExpr.d.ts +31 -0
  35. package/dist/columnExpressions/mixins/ManipulationExpr.js +35 -0
  36. package/dist/columnExpressions/mixins/StringExpr.d.ts +79 -0
  37. package/dist/columnExpressions/mixins/StringExpr.js +249 -0
  38. package/dist/columnExpressions/mixins/TemporalExpr.d.ts +69 -0
  39. package/dist/columnExpressions/mixins/TemporalExpr.js +127 -0
  40. package/dist/columnExpressions/mixins/WindowExpr.d.ts +50 -0
  41. package/dist/columnExpressions/mixins/WindowExpr.js +281 -0
  42. package/dist/columnExpressions/types.d.ts +7 -0
  43. package/dist/columnExpressions/types.js +2 -0
  44. package/dist/dataframe/constants.d.ts +1 -0
  45. package/dist/dataframe/constants.js +4 -0
  46. package/dist/dataframe/dataframe.d.ts +42 -0
  47. package/dist/dataframe/dataframe.js +749 -0
  48. package/dist/dataframe/grouped/grouped.d.ts +15 -0
  49. package/dist/dataframe/grouped/grouped.js +113 -0
  50. package/dist/dataframe/index.d.ts +4 -0
  51. package/dist/dataframe/index.js +20 -0
  52. package/dist/dataframe/types.d.ts +33 -0
  53. package/dist/dataframe/types.js +2 -0
  54. package/dist/dataframe/utils.d.ts +11 -0
  55. package/dist/dataframe/utils.js +215 -0
  56. package/dist/datatypes/DataType.d.ts +40 -0
  57. package/dist/datatypes/DataType.js +47 -0
  58. package/dist/datatypes/index.d.ts +29 -0
  59. package/dist/datatypes/index.js +46 -0
  60. package/dist/datatypes/types/Binary.d.ts +9 -0
  61. package/dist/datatypes/types/Binary.js +18 -0
  62. package/dist/datatypes/types/Boolean.d.ts +9 -0
  63. package/dist/datatypes/types/Boolean.js +19 -0
  64. package/dist/datatypes/types/Date.d.ts +8 -0
  65. package/dist/datatypes/types/Date.js +21 -0
  66. package/dist/datatypes/types/Datetime.d.ts +8 -0
  67. package/dist/datatypes/types/Datetime.js +17 -0
  68. package/dist/datatypes/types/Decimal.d.ts +10 -0
  69. package/dist/datatypes/types/Decimal.js +28 -0
  70. package/dist/datatypes/types/Duration.d.ts +8 -0
  71. package/dist/datatypes/types/Duration.js +17 -0
  72. package/dist/datatypes/types/Float32.d.ts +8 -0
  73. package/dist/datatypes/types/Float32.js +17 -0
  74. package/dist/datatypes/types/Float64.d.ts +8 -0
  75. package/dist/datatypes/types/Float64.js +17 -0
  76. package/dist/datatypes/types/Int16.d.ts +8 -0
  77. package/dist/datatypes/types/Int16.js +17 -0
  78. package/dist/datatypes/types/Int32.d.ts +8 -0
  79. package/dist/datatypes/types/Int32.js +17 -0
  80. package/dist/datatypes/types/Int64.d.ts +8 -0
  81. package/dist/datatypes/types/Int64.js +17 -0
  82. package/dist/datatypes/types/Int8.d.ts +8 -0
  83. package/dist/datatypes/types/Int8.js +17 -0
  84. package/dist/datatypes/types/List.d.ts +10 -0
  85. package/dist/datatypes/types/List.js +31 -0
  86. package/dist/datatypes/types/Null.d.ts +9 -0
  87. package/dist/datatypes/types/Null.js +17 -0
  88. package/dist/datatypes/types/Object.d.ts +9 -0
  89. package/dist/datatypes/types/Object.js +17 -0
  90. package/dist/datatypes/types/Struct.d.ts +14 -0
  91. package/dist/datatypes/types/Struct.js +39 -0
  92. package/dist/datatypes/types/Time.d.ts +8 -0
  93. package/dist/datatypes/types/Time.js +29 -0
  94. package/dist/datatypes/types/UInt16.d.ts +8 -0
  95. package/dist/datatypes/types/UInt16.js +17 -0
  96. package/dist/datatypes/types/UInt32.d.ts +8 -0
  97. package/dist/datatypes/types/UInt32.js +17 -0
  98. package/dist/datatypes/types/UInt64.d.ts +8 -0
  99. package/dist/datatypes/types/UInt64.js +17 -0
  100. package/dist/datatypes/types/UInt8.d.ts +8 -0
  101. package/dist/datatypes/types/UInt8.js +17 -0
  102. package/dist/datatypes/types/Utf8.d.ts +10 -0
  103. package/dist/datatypes/types/Utf8.js +20 -0
  104. package/dist/datatypes/types.d.ts +172 -0
  105. package/dist/datatypes/types.js +286 -0
  106. package/dist/exceptions/index.d.ts +13 -0
  107. package/dist/exceptions/index.js +43 -0
  108. package/dist/exceptions/utils.d.ts +2 -0
  109. package/dist/exceptions/utils.js +9 -0
  110. package/dist/functions/concat.d.ts +4 -0
  111. package/dist/functions/concat.js +248 -0
  112. package/dist/functions/index.d.ts +1 -0
  113. package/dist/functions/index.js +17 -0
  114. package/dist/index.d.ts +7 -0
  115. package/dist/index.js +1 -0
  116. package/dist/types.d.ts +47 -0
  117. package/dist/types.js +2 -0
  118. package/dist/utils/boolean.d.ts +1 -0
  119. package/dist/utils/boolean.js +18 -0
  120. package/dist/utils/date.d.ts +57 -0
  121. package/dist/utils/date.js +349 -0
  122. package/dist/utils/guards.d.ts +14 -0
  123. package/dist/utils/guards.js +143 -0
  124. package/dist/utils/index.d.ts +5 -0
  125. package/dist/utils/index.js +21 -0
  126. package/dist/utils/json.d.ts +2 -0
  127. package/dist/utils/json.js +33 -0
  128. package/dist/utils/list.d.ts +23 -0
  129. package/dist/utils/list.js +128 -0
  130. package/dist/utils/number.d.ts +86 -0
  131. package/dist/utils/number.js +223 -0
  132. package/dist/utils/string.d.ts +52 -0
  133. package/dist/utils/string.js +120 -0
  134. package/package.json +34 -0
@@ -0,0 +1,15 @@
1
+ import { DataFrame } from "../dataframe";
2
+ import type { GroupMap } from "../types";
3
+ import { DataType } from "../../datatypes";
4
+ import type { IExpr, ColumnDict, RowRecord } from "../../types";
5
+ export declare class GroupedData<T, K extends keyof T> {
6
+ private groups;
7
+ private keys;
8
+ private allKeys;
9
+ private parentColumns;
10
+ private parentHeight;
11
+ private parentSchema;
12
+ constructor(groups: GroupMap, keys: K[], allKeys: (keyof T)[], parentColumns: ColumnDict, parentHeight: number, parentSchema: Record<string, DataType>);
13
+ to_dataframe<U extends RowRecord = any>(): DataFrame<U>;
14
+ agg<U extends RowRecord = any>(...exprs: (IExpr | any)[]): DataFrame<U>;
15
+ }
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GroupedData = void 0;
4
+ const dataframe_1 = require("../dataframe");
5
+ const utils_1 = require("../utils");
6
+ const columnExpressions_1 = require("../../columnExpressions");
7
+ class GroupedData {
8
+ groups;
9
+ keys;
10
+ allKeys;
11
+ parentColumns;
12
+ parentHeight;
13
+ parentSchema;
14
+ constructor(groups, keys, allKeys, parentColumns, parentHeight, parentSchema) {
15
+ this.groups = groups;
16
+ this.keys = keys;
17
+ this.allKeys = allKeys;
18
+ this.parentColumns = parentColumns;
19
+ this.parentHeight = parentHeight;
20
+ this.parentSchema = parentSchema;
21
+ }
22
+ to_dataframe() {
23
+ const keysLen = this.keys.length;
24
+ const keysStr = new Array(keysLen);
25
+ for (let i = 0; i < keysLen; i++) {
26
+ keysStr[i] = String(this.keys[i]);
27
+ }
28
+ const numGroups = this.groups.size;
29
+ const newColumns = {};
30
+ for (let i = 0; i < keysStr.length; i++) {
31
+ newColumns[keysStr[i]] = new Array(numGroups);
32
+ }
33
+ let groupIdx = 0;
34
+ for (const indices of this.groups.values()) {
35
+ if (indices.length === 0)
36
+ continue;
37
+ const firstIdx = indices[0];
38
+ for (let i = 0; i < keysStr.length; i++) {
39
+ const k = keysStr[i];
40
+ const val = this.parentColumns[k][firstIdx];
41
+ newColumns[k][groupIdx] = val === undefined ? null : val;
42
+ }
43
+ groupIdx++;
44
+ }
45
+ const outSchema = {};
46
+ for (const k of keysStr) {
47
+ outSchema[k] = this.parentSchema[k];
48
+ }
49
+ return new dataframe_1.DataFrame(newColumns, outSchema, groupIdx);
50
+ }
51
+ agg(...exprs) {
52
+ const allKeysLen = this.allKeys.length;
53
+ const allKeysStr = new Array(allKeysLen);
54
+ for (let i = 0; i < allKeysLen; i++) {
55
+ allKeysStr[i] = String(this.allKeys[i]);
56
+ }
57
+ const keysLen = this.keys.length;
58
+ const keysStr = new Array(keysLen);
59
+ for (let i = 0; i < keysLen; i++) {
60
+ keysStr[i] = String(this.keys[i]);
61
+ }
62
+ const expandedExprs = (0, columnExpressions_1.resolveColumnSelectors)(exprs.flat(), allKeysStr, keysStr);
63
+ const numGroups = this.groups.size;
64
+ const newColumns = {};
65
+ for (let i = 0; i < keysStr.length; i++) {
66
+ newColumns[keysStr[i]] = new Array(numGroups);
67
+ }
68
+ let groupIdx = 0;
69
+ for (const indices of this.groups.values()) {
70
+ if (indices.length === 0)
71
+ continue;
72
+ const firstIdx = indices[0];
73
+ for (let i = 0; i < keysStr.length; i++) {
74
+ const k = keysStr[i];
75
+ const val = this.parentColumns[k][firstIdx];
76
+ newColumns[k][groupIdx] = val === undefined ? null : val;
77
+ }
78
+ groupIdx++;
79
+ }
80
+ for (let i = 0; i < expandedExprs.length; i++) {
81
+ const e = expandedExprs[i];
82
+ const targetKey = e.outputName || e.colName || columnExpressions_1.ALL_COLUMNS_MARKER;
83
+ if (!e.aggFn) {
84
+ newColumns[targetKey] = e.evaluate(newColumns, numGroups);
85
+ continue;
86
+ }
87
+ const preGroupedCol = e.evaluatePreGrouping(this.parentColumns, this.parentHeight);
88
+ const aggregatedGroupValues = new Array(numGroups);
89
+ let gIdx = 0;
90
+ for (const indices of this.groups.values()) {
91
+ if (indices.length === 0)
92
+ continue;
93
+ const groupValues = new Array(indices.length);
94
+ for (let k = 0; k < indices.length; k++) {
95
+ groupValues[k] = preGroupedCol[indices[k]];
96
+ }
97
+ aggregatedGroupValues[gIdx] = e.aggFn(groupValues);
98
+ gIdx++;
99
+ }
100
+ newColumns[targetKey] = e.evaluatePostGrouping(aggregatedGroupValues, newColumns);
101
+ }
102
+ const outSchema = {};
103
+ for (const k of keysStr) {
104
+ outSchema[k] = this.parentSchema[k];
105
+ }
106
+ for (const e of expandedExprs) {
107
+ const targetKey = e.outputName || e.colName || columnExpressions_1.ALL_COLUMNS_MARKER;
108
+ outSchema[targetKey] = (0, utils_1.inferColumnType)(newColumns[targetKey]);
109
+ }
110
+ return new dataframe_1.DataFrame(newColumns, outSchema, groupIdx);
111
+ }
112
+ }
113
+ exports.GroupedData = GroupedData;
@@ -0,0 +1,4 @@
1
+ export * from "./dataframe";
2
+ export * from "./utils";
3
+ export * from "./types";
4
+ export * from "./grouped/grouped";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./dataframe"), exports);
18
+ __exportStar(require("./utils"), exports);
19
+ __exportStar(require("./types"), exports);
20
+ __exportStar(require("./grouped/grouped"), exports);
@@ -0,0 +1,33 @@
1
+ import type { IExpr, AggFn, RowRecord } from "../types";
2
+ import type { DataFrame } from "./dataframe";
3
+ export type JoinType = "inner" | "left" | "outer" | "right";
4
+ export type LimitPosition = "start" | "end";
5
+ export type GroupMap = Map<string, number[]>;
6
+ export interface LimitOptions {
7
+ offset?: number;
8
+ from?: LimitPosition;
9
+ }
10
+ export interface SortOptions<T> {
11
+ by: keyof T | (keyof T)[] | IExpr | IExpr[];
12
+ descending?: boolean | boolean[];
13
+ nullsLast?: boolean;
14
+ custom?: Partial<Record<keyof T, (a: any, b: any) => number>>;
15
+ }
16
+ export interface PivotOptions<T> {
17
+ index: (keyof T) | (keyof T)[];
18
+ columns: keyof T;
19
+ values: keyof T;
20
+ agg?: AggFn<any> | string;
21
+ }
22
+ export interface JoinOptions<T, U extends RowRecord = any> {
23
+ other: DataFrame<U>;
24
+ on: (keyof T & keyof U) | (keyof T & keyof U)[];
25
+ how?: JoinType;
26
+ suffixes?: [string, string];
27
+ }
28
+ export interface UnpivotOptions<T> {
29
+ idVars: (keyof T) | (keyof T)[];
30
+ valueVars: (keyof T) | (keyof T)[];
31
+ varName?: string;
32
+ valueName?: string;
33
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ import type { IExpr, ColumnData, ColumnDict } from "../types";
2
+ import { DataType } from "../datatypes";
3
+ export declare function resolveWindowExpr(expr: IExpr, columns: ColumnDict, height: number): ColumnData;
4
+ export declare function rowsToColumns(rows: any[]): {
5
+ columns: ColumnDict;
6
+ height: number;
7
+ };
8
+ export declare function columnsToRows(columns: ColumnDict, height: number): any[];
9
+ export declare function getRowFromColumns(columns: ColumnDict, idx: number, keys: string[]): any;
10
+ export declare function inferColumnType(col: ColumnData): DataType;
11
+ export declare function gatherColumnsByIndices(columns: ColumnDict, indices: number[]): ColumnDict;
@@ -0,0 +1,215 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveWindowExpr = resolveWindowExpr;
4
+ exports.rowsToColumns = rowsToColumns;
5
+ exports.columnsToRows = columnsToRows;
6
+ exports.getRowFromColumns = getRowFromColumns;
7
+ exports.inferColumnType = inferColumnType;
8
+ exports.gatherColumnsByIndices = gatherColumnsByIndices;
9
+ const datatypes_1 = require("../datatypes");
10
+ const constants_1 = require("./constants");
11
+ const utils_1 = require("../utils");
12
+ const exceptions_1 = require("../exceptions");
13
+ function partition_by_columns(columns, height, partitionKeys) {
14
+ const partitionMap = new Map();
15
+ const pKeysLen = partitionKeys.length;
16
+ const keyColumns = new Array(pKeysLen);
17
+ for (let i = 0; i < pKeysLen; i++) {
18
+ const pKey = partitionKeys[i];
19
+ if (typeof pKey === "string") {
20
+ (0, exceptions_1.assertColumnExists)(pKey, columns, "Partition key", " in the DataFrame.");
21
+ keyColumns[i] = columns[pKey];
22
+ }
23
+ else {
24
+ keyColumns[i] = pKey.evaluate(columns, height);
25
+ }
26
+ }
27
+ for (let i = 0; i < height; i++) {
28
+ const keyValues = new Array(keyColumns.length);
29
+ for (let j = 0; j < keyColumns.length; j++) {
30
+ const val = keyColumns[j][i];
31
+ keyValues[j] = val == null ? "" : String(val);
32
+ }
33
+ const hash = keyValues.join(constants_1.KEY_SEPARATOR);
34
+ let group = partitionMap.get(hash);
35
+ if (group === undefined) {
36
+ group = [];
37
+ partitionMap.set(hash, group);
38
+ }
39
+ group.push(i);
40
+ }
41
+ return partitionMap;
42
+ }
43
+ function resolveWindowExpr(expr, columns, height) {
44
+ const results = new Array(height);
45
+ if (height === 0)
46
+ return results;
47
+ const partitionKeys = expr.partitionBy || [];
48
+ const partitionGroups = partition_by_columns(columns, height, partitionKeys);
49
+ const prePartitionArray = expr.evaluatePrePartition(columns, height);
50
+ for (const indices of partitionGroups.values()) {
51
+ const groupLen = indices.length;
52
+ const groupPreValues = new Array(groupLen);
53
+ for (let k = 0; k < groupLen; k++) {
54
+ groupPreValues[k] = prePartitionArray[indices[k]];
55
+ }
56
+ if (expr.evaluateWindow) {
57
+ for (let k = 0; k < groupLen; k++) {
58
+ results[indices[k]] = expr.evaluateWindow(groupPreValues, indices, k);
59
+ }
60
+ continue;
61
+ }
62
+ if (expr.aggFn) {
63
+ const aggregatedVal = expr.aggFn(groupPreValues);
64
+ for (let k = 0; k < groupLen; k++) {
65
+ results[indices[k]] = aggregatedVal;
66
+ }
67
+ continue;
68
+ }
69
+ for (let k = 0; k < groupLen; k++) {
70
+ results[indices[k]] = prePartitionArray[indices[k]];
71
+ }
72
+ }
73
+ return expr.evaluatePostPartition(results, columns);
74
+ }
75
+ function rowsToColumns(rows) {
76
+ if (!Array.isArray(rows) || rows.length === 0) {
77
+ return { columns: {}, height: 0 };
78
+ }
79
+ const height = rows.length;
80
+ const keysSet = new Set();
81
+ for (let r = 0; r < height; r++) {
82
+ const row = rows[r];
83
+ if ((0, utils_1.isObj)(row)) {
84
+ const rowKeys = Object.keys(row);
85
+ for (let i = 0; i < rowKeys.length; i++) {
86
+ keysSet.add(rowKeys[i]);
87
+ }
88
+ }
89
+ }
90
+ const keys = Array.from(keysSet);
91
+ const columns = {};
92
+ for (let i = 0; i < keys.length; i++) {
93
+ columns[keys[i]] = new Array(height);
94
+ }
95
+ for (let r = 0; r < height; r++) {
96
+ const row = rows[r] || {};
97
+ for (let i = 0; i < keys.length; i++) {
98
+ const k = keys[i];
99
+ const val = row[k];
100
+ columns[k][r] = val === undefined ? null : val;
101
+ }
102
+ }
103
+ return { columns, height };
104
+ }
105
+ function columnsToRows(columns, height) {
106
+ const keys = Object.keys(columns);
107
+ const rows = new Array(height);
108
+ for (let r = 0; r < height; r++) {
109
+ const row = {};
110
+ for (let i = 0; i < keys.length; i++) {
111
+ const k = keys[i];
112
+ const val = columns[k][r];
113
+ row[k] = val === undefined ? null : val;
114
+ }
115
+ rows[r] = row;
116
+ }
117
+ return rows;
118
+ }
119
+ function getRowFromColumns(columns, idx, keys) {
120
+ const row = {};
121
+ for (let i = 0; i < keys.length; i++) {
122
+ const k = keys[i];
123
+ const val = columns[k][idx];
124
+ row[k] = val === undefined ? null : val;
125
+ }
126
+ return row;
127
+ }
128
+ function inferColumnType(col) {
129
+ if (col.length === 0)
130
+ return datatypes_1.DataTypeRegistry.Utf8;
131
+ let isBoolean = true;
132
+ let isInteger = true;
133
+ let isNumeric = true;
134
+ let isBigInt = true;
135
+ let isDate = true;
136
+ let isList = true;
137
+ let hasDateObj = false;
138
+ let hasNonNull = false;
139
+ const allListElements = [];
140
+ for (let i = 0; i < col.length; i++) {
141
+ const val = col[i];
142
+ if (val == null)
143
+ continue;
144
+ hasNonNull = true;
145
+ if (!Array.isArray(val)) {
146
+ isList = false;
147
+ }
148
+ else {
149
+ for (let j = 0; j < val.length; j++) {
150
+ allListElements.push(val[j]);
151
+ }
152
+ }
153
+ if (val instanceof Date)
154
+ hasDateObj = true;
155
+ if (typeof val !== "boolean")
156
+ isBoolean = false;
157
+ if (typeof val !== "bigint")
158
+ isBigInt = false;
159
+ if (typeof val !== "number") {
160
+ isNumeric = false;
161
+ isInteger = false;
162
+ }
163
+ else {
164
+ if (!Number.isInteger(val))
165
+ isInteger = false;
166
+ }
167
+ if (!(val instanceof Date) && (typeof val !== "string" || isNaN(Date.parse(val)))) {
168
+ isDate = false;
169
+ }
170
+ }
171
+ if (!hasNonNull)
172
+ return datatypes_1.DataTypeRegistry.Utf8;
173
+ if (isList) {
174
+ const innerType = inferColumnType(allListElements);
175
+ return datatypes_1.DataTypeRegistry.List(innerType);
176
+ }
177
+ if (isBoolean)
178
+ return datatypes_1.DataTypeRegistry.Boolean;
179
+ if (isBigInt)
180
+ return datatypes_1.DataTypeRegistry.Int64;
181
+ if (isNumeric && !isInteger)
182
+ return datatypes_1.DataTypeRegistry.Float64;
183
+ if (isNumeric && isInteger) {
184
+ let fitsInInt32 = true;
185
+ for (let i = 0; i < col.length; i++) {
186
+ const val = col[i];
187
+ if (val == null)
188
+ continue;
189
+ if (val < -2147483648 || val > 2147483647) {
190
+ fitsInInt32 = false;
191
+ break;
192
+ }
193
+ }
194
+ return fitsInInt32 ? datatypes_1.DataTypeRegistry.Int32 : datatypes_1.DataTypeRegistry.Float64;
195
+ }
196
+ if (isDate && hasDateObj)
197
+ return datatypes_1.DataTypeRegistry.Datetime;
198
+ return datatypes_1.DataTypeRegistry.Utf8;
199
+ }
200
+ function gatherColumnsByIndices(columns, indices) {
201
+ const keys = Object.keys(columns);
202
+ const numKeys = keys.length;
203
+ const newHeight = indices.length;
204
+ const res = {};
205
+ for (let j = 0; j < numKeys; j++) {
206
+ const k = keys[j];
207
+ const oldCol = columns[k];
208
+ const newCol = new Array(newHeight);
209
+ for (let idx = 0; idx < newHeight; idx++) {
210
+ newCol[idx] = oldCol[indices[idx]];
211
+ }
212
+ res[k] = newCol;
213
+ }
214
+ return res;
215
+ }
@@ -0,0 +1,40 @@
1
+ export declare abstract class DataType<T = any> {
2
+ abstract readonly name: string;
3
+ abstract coerce(val: any): T;
4
+ abstract equals(other: DataType): boolean;
5
+ abstract allocate(size: number): ArrayLike<T>;
6
+ get isNumeric(): boolean;
7
+ get isInteger(): boolean;
8
+ get isFloat(): boolean;
9
+ get isSigned(): boolean;
10
+ get isUnsigned(): boolean;
11
+ get isTemporal(): boolean;
12
+ get isNested(): boolean;
13
+ get isBoolean(): boolean;
14
+ get isString(): boolean;
15
+ get isUtf8(): boolean;
16
+ get isObject(): boolean;
17
+ get isNull(): boolean;
18
+ get isBinary(): boolean;
19
+ }
20
+ export declare abstract class NumericDataType<T = number | null> extends DataType<T> {
21
+ get isNumeric(): boolean;
22
+ }
23
+ export declare abstract class IntegerDataType<T = number | null> extends NumericDataType<T> {
24
+ get isInteger(): boolean;
25
+ }
26
+ export declare abstract class SignedIntegerType<T = number | null> extends IntegerDataType<T> {
27
+ get isSigned(): boolean;
28
+ }
29
+ export declare abstract class UnsignedIntegerType<T = number | null> extends IntegerDataType<T> {
30
+ get isUnsigned(): boolean;
31
+ }
32
+ export declare abstract class FloatDataType<T = number | null> extends NumericDataType<T> {
33
+ get isFloat(): boolean;
34
+ }
35
+ export declare abstract class TemporalDataType<T = any> extends DataType<T> {
36
+ get isTemporal(): boolean;
37
+ }
38
+ export declare abstract class NestedDataType<T = any> extends DataType<T> {
39
+ get isNested(): boolean;
40
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NestedDataType = exports.TemporalDataType = exports.FloatDataType = exports.UnsignedIntegerType = exports.SignedIntegerType = exports.IntegerDataType = exports.NumericDataType = exports.DataType = void 0;
4
+ class DataType {
5
+ get isNumeric() { return false; }
6
+ get isInteger() { return false; }
7
+ get isFloat() { return false; }
8
+ get isSigned() { return false; }
9
+ get isUnsigned() { return false; }
10
+ get isTemporal() { return false; }
11
+ get isNested() { return false; }
12
+ get isBoolean() { return false; }
13
+ get isString() { return false; }
14
+ get isUtf8() { return false; }
15
+ get isObject() { return false; }
16
+ get isNull() { return false; }
17
+ get isBinary() { return false; }
18
+ }
19
+ exports.DataType = DataType;
20
+ class NumericDataType extends DataType {
21
+ get isNumeric() { return true; }
22
+ }
23
+ exports.NumericDataType = NumericDataType;
24
+ class IntegerDataType extends NumericDataType {
25
+ get isInteger() { return true; }
26
+ }
27
+ exports.IntegerDataType = IntegerDataType;
28
+ class SignedIntegerType extends IntegerDataType {
29
+ get isSigned() { return true; }
30
+ }
31
+ exports.SignedIntegerType = SignedIntegerType;
32
+ class UnsignedIntegerType extends IntegerDataType {
33
+ get isUnsigned() { return true; }
34
+ }
35
+ exports.UnsignedIntegerType = UnsignedIntegerType;
36
+ class FloatDataType extends NumericDataType {
37
+ get isFloat() { return true; }
38
+ }
39
+ exports.FloatDataType = FloatDataType;
40
+ class TemporalDataType extends DataType {
41
+ get isTemporal() { return true; }
42
+ }
43
+ exports.TemporalDataType = TemporalDataType;
44
+ class NestedDataType extends DataType {
45
+ get isNested() { return true; }
46
+ }
47
+ exports.NestedDataType = NestedDataType;
@@ -0,0 +1,29 @@
1
+ import { DataType as BaseDataType } from "./DataType";
2
+ import { DecimalType } from "./types";
3
+ export { BaseDataType as DataType };
4
+ export declare const DataTypeRegistry: {
5
+ Int8: import("./types").Int8Type;
6
+ Int16: import("./types").Int16Type;
7
+ Int32: import("./types").Int32Type;
8
+ Int64: import("./types").Int64Type;
9
+ UInt8: import("./types").UInt8Type;
10
+ UInt16: import("./types").UInt16Type;
11
+ UInt32: import("./types").UInt32Type;
12
+ UInt64: import("./types").UInt64Type;
13
+ Float32: import("./types").Float32Type;
14
+ Float64: import("./types").Float64Type;
15
+ Decimal: (precision?: number, scale?: number) => DecimalType;
16
+ Boolean: import("./types").BooleanType;
17
+ Utf8: import("./types").Utf8Type;
18
+ Binary: import("./types").BinaryType;
19
+ Date: import("./types").DateType;
20
+ Datetime: import("./types").DatetimeType;
21
+ Time: import("./types").TimeType;
22
+ Duration: import("./types").DurationType;
23
+ Object: import("./types").ObjectType;
24
+ Null: import("./types").NullType;
25
+ List: <TInner>(inner: BaseDataType<TInner>) => import("./types").ListType<TInner>;
26
+ Struct: <TFields extends import("..").RowRecord>(fields: { [K in keyof TFields]: BaseDataType<TFields[K]>; }) => import("./types").StructType<TFields>;
27
+ };
28
+ export * from "./types";
29
+ export * from "./DataType";
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.DataTypeRegistry = exports.DataType = void 0;
18
+ const DataType_1 = require("./DataType");
19
+ Object.defineProperty(exports, "DataType", { enumerable: true, get: function () { return DataType_1.DataType; } });
20
+ const types_1 = require("./types");
21
+ exports.DataTypeRegistry = {
22
+ Int8: types_1.Int8,
23
+ Int16: types_1.Int16,
24
+ Int32: types_1.Int32,
25
+ Int64: types_1.Int64,
26
+ UInt8: types_1.UInt8,
27
+ UInt16: types_1.UInt16,
28
+ UInt32: types_1.UInt32,
29
+ UInt64: types_1.UInt64,
30
+ Float32: types_1.Float32,
31
+ Float64: types_1.Float64,
32
+ Decimal: (precision, scale) => new types_1.DecimalType(precision, scale),
33
+ Boolean: types_1.Boolean,
34
+ Utf8: types_1.Utf8,
35
+ Binary: types_1.Binary,
36
+ Date: types_1.Date,
37
+ Datetime: types_1.Datetime,
38
+ Time: types_1.Time,
39
+ Duration: types_1.Duration,
40
+ Object: types_1.Object,
41
+ Null: types_1.Null,
42
+ List: types_1.List,
43
+ Struct: types_1.Struct
44
+ };
45
+ __exportStar(require("./types"), exports);
46
+ __exportStar(require("./DataType"), exports);
@@ -0,0 +1,9 @@
1
+ import { DataType } from "../DataType";
2
+ export declare class BinaryType extends DataType {
3
+ readonly name = "Binary";
4
+ get isBinary(): boolean;
5
+ coerce(val: any): Uint8Array | null;
6
+ equals(other: DataType): boolean;
7
+ allocate(size: number): (Uint8Array | null)[];
8
+ }
9
+ export declare const Binary: BinaryType;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Binary = exports.BinaryType = void 0;
4
+ const DataType_1 = require("../DataType");
5
+ const utils_1 = require("../../utils");
6
+ class BinaryType extends DataType_1.DataType {
7
+ name = "Binary";
8
+ get isBinary() { return true; }
9
+ coerce(val) {
10
+ return (0, utils_1.toValidBinary)(val);
11
+ }
12
+ equals(other) {
13
+ return other.name === "Binary";
14
+ }
15
+ allocate(size) { return new Array(size).fill(null); }
16
+ }
17
+ exports.BinaryType = BinaryType;
18
+ exports.Binary = new BinaryType();
@@ -0,0 +1,9 @@
1
+ import { DataType } from "../DataType";
2
+ export declare class BooleanType extends DataType {
3
+ readonly name = "Boolean";
4
+ get isBoolean(): boolean;
5
+ coerce(val: any): boolean | null;
6
+ equals(other: DataType): boolean;
7
+ allocate(size: number): (boolean | null)[];
8
+ }
9
+ export declare const Boolean: BooleanType;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Boolean = exports.BooleanType = void 0;
4
+ const DataType_1 = require("../DataType");
5
+ class BooleanType extends DataType_1.DataType {
6
+ name = "Boolean";
7
+ get isBoolean() { return true; }
8
+ coerce(val) {
9
+ if (val == null)
10
+ return null;
11
+ return !!val;
12
+ }
13
+ equals(other) {
14
+ return other.name === "Boolean";
15
+ }
16
+ allocate(size) { return new Array(size).fill(null); }
17
+ }
18
+ exports.BooleanType = BooleanType;
19
+ exports.Boolean = new BooleanType();
@@ -0,0 +1,8 @@
1
+ import { TemporalDataType, DataType } from "../DataType";
2
+ export declare class DateType extends TemporalDataType {
3
+ readonly name = "Date";
4
+ coerce(val: any): Date | null;
5
+ equals(other: DataType): boolean;
6
+ allocate(size: number): (Date | null)[];
7
+ }
8
+ export declare const Date: DateType;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Date = exports.DateType = void 0;
4
+ const DataType_1 = require("../DataType");
5
+ const utils_1 = require("../../utils");
6
+ class DateType extends DataType_1.TemporalDataType {
7
+ name = "Date";
8
+ coerce(val) {
9
+ const d = (0, utils_1.toValidDate)(val);
10
+ if (!d)
11
+ return null;
12
+ d.setUTCHours(0, 0, 0, 0);
13
+ return d;
14
+ }
15
+ equals(other) {
16
+ return other.name === "Date";
17
+ }
18
+ allocate(size) { return new Array(size).fill(null); }
19
+ }
20
+ exports.DateType = DateType;
21
+ exports.Date = new DateType();