@leonardovida-md/drizzle-neo-duckdb 2.2.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/dist/client.d.ts +29 -1
- package/dist/columns.d.ts +32 -32
- package/dist/dialect.d.ts +3 -0
- package/dist/driver.d.ts +27 -2
- package/dist/duckdb-introspect.mjs +381 -231
- package/dist/helpers.mjs +24 -17
- package/dist/index.mjs +502 -376
- package/dist/motherduck.d.ts +8 -3
- package/dist/prepared-statement-cache.d.ts +4 -1
- package/dist/session.d.ts +15 -3
- package/dist/sql/result-mapper.d.ts +17 -0
- package/dist/time.d.ts +5 -0
- package/package.json +21 -8
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
JSDuckDBValueConverter,
|
|
16
16
|
JsonDuckDBValueConverter,
|
|
17
17
|
listValue as listValue2,
|
|
18
|
+
TIMESTAMPTZ as TIMESTAMPTZ2,
|
|
18
19
|
timestampTZValue as timestampTZValue2,
|
|
19
20
|
timestampValue as timestampValue2
|
|
20
21
|
} from "@duckdb/node-api";
|
|
@@ -69,6 +70,13 @@ import {
|
|
|
69
70
|
|
|
70
71
|
// src/time.ts
|
|
71
72
|
var MICROS_PER_DAY = 86400000000n;
|
|
73
|
+
var droppedMicros = new WeakMap;
|
|
74
|
+
function rememberDroppedMicros(date, micros) {
|
|
75
|
+
droppedMicros.set(date, micros);
|
|
76
|
+
}
|
|
77
|
+
function dateSubMillisecondMicros(date) {
|
|
78
|
+
return droppedMicros.get(date) ?? 0;
|
|
79
|
+
}
|
|
72
80
|
function pad(value, length) {
|
|
73
81
|
return value.toString().padStart(length, "0");
|
|
74
82
|
}
|
|
@@ -114,6 +122,38 @@ function timestampStringToDateInput(value, parts = parseTimestampString(value))
|
|
|
114
122
|
const normalized = !value.includes("T") && value.includes(" ") ? value.replace(" ", "T") : value;
|
|
115
123
|
return normalized.endsWith("Z") || /[+-]\d{2}:?\d{2}$/.test(normalized) ? normalized : `${normalized}Z`;
|
|
116
124
|
}
|
|
125
|
+
function dateStringFromDate(value) {
|
|
126
|
+
const year = value.getUTCFullYear();
|
|
127
|
+
const bc = year <= 0;
|
|
128
|
+
const month = String(value.getUTCMonth() + 1).padStart(2, "0");
|
|
129
|
+
const day = String(value.getUTCDate()).padStart(2, "0");
|
|
130
|
+
const text = `${String(bc ? 1 - year : year).padStart(4, "0")}-${month}-${day}`;
|
|
131
|
+
return bc ? `${text} (BC)` : text;
|
|
132
|
+
}
|
|
133
|
+
function pluralUnit(value, unit) {
|
|
134
|
+
return `${value} ${unit}${Math.abs(value) === 1 ? "" : "s"}`;
|
|
135
|
+
}
|
|
136
|
+
function intervalToString(months, days, micros) {
|
|
137
|
+
const parts = [];
|
|
138
|
+
const years = Math.trunc(months / 12);
|
|
139
|
+
const extraMonths = months - years * 12;
|
|
140
|
+
if (years !== 0)
|
|
141
|
+
parts.push(pluralUnit(years, "year"));
|
|
142
|
+
if (extraMonths !== 0)
|
|
143
|
+
parts.push(pluralUnit(extraMonths, "month"));
|
|
144
|
+
if (days !== 0)
|
|
145
|
+
parts.push(pluralUnit(days, "day"));
|
|
146
|
+
if (micros !== 0n) {
|
|
147
|
+
const negative = micros < 0n;
|
|
148
|
+
const abs = negative ? -micros : micros;
|
|
149
|
+
const hours = abs / 3600000000n;
|
|
150
|
+
const minutes = abs / 60000000n % 60n;
|
|
151
|
+
const seconds = abs / 1000000n % 60n;
|
|
152
|
+
const fraction = pad(abs % 1000000n, 6).replace(/0+$/, "");
|
|
153
|
+
parts.push(`${negative ? "-" : ""}${pad(hours, 2)}:${pad(minutes, 2)}:${pad(seconds, 2)}${fraction ? `.${fraction}` : ""}`);
|
|
154
|
+
}
|
|
155
|
+
return parts.length > 0 ? parts.join(" ") : "00:00:00";
|
|
156
|
+
}
|
|
117
157
|
function subMillisecondMicros(fraction) {
|
|
118
158
|
if (!fraction || fraction.length <= 3) {
|
|
119
159
|
return 0n;
|
|
@@ -605,120 +645,10 @@ function parsePgArrayLiteral(value) {
|
|
|
605
645
|
|
|
606
646
|
// src/prepared-statement-cache.ts
|
|
607
647
|
var PREPARED_CACHE = Symbol.for("drizzle-duckdb:prepared-cache");
|
|
608
|
-
function destroyPreparedStatement(entry) {
|
|
609
|
-
if (!entry)
|
|
610
|
-
return;
|
|
611
|
-
try {
|
|
612
|
-
entry.statement.destroySync();
|
|
613
|
-
} catch {}
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
class PreparedStatementCache {
|
|
617
|
-
connection;
|
|
618
|
-
size;
|
|
619
|
-
entries = new Map;
|
|
620
|
-
executionTail = Promise.resolve();
|
|
621
|
-
hits = 0;
|
|
622
|
-
misses = 0;
|
|
623
|
-
evictions = 0;
|
|
624
|
-
stats() {
|
|
625
|
-
return {
|
|
626
|
-
size: this.entries.size,
|
|
627
|
-
capacity: this.size,
|
|
628
|
-
hits: this.hits,
|
|
629
|
-
misses: this.misses,
|
|
630
|
-
evictions: this.evictions
|
|
631
|
-
};
|
|
632
|
-
}
|
|
633
|
-
constructor(connection, size) {
|
|
634
|
-
this.connection = connection;
|
|
635
|
-
this.size = size;
|
|
636
|
-
}
|
|
637
|
-
resize(size) {
|
|
638
|
-
this.size = size;
|
|
639
|
-
this.trimToSize();
|
|
640
|
-
}
|
|
641
|
-
async getOrPrepare(query) {
|
|
642
|
-
const cached = this.entries.get(query);
|
|
643
|
-
if (cached) {
|
|
644
|
-
this.hits += 1;
|
|
645
|
-
return this.remember(query, cached.statement);
|
|
646
|
-
}
|
|
647
|
-
this.misses += 1;
|
|
648
|
-
const statement = await this.connection.prepare(query);
|
|
649
|
-
this.remember(query, statement);
|
|
650
|
-
return statement;
|
|
651
|
-
}
|
|
652
|
-
async runExclusive(operation) {
|
|
653
|
-
const previous = this.executionTail;
|
|
654
|
-
let release = () => {};
|
|
655
|
-
this.executionTail = new Promise((resolve) => {
|
|
656
|
-
release = resolve;
|
|
657
|
-
});
|
|
658
|
-
await previous;
|
|
659
|
-
try {
|
|
660
|
-
return await operation();
|
|
661
|
-
} finally {
|
|
662
|
-
release();
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
remember(query, statement) {
|
|
666
|
-
this.entries.delete(query);
|
|
667
|
-
this.entries.set(query, { statement });
|
|
668
|
-
this.trimToSize();
|
|
669
|
-
return statement;
|
|
670
|
-
}
|
|
671
|
-
evict(query) {
|
|
672
|
-
const entry = this.entries.get(query);
|
|
673
|
-
if (entry)
|
|
674
|
-
this.evictions += 1;
|
|
675
|
-
this.entries.delete(query);
|
|
676
|
-
destroyPreparedStatement(entry);
|
|
677
|
-
}
|
|
678
|
-
clear() {
|
|
679
|
-
for (const entry of this.entries.values()) {
|
|
680
|
-
destroyPreparedStatement(entry);
|
|
681
|
-
}
|
|
682
|
-
this.entries.clear();
|
|
683
|
-
}
|
|
684
|
-
evictOldest() {
|
|
685
|
-
const oldest = this.entries.keys().next();
|
|
686
|
-
if (!oldest.done) {
|
|
687
|
-
this.evict(oldest.value);
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
trimToSize() {
|
|
691
|
-
while (this.entries.size > this.size) {
|
|
692
|
-
this.evictOldest();
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
function getPreparedStatementCache(connection, size) {
|
|
697
|
-
const store = connection;
|
|
698
|
-
const existing = store[PREPARED_CACHE];
|
|
699
|
-
if (existing) {
|
|
700
|
-
existing.resize(size);
|
|
701
|
-
return existing;
|
|
702
|
-
}
|
|
703
|
-
const cache = new PreparedStatementCache(connection, size);
|
|
704
|
-
store[PREPARED_CACHE] = cache;
|
|
705
|
-
return cache;
|
|
706
|
-
}
|
|
707
648
|
function clearPreparedStatementCache(connection) {
|
|
708
649
|
const store = connection;
|
|
709
650
|
store[PREPARED_CACHE]?.clear();
|
|
710
651
|
}
|
|
711
|
-
function bindPreparedStatement(statement, values, types) {
|
|
712
|
-
if (values) {
|
|
713
|
-
if (types) {
|
|
714
|
-
statement.bind(values, types);
|
|
715
|
-
} else {
|
|
716
|
-
statement.bind(values);
|
|
717
|
-
}
|
|
718
|
-
return;
|
|
719
|
-
}
|
|
720
|
-
statement.clearBindings?.();
|
|
721
|
-
}
|
|
722
652
|
|
|
723
653
|
// src/client.ts
|
|
724
654
|
function isPool(client) {
|
|
@@ -884,14 +814,16 @@ var NO_NODE_API_PARAMS = {
|
|
|
884
814
|
values: undefined,
|
|
885
815
|
types: undefined
|
|
886
816
|
};
|
|
887
|
-
function toNodeApiParams(params) {
|
|
817
|
+
function toNodeApiParams(params, dateParams) {
|
|
888
818
|
if (params.length === 0) {
|
|
889
819
|
return NO_NODE_API_PARAMS;
|
|
890
820
|
}
|
|
821
|
+
const dateType = dateParams === "timestamptz" ? TIMESTAMPTZ2 : undefined;
|
|
891
822
|
const values = new Array(params.length);
|
|
892
823
|
let types;
|
|
893
824
|
for (let index = 0;index < params.length; index += 1) {
|
|
894
|
-
const
|
|
825
|
+
const param = params[index];
|
|
826
|
+
const value = toNodeApiValue(param, dateType && param instanceof Date ? dateType : undefined);
|
|
895
827
|
values[index] = value;
|
|
896
828
|
if (value !== null && (typeof value === "object" || typeof value === "number" && Number.isInteger(value) && (value > 2147483647 || value < -2147483648))) {
|
|
897
829
|
const typed = typedNodeApiParam(value);
|
|
@@ -1034,18 +966,35 @@ function wrapUnsupportedNodeApiTypeError(result, error) {
|
|
|
1034
966
|
}
|
|
1035
967
|
var TIMESTAMP_INFINITY_MICROS = 9223372036854775807n;
|
|
1036
968
|
var MAX_DATE_MILLIS = 8640000000000000n;
|
|
969
|
+
var MAX_SAFE_MICROS = BigInt(Number.MAX_SAFE_INTEGER);
|
|
970
|
+
var DATE_INFINITY_DAYS = 2147483647;
|
|
1037
971
|
function dateFromTimestampUnits(units, unitsPerMilli, typeName) {
|
|
1038
972
|
if (units === TIMESTAMP_INFINITY_MICROS)
|
|
1039
973
|
return "infinity";
|
|
1040
974
|
if (units === -TIMESTAMP_INFINITY_MICROS)
|
|
1041
975
|
return "-infinity";
|
|
976
|
+
if (unitsPerMilli === 1000n && units >= -MAX_SAFE_MICROS && units <= MAX_SAFE_MICROS) {
|
|
977
|
+
const micros = Number(units);
|
|
978
|
+
const wholeMillis = Math.floor(micros / 1000);
|
|
979
|
+
const date = new Date(wholeMillis);
|
|
980
|
+
const dropped = micros - wholeMillis * 1000;
|
|
981
|
+
if (dropped !== 0)
|
|
982
|
+
rememberDroppedMicros(date, dropped);
|
|
983
|
+
return date;
|
|
984
|
+
}
|
|
1042
985
|
let millis = units / unitsPerMilli;
|
|
1043
986
|
if (units % unitsPerMilli < 0n)
|
|
1044
987
|
millis -= 1n;
|
|
1045
988
|
if (millis < -MAX_DATE_MILLIS || millis > MAX_DATE_MILLIS) {
|
|
1046
989
|
throw new Error(`${typeName} value out of range for JS Date: ${units}`);
|
|
1047
990
|
}
|
|
1048
|
-
|
|
991
|
+
const date = new Date(Number(millis));
|
|
992
|
+
if (unitsPerMilli === 1000n) {
|
|
993
|
+
const dropped = units - millis * 1000n;
|
|
994
|
+
if (dropped !== 0n)
|
|
995
|
+
rememberDroppedMicros(date, Number(dropped));
|
|
996
|
+
}
|
|
997
|
+
return date;
|
|
1049
998
|
}
|
|
1050
999
|
var JSValueConverter = (value, type, converter) => {
|
|
1051
1000
|
if (value !== null && typeof value === "object") {
|
|
@@ -1055,13 +1004,20 @@ var JSValueConverter = (value, type, converter) => {
|
|
|
1055
1004
|
if (type.typeId === DuckDBTypeId2.TIMESTAMP_NS && "nanos" in value && typeof value.nanos === "bigint") {
|
|
1056
1005
|
return dateFromTimestampUnits(value.nanos, 1000000n, "TIMESTAMP_NS");
|
|
1057
1006
|
}
|
|
1007
|
+
if (type.typeId === DuckDBTypeId2.DATE && "days" in value) {
|
|
1008
|
+
if (value.days === DATE_INFINITY_DAYS)
|
|
1009
|
+
return "infinity";
|
|
1010
|
+
if (value.days === -DATE_INFINITY_DAYS)
|
|
1011
|
+
return "-infinity";
|
|
1012
|
+
}
|
|
1058
1013
|
}
|
|
1059
1014
|
return JSDuckDBValueConverter(value, type, converter);
|
|
1060
1015
|
};
|
|
1061
1016
|
var FLOORED_TIMESTAMP_TYPE_IDS = new Set([
|
|
1062
1017
|
DuckDBTypeId2.TIMESTAMP,
|
|
1063
1018
|
DuckDBTypeId2.TIMESTAMP_TZ,
|
|
1064
|
-
DuckDBTypeId2.TIMESTAMP_NS
|
|
1019
|
+
DuckDBTypeId2.TIMESTAMP_NS,
|
|
1020
|
+
DuckDBTypeId2.DATE
|
|
1065
1021
|
]);
|
|
1066
1022
|
function containsFlooredTimestamp(type) {
|
|
1067
1023
|
if (FLOORED_TIMESTAMP_TYPE_IDS.has(type.typeId))
|
|
@@ -1143,8 +1099,8 @@ function hasFetchAllChunks(result) {
|
|
|
1143
1099
|
async function materializeResultRows(result, exactDecimals = false, recursiveDecimals = false) {
|
|
1144
1100
|
if (hasFetchAllChunks(result)) {
|
|
1145
1101
|
const exactDecimalColumns = exactDecimals || recursiveDecimals ? findDecimalColumns(result) : [];
|
|
1146
|
-
const
|
|
1147
|
-
return exactDecimalColumns.length > 0 ? { columns: resolveResultColumns(result), rows
|
|
1102
|
+
const rows = await readResultChunkRows(result, exactDecimalColumns.length > 0, recursiveDecimals);
|
|
1103
|
+
return exactDecimalColumns.length > 0 ? { columns: resolveResultColumns(result), rows, exactDecimalColumns } : { columns: resolveResultColumns(result), rows };
|
|
1148
1104
|
}
|
|
1149
1105
|
const getRowsJson = typeof result.getRowsJson === "function" ? result.getRowsJson.bind(result) : undefined;
|
|
1150
1106
|
const rows = await readPreferredResult({
|
|
@@ -1155,36 +1111,6 @@ async function materializeResultRows(result, exactDecimals = false, recursiveDec
|
|
|
1155
1111
|
const columns = resolveResultColumns(result);
|
|
1156
1112
|
return { columns, rows };
|
|
1157
1113
|
}
|
|
1158
|
-
function isUnpreparableQueryError(error) {
|
|
1159
|
-
return error instanceof Error && /Cannot prepare multiple statements at once|No statement to prepare/.test(error.message);
|
|
1160
|
-
}
|
|
1161
|
-
async function executePreparedQuery(connection, query, { values, types }, cacheConfig, exactDecimals = false, recursiveDecimals = false) {
|
|
1162
|
-
const cache = getPreparedStatementCache(connection, cacheConfig.size);
|
|
1163
|
-
return await cache.runExclusive(async () => {
|
|
1164
|
-
assertConnectionAvailable(connection);
|
|
1165
|
-
let statement;
|
|
1166
|
-
try {
|
|
1167
|
-
statement = await cache.getOrPrepare(query);
|
|
1168
|
-
} catch (error) {
|
|
1169
|
-
if (!isUnpreparableQueryError(error)) {
|
|
1170
|
-
throw error;
|
|
1171
|
-
}
|
|
1172
|
-
assertConnectionAvailable(connection);
|
|
1173
|
-
const result = await connection.run(query, values, types);
|
|
1174
|
-
return await materializeResultRows(result, exactDecimals, recursiveDecimals);
|
|
1175
|
-
}
|
|
1176
|
-
assertConnectionAvailable(connection);
|
|
1177
|
-
try {
|
|
1178
|
-
bindPreparedStatement(statement, values, types);
|
|
1179
|
-
const result = await statement.run();
|
|
1180
|
-
cache.remember(query, statement);
|
|
1181
|
-
return await materializeResultRows(result, exactDecimals, recursiveDecimals);
|
|
1182
|
-
} catch (error) {
|
|
1183
|
-
cache.evict(query);
|
|
1184
|
-
throw error;
|
|
1185
|
-
}
|
|
1186
|
-
});
|
|
1187
|
-
}
|
|
1188
1114
|
async function* yieldChunkRows(fetchChunk, preferJson, exactDecimals = false) {
|
|
1189
1115
|
let useJson = preferJson;
|
|
1190
1116
|
let yieldedRows = false;
|
|
@@ -1227,10 +1153,7 @@ async function materializeRows(client, query, params, options = {}) {
|
|
|
1227
1153
|
return await materializePgDuckRows(connection, query, params);
|
|
1228
1154
|
}
|
|
1229
1155
|
return await runNodeApiOperation(connection, async () => {
|
|
1230
|
-
const nodeApiParams = toNodeApiParams(params);
|
|
1231
|
-
if (options.prepareCache && typeof connection.prepare === "function") {
|
|
1232
|
-
return await executePreparedQuery(connection, query, nodeApiParams, options.prepareCache, options.exactDecimals, options.decimalMode === "string");
|
|
1233
|
-
}
|
|
1156
|
+
const nodeApiParams = toNodeApiParams(params, options.dateParams);
|
|
1234
1157
|
const result = await connection.run(query, nodeApiParams.values, nodeApiParams.types);
|
|
1235
1158
|
return await materializeResultRows(result, options.exactDecimals, options.decimalMode === "string");
|
|
1236
1159
|
});
|
|
@@ -1375,7 +1298,7 @@ async function* streamRawBatches(client, query, params, options = {}) {
|
|
|
1375
1298
|
streamingConnections.add(connection);
|
|
1376
1299
|
let result;
|
|
1377
1300
|
try {
|
|
1378
|
-
const { values, types } = toNodeApiParams(params);
|
|
1301
|
+
const { values, types } = toNodeApiParams(params, options.dateParams);
|
|
1379
1302
|
result = await connection.stream(query, values, types);
|
|
1380
1303
|
yield* streamNodeApiResult(connection, result, rowsPerChunk, options.decimalMode === "string");
|
|
1381
1304
|
} finally {
|
|
@@ -1440,7 +1363,7 @@ async function executeArrowOnClient(client, query, params, options = {}) {
|
|
|
1440
1363
|
return mapRowsToColumnData(columns, rows);
|
|
1441
1364
|
}
|
|
1442
1365
|
return await runNodeApiOperation(connection, async () => {
|
|
1443
|
-
const { values, types } = toNodeApiParams(params);
|
|
1366
|
+
const { values, types } = toNodeApiParams(params, options.dateParams);
|
|
1444
1367
|
const result = await connection.run(query, values, types);
|
|
1445
1368
|
const maybeArrow = result.toArrow ?? result.getArrowTable;
|
|
1446
1369
|
if (typeof maybeArrow === "function") {
|
|
@@ -1453,23 +1376,23 @@ async function executeArrowOnClient(client, query, params, options = {}) {
|
|
|
1453
1376
|
const js = jsConverterFor(chunks);
|
|
1454
1377
|
const convert = (baseConverter) => {
|
|
1455
1378
|
const converter = options.decimalMode === "string" ? withExactDecimals(baseConverter, true, js) : baseConverter;
|
|
1456
|
-
const
|
|
1379
|
+
const values = columns.map(() => []);
|
|
1457
1380
|
for (const chunk of chunks) {
|
|
1458
1381
|
if (typeof chunk.convertColumns === "function") {
|
|
1459
1382
|
const converted = chunk.convertColumns(converter);
|
|
1460
1383
|
for (let index = 0;index < columns.length; index += 1) {
|
|
1461
1384
|
for (const value of converted[index] ?? [])
|
|
1462
|
-
|
|
1385
|
+
values[index].push(value);
|
|
1463
1386
|
}
|
|
1464
1387
|
} else {
|
|
1465
1388
|
for (const row of chunk.convertRows(converter)) {
|
|
1466
1389
|
for (let index = 0;index < columns.length; index += 1)
|
|
1467
|
-
|
|
1390
|
+
values[index].push(row[index]);
|
|
1468
1391
|
}
|
|
1469
1392
|
}
|
|
1470
1393
|
}
|
|
1471
1394
|
const data = {};
|
|
1472
|
-
columns.forEach((name, index) => assignOwnProperty(data, name,
|
|
1395
|
+
columns.forEach((name, index) => assignOwnProperty(data, name, values[index]));
|
|
1473
1396
|
return data;
|
|
1474
1397
|
};
|
|
1475
1398
|
try {
|
|
@@ -1493,6 +1416,96 @@ async function executeArrowOnClient(client, query, params, options = {}) {
|
|
|
1493
1416
|
});
|
|
1494
1417
|
});
|
|
1495
1418
|
}
|
|
1419
|
+
var APPEND_DEFAULT = Symbol("drizzle-duckdb:append-default");
|
|
1420
|
+
var INT64_MIN = -(2n ** 63n);
|
|
1421
|
+
var INT64_MAX = 2n ** 63n - 1n;
|
|
1422
|
+
function appendCell(appender, value, columnType) {
|
|
1423
|
+
if (value === APPEND_DEFAULT) {
|
|
1424
|
+
appender.appendDefault();
|
|
1425
|
+
return;
|
|
1426
|
+
}
|
|
1427
|
+
const nodeValue = toNodeApiValue(value, columnType);
|
|
1428
|
+
if (nodeValue === null) {
|
|
1429
|
+
appender.appendNull();
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
switch (typeof nodeValue) {
|
|
1433
|
+
case "string":
|
|
1434
|
+
appender.appendVarchar(nodeValue);
|
|
1435
|
+
return;
|
|
1436
|
+
case "number":
|
|
1437
|
+
if (!Number.isInteger(nodeValue)) {
|
|
1438
|
+
appender.appendDouble(nodeValue);
|
|
1439
|
+
} else if (nodeValue >= -2147483648 && nodeValue <= 2147483647) {
|
|
1440
|
+
appender.appendInteger(nodeValue);
|
|
1441
|
+
} else if (Number.isSafeInteger(nodeValue)) {
|
|
1442
|
+
appender.appendBigInt(BigInt(nodeValue));
|
|
1443
|
+
} else {
|
|
1444
|
+
appender.appendDouble(nodeValue);
|
|
1445
|
+
}
|
|
1446
|
+
return;
|
|
1447
|
+
case "boolean":
|
|
1448
|
+
appender.appendBoolean(nodeValue);
|
|
1449
|
+
return;
|
|
1450
|
+
case "bigint":
|
|
1451
|
+
if (nodeValue >= INT64_MIN && nodeValue <= INT64_MAX) {
|
|
1452
|
+
appender.appendBigInt(nodeValue);
|
|
1453
|
+
} else {
|
|
1454
|
+
appender.appendHugeInt(nodeValue);
|
|
1455
|
+
}
|
|
1456
|
+
return;
|
|
1457
|
+
default: {
|
|
1458
|
+
const typed = typedNodeApiParam(nodeValue);
|
|
1459
|
+
if (typed) {
|
|
1460
|
+
appender.appendValue(typed.value, typed.type);
|
|
1461
|
+
} else {
|
|
1462
|
+
appender.appendValue(nodeValue);
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
function quoteAppendIdentifier(name) {
|
|
1468
|
+
return `"${name.replace(/"/g, '""')}"`;
|
|
1469
|
+
}
|
|
1470
|
+
async function appendRowsOnClient(client, target, rowCount, bindRows) {
|
|
1471
|
+
return await withConnection(client, async (connection) => {
|
|
1472
|
+
if (!isNodeApiConnection(connection) || typeof connection.createAppender !== "function") {
|
|
1473
|
+
throw new Error("appendRows() needs an @duckdb/node-api connection. Use db.insert() with pg_duckdb clients.");
|
|
1474
|
+
}
|
|
1475
|
+
return await runNodeApiOperation(connection, async () => {
|
|
1476
|
+
const qualified = target.schema ? `${quoteAppendIdentifier(target.schema)}.${quoteAppendIdentifier(target.table)}` : quoteAppendIdentifier(target.table);
|
|
1477
|
+
const probe = await connection.run(`select * from ${qualified} limit 0`);
|
|
1478
|
+
const columnNames = probe.columnNames();
|
|
1479
|
+
const appender = await connection.createAppender(target.table, target.schema ?? null);
|
|
1480
|
+
let failed = true;
|
|
1481
|
+
try {
|
|
1482
|
+
if (appender.columnCount !== columnNames.length) {
|
|
1483
|
+
throw new Error(`appendRows() cannot append to ${qualified}: the table has ${columnNames.length} columns but its appender takes ${appender.columnCount}, as for tables with generated columns. Use db.insert() instead.`);
|
|
1484
|
+
}
|
|
1485
|
+
const columnTypes = columnNames.map((_, index) => appender.columnType(index));
|
|
1486
|
+
const valueAt = bindRows(columnNames);
|
|
1487
|
+
for (let rowIndex = 0;rowIndex < rowCount; rowIndex += 1) {
|
|
1488
|
+
for (let columnIndex = 0;columnIndex < columnTypes.length; columnIndex += 1) {
|
|
1489
|
+
appendCell(appender, valueAt(rowIndex, columnIndex), columnTypes[columnIndex]);
|
|
1490
|
+
}
|
|
1491
|
+
appender.endRow();
|
|
1492
|
+
}
|
|
1493
|
+
appender.flushSync();
|
|
1494
|
+
failed = false;
|
|
1495
|
+
return rowCount;
|
|
1496
|
+
} finally {
|
|
1497
|
+
try {
|
|
1498
|
+
if (failed)
|
|
1499
|
+
appender.clear?.();
|
|
1500
|
+
appender.closeSync();
|
|
1501
|
+
} catch (error) {
|
|
1502
|
+
if (!failed)
|
|
1503
|
+
throw error;
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
});
|
|
1507
|
+
});
|
|
1508
|
+
}
|
|
1496
1509
|
|
|
1497
1510
|
// src/driver.ts
|
|
1498
1511
|
import { DuckDBInstance as DuckDBInstance2 } from "@duckdb/node-api";
|
|
@@ -1508,7 +1521,7 @@ import {
|
|
|
1508
1521
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
1509
1522
|
import { entityKind as entityKind3, is as is4 } from "drizzle-orm/entity";
|
|
1510
1523
|
import { NoopLogger } from "drizzle-orm/logger";
|
|
1511
|
-
import { PgArray as PgArray2, PgTransaction } from "drizzle-orm/pg-core";
|
|
1524
|
+
import { getTableConfig, PgArray as PgArray2, PgTransaction } from "drizzle-orm/pg-core";
|
|
1512
1525
|
import { PgPreparedQuery, PgSession } from "drizzle-orm/pg-core/session";
|
|
1513
1526
|
import {
|
|
1514
1527
|
fillPlaceholders,
|
|
@@ -1599,7 +1612,7 @@ function arrayBoundsSql(name, arr) {
|
|
|
1599
1612
|
if (name === "array_lower") {
|
|
1600
1613
|
return `CASE WHEN array_length(${arr}) > 0 THEN 1 ELSE NULL END`;
|
|
1601
1614
|
}
|
|
1602
|
-
return arr.includes("(") ? `list_extract(list_transform(list_value(array_length(${arr})), __drizzle_array_length
|
|
1615
|
+
return arr.includes("(") ? `list_extract(list_transform(list_value(array_length(${arr})), lambda __drizzle_array_length: nullif(__drizzle_array_length, 0)), 1)` : `nullif(array_length(${arr}), 0)`;
|
|
1603
1616
|
}
|
|
1604
1617
|
function rewriteArrayBoundsText(query) {
|
|
1605
1618
|
let output = "";
|
|
@@ -1767,8 +1780,8 @@ function normalizeInet(value) {
|
|
|
1767
1780
|
if (address < 0n || address > 0xffffffffn) {
|
|
1768
1781
|
return value;
|
|
1769
1782
|
}
|
|
1770
|
-
const
|
|
1771
|
-
return `${formatIpv4(address)}${
|
|
1783
|
+
const suffix = value.mask !== 32 ? `/${value.mask}` : "";
|
|
1784
|
+
return `${formatIpv4(address)}${suffix}`;
|
|
1772
1785
|
}
|
|
1773
1786
|
const unsigned = address + (1n << 127n);
|
|
1774
1787
|
if (unsigned < 0n || unsigned >= 1n << 128n) {
|
|
@@ -1785,8 +1798,10 @@ function normalizeTimestampString(value, withTimezone) {
|
|
|
1785
1798
|
return value;
|
|
1786
1799
|
}
|
|
1787
1800
|
if (value instanceof Date) {
|
|
1788
|
-
const
|
|
1789
|
-
|
|
1801
|
+
const micros = dateSubMillisecondMicros(value);
|
|
1802
|
+
const digits = micros ? String(micros).padStart(3, "0").replace(/0+$/, "") : "";
|
|
1803
|
+
const iso = value.toISOString().replace("T", " ").replace("Z", digits);
|
|
1804
|
+
return withTimezone ? `${iso}+00` : iso;
|
|
1790
1805
|
}
|
|
1791
1806
|
if (typeof value === "string") {
|
|
1792
1807
|
const normalized = value.replace("T", " ");
|
|
@@ -1812,10 +1827,10 @@ function normalizeTimestamp(value, withTimezone) {
|
|
|
1812
1827
|
}
|
|
1813
1828
|
function normalizeDateString(value) {
|
|
1814
1829
|
if (value instanceof Date) {
|
|
1815
|
-
return value
|
|
1830
|
+
return dateStringFromDate(value);
|
|
1816
1831
|
}
|
|
1817
1832
|
if (typeof value === "string") {
|
|
1818
|
-
return value.slice(0, 10);
|
|
1833
|
+
return value === "infinity" || value === "-infinity" ? value : value.slice(0, 10);
|
|
1819
1834
|
}
|
|
1820
1835
|
return value;
|
|
1821
1836
|
}
|
|
@@ -1823,6 +1838,9 @@ function normalizeDateValue(value) {
|
|
|
1823
1838
|
if (value instanceof Date) {
|
|
1824
1839
|
return value;
|
|
1825
1840
|
}
|
|
1841
|
+
if (isInfiniteTimestamp(value)) {
|
|
1842
|
+
return value;
|
|
1843
|
+
}
|
|
1826
1844
|
if (typeof value === "string") {
|
|
1827
1845
|
return new Date(`${value.slice(0, 10)}T00:00:00Z`);
|
|
1828
1846
|
}
|
|
@@ -1840,13 +1858,7 @@ function normalizeTime(value) {
|
|
|
1840
1858
|
function normalizeInterval(value) {
|
|
1841
1859
|
if (value && typeof value === "object" && "days" in value && "months" in value) {
|
|
1842
1860
|
const { months, days, micros } = value;
|
|
1843
|
-
|
|
1844
|
-
if (micros && Number(micros) !== 0) {
|
|
1845
|
-
const seconds = Number(micros) / 1e6;
|
|
1846
|
-
return `${days} day${days === 1 ? "" : "s"} ${seconds} seconds`.trim();
|
|
1847
|
-
}
|
|
1848
|
-
return `${days} day${days === 1 ? "" : "s"}`;
|
|
1849
|
-
}
|
|
1861
|
+
return intervalToString(Number(months), Number(days), BigInt(micros ?? 0));
|
|
1850
1862
|
}
|
|
1851
1863
|
return value;
|
|
1852
1864
|
}
|
|
@@ -1859,20 +1871,13 @@ function decodesInet(decoder) {
|
|
|
1859
1871
|
}
|
|
1860
1872
|
return true;
|
|
1861
1873
|
}
|
|
1862
|
-
function
|
|
1874
|
+
function compileValueNormalizer(decoder) {
|
|
1863
1875
|
const inet = decodesInet(decoder) ? normalizeInet : (value) => value;
|
|
1864
1876
|
let normalize = (value) => value;
|
|
1865
1877
|
if (is(decoder, PgTimestampString)) {
|
|
1866
1878
|
normalize = (value) => normalizeTimestampString(value, decoder.withTimezone);
|
|
1867
1879
|
} else if (is(decoder, PgTimestamp)) {
|
|
1868
|
-
|
|
1869
|
-
if (value === null)
|
|
1870
|
-
return null;
|
|
1871
|
-
if (isInfiniteTimestamp(value))
|
|
1872
|
-
return value;
|
|
1873
|
-
const normalized = normalizeTimestamp(inet(value), decoder.withTimezone);
|
|
1874
|
-
return normalized instanceof Date ? normalized : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
|
|
1875
|
-
};
|
|
1880
|
+
normalize = (value) => isInfiniteTimestamp(value) ? value : normalizeTimestamp(value, decoder.withTimezone);
|
|
1876
1881
|
} else if (is(decoder, PgDateString)) {
|
|
1877
1882
|
normalize = normalizeDateString;
|
|
1878
1883
|
} else if (is(decoder, PgDate)) {
|
|
@@ -1882,7 +1887,30 @@ function compileValueMapper(decoder) {
|
|
|
1882
1887
|
} else if (is(decoder, PgInterval)) {
|
|
1883
1888
|
normalize = normalizeInterval;
|
|
1884
1889
|
}
|
|
1885
|
-
return (value) => value === null ? null :
|
|
1890
|
+
return (value) => value === null ? null : normalize(inet(value));
|
|
1891
|
+
}
|
|
1892
|
+
function compileValueMapper(decoder) {
|
|
1893
|
+
const normalize = compileValueNormalizer(decoder);
|
|
1894
|
+
if (is(decoder, PgTimestamp) || is(decoder, PgDate)) {
|
|
1895
|
+
return (value) => {
|
|
1896
|
+
const normalized = normalize(value);
|
|
1897
|
+
return normalized === null || normalized instanceof Date || isInfiniteTimestamp(normalized) ? normalized : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
|
|
1898
|
+
};
|
|
1899
|
+
}
|
|
1900
|
+
return (value) => {
|
|
1901
|
+
const normalized = normalize(value);
|
|
1902
|
+
return normalized === null ? null : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
|
|
1903
|
+
};
|
|
1904
|
+
}
|
|
1905
|
+
function compileRelationalRowNormalizer(selection) {
|
|
1906
|
+
const normalizers = selection.map((item) => item.isJson ? undefined : compileValueNormalizer(resolveFieldDecoder(item.field)));
|
|
1907
|
+
return (row) => {
|
|
1908
|
+
for (let index = 0;index < normalizers.length; index += 1) {
|
|
1909
|
+
const normalize = normalizers[index];
|
|
1910
|
+
if (normalize)
|
|
1911
|
+
row[index] = normalize(row[index]);
|
|
1912
|
+
}
|
|
1913
|
+
};
|
|
1886
1914
|
}
|
|
1887
1915
|
function assignResultPath(result, path, value) {
|
|
1888
1916
|
if (path.length === 0) {
|
|
@@ -2029,8 +2057,17 @@ function arrayBoundsExpr(functionName, arrayExpr) {
|
|
|
2029
2057
|
call("list_value", [lengthExpr]),
|
|
2030
2058
|
{
|
|
2031
2059
|
type: "binary_expr",
|
|
2032
|
-
operator: "
|
|
2033
|
-
left:
|
|
2060
|
+
operator: ":",
|
|
2061
|
+
left: {
|
|
2062
|
+
type: "column_ref",
|
|
2063
|
+
table: null,
|
|
2064
|
+
column: {
|
|
2065
|
+
expr: {
|
|
2066
|
+
type: "default",
|
|
2067
|
+
value: "lambda __drizzle_array_length"
|
|
2068
|
+
}
|
|
2069
|
+
}
|
|
2070
|
+
},
|
|
2034
2071
|
right: nullIf(length)
|
|
2035
2072
|
}
|
|
2036
2073
|
]),
|
|
@@ -2969,20 +3006,20 @@ function scanSqlTokens(query) {
|
|
|
2969
3006
|
};
|
|
2970
3007
|
const readQuoted = (start, quote) => {
|
|
2971
3008
|
const backslashEscapes = isEscapeStringAt(query, start);
|
|
2972
|
-
let
|
|
2973
|
-
while (
|
|
2974
|
-
if (backslashEscapes && query[
|
|
2975
|
-
|
|
3009
|
+
let index = start + 1;
|
|
3010
|
+
while (index < query.length) {
|
|
3011
|
+
if (backslashEscapes && query[index] === "\\") {
|
|
3012
|
+
index += 2;
|
|
2976
3013
|
continue;
|
|
2977
3014
|
}
|
|
2978
|
-
if (query[
|
|
2979
|
-
if (query[
|
|
2980
|
-
|
|
3015
|
+
if (query[index] === quote) {
|
|
3016
|
+
if (query[index + 1] === quote) {
|
|
3017
|
+
index += 2;
|
|
2981
3018
|
continue;
|
|
2982
3019
|
}
|
|
2983
|
-
return
|
|
3020
|
+
return index;
|
|
2984
3021
|
}
|
|
2985
|
-
|
|
3022
|
+
index += 1;
|
|
2986
3023
|
}
|
|
2987
3024
|
return -1;
|
|
2988
3025
|
};
|
|
@@ -3134,8 +3171,8 @@ function transformSQL(query, options = {}) {
|
|
|
3134
3171
|
function rewriteRemainingArrayBounds(result) {
|
|
3135
3172
|
if (!ARRAY_BOUNDS_PATTERN.test(result.sql))
|
|
3136
3173
|
return result;
|
|
3137
|
-
const
|
|
3138
|
-
return
|
|
3174
|
+
const sql = rewriteArrayBoundsText(result.sql);
|
|
3175
|
+
return sql === result.sql ? result : { sql, transformed: true };
|
|
3139
3176
|
}
|
|
3140
3177
|
function transformWithAst(query, {
|
|
3141
3178
|
needsArrayTransform,
|
|
@@ -3185,6 +3222,10 @@ function transformWithAst(query, {
|
|
|
3185
3222
|
// src/dialect.ts
|
|
3186
3223
|
var PG_JSON_UNSUPPORTED_MESSAGE = "Pg JSON/JSONB columns are not supported in DuckDB. Replace them with duckDbJson() to use DuckDB's native JSON type.";
|
|
3187
3224
|
var DUCKDB_SCALAR_COLUMN_TYPING = "duckdb:scalar";
|
|
3225
|
+
var relationalSelections = new WeakMap;
|
|
3226
|
+
function relationalSelectionOf(query) {
|
|
3227
|
+
return relationalSelections.get(query);
|
|
3228
|
+
}
|
|
3188
3229
|
var DUCKDB_ARRAY_COLUMN_TYPING = "duckdb:array";
|
|
3189
3230
|
var MIGRATION_CONFLICT_MAX_ATTEMPTS = 10;
|
|
3190
3231
|
var MIGRATION_RETRY_BASE_DELAY_MS = 20;
|
|
@@ -3233,6 +3274,7 @@ var RELATIONAL_JSON_CHUNKS = new Map([
|
|
|
3233
3274
|
["), '[]'::json)", ")), '[]'::json)"]
|
|
3234
3275
|
]);
|
|
3235
3276
|
var RELATIONAL_LIMIT_CHUNKS = new Set([" limit ", " offset "]);
|
|
3277
|
+
var EXACT_BIGINT_COLUMN_TYPES = new Set(["PgBigInt64", "PgBigSerial64"]);
|
|
3236
3278
|
function rewriteRelationalJsonChunks(chunks, exactDecimals = false) {
|
|
3237
3279
|
for (let index = 0;index < chunks.length; index += 1) {
|
|
3238
3280
|
const chunk = chunks[index];
|
|
@@ -3241,10 +3283,17 @@ function rewriteRelationalJsonChunks(chunks, exactDecimals = false) {
|
|
|
3241
3283
|
const replacement = value === undefined ? undefined : RELATIONAL_JSON_CHUNKS.get(value);
|
|
3242
3284
|
const next = chunks[index + 1];
|
|
3243
3285
|
if (replacement) {
|
|
3244
|
-
if (
|
|
3286
|
+
if (value === "json_build_array(" && is2(next, SQL2)) {
|
|
3245
3287
|
const fields = next.queryChunks.map((item) => {
|
|
3246
|
-
if (!is2(item, Column2))
|
|
3288
|
+
if (!is2(item, Column2)) {
|
|
3247
3289
|
return item;
|
|
3290
|
+
}
|
|
3291
|
+
if (EXACT_BIGINT_COLUMN_TYPES.has(item.columnType)) {
|
|
3292
|
+
return sql2`cast(${item} as VARCHAR)`;
|
|
3293
|
+
}
|
|
3294
|
+
if (!exactDecimals && !item.columnType.startsWith("PgNumeric")) {
|
|
3295
|
+
return item;
|
|
3296
|
+
}
|
|
3248
3297
|
const originalType = item.getSQLType();
|
|
3249
3298
|
const exactType = decimalStringType(originalType);
|
|
3250
3299
|
return exactType === originalType ? item : sql2`cast(${item} as ${sql2.raw(exactType)})`;
|
|
@@ -3402,6 +3451,7 @@ class DuckDBDialect extends PgDialect {
|
|
|
3402
3451
|
const result = super.buildRelationalQueryWithoutPK(config);
|
|
3403
3452
|
if (!config.nestedQueryRelation && is2(result.sql, SQL2)) {
|
|
3404
3453
|
rewriteRelationalJsonChunks(result.sql.queryChunks, this.options.decimalMode === "string");
|
|
3454
|
+
relationalSelections.set(result.sql, result.selection);
|
|
3405
3455
|
}
|
|
3406
3456
|
return result;
|
|
3407
3457
|
}
|
|
@@ -3410,10 +3460,14 @@ class DuckDBDialect extends PgDialect {
|
|
|
3410
3460
|
const transformed = transformSQL(result.sql, {
|
|
3411
3461
|
qualifyJoinColumns: this.options.qualifyRawJoinColumns === true
|
|
3412
3462
|
});
|
|
3413
|
-
|
|
3463
|
+
const query = {
|
|
3414
3464
|
...result,
|
|
3415
3465
|
sql: transformed.sql
|
|
3416
3466
|
};
|
|
3467
|
+
const selection = relationalSelections.get(sqlObj);
|
|
3468
|
+
if (selection)
|
|
3469
|
+
relationalSelections.set(query, selection);
|
|
3470
|
+
return query;
|
|
3417
3471
|
}
|
|
3418
3472
|
}
|
|
3419
3473
|
|
|
@@ -3537,8 +3591,8 @@ function qualifySubqueryFields(select) {
|
|
|
3537
3591
|
const internals = select;
|
|
3538
3592
|
const baseAs = internals.as.bind(internals);
|
|
3539
3593
|
internals.as = (alias) => {
|
|
3540
|
-
const { sql
|
|
3541
|
-
return new Proxy(new Subquery3(
|
|
3594
|
+
const { sql, usedTables } = baseAs(alias)._;
|
|
3595
|
+
return new Proxy(new Subquery3(sql, exposeSubqueryFields(internals.config.fields), alias, false, usedTables), new SelectionProxyHandler({
|
|
3542
3596
|
alias,
|
|
3543
3597
|
sqlAliasedBehavior: "alias",
|
|
3544
3598
|
sqlBehavior: "error"
|
|
@@ -3853,9 +3907,12 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
|
|
|
3853
3907
|
onStatementError;
|
|
3854
3908
|
decimalMode;
|
|
3855
3909
|
waitForTransactions;
|
|
3910
|
+
relationalSelection;
|
|
3911
|
+
dateParams;
|
|
3856
3912
|
static [entityKind3] = "DuckDBPreparedQuery";
|
|
3857
3913
|
resultMapper;
|
|
3858
|
-
|
|
3914
|
+
relationalRowNormalizer;
|
|
3915
|
+
constructor(client, dialect, queryString, params, logger, fields, _isResponseInArrayMode, customResultMapper, rejectStringArrayLiterals, prepareCache, queryMetadata, cacheConfig, warnOnStringArrayLiteral, scalarParamIndexes, onStatementError, decimalMode = "number", waitForTransactions = false, relationalSelection, dateParams) {
|
|
3859
3916
|
super(...[
|
|
3860
3917
|
{ sql: queryString, params },
|
|
3861
3918
|
undefined,
|
|
@@ -3877,6 +3934,8 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
|
|
|
3877
3934
|
this.onStatementError = onStatementError;
|
|
3878
3935
|
this.decimalMode = decimalMode;
|
|
3879
3936
|
this.waitForTransactions = waitForTransactions;
|
|
3937
|
+
this.relationalSelection = relationalSelection;
|
|
3938
|
+
this.dateParams = dateParams;
|
|
3880
3939
|
}
|
|
3881
3940
|
async execute(placeholderValues = {}) {
|
|
3882
3941
|
try {
|
|
@@ -3902,23 +3961,32 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
|
|
|
3902
3961
|
});
|
|
3903
3962
|
const { fields, joinsNotNullableMap, customResultMapper } = this;
|
|
3904
3963
|
if (!fields && !customResultMapper) {
|
|
3905
|
-
const
|
|
3964
|
+
const rows = await executeOnClient(this.client, queryString, params, {
|
|
3906
3965
|
prepareCache: this.prepareCache,
|
|
3907
|
-
decimalMode: this.decimalMode
|
|
3966
|
+
decimalMode: this.decimalMode,
|
|
3967
|
+
dateParams: this.dateParams
|
|
3908
3968
|
});
|
|
3909
|
-
return
|
|
3969
|
+
return rows;
|
|
3910
3970
|
}
|
|
3971
|
+
const relationalSelection = customResultMapper ? this.relationalSelection : undefined;
|
|
3911
3972
|
const { rows, exactDecimalColumns } = await executeArraysOnClient(this.client, queryString, params, {
|
|
3912
3973
|
prepareCache: this.prepareCache,
|
|
3913
|
-
exactDecimals: !customResultMapper,
|
|
3914
|
-
decimalMode: this.decimalMode
|
|
3974
|
+
exactDecimals: !customResultMapper || relationalSelection !== undefined,
|
|
3975
|
+
decimalMode: this.decimalMode,
|
|
3976
|
+
dateParams: this.dateParams
|
|
3915
3977
|
});
|
|
3978
|
+
const decimalFields = relationalSelection ?? fields;
|
|
3979
|
+
if (exactDecimalColumns && decimalFields && this.decimalMode !== "string") {
|
|
3980
|
+
restoreNonNumericDecimals(rows, decimalFields, exactDecimalColumns);
|
|
3981
|
+
}
|
|
3916
3982
|
if (customResultMapper) {
|
|
3983
|
+
if (relationalSelection) {
|
|
3984
|
+
this.relationalRowNormalizer ??= compileRelationalRowNormalizer(relationalSelection);
|
|
3985
|
+
for (const row of rows)
|
|
3986
|
+
this.relationalRowNormalizer(row);
|
|
3987
|
+
}
|
|
3917
3988
|
return customResultMapper(rows);
|
|
3918
3989
|
}
|
|
3919
|
-
if (exactDecimalColumns && this.decimalMode !== "string") {
|
|
3920
|
-
restoreNonNumericDecimals(rows, fields, exactDecimalColumns);
|
|
3921
|
-
}
|
|
3922
3990
|
this.resultMapper ??= compileResultMapper(fields, joinsNotNullableMap);
|
|
3923
3991
|
return rows.map(this.resultMapper);
|
|
3924
3992
|
}
|
|
@@ -3959,7 +4027,7 @@ class DuckDBSession extends PgSession {
|
|
|
3959
4027
|
};
|
|
3960
4028
|
}
|
|
3961
4029
|
prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
|
|
3962
|
-
return new DuckDBPreparedQuery(this.client, this.dialect, query.sql, bindEmptyArrays(query.params, query.params, query.typings), this.logger, fields, isResponseInArrayMode, customResultMapper, this.rejectStringArrayLiterals, this.prepareCache, queryMetadata, cacheConfig, this.rejectStringArrayLiterals ? undefined : this.warnOnStringArrayLiteral, getScalarParamIndexes(query.params, query.typings), this.statementFailures || this.waitsForTransactions() ? this.recordStatementFailure : undefined, this.options.decimalMode, this.waitsForTransactions());
|
|
4030
|
+
return new DuckDBPreparedQuery(this.client, this.dialect, query.sql, bindEmptyArrays(query.params, query.params, query.typings), this.logger, fields, isResponseInArrayMode, customResultMapper, this.rejectStringArrayLiterals, this.prepareCache, queryMetadata, cacheConfig, this.rejectStringArrayLiterals ? undefined : this.warnOnStringArrayLiteral, getScalarParamIndexes(query.params, query.typings), this.statementFailures || this.waitsForTransactions() ? this.recordStatementFailure : undefined, this.options.decimalMode, this.waitsForTransactions(), relationalSelectionOf(query), this.options.dateParams);
|
|
3963
4031
|
}
|
|
3964
4032
|
waitsForTransactions() {
|
|
3965
4033
|
return !this.statementFailures && !isPool(this.client);
|
|
@@ -4127,6 +4195,7 @@ query: ${query}`, []);
|
|
|
4127
4195
|
const { sql: queryString, params } = this.prepareQueryExecution(query);
|
|
4128
4196
|
return this.trackStreamFailures(this.afterOpenTransaction(executeInBatches(this.client, queryString, params, {
|
|
4129
4197
|
decimalMode: this.options.decimalMode,
|
|
4198
|
+
dateParams: this.options.dateParams,
|
|
4130
4199
|
...options
|
|
4131
4200
|
})));
|
|
4132
4201
|
}
|
|
@@ -4134,6 +4203,7 @@ query: ${query}`, []);
|
|
|
4134
4203
|
const { sql: queryString, params } = this.prepareQueryExecution(query);
|
|
4135
4204
|
return this.trackStreamFailures(this.afterOpenTransaction(executeInBatchesRaw(this.client, queryString, params, {
|
|
4136
4205
|
decimalMode: this.options.decimalMode,
|
|
4206
|
+
dateParams: this.options.dateParams,
|
|
4137
4207
|
...options
|
|
4138
4208
|
})));
|
|
4139
4209
|
}
|
|
@@ -4144,7 +4214,65 @@ query: ${query}`, []);
|
|
|
4144
4214
|
}
|
|
4145
4215
|
try {
|
|
4146
4216
|
return await executeArrowOnClient(this.client, queryString, params, {
|
|
4147
|
-
decimalMode: this.options.decimalMode
|
|
4217
|
+
decimalMode: this.options.decimalMode,
|
|
4218
|
+
dateParams: this.options.dateParams
|
|
4219
|
+
});
|
|
4220
|
+
} catch (error) {
|
|
4221
|
+
this.recordStatementFailure(error);
|
|
4222
|
+
throw error;
|
|
4223
|
+
}
|
|
4224
|
+
}
|
|
4225
|
+
async appendRows(table, rows) {
|
|
4226
|
+
if (rows.length === 0) {
|
|
4227
|
+
return 0;
|
|
4228
|
+
}
|
|
4229
|
+
const { name, schema, columns } = getTableConfig(table);
|
|
4230
|
+
const keyOf = new Map;
|
|
4231
|
+
for (const [key, column] of Object.entries(table)) {
|
|
4232
|
+
keyOf.set(column, key);
|
|
4233
|
+
}
|
|
4234
|
+
const byName = new Map(columns.map((column) => [column.name, column]));
|
|
4235
|
+
if (this.waitsForTransactions()) {
|
|
4236
|
+
await waitForOpenTransaction(this.client);
|
|
4237
|
+
}
|
|
4238
|
+
try {
|
|
4239
|
+
return await appendRowsOnClient(this.client, { table: name, schema }, rows.length, (columnNames) => {
|
|
4240
|
+
const known = new Set(columnNames);
|
|
4241
|
+
for (const column of columns) {
|
|
4242
|
+
if (!known.has(column.name)) {
|
|
4243
|
+
throw new Error(`appendRows(): column "${column.name}" is not a column of table "${name}".`);
|
|
4244
|
+
}
|
|
4245
|
+
}
|
|
4246
|
+
const plan = columnNames.map((columnName) => {
|
|
4247
|
+
const column = byName.get(columnName);
|
|
4248
|
+
return column ? { column, key: keyOf.get(column) ?? column.name } : undefined;
|
|
4249
|
+
});
|
|
4250
|
+
return (rowIndex, columnIndex) => {
|
|
4251
|
+
const entry = plan[columnIndex];
|
|
4252
|
+
if (!entry)
|
|
4253
|
+
return APPEND_DEFAULT;
|
|
4254
|
+
const { column, key } = entry;
|
|
4255
|
+
let value = rows[rowIndex][key];
|
|
4256
|
+
if (value === undefined) {
|
|
4257
|
+
if (column.defaultFn !== undefined) {
|
|
4258
|
+
value = column.defaultFn();
|
|
4259
|
+
} else if (column.onUpdateFn !== undefined) {
|
|
4260
|
+
value = column.onUpdateFn();
|
|
4261
|
+
} else {
|
|
4262
|
+
return APPEND_DEFAULT;
|
|
4263
|
+
}
|
|
4264
|
+
}
|
|
4265
|
+
if (value === null)
|
|
4266
|
+
return null;
|
|
4267
|
+
if (is4(value, SQL4)) {
|
|
4268
|
+
throw new Error(`appendRows(): column "${column.name}" got a SQL expression, which the appender cannot evaluate. Use db.insert() for such rows.`);
|
|
4269
|
+
}
|
|
4270
|
+
const driverValue = column.mapToDriverValue(value);
|
|
4271
|
+
if (is4(driverValue, SQL4)) {
|
|
4272
|
+
throw new Error(`appendRows(): column "${column.name}" encodes this value as a SQL literal (a struct, union, empty list, or a list of objects or blobs), which the appender cannot take. Use db.insert() for such rows.`);
|
|
4273
|
+
}
|
|
4274
|
+
return driverValue;
|
|
4275
|
+
};
|
|
4148
4276
|
});
|
|
4149
4277
|
} catch (error) {
|
|
4150
4278
|
this.recordStatementFailure(error);
|
|
@@ -4225,6 +4353,9 @@ class DuckDBTransaction extends PgTransaction {
|
|
|
4225
4353
|
executeArrow(query) {
|
|
4226
4354
|
return this.getInternals().session.executeArrow(query);
|
|
4227
4355
|
}
|
|
4356
|
+
appendRows(table, rows) {
|
|
4357
|
+
return this.getInternals().session.appendRows(table, rows);
|
|
4358
|
+
}
|
|
4228
4359
|
async transaction(transaction) {
|
|
4229
4360
|
const internals = this.getInternals();
|
|
4230
4361
|
const savepoint = `drizzle_savepoint_${this.nestedIndex + 1}`;
|
|
@@ -4324,8 +4455,12 @@ function resolvePoolSize(pool) {
|
|
|
4324
4455
|
return false;
|
|
4325
4456
|
if (pool === undefined)
|
|
4326
4457
|
return DEFAULT_POOL_SIZE;
|
|
4327
|
-
if (typeof pool === "string")
|
|
4328
|
-
|
|
4458
|
+
if (typeof pool === "string") {
|
|
4459
|
+
if (!Object.hasOwn(POOL_PRESETS, pool)) {
|
|
4460
|
+
throw new Error(`Unknown pool preset ${JSON.stringify(pool)}. Expected one of: ${Object.keys(POOL_PRESETS).join(", ")}.`);
|
|
4461
|
+
}
|
|
4462
|
+
return POOL_PRESETS[pool];
|
|
4463
|
+
}
|
|
4329
4464
|
return resolveSize(pool.size);
|
|
4330
4465
|
}
|
|
4331
4466
|
var POOL_CLOSED_MESSAGE = "DuckDB connection pool is closed";
|
|
@@ -4557,9 +4692,9 @@ function createDuckDBConnectionPool(instance, options = {}) {
|
|
|
4557
4692
|
if (waiting.length >= maxWaitingRequests) {
|
|
4558
4693
|
throw new Error(`DuckDB connection pool queue is full (max ${maxWaitingRequests} waiting requests)`);
|
|
4559
4694
|
}
|
|
4560
|
-
return await new Promise((
|
|
4695
|
+
return await new Promise((resolve, reject) => {
|
|
4561
4696
|
const waiter = {
|
|
4562
|
-
resolve
|
|
4697
|
+
resolve,
|
|
4563
4698
|
reject,
|
|
4564
4699
|
timeoutId: undefined,
|
|
4565
4700
|
queuedAt: Date.now()
|
|
@@ -4948,7 +5083,8 @@ class DuckDBDriver {
|
|
|
4948
5083
|
rejectStringArrayLiterals: this.options.rejectStringArrayLiterals,
|
|
4949
5084
|
arrayLiteralWarning: this.options.arrayLiteralWarning,
|
|
4950
5085
|
prepareCache: this.options.prepareCache,
|
|
4951
|
-
decimalMode: this.options.decimalMode
|
|
5086
|
+
decimalMode: this.options.decimalMode,
|
|
5087
|
+
dateParams: this.options.dateParams
|
|
4952
5088
|
});
|
|
4953
5089
|
}
|
|
4954
5090
|
}
|
|
@@ -5010,6 +5146,15 @@ function isNodePostgresPool(value) {
|
|
|
5010
5146
|
const candidate = value;
|
|
5011
5147
|
return typeof candidate.connect === "function" && typeof candidate.query === "function" && typeof candidate.acquire !== "function" && typeof candidate.totalCount === "number" && typeof candidate.idleCount === "number";
|
|
5012
5148
|
}
|
|
5149
|
+
var PREPARE_CACHE_WARNING = "The prepareCache option is deprecated and ignored. DuckDB plans a prepared statement from the table statistics at prepare time, so a cached statement can return wrong results after the data changes. Remove the option. It will be removed in the next major version.";
|
|
5150
|
+
var hasWarnedPrepareCache = false;
|
|
5151
|
+
function warnPrepareCacheIgnored() {
|
|
5152
|
+
if (hasWarnedPrepareCache) {
|
|
5153
|
+
return;
|
|
5154
|
+
}
|
|
5155
|
+
hasWarnedPrepareCache = true;
|
|
5156
|
+
console.warn(PREPARE_CACHE_WARNING);
|
|
5157
|
+
}
|
|
5013
5158
|
function createFromClient(client, config = {}, instance) {
|
|
5014
5159
|
let finalClient = isNodePostgresPool(client) ? createPgDuckConnectionPool(client) : client;
|
|
5015
5160
|
if (config.ducklake) {
|
|
@@ -5025,6 +5170,9 @@ function createFromClient(client, config = {}, instance) {
|
|
|
5025
5170
|
decimalMode: config.decimalMode
|
|
5026
5171
|
});
|
|
5027
5172
|
const prepareCache = resolvePrepareCacheOption(config.prepareCache);
|
|
5173
|
+
if (prepareCache) {
|
|
5174
|
+
warnPrepareCacheIgnored();
|
|
5175
|
+
}
|
|
5028
5176
|
const logger = config.logger === true ? new DefaultLogger : config.logger || undefined;
|
|
5029
5177
|
let schema;
|
|
5030
5178
|
if (config.schema) {
|
|
@@ -5040,7 +5188,8 @@ function createFromClient(client, config = {}, instance) {
|
|
|
5040
5188
|
rejectStringArrayLiterals: config.rejectStringArrayLiterals,
|
|
5041
5189
|
arrayLiteralWarning: config.arrayLiteralWarning,
|
|
5042
5190
|
prepareCache,
|
|
5043
|
-
decimalMode: config.decimalMode
|
|
5191
|
+
decimalMode: config.decimalMode,
|
|
5192
|
+
dateParams: config.dateParams
|
|
5044
5193
|
});
|
|
5045
5194
|
const session = driver.createSession(schema);
|
|
5046
5195
|
const db = new DuckDBDatabase(dialect, session, schema, finalClient, instance);
|
|
@@ -5163,6 +5312,9 @@ class DuckDBDatabase extends PgDatabase {
|
|
|
5163
5312
|
executeArrow(query) {
|
|
5164
5313
|
return this.session.executeArrow(query);
|
|
5165
5314
|
}
|
|
5315
|
+
appendRows(table, rows) {
|
|
5316
|
+
return this.session.appendRows(table, rows);
|
|
5317
|
+
}
|
|
5166
5318
|
async transaction(transaction, config) {
|
|
5167
5319
|
return await this.session.transaction(transaction, config);
|
|
5168
5320
|
}
|
|
@@ -5713,26 +5865,24 @@ function emitConstraints(plan, plans, tableIdentifiers) {
|
|
|
5713
5865
|
const param = allocateIdentifier("t", referencedTables);
|
|
5714
5866
|
const ownColumns = (columns) => columns.map((col) => memberAccess(param, plan.columnProperties.get(col) ?? columnProperty(col))).join(", ");
|
|
5715
5867
|
const entries = [];
|
|
5716
|
-
const usedKeys = new Set;
|
|
5717
5868
|
for (const constraint of constraints) {
|
|
5718
|
-
const key = allocateIdentifier(toIdentifier(constraint.name || `${table.name}_constraint`), usedKeys);
|
|
5719
5869
|
const name = JSON.stringify(constraint.name);
|
|
5720
5870
|
if (constraint.type === "PRIMARY KEY") {
|
|
5721
|
-
entries.push(
|
|
5871
|
+
entries.push(`primaryKey({ columns: [${ownColumns(constraint.columns)}], name: ${name} })`);
|
|
5722
5872
|
} else if (constraint.type === "UNIQUE") {
|
|
5723
|
-
entries.push(
|
|
5873
|
+
entries.push(`unique(${name}).on(${ownColumns(constraint.columns)})`);
|
|
5724
5874
|
} else {
|
|
5725
5875
|
const target = resolveTarget(constraint);
|
|
5726
5876
|
const targetVar = target.key === selfKey ? param : target.identifier;
|
|
5727
5877
|
const foreignColumns = constraint.referencedTable.columns.map((col) => memberAccess(targetVar, target.columnProperties?.get(col) ?? columnProperty(col))).join(", ");
|
|
5728
|
-
entries.push(
|
|
5878
|
+
entries.push(`foreignKey({ columns: [${ownColumns(constraint.columns)}], foreignColumns: [${foreignColumns}], name: ${name} })`);
|
|
5729
5879
|
}
|
|
5730
5880
|
}
|
|
5731
|
-
const lines = [`(${param}) =>
|
|
5881
|
+
const lines = [`(${param}) => [`];
|
|
5732
5882
|
for (const entry of entries) {
|
|
5733
5883
|
lines.push(` ${entry},`);
|
|
5734
5884
|
}
|
|
5735
|
-
lines.push("
|
|
5885
|
+
lines.push("]");
|
|
5736
5886
|
return lines.join(`
|
|
5737
5887
|
`);
|
|
5738
5888
|
}
|
|
@@ -5949,7 +6099,7 @@ function nestedValueType(raw) {
|
|
|
5949
6099
|
return `Array<${nestedValueType(array[1])} | null>`;
|
|
5950
6100
|
if (type.startsWith("STRUCT")) {
|
|
5951
6101
|
const fields = parseStructFields(type.replace(/^STRUCT\s*\(/i, "").replace(/\)$/, ""));
|
|
5952
|
-
return `{ ${fields.map(({ name, type
|
|
6102
|
+
return `{ ${fields.map(({ name, type }) => `${JSON.stringify(name)}: ${nestedValueType(type)} | null`).join("; ")} }`;
|
|
5953
6103
|
}
|
|
5954
6104
|
if (type.startsWith("MAP(")) {
|
|
5955
6105
|
const { keyType, valueType } = parseMapTypes(type);
|
|
@@ -6017,14 +6167,14 @@ function mapDuckDbType(column, imports, options) {
|
|
|
6017
6167
|
imports.pgCore.add("numeric");
|
|
6018
6168
|
const precision = column.numericPrecision;
|
|
6019
6169
|
const scale = column.numericScale;
|
|
6020
|
-
const
|
|
6170
|
+
const options = [];
|
|
6021
6171
|
if (precision !== null && precision !== undefined) {
|
|
6022
|
-
|
|
6172
|
+
options.push(`precision: ${precision}`);
|
|
6023
6173
|
}
|
|
6024
6174
|
if (scale !== null && scale !== undefined) {
|
|
6025
|
-
|
|
6175
|
+
options.push(`scale: ${scale}`);
|
|
6026
6176
|
}
|
|
6027
|
-
const suffix =
|
|
6177
|
+
const suffix = options.length ? `, { ${options.join(", ")} }` : "";
|
|
6028
6178
|
return {
|
|
6029
6179
|
builder: `numeric(${columnName(column.name)}${suffix})`,
|
|
6030
6180
|
defaultLiterals: STRING_DEFAULTS
|
|
@@ -6325,9 +6475,9 @@ function uniqueSchemas(tables) {
|
|
|
6325
6475
|
}
|
|
6326
6476
|
function renderImports(imports, importBasePath) {
|
|
6327
6477
|
const lines = [];
|
|
6328
|
-
const
|
|
6329
|
-
if (
|
|
6330
|
-
lines.push(`import { ${
|
|
6478
|
+
const drizzle = [...imports.drizzle];
|
|
6479
|
+
if (drizzle.length) {
|
|
6480
|
+
lines.push(`import { ${drizzle.sort().join(", ")} } from 'drizzle-orm';`);
|
|
6331
6481
|
}
|
|
6332
6482
|
const pgCore = [...imports.pgCore];
|
|
6333
6483
|
if (pgCore.length) {
|