@leonardovida-md/drizzle-neo-duckdb 2.3.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.
@@ -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";
@@ -121,6 +122,14 @@ function timestampStringToDateInput(value, parts = parseTimestampString(value))
121
122
  const normalized = !value.includes("T") && value.includes(" ") ? value.replace(" ", "T") : value;
122
123
  return normalized.endsWith("Z") || /[+-]\d{2}:?\d{2}$/.test(normalized) ? normalized : `${normalized}Z`;
123
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
+ }
124
133
  function pluralUnit(value, unit) {
125
134
  return `${value} ${unit}${Math.abs(value) === 1 ? "" : "s"}`;
126
135
  }
@@ -636,120 +645,10 @@ function parsePgArrayLiteral(value) {
636
645
 
637
646
  // src/prepared-statement-cache.ts
638
647
  var PREPARED_CACHE = Symbol.for("drizzle-duckdb:prepared-cache");
639
- function destroyPreparedStatement(entry) {
640
- if (!entry)
641
- return;
642
- try {
643
- entry.statement.destroySync();
644
- } catch {}
645
- }
646
-
647
- class PreparedStatementCache {
648
- connection;
649
- size;
650
- entries = new Map;
651
- executionTail = Promise.resolve();
652
- hits = 0;
653
- misses = 0;
654
- evictions = 0;
655
- stats() {
656
- return {
657
- size: this.entries.size,
658
- capacity: this.size,
659
- hits: this.hits,
660
- misses: this.misses,
661
- evictions: this.evictions
662
- };
663
- }
664
- constructor(connection, size) {
665
- this.connection = connection;
666
- this.size = size;
667
- }
668
- resize(size) {
669
- this.size = size;
670
- this.trimToSize();
671
- }
672
- async getOrPrepare(query) {
673
- const cached = this.entries.get(query);
674
- if (cached) {
675
- this.hits += 1;
676
- return this.remember(query, cached.statement);
677
- }
678
- this.misses += 1;
679
- const statement = await this.connection.prepare(query);
680
- this.remember(query, statement);
681
- return statement;
682
- }
683
- async runExclusive(operation) {
684
- const previous = this.executionTail;
685
- let release = () => {};
686
- this.executionTail = new Promise((resolve) => {
687
- release = resolve;
688
- });
689
- await previous;
690
- try {
691
- return await operation();
692
- } finally {
693
- release();
694
- }
695
- }
696
- remember(query, statement) {
697
- this.entries.delete(query);
698
- this.entries.set(query, { statement });
699
- this.trimToSize();
700
- return statement;
701
- }
702
- evict(query) {
703
- const entry = this.entries.get(query);
704
- if (entry)
705
- this.evictions += 1;
706
- this.entries.delete(query);
707
- destroyPreparedStatement(entry);
708
- }
709
- clear() {
710
- for (const entry of this.entries.values()) {
711
- destroyPreparedStatement(entry);
712
- }
713
- this.entries.clear();
714
- }
715
- evictOldest() {
716
- const oldest = this.entries.keys().next();
717
- if (!oldest.done) {
718
- this.evict(oldest.value);
719
- }
720
- }
721
- trimToSize() {
722
- while (this.entries.size > this.size) {
723
- this.evictOldest();
724
- }
725
- }
726
- }
727
- function getPreparedStatementCache(connection, size) {
728
- const store = connection;
729
- const existing = store[PREPARED_CACHE];
730
- if (existing) {
731
- existing.resize(size);
732
- return existing;
733
- }
734
- const cache = new PreparedStatementCache(connection, size);
735
- store[PREPARED_CACHE] = cache;
736
- return cache;
737
- }
738
648
  function clearPreparedStatementCache(connection) {
739
649
  const store = connection;
740
650
  store[PREPARED_CACHE]?.clear();
741
651
  }
742
- function bindPreparedStatement(statement, values, types) {
743
- if (values) {
744
- if (types) {
745
- statement.bind(values, types);
746
- } else {
747
- statement.bind(values);
748
- }
749
- return;
750
- }
751
- statement.clearBindings?.();
752
- }
753
652
 
754
653
  // src/client.ts
755
654
  function isPool(client) {
@@ -915,14 +814,16 @@ var NO_NODE_API_PARAMS = {
915
814
  values: undefined,
916
815
  types: undefined
917
816
  };
918
- function toNodeApiParams(params) {
817
+ function toNodeApiParams(params, dateParams) {
919
818
  if (params.length === 0) {
920
819
  return NO_NODE_API_PARAMS;
921
820
  }
821
+ const dateType = dateParams === "timestamptz" ? TIMESTAMPTZ2 : undefined;
922
822
  const values = new Array(params.length);
923
823
  let types;
924
824
  for (let index = 0;index < params.length; index += 1) {
925
- const value = toNodeApiValue(params[index]);
825
+ const param = params[index];
826
+ const value = toNodeApiValue(param, dateType && param instanceof Date ? dateType : undefined);
926
827
  values[index] = value;
927
828
  if (value !== null && (typeof value === "object" || typeof value === "number" && Number.isInteger(value) && (value > 2147483647 || value < -2147483648))) {
928
829
  const typed = typedNodeApiParam(value);
@@ -1065,11 +966,22 @@ function wrapUnsupportedNodeApiTypeError(result, error) {
1065
966
  }
1066
967
  var TIMESTAMP_INFINITY_MICROS = 9223372036854775807n;
1067
968
  var MAX_DATE_MILLIS = 8640000000000000n;
969
+ var MAX_SAFE_MICROS = BigInt(Number.MAX_SAFE_INTEGER);
970
+ var DATE_INFINITY_DAYS = 2147483647;
1068
971
  function dateFromTimestampUnits(units, unitsPerMilli, typeName) {
1069
972
  if (units === TIMESTAMP_INFINITY_MICROS)
1070
973
  return "infinity";
1071
974
  if (units === -TIMESTAMP_INFINITY_MICROS)
1072
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
+ }
1073
985
  let millis = units / unitsPerMilli;
1074
986
  if (units % unitsPerMilli < 0n)
1075
987
  millis -= 1n;
@@ -1092,13 +1004,20 @@ var JSValueConverter = (value, type, converter) => {
1092
1004
  if (type.typeId === DuckDBTypeId2.TIMESTAMP_NS && "nanos" in value && typeof value.nanos === "bigint") {
1093
1005
  return dateFromTimestampUnits(value.nanos, 1000000n, "TIMESTAMP_NS");
1094
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
+ }
1095
1013
  }
1096
1014
  return JSDuckDBValueConverter(value, type, converter);
1097
1015
  };
1098
1016
  var FLOORED_TIMESTAMP_TYPE_IDS = new Set([
1099
1017
  DuckDBTypeId2.TIMESTAMP,
1100
1018
  DuckDBTypeId2.TIMESTAMP_TZ,
1101
- DuckDBTypeId2.TIMESTAMP_NS
1019
+ DuckDBTypeId2.TIMESTAMP_NS,
1020
+ DuckDBTypeId2.DATE
1102
1021
  ]);
1103
1022
  function containsFlooredTimestamp(type) {
1104
1023
  if (FLOORED_TIMESTAMP_TYPE_IDS.has(type.typeId))
@@ -1180,8 +1099,8 @@ function hasFetchAllChunks(result) {
1180
1099
  async function materializeResultRows(result, exactDecimals = false, recursiveDecimals = false) {
1181
1100
  if (hasFetchAllChunks(result)) {
1182
1101
  const exactDecimalColumns = exactDecimals || recursiveDecimals ? findDecimalColumns(result) : [];
1183
- const rows2 = await readResultChunkRows(result, exactDecimalColumns.length > 0, recursiveDecimals);
1184
- return exactDecimalColumns.length > 0 ? { columns: resolveResultColumns(result), rows: rows2, exactDecimalColumns } : { columns: resolveResultColumns(result), rows: rows2 };
1102
+ const rows = await readResultChunkRows(result, exactDecimalColumns.length > 0, recursiveDecimals);
1103
+ return exactDecimalColumns.length > 0 ? { columns: resolveResultColumns(result), rows, exactDecimalColumns } : { columns: resolveResultColumns(result), rows };
1185
1104
  }
1186
1105
  const getRowsJson = typeof result.getRowsJson === "function" ? result.getRowsJson.bind(result) : undefined;
1187
1106
  const rows = await readPreferredResult({
@@ -1192,36 +1111,6 @@ async function materializeResultRows(result, exactDecimals = false, recursiveDec
1192
1111
  const columns = resolveResultColumns(result);
1193
1112
  return { columns, rows };
1194
1113
  }
1195
- function isUnpreparableQueryError(error) {
1196
- return error instanceof Error && /Cannot prepare multiple statements at once|No statement to prepare/.test(error.message);
1197
- }
1198
- async function executePreparedQuery(connection, query, { values, types }, cacheConfig, exactDecimals = false, recursiveDecimals = false) {
1199
- const cache = getPreparedStatementCache(connection, cacheConfig.size);
1200
- return await cache.runExclusive(async () => {
1201
- assertConnectionAvailable(connection);
1202
- let statement;
1203
- try {
1204
- statement = await cache.getOrPrepare(query);
1205
- } catch (error) {
1206
- if (!isUnpreparableQueryError(error)) {
1207
- throw error;
1208
- }
1209
- assertConnectionAvailable(connection);
1210
- const result = await connection.run(query, values, types);
1211
- return await materializeResultRows(result, exactDecimals, recursiveDecimals);
1212
- }
1213
- assertConnectionAvailable(connection);
1214
- try {
1215
- bindPreparedStatement(statement, values, types);
1216
- const result = await statement.run();
1217
- cache.remember(query, statement);
1218
- return await materializeResultRows(result, exactDecimals, recursiveDecimals);
1219
- } catch (error) {
1220
- cache.evict(query);
1221
- throw error;
1222
- }
1223
- });
1224
- }
1225
1114
  async function* yieldChunkRows(fetchChunk, preferJson, exactDecimals = false) {
1226
1115
  let useJson = preferJson;
1227
1116
  let yieldedRows = false;
@@ -1264,10 +1153,7 @@ async function materializeRows(client, query, params, options = {}) {
1264
1153
  return await materializePgDuckRows(connection, query, params);
1265
1154
  }
1266
1155
  return await runNodeApiOperation(connection, async () => {
1267
- const nodeApiParams = toNodeApiParams(params);
1268
- if (options.prepareCache && typeof connection.prepare === "function") {
1269
- return await executePreparedQuery(connection, query, nodeApiParams, options.prepareCache, options.exactDecimals, options.decimalMode === "string");
1270
- }
1156
+ const nodeApiParams = toNodeApiParams(params, options.dateParams);
1271
1157
  const result = await connection.run(query, nodeApiParams.values, nodeApiParams.types);
1272
1158
  return await materializeResultRows(result, options.exactDecimals, options.decimalMode === "string");
1273
1159
  });
@@ -1412,7 +1298,7 @@ async function* streamRawBatches(client, query, params, options = {}) {
1412
1298
  streamingConnections.add(connection);
1413
1299
  let result;
1414
1300
  try {
1415
- const { values, types } = toNodeApiParams(params);
1301
+ const { values, types } = toNodeApiParams(params, options.dateParams);
1416
1302
  result = await connection.stream(query, values, types);
1417
1303
  yield* streamNodeApiResult(connection, result, rowsPerChunk, options.decimalMode === "string");
1418
1304
  } finally {
@@ -1477,7 +1363,7 @@ async function executeArrowOnClient(client, query, params, options = {}) {
1477
1363
  return mapRowsToColumnData(columns, rows);
1478
1364
  }
1479
1365
  return await runNodeApiOperation(connection, async () => {
1480
- const { values, types } = toNodeApiParams(params);
1366
+ const { values, types } = toNodeApiParams(params, options.dateParams);
1481
1367
  const result = await connection.run(query, values, types);
1482
1368
  const maybeArrow = result.toArrow ?? result.getArrowTable;
1483
1369
  if (typeof maybeArrow === "function") {
@@ -1490,23 +1376,23 @@ async function executeArrowOnClient(client, query, params, options = {}) {
1490
1376
  const js = jsConverterFor(chunks);
1491
1377
  const convert = (baseConverter) => {
1492
1378
  const converter = options.decimalMode === "string" ? withExactDecimals(baseConverter, true, js) : baseConverter;
1493
- const values2 = columns.map(() => []);
1379
+ const values = columns.map(() => []);
1494
1380
  for (const chunk of chunks) {
1495
1381
  if (typeof chunk.convertColumns === "function") {
1496
1382
  const converted = chunk.convertColumns(converter);
1497
1383
  for (let index = 0;index < columns.length; index += 1) {
1498
1384
  for (const value of converted[index] ?? [])
1499
- values2[index].push(value);
1385
+ values[index].push(value);
1500
1386
  }
1501
1387
  } else {
1502
1388
  for (const row of chunk.convertRows(converter)) {
1503
1389
  for (let index = 0;index < columns.length; index += 1)
1504
- values2[index].push(row[index]);
1390
+ values[index].push(row[index]);
1505
1391
  }
1506
1392
  }
1507
1393
  }
1508
1394
  const data = {};
1509
- columns.forEach((name, index) => assignOwnProperty(data, name, values2[index]));
1395
+ columns.forEach((name, index) => assignOwnProperty(data, name, values[index]));
1510
1396
  return data;
1511
1397
  };
1512
1398
  try {
@@ -1530,6 +1416,96 @@ async function executeArrowOnClient(client, query, params, options = {}) {
1530
1416
  });
1531
1417
  });
1532
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
+ }
1533
1509
 
1534
1510
  // src/driver.ts
1535
1511
  import { DuckDBInstance as DuckDBInstance2 } from "@duckdb/node-api";
@@ -1545,7 +1521,7 @@ import {
1545
1521
  import { AsyncLocalStorage } from "node:async_hooks";
1546
1522
  import { entityKind as entityKind3, is as is4 } from "drizzle-orm/entity";
1547
1523
  import { NoopLogger } from "drizzle-orm/logger";
1548
- import { PgArray as PgArray2, PgTransaction } from "drizzle-orm/pg-core";
1524
+ import { getTableConfig, PgArray as PgArray2, PgTransaction } from "drizzle-orm/pg-core";
1549
1525
  import { PgPreparedQuery, PgSession } from "drizzle-orm/pg-core/session";
1550
1526
  import {
1551
1527
  fillPlaceholders,
@@ -1636,7 +1612,7 @@ function arrayBoundsSql(name, arr) {
1636
1612
  if (name === "array_lower") {
1637
1613
  return `CASE WHEN array_length(${arr}) > 0 THEN 1 ELSE NULL END`;
1638
1614
  }
1639
- return arr.includes("(") ? `list_extract(list_transform(list_value(array_length(${arr})), __drizzle_array_length -> nullif(__drizzle_array_length, 0)), 1)` : `nullif(array_length(${arr}), 0)`;
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)`;
1640
1616
  }
1641
1617
  function rewriteArrayBoundsText(query) {
1642
1618
  let output = "";
@@ -1804,8 +1780,8 @@ function normalizeInet(value) {
1804
1780
  if (address < 0n || address > 0xffffffffn) {
1805
1781
  return value;
1806
1782
  }
1807
- const suffix2 = value.mask !== 32 ? `/${value.mask}` : "";
1808
- return `${formatIpv4(address)}${suffix2}`;
1783
+ const suffix = value.mask !== 32 ? `/${value.mask}` : "";
1784
+ return `${formatIpv4(address)}${suffix}`;
1809
1785
  }
1810
1786
  const unsigned = address + (1n << 127n);
1811
1787
  if (unsigned < 0n || unsigned >= 1n << 128n) {
@@ -1851,10 +1827,10 @@ function normalizeTimestamp(value, withTimezone) {
1851
1827
  }
1852
1828
  function normalizeDateString(value) {
1853
1829
  if (value instanceof Date) {
1854
- return value.toISOString().slice(0, 10);
1830
+ return dateStringFromDate(value);
1855
1831
  }
1856
1832
  if (typeof value === "string") {
1857
- return value.slice(0, 10);
1833
+ return value === "infinity" || value === "-infinity" ? value : value.slice(0, 10);
1858
1834
  }
1859
1835
  return value;
1860
1836
  }
@@ -1862,6 +1838,9 @@ function normalizeDateValue(value) {
1862
1838
  if (value instanceof Date) {
1863
1839
  return value;
1864
1840
  }
1841
+ if (isInfiniteTimestamp(value)) {
1842
+ return value;
1843
+ }
1865
1844
  if (typeof value === "string") {
1866
1845
  return new Date(`${value.slice(0, 10)}T00:00:00Z`);
1867
1846
  }
@@ -1912,7 +1891,7 @@ function compileValueNormalizer(decoder) {
1912
1891
  }
1913
1892
  function compileValueMapper(decoder) {
1914
1893
  const normalize = compileValueNormalizer(decoder);
1915
- if (is(decoder, PgTimestamp)) {
1894
+ if (is(decoder, PgTimestamp) || is(decoder, PgDate)) {
1916
1895
  return (value) => {
1917
1896
  const normalized = normalize(value);
1918
1897
  return normalized === null || normalized instanceof Date || isInfiniteTimestamp(normalized) ? normalized : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
@@ -2078,8 +2057,17 @@ function arrayBoundsExpr(functionName, arrayExpr) {
2078
2057
  call("list_value", [lengthExpr]),
2079
2058
  {
2080
2059
  type: "binary_expr",
2081
- operator: "->",
2082
- left: length,
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
+ },
2083
2071
  right: nullIf(length)
2084
2072
  }
2085
2073
  ]),
@@ -3018,20 +3006,20 @@ function scanSqlTokens(query) {
3018
3006
  };
3019
3007
  const readQuoted = (start, quote) => {
3020
3008
  const backslashEscapes = isEscapeStringAt(query, start);
3021
- let index2 = start + 1;
3022
- while (index2 < query.length) {
3023
- if (backslashEscapes && query[index2] === "\\") {
3024
- index2 += 2;
3009
+ let index = start + 1;
3010
+ while (index < query.length) {
3011
+ if (backslashEscapes && query[index] === "\\") {
3012
+ index += 2;
3025
3013
  continue;
3026
3014
  }
3027
- if (query[index2] === quote) {
3028
- if (query[index2 + 1] === quote) {
3029
- index2 += 2;
3015
+ if (query[index] === quote) {
3016
+ if (query[index + 1] === quote) {
3017
+ index += 2;
3030
3018
  continue;
3031
3019
  }
3032
- return index2;
3020
+ return index;
3033
3021
  }
3034
- index2 += 1;
3022
+ index += 1;
3035
3023
  }
3036
3024
  return -1;
3037
3025
  };
@@ -3183,8 +3171,8 @@ function transformSQL(query, options = {}) {
3183
3171
  function rewriteRemainingArrayBounds(result) {
3184
3172
  if (!ARRAY_BOUNDS_PATTERN.test(result.sql))
3185
3173
  return result;
3186
- const sql2 = rewriteArrayBoundsText(result.sql);
3187
- return sql2 === result.sql ? result : { sql: sql2, transformed: true };
3174
+ const sql = rewriteArrayBoundsText(result.sql);
3175
+ return sql === result.sql ? result : { sql, transformed: true };
3188
3176
  }
3189
3177
  function transformWithAst(query, {
3190
3178
  needsArrayTransform,
@@ -3286,6 +3274,7 @@ var RELATIONAL_JSON_CHUNKS = new Map([
3286
3274
  ["), '[]'::json)", ")), '[]'::json)"]
3287
3275
  ]);
3288
3276
  var RELATIONAL_LIMIT_CHUNKS = new Set([" limit ", " offset "]);
3277
+ var EXACT_BIGINT_COLUMN_TYPES = new Set(["PgBigInt64", "PgBigSerial64"]);
3289
3278
  function rewriteRelationalJsonChunks(chunks, exactDecimals = false) {
3290
3279
  for (let index = 0;index < chunks.length; index += 1) {
3291
3280
  const chunk = chunks[index];
@@ -3296,7 +3285,13 @@ function rewriteRelationalJsonChunks(chunks, exactDecimals = false) {
3296
3285
  if (replacement) {
3297
3286
  if (value === "json_build_array(" && is2(next, SQL2)) {
3298
3287
  const fields = next.queryChunks.map((item) => {
3299
- if (!is2(item, Column2) || !exactDecimals && !item.columnType.startsWith("PgNumeric")) {
3288
+ if (!is2(item, Column2)) {
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")) {
3300
3295
  return item;
3301
3296
  }
3302
3297
  const originalType = item.getSQLType();
@@ -3596,8 +3591,8 @@ function qualifySubqueryFields(select) {
3596
3591
  const internals = select;
3597
3592
  const baseAs = internals.as.bind(internals);
3598
3593
  internals.as = (alias) => {
3599
- const { sql: sql4, usedTables } = baseAs(alias)._;
3600
- return new Proxy(new Subquery3(sql4, exposeSubqueryFields(internals.config.fields), alias, false, usedTables), new SelectionProxyHandler({
3594
+ const { sql, usedTables } = baseAs(alias)._;
3595
+ return new Proxy(new Subquery3(sql, exposeSubqueryFields(internals.config.fields), alias, false, usedTables), new SelectionProxyHandler({
3601
3596
  alias,
3602
3597
  sqlAliasedBehavior: "alias",
3603
3598
  sqlBehavior: "error"
@@ -3913,10 +3908,11 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
3913
3908
  decimalMode;
3914
3909
  waitForTransactions;
3915
3910
  relationalSelection;
3911
+ dateParams;
3916
3912
  static [entityKind3] = "DuckDBPreparedQuery";
3917
3913
  resultMapper;
3918
3914
  relationalRowNormalizer;
3919
- constructor(client, dialect, queryString, params, logger, fields, _isResponseInArrayMode, customResultMapper, rejectStringArrayLiterals, prepareCache, queryMetadata, cacheConfig, warnOnStringArrayLiteral, scalarParamIndexes, onStatementError, decimalMode = "number", waitForTransactions = false, relationalSelection) {
3915
+ constructor(client, dialect, queryString, params, logger, fields, _isResponseInArrayMode, customResultMapper, rejectStringArrayLiterals, prepareCache, queryMetadata, cacheConfig, warnOnStringArrayLiteral, scalarParamIndexes, onStatementError, decimalMode = "number", waitForTransactions = false, relationalSelection, dateParams) {
3920
3916
  super(...[
3921
3917
  { sql: queryString, params },
3922
3918
  undefined,
@@ -3939,6 +3935,7 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
3939
3935
  this.decimalMode = decimalMode;
3940
3936
  this.waitForTransactions = waitForTransactions;
3941
3937
  this.relationalSelection = relationalSelection;
3938
+ this.dateParams = dateParams;
3942
3939
  }
3943
3940
  async execute(placeholderValues = {}) {
3944
3941
  try {
@@ -3964,17 +3961,19 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
3964
3961
  });
3965
3962
  const { fields, joinsNotNullableMap, customResultMapper } = this;
3966
3963
  if (!fields && !customResultMapper) {
3967
- const rows2 = await executeOnClient(this.client, queryString, params, {
3964
+ const rows = await executeOnClient(this.client, queryString, params, {
3968
3965
  prepareCache: this.prepareCache,
3969
- decimalMode: this.decimalMode
3966
+ decimalMode: this.decimalMode,
3967
+ dateParams: this.dateParams
3970
3968
  });
3971
- return rows2;
3969
+ return rows;
3972
3970
  }
3973
3971
  const relationalSelection = customResultMapper ? this.relationalSelection : undefined;
3974
3972
  const { rows, exactDecimalColumns } = await executeArraysOnClient(this.client, queryString, params, {
3975
3973
  prepareCache: this.prepareCache,
3976
3974
  exactDecimals: !customResultMapper || relationalSelection !== undefined,
3977
- decimalMode: this.decimalMode
3975
+ decimalMode: this.decimalMode,
3976
+ dateParams: this.dateParams
3978
3977
  });
3979
3978
  const decimalFields = relationalSelection ?? fields;
3980
3979
  if (exactDecimalColumns && decimalFields && this.decimalMode !== "string") {
@@ -4028,7 +4027,7 @@ class DuckDBSession extends PgSession {
4028
4027
  };
4029
4028
  }
4030
4029
  prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
4031
- 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));
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);
4032
4031
  }
4033
4032
  waitsForTransactions() {
4034
4033
  return !this.statementFailures && !isPool(this.client);
@@ -4196,6 +4195,7 @@ query: ${query}`, []);
4196
4195
  const { sql: queryString, params } = this.prepareQueryExecution(query);
4197
4196
  return this.trackStreamFailures(this.afterOpenTransaction(executeInBatches(this.client, queryString, params, {
4198
4197
  decimalMode: this.options.decimalMode,
4198
+ dateParams: this.options.dateParams,
4199
4199
  ...options
4200
4200
  })));
4201
4201
  }
@@ -4203,6 +4203,7 @@ query: ${query}`, []);
4203
4203
  const { sql: queryString, params } = this.prepareQueryExecution(query);
4204
4204
  return this.trackStreamFailures(this.afterOpenTransaction(executeInBatchesRaw(this.client, queryString, params, {
4205
4205
  decimalMode: this.options.decimalMode,
4206
+ dateParams: this.options.dateParams,
4206
4207
  ...options
4207
4208
  })));
4208
4209
  }
@@ -4213,7 +4214,65 @@ query: ${query}`, []);
4213
4214
  }
4214
4215
  try {
4215
4216
  return await executeArrowOnClient(this.client, queryString, params, {
4216
- 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
+ };
4217
4276
  });
4218
4277
  } catch (error) {
4219
4278
  this.recordStatementFailure(error);
@@ -4294,6 +4353,9 @@ class DuckDBTransaction extends PgTransaction {
4294
4353
  executeArrow(query) {
4295
4354
  return this.getInternals().session.executeArrow(query);
4296
4355
  }
4356
+ appendRows(table, rows) {
4357
+ return this.getInternals().session.appendRows(table, rows);
4358
+ }
4297
4359
  async transaction(transaction) {
4298
4360
  const internals = this.getInternals();
4299
4361
  const savepoint = `drizzle_savepoint_${this.nestedIndex + 1}`;
@@ -4630,9 +4692,9 @@ function createDuckDBConnectionPool(instance, options = {}) {
4630
4692
  if (waiting.length >= maxWaitingRequests) {
4631
4693
  throw new Error(`DuckDB connection pool queue is full (max ${maxWaitingRequests} waiting requests)`);
4632
4694
  }
4633
- return await new Promise((resolve2, reject) => {
4695
+ return await new Promise((resolve, reject) => {
4634
4696
  const waiter = {
4635
- resolve: resolve2,
4697
+ resolve,
4636
4698
  reject,
4637
4699
  timeoutId: undefined,
4638
4700
  queuedAt: Date.now()
@@ -5021,7 +5083,8 @@ class DuckDBDriver {
5021
5083
  rejectStringArrayLiterals: this.options.rejectStringArrayLiterals,
5022
5084
  arrayLiteralWarning: this.options.arrayLiteralWarning,
5023
5085
  prepareCache: this.options.prepareCache,
5024
- decimalMode: this.options.decimalMode
5086
+ decimalMode: this.options.decimalMode,
5087
+ dateParams: this.options.dateParams
5025
5088
  });
5026
5089
  }
5027
5090
  }
@@ -5083,6 +5146,15 @@ function isNodePostgresPool(value) {
5083
5146
  const candidate = value;
5084
5147
  return typeof candidate.connect === "function" && typeof candidate.query === "function" && typeof candidate.acquire !== "function" && typeof candidate.totalCount === "number" && typeof candidate.idleCount === "number";
5085
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
+ }
5086
5158
  function createFromClient(client, config = {}, instance) {
5087
5159
  let finalClient = isNodePostgresPool(client) ? createPgDuckConnectionPool(client) : client;
5088
5160
  if (config.ducklake) {
@@ -5098,6 +5170,9 @@ function createFromClient(client, config = {}, instance) {
5098
5170
  decimalMode: config.decimalMode
5099
5171
  });
5100
5172
  const prepareCache = resolvePrepareCacheOption(config.prepareCache);
5173
+ if (prepareCache) {
5174
+ warnPrepareCacheIgnored();
5175
+ }
5101
5176
  const logger = config.logger === true ? new DefaultLogger : config.logger || undefined;
5102
5177
  let schema;
5103
5178
  if (config.schema) {
@@ -5113,7 +5188,8 @@ function createFromClient(client, config = {}, instance) {
5113
5188
  rejectStringArrayLiterals: config.rejectStringArrayLiterals,
5114
5189
  arrayLiteralWarning: config.arrayLiteralWarning,
5115
5190
  prepareCache,
5116
- decimalMode: config.decimalMode
5191
+ decimalMode: config.decimalMode,
5192
+ dateParams: config.dateParams
5117
5193
  });
5118
5194
  const session = driver.createSession(schema);
5119
5195
  const db = new DuckDBDatabase(dialect, session, schema, finalClient, instance);
@@ -5236,6 +5312,9 @@ class DuckDBDatabase extends PgDatabase {
5236
5312
  executeArrow(query) {
5237
5313
  return this.session.executeArrow(query);
5238
5314
  }
5315
+ appendRows(table, rows) {
5316
+ return this.session.appendRows(table, rows);
5317
+ }
5239
5318
  async transaction(transaction, config) {
5240
5319
  return await this.session.transaction(transaction, config);
5241
5320
  }
@@ -6020,7 +6099,7 @@ function nestedValueType(raw) {
6020
6099
  return `Array<${nestedValueType(array[1])} | null>`;
6021
6100
  if (type.startsWith("STRUCT")) {
6022
6101
  const fields = parseStructFields(type.replace(/^STRUCT\s*\(/i, "").replace(/\)$/, ""));
6023
- return `{ ${fields.map(({ name, type: type2 }) => `${JSON.stringify(name)}: ${nestedValueType(type2)} | null`).join("; ")} }`;
6102
+ return `{ ${fields.map(({ name, type }) => `${JSON.stringify(name)}: ${nestedValueType(type)} | null`).join("; ")} }`;
6024
6103
  }
6025
6104
  if (type.startsWith("MAP(")) {
6026
6105
  const { keyType, valueType } = parseMapTypes(type);
@@ -6088,14 +6167,14 @@ function mapDuckDbType(column, imports, options) {
6088
6167
  imports.pgCore.add("numeric");
6089
6168
  const precision = column.numericPrecision;
6090
6169
  const scale = column.numericScale;
6091
- const options2 = [];
6170
+ const options = [];
6092
6171
  if (precision !== null && precision !== undefined) {
6093
- options2.push(`precision: ${precision}`);
6172
+ options.push(`precision: ${precision}`);
6094
6173
  }
6095
6174
  if (scale !== null && scale !== undefined) {
6096
- options2.push(`scale: ${scale}`);
6175
+ options.push(`scale: ${scale}`);
6097
6176
  }
6098
- const suffix = options2.length ? `, { ${options2.join(", ")} }` : "";
6177
+ const suffix = options.length ? `, { ${options.join(", ")} }` : "";
6099
6178
  return {
6100
6179
  builder: `numeric(${columnName(column.name)}${suffix})`,
6101
6180
  defaultLiterals: STRING_DEFAULTS
@@ -6396,9 +6475,9 @@ function uniqueSchemas(tables) {
6396
6475
  }
6397
6476
  function renderImports(imports, importBasePath) {
6398
6477
  const lines = [];
6399
- const drizzle2 = [...imports.drizzle];
6400
- if (drizzle2.length) {
6401
- lines.push(`import { ${drizzle2.sort().join(", ")} } from 'drizzle-orm';`);
6478
+ const drizzle = [...imports.drizzle];
6479
+ if (drizzle.length) {
6480
+ lines.push(`import { ${drizzle.sort().join(", ")} } from 'drizzle-orm';`);
6402
6481
  }
6403
6482
  const pgCore = [...imports.pgCore];
6404
6483
  if (pgCore.length) {