@loaders.gl/sql 5.0.0-alpha.1 → 5.0.0-alpha.2
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 +14 -3
- package/README.md +49 -0
- package/dist/duckdb-sql-source.js +1 -1
- package/dist/index.cjs +1699 -19
- package/dist/index.cjs.map +3 -4
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/parse-sql-predicate.d.ts +10 -0
- package/dist/parse-sql-predicate.d.ts.map +1 -0
- package/dist/parse-sql-predicate.js +316 -0
- package/dist/parse-sql-predicate.js.map +1 -0
- package/dist/snowflake-sql-source.js +1 -1
- package/dist/sql-predicate-schema.d.ts +141 -0
- package/dist/sql-predicate-schema.d.ts.map +1 -0
- package/dist/sql-predicate-schema.js +205 -0
- package/dist/sql-predicate-schema.js.map +1 -0
- package/dist/sql-predicate-types.d.ts +55 -0
- package/dist/sql-predicate-types.d.ts.map +1 -0
- package/dist/sql-predicate-types.js +5 -0
- package/dist/sql-predicate-types.js.map +1 -0
- package/dist/sql-predicate-zod.cjs +53 -0
- package/dist/sql-predicate-zod.cjs.map +6 -0
- package/dist/sql-predicate-zod.d.ts +11 -0
- package/dist/sql-predicate-zod.d.ts.map +1 -0
- package/dist/sql-predicate-zod.js +42 -0
- package/dist/sql-predicate-zod.js.map +1 -0
- package/dist/sql-predicate.cjs +500 -0
- package/dist/sql-predicate.cjs.map +6 -0
- package/dist/sql-predicate.d.ts +4 -0
- package/dist/sql-predicate.d.ts.map +1 -0
- package/dist/sql-predicate.js +6 -0
- package/dist/sql-predicate.js.map +1 -0
- package/package.json +35 -8
- package/src/index.ts +18 -0
- package/src/parse-sql-predicate.ts +365 -0
- package/src/sql-predicate-schema.ts +226 -0
- package/src/sql-predicate-types.ts +71 -0
- package/src/sql-predicate-zod.ts +49 -0
- package/src/sql-predicate.ts +22 -0
package/dist/index.cjs
CHANGED
|
@@ -28,24 +28,1234 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
30
|
// dist/index.js
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
33
|
DuckDBSQLDataSource: () => DuckDBSQLDataSource,
|
|
34
34
|
DuckDBSQLSource: () => DuckDBSQLSource,
|
|
35
35
|
SQLDataSource: () => SQLDataSource,
|
|
36
|
+
SQL_PREDICATE_JSON_SCHEMA: () => SQL_PREDICATE_JSON_SCHEMA,
|
|
36
37
|
SnowflakeSQLDataSource: () => SnowflakeSQLDataSource,
|
|
37
38
|
SnowflakeSQLSource: () => SnowflakeSQLSource,
|
|
38
39
|
getSQLAdapterFactory: () => getSQLAdapterFactory,
|
|
39
|
-
|
|
40
|
+
isSQLPredicate: () => isSQLPredicate,
|
|
41
|
+
parseSQLPredicate: () => parseSQLPredicate,
|
|
42
|
+
registerSQLAdapter: () => registerSQLAdapter,
|
|
43
|
+
validateSQLPredicate: () => validateSQLPredicate
|
|
40
44
|
});
|
|
41
|
-
module.exports = __toCommonJS(
|
|
45
|
+
module.exports = __toCommonJS(index_exports);
|
|
42
46
|
|
|
43
|
-
//
|
|
44
|
-
var
|
|
47
|
+
// ../loader-utils/src/lib/log-utils/log.ts
|
|
48
|
+
var import_log = require("@probe.gl/log");
|
|
49
|
+
var VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
|
|
50
|
+
var version = VERSION[0] >= "0" && VERSION[0] <= "9" ? `v${VERSION}` : "";
|
|
51
|
+
function createLog() {
|
|
52
|
+
const log2 = new import_log.Log({ id: "loaders.gl" });
|
|
53
|
+
globalThis.loaders ||= {};
|
|
54
|
+
globalThis.loaders.log = log2;
|
|
55
|
+
globalThis.loaders.version = version;
|
|
56
|
+
globalThis.probe ||= {};
|
|
57
|
+
globalThis.probe.loaders = log2;
|
|
58
|
+
return log2;
|
|
59
|
+
}
|
|
60
|
+
var log = createLog();
|
|
61
|
+
|
|
62
|
+
// ../loader-utils/src/lib/javascript-utils/is-type.ts
|
|
63
|
+
var isObject = (value) => value !== null && typeof value === "object";
|
|
64
|
+
var isPureObject = (value) => isObject(value) && value.constructor === {}.constructor;
|
|
65
|
+
|
|
66
|
+
// ../loader-utils/src/lib/option-utils/merge-options.ts
|
|
67
|
+
function mergeOptions(baseOptions, newOptions) {
|
|
68
|
+
return mergeOptionsRecursively(baseOptions || {}, newOptions);
|
|
69
|
+
}
|
|
70
|
+
function mergeOptionsRecursively(baseOptions, newOptions, level = 0) {
|
|
71
|
+
if (level > 3) {
|
|
72
|
+
return newOptions;
|
|
73
|
+
}
|
|
74
|
+
const options = { ...baseOptions };
|
|
75
|
+
for (const [key, newValue] of Object.entries(newOptions)) {
|
|
76
|
+
if (isPureObject(newValue)) {
|
|
77
|
+
options[key] = mergeOptionsRecursively(
|
|
78
|
+
options[key] || {},
|
|
79
|
+
newOptions[key],
|
|
80
|
+
level + 1
|
|
81
|
+
);
|
|
82
|
+
} else {
|
|
83
|
+
options[key] = newOptions[key];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return options;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ../loader-utils/src/lib/path-utils/file-aliases.ts
|
|
90
|
+
var pathPrefix = "";
|
|
91
|
+
var fileAliases = {};
|
|
92
|
+
function resolvePath(filename) {
|
|
93
|
+
for (const alias in fileAliases) {
|
|
94
|
+
if (filename.startsWith(alias)) {
|
|
95
|
+
const replacement = fileAliases[alias];
|
|
96
|
+
filename = filename.replace(alias, replacement);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (!filename.startsWith("http://") && !filename.startsWith("https://")) {
|
|
100
|
+
filename = `${pathPrefix}${filename}`;
|
|
101
|
+
}
|
|
102
|
+
return filename;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ../loader-utils/src/lib/sources/data-source.ts
|
|
106
|
+
var UNAVAILABLE_CORE_API = {
|
|
107
|
+
fetchFile: unavailableCoreApiMethod("fetchFile"),
|
|
108
|
+
parse: unavailableCoreApiMethod("parse"),
|
|
109
|
+
parseFile: unavailableCoreApiMethod("parseFile"),
|
|
110
|
+
parseSync: unavailableCoreApiMethod("parseSync"),
|
|
111
|
+
parseInBatches: unavailableCoreApiMethod("parseInBatches"),
|
|
112
|
+
load: unavailableCoreApiMethod("load"),
|
|
113
|
+
loadInBatches: unavailableCoreApiMethod("loadInBatches")
|
|
114
|
+
};
|
|
115
|
+
var DataSource = class _DataSource {
|
|
116
|
+
static defaultOptions = {
|
|
117
|
+
core: {
|
|
118
|
+
type: "auto",
|
|
119
|
+
attributions: [],
|
|
120
|
+
loadOptions: {},
|
|
121
|
+
loaders: [],
|
|
122
|
+
onError: void 0
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
optionsType;
|
|
126
|
+
options;
|
|
127
|
+
data;
|
|
128
|
+
url;
|
|
129
|
+
/** The actual load options, if calling a loaders.gl loader */
|
|
130
|
+
loadOptions;
|
|
131
|
+
/** A resolved fetch function extracted from loadOptions prop */
|
|
132
|
+
fetch;
|
|
133
|
+
/** Shared source-level runtime hooks, when supplied by the source factory. */
|
|
134
|
+
coreApi;
|
|
135
|
+
/** Whether a real CoreAPI instance was injected by the integration layer. */
|
|
136
|
+
hasCoreApi;
|
|
137
|
+
_needsRefresh = true;
|
|
138
|
+
constructor(data, options, defaultOptions, coreApi) {
|
|
139
|
+
var _a;
|
|
140
|
+
if (defaultOptions) {
|
|
141
|
+
this.options = mergeOptions({ ...defaultOptions, core: _DataSource.defaultOptions }, options);
|
|
142
|
+
} else {
|
|
143
|
+
this.options = { ...options };
|
|
144
|
+
}
|
|
145
|
+
this.data = data;
|
|
146
|
+
this.url = typeof data === "string" ? resolvePath(data) : "";
|
|
147
|
+
const loadOptions = normalizeDirectLoaderOptions((_a = this.options.core) == null ? void 0 : _a.loadOptions);
|
|
148
|
+
this.loadOptions = loadOptions;
|
|
149
|
+
const fetch2 = getFetchFunction(loadOptions);
|
|
150
|
+
this.coreApi = coreApi || UNAVAILABLE_CORE_API;
|
|
151
|
+
this.hasCoreApi = Boolean(coreApi);
|
|
152
|
+
this.fetch = fetch2;
|
|
153
|
+
}
|
|
154
|
+
setProps(options) {
|
|
155
|
+
this.options = Object.assign(this.options, options);
|
|
156
|
+
this.setNeedsRefresh();
|
|
157
|
+
}
|
|
158
|
+
/** Mark this data source as needing a refresh (redraw) */
|
|
159
|
+
setNeedsRefresh() {
|
|
160
|
+
this._needsRefresh = true;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Does this data source need refreshing?
|
|
164
|
+
* @note The specifics of the refresh mechanism depends on type of data source
|
|
165
|
+
*/
|
|
166
|
+
getNeedsRefresh(clear = true) {
|
|
167
|
+
const needsRefresh = this._needsRefresh;
|
|
168
|
+
if (clear) {
|
|
169
|
+
this._needsRefresh = false;
|
|
170
|
+
}
|
|
171
|
+
return needsRefresh;
|
|
172
|
+
}
|
|
173
|
+
/** Reports a source-level failure through the configured callback or the shared logger. */
|
|
174
|
+
reportError(error, message) {
|
|
175
|
+
var _a;
|
|
176
|
+
const normalizedError = normalizeError(error, message);
|
|
177
|
+
const callback = (_a = this.options.core) == null ? void 0 : _a.onError;
|
|
178
|
+
if (callback) {
|
|
179
|
+
callback(normalizedError, this);
|
|
180
|
+
} else {
|
|
181
|
+
log.warn(`${this.constructor.name}: ${normalizedError.message}`)();
|
|
182
|
+
}
|
|
183
|
+
return normalizedError;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
function getFetchFunction(options) {
|
|
187
|
+
var _a;
|
|
188
|
+
const fetchFunction = (_a = options == null ? void 0 : options.core) == null ? void 0 : _a.fetch;
|
|
189
|
+
if (fetchFunction && typeof fetchFunction === "function") {
|
|
190
|
+
return (url, fetchOptions2) => fetchFunction(url, fetchOptions2);
|
|
191
|
+
}
|
|
192
|
+
const fetchOptions = options == null ? void 0 : options.fetch;
|
|
193
|
+
if (fetchOptions && typeof fetchOptions !== "function") {
|
|
194
|
+
return (url, requestOptions) => fetch(url, mergeFetchOptions(fetchOptions, requestOptions));
|
|
195
|
+
}
|
|
196
|
+
return (url, requestOptions) => fetch(url, requestOptions);
|
|
197
|
+
}
|
|
198
|
+
function mergeFetchOptions(fetchOptions, requestOptions) {
|
|
199
|
+
const mergedOptions = { ...fetchOptions, ...requestOptions };
|
|
200
|
+
if (fetchOptions.headers || (requestOptions == null ? void 0 : requestOptions.headers)) {
|
|
201
|
+
mergedOptions.headers = mergeHeaders(fetchOptions.headers, requestOptions == null ? void 0 : requestOptions.headers);
|
|
202
|
+
}
|
|
203
|
+
return mergedOptions;
|
|
204
|
+
}
|
|
205
|
+
function mergeHeaders(defaultHeaders, requestHeaders) {
|
|
206
|
+
const headers = new Headers(defaultHeaders);
|
|
207
|
+
if (requestHeaders) {
|
|
208
|
+
new Headers(requestHeaders).forEach((value, key) => headers.set(key, value));
|
|
209
|
+
}
|
|
210
|
+
return headers;
|
|
211
|
+
}
|
|
212
|
+
function normalizeDirectLoaderOptions(options) {
|
|
213
|
+
const loadOptions = { ...options };
|
|
214
|
+
if (options == null ? void 0 : options.core) {
|
|
215
|
+
loadOptions.core = { ...options.core };
|
|
216
|
+
}
|
|
217
|
+
const topLevelBaseUri = typeof loadOptions.baseUri === "string" ? loadOptions.baseUri : void 0;
|
|
218
|
+
const topLevelBaseUrl = typeof loadOptions.baseUrl === "string" ? loadOptions.baseUrl : void 0;
|
|
219
|
+
if (topLevelBaseUri !== void 0 || topLevelBaseUrl !== void 0) {
|
|
220
|
+
loadOptions.core ||= {};
|
|
221
|
+
if (loadOptions.core.baseUrl === void 0) {
|
|
222
|
+
loadOptions.core.baseUrl = topLevelBaseUrl ?? topLevelBaseUri;
|
|
223
|
+
}
|
|
224
|
+
delete loadOptions.baseUri;
|
|
225
|
+
delete loadOptions.baseUrl;
|
|
226
|
+
}
|
|
227
|
+
return loadOptions;
|
|
228
|
+
}
|
|
229
|
+
function normalizeError(error, message) {
|
|
230
|
+
if (error instanceof Error) {
|
|
231
|
+
return error;
|
|
232
|
+
}
|
|
233
|
+
if (typeof error === "string") {
|
|
234
|
+
return new Error(error);
|
|
235
|
+
}
|
|
236
|
+
return new Error(message);
|
|
237
|
+
}
|
|
238
|
+
function unavailableCoreApiMethod(methodName) {
|
|
239
|
+
return () => {
|
|
240
|
+
throw new Error(
|
|
241
|
+
`CoreAPI.${methodName} is unavailable. Use @loaders.gl/core.createDataSource().`
|
|
242
|
+
);
|
|
243
|
+
};
|
|
244
|
+
}
|
|
45
245
|
|
|
46
246
|
// dist/sql-utils.js
|
|
247
|
+
var arrow5 = __toESM(require("apache-arrow"), 1);
|
|
248
|
+
|
|
249
|
+
// ../schema-utils/src/lib/schema/data-type.ts
|
|
250
|
+
function getDataTypeFromValue(value, defaultNumberType = "float32") {
|
|
251
|
+
if (value instanceof Date) {
|
|
252
|
+
return "date-millisecond";
|
|
253
|
+
}
|
|
254
|
+
if (typeof value === "number") {
|
|
255
|
+
return defaultNumberType;
|
|
256
|
+
}
|
|
257
|
+
if (typeof value === "boolean") {
|
|
258
|
+
return "bool";
|
|
259
|
+
}
|
|
260
|
+
if (typeof value === "string") {
|
|
261
|
+
return "utf8";
|
|
262
|
+
}
|
|
263
|
+
if (value === null || value === "undefined") {
|
|
264
|
+
return "null";
|
|
265
|
+
}
|
|
266
|
+
return "null";
|
|
267
|
+
}
|
|
268
|
+
function getDataTypeFromArray(array) {
|
|
269
|
+
let type = getDataTypeFromTypedArray(array);
|
|
270
|
+
if (type !== "null") {
|
|
271
|
+
return { type, nullable: false };
|
|
272
|
+
}
|
|
273
|
+
if (array.length > 0) {
|
|
274
|
+
type = getDataTypeFromValue(array[0]);
|
|
275
|
+
return { type, nullable: true };
|
|
276
|
+
}
|
|
277
|
+
return { type: "null", nullable: true };
|
|
278
|
+
}
|
|
279
|
+
function getDataTypeFromTypedArray(array) {
|
|
280
|
+
switch (array.constructor) {
|
|
281
|
+
case Int8Array:
|
|
282
|
+
return "int8";
|
|
283
|
+
case Uint8Array:
|
|
284
|
+
case Uint8ClampedArray:
|
|
285
|
+
return "uint8";
|
|
286
|
+
case Int16Array:
|
|
287
|
+
return "int16";
|
|
288
|
+
case Uint16Array:
|
|
289
|
+
return "uint16";
|
|
290
|
+
case Int32Array:
|
|
291
|
+
return "int32";
|
|
292
|
+
case Uint32Array:
|
|
293
|
+
return "uint32";
|
|
294
|
+
case Float32Array:
|
|
295
|
+
return "float32";
|
|
296
|
+
case Float64Array:
|
|
297
|
+
return "float64";
|
|
298
|
+
default:
|
|
299
|
+
return "null";
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function getArrayTypeFromDataType(type, nullable) {
|
|
303
|
+
if (!nullable) {
|
|
304
|
+
switch (type) {
|
|
305
|
+
case "int8":
|
|
306
|
+
return Int8Array;
|
|
307
|
+
case "uint8":
|
|
308
|
+
return Uint8Array;
|
|
309
|
+
case "int16":
|
|
310
|
+
return Int16Array;
|
|
311
|
+
case "uint16":
|
|
312
|
+
return Uint16Array;
|
|
313
|
+
case "int32":
|
|
314
|
+
return Int32Array;
|
|
315
|
+
case "uint32":
|
|
316
|
+
return Uint32Array;
|
|
317
|
+
case "float32":
|
|
318
|
+
return Float32Array;
|
|
319
|
+
case "float64":
|
|
320
|
+
return Float64Array;
|
|
321
|
+
default:
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return Array;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ../schema-utils/src/lib/schema/convert-arrow-schema.ts
|
|
47
329
|
var arrow = __toESM(require("apache-arrow"), 1);
|
|
48
|
-
|
|
330
|
+
function convertArrowToSchema(arrowSchema) {
|
|
331
|
+
return serializeArrowSchema(arrowSchema);
|
|
332
|
+
}
|
|
333
|
+
function convertSchemaToArrow(schema, options) {
|
|
334
|
+
return deserializeArrowSchema(schema, options);
|
|
335
|
+
}
|
|
336
|
+
function serializeArrowSchema(arrowSchema) {
|
|
337
|
+
return {
|
|
338
|
+
fields: arrowSchema.fields.map((arrowField) => serializeArrowField(arrowField)),
|
|
339
|
+
metadata: serializeArrowMetadata(arrowSchema.metadata)
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
function deserializeArrowSchema(schema, options) {
|
|
343
|
+
return new arrow.Schema(
|
|
344
|
+
schema.fields.map((field) => deserializeArrowField(field, options)),
|
|
345
|
+
deserializeArrowMetadata(schema.metadata)
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
function serializeArrowMetadata(arrowMetadata) {
|
|
349
|
+
return Object.fromEntries(arrowMetadata);
|
|
350
|
+
}
|
|
351
|
+
function deserializeArrowMetadata(metadata) {
|
|
352
|
+
return metadata ? new Map(Object.entries(metadata)) : /* @__PURE__ */ new Map();
|
|
353
|
+
}
|
|
354
|
+
function serializeArrowField(field) {
|
|
355
|
+
return {
|
|
356
|
+
name: field.name,
|
|
357
|
+
type: serializeArrowType(field.type),
|
|
358
|
+
nullable: field.nullable,
|
|
359
|
+
metadata: serializeArrowMetadata(field.metadata)
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
function deserializeArrowField(field, options) {
|
|
363
|
+
return new arrow.Field(
|
|
364
|
+
field.name,
|
|
365
|
+
deserializeArrowType(field.type, options),
|
|
366
|
+
field.nullable,
|
|
367
|
+
deserializeArrowMetadata(field.metadata)
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
function serializeArrowType(arrowType) {
|
|
371
|
+
if (isArrowViewType(arrowType, "BinaryView")) {
|
|
372
|
+
return "binary-view";
|
|
373
|
+
}
|
|
374
|
+
if (isArrowViewType(arrowType, "Utf8View")) {
|
|
375
|
+
return "utf8-view";
|
|
376
|
+
}
|
|
377
|
+
switch (arrowType.constructor) {
|
|
378
|
+
case arrow.Null:
|
|
379
|
+
return "null";
|
|
380
|
+
case arrow.Binary:
|
|
381
|
+
return "binary";
|
|
382
|
+
case arrow.FixedSizeBinary:
|
|
383
|
+
return {
|
|
384
|
+
type: "fixed-size-binary",
|
|
385
|
+
byteWidth: arrowType.byteWidth
|
|
386
|
+
};
|
|
387
|
+
case arrow.Bool:
|
|
388
|
+
return "bool";
|
|
389
|
+
case arrow.Int:
|
|
390
|
+
const intType = arrowType;
|
|
391
|
+
return `${intType.isSigned ? "u" : ""}int${intType.bitWidth}`;
|
|
392
|
+
case arrow.Int8:
|
|
393
|
+
return "int8";
|
|
394
|
+
case arrow.Int16:
|
|
395
|
+
return "int16";
|
|
396
|
+
case arrow.Int32:
|
|
397
|
+
return "int32";
|
|
398
|
+
case arrow.Int64:
|
|
399
|
+
return "int64";
|
|
400
|
+
case arrow.Uint8:
|
|
401
|
+
return "uint8";
|
|
402
|
+
case arrow.Uint16:
|
|
403
|
+
return "uint16";
|
|
404
|
+
case arrow.Uint32:
|
|
405
|
+
return "uint32";
|
|
406
|
+
case arrow.Uint64:
|
|
407
|
+
return "uint64";
|
|
408
|
+
case arrow.Float:
|
|
409
|
+
const precision = arrowType.precision;
|
|
410
|
+
switch (precision) {
|
|
411
|
+
case arrow.Precision.HALF:
|
|
412
|
+
return "float16";
|
|
413
|
+
case arrow.Precision.SINGLE:
|
|
414
|
+
return "float32";
|
|
415
|
+
case arrow.Precision.DOUBLE:
|
|
416
|
+
return "float64";
|
|
417
|
+
default:
|
|
418
|
+
return "float16";
|
|
419
|
+
}
|
|
420
|
+
case arrow.Float16:
|
|
421
|
+
return "float16";
|
|
422
|
+
case arrow.Float32:
|
|
423
|
+
return "float32";
|
|
424
|
+
case arrow.Float64:
|
|
425
|
+
return "float64";
|
|
426
|
+
case arrow.Utf8:
|
|
427
|
+
return "utf8";
|
|
428
|
+
case arrow.Decimal:
|
|
429
|
+
const decimal = arrowType;
|
|
430
|
+
return {
|
|
431
|
+
type: "decimal",
|
|
432
|
+
bitWidth: decimal.bitWidth,
|
|
433
|
+
precision: decimal.precision,
|
|
434
|
+
scale: decimal.scale
|
|
435
|
+
};
|
|
436
|
+
case arrow.Date_:
|
|
437
|
+
const dateUnit = arrowType.unit;
|
|
438
|
+
return dateUnit === arrow.DateUnit.DAY ? "date-day" : "date-millisecond";
|
|
439
|
+
case arrow.DateDay:
|
|
440
|
+
return "date-day";
|
|
441
|
+
case arrow.DateMillisecond:
|
|
442
|
+
return "date-millisecond";
|
|
443
|
+
case arrow.Time:
|
|
444
|
+
const timeUnit = arrowType.unit;
|
|
445
|
+
switch (timeUnit) {
|
|
446
|
+
case arrow.TimeUnit.SECOND:
|
|
447
|
+
return "time-second";
|
|
448
|
+
case arrow.TimeUnit.MILLISECOND:
|
|
449
|
+
return "time-millisecond";
|
|
450
|
+
case arrow.TimeUnit.MICROSECOND:
|
|
451
|
+
return "time-microsecond";
|
|
452
|
+
case arrow.TimeUnit.NANOSECOND:
|
|
453
|
+
return "time-nanosecond";
|
|
454
|
+
default:
|
|
455
|
+
return "time-second";
|
|
456
|
+
}
|
|
457
|
+
case arrow.TimeMillisecond:
|
|
458
|
+
return "time-millisecond";
|
|
459
|
+
case arrow.TimeSecond:
|
|
460
|
+
return "time-second";
|
|
461
|
+
case arrow.TimeMicrosecond:
|
|
462
|
+
return "time-microsecond";
|
|
463
|
+
case arrow.TimeNanosecond:
|
|
464
|
+
return "time-nanosecond";
|
|
465
|
+
case arrow.Timestamp:
|
|
466
|
+
const timeStampUnit = arrowType.unit;
|
|
467
|
+
switch (timeStampUnit) {
|
|
468
|
+
case arrow.TimeUnit.SECOND:
|
|
469
|
+
return "timestamp-second";
|
|
470
|
+
case arrow.TimeUnit.MILLISECOND:
|
|
471
|
+
return "timestamp-millisecond";
|
|
472
|
+
case arrow.TimeUnit.MICROSECOND:
|
|
473
|
+
return "timestamp-microsecond";
|
|
474
|
+
case arrow.TimeUnit.NANOSECOND:
|
|
475
|
+
return "timestamp-nanosecond";
|
|
476
|
+
default:
|
|
477
|
+
return "timestamp-second";
|
|
478
|
+
}
|
|
479
|
+
case arrow.TimestampSecond:
|
|
480
|
+
return "timestamp-second";
|
|
481
|
+
case arrow.TimestampMillisecond:
|
|
482
|
+
return "timestamp-millisecond";
|
|
483
|
+
case arrow.TimestampMicrosecond:
|
|
484
|
+
return "timestamp-microsecond";
|
|
485
|
+
case arrow.TimestampNanosecond:
|
|
486
|
+
return "timestamp-nanosecond";
|
|
487
|
+
case arrow.Interval:
|
|
488
|
+
const intervalUnit = arrowType.unit;
|
|
489
|
+
switch (intervalUnit) {
|
|
490
|
+
case arrow.IntervalUnit.DAY_TIME:
|
|
491
|
+
return "interval-daytime";
|
|
492
|
+
case arrow.IntervalUnit.YEAR_MONTH:
|
|
493
|
+
return "interval-yearmonth";
|
|
494
|
+
default:
|
|
495
|
+
return "interval-daytime";
|
|
496
|
+
}
|
|
497
|
+
case arrow.IntervalDayTime:
|
|
498
|
+
return "interval-daytime";
|
|
499
|
+
case arrow.IntervalYearMonth:
|
|
500
|
+
return "interval-yearmonth";
|
|
501
|
+
case arrow.Map_:
|
|
502
|
+
const mapType = arrowType;
|
|
503
|
+
return {
|
|
504
|
+
type: "map",
|
|
505
|
+
keysSorted: mapType.keysSorted,
|
|
506
|
+
children: mapType.children.map((arrowField) => serializeArrowField(arrowField))
|
|
507
|
+
};
|
|
508
|
+
case arrow.List:
|
|
509
|
+
const listType = arrowType;
|
|
510
|
+
const listField = listType.valueField;
|
|
511
|
+
return {
|
|
512
|
+
type: "list",
|
|
513
|
+
children: [serializeArrowField(listField)]
|
|
514
|
+
};
|
|
515
|
+
case arrow.FixedSizeList:
|
|
516
|
+
const fixedSizeList = arrowType;
|
|
517
|
+
return {
|
|
518
|
+
type: "fixed-size-list",
|
|
519
|
+
listSize: fixedSizeList.listSize,
|
|
520
|
+
children: [serializeArrowField(fixedSizeList.children[0])]
|
|
521
|
+
};
|
|
522
|
+
case arrow.Struct:
|
|
523
|
+
const structType = arrowType;
|
|
524
|
+
return {
|
|
525
|
+
type: "struct",
|
|
526
|
+
children: structType.children.map((arrowField) => serializeArrowField(arrowField))
|
|
527
|
+
};
|
|
528
|
+
case arrow.Dictionary:
|
|
529
|
+
const dictionaryType = arrowType;
|
|
530
|
+
return {
|
|
531
|
+
type: "dictionary",
|
|
532
|
+
id: dictionaryType.id,
|
|
533
|
+
indices: serializeArrowDictionaryKeyType(dictionaryType.indices),
|
|
534
|
+
dictionary: serializeArrowType(dictionaryType.dictionary),
|
|
535
|
+
isOrdered: dictionaryType.isOrdered
|
|
536
|
+
};
|
|
537
|
+
default:
|
|
538
|
+
throw new Error(`arrow type not supported: ${arrowType.constructor.name}`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
function deserializeArrowType(dataType, options) {
|
|
542
|
+
if (typeof dataType === "object") {
|
|
543
|
+
switch (dataType.type) {
|
|
544
|
+
case "decimal":
|
|
545
|
+
return new arrow.Decimal(dataType.scale, dataType.precision, dataType.bitWidth);
|
|
546
|
+
case "map": {
|
|
547
|
+
const children = dataType.children.map(
|
|
548
|
+
(arrowField) => deserializeArrowField(arrowField, options)
|
|
549
|
+
);
|
|
550
|
+
return new arrow.Map_(children, dataType.keysSorted);
|
|
551
|
+
}
|
|
552
|
+
case "list": {
|
|
553
|
+
const field = deserializeArrowField(dataType.children[0], options);
|
|
554
|
+
return new arrow.List(field);
|
|
555
|
+
}
|
|
556
|
+
case "fixed-size-list": {
|
|
557
|
+
const child = deserializeArrowField(dataType.children[0], options);
|
|
558
|
+
return new arrow.FixedSizeList(dataType.listSize, child);
|
|
559
|
+
}
|
|
560
|
+
case "fixed-size-binary":
|
|
561
|
+
return new arrow.FixedSizeBinary(dataType.byteWidth);
|
|
562
|
+
case "struct": {
|
|
563
|
+
const children = dataType.children.map(
|
|
564
|
+
(arrowField) => deserializeArrowField(arrowField, options)
|
|
565
|
+
);
|
|
566
|
+
return new arrow.Struct(children);
|
|
567
|
+
}
|
|
568
|
+
case "dictionary": {
|
|
569
|
+
return new arrow.Dictionary(
|
|
570
|
+
deserializeArrowType(dataType.dictionary, options),
|
|
571
|
+
deserializeArrowDictionaryKeyType(dataType.indices),
|
|
572
|
+
dataType.id,
|
|
573
|
+
dataType.isOrdered
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
default:
|
|
577
|
+
throw new Error("array type not supported");
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
switch (dataType) {
|
|
581
|
+
case "null":
|
|
582
|
+
return new arrow.Null();
|
|
583
|
+
case "binary":
|
|
584
|
+
return makeVariableWidthArrowType("BinaryView", new arrow.Binary(), options);
|
|
585
|
+
case "binary-view":
|
|
586
|
+
return makeRequiredArrowViewType("BinaryView");
|
|
587
|
+
case "bool":
|
|
588
|
+
return new arrow.Bool();
|
|
589
|
+
case "int8":
|
|
590
|
+
return new arrow.Int8();
|
|
591
|
+
case "int16":
|
|
592
|
+
return new arrow.Int16();
|
|
593
|
+
case "int32":
|
|
594
|
+
return new arrow.Int32();
|
|
595
|
+
case "int64":
|
|
596
|
+
return new arrow.Int64();
|
|
597
|
+
case "uint8":
|
|
598
|
+
return new arrow.Uint8();
|
|
599
|
+
case "uint16":
|
|
600
|
+
return new arrow.Uint16();
|
|
601
|
+
case "uint32":
|
|
602
|
+
return new arrow.Uint32();
|
|
603
|
+
case "uint64":
|
|
604
|
+
return new arrow.Uint64();
|
|
605
|
+
case "float16":
|
|
606
|
+
return new arrow.Float16();
|
|
607
|
+
case "float32":
|
|
608
|
+
return new arrow.Float32();
|
|
609
|
+
case "float64":
|
|
610
|
+
return new arrow.Float64();
|
|
611
|
+
case "utf8":
|
|
612
|
+
return makeVariableWidthArrowType("Utf8View", new arrow.Utf8(), options);
|
|
613
|
+
case "utf8-view":
|
|
614
|
+
return makeRequiredArrowViewType("Utf8View");
|
|
615
|
+
case "date-day":
|
|
616
|
+
return new arrow.DateDay();
|
|
617
|
+
case "date-millisecond":
|
|
618
|
+
return new arrow.DateMillisecond();
|
|
619
|
+
case "time-second":
|
|
620
|
+
return new arrow.TimeSecond();
|
|
621
|
+
case "time-millisecond":
|
|
622
|
+
return new arrow.TimeMillisecond();
|
|
623
|
+
case "time-microsecond":
|
|
624
|
+
return new arrow.TimeMicrosecond();
|
|
625
|
+
case "time-nanosecond":
|
|
626
|
+
return new arrow.TimeNanosecond();
|
|
627
|
+
case "timestamp-second":
|
|
628
|
+
return new arrow.TimestampSecond();
|
|
629
|
+
case "timestamp-millisecond":
|
|
630
|
+
return new arrow.TimestampMillisecond();
|
|
631
|
+
case "timestamp-microsecond":
|
|
632
|
+
return new arrow.TimestampMicrosecond();
|
|
633
|
+
case "timestamp-nanosecond":
|
|
634
|
+
return new arrow.TimestampNanosecond();
|
|
635
|
+
case "interval-daytime":
|
|
636
|
+
return new arrow.IntervalDayTime();
|
|
637
|
+
case "interval-yearmonth":
|
|
638
|
+
return new arrow.IntervalYearMonth();
|
|
639
|
+
default:
|
|
640
|
+
throw new Error("array type not supported");
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
function getArrowViewConstructors() {
|
|
644
|
+
return arrow;
|
|
645
|
+
}
|
|
646
|
+
function isArrowViewType(arrowType, viewTypeName) {
|
|
647
|
+
const constructor = getArrowViewConstructors()[viewTypeName];
|
|
648
|
+
if (constructor && arrowType instanceof constructor) {
|
|
649
|
+
return true;
|
|
650
|
+
}
|
|
651
|
+
const guards = arrow.DataType;
|
|
652
|
+
const guard = viewTypeName === "BinaryView" ? guards.isBinaryView : guards.isUtf8View;
|
|
653
|
+
return Boolean(guard == null ? void 0 : guard(arrowType));
|
|
654
|
+
}
|
|
655
|
+
function makeVariableWidthArrowType(viewTypeName, fallbackType, options) {
|
|
656
|
+
const viewTypeMode = (options == null ? void 0 : options.viewTypes) || "never";
|
|
657
|
+
if (viewTypeMode === "never") {
|
|
658
|
+
return fallbackType;
|
|
659
|
+
}
|
|
660
|
+
const constructor = getArrowViewConstructors()[viewTypeName];
|
|
661
|
+
if (constructor) {
|
|
662
|
+
return new constructor();
|
|
663
|
+
}
|
|
664
|
+
if (viewTypeMode === "require") {
|
|
665
|
+
throw new Error(
|
|
666
|
+
`${viewTypeName} requires apache-arrow 21.2.0 or later; the installed runtime does not support it`
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
return fallbackType;
|
|
670
|
+
}
|
|
671
|
+
function makeRequiredArrowViewType(viewTypeName) {
|
|
672
|
+
const constructor = getArrowViewConstructors()[viewTypeName];
|
|
673
|
+
if (!constructor) {
|
|
674
|
+
throw new Error(
|
|
675
|
+
`${viewTypeName} requires apache-arrow 21.2.0 or later; the installed runtime does not support it`
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
return new constructor();
|
|
679
|
+
}
|
|
680
|
+
function serializeArrowDictionaryKeyType(arrowType) {
|
|
681
|
+
if (arrowType instanceof arrow.Int) {
|
|
682
|
+
const prefix = arrowType.isSigned ? "int" : "uint";
|
|
683
|
+
const keyType = `${prefix}${arrowType.bitWidth}`;
|
|
684
|
+
if (isSchemaDictionaryKeyType(keyType)) {
|
|
685
|
+
return keyType;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
switch (arrowType.constructor) {
|
|
689
|
+
case arrow.Int8:
|
|
690
|
+
return "int8";
|
|
691
|
+
case arrow.Int16:
|
|
692
|
+
return "int16";
|
|
693
|
+
case arrow.Int32:
|
|
694
|
+
return "int32";
|
|
695
|
+
case arrow.Uint8:
|
|
696
|
+
return "uint8";
|
|
697
|
+
case arrow.Uint16:
|
|
698
|
+
return "uint16";
|
|
699
|
+
case arrow.Uint32:
|
|
700
|
+
return "uint32";
|
|
701
|
+
default:
|
|
702
|
+
throw new Error(`arrow dictionary index type not supported: ${arrowType.constructor.name}`);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
function isSchemaDictionaryKeyType(keyType) {
|
|
706
|
+
return ["int8", "int16", "int32", "uint8", "uint16", "uint32"].includes(keyType);
|
|
707
|
+
}
|
|
708
|
+
function deserializeArrowDictionaryKeyType(keyType) {
|
|
709
|
+
switch (keyType) {
|
|
710
|
+
case "int8":
|
|
711
|
+
return new arrow.Int8();
|
|
712
|
+
case "int16":
|
|
713
|
+
return new arrow.Int16();
|
|
714
|
+
case "int32":
|
|
715
|
+
return new arrow.Int32();
|
|
716
|
+
case "uint8":
|
|
717
|
+
return new arrow.Uint8();
|
|
718
|
+
case "uint16":
|
|
719
|
+
return new arrow.Uint16();
|
|
720
|
+
case "uint32":
|
|
721
|
+
return new arrow.Uint32();
|
|
722
|
+
default:
|
|
723
|
+
throw new Error(`schema dictionary index type not supported: ${keyType}`);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// ../schema-utils/src/lib/schema/deduce-table-schema.ts
|
|
728
|
+
function deduceTableSchema(table) {
|
|
729
|
+
switch (table.shape) {
|
|
730
|
+
case "array-row-table":
|
|
731
|
+
case "object-row-table":
|
|
732
|
+
return deduceSchemaFromRows(table.data);
|
|
733
|
+
case "geojson-table":
|
|
734
|
+
return deduceSchemaFromGeoJSON(table.features);
|
|
735
|
+
case "columnar-table":
|
|
736
|
+
return deduceSchemaFromColumns(table.data);
|
|
737
|
+
case "arrow-table":
|
|
738
|
+
default:
|
|
739
|
+
throw new Error("Deduce schema");
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
function deduceSchemaFromColumns(columnarTable) {
|
|
743
|
+
const fields = [];
|
|
744
|
+
for (const [columnName, column] of Object.entries(columnarTable)) {
|
|
745
|
+
const field = deduceFieldFromColumn(column, columnName);
|
|
746
|
+
fields.push(field);
|
|
747
|
+
}
|
|
748
|
+
return { fields, metadata: {} };
|
|
749
|
+
}
|
|
750
|
+
function deduceSchemaFromRows(rowTable) {
|
|
751
|
+
if (!rowTable.length) {
|
|
752
|
+
throw new Error("deduce from empty table");
|
|
753
|
+
}
|
|
754
|
+
const fields = [];
|
|
755
|
+
const row0 = rowTable[0];
|
|
756
|
+
for (const [columnName, value] of Object.entries(row0)) {
|
|
757
|
+
fields.push(deduceFieldFromValue(value, columnName));
|
|
758
|
+
}
|
|
759
|
+
return { fields, metadata: {} };
|
|
760
|
+
}
|
|
761
|
+
function deduceSchemaFromGeoJSON(features) {
|
|
762
|
+
if (!features.length) {
|
|
763
|
+
throw new Error("deduce from empty table");
|
|
764
|
+
}
|
|
765
|
+
const fields = [];
|
|
766
|
+
const row0 = features[0].properties || {};
|
|
767
|
+
for (const [columnName, value] of Object.entries(row0)) {
|
|
768
|
+
fields.push(deduceFieldFromValue(value, columnName));
|
|
769
|
+
}
|
|
770
|
+
return { fields, metadata: {} };
|
|
771
|
+
}
|
|
772
|
+
function deduceFieldFromColumn(column, name) {
|
|
773
|
+
if (ArrayBuffer.isView(column)) {
|
|
774
|
+
const type = getDataTypeFromArray(column);
|
|
775
|
+
return {
|
|
776
|
+
name,
|
|
777
|
+
type: type.type || "null",
|
|
778
|
+
nullable: type.nullable
|
|
779
|
+
// metadata: {}
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
if (Array.isArray(column) && column.length > 0) {
|
|
783
|
+
const value = column[0];
|
|
784
|
+
const type = getDataTypeFromValue(value);
|
|
785
|
+
return {
|
|
786
|
+
name,
|
|
787
|
+
type,
|
|
788
|
+
nullable: true
|
|
789
|
+
// metadata: {},
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
throw new Error("empty table");
|
|
793
|
+
}
|
|
794
|
+
function deduceFieldFromValue(value, name) {
|
|
795
|
+
const type = getDataTypeFromValue(value);
|
|
796
|
+
return {
|
|
797
|
+
name,
|
|
798
|
+
type,
|
|
799
|
+
nullable: true
|
|
800
|
+
// metadata: {}
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// ../schema-utils/src/lib/table/tables/make-table.ts
|
|
805
|
+
var arrow2 = __toESM(require("apache-arrow"), 1);
|
|
806
|
+
function makeTableFromData(data) {
|
|
807
|
+
let table;
|
|
808
|
+
switch (getTableShapeFromData(data)) {
|
|
809
|
+
case "array-row-table":
|
|
810
|
+
table = { shape: "array-row-table", data };
|
|
811
|
+
break;
|
|
812
|
+
case "object-row-table":
|
|
813
|
+
table = { shape: "object-row-table", data };
|
|
814
|
+
break;
|
|
815
|
+
case "columnar-table":
|
|
816
|
+
table = { shape: "columnar-table", data };
|
|
817
|
+
break;
|
|
818
|
+
case "arrow-table":
|
|
819
|
+
table = { shape: "arrow-table", data };
|
|
820
|
+
break;
|
|
821
|
+
default:
|
|
822
|
+
throw new Error("table");
|
|
823
|
+
}
|
|
824
|
+
const schema = deduceTableSchema(table);
|
|
825
|
+
return { ...table, schema };
|
|
826
|
+
}
|
|
827
|
+
function getTableShapeFromData(data) {
|
|
828
|
+
if (data instanceof arrow2.Table) {
|
|
829
|
+
return "arrow-table";
|
|
830
|
+
}
|
|
831
|
+
if (Array.isArray(data)) {
|
|
832
|
+
if (data.length === 0) {
|
|
833
|
+
throw new Error("cannot deduce type of empty table");
|
|
834
|
+
}
|
|
835
|
+
const firstRow = data[0];
|
|
836
|
+
if (Array.isArray(firstRow)) {
|
|
837
|
+
return "array-row-table";
|
|
838
|
+
}
|
|
839
|
+
if (firstRow && typeof firstRow === "object") {
|
|
840
|
+
return "object-row-table";
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
if (data && typeof data === "object") {
|
|
844
|
+
return "columnar-table";
|
|
845
|
+
}
|
|
846
|
+
throw new Error("invalid table");
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
// ../schema-utils/src/lib/table/tables/table-accessors.ts
|
|
850
|
+
function getTableLength(table) {
|
|
851
|
+
switch (table.shape) {
|
|
852
|
+
case "array-row-table":
|
|
853
|
+
case "object-row-table":
|
|
854
|
+
return table.data.length;
|
|
855
|
+
case "geojson-table":
|
|
856
|
+
return table.features.length;
|
|
857
|
+
case "arrow-table":
|
|
858
|
+
const arrowTable = table.data;
|
|
859
|
+
return arrowTable.numRows;
|
|
860
|
+
case "columnar-table":
|
|
861
|
+
for (const column of Object.values(table.data)) {
|
|
862
|
+
return column.length || 0;
|
|
863
|
+
}
|
|
864
|
+
return 0;
|
|
865
|
+
default:
|
|
866
|
+
throw new Error("table");
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
function getTableNumCols(table) {
|
|
870
|
+
if (table.schema) {
|
|
871
|
+
return table.schema.fields.length;
|
|
872
|
+
}
|
|
873
|
+
if (getTableLength(table) === 0) {
|
|
874
|
+
throw new Error("empty table");
|
|
875
|
+
}
|
|
876
|
+
switch (table.shape) {
|
|
877
|
+
case "array-row-table":
|
|
878
|
+
return table.data[0].length;
|
|
879
|
+
case "object-row-table":
|
|
880
|
+
return Object.keys(table.data[0]).length;
|
|
881
|
+
case "geojson-table":
|
|
882
|
+
return Object.keys(table.features[0]).length;
|
|
883
|
+
case "columnar-table":
|
|
884
|
+
return Object.keys(table.data).length;
|
|
885
|
+
case "arrow-table":
|
|
886
|
+
const arrowTable = table.data;
|
|
887
|
+
return arrowTable.numCols;
|
|
888
|
+
default:
|
|
889
|
+
throw new Error("table");
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
function getTableCell(table, rowIndex, columnName) {
|
|
893
|
+
var _a;
|
|
894
|
+
switch (table.shape) {
|
|
895
|
+
case "array-row-table":
|
|
896
|
+
const columnIndex = getTableColumnIndex(table, columnName);
|
|
897
|
+
return table.data[rowIndex][columnIndex];
|
|
898
|
+
case "object-row-table":
|
|
899
|
+
return table.data[rowIndex][columnName];
|
|
900
|
+
case "geojson-table":
|
|
901
|
+
return table.features[rowIndex][columnName];
|
|
902
|
+
case "columnar-table":
|
|
903
|
+
const column = table.data[columnName];
|
|
904
|
+
return column[rowIndex];
|
|
905
|
+
case "arrow-table":
|
|
906
|
+
const arrowTable = table.data;
|
|
907
|
+
const arrowColumnIndex = arrowTable.schema.fields.findIndex(
|
|
908
|
+
(field) => field.name === columnName
|
|
909
|
+
);
|
|
910
|
+
return (_a = arrowTable.getChildAt(arrowColumnIndex)) == null ? void 0 : _a.get(rowIndex);
|
|
911
|
+
default:
|
|
912
|
+
throw new Error("todo");
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
function getTableCellAt(table, rowIndex, columnIndex) {
|
|
916
|
+
var _a;
|
|
917
|
+
switch (table.shape) {
|
|
918
|
+
case "array-row-table":
|
|
919
|
+
return table.data[rowIndex][columnIndex];
|
|
920
|
+
case "object-row-table":
|
|
921
|
+
const columnName1 = getTableColumnName(table, columnIndex);
|
|
922
|
+
return table.data[rowIndex][columnName1];
|
|
923
|
+
case "geojson-table":
|
|
924
|
+
const columnName2 = getTableColumnName(table, columnIndex);
|
|
925
|
+
return table.features[rowIndex][columnName2];
|
|
926
|
+
case "columnar-table":
|
|
927
|
+
const columnName3 = getTableColumnName(table, columnIndex);
|
|
928
|
+
const column = table.data[columnName3];
|
|
929
|
+
return column[rowIndex];
|
|
930
|
+
case "arrow-table":
|
|
931
|
+
const arrowTable = table.data;
|
|
932
|
+
return (_a = arrowTable.getChildAt(columnIndex)) == null ? void 0 : _a.get(rowIndex);
|
|
933
|
+
default:
|
|
934
|
+
throw new Error("todo");
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
function getTableColumnIndex(table, columnName) {
|
|
938
|
+
var _a;
|
|
939
|
+
const columnIndex = (_a = table.schema) == null ? void 0 : _a.fields.findIndex((field) => field.name === columnName);
|
|
940
|
+
if (columnIndex === void 0) {
|
|
941
|
+
throw new Error(columnName);
|
|
942
|
+
}
|
|
943
|
+
return columnIndex;
|
|
944
|
+
}
|
|
945
|
+
function getTableColumnName(table, columnIndex) {
|
|
946
|
+
var _a, _b;
|
|
947
|
+
const columnName = (_b = (_a = table.schema) == null ? void 0 : _a.fields[columnIndex]) == null ? void 0 : _b.name;
|
|
948
|
+
if (!columnName) {
|
|
949
|
+
throw new Error(`${columnIndex}`);
|
|
950
|
+
}
|
|
951
|
+
return columnName;
|
|
952
|
+
}
|
|
953
|
+
function getTableRowAsObject(table, rowIndex, target, copy) {
|
|
954
|
+
switch (table.shape) {
|
|
955
|
+
case "object-row-table":
|
|
956
|
+
return copy ? Object.fromEntries(Object.entries(table.data[rowIndex])) : table.data[rowIndex];
|
|
957
|
+
case "array-row-table":
|
|
958
|
+
if (table.schema) {
|
|
959
|
+
const objectRow2 = target || {};
|
|
960
|
+
for (let i = 0; i < table.schema.fields.length; i++) {
|
|
961
|
+
objectRow2[table.schema.fields[i].name] = table.data[rowIndex][i];
|
|
962
|
+
}
|
|
963
|
+
return objectRow2;
|
|
964
|
+
}
|
|
965
|
+
throw new Error("no schema");
|
|
966
|
+
case "geojson-table":
|
|
967
|
+
if (table.schema) {
|
|
968
|
+
const objectRow2 = target || {};
|
|
969
|
+
for (let i = 0; i < table.schema.fields.length; i++) {
|
|
970
|
+
objectRow2[table.schema.fields[i].name] = table.features[rowIndex][i];
|
|
971
|
+
}
|
|
972
|
+
return objectRow2;
|
|
973
|
+
}
|
|
974
|
+
throw new Error("no schema");
|
|
975
|
+
case "columnar-table":
|
|
976
|
+
if (table.schema) {
|
|
977
|
+
const objectRow2 = target || {};
|
|
978
|
+
for (let i = 0; i < table.schema.fields.length; i++) {
|
|
979
|
+
objectRow2[table.schema.fields[i].name] = table.data[table.schema.fields[i].name][rowIndex];
|
|
980
|
+
}
|
|
981
|
+
return objectRow2;
|
|
982
|
+
} else {
|
|
983
|
+
const objectRow2 = target || {};
|
|
984
|
+
for (const [name, column] of Object.entries(table.data)) {
|
|
985
|
+
objectRow2[name] = column[rowIndex];
|
|
986
|
+
}
|
|
987
|
+
return objectRow2;
|
|
988
|
+
}
|
|
989
|
+
case "arrow-table":
|
|
990
|
+
const arrowTable = table.data;
|
|
991
|
+
const objectRow = target || {};
|
|
992
|
+
const row = arrowTable.get(rowIndex);
|
|
993
|
+
const schema = arrowTable.schema;
|
|
994
|
+
for (let i = 0; i < schema.fields.length; i++) {
|
|
995
|
+
objectRow[schema.fields[i].name] = row == null ? void 0 : row[schema.fields[i].name];
|
|
996
|
+
}
|
|
997
|
+
return objectRow;
|
|
998
|
+
default:
|
|
999
|
+
throw new Error("shape");
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
function getTableRowAsArray(table, rowIndex, target, copy) {
|
|
1003
|
+
switch (table.shape) {
|
|
1004
|
+
case "array-row-table":
|
|
1005
|
+
return copy ? Array.from(table.data[rowIndex]) : table.data[rowIndex];
|
|
1006
|
+
case "object-row-table":
|
|
1007
|
+
if (table.schema) {
|
|
1008
|
+
const arrayRow2 = target || [];
|
|
1009
|
+
for (let i = 0; i < table.schema.fields.length; i++) {
|
|
1010
|
+
arrayRow2[i] = table.data[rowIndex][table.schema.fields[i].name];
|
|
1011
|
+
}
|
|
1012
|
+
return arrayRow2;
|
|
1013
|
+
}
|
|
1014
|
+
return Object.values(table.data[rowIndex]);
|
|
1015
|
+
case "geojson-table":
|
|
1016
|
+
if (table.schema) {
|
|
1017
|
+
const arrayRow2 = target || [];
|
|
1018
|
+
for (let i = 0; i < table.schema.fields.length; i++) {
|
|
1019
|
+
arrayRow2[i] = table.features[rowIndex][table.schema.fields[i].name];
|
|
1020
|
+
}
|
|
1021
|
+
return arrayRow2;
|
|
1022
|
+
}
|
|
1023
|
+
return Object.values(table.features[rowIndex]);
|
|
1024
|
+
case "columnar-table":
|
|
1025
|
+
if (table.schema) {
|
|
1026
|
+
const arrayRow2 = target || [];
|
|
1027
|
+
for (let i = 0; i < table.schema.fields.length; i++) {
|
|
1028
|
+
arrayRow2[i] = table.data[table.schema.fields[i].name][rowIndex];
|
|
1029
|
+
}
|
|
1030
|
+
return arrayRow2;
|
|
1031
|
+
} else {
|
|
1032
|
+
const arrayRow2 = target || [];
|
|
1033
|
+
let i = 0;
|
|
1034
|
+
for (const column of Object.values(table.data)) {
|
|
1035
|
+
arrayRow2[i] = column[rowIndex];
|
|
1036
|
+
i++;
|
|
1037
|
+
}
|
|
1038
|
+
return arrayRow2;
|
|
1039
|
+
}
|
|
1040
|
+
case "arrow-table":
|
|
1041
|
+
const arrowTable = table.data;
|
|
1042
|
+
const arrayRow = target || [];
|
|
1043
|
+
const row = arrowTable.get(rowIndex);
|
|
1044
|
+
const schema = arrowTable.schema;
|
|
1045
|
+
for (let i = 0; i < schema.fields.length; i++) {
|
|
1046
|
+
arrayRow[i] = row == null ? void 0 : row[schema.fields[i].name];
|
|
1047
|
+
}
|
|
1048
|
+
return arrayRow;
|
|
1049
|
+
default:
|
|
1050
|
+
throw new Error("shape");
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// ../schema-utils/src/lib/table/tables/table-column.ts
|
|
1055
|
+
function makeColumnFromField(field, length) {
|
|
1056
|
+
const ArrayType = getArrayTypeFromDataType(field.type, field.nullable);
|
|
1057
|
+
return new ArrayType(length);
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
// ../schema-utils/src/lib/table/tables/convert-arrow-table.ts
|
|
1061
|
+
var arrow4 = __toESM(require("apache-arrow"), 1);
|
|
1062
|
+
|
|
1063
|
+
// ../schema-utils/src/lib/table/batches/make-arrow-batch-iterator.ts
|
|
1064
|
+
var arrow3 = __toESM(require("apache-arrow"), 1);
|
|
1065
|
+
function* makeArrowRecordBatchIterator(table, options) {
|
|
1066
|
+
const arrowSchema = convertSchemaToArrow(table.schema);
|
|
1067
|
+
const length = getTableLength(table);
|
|
1068
|
+
const numColumns = getTableNumCols(table);
|
|
1069
|
+
const batchSize = (options == null ? void 0 : options.batchSize) || length;
|
|
1070
|
+
const builders = arrowSchema == null ? void 0 : arrowSchema.fields.map(
|
|
1071
|
+
(arrowField) => arrow3.makeBuilder({
|
|
1072
|
+
type: arrowField.type,
|
|
1073
|
+
nullValues: [null, void 0]
|
|
1074
|
+
})
|
|
1075
|
+
);
|
|
1076
|
+
const structField = new arrow3.Struct(arrowSchema.fields);
|
|
1077
|
+
let batchLength = 0;
|
|
1078
|
+
for (let rowIndex = 0; rowIndex < length; rowIndex++) {
|
|
1079
|
+
for (let columnIndex = 0; columnIndex < numColumns; ++columnIndex) {
|
|
1080
|
+
const value = getTableCellAt(table, rowIndex, columnIndex);
|
|
1081
|
+
const builder = builders[columnIndex];
|
|
1082
|
+
builder.append(value);
|
|
1083
|
+
}
|
|
1084
|
+
batchLength++;
|
|
1085
|
+
if (batchLength >= batchSize) {
|
|
1086
|
+
const datas = builders.map((builder) => builder.flush());
|
|
1087
|
+
const structData = new arrow3.Data(structField, 0, batchLength, 0, void 0, datas);
|
|
1088
|
+
yield new arrow3.RecordBatch(arrowSchema, structData);
|
|
1089
|
+
batchLength = 0;
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
if (batchLength > 0) {
|
|
1093
|
+
const datas = builders.map((builder) => builder.flush());
|
|
1094
|
+
const structData = new arrow3.Data(structField, 0, batchLength, 0, void 0, datas);
|
|
1095
|
+
yield new arrow3.RecordBatch(arrowSchema, structData);
|
|
1096
|
+
batchLength = 0;
|
|
1097
|
+
}
|
|
1098
|
+
builders.map((builder) => builder.finish());
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
// ../schema-utils/src/lib/table/tables/convert-arrow-table.ts
|
|
1102
|
+
function convertTableToArrow(table, options) {
|
|
1103
|
+
switch (table.shape) {
|
|
1104
|
+
case "arrow-table":
|
|
1105
|
+
return table.data;
|
|
1106
|
+
case "columnar-table":
|
|
1107
|
+
// TODO - optimized implementation is possible
|
|
1108
|
+
// return convertColumnarTableToArrow(table, options);
|
|
1109
|
+
// fall through
|
|
1110
|
+
default:
|
|
1111
|
+
const arrowBatchIterator = makeArrowRecordBatchIterator(table, options);
|
|
1112
|
+
return new arrow4.Table(arrowBatchIterator);
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
function convertArrowToTable(arrowTable, shape) {
|
|
1116
|
+
switch (shape) {
|
|
1117
|
+
case "arrow-table":
|
|
1118
|
+
return convertArrowToArrowTable(arrowTable);
|
|
1119
|
+
case "array-row-table":
|
|
1120
|
+
return convertArrowToArrayRowTable(arrowTable);
|
|
1121
|
+
case "object-row-table":
|
|
1122
|
+
return convertArrowToObjectRowTable(arrowTable);
|
|
1123
|
+
case "columnar-table":
|
|
1124
|
+
return convertArrowToColumnarTable(arrowTable);
|
|
1125
|
+
case "geojson-table":
|
|
1126
|
+
return convertArrowToGeoJSONTable(arrowTable);
|
|
1127
|
+
default:
|
|
1128
|
+
throw new Error(shape);
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
function convertArrowToArrowTable(arrowTable) {
|
|
1132
|
+
return {
|
|
1133
|
+
shape: "arrow-table",
|
|
1134
|
+
schema: convertArrowToSchema(arrowTable.schema),
|
|
1135
|
+
data: arrowTable
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
1138
|
+
function convertArrowToArrayRowTable(arrowTable) {
|
|
1139
|
+
const columnarTable = convertArrowToColumnarTable(arrowTable);
|
|
1140
|
+
return convertTable(columnarTable, "array-row-table");
|
|
1141
|
+
}
|
|
1142
|
+
function convertArrowToObjectRowTable(arrowTable) {
|
|
1143
|
+
const columnarTable = convertArrowToColumnarTable(arrowTable);
|
|
1144
|
+
return convertTable(columnarTable, "object-row-table");
|
|
1145
|
+
}
|
|
1146
|
+
function convertArrowToColumnarTable(arrowTable) {
|
|
1147
|
+
const columns = {};
|
|
1148
|
+
for (const field of arrowTable.schema.fields) {
|
|
1149
|
+
const arrowColumn = arrowTable.getChild(field.name);
|
|
1150
|
+
const values = arrowColumn == null ? void 0 : arrowColumn.toArray();
|
|
1151
|
+
columns[field.name] = values;
|
|
1152
|
+
}
|
|
1153
|
+
const schema = convertArrowToSchema(arrowTable.schema);
|
|
1154
|
+
return {
|
|
1155
|
+
shape: "columnar-table",
|
|
1156
|
+
schema,
|
|
1157
|
+
data: columns
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
function convertArrowToGeoJSONTable(arrowTable) {
|
|
1161
|
+
var _a;
|
|
1162
|
+
const schema = convertArrowToSchema(arrowTable.schema);
|
|
1163
|
+
const features = [];
|
|
1164
|
+
for (let row = 0; row < arrowTable.numRows; row++) {
|
|
1165
|
+
const properties = ((_a = arrowTable.get(row)) == null ? void 0 : _a.toJSON()) || {};
|
|
1166
|
+
features.push({ type: "Feature", geometry: null, properties });
|
|
1167
|
+
}
|
|
1168
|
+
return {
|
|
1169
|
+
shape: "geojson-table",
|
|
1170
|
+
type: "FeatureCollection",
|
|
1171
|
+
schema,
|
|
1172
|
+
features
|
|
1173
|
+
};
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
// ../schema-utils/src/lib/table/tables/convert-table.ts
|
|
1177
|
+
function convertTable(table, shape) {
|
|
1178
|
+
switch (shape) {
|
|
1179
|
+
case "object-row-table":
|
|
1180
|
+
return convertToObjectRowTable(table);
|
|
1181
|
+
case "array-row-table":
|
|
1182
|
+
return convertToArrayRowTable(table);
|
|
1183
|
+
case "columnar-table":
|
|
1184
|
+
return convertToColumnarTable(table);
|
|
1185
|
+
case "arrow-table":
|
|
1186
|
+
return convertToArrowTable(table);
|
|
1187
|
+
default:
|
|
1188
|
+
throw new Error(shape);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
function convertToColumnarTable(table) {
|
|
1192
|
+
var _a;
|
|
1193
|
+
const schema = table.schema || deduceTableSchema(table);
|
|
1194
|
+
const fields = ((_a = table.schema) == null ? void 0 : _a.fields) || [];
|
|
1195
|
+
if (table.shape === "columnar-table") {
|
|
1196
|
+
return { ...table, schema };
|
|
1197
|
+
}
|
|
1198
|
+
const length = getTableLength(table);
|
|
1199
|
+
const columns = {};
|
|
1200
|
+
for (const field of fields) {
|
|
1201
|
+
const column = makeColumnFromField(field, length);
|
|
1202
|
+
columns[field.name] = column;
|
|
1203
|
+
for (let rowIndex = 0; rowIndex < length; rowIndex++) {
|
|
1204
|
+
column[rowIndex] = getTableCell(table, rowIndex, field.name);
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
return {
|
|
1208
|
+
shape: "columnar-table",
|
|
1209
|
+
schema,
|
|
1210
|
+
data: columns
|
|
1211
|
+
};
|
|
1212
|
+
}
|
|
1213
|
+
function convertToArrayRowTable(table) {
|
|
1214
|
+
if (table.shape === "array-row-table") {
|
|
1215
|
+
return table;
|
|
1216
|
+
}
|
|
1217
|
+
const length = getTableLength(table);
|
|
1218
|
+
const data = new Array(length);
|
|
1219
|
+
for (let rowIndex = 0; rowIndex < length; rowIndex++) {
|
|
1220
|
+
data[rowIndex] = getTableRowAsArray(table, rowIndex);
|
|
1221
|
+
}
|
|
1222
|
+
return {
|
|
1223
|
+
shape: "array-row-table",
|
|
1224
|
+
schema: table.schema,
|
|
1225
|
+
data
|
|
1226
|
+
};
|
|
1227
|
+
}
|
|
1228
|
+
function convertToObjectRowTable(table) {
|
|
1229
|
+
if (table.shape === "object-row-table") {
|
|
1230
|
+
return table;
|
|
1231
|
+
}
|
|
1232
|
+
const length = getTableLength(table);
|
|
1233
|
+
const data = new Array(length);
|
|
1234
|
+
for (let rowIndex = 0; rowIndex < length; rowIndex++) {
|
|
1235
|
+
data[rowIndex] = getTableRowAsObject(table, rowIndex);
|
|
1236
|
+
}
|
|
1237
|
+
return {
|
|
1238
|
+
shape: "object-row-table",
|
|
1239
|
+
schema: table.schema,
|
|
1240
|
+
data
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
function convertToArrowTable(table) {
|
|
1244
|
+
const schema = table.schema || deduceTableSchema(table);
|
|
1245
|
+
const arrowTable = convertTableToArrow({ ...table, schema });
|
|
1246
|
+
return {
|
|
1247
|
+
shape: "arrow-table",
|
|
1248
|
+
schema,
|
|
1249
|
+
data: arrowTable
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
// ../schema-utils/src/deprecated.ts
|
|
1254
|
+
var convertArrowToTable2 = ((...args) => convertArrowToTable(
|
|
1255
|
+
...args
|
|
1256
|
+
));
|
|
1257
|
+
|
|
1258
|
+
// dist/sql-utils.js
|
|
49
1259
|
function isNodeRuntime() {
|
|
50
1260
|
var _a;
|
|
51
1261
|
return typeof process !== "undefined" && Boolean((_a = process.versions) == null ? void 0 : _a.node);
|
|
@@ -57,19 +1267,19 @@ function quoteSqlIdentifier(identifier) {
|
|
|
57
1267
|
return `"${identifier.replace(/"/g, '""')}"`;
|
|
58
1268
|
}
|
|
59
1269
|
function convertRowsToArrowTable(rows) {
|
|
60
|
-
const table = rows.length > 0 ?
|
|
1270
|
+
const table = rows.length > 0 ? makeTableFromData(rows) : { shape: "object-row-table", schema: { fields: [], metadata: {} }, data: rows };
|
|
61
1271
|
const schema = deducePrimitiveAwareSchema(table);
|
|
62
1272
|
if (table.data.length === 0 || schema.fields.length === 0) {
|
|
63
1273
|
return {
|
|
64
1274
|
shape: "arrow-table",
|
|
65
1275
|
schema,
|
|
66
|
-
data: new
|
|
1276
|
+
data: new arrow5.Table(new arrow5.Schema([]))
|
|
67
1277
|
};
|
|
68
1278
|
}
|
|
69
1279
|
return {
|
|
70
1280
|
shape: "arrow-table",
|
|
71
1281
|
schema,
|
|
72
|
-
data:
|
|
1282
|
+
data: arrow5.tableFromJSON(table.data)
|
|
73
1283
|
};
|
|
74
1284
|
}
|
|
75
1285
|
function convertSQLColumnsToSchema(columns) {
|
|
@@ -121,7 +1331,7 @@ function deducePrimitiveAwareSchema(table) {
|
|
|
121
1331
|
return field;
|
|
122
1332
|
}
|
|
123
1333
|
const columnValues = table.data.map((row) => row[field.name]);
|
|
124
|
-
const inferredType =
|
|
1334
|
+
const inferredType = getDataTypeFromArray(columnValues);
|
|
125
1335
|
return { ...field, type: inferredType.type, nullable: inferredType.nullable };
|
|
126
1336
|
})
|
|
127
1337
|
};
|
|
@@ -163,7 +1373,7 @@ var SQL_SOURCE_DEFAULT_OPTIONS = {
|
|
|
163
1373
|
authenticator: void 0
|
|
164
1374
|
}
|
|
165
1375
|
};
|
|
166
|
-
var SQLDataSource = class extends
|
|
1376
|
+
var SQLDataSource = class extends DataSource {
|
|
167
1377
|
sourceType;
|
|
168
1378
|
adapterFactory;
|
|
169
1379
|
adapterPromise = null;
|
|
@@ -268,15 +1478,14 @@ var SQLDataSource = class extends import_loader_utils.DataSource {
|
|
|
268
1478
|
};
|
|
269
1479
|
|
|
270
1480
|
// dist/duckdb-sql-source.js
|
|
271
|
-
var
|
|
272
|
-
var VERSION = true ? "5.0.0-alpha.1" : "latest";
|
|
1481
|
+
var VERSION2 = true ? "5.0.0-alpha.2" : "latest";
|
|
273
1482
|
var DuckDBSQLSource = {
|
|
274
1483
|
dataType: null,
|
|
275
1484
|
batchType: null,
|
|
276
1485
|
name: "DuckDBSQLSource",
|
|
277
1486
|
id: "duckdb-sql",
|
|
278
1487
|
module: "sql",
|
|
279
|
-
version:
|
|
1488
|
+
version: VERSION2,
|
|
280
1489
|
extensions: ["duckdb", "db"],
|
|
281
1490
|
mimeTypes: [],
|
|
282
1491
|
type: "duckdb-sql",
|
|
@@ -450,7 +1659,7 @@ async function createDuckDBBrowserAdapter(context) {
|
|
|
450
1659
|
const connection = await database.connect();
|
|
451
1660
|
const executeRows = async (sqlText) => {
|
|
452
1661
|
const result = await connection.query(sqlText);
|
|
453
|
-
return (
|
|
1662
|
+
return convertArrowToTable2(result, "object-row-table").data;
|
|
454
1663
|
};
|
|
455
1664
|
return {
|
|
456
1665
|
capabilities: {
|
|
@@ -535,7 +1744,7 @@ async function createDuckDBBrowserAdapter(context) {
|
|
|
535
1744
|
},
|
|
536
1745
|
async executeArrow(sqlText) {
|
|
537
1746
|
const result = await connection.query(sqlText);
|
|
538
|
-
return (
|
|
1747
|
+
return convertArrowToTable2(result, "arrow-table");
|
|
539
1748
|
}
|
|
540
1749
|
};
|
|
541
1750
|
}
|
|
@@ -553,14 +1762,14 @@ function getDuckDBDatabasePath(url, options) {
|
|
|
553
1762
|
}
|
|
554
1763
|
|
|
555
1764
|
// dist/snowflake-sql-source.js
|
|
556
|
-
var
|
|
1765
|
+
var VERSION3 = true ? "5.0.0-alpha.2" : "latest";
|
|
557
1766
|
var SnowflakeSQLSource = {
|
|
558
1767
|
dataType: null,
|
|
559
1768
|
batchType: null,
|
|
560
1769
|
name: "SnowflakeSQLSource",
|
|
561
1770
|
id: "snowflake-sql",
|
|
562
1771
|
module: "sql",
|
|
563
|
-
version:
|
|
1772
|
+
version: VERSION3,
|
|
564
1773
|
extensions: [],
|
|
565
1774
|
mimeTypes: [],
|
|
566
1775
|
type: "snowflake-sql",
|
|
@@ -756,4 +1965,475 @@ function getSnowflakeBaseUrl(url, options) {
|
|
|
756
1965
|
}
|
|
757
1966
|
return `https://${account}.snowflakecomputing.com`;
|
|
758
1967
|
}
|
|
1968
|
+
|
|
1969
|
+
// dist/sql-predicate-schema.js
|
|
1970
|
+
var MAXIMUM_PREDICATE_DEPTH = 64;
|
|
1971
|
+
var MAXIMUM_PREDICATE_NODES = 4096;
|
|
1972
|
+
var SQL_PREDICATE_JSON_SCHEMA = {
|
|
1973
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
1974
|
+
$id: "https://loaders.gl/schemas/sql-predicate.schema.json",
|
|
1975
|
+
title: "loaders.gl SQL predicate",
|
|
1976
|
+
oneOf: [
|
|
1977
|
+
{ $ref: "#/$defs/comparison" },
|
|
1978
|
+
{ $ref: "#/$defs/in" },
|
|
1979
|
+
{ $ref: "#/$defs/isNull" },
|
|
1980
|
+
{ $ref: "#/$defs/logical" },
|
|
1981
|
+
{ $ref: "#/$defs/not" }
|
|
1982
|
+
],
|
|
1983
|
+
$defs: {
|
|
1984
|
+
property: {
|
|
1985
|
+
type: "object",
|
|
1986
|
+
required: ["property"],
|
|
1987
|
+
properties: { property: { type: "string", minLength: 1 } },
|
|
1988
|
+
additionalProperties: false
|
|
1989
|
+
},
|
|
1990
|
+
value: { type: ["boolean", "number", "string"] },
|
|
1991
|
+
comparison: {
|
|
1992
|
+
type: "object",
|
|
1993
|
+
required: ["op", "args"],
|
|
1994
|
+
properties: {
|
|
1995
|
+
op: { enum: ["=", "<>", "<", "<=", ">", ">="] },
|
|
1996
|
+
args: {
|
|
1997
|
+
type: "array",
|
|
1998
|
+
prefixItems: [{ $ref: "#/$defs/property" }, { $ref: "#/$defs/value" }],
|
|
1999
|
+
minItems: 2,
|
|
2000
|
+
maxItems: 2
|
|
2001
|
+
}
|
|
2002
|
+
},
|
|
2003
|
+
additionalProperties: false
|
|
2004
|
+
},
|
|
2005
|
+
in: {
|
|
2006
|
+
type: "object",
|
|
2007
|
+
required: ["op", "args"],
|
|
2008
|
+
properties: {
|
|
2009
|
+
op: { const: "in" },
|
|
2010
|
+
args: {
|
|
2011
|
+
type: "array",
|
|
2012
|
+
prefixItems: [
|
|
2013
|
+
{ $ref: "#/$defs/property" },
|
|
2014
|
+
{ type: "array", items: { $ref: "#/$defs/value" }, minItems: 1 }
|
|
2015
|
+
],
|
|
2016
|
+
minItems: 2,
|
|
2017
|
+
maxItems: 2
|
|
2018
|
+
}
|
|
2019
|
+
},
|
|
2020
|
+
additionalProperties: false
|
|
2021
|
+
},
|
|
2022
|
+
isNull: {
|
|
2023
|
+
type: "object",
|
|
2024
|
+
required: ["op", "args"],
|
|
2025
|
+
properties: {
|
|
2026
|
+
op: { const: "isNull" },
|
|
2027
|
+
args: {
|
|
2028
|
+
type: "array",
|
|
2029
|
+
prefixItems: [{ $ref: "#/$defs/property" }],
|
|
2030
|
+
minItems: 1,
|
|
2031
|
+
maxItems: 1
|
|
2032
|
+
}
|
|
2033
|
+
},
|
|
2034
|
+
additionalProperties: false
|
|
2035
|
+
},
|
|
2036
|
+
logical: {
|
|
2037
|
+
type: "object",
|
|
2038
|
+
required: ["op", "args"],
|
|
2039
|
+
properties: {
|
|
2040
|
+
op: { enum: ["and", "or"] },
|
|
2041
|
+
args: { type: "array", items: { $ref: "#" }, minItems: 2 }
|
|
2042
|
+
},
|
|
2043
|
+
additionalProperties: false
|
|
2044
|
+
},
|
|
2045
|
+
not: {
|
|
2046
|
+
type: "object",
|
|
2047
|
+
required: ["op", "args"],
|
|
2048
|
+
properties: {
|
|
2049
|
+
op: { const: "not" },
|
|
2050
|
+
args: { type: "array", prefixItems: [{ $ref: "#" }], minItems: 1, maxItems: 1 }
|
|
2051
|
+
},
|
|
2052
|
+
additionalProperties: false
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
};
|
|
2056
|
+
function isSQLPredicate(value) {
|
|
2057
|
+
try {
|
|
2058
|
+
validateSQLPredicate(value);
|
|
2059
|
+
return true;
|
|
2060
|
+
} catch {
|
|
2061
|
+
return false;
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
function validateSQLPredicate(value) {
|
|
2065
|
+
const state = { nodes: 0 };
|
|
2066
|
+
validatePredicateNode(value, "$", 0, state);
|
|
2067
|
+
}
|
|
2068
|
+
function validatePredicateNode(value, path, depth, state) {
|
|
2069
|
+
if (depth > MAXIMUM_PREDICATE_DEPTH) {
|
|
2070
|
+
throw new Error(`SQL predicate ${path} exceeds the maximum nesting depth`);
|
|
2071
|
+
}
|
|
2072
|
+
state.nodes++;
|
|
2073
|
+
if (state.nodes > MAXIMUM_PREDICATE_NODES) {
|
|
2074
|
+
throw new Error("SQL predicate exceeds the maximum node count");
|
|
2075
|
+
}
|
|
2076
|
+
if (!isRecord(value) || !hasExactKeys(value, ["op", "args"])) {
|
|
2077
|
+
throw new Error(`SQL predicate ${path} must contain only op and args`);
|
|
2078
|
+
}
|
|
2079
|
+
if (typeof value.op !== "string" || !Array.isArray(value.args)) {
|
|
2080
|
+
throw new Error(`SQL predicate ${path} requires string op and array args`);
|
|
2081
|
+
}
|
|
2082
|
+
switch (value.op) {
|
|
2083
|
+
case "and":
|
|
2084
|
+
case "or":
|
|
2085
|
+
if (value.args.length < 2) {
|
|
2086
|
+
throw new Error(`SQL predicate ${path}.${value.op} requires at least two arguments`);
|
|
2087
|
+
}
|
|
2088
|
+
for (let index = 0; index < value.args.length; index++) {
|
|
2089
|
+
validatePredicateNode(value.args[index], `${path}.args[${index}]`, depth + 1, state);
|
|
2090
|
+
}
|
|
2091
|
+
return;
|
|
2092
|
+
case "not":
|
|
2093
|
+
if (value.args.length !== 1) {
|
|
2094
|
+
throw new Error(`SQL predicate ${path}.not requires exactly one argument`);
|
|
2095
|
+
}
|
|
2096
|
+
validatePredicateNode(value.args[0], `${path}.args[0]`, depth + 1, state);
|
|
2097
|
+
return;
|
|
2098
|
+
case "isNull":
|
|
2099
|
+
if (value.args.length !== 1) {
|
|
2100
|
+
throw new Error(`SQL predicate ${path}.isNull requires exactly one argument`);
|
|
2101
|
+
}
|
|
2102
|
+
validatePredicateProperty(value.args[0], `${path}.args[0]`);
|
|
2103
|
+
return;
|
|
2104
|
+
case "in":
|
|
2105
|
+
if (value.args.length !== 2 || !Array.isArray(value.args[1]) || value.args[1].length === 0) {
|
|
2106
|
+
throw new Error(`SQL predicate ${path}.in requires a property and non-empty value list`);
|
|
2107
|
+
}
|
|
2108
|
+
validatePredicateProperty(value.args[0], `${path}.args[0]`);
|
|
2109
|
+
value.args[1].forEach((item, index) => validatePredicateValue(item, `${path}.args[1][${index}]`));
|
|
2110
|
+
return;
|
|
2111
|
+
case "=":
|
|
2112
|
+
case "<>":
|
|
2113
|
+
case "<":
|
|
2114
|
+
case "<=":
|
|
2115
|
+
case ">":
|
|
2116
|
+
case ">=":
|
|
2117
|
+
if (value.args.length !== 2) {
|
|
2118
|
+
throw new Error(`SQL predicate ${path}.${value.op} requires exactly two arguments`);
|
|
2119
|
+
}
|
|
2120
|
+
validatePredicateProperty(value.args[0], `${path}.args[0]`);
|
|
2121
|
+
validatePredicateValue(value.args[1], `${path}.args[1]`);
|
|
2122
|
+
return;
|
|
2123
|
+
default:
|
|
2124
|
+
throw new Error(`SQL predicate ${path} has unsupported operator ${JSON.stringify(value.op)}`);
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
function validatePredicateProperty(value, path) {
|
|
2128
|
+
if (!isRecord(value) || !hasExactKeys(value, ["property"]) || typeof value.property !== "string" || value.property.length === 0) {
|
|
2129
|
+
throw new Error(`SQL predicate ${path} requires a non-empty property reference`);
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
function validatePredicateValue(value, path) {
|
|
2133
|
+
if (typeof value === "boolean" || typeof value === "bigint" || typeof value === "string" || value instanceof Uint8Array) {
|
|
2134
|
+
return;
|
|
2135
|
+
}
|
|
2136
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
2137
|
+
return;
|
|
2138
|
+
}
|
|
2139
|
+
if (value instanceof Date && Number.isFinite(value.getTime())) {
|
|
2140
|
+
return;
|
|
2141
|
+
}
|
|
2142
|
+
throw new Error(`SQL predicate ${path} contains an unsupported scalar value`);
|
|
2143
|
+
}
|
|
2144
|
+
function isRecord(value) {
|
|
2145
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2146
|
+
}
|
|
2147
|
+
function hasExactKeys(value, expectedKeys) {
|
|
2148
|
+
const keys = Object.keys(value);
|
|
2149
|
+
return keys.length === expectedKeys.length && expectedKeys.every((key) => keys.includes(key));
|
|
2150
|
+
}
|
|
2151
|
+
|
|
2152
|
+
// dist/parse-sql-predicate.js
|
|
2153
|
+
var MAXIMUM_SQL_LENGTH = 65536;
|
|
2154
|
+
var MAXIMUM_SQL_TOKENS = 4096;
|
|
2155
|
+
var MAXIMUM_EXPRESSION_DEPTH = 64;
|
|
2156
|
+
function parseSQLPredicate(source, options = {}) {
|
|
2157
|
+
const parser = new SQLPredicateParser(source, options.parameters ?? {});
|
|
2158
|
+
const predicate = parser.parse();
|
|
2159
|
+
validateSQLPredicate(predicate);
|
|
2160
|
+
return predicate;
|
|
2161
|
+
}
|
|
2162
|
+
var SQLPredicateParser = class {
|
|
2163
|
+
/** Token stream parsed by this parser instance. */
|
|
2164
|
+
tokens;
|
|
2165
|
+
/** Named scalar parameters available to the expression. */
|
|
2166
|
+
parameters;
|
|
2167
|
+
/** Current index in the token stream. */
|
|
2168
|
+
position = 0;
|
|
2169
|
+
constructor(source, parameters) {
|
|
2170
|
+
this.tokens = tokenizeSQLPredicate(source);
|
|
2171
|
+
this.parameters = parameters;
|
|
2172
|
+
}
|
|
2173
|
+
/** Parses the complete token stream. */
|
|
2174
|
+
parse() {
|
|
2175
|
+
const predicate = this.parseOr(0);
|
|
2176
|
+
this.takeSymbol(";");
|
|
2177
|
+
const token = this.peek();
|
|
2178
|
+
if (token) {
|
|
2179
|
+
throw new Error(`SQL predicate contains unexpected token ${JSON.stringify(token.value)}`);
|
|
2180
|
+
}
|
|
2181
|
+
return predicate;
|
|
2182
|
+
}
|
|
2183
|
+
/** Parses left-associative disjunction. */
|
|
2184
|
+
parseOr(depth) {
|
|
2185
|
+
let predicate = this.parseAnd(depth + 1);
|
|
2186
|
+
while (this.takeKeyword("OR")) {
|
|
2187
|
+
predicate = combineLogicalPredicate("or", predicate, this.parseAnd(depth + 1));
|
|
2188
|
+
}
|
|
2189
|
+
return predicate;
|
|
2190
|
+
}
|
|
2191
|
+
/** Parses left-associative conjunction. */
|
|
2192
|
+
parseAnd(depth) {
|
|
2193
|
+
let predicate = this.parseNot(depth + 1);
|
|
2194
|
+
while (this.takeKeyword("AND")) {
|
|
2195
|
+
predicate = combineLogicalPredicate("and", predicate, this.parseNot(depth + 1));
|
|
2196
|
+
}
|
|
2197
|
+
return predicate;
|
|
2198
|
+
}
|
|
2199
|
+
/** Parses boolean negation and parenthesized predicates. */
|
|
2200
|
+
parseNot(depth) {
|
|
2201
|
+
this.assertDepth(depth);
|
|
2202
|
+
if (this.takeKeyword("NOT")) {
|
|
2203
|
+
return { op: "not", args: [this.parseNot(depth + 1)] };
|
|
2204
|
+
}
|
|
2205
|
+
if (this.takeSymbol("(")) {
|
|
2206
|
+
const predicate = this.parseOr(depth + 1);
|
|
2207
|
+
this.expectSymbol(")");
|
|
2208
|
+
return predicate;
|
|
2209
|
+
}
|
|
2210
|
+
return this.parseLeaf();
|
|
2211
|
+
}
|
|
2212
|
+
/** Parses one comparison, membership, or null predicate. */
|
|
2213
|
+
parseLeaf() {
|
|
2214
|
+
var _a, _b;
|
|
2215
|
+
const property = this.parseProperty();
|
|
2216
|
+
if (this.takeKeyword("IS")) {
|
|
2217
|
+
const negated = this.takeKeyword("NOT");
|
|
2218
|
+
this.expectKeyword("NULL");
|
|
2219
|
+
const predicate = { op: "isNull", args: [property] };
|
|
2220
|
+
return negated ? { op: "not", args: [predicate] } : predicate;
|
|
2221
|
+
}
|
|
2222
|
+
const negatedMembership = this.takeKeyword("NOT");
|
|
2223
|
+
if (negatedMembership || this.peekKeyword("IN")) {
|
|
2224
|
+
this.expectKeyword("IN");
|
|
2225
|
+
this.expectSymbol("(");
|
|
2226
|
+
if (((_a = this.peek()) == null ? void 0 : _a.kind) === "symbol" && ((_b = this.peek()) == null ? void 0 : _b.value) === ")") {
|
|
2227
|
+
throw new Error("SQL predicate IN requires at least one scalar value");
|
|
2228
|
+
}
|
|
2229
|
+
const values = [this.parseValue()];
|
|
2230
|
+
while (this.takeSymbol(",")) {
|
|
2231
|
+
values.push(this.parseValue());
|
|
2232
|
+
}
|
|
2233
|
+
this.expectSymbol(")");
|
|
2234
|
+
const predicate = { op: "in", args: [property, values] };
|
|
2235
|
+
return negatedMembership ? { op: "not", args: [predicate] } : predicate;
|
|
2236
|
+
}
|
|
2237
|
+
const token = this.take();
|
|
2238
|
+
if ((token == null ? void 0 : token.kind) !== "symbol" || !["=", "!=", "<>", "<", "<=", ">", ">="].includes(token.value)) {
|
|
2239
|
+
throw new Error("SQL predicate expected a comparison, IN, or IS NULL operator");
|
|
2240
|
+
}
|
|
2241
|
+
const operator = token.value === "!=" ? "<>" : token.value;
|
|
2242
|
+
return {
|
|
2243
|
+
op: operator,
|
|
2244
|
+
args: [property, this.parseValue()]
|
|
2245
|
+
};
|
|
2246
|
+
}
|
|
2247
|
+
/** Parses an identifier into a property reference. */
|
|
2248
|
+
parseProperty() {
|
|
2249
|
+
const token = this.take();
|
|
2250
|
+
if ((token == null ? void 0 : token.kind) !== "identifier") {
|
|
2251
|
+
throw new Error("SQL predicate expected a column identifier");
|
|
2252
|
+
}
|
|
2253
|
+
return { property: token.value };
|
|
2254
|
+
}
|
|
2255
|
+
/** Parses one scalar literal or named parameter. */
|
|
2256
|
+
parseValue() {
|
|
2257
|
+
const negative = this.takeSymbol("-");
|
|
2258
|
+
const token = this.take();
|
|
2259
|
+
if (!token) {
|
|
2260
|
+
throw new Error("SQL predicate expected a scalar value");
|
|
2261
|
+
}
|
|
2262
|
+
if (negative && token.kind !== "number") {
|
|
2263
|
+
throw new Error("SQL predicate only permits unary minus on numeric literals");
|
|
2264
|
+
}
|
|
2265
|
+
if (token.kind === "number") {
|
|
2266
|
+
return parseNumericLiteral(negative ? `-${token.value}` : token.value);
|
|
2267
|
+
}
|
|
2268
|
+
if (negative) {
|
|
2269
|
+
throw new Error("SQL predicate expected a numeric literal after unary minus");
|
|
2270
|
+
}
|
|
2271
|
+
if (token.kind === "string") {
|
|
2272
|
+
return token.value;
|
|
2273
|
+
}
|
|
2274
|
+
if (token.kind === "parameter") {
|
|
2275
|
+
if (!Object.hasOwn(this.parameters, token.value)) {
|
|
2276
|
+
throw new Error(`SQL predicate parameter ":${token.value}" requires a value`);
|
|
2277
|
+
}
|
|
2278
|
+
const value = this.parameters[token.value];
|
|
2279
|
+
validateParameterValue(value, token.value);
|
|
2280
|
+
return value;
|
|
2281
|
+
}
|
|
2282
|
+
if (token.kind === "identifier" && /^(TRUE|FALSE)$/i.test(token.value)) {
|
|
2283
|
+
return /^TRUE$/i.test(token.value);
|
|
2284
|
+
}
|
|
2285
|
+
if (token.kind === "identifier" && /^NULL$/i.test(token.value)) {
|
|
2286
|
+
throw new Error("SQL predicate comparisons with NULL must use IS NULL or IS NOT NULL");
|
|
2287
|
+
}
|
|
2288
|
+
throw new Error(`SQL predicate does not support scalar token ${JSON.stringify(token.value)}`);
|
|
2289
|
+
}
|
|
2290
|
+
/** Rejects excessively nested inputs before recursion can exhaust the stack. */
|
|
2291
|
+
assertDepth(depth) {
|
|
2292
|
+
if (depth > MAXIMUM_EXPRESSION_DEPTH) {
|
|
2293
|
+
throw new Error("SQL predicate nesting exceeds its safe limit");
|
|
2294
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
/** Consumes one required keyword. */
|
|
2297
|
+
expectKeyword(keyword) {
|
|
2298
|
+
if (!this.takeKeyword(keyword)) {
|
|
2299
|
+
throw new Error(`SQL predicate expected ${keyword}`);
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
/** Returns whether the next token is a keyword without consuming it. */
|
|
2303
|
+
peekKeyword(keyword) {
|
|
2304
|
+
const token = this.peek();
|
|
2305
|
+
return (token == null ? void 0 : token.kind) === "identifier" && token.value.toUpperCase() === keyword;
|
|
2306
|
+
}
|
|
2307
|
+
/** Consumes one optional keyword. */
|
|
2308
|
+
takeKeyword(keyword) {
|
|
2309
|
+
if (!this.peekKeyword(keyword)) {
|
|
2310
|
+
return false;
|
|
2311
|
+
}
|
|
2312
|
+
this.position++;
|
|
2313
|
+
return true;
|
|
2314
|
+
}
|
|
2315
|
+
/** Consumes one required symbol. */
|
|
2316
|
+
expectSymbol(symbol) {
|
|
2317
|
+
if (!this.takeSymbol(symbol)) {
|
|
2318
|
+
throw new Error(`SQL predicate expected ${JSON.stringify(symbol)}`);
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2321
|
+
/** Consumes one optional symbol. */
|
|
2322
|
+
takeSymbol(symbol) {
|
|
2323
|
+
const token = this.peek();
|
|
2324
|
+
if ((token == null ? void 0 : token.kind) !== "symbol" || token.value !== symbol) {
|
|
2325
|
+
return false;
|
|
2326
|
+
}
|
|
2327
|
+
this.position++;
|
|
2328
|
+
return true;
|
|
2329
|
+
}
|
|
2330
|
+
/** Consumes and returns the next token. */
|
|
2331
|
+
take() {
|
|
2332
|
+
const token = this.tokens[this.position];
|
|
2333
|
+
if (token) {
|
|
2334
|
+
this.position++;
|
|
2335
|
+
}
|
|
2336
|
+
return token;
|
|
2337
|
+
}
|
|
2338
|
+
/** Returns the next token without consuming it. */
|
|
2339
|
+
peek() {
|
|
2340
|
+
return this.tokens[this.position];
|
|
2341
|
+
}
|
|
2342
|
+
};
|
|
2343
|
+
function combineLogicalPredicate(operator, left, right) {
|
|
2344
|
+
return left.op === operator ? { op: operator, args: [...left.args, right] } : { op: operator, args: [left, right] };
|
|
2345
|
+
}
|
|
2346
|
+
function parseNumericLiteral(source) {
|
|
2347
|
+
if (/^-?\d+$/.test(source)) {
|
|
2348
|
+
const bigint = BigInt(source);
|
|
2349
|
+
if (bigint > BigInt(Number.MAX_SAFE_INTEGER) || bigint < BigInt(Number.MIN_SAFE_INTEGER)) {
|
|
2350
|
+
return bigint;
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2353
|
+
const number = Number(source);
|
|
2354
|
+
if (!Number.isFinite(number)) {
|
|
2355
|
+
throw new Error("SQL predicate numeric literals must be finite");
|
|
2356
|
+
}
|
|
2357
|
+
return number;
|
|
2358
|
+
}
|
|
2359
|
+
function validateParameterValue(value, name) {
|
|
2360
|
+
const probe = { op: "=", args: [{ property: "_" }, value] };
|
|
2361
|
+
try {
|
|
2362
|
+
validateSQLPredicate(probe);
|
|
2363
|
+
} catch {
|
|
2364
|
+
throw new Error(`SQL predicate parameter ":${name}" has an unsupported value`);
|
|
2365
|
+
}
|
|
2366
|
+
}
|
|
2367
|
+
function tokenizeSQLPredicate(source) {
|
|
2368
|
+
if (typeof source !== "string" || source.trim().length === 0 || source.length > MAXIMUM_SQL_LENGTH) {
|
|
2369
|
+
throw new Error("SQL predicate requires a non-empty, safely bounded expression");
|
|
2370
|
+
}
|
|
2371
|
+
const tokens = [];
|
|
2372
|
+
let position = 0;
|
|
2373
|
+
while (position < source.length) {
|
|
2374
|
+
const character = source[position];
|
|
2375
|
+
if (/\s/.test(character)) {
|
|
2376
|
+
position++;
|
|
2377
|
+
continue;
|
|
2378
|
+
}
|
|
2379
|
+
if (character === "'") {
|
|
2380
|
+
const result = readQuotedToken(source, position, "'", "'");
|
|
2381
|
+
tokens.push({ kind: "string", value: result.value });
|
|
2382
|
+
position = result.position;
|
|
2383
|
+
} else if (character === '"') {
|
|
2384
|
+
const result = readQuotedToken(source, position, '"', '"');
|
|
2385
|
+
tokens.push({ kind: "identifier", value: result.value });
|
|
2386
|
+
position = result.position;
|
|
2387
|
+
} else if (character === ":") {
|
|
2388
|
+
const match = /^[A-Za-z_][A-Za-z0-9_]*/.exec(source.slice(position + 1));
|
|
2389
|
+
if (!match) {
|
|
2390
|
+
throw new Error(`SQL predicate contains an invalid parameter at character ${position}`);
|
|
2391
|
+
}
|
|
2392
|
+
tokens.push({ kind: "parameter", value: match[0] });
|
|
2393
|
+
position += match[0].length + 1;
|
|
2394
|
+
} else {
|
|
2395
|
+
const numberMatch = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(source.slice(position));
|
|
2396
|
+
const identifierMatch = /^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*/.exec(source.slice(position));
|
|
2397
|
+
if (numberMatch) {
|
|
2398
|
+
tokens.push({ kind: "number", value: numberMatch[0] });
|
|
2399
|
+
position += numberMatch[0].length;
|
|
2400
|
+
} else if (identifierMatch) {
|
|
2401
|
+
tokens.push({ kind: "identifier", value: identifierMatch[0] });
|
|
2402
|
+
position += identifierMatch[0].length;
|
|
2403
|
+
} else {
|
|
2404
|
+
const twoCharacters = source.slice(position, position + 2);
|
|
2405
|
+
if (["<=", ">=", "<>", "!="].includes(twoCharacters)) {
|
|
2406
|
+
tokens.push({ kind: "symbol", value: twoCharacters });
|
|
2407
|
+
position += 2;
|
|
2408
|
+
} else if ("(),;-=<>".includes(character)) {
|
|
2409
|
+
tokens.push({ kind: "symbol", value: character });
|
|
2410
|
+
position++;
|
|
2411
|
+
} else {
|
|
2412
|
+
throw new Error(`SQL predicate contains an unsupported token at character ${position}`);
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
}
|
|
2416
|
+
if (tokens.length > MAXIMUM_SQL_TOKENS) {
|
|
2417
|
+
throw new Error("SQL predicate exceeds its safe token limit");
|
|
2418
|
+
}
|
|
2419
|
+
}
|
|
2420
|
+
return tokens;
|
|
2421
|
+
}
|
|
2422
|
+
function readQuotedToken(source, start, quote, escapedQuote) {
|
|
2423
|
+
let value = "";
|
|
2424
|
+
let position = start + 1;
|
|
2425
|
+
while (position < source.length) {
|
|
2426
|
+
if (source[position] !== quote) {
|
|
2427
|
+
value += source[position++];
|
|
2428
|
+
continue;
|
|
2429
|
+
}
|
|
2430
|
+
if (source[position + 1] === escapedQuote) {
|
|
2431
|
+
value += quote;
|
|
2432
|
+
position += 2;
|
|
2433
|
+
continue;
|
|
2434
|
+
}
|
|
2435
|
+
return { value, position: position + 1 };
|
|
2436
|
+
}
|
|
2437
|
+
throw new Error(`SQL predicate contains an unterminated quoted value at character ${start}`);
|
|
2438
|
+
}
|
|
759
2439
|
//# sourceMappingURL=index.cjs.map
|