@nshiab/simple-data-analysis-core 2.0.5 → 2.0.6
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/README.md +118 -12
- package/esm/class/Simple.d.ts +8 -2
- package/esm/class/Simple.d.ts.map +1 -1
- package/esm/class/Simple.js +9 -3
- package/esm/class/SimpleDB.d.ts +72 -1
- package/esm/class/SimpleDB.d.ts.map +1 -1
- package/esm/class/SimpleDB.js +81 -1
- package/esm/class/SimpleTable.d.ts +251 -274
- package/esm/class/SimpleTable.d.ts.map +1 -1
- package/esm/class/SimpleTable.js +248 -325
- package/esm/helpers/LogValue.d.ts +8 -0
- package/esm/helpers/LogValue.d.ts.map +1 -0
- package/esm/helpers/LogValue.js +23 -0
- package/esm/helpers/cacheTableDependencies.d.ts +9 -0
- package/esm/helpers/cacheTableDependencies.d.ts.map +1 -1
- package/esm/helpers/cacheTableDependencies.js +131 -21
- package/esm/helpers/cleanSQL.d.ts +1 -1
- package/esm/helpers/cleanSQL.d.ts.map +1 -1
- package/esm/helpers/cleanSQL.js +268 -23
- package/esm/helpers/flushAllTables.d.ts.map +1 -1
- package/esm/helpers/flushAllTables.js +23 -28
- package/esm/helpers/foldIdentifier.d.ts +3 -0
- package/esm/helpers/foldIdentifier.d.ts.map +1 -0
- package/esm/helpers/foldIdentifier.js +4 -0
- package/esm/helpers/geometryFromJSON.d.ts +3 -0
- package/esm/helpers/geometryFromJSON.d.ts.map +1 -0
- package/esm/helpers/geometryFromJSON.js +4 -0
- package/esm/helpers/index.d.ts +1 -0
- package/esm/helpers/index.d.ts.map +1 -1
- package/esm/helpers/index.js +1 -0
- package/esm/helpers/logData.d.ts.map +1 -1
- package/esm/helpers/logData.js +45 -44
- package/esm/helpers/makeLogConverter.d.ts +5 -0
- package/esm/helpers/makeLogConverter.d.ts.map +1 -0
- package/esm/helpers/makeLogConverter.js +64 -0
- package/esm/helpers/osmFiles.d.ts.map +1 -1
- package/esm/helpers/parseTypes.d.ts +1 -1
- package/esm/helpers/parseTypes.d.ts.map +1 -1
- package/esm/helpers/parseTypes.js +4 -0
- package/esm/helpers/pendingOps.d.ts +4 -2
- package/esm/helpers/pendingOps.d.ts.map +1 -1
- package/esm/helpers/prepareGeometry.d.ts +3 -0
- package/esm/helpers/prepareGeometry.d.ts.map +1 -0
- package/esm/helpers/prepareGeometry.js +99 -0
- package/esm/helpers/prepareJSON.d.ts +3 -0
- package/esm/helpers/prepareJSON.d.ts.map +1 -0
- package/esm/helpers/prepareJSON.js +50 -0
- package/esm/helpers/printTable.d.ts +5 -4
- package/esm/helpers/printTable.d.ts.map +1 -1
- package/esm/helpers/printTable.js +32 -36
- package/esm/helpers/queryDB.js +1 -1
- package/esm/helpers/queueAsyncBarrier.d.ts +4 -0
- package/esm/helpers/queueAsyncBarrier.d.ts.map +1 -1
- package/esm/helpers/queueAsyncBarrier.js +4 -0
- package/esm/helpers/queueOp.d.ts.map +1 -1
- package/esm/helpers/queueOp.js +5 -3
- package/esm/helpers/readMutationRows.d.ts +9 -0
- package/esm/helpers/readMutationRows.d.ts.map +1 -0
- package/esm/helpers/readMutationRows.js +94 -0
- package/esm/helpers/referencedTables.d.ts +9 -0
- package/esm/helpers/referencedTables.d.ts.map +1 -0
- package/esm/helpers/referencedTables.js +22 -0
- package/esm/helpers/runQuery.d.ts.map +1 -1
- package/esm/helpers/runQuery.js +11 -13
- package/esm/helpers/scanSQL.d.ts +7 -0
- package/esm/helpers/scanSQL.d.ts.map +1 -0
- package/esm/helpers/scanSQL.js +71 -0
- package/esm/helpers/tableRegistry.d.ts +2 -0
- package/esm/helpers/tableRegistry.d.ts.map +1 -1
- package/esm/helpers/tableRegistry.js +12 -4
- package/esm/helpers/updateColumnsWithJS.d.ts +52 -0
- package/esm/helpers/updateColumnsWithJS.d.ts.map +1 -0
- package/esm/helpers/updateColumnsWithJS.js +139 -0
- package/esm/methods/addColumn.d.ts +1 -1
- package/esm/methods/addColumn.d.ts.map +1 -1
- package/esm/methods/bm25.d.ts.map +1 -1
- package/esm/methods/bm25.js +1 -2
- package/esm/methods/cache.d.ts.map +1 -1
- package/esm/methods/cache.js +4 -6
- package/esm/methods/convert.d.ts +2 -2
- package/esm/methods/convert.d.ts.map +1 -1
- package/esm/methods/convert.js +5 -0
- package/esm/methods/getBottom.d.ts +1 -1
- package/esm/methods/getBottom.d.ts.map +1 -1
- package/esm/methods/getBottom.js +2 -2
- package/esm/methods/getData.d.ts +1 -1
- package/esm/methods/getData.d.ts.map +1 -1
- package/esm/methods/getData.js +2 -2
- package/esm/methods/join.d.ts.map +1 -1
- package/esm/methods/join.js +8 -16
- package/esm/methods/joinGeo.d.ts.map +1 -1
- package/esm/methods/joinGeo.js +10 -19
- package/esm/methods/loadArray.d.ts +1 -1
- package/esm/methods/loadArray.d.ts.map +1 -1
- package/esm/methods/loadArray.js +74 -19
- package/esm/methods/loadData.js +3 -0
- package/esm/methods/log.d.ts +7 -0
- package/esm/methods/log.d.ts.map +1 -0
- package/esm/methods/log.js +58 -0
- package/esm/methods/logBottom.d.ts +5 -0
- package/esm/methods/logBottom.d.ts.map +1 -0
- package/esm/methods/logBottom.js +9 -0
- package/esm/methods/removeTable.d.ts.map +1 -1
- package/esm/methods/removeTable.js +2 -0
- package/esm/methods/removeTables.d.ts.map +1 -1
- package/esm/methods/removeTables.js +7 -0
- package/esm/methods/renameTable.d.ts.map +1 -1
- package/esm/methods/renameTable.js +4 -0
- package/esm/methods/rowToText.js +2 -2
- package/esm/methods/selectTables.d.ts.map +1 -1
- package/esm/methods/selectTables.js +6 -0
- package/esm/methods/setTypes.d.ts +1 -1
- package/esm/methods/setTypes.d.ts.map +1 -1
- package/esm/methods/stream.d.ts.map +1 -1
- package/esm/methods/stream.js +2 -1
- package/esm/methods/updateWithJS.d.ts +1 -3
- package/esm/methods/updateWithJS.d.ts.map +1 -1
- package/esm/methods/updateWithJS.js +123 -37
- package/llm.md +337 -444
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/script/class/Simple.d.ts +8 -2
- package/script/class/Simple.d.ts.map +1 -1
- package/script/class/Simple.js +9 -3
- package/script/class/SimpleDB.d.ts +72 -1
- package/script/class/SimpleDB.d.ts.map +1 -1
- package/script/class/SimpleDB.js +81 -1
- package/script/class/SimpleTable.d.ts +251 -274
- package/script/class/SimpleTable.d.ts.map +1 -1
- package/script/class/SimpleTable.js +248 -325
- package/script/helpers/LogValue.d.ts +8 -0
- package/script/helpers/LogValue.d.ts.map +1 -0
- package/script/helpers/LogValue.js +26 -0
- package/script/helpers/cacheTableDependencies.d.ts +9 -0
- package/script/helpers/cacheTableDependencies.d.ts.map +1 -1
- package/script/helpers/cacheTableDependencies.js +170 -20
- package/script/helpers/cleanSQL.d.ts +1 -1
- package/script/helpers/cleanSQL.d.ts.map +1 -1
- package/script/helpers/cleanSQL.js +271 -23
- package/script/helpers/flushAllTables.d.ts.map +1 -1
- package/script/helpers/flushAllTables.js +24 -29
- package/script/helpers/foldIdentifier.d.ts +3 -0
- package/script/helpers/foldIdentifier.d.ts.map +1 -0
- package/script/helpers/foldIdentifier.js +7 -0
- package/script/helpers/geometryFromJSON.d.ts +3 -0
- package/script/helpers/geometryFromJSON.d.ts.map +1 -0
- package/script/helpers/geometryFromJSON.js +7 -0
- package/script/helpers/index.d.ts +1 -0
- package/script/helpers/index.d.ts.map +1 -1
- package/script/helpers/index.js +3 -1
- package/script/helpers/logData.d.ts.map +1 -1
- package/script/helpers/logData.js +45 -44
- package/script/helpers/makeLogConverter.d.ts +5 -0
- package/script/helpers/makeLogConverter.d.ts.map +1 -0
- package/script/helpers/makeLogConverter.js +70 -0
- package/script/helpers/osmFiles.d.ts.map +1 -1
- package/script/helpers/parseTypes.d.ts +1 -1
- package/script/helpers/parseTypes.d.ts.map +1 -1
- package/script/helpers/parseTypes.js +4 -0
- package/script/helpers/pendingOps.d.ts +4 -2
- package/script/helpers/pendingOps.d.ts.map +1 -1
- package/script/helpers/prepareGeometry.d.ts +3 -0
- package/script/helpers/prepareGeometry.d.ts.map +1 -0
- package/script/helpers/prepareGeometry.js +102 -0
- package/script/helpers/prepareJSON.d.ts +3 -0
- package/script/helpers/prepareJSON.d.ts.map +1 -0
- package/script/helpers/prepareJSON.js +53 -0
- package/script/helpers/printTable.d.ts +5 -4
- package/script/helpers/printTable.d.ts.map +1 -1
- package/script/helpers/printTable.js +32 -36
- package/script/helpers/queryDB.js +1 -1
- package/script/helpers/queueAsyncBarrier.d.ts +4 -0
- package/script/helpers/queueAsyncBarrier.d.ts.map +1 -1
- package/script/helpers/queueAsyncBarrier.js +4 -0
- package/script/helpers/queueOp.d.ts.map +1 -1
- package/script/helpers/queueOp.js +4 -2
- package/script/helpers/readMutationRows.d.ts +9 -0
- package/script/helpers/readMutationRows.d.ts.map +1 -0
- package/script/helpers/readMutationRows.js +97 -0
- package/script/helpers/referencedTables.d.ts +9 -0
- package/script/helpers/referencedTables.d.ts.map +1 -0
- package/script/helpers/referencedTables.js +28 -0
- package/script/helpers/runQuery.d.ts.map +1 -1
- package/script/helpers/runQuery.js +11 -13
- package/script/helpers/scanSQL.d.ts +7 -0
- package/script/helpers/scanSQL.d.ts.map +1 -0
- package/script/helpers/scanSQL.js +74 -0
- package/script/helpers/tableRegistry.d.ts +2 -0
- package/script/helpers/tableRegistry.d.ts.map +1 -1
- package/script/helpers/tableRegistry.js +16 -4
- package/script/helpers/updateColumnsWithJS.d.ts +52 -0
- package/script/helpers/updateColumnsWithJS.d.ts.map +1 -0
- package/script/helpers/updateColumnsWithJS.js +145 -0
- package/script/methods/addColumn.d.ts +1 -1
- package/script/methods/addColumn.d.ts.map +1 -1
- package/script/methods/bm25.d.ts.map +1 -1
- package/script/methods/bm25.js +1 -2
- package/script/methods/cache.d.ts.map +1 -1
- package/script/methods/cache.js +3 -5
- package/script/methods/convert.d.ts +2 -2
- package/script/methods/convert.d.ts.map +1 -1
- package/script/methods/convert.js +5 -0
- package/script/methods/getBottom.d.ts +1 -1
- package/script/methods/getBottom.d.ts.map +1 -1
- package/script/methods/getBottom.js +2 -2
- package/script/methods/getData.d.ts +1 -1
- package/script/methods/getData.d.ts.map +1 -1
- package/script/methods/getData.js +2 -2
- package/script/methods/join.d.ts.map +1 -1
- package/script/methods/join.js +8 -16
- package/script/methods/joinGeo.d.ts.map +1 -1
- package/script/methods/joinGeo.js +10 -19
- package/script/methods/loadArray.d.ts +1 -1
- package/script/methods/loadArray.d.ts.map +1 -1
- package/script/methods/loadArray.js +74 -19
- package/script/methods/loadData.js +3 -0
- package/script/methods/log.d.ts +7 -0
- package/script/methods/log.d.ts.map +1 -0
- package/script/methods/log.js +64 -0
- package/script/methods/logBottom.d.ts +5 -0
- package/script/methods/logBottom.d.ts.map +1 -0
- package/script/methods/logBottom.js +15 -0
- package/script/methods/removeTable.d.ts.map +1 -1
- package/script/methods/removeTable.js +2 -0
- package/script/methods/removeTables.d.ts.map +1 -1
- package/script/methods/removeTables.js +7 -0
- package/script/methods/renameTable.d.ts.map +1 -1
- package/script/methods/renameTable.js +4 -0
- package/script/methods/rowToText.js +2 -2
- package/script/methods/selectTables.d.ts.map +1 -1
- package/script/methods/selectTables.js +6 -0
- package/script/methods/setTypes.d.ts +1 -1
- package/script/methods/setTypes.d.ts.map +1 -1
- package/script/methods/stream.d.ts.map +1 -1
- package/script/methods/stream.js +2 -1
- package/script/methods/updateWithJS.d.ts +1 -3
- package/script/methods/updateWithJS.d.ts.map +1 -1
- package/script/methods/updateWithJS.js +122 -36
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import quoteIdentifier from "./quoteIdentifier.js";
|
|
2
|
+
import readMutationRows from "./readMutationRows.js";
|
|
3
|
+
import { retainRegisteredTables } from "./tableRegistry.js";
|
|
4
|
+
import { executePreparedArray, prepareArray } from "../methods/loadArray.js";
|
|
5
|
+
/**
|
|
6
|
+
* Generates columns from selected input columns, keeping other SQL values in
|
|
7
|
+
* DuckDB. Call from an asynchronous extension barrier. The callback must return
|
|
8
|
+
* one row per input row in the same order; only the declared output columns are
|
|
9
|
+
* stored. Existing output columns are replaced, with types inferred from the
|
|
10
|
+
* generated values. Generation runs sequentially in bounded batches (1,000 rows
|
|
11
|
+
* by default); callbacks must not depend on receiving the whole table. Non-null
|
|
12
|
+
* output types must agree across batches. All-null batches defer type inference.
|
|
13
|
+
* Only one batch of inputs and outputs is retained by this helper in JavaScript;
|
|
14
|
+
* callers must also bound their own buffering. Reads finish before callbacks or
|
|
15
|
+
* writes use the shared connection. The original table is replaced only after
|
|
16
|
+
* staging succeeds. Empty tables are unchanged and do not invoke the callback;
|
|
17
|
+
* outputs that remain null in every batch use VARCHAR.
|
|
18
|
+
*
|
|
19
|
+
* Untouched geometry columns remain in DuckDB, preserving their values, types
|
|
20
|
+
* and CRS without GeoJSON conversion. Geometry columns cannot be selected as
|
|
21
|
+
* inputs or overwritten as outputs; use updateWithJS() for editable GeoJSON.
|
|
22
|
+
* This SQL-only preservation path accepts any source CRS.
|
|
23
|
+
*
|
|
24
|
+
* @param table - Table to enrich.
|
|
25
|
+
* @param inputColumns - Columns available to the callback.
|
|
26
|
+
* @param outputColumns - Generated columns to add or replace.
|
|
27
|
+
* @param generate - Produces one result per input row, in input order.
|
|
28
|
+
* @param options - Controls the maximum rows read, generated, and staged at once.
|
|
29
|
+
* @returns Resolves after merging the generated columns into the table.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* queueAsyncBarrier(table, {
|
|
33
|
+
* method: "label()",
|
|
34
|
+
* parameters: null,
|
|
35
|
+
* execute: () => updateColumnsWithJS(table, ["name"], ["label"], async (rows) =>
|
|
36
|
+
* rows.map((row) => ({ label: String(row.name).toUpperCase() })),
|
|
37
|
+
* { batchSize: 500 }),
|
|
38
|
+
* });
|
|
39
|
+
* await table.log();
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export default async function updateColumnsWithJS(table, inputColumns, outputColumns, generate, options = {}) {
|
|
43
|
+
const batchSize = options.batchSize ?? 1000;
|
|
44
|
+
if (!Number.isSafeInteger(batchSize) || batchSize < 1) {
|
|
45
|
+
throw new Error("batchSize must be a positive safe integer.");
|
|
46
|
+
}
|
|
47
|
+
if (inputColumns.length === 0 || outputColumns.length === 0) {
|
|
48
|
+
throw new Error("Input and output columns must not be empty.");
|
|
49
|
+
}
|
|
50
|
+
if (new Set(outputColumns.map((name) => name.toLowerCase())).size !==
|
|
51
|
+
outputColumns.length) {
|
|
52
|
+
throw new Error("Output columns must be unique.");
|
|
53
|
+
}
|
|
54
|
+
const types = await table.getTypes();
|
|
55
|
+
for (const [name, type] of Object.entries(types)) {
|
|
56
|
+
if (type.toUpperCase().startsWith("GEOMETRY") &&
|
|
57
|
+
[...inputColumns, ...outputColumns].some((column) => column.toLowerCase() === name.toLowerCase())) {
|
|
58
|
+
throw new Error(`Geometry column ${JSON.stringify(name)} cannot be an input or output of updateColumnsWithJS(); use updateWithJS() for editable GeoJSON.`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const suffix = crypto.randomUUID().replaceAll("-", "");
|
|
62
|
+
const id = `__sda_id_${suffix}`;
|
|
63
|
+
const snapshot = `__sda_source_${suffix}`;
|
|
64
|
+
const staged = table.sdb.newTable(`__sda_generated_${suffix}`);
|
|
65
|
+
const batch = table.sdb.newTable(`__sda_batch_${suffix}`);
|
|
66
|
+
const q = quoteIdentifier;
|
|
67
|
+
try {
|
|
68
|
+
// A SQL snapshot gives every row a stable identity, including duplicate rows
|
|
69
|
+
// and user columns named rowid. No untouched values cross the JS boundary.
|
|
70
|
+
await table.sdb.customQuery(`CREATE TEMP TABLE ${q(snapshot)} AS SELECT *, row_number() OVER () AS ${q(id)} FROM ${q(table.name)}`);
|
|
71
|
+
const knownTypes = new Map();
|
|
72
|
+
let lastId = "0";
|
|
73
|
+
let hasRows = false;
|
|
74
|
+
while (true) {
|
|
75
|
+
const upperId = BigInt(lastId) + BigInt(batchSize);
|
|
76
|
+
const { rows } = await readMutationRows(table.connection, `SELECT ${q(id)}, ${inputColumns.map(q).join(", ")} FROM ${q(snapshot)} WHERE ${q(id)} > ${lastId} AND ${q(id)} <= ${upperId}
|
|
77
|
+
ORDER BY ${q(id)} LIMIT ${batchSize}`);
|
|
78
|
+
if (rows.length === 0)
|
|
79
|
+
break;
|
|
80
|
+
const ids = rows.map((row) => String(row[id]));
|
|
81
|
+
lastId = ids[ids.length - 1];
|
|
82
|
+
for (const row of rows)
|
|
83
|
+
delete row[id];
|
|
84
|
+
const generated = await generate(rows);
|
|
85
|
+
if (generated.length !== ids.length) {
|
|
86
|
+
throw new Error("Column generation must return one row per input row.");
|
|
87
|
+
}
|
|
88
|
+
// Generated vectors retain this helper's existing inference contract.
|
|
89
|
+
await executePreparedArray(batch, prepareArray(generated.map((row, i) => ({
|
|
90
|
+
...Object.fromEntries(outputColumns.map((name) => [name, row[name]])),
|
|
91
|
+
[id]: ids[i],
|
|
92
|
+
}))));
|
|
93
|
+
const batchTypes = await batch.getTypes();
|
|
94
|
+
for (const name of outputColumns) {
|
|
95
|
+
const nonNull = generated.some((row) => row[name] !== null && row[name] !== undefined);
|
|
96
|
+
if (!nonNull)
|
|
97
|
+
continue;
|
|
98
|
+
const previous = knownTypes.get(name);
|
|
99
|
+
const current = batchTypes[name];
|
|
100
|
+
if (previous !== undefined && previous !== current) {
|
|
101
|
+
throw new Error(`Generated column ${JSON.stringify(name)} changed type from ${previous} to ${current}.`);
|
|
102
|
+
}
|
|
103
|
+
if (previous === undefined) {
|
|
104
|
+
knownTypes.set(name, current);
|
|
105
|
+
if (hasRows) {
|
|
106
|
+
await table.sdb.customQuery(`ALTER TABLE ${q(staged.name)} ALTER COLUMN ${q(name)} TYPE ${current} USING NULL`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (!hasRows) {
|
|
111
|
+
await table.sdb.customQuery(`CREATE TEMP TABLE ${q(staged.name)} AS SELECT * FROM ${q(batch.name)}`);
|
|
112
|
+
hasRows = true;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
await table.sdb.customQuery(`INSERT INTO ${q(staged.name)} SELECT * FROM ${q(batch.name)}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (!hasRows)
|
|
119
|
+
return;
|
|
120
|
+
const outputs = new Map(outputColumns.map((name) => [name.toLowerCase(), name]));
|
|
121
|
+
const columns = Object.keys(types).map((name) => {
|
|
122
|
+
const replacement = outputs.get(name.toLowerCase());
|
|
123
|
+
return replacement === undefined
|
|
124
|
+
? `original.${q(name)}`
|
|
125
|
+
: `generated.${q(replacement)} AS ${q(name)}`;
|
|
126
|
+
});
|
|
127
|
+
const existing = new Set(Object.keys(types).map((name) => name.toLowerCase()));
|
|
128
|
+
for (const name of outputColumns) {
|
|
129
|
+
if (!existing.has(name.toLowerCase())) {
|
|
130
|
+
columns.push(`generated.${q(name)}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
await table.sdb.customQuery(`CREATE OR REPLACE TABLE ${q(table.name)} AS SELECT ${columns.join(", ")} FROM ${q(snapshot)} original JOIN ${q(staged.name)} generated ON original.${q(id)} = CAST(generated.${q(id)} AS BIGINT) ORDER BY original.${q(id)}`);
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
await table.sdb.customQuery(`DROP TABLE IF EXISTS ${q(snapshot)}; DROP TABLE IF EXISTS ${q(staged.name)}; DROP TABLE IF EXISTS ${q(batch.name)};`);
|
|
137
|
+
retainRegisteredTables(table.sdb, (item) => item !== staged && item !== batch);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type SimpleTable from "../class/SimpleTable.js";
|
|
2
|
-
export default function addColumn(simpleTable: SimpleTable, newColumn: string, type: "integer" | "float" | "number" | "string" | "date" | "time" | "datetime" | "datetimeTz" | "bigint" | "double" | "varchar" | "timestamp" | "timestamp with time zone" | "boolean" | `geometry('${string}')` | `GEOMETRY('${string}')`, definition: string): void;
|
|
2
|
+
export default function addColumn(simpleTable: SimpleTable, newColumn: string, type: "integer" | "float" | "number" | "string" | "date" | "time" | "datetime" | "datetimeTz" | "bigint" | "double" | "varchar" | "timestamp" | "timestamp with time zone" | "boolean" | "json" | "JSON" | `float[${number}]` | `FLOAT[${number}]` | `geometry('${string}')` | `GEOMETRY('${string}')`, definition: string): void;
|
|
3
3
|
//# sourceMappingURL=addColumn.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addColumn.d.ts","sourceRoot":"","sources":["../../src/methods/addColumn.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAEvD,MAAM,CAAC,OAAO,UAAU,SAAS,CAC/B,WAAW,EAAE,WAAW,EACxB,SAAS,EAAE,MAAM,EACjB,IAAI,EACA,SAAS,GACT,OAAO,GACP,QAAQ,GACR,QAAQ,GACR,MAAM,GACN,MAAM,GACN,UAAU,GACV,YAAY,GACZ,QAAQ,GACR,QAAQ,GACR,SAAS,GACT,WAAW,GACX,0BAA0B,GAC1B,SAAS,GACT,aAAa,MAAM,IAAI,GACvB,aAAa,MAAM,IAAI,EAC3B,UAAU,EAAE,MAAM,QAuBnB"}
|
|
1
|
+
{"version":3,"file":"addColumn.d.ts","sourceRoot":"","sources":["../../src/methods/addColumn.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAEvD,MAAM,CAAC,OAAO,UAAU,SAAS,CAC/B,WAAW,EAAE,WAAW,EACxB,SAAS,EAAE,MAAM,EACjB,IAAI,EACA,SAAS,GACT,OAAO,GACP,QAAQ,GACR,QAAQ,GACR,MAAM,GACN,MAAM,GACN,UAAU,GACV,YAAY,GACZ,QAAQ,GACR,QAAQ,GACR,SAAS,GACT,WAAW,GACX,0BAA0B,GAC1B,SAAS,GACT,MAAM,GACN,MAAM,GACN,SAAS,MAAM,GAAG,GAClB,SAAS,MAAM,GAAG,GAClB,aAAa,MAAM,IAAI,GACvB,aAAa,MAAM,IAAI,EAC3B,UAAU,EAAE,MAAM,QAuBnB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bm25.d.ts","sourceRoot":"","sources":["../../src/methods/bm25.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bm25.d.ts","sourceRoot":"","sources":["../../src/methods/bm25.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAMvD,MAAM,CAAC,OAAO,UAAU,IAAI,CAC1B,WAAW,EAAE,WAAW,EACxB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;IACP,OAAO,CAAC,EACJ,QAAQ,GACR,QAAQ,GACR,SAAS,GACT,QAAQ,GACR,OAAO,GACP,SAAS,GACT,SAAS,GACT,QAAQ,GACR,QAAQ,GACR,OAAO,GACP,OAAO,GACP,WAAW,GACX,YAAY,GACZ,OAAO,GACP,SAAS,GACT,YAAY,GACZ,QAAQ,GACR,WAAW,GACX,QAAQ,GACR,YAAY,GACZ,UAAU,GACV,SAAS,GACT,SAAS,GACT,SAAS,GACT,SAAS,GACT,OAAO,GACP,SAAS,GACT,MAAM,CAAC;IACX,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,OAAO,CAAC;CACd,GACL,WAAW,CA4Bb"}
|
package/esm/methods/bm25.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import quoteIdentifier from "../helpers/quoteIdentifier.js";
|
|
2
|
-
import camelCase from "../helpers/camelCase.js";
|
|
3
2
|
import queryDB from "../helpers/queryDB.js";
|
|
4
3
|
import mergeOptions from "../helpers/mergeOptions.js";
|
|
5
4
|
import queueOp from "../helpers/queueOp.js";
|
|
@@ -39,7 +38,7 @@ async function executeBm25(simpleTable, outputTable, text, idColumn, textColumn,
|
|
|
39
38
|
const selectClause = typeof options.scoreColumn === "string"
|
|
40
39
|
? `* EXCLUDE(score), score AS ${quoteIdentifier(options.scoreColumn)}`
|
|
41
40
|
: `* EXCLUDE(score)`;
|
|
42
|
-
await queryDB(simpleTable, `CREATE OR REPLACE TABLE ${quoteIdentifier(outputTable.name)} AS SELECT ${selectClause} FROM (SELECT *, ${quoteIdentifier(`fts_main_${
|
|
41
|
+
await queryDB(simpleTable, `CREATE OR REPLACE TABLE ${quoteIdentifier(outputTable.name)} AS SELECT ${selectClause} FROM (SELECT *, ${quoteIdentifier(`fts_main_${simpleTable.name}`)}.match_bm25(${quoteIdentifier(idColumn)}, ?${typeof options.k === "number" ? `, k := ${options.k}` : ""}${typeof options.b === "number" ? `, b := ${options.b}` : ""}${options.conjunctive === true ? `, conjunctive := 1` : ""}) AS score FROM ${quoteIdentifier(simpleTable.name)}) sq WHERE score NOT NULL${minScoreCondition} ORDER BY score DESC LIMIT ${count};`, mergeOptions(simpleTable, {
|
|
43
42
|
table: simpleTable.name,
|
|
44
43
|
method: "bm25",
|
|
45
44
|
parameters: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/methods/cache.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/methods/cache.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AA4DvD,wBAA8B,KAAK,CAAC,KAAK,SAAS,WAAW,EAC3D,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EAC/C,OAAO,GAAE;IACP,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,SAAS,OAAO,EAAE,CAAC;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;CACd,iBA4LP"}
|
package/esm/methods/cache.js
CHANGED
|
@@ -6,11 +6,12 @@ import { cacheEntryExpired, cacheLoadMessage, cacheTtlMessage, } from "../helper
|
|
|
6
6
|
import serializeCacheInputs, { getCacheInputLabel, } from "../helpers/serializeCacheInputs.js";
|
|
7
7
|
import { createTableGenerationId, peekTableGeneration, restoreTableGeneration, } from "../helpers/tableGeneration.js";
|
|
8
8
|
import { loadCacheDatabase, writeCacheDatabase, } from "../helpers/cacheDatabase.js";
|
|
9
|
-
import { cacheTableDependenciesMatch, captureCacheTableDependencies, changedCacheTableDependenciesMessage, recordCacheTableAccess, unchangedCacheTableDependenciesMessage, } from "../helpers/cacheTableDependencies.js";
|
|
9
|
+
import { assertCacheTableMutation, cacheTableDependenciesMatch, captureCacheTableDependencies, changedCacheTableDependenciesMessage, recordCacheTableAccess, unchangedCacheTableDependenciesMessage, } from "../helpers/cacheTableDependencies.js";
|
|
10
10
|
const CACHE_FORMAT_VERSION = "duckdb-v4";
|
|
11
11
|
export default async function cache(table, compute, options = {}) {
|
|
12
12
|
// If cache() is itself called from another cache computation, the outer
|
|
13
13
|
// computation depends on the table produced here.
|
|
14
|
+
assertCacheTableMutation(table);
|
|
14
15
|
recordCacheTableAccess(table);
|
|
15
16
|
options.verbose &&
|
|
16
17
|
console.log(`\ncache() for ${table.name}`);
|
|
@@ -31,12 +32,13 @@ export default async function cache(table, compute, options = {}) {
|
|
|
31
32
|
const inputs = options.inputs?.filter((input) => input !== table) ?? [];
|
|
32
33
|
const hasInputs = inputs.length > 0;
|
|
33
34
|
const serializedInputs = hasInputs ? serializeCacheInputs(inputs) : null;
|
|
34
|
-
const codeHash = createHash(functionBody);
|
|
35
|
+
const codeHash = createHash(JSON.stringify([table.sdb.expressionSyntax, functionBody]));
|
|
35
36
|
const inputHashes = hasInputs
|
|
36
37
|
? inputs.map((input) => createHash(serializeCacheInputs([input])))
|
|
37
38
|
: [];
|
|
38
39
|
const hash = createHash(JSON.stringify([
|
|
39
40
|
CACHE_FORMAT_VERSION,
|
|
41
|
+
table.sdb.expressionSyntax,
|
|
40
42
|
table.name,
|
|
41
43
|
entryGeneration,
|
|
42
44
|
functionBody,
|
|
@@ -119,10 +121,6 @@ async function runAndWrite(table, compute, cacheSources, cacheSourcesPath, cache
|
|
|
119
121
|
const start = Date.now();
|
|
120
122
|
const tableDependencies = await captureCacheTableDependencies(table, async () => {
|
|
121
123
|
await compute(table);
|
|
122
|
-
// run() only queues the sync builders in the user's callback; the actual
|
|
123
|
-
// computation happens at the flush, so it must be included in the timing
|
|
124
|
-
// that decides how much the cache saves on later hits.
|
|
125
|
-
await flushAllTables(table.sdb);
|
|
126
124
|
});
|
|
127
125
|
const end = Date.now();
|
|
128
126
|
const duration = end - start;
|
package/esm/methods/convert.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type SimpleTable from "../class/SimpleTable.js";
|
|
2
2
|
import type { TableSchema } from "../helpers/pendingOps.js";
|
|
3
3
|
export default function convert(simpleTable: SimpleTable, types: {
|
|
4
|
-
[key: string]: "integer" | "float" | "number" | "string" | "date" | "time" | "datetime" | "datetimeTz" | "bigint" | "double" | "varchar" | "timestamp" | "timestamp with time zone" | "boolean"
|
|
4
|
+
[key: string]: "integer" | "float" | "number" | "string" | "date" | "time" | "datetime" | "datetimeTz" | "bigint" | "double" | "varchar" | "timestamp" | "timestamp with time zone" | "boolean" | "json" | "JSON" | `float[${number}]` | `FLOAT[${number}]` | `geometry('${string}')` | `GEOMETRY('${string}')`;
|
|
5
5
|
}, options?: {
|
|
6
6
|
strict?: boolean;
|
|
7
7
|
datetimeFormat?: string;
|
|
8
8
|
}): void;
|
|
9
|
-
export declare function convertSelect(input: string, columns: string[], columnsTypes: ("integer" | "float" | "number" | "string" | "date" | "time" | "datetime" | "datetimeTz" | "bigint" | "double" | "varchar" | "timestamp" | "timestamp with time zone" | "boolean")[], allColumns: string[], allTypes: TableSchema, options: {
|
|
9
|
+
export declare function convertSelect(input: string, columns: string[], columnsTypes: ("integer" | "float" | "number" | "string" | "date" | "time" | "datetime" | "datetimeTz" | "bigint" | "double" | "varchar" | "timestamp" | "timestamp with time zone" | "boolean" | "json" | "JSON" | `float[${number}]` | `FLOAT[${number}]` | `geometry('${string}')` | `GEOMETRY('${string}')`)[], allColumns: string[], allTypes: TableSchema, options: {
|
|
10
10
|
datetimeFormat?: string;
|
|
11
11
|
strict?: boolean;
|
|
12
12
|
}): string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../src/methods/convert.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AACvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE5D,MAAM,CAAC,OAAO,UAAU,OAAO,CAC7B,WAAW,EAAE,WAAW,EACxB,KAAK,EAAE;IACL,CAAC,GAAG,EAAE,MAAM,GACR,SAAS,GACT,OAAO,GACP,QAAQ,GACR,QAAQ,GACR,MAAM,GACN,MAAM,GACN,UAAU,GACV,YAAY,GACZ,QAAQ,GACR,QAAQ,GACR,SAAS,GACT,WAAW,GACX,0BAA0B,GAC1B,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../src/methods/convert.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AACvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE5D,MAAM,CAAC,OAAO,UAAU,OAAO,CAC7B,WAAW,EAAE,WAAW,EACxB,KAAK,EAAE;IACL,CAAC,GAAG,EAAE,MAAM,GACR,SAAS,GACT,OAAO,GACP,QAAQ,GACR,QAAQ,GACR,MAAM,GACN,MAAM,GACN,UAAU,GACV,YAAY,GACZ,QAAQ,GACR,QAAQ,GACR,SAAS,GACT,WAAW,GACX,0BAA0B,GAC1B,SAAS,GACT,MAAM,GACN,MAAM,GACN,SAAS,MAAM,GAAG,GAClB,SAAS,MAAM,GAAG,GAClB,aAAa,MAAM,IAAI,GACvB,aAAa,MAAM,IAAI,CAAC;CAC7B,EACD,OAAO,GAAE;IACP,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;CACpB,QAkDP;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,EAAE,EACjB,YAAY,EAAE,CACV,SAAS,GACT,OAAO,GACP,QAAQ,GACR,QAAQ,GACR,MAAM,GACN,MAAM,GACN,UAAU,GACV,YAAY,GACZ,QAAQ,GACR,QAAQ,GACR,SAAS,GACT,WAAW,GACX,0BAA0B,GAC1B,SAAS,GACT,MAAM,GACN,MAAM,GACN,SAAS,MAAM,GAAG,GAClB,SAAS,MAAM,GAAG,GAClB,aAAa,MAAM,IAAI,GACvB,aAAa,MAAM,IAAI,CAC1B,EAAE,EACH,UAAU,EAAE,MAAM,EAAE,EACpB,QAAQ,EAAE,WAAW,EACrB,OAAO,EAAE;IAAE,cAAc,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,UAuFvD"}
|
package/esm/methods/convert.js
CHANGED
|
@@ -9,6 +9,7 @@ export default function convert(simpleTable, types, options = {}) {
|
|
|
9
9
|
method: "convert()",
|
|
10
10
|
parameters: { types, options },
|
|
11
11
|
needsSchema: true,
|
|
12
|
+
needsSpatial: Object.values(types).some((type) => type.toLowerCase().startsWith("geometry")),
|
|
12
13
|
outputSchema: (allTypes) => ({
|
|
13
14
|
...allTypes,
|
|
14
15
|
...Object.fromEntries(Object.entries(types).map(([column, type]) => [
|
|
@@ -78,6 +79,10 @@ export function convertSelect(input, columns, columnsTypes, allColumns, allTypes
|
|
|
78
79
|
else if (msToTimestamp) {
|
|
79
80
|
query += ` TIMESTAMP '1970-01-01 00:00:00' + to_milliseconds(${quoteIdentifier(column)}) AS ${quoteIdentifier(column)},`;
|
|
80
81
|
}
|
|
82
|
+
else if (options.strict === false && expectedType.startsWith("GEOMETRY")) {
|
|
83
|
+
// DuckDB's geometry parser can throw even inside TRY_CAST.
|
|
84
|
+
query += ` TRY(CAST(${quoteIdentifier(column)} AS ${expectedType})) AS ${quoteIdentifier(column)},`;
|
|
85
|
+
}
|
|
81
86
|
else if (stringToNumber) {
|
|
82
87
|
// Thousand separators would make the cast fail.
|
|
83
88
|
query += ` ${cast}(REPLACE(${quoteIdentifier(column)}, ',', '') AS ${parseType(columnsTypes[indexOf])}) AS ${quoteIdentifier(column)},`;
|
|
@@ -2,7 +2,7 @@ import type SimpleTable from "../class/SimpleTable.js";
|
|
|
2
2
|
export default function getBottom(simpleTable: SimpleTable, count: number, options?: {
|
|
3
3
|
originalOrder?: boolean;
|
|
4
4
|
conditions?: string;
|
|
5
|
-
}): Promise<{
|
|
5
|
+
}, method?: "getBottom()" | "logBottom()"): Promise<{
|
|
6
6
|
[key: string]: unknown;
|
|
7
7
|
}[]>;
|
|
8
8
|
//# sourceMappingURL=getBottom.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBottom.d.ts","sourceRoot":"","sources":["../../src/methods/getBottom.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAEvD,wBAA8B,SAAS,CACrC,WAAW,EAAE,WAAW,EACxB,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;IACP,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;CAChB;;
|
|
1
|
+
{"version":3,"file":"getBottom.d.ts","sourceRoot":"","sources":["../../src/methods/getBottom.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAEvD,wBAA8B,SAAS,CACrC,WAAW,EAAE,WAAW,EACxB,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;IACP,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;CAChB,EACN,MAAM,GAAE,aAAa,GAAG,aAA6B;;KA6BtD"}
|
package/esm/methods/getBottom.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import quoteIdentifier from "../helpers/quoteIdentifier.js";
|
|
2
2
|
import mergeOptions from "../helpers/mergeOptions.js";
|
|
3
3
|
import queryDB from "../helpers/queryDB.js";
|
|
4
|
-
export default async function getBottom(simpleTable, count, options = {}) {
|
|
4
|
+
export default async function getBottom(simpleTable, count, options = {}, method = "getBottom()") {
|
|
5
5
|
const queryResult = await queryDB(simpleTable, `WITH "numberedRowsForGetBottom" AS (
|
|
6
6
|
SELECT *, row_number() OVER () as "rowNumberForGetBottom" FROM ${quoteIdentifier(simpleTable.name)}${options.conditions ? ` WHERE ${options.conditions}` : ""}
|
|
7
7
|
)
|
|
8
8
|
SELECT * FROM "numberedRowsForGetBottom" ORDER BY "rowNumberForGetBottom" DESC LIMIT ${count};`, mergeOptions(simpleTable, {
|
|
9
9
|
table: simpleTable.name,
|
|
10
10
|
returnData: true,
|
|
11
|
-
method
|
|
11
|
+
method,
|
|
12
12
|
parameters: { count, options },
|
|
13
13
|
}));
|
|
14
14
|
if (!queryResult) {
|
package/esm/methods/getData.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export default function getData(simpleTable: SimpleTable, options?: {
|
|
|
3
3
|
columns?: string | string[];
|
|
4
4
|
conditions?: string;
|
|
5
5
|
limit?: number;
|
|
6
|
-
}): Promise<{
|
|
6
|
+
}, method?: "getData()" | "log()"): Promise<{
|
|
7
7
|
[key: string]: unknown;
|
|
8
8
|
}[]>;
|
|
9
9
|
//# sourceMappingURL=getData.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getData.d.ts","sourceRoot":"","sources":["../../src/methods/getData.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAIvD,wBAA8B,OAAO,CACnC,WAAW,EAAE,WAAW,EACxB,OAAO,GAAE;IACP,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;CACX;;
|
|
1
|
+
{"version":3,"file":"getData.d.ts","sourceRoot":"","sources":["../../src/methods/getData.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAIvD,wBAA8B,OAAO,CACnC,WAAW,EAAE,WAAW,EACxB,OAAO,GAAE;IACP,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;CACX,EACN,MAAM,GAAE,WAAW,GAAG,OAAqB;;KAmC5C"}
|
package/esm/methods/getData.js
CHANGED
|
@@ -2,7 +2,7 @@ import mergeOptions from "../helpers/mergeOptions.js";
|
|
|
2
2
|
import queryDB from "../helpers/queryDB.js";
|
|
3
3
|
import quoteIdentifier from "../helpers/quoteIdentifier.js";
|
|
4
4
|
import quoteQualifiedIdentifier from "../helpers/quoteQualifiedIdentifier.js";
|
|
5
|
-
export default async function getData(simpleTable, options = {}) {
|
|
5
|
+
export default async function getData(simpleTable, options = {}, method = "getData()") {
|
|
6
6
|
if (options.limit !== undefined &&
|
|
7
7
|
(!Number.isInteger(options.limit) || options.limit < 0)) {
|
|
8
8
|
throw new Error("getData() limit must be an integer greater than or equal to 0.");
|
|
@@ -17,7 +17,7 @@ export default async function getData(simpleTable, options = {}) {
|
|
|
17
17
|
: "*"} from ${quoteIdentifier(simpleTable.name)}${options.conditions ? ` WHERE ${options.conditions}` : ""}${options.limit === undefined ? "" : ` LIMIT ${options.limit}`}`, mergeOptions(simpleTable, {
|
|
18
18
|
returnData: true,
|
|
19
19
|
table: simpleTable.name,
|
|
20
|
-
method
|
|
20
|
+
method,
|
|
21
21
|
parameters: { options },
|
|
22
22
|
})));
|
|
23
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"join.d.ts","sourceRoot":"","sources":["../../src/methods/join.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"join.d.ts","sourceRoot":"","sources":["../../src/methods/join.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAKvD,MAAM,CAAC,OAAO,UAAU,IAAI,CAC1B,SAAS,EAAE,WAAW,EACtB,UAAU,EAAE,WAAW,EACvB,OAAO,GAAE;IACP,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACvB,IAAI,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;IAC3C,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CAC3B,GACL,WAAW,CAkBb"}
|
package/esm/methods/join.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import quoteIdentifier from "../helpers/quoteIdentifier.js";
|
|
2
2
|
import getIdenticalColumns from "../helpers/getIdenticalColumns.js";
|
|
3
|
-
import mergeOptions from "../helpers/mergeOptions.js";
|
|
4
|
-
import queryDB from "../helpers/queryDB.js";
|
|
5
3
|
import queueOp from "../helpers/queueOp.js";
|
|
6
4
|
import assertSameDatabase from "../helpers/assertSameDatabase.js";
|
|
7
5
|
export default function join(leftTable, rightTable, options = {}) {
|
|
@@ -13,14 +11,15 @@ export default function join(leftTable, rightTable, options = {}) {
|
|
|
13
11
|
? leftTable.sdb.newTable(options.outputTable)
|
|
14
12
|
: leftTable;
|
|
15
13
|
queueOp(outputTable, {
|
|
16
|
-
kind: "
|
|
14
|
+
kind: "source",
|
|
15
|
+
rawSQL: [quoteIdentifier(leftTable.name), quoteIdentifier(rightTable.name)],
|
|
17
16
|
method: "join()",
|
|
18
17
|
parameters: { rightTable: rightTable.name, options },
|
|
19
|
-
|
|
18
|
+
buildSelect: () => buildJoinSelect(leftTable, rightTable, options),
|
|
20
19
|
});
|
|
21
20
|
return outputTable;
|
|
22
21
|
}
|
|
23
|
-
async function
|
|
22
|
+
async function buildJoinSelect(leftTable, rightTable, options) {
|
|
24
23
|
const leftTableColumns = await leftTable.getColumns();
|
|
25
24
|
const rightTableColumns = await rightTable.getColumns();
|
|
26
25
|
const identicalColumns = getIdenticalColumns(leftTableColumns, rightTableColumns);
|
|
@@ -74,17 +73,10 @@ async function executeJoin(leftTable, rightTable, outputTable, options) {
|
|
|
74
73
|
.filter((d) => !on.includes(d))
|
|
75
74
|
.map((d) => `${quoteIdentifier(rightTable.name)}.${quoteIdentifier(d)}`),
|
|
76
75
|
].join(", ");
|
|
77
|
-
|
|
78
|
-
table: outputTable.name,
|
|
79
|
-
method: "join()",
|
|
80
|
-
parameters: {
|
|
81
|
-
rightTable: rightTable.name,
|
|
82
|
-
options,
|
|
83
|
-
},
|
|
84
|
-
}));
|
|
76
|
+
return joinQuery(leftTable.name, rightTable.name, on, type, selectList);
|
|
85
77
|
}
|
|
86
|
-
function joinQuery(leftTable, rightTable, on, join,
|
|
87
|
-
let query = `
|
|
78
|
+
function joinQuery(leftTable, rightTable, on, join, selectList) {
|
|
79
|
+
let query = `SELECT ${selectList}`;
|
|
88
80
|
if (join === "inner") {
|
|
89
81
|
query += ` FROM ${quoteIdentifier(leftTable)} JOIN ${quoteIdentifier(rightTable)}`;
|
|
90
82
|
}
|
|
@@ -101,6 +93,6 @@ function joinQuery(leftTable, rightTable, on, join, outputTable, selectList) {
|
|
|
101
93
|
throw new Error(`Unknown ${join} join.`);
|
|
102
94
|
}
|
|
103
95
|
query += ` ON (${on.map((d) => `${quoteIdentifier(leftTable)}.${quoteIdentifier(d)} = ${quoteIdentifier(rightTable)}.${quoteIdentifier(d)}`)
|
|
104
|
-
.join(" AND ")})
|
|
96
|
+
.join(" AND ")})`;
|
|
105
97
|
return query;
|
|
106
98
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"joinGeo.d.ts","sourceRoot":"","sources":["../../src/methods/joinGeo.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"joinGeo.d.ts","sourceRoot":"","sources":["../../src/methods/joinGeo.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAMvD,MAAM,CAAC,OAAO,UAAU,OAAO,CAC7B,SAAS,EAAE,WAAW,EACtB,MAAM,EAAE,WAAW,GAAG,QAAQ,GAAG,gBAAgB,EACjD,UAAU,EAAE,WAAW,EACvB,OAAO,GAAE;IACP,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,KAAK,GAAG,WAAW,GAAG,UAAU,CAAC;IAClD,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CAC3B,GACL,WAAW,CAqBb"}
|
package/esm/methods/joinGeo.js
CHANGED
|
@@ -2,8 +2,6 @@ import quoteIdentifier from "../helpers/quoteIdentifier.js";
|
|
|
2
2
|
import capitalize from "../helpers/capitalize.js";
|
|
3
3
|
import findGeoColumnFromSchema from "../helpers/findGeoColumnFromSchema.js";
|
|
4
4
|
import getIdenticalColumns from "../helpers/getIdenticalColumns.js";
|
|
5
|
-
import mergeOptions from "../helpers/mergeOptions.js";
|
|
6
|
-
import queryDB from "../helpers/queryDB.js";
|
|
7
5
|
import queueOp from "../helpers/queueOp.js";
|
|
8
6
|
import assertSameDatabase from "../helpers/assertSameDatabase.js";
|
|
9
7
|
export default function joinGeo(leftTable, method, rightTable, options = {}) {
|
|
@@ -16,10 +14,12 @@ export default function joinGeo(leftTable, method, rightTable, options = {}) {
|
|
|
16
14
|
? leftTable.sdb.newTable(options.outputTable)
|
|
17
15
|
: leftTable;
|
|
18
16
|
queueOp(outputTable, {
|
|
19
|
-
kind: "
|
|
17
|
+
kind: "source",
|
|
18
|
+
rawSQL: [quoteIdentifier(leftTable.name), quoteIdentifier(rightTable.name)],
|
|
19
|
+
needsSpatial: true,
|
|
20
20
|
method: "joinGeo()",
|
|
21
21
|
parameters: { method, rightTable: rightTable.name, options },
|
|
22
|
-
|
|
22
|
+
buildSelect: () => buildJoinGeoSelect(leftTable, method, rightTable, options),
|
|
23
23
|
});
|
|
24
24
|
return outputTable;
|
|
25
25
|
}
|
|
@@ -43,7 +43,7 @@ function validateJoinGeoOptions(method, options) {
|
|
|
43
43
|
function formatReceivedValue(value) {
|
|
44
44
|
return JSON.stringify(value) ?? String(value);
|
|
45
45
|
}
|
|
46
|
-
async function
|
|
46
|
+
async function buildJoinGeoSelect(leftTable, method, rightTable, options) {
|
|
47
47
|
const leftTypes = await leftTable.getTypes();
|
|
48
48
|
const rightTypes = await rightTable.getTypes();
|
|
49
49
|
const leftColumn = options.leftColumn ??
|
|
@@ -83,19 +83,10 @@ async function executeJoinGeo(leftTable, method, rightTable, outputTable, option
|
|
|
83
83
|
]),
|
|
84
84
|
].join(", ");
|
|
85
85
|
const type = options.type ?? "left";
|
|
86
|
-
|
|
87
|
-
table: outputTable.name,
|
|
88
|
-
method: "joinGeo()",
|
|
89
|
-
parameters: {
|
|
90
|
-
leftTable: leftTable.name,
|
|
91
|
-
method,
|
|
92
|
-
rightTable: rightTable.name,
|
|
93
|
-
options,
|
|
94
|
-
},
|
|
95
|
-
}));
|
|
86
|
+
return joinGeoQuery(leftTable.name, leftColumn, method, rightTable.name, rightColumn, type, selectList, options.distance, options.distanceMethod);
|
|
96
87
|
}
|
|
97
|
-
function joinGeoQuery(leftTable, leftColumn, method, rightTable, rightColumn, join,
|
|
98
|
-
let query = `
|
|
88
|
+
function joinGeoQuery(leftTable, leftColumn, method, rightTable, rightColumn, join, selectList, distance, distanceMethod) {
|
|
89
|
+
let query = `SELECT ${selectList}`;
|
|
99
90
|
if (join === "inner") {
|
|
100
91
|
query += ` FROM ${quoteIdentifier(leftTable)} JOIN ${quoteIdentifier(rightTable)}`;
|
|
101
92
|
}
|
|
@@ -112,11 +103,11 @@ function joinGeoQuery(leftTable, leftColumn, method, rightTable, rightColumn, jo
|
|
|
112
103
|
throw new Error(`Unknown ${join} join.`);
|
|
113
104
|
}
|
|
114
105
|
if (method === "intersect") {
|
|
115
|
-
query += ` ON ST_Intersects(${quoteIdentifier(leftTable)}.${quoteIdentifier(leftColumn)}, ${quoteIdentifier(rightTable)}.${quoteIdentifier(rightColumn)})
|
|
106
|
+
query += ` ON ST_Intersects(${quoteIdentifier(leftTable)}.${quoteIdentifier(leftColumn)}, ${quoteIdentifier(rightTable)}.${quoteIdentifier(rightColumn)})`;
|
|
116
107
|
}
|
|
117
108
|
else if (method === "inside") {
|
|
118
109
|
// Order is important
|
|
119
|
-
query += ` ON ST_Covers(${quoteIdentifier(rightTable)}.${quoteIdentifier(rightColumn)}, ${quoteIdentifier(leftTable)}.${quoteIdentifier(leftColumn)})
|
|
110
|
+
query += ` ON ST_Covers(${quoteIdentifier(rightTable)}.${quoteIdentifier(rightColumn)}, ${quoteIdentifier(leftTable)}.${quoteIdentifier(leftColumn)})`;
|
|
120
111
|
}
|
|
121
112
|
else if (method === "withinDistance") {
|
|
122
113
|
if (typeof distance === "number") {
|
|
@@ -13,7 +13,7 @@ export declare function prepareArray(rows: {
|
|
|
13
13
|
[key: string]: unknown;
|
|
14
14
|
}[], columnTypes?: {
|
|
15
15
|
[key: string]: string;
|
|
16
|
-
}): PreparedArray;
|
|
16
|
+
}, requireExplicitNestedTypes?: boolean): PreparedArray;
|
|
17
17
|
export declare function executePreparedArray(simpleTable: SimpleTable, prepared: PreparedArray): Promise<void>;
|
|
18
18
|
export {};
|
|
19
19
|
//# sourceMappingURL=loadArray.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadArray.d.ts","sourceRoot":"","sources":["../../src/methods/loadArray.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,KAAK,WAAW,EACjB,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"loadArray.d.ts","sourceRoot":"","sources":["../../src/methods/loadArray.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,KAAK,WAAW,EACjB,MAAM,kBAAkB,CAAC;AAK1B,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAKvD,MAAM,CAAC,OAAO,UAAU,SAAS,CAC/B,WAAW,EAAE,WAAW,EACxB,IAAI,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,EAAE,EAClC,OAAO,GAAE,WAAW,CAAC,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAM,QAiBnE;AAED,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,WAAW,EAAE,WAAW,EAAE,EAAE,CAAC;IAC7B,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,wBAAgB,YAAY,CAC1B,IAAI,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,EAAE,EAClC,WAAW,GAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAO,EAC3C,0BAA0B,UAAQ,GACjC,aAAa,CAsHf;AAED,wBAAsB,oBAAoB,CACxC,WAAW,EAAE,WAAW,EACxB,QAAQ,EAAE,aAAa,iBA4ExB"}
|
package/esm/methods/loadArray.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { arrayValue, DuckDBDataChunk, DuckDBDateValue, DuckDBTimestampTZValue, DuckDBTimestampValue, DuckDBTimeValue, } from "@duckdb/node-api";
|
|
2
|
+
import prepareGeometry from "../helpers/prepareGeometry.js";
|
|
3
|
+
import prepareJSON from "../helpers/prepareJSON.js";
|
|
4
|
+
import geometryFromJSON from "../helpers/geometryFromJSON.js";
|
|
2
5
|
import queueOp from "../helpers/queueOp.js";
|
|
3
6
|
import parseType from "../helpers/parseTypes.js";
|
|
4
7
|
import parseDuckDBType from "../helpers/parseDuckDBType.js";
|
|
@@ -8,7 +11,7 @@ export default function loadArray(simpleTable, rows, options = {}) {
|
|
|
8
11
|
if (rows.length === 0) {
|
|
9
12
|
throw new Error("The array is empty. loadArray needs at least one object to infer the column types.");
|
|
10
13
|
}
|
|
11
|
-
const prepared = prepareArray(rows, options.columnTypes);
|
|
14
|
+
const prepared = prepareArray(rows, options.columnTypes, true);
|
|
12
15
|
queueOp(simpleTable, {
|
|
13
16
|
kind: "barrier",
|
|
14
17
|
method: "loadArray()",
|
|
@@ -16,17 +19,26 @@ export default function loadArray(simpleTable, rows, options = {}) {
|
|
|
16
19
|
execute: () => executePreparedArray(simpleTable, prepared),
|
|
17
20
|
});
|
|
18
21
|
}
|
|
19
|
-
export function prepareArray(rows, columnTypes = {}) {
|
|
22
|
+
export function prepareArray(rows, columnTypes = {}, requireExplicitNestedTypes = false) {
|
|
20
23
|
const keys = Object.keys(rows[0]);
|
|
21
24
|
const overrides = new Map();
|
|
22
25
|
for (const [key, type] of Object.entries(columnTypes)) {
|
|
23
26
|
if (!keys.includes(key)) {
|
|
24
27
|
throw new Error(`Unknown column ${JSON.stringify(key)} in loadArray columnTypes.`);
|
|
25
28
|
}
|
|
29
|
+
if (type.toUpperCase() === "JSON") {
|
|
30
|
+
overrides.set(key, "JSON");
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (/^geometry/i.test(type)) {
|
|
34
|
+
if (!/^geometry\('EPSG:4326'\)$/i.test(type)) {
|
|
35
|
+
throw new Error(`Unsupported type ${JSON.stringify(type)} for column ${JSON.stringify(key)}. GeoJSON ingestion requires GEOMETRY('EPSG:4326'); reproject input to WGS84 first.`);
|
|
36
|
+
}
|
|
37
|
+
overrides.set(key, "GEOMETRY('EPSG:4326')");
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
26
40
|
try {
|
|
27
|
-
const normalized =
|
|
28
|
-
? type.toUpperCase()
|
|
29
|
-
: parseType(type);
|
|
41
|
+
const normalized = parseType(type);
|
|
30
42
|
parseDuckDBType(normalized);
|
|
31
43
|
overrides.set(key, normalized);
|
|
32
44
|
}
|
|
@@ -48,6 +60,14 @@ export function prepareArray(rows, columnTypes = {}) {
|
|
|
48
60
|
}
|
|
49
61
|
continue;
|
|
50
62
|
}
|
|
63
|
+
if (requireExplicitNestedTypes) {
|
|
64
|
+
for (let j = 0; j < rows.length; j++) {
|
|
65
|
+
const cell = rows[j][key];
|
|
66
|
+
if (cell !== null && typeof cell === "object" && !(cell instanceof Date)) {
|
|
67
|
+
throw new Error(`Column ${JSON.stringify(key)}, row ${j + 1}: array and object cells require an explicit columnTypes entry. Use JSON for nested data, FLOAT[n] for fixed-size vectors, or GEOMETRY('EPSG:4326') for GeoJSON geometries.`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
51
71
|
const value = firstNonNullValue[i];
|
|
52
72
|
const type = typeof value;
|
|
53
73
|
if (type === "symbol" || type === "function") {
|
|
@@ -104,24 +124,59 @@ export async function executePreparedArray(simpleTable, prepared) {
|
|
|
104
124
|
simpleTable.connection = simpleTable.sdb.connection;
|
|
105
125
|
}
|
|
106
126
|
const { keys, types, columnsData, rowCount } = prepared;
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
appender.
|
|
127
|
+
const hasGeometry = types.includes("GEOMETRY('EPSG:4326')");
|
|
128
|
+
const needsConversion = hasGeometry || types.includes("JSON");
|
|
129
|
+
const staged = needsConversion
|
|
130
|
+
? `__sda_array_${crypto.randomUUID().replaceAll("-", "")}`
|
|
131
|
+
: simpleTable.name;
|
|
132
|
+
const storageTypes = types.map((type) => type.startsWith("GEOMETRY") || type === "JSON" ? "VARCHAR" : type);
|
|
133
|
+
if (hasGeometry && !simpleTable.sdb.spatialLoaded) {
|
|
134
|
+
await simpleTable.sdb.customQuery("INSTALL spatial; LOAD spatial; SET geometry_always_xy = true;");
|
|
135
|
+
simpleTable.sdb.spatialLoaded = true;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
await simpleTable.sdb.customQuery(`CREATE ${needsConversion ? "TEMP" : "OR REPLACE"} TABLE ${quoteIdentifier(staged)}(${keys.map((key, i) => `${quoteIdentifier(key)} ${storageTypes[i]}`).join(", ")})`);
|
|
139
|
+
const appender = await simpleTable.connection
|
|
140
|
+
.createAppender(staged);
|
|
141
|
+
try {
|
|
142
|
+
const duckDBTypes = storageTypes.map((d) => parseDuckDBType(d));
|
|
143
|
+
// The maximum capacity of a DuckDB data chunk is 2048 rows.
|
|
144
|
+
const chunkSize = 2000;
|
|
145
|
+
for (let start = 0; start < rowCount; start += chunkSize) {
|
|
146
|
+
const end = Math.min(start + chunkSize, rowCount);
|
|
147
|
+
const dataChunk = DuckDBDataChunk.create(duckDBTypes, end - start);
|
|
148
|
+
for (let i = 0; i < keys.length; i++) {
|
|
149
|
+
dataChunk.setColumnValues(i, columnsData[i].slice(start, end));
|
|
150
|
+
}
|
|
151
|
+
appender.appendDataChunk(dataChunk);
|
|
152
|
+
}
|
|
153
|
+
appender.flushSync();
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
appender.closeSync();
|
|
157
|
+
}
|
|
158
|
+
if (!needsConversion)
|
|
159
|
+
return;
|
|
160
|
+
// CTAS is atomic: conversion must finish before the destination is replaced.
|
|
161
|
+
await simpleTable.sdb.customQuery(`CREATE OR REPLACE TABLE ${quoteIdentifier(simpleTable.name)} AS SELECT ${keys.map((key, i) => types[i].startsWith("GEOMETRY")
|
|
162
|
+
? `${geometryFromJSON(quoteIdentifier(key))} AS ${quoteIdentifier(key)}`
|
|
163
|
+
: types[i] === "JSON"
|
|
164
|
+
? `${quoteIdentifier(key)}::JSON AS ${quoteIdentifier(key)}`
|
|
165
|
+
: quoteIdentifier(key)).join(", ")} FROM ${quoteIdentifier(staged)}`);
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
if (needsConversion) {
|
|
169
|
+
await simpleTable.sdb.customQuery(`DROP TABLE IF EXISTS ${quoteIdentifier(staged)}`);
|
|
170
|
+
}
|
|
120
171
|
}
|
|
121
|
-
appender.flushSync();
|
|
122
172
|
}
|
|
123
173
|
/** Validates and snapshots a value for an explicitly declared destination type. */
|
|
124
174
|
function prepareTypedValue(value, type, column, row) {
|
|
175
|
+
if (type === "JSON")
|
|
176
|
+
return prepareJSON(value, column, row);
|
|
177
|
+
if (type === "GEOMETRY('EPSG:4326')") {
|
|
178
|
+
return prepareGeometry(value, column, row);
|
|
179
|
+
}
|
|
125
180
|
if (value === null || value === undefined || Number.isNaN(value))
|
|
126
181
|
return null;
|
|
127
182
|
let compatible = false;
|