@avidian/duckdb-bun 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +286 -2
- package/package.json +96 -4
- package/src/bindings/api/appender.ts +288 -0
- package/src/bindings/api/data-chunk.ts +58 -0
- package/src/bindings/api/database.ts +232 -0
- package/src/bindings/api/datetime.ts +158 -0
- package/src/bindings/api/index.ts +13 -0
- package/src/bindings/api/logical-types.ts +227 -0
- package/src/bindings/api/query.ts +616 -0
- package/src/bindings/api/scalar-function.ts +274 -0
- package/src/bindings/api/table-function.ts +339 -0
- package/src/bindings/api/values.ts +546 -0
- package/src/bindings/api/vector.ts +266 -0
- package/src/bindings/generated/capi.ts +695 -0
- package/src/bindings/generated/contract.ts +162 -0
- package/src/bindings/index.ts +10 -0
- package/src/bindings/runtime/convert.ts +307 -0
- package/src/bindings/runtime/diagnostics.ts +45 -0
- package/src/bindings/runtime/dispatch.ts +213 -0
- package/src/bindings/runtime/exception.ts +103 -0
- package/src/bindings/runtime/flags.ts +19 -0
- package/src/bindings/runtime/handle.ts +217 -0
- package/src/bindings/runtime/kinds.ts +53 -0
- package/src/bindings/runtime/marshal.ts +190 -0
- package/src/bindings/runtime/native.ts +153 -0
- package/src/bindings/runtime/ring.ts +54 -0
- package/src/bindings/runtime/scope.ts +133 -0
- package/src/bindings/runtime/trace.ts +65 -0
- package/src/connection.ts +35 -0
- package/src/database.ts +27 -0
- package/src/errors.ts +30 -0
- package/src/file-assets.d.ts +10 -0
- package/src/index.ts +17 -0
- package/src/library.ts +83 -0
- package/src/neo.ts +16 -0
- package/src/reader.ts +47 -0
- package/src/statement.ts +61 -0
- package/src/types.ts +52 -0
- package/src/version.ts +6 -0
- package/vendor/node-api/LICENSE +7 -0
- package/vendor/node-api/PROVENANCE.md +54 -0
- package/vendor/node-api/node-bindings.d.ts +1669 -0
- package/vendor/node-api/src/DuckDBAppender.ts +237 -0
- package/vendor/node-api/src/DuckDBClientContext.ts +13 -0
- package/vendor/node-api/src/DuckDBConnection.ts +315 -0
- package/vendor/node-api/src/DuckDBDataChunk.ts +412 -0
- package/vendor/node-api/src/DuckDBDataChunkWriter.ts +144 -0
- package/vendor/node-api/src/DuckDBExtractedStatements.ts +37 -0
- package/vendor/node-api/src/DuckDBInstance.ts +35 -0
- package/vendor/node-api/src/DuckDBInstanceCache.ts +29 -0
- package/vendor/node-api/src/DuckDBLogicalType.ts +446 -0
- package/vendor/node-api/src/DuckDBMaterializedResult.ts +39 -0
- package/vendor/node-api/src/DuckDBPendingResult.ts +83 -0
- package/vendor/node-api/src/DuckDBPreparedStatement.ts +376 -0
- package/vendor/node-api/src/DuckDBPreparedStatementCollection.ts +5 -0
- package/vendor/node-api/src/DuckDBPreparedStatementWeakRefCollection.ts +31 -0
- package/vendor/node-api/src/DuckDBResult.ts +403 -0
- package/vendor/node-api/src/DuckDBResultReader.ts +283 -0
- package/vendor/node-api/src/DuckDBScalarFunction.ts +149 -0
- package/vendor/node-api/src/DuckDBScalarFunctionBindInfo.ts +34 -0
- package/vendor/node-api/src/DuckDBScalarFunctionInfo.ts +34 -0
- package/vendor/node-api/src/DuckDBScalarFunctionInitInfo.ts +35 -0
- package/vendor/node-api/src/DuckDBTableFunction.ts +149 -0
- package/vendor/node-api/src/DuckDBTableFunctionBindInfo.ts +56 -0
- package/vendor/node-api/src/DuckDBTableFunctionInfo.ts +36 -0
- package/vendor/node-api/src/DuckDBTableFunctionInitInfo.ts +52 -0
- package/vendor/node-api/src/DuckDBType.ts +1 -0
- package/vendor/node-api/src/DuckDBTypeId.ts +46 -0
- package/vendor/node-api/src/DuckDBValueConverter.ts +23 -0
- package/vendor/node-api/src/DuckDBValueConverters.ts +461 -0
- package/vendor/node-api/src/DuckDBVector.ts +1 -0
- package/vendor/node-api/src/JS.ts +11 -0
- package/vendor/node-api/src/JSDuckDBValueConverter.ts +85 -0
- package/vendor/node-api/src/JSInputTypeForTypeId.ts +101 -0
- package/vendor/node-api/src/JSToDuckDBValueConverter.ts +136 -0
- package/vendor/node-api/src/JSTypeForTypeId.ts +78 -0
- package/vendor/node-api/src/Json.ts +7 -0
- package/vendor/node-api/src/JsonDuckDBValueConverter.ts +72 -0
- package/vendor/node-api/src/JsonTypeForTypeId.ts +86 -0
- package/vendor/node-api/src/ToDuckDBValueConverter.ts +68 -0
- package/vendor/node-api/src/ToDuckDBValueConverters.ts +303 -0
- package/vendor/node-api/src/configurationOptionDescriptions.ts +13 -0
- package/vendor/node-api/src/conversion/bytesFromString.ts +5 -0
- package/vendor/node-api/src/conversion/dateTimeStringConversion.ts +343 -0
- package/vendor/node-api/src/conversion/displayStringForDuckDBValue.ts +12 -0
- package/vendor/node-api/src/conversion/stringFromBlob.ts +48 -0
- package/vendor/node-api/src/conversion/stringFromDecimal.ts +129 -0
- package/vendor/node-api/src/conversion/varintDecode.ts +42 -0
- package/vendor/node-api/src/convertColumnsFromChunks.ts +28 -0
- package/vendor/node-api/src/convertColumnsObjectFromChunks.ts +29 -0
- package/vendor/node-api/src/convertRowObjectsFromChunks.ts +21 -0
- package/vendor/node-api/src/convertRowsFromChunks.ts +16 -0
- package/vendor/node-api/src/createConfig.ts +14 -0
- package/vendor/node-api/src/createDuckDBValueConverter.ts +17 -0
- package/vendor/node-api/src/createResult.ts +15 -0
- package/vendor/node-api/src/createValue.ts +355 -0
- package/vendor/node-api/src/duckdb.ts +51 -0
- package/vendor/node-api/src/enums.ts +7 -0
- package/vendor/node-api/src/getColumnsFromChunks.ts +13 -0
- package/vendor/node-api/src/getColumnsObjectFromChunks.ts +16 -0
- package/vendor/node-api/src/getRowObjectsFromChunks.ts +13 -0
- package/vendor/node-api/src/getRowsFromChunks.ts +12 -0
- package/vendor/node-api/src/index.ts +3 -0
- package/vendor/node-api/src/readValue.ts +31 -0
- package/vendor/node-api/src/sleep.ts +5 -0
- package/vendor/node-api/src/sql.ts +7 -0
- package/vendor/node-api/src/tsconfig.json +7 -0
- package/vendor/node-api/src/typeForValue.ts +144 -0
- package/vendor/node-api/src/types/BaseDuckDBType.ts +31 -0
- package/vendor/node-api/src/types/DuckDBAnyType.ts +13 -0
- package/vendor/node-api/src/types/DuckDBArrayType.ts +43 -0
- package/vendor/node-api/src/types/DuckDBBigIntType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBBigNumType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBBitType.ts +13 -0
- package/vendor/node-api/src/types/DuckDBBlobType.ts +13 -0
- package/vendor/node-api/src/types/DuckDBBooleanType.ts +13 -0
- package/vendor/node-api/src/types/DuckDBDateType.ts +29 -0
- package/vendor/node-api/src/types/DuckDBDecimalType.ts +46 -0
- package/vendor/node-api/src/types/DuckDBDoubleType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBEnumType.ts +111 -0
- package/vendor/node-api/src/types/DuckDBFloatType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBGeometryType.ts +36 -0
- package/vendor/node-api/src/types/DuckDBHugeIntType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBIntegerLiteralType.ts +13 -0
- package/vendor/node-api/src/types/DuckDBIntegerType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBIntervalType.ts +13 -0
- package/vendor/node-api/src/types/DuckDBListType.ts +35 -0
- package/vendor/node-api/src/types/DuckDBMapType.ts +47 -0
- package/vendor/node-api/src/types/DuckDBSQLNullType.ts +17 -0
- package/vendor/node-api/src/types/DuckDBSmallIntType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBStringLiteralType.ts +13 -0
- package/vendor/node-api/src/types/DuckDBStructType.ts +104 -0
- package/vendor/node-api/src/types/DuckDBTimeNSType.ts +20 -0
- package/vendor/node-api/src/types/DuckDBTimeTZType.ts +23 -0
- package/vendor/node-api/src/types/DuckDBTimeType.ts +20 -0
- package/vendor/node-api/src/types/DuckDBTimestampMillisecondsType.ts +29 -0
- package/vendor/node-api/src/types/DuckDBTimestampNanosecondsType.ts +29 -0
- package/vendor/node-api/src/types/DuckDBTimestampSecondsType.ts +29 -0
- package/vendor/node-api/src/types/DuckDBTimestampTZNanosecondsType.ts +32 -0
- package/vendor/node-api/src/types/DuckDBTimestampTZType.ts +32 -0
- package/vendor/node-api/src/types/DuckDBTimestampType.ts +32 -0
- package/vendor/node-api/src/types/DuckDBTinyIntType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBType.ts +86 -0
- package/vendor/node-api/src/types/DuckDBUBigIntType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBUHugeIntType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBUIntegerType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBUSmallIntType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBUTinyIntType.ts +21 -0
- package/vendor/node-api/src/types/DuckDBUUIDType.ts +20 -0
- package/vendor/node-api/src/types/DuckDBUnionType.ts +74 -0
- package/vendor/node-api/src/types/DuckDBVarCharType.ts +13 -0
- package/vendor/node-api/src/types/DuckDBVariantType.ts +13 -0
- package/vendor/node-api/src/types/index.ts +44 -0
- package/vendor/node-api/src/values/DuckDBArrayValue.ts +18 -0
- package/vendor/node-api/src/values/DuckDBBitValue.ts +117 -0
- package/vendor/node-api/src/values/DuckDBBlobValue.ts +26 -0
- package/vendor/node-api/src/values/DuckDBDateValue.ts +43 -0
- package/vendor/node-api/src/values/DuckDBDecimalValue.ts +44 -0
- package/vendor/node-api/src/values/DuckDBGeometryValue.ts +30 -0
- package/vendor/node-api/src/values/DuckDBIntervalValue.ts +22 -0
- package/vendor/node-api/src/values/DuckDBListValue.ts +18 -0
- package/vendor/node-api/src/values/DuckDBMapValue.ts +25 -0
- package/vendor/node-api/src/values/DuckDBStructValue.ts +22 -0
- package/vendor/node-api/src/values/DuckDBTimeNSValue.ts +23 -0
- package/vendor/node-api/src/values/DuckDBTimeTZValue.ts +96 -0
- package/vendor/node-api/src/values/DuckDBTimeValue.ts +34 -0
- package/vendor/node-api/src/values/DuckDBTimestampMillisecondsValue.ts +29 -0
- package/vendor/node-api/src/values/DuckDBTimestampNanosecondsValue.ts +28 -0
- package/vendor/node-api/src/values/DuckDBTimestampSecondsValue.ts +28 -0
- package/vendor/node-api/src/values/DuckDBTimestampTZNanosecondsValue.ts +44 -0
- package/vendor/node-api/src/values/DuckDBTimestampTZValue.ts +54 -0
- package/vendor/node-api/src/values/DuckDBTimestampValue.ts +45 -0
- package/vendor/node-api/src/values/DuckDBTupleValue.ts +22 -0
- package/vendor/node-api/src/values/DuckDBUUIDValue.ts +48 -0
- package/vendor/node-api/src/values/DuckDBUnionValue.ts +22 -0
- package/vendor/node-api/src/values/DuckDBValue.ts +53 -0
- package/vendor/node-api/src/values/DuckDBVariantValue.ts +34 -0
- package/vendor/node-api/src/values/index.ts +24 -0
- package/vendor/node-api/src/vectors/DuckDBArrayVector.ts +107 -0
- package/vendor/node-api/src/vectors/DuckDBBigIntVector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBBigNumVector.ts +98 -0
- package/vendor/node-api/src/vectors/DuckDBBitVector.ts +102 -0
- package/vendor/node-api/src/vectors/DuckDBBlobVector.ts +112 -0
- package/vendor/node-api/src/vectors/DuckDBBooleanVector.ts +84 -0
- package/vendor/node-api/src/vectors/DuckDBDateVector.ts +80 -0
- package/vendor/node-api/src/vectors/DuckDBDecimal128Vector.ts +107 -0
- package/vendor/node-api/src/vectors/DuckDBDecimal16Vector.ts +104 -0
- package/vendor/node-api/src/vectors/DuckDBDecimal32Vector.ts +104 -0
- package/vendor/node-api/src/vectors/DuckDBDecimal64Vector.ts +104 -0
- package/vendor/node-api/src/vectors/DuckDBDoubleVector.ts +68 -0
- package/vendor/node-api/src/vectors/DuckDBEnum16Vector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBEnum32Vector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBEnum8Vector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBFloatVector.ts +68 -0
- package/vendor/node-api/src/vectors/DuckDBGeometryVector.ts +97 -0
- package/vendor/node-api/src/vectors/DuckDBHugeIntVector.ts +89 -0
- package/vendor/node-api/src/vectors/DuckDBIntegerVector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBIntervalVector.ts +100 -0
- package/vendor/node-api/src/vectors/DuckDBListVector.ts +221 -0
- package/vendor/node-api/src/vectors/DuckDBMapVector.ts +88 -0
- package/vendor/node-api/src/vectors/DuckDBSQLNullVector.ts +53 -0
- package/vendor/node-api/src/vectors/DuckDBSmallIntVector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBStructVector.ts +138 -0
- package/vendor/node-api/src/vectors/DuckDBTimeNSVector.ts +80 -0
- package/vendor/node-api/src/vectors/DuckDBTimeTZVector.ts +80 -0
- package/vendor/node-api/src/vectors/DuckDBTimeVector.ts +80 -0
- package/vendor/node-api/src/vectors/DuckDBTimestampMillisecondsVector.ts +88 -0
- package/vendor/node-api/src/vectors/DuckDBTimestampNanosecondsVector.ts +88 -0
- package/vendor/node-api/src/vectors/DuckDBTimestampSecondsVector.ts +88 -0
- package/vendor/node-api/src/vectors/DuckDBTimestampTZNanosecondsVector.ts +88 -0
- package/vendor/node-api/src/vectors/DuckDBTimestampTZVector.ts +86 -0
- package/vendor/node-api/src/vectors/DuckDBTimestampVector.ts +83 -0
- package/vendor/node-api/src/vectors/DuckDBTinyIntVector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBTupleVector.ts +125 -0
- package/vendor/node-api/src/vectors/DuckDBUBigIntVector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBUHugeIntVector.ts +89 -0
- package/vendor/node-api/src/vectors/DuckDBUIntegerVector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBUSmallIntVector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBUTinyIntVector.ts +75 -0
- package/vendor/node-api/src/vectors/DuckDBUUIDVector.ts +89 -0
- package/vendor/node-api/src/vectors/DuckDBUnionVector.ts +84 -0
- package/vendor/node-api/src/vectors/DuckDBValidity.ts +104 -0
- package/vendor/node-api/src/vectors/DuckDBVarCharVector.ts +100 -0
- package/vendor/node-api/src/vectors/DuckDBVariantVector.ts +609 -0
- package/vendor/node-api/src/vectors/DuckDBVector.ts +33 -0
- package/vendor/node-api/src/vectors/createVector.ts +269 -0
- package/vendor/node-api/src/vectors/dataAccessors.ts +387 -0
- package/vendor/node-api/src/vectors/index.ts +50 -0
- package/vendor/node-api/src/vectors/vectorRegistry.ts +24 -0
- package/vendor/node-api/src/version.ts +5 -0
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DuckDBArrayType,
|
|
3
|
+
DuckDBListType,
|
|
4
|
+
DuckDBMapType,
|
|
5
|
+
DuckDBStructType,
|
|
6
|
+
DuckDBType,
|
|
7
|
+
DuckDBUnionType,
|
|
8
|
+
} from './DuckDBType';
|
|
9
|
+
import { DuckDBTypeId } from './DuckDBTypeId';
|
|
10
|
+
import { DuckDBValueConverter } from './DuckDBValueConverter';
|
|
11
|
+
import { typeForValue } from './typeForValue';
|
|
12
|
+
import {
|
|
13
|
+
DuckDBArrayValue,
|
|
14
|
+
DuckDBBitValue,
|
|
15
|
+
DuckDBBlobValue,
|
|
16
|
+
DuckDBDateValue,
|
|
17
|
+
DuckDBDecimalValue,
|
|
18
|
+
DuckDBGeometryValue,
|
|
19
|
+
DuckDBIntervalValue,
|
|
20
|
+
DuckDBListValue,
|
|
21
|
+
DuckDBMapValue,
|
|
22
|
+
DuckDBStructValue,
|
|
23
|
+
DuckDBTimeTZValue,
|
|
24
|
+
DuckDBTimeNSValue,
|
|
25
|
+
DuckDBTimeValue,
|
|
26
|
+
DuckDBTimestampMillisecondsValue,
|
|
27
|
+
DuckDBTimestampNanosecondsValue,
|
|
28
|
+
DuckDBTimestampSecondsValue,
|
|
29
|
+
DuckDBTimestampTZValue,
|
|
30
|
+
DuckDBTimestampTZNanosecondsValue,
|
|
31
|
+
DuckDBTimestampValue,
|
|
32
|
+
DuckDBTupleValue,
|
|
33
|
+
DuckDBUnionValue,
|
|
34
|
+
DuckDBValue,
|
|
35
|
+
DuckDBVariantValue,
|
|
36
|
+
} from './values';
|
|
37
|
+
|
|
38
|
+
const MIN_DATE_DAYS = -100000000;
|
|
39
|
+
const MAX_DATE_DAYS = 100000000;
|
|
40
|
+
// -8640000000000
|
|
41
|
+
const MIN_DATE_MILLIS = -8640000000000000;
|
|
42
|
+
const MAX_DATE_MILLIS = 8640000000000000;
|
|
43
|
+
|
|
44
|
+
/** Whether a TIMESTAMP_NS or TIMESTAMPTZ_NS value is not +/-infinity. */
|
|
45
|
+
function isFiniteNanos(nanos: bigint): boolean {
|
|
46
|
+
return (
|
|
47
|
+
nanos !== DuckDBTimestampNanosecondsValue.PosInf.nanos &&
|
|
48
|
+
nanos !== DuckDBTimestampNanosecondsValue.NegInf.nanos
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const MILLS_PER_DAY = 24 * 60 * 60 * 1000;
|
|
53
|
+
|
|
54
|
+
export function unsupportedConverter(_: DuckDBValue, type: DuckDBType): null {
|
|
55
|
+
throw new Error(`Unsupported type: ${type}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function nullConverter(_: DuckDBValue): null {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function booleanFromValue(value: DuckDBValue): boolean {
|
|
63
|
+
return Boolean(value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function numberFromValue(value: DuckDBValue): number {
|
|
67
|
+
return Number(value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function jsonNumberFromValue(value: DuckDBValue): number | string {
|
|
71
|
+
if (Number.isFinite(value)) {
|
|
72
|
+
return Number(value);
|
|
73
|
+
}
|
|
74
|
+
return String(value);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function bigintFromBigIntValue(
|
|
78
|
+
value: DuckDBValue,
|
|
79
|
+
type: DuckDBType
|
|
80
|
+
): bigint {
|
|
81
|
+
if (typeof value === 'bigint') {
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
throw new Error(`Expected bigint value for type ${type}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function stringFromValue(value: DuckDBValue): string {
|
|
88
|
+
return String(value);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function bytesFromBlobValue(value: DuckDBValue): Uint8Array {
|
|
92
|
+
if (value instanceof DuckDBBlobValue) {
|
|
93
|
+
return value.bytes;
|
|
94
|
+
}
|
|
95
|
+
throw new Error(`Expected DuckDBBlobValue`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function bytesFromBitValue(value: DuckDBValue): Uint8Array {
|
|
99
|
+
if (value instanceof DuckDBBitValue) {
|
|
100
|
+
return value.data;
|
|
101
|
+
}
|
|
102
|
+
throw new Error(`Expected DuckDBBitValue`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function bytesFromGeometryValue(value: DuckDBValue): Uint8Array {
|
|
106
|
+
if (value instanceof DuckDBGeometryValue) {
|
|
107
|
+
return value.bytes;
|
|
108
|
+
}
|
|
109
|
+
throw new Error(`Expected DuckDBGeometryValue`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function dateFromDateValue(value: DuckDBValue): Date {
|
|
113
|
+
if (value instanceof DuckDBDateValue) {
|
|
114
|
+
if (MIN_DATE_DAYS <= value.days && value.days <= MAX_DATE_DAYS) {
|
|
115
|
+
return new Date(value.days * MILLS_PER_DAY);
|
|
116
|
+
}
|
|
117
|
+
throw new Error(`DATE value out of range for JS Date: ${value.days} days`);
|
|
118
|
+
}
|
|
119
|
+
throw new Error(`Expected DuckDBDateValue`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function bigintFromTimeValue(value: DuckDBValue): bigint {
|
|
123
|
+
if (value instanceof DuckDBTimeValue) {
|
|
124
|
+
return value.micros;
|
|
125
|
+
}
|
|
126
|
+
throw new Error(`Expected DuckDBTimeValue`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function bigintFromTimeNSValue(value: DuckDBValue): bigint {
|
|
130
|
+
if (value instanceof DuckDBTimeNSValue) {
|
|
131
|
+
return value.nanos;
|
|
132
|
+
}
|
|
133
|
+
throw new Error(`Expected DuckDBTimeNSValue`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function dateFromTimestampValue(value: DuckDBValue): Date {
|
|
137
|
+
if (value instanceof DuckDBTimestampValue) {
|
|
138
|
+
const millis = value.micros / 1000n;
|
|
139
|
+
if (MIN_DATE_MILLIS <= millis && millis <= MAX_DATE_MILLIS) {
|
|
140
|
+
return new Date(Number(millis));
|
|
141
|
+
}
|
|
142
|
+
throw new Error(
|
|
143
|
+
`TIMESTAMP value out of range for JS Date: ${value.micros} micros`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
throw new Error(`Expected DuckDBTimestampValue`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function dateFromTimestampSecondsValue(value: DuckDBValue): Date {
|
|
150
|
+
if (value instanceof DuckDBTimestampSecondsValue) {
|
|
151
|
+
const millis = value.seconds * 1000n;
|
|
152
|
+
if (MIN_DATE_MILLIS <= millis && millis <= MAX_DATE_MILLIS) {
|
|
153
|
+
return new Date(Number(millis));
|
|
154
|
+
}
|
|
155
|
+
throw new Error(
|
|
156
|
+
`TIMESTAMP_S value out of range for JS Date: ${value.seconds} seconds`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
throw new Error(`Expected DuckDBTimestampSecondsValue`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function dateFromTimestampMillisecondsValue(value: DuckDBValue): Date {
|
|
163
|
+
if (value instanceof DuckDBTimestampMillisecondsValue) {
|
|
164
|
+
const millis = value.millis;
|
|
165
|
+
if (MIN_DATE_MILLIS <= millis && millis <= MAX_DATE_MILLIS) {
|
|
166
|
+
return new Date(Number(millis));
|
|
167
|
+
}
|
|
168
|
+
throw new Error(
|
|
169
|
+
`TIMESTAMP_MS value out of range for JS Date: ${value.millis} millis`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
throw new Error(`Expected DuckDBTimestampMillisecondsValue`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function dateFromTimestampNanosecondsValue(value: DuckDBValue): Date {
|
|
176
|
+
if (value instanceof DuckDBTimestampNanosecondsValue) {
|
|
177
|
+
const millis = value.nanos / 1000000n;
|
|
178
|
+
if (
|
|
179
|
+
isFiniteNanos(value.nanos) &&
|
|
180
|
+
MIN_DATE_MILLIS <= millis &&
|
|
181
|
+
millis <= MAX_DATE_MILLIS
|
|
182
|
+
) {
|
|
183
|
+
return new Date(Number(millis));
|
|
184
|
+
}
|
|
185
|
+
throw new Error(
|
|
186
|
+
`TIMESTAMP_NS value out of range for JS Date: ${value.nanos} nanos`
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
throw new Error(`Expected DuckDBTimestampNanosecondsValue`);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function objectFromTimeTZValue(value: DuckDBValue): {
|
|
193
|
+
micros: bigint;
|
|
194
|
+
offset: number;
|
|
195
|
+
} {
|
|
196
|
+
if (value instanceof DuckDBTimeTZValue) {
|
|
197
|
+
return {
|
|
198
|
+
micros: value.micros,
|
|
199
|
+
offset: value.offset,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
throw new Error(`Expected DuckDBTimeTZValue`);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function dateFromTimestampTZValue(value: DuckDBValue): Date {
|
|
206
|
+
if (value instanceof DuckDBTimestampTZValue) {
|
|
207
|
+
const millis = value.micros / 1000n;
|
|
208
|
+
if (MIN_DATE_MILLIS <= millis && millis <= MAX_DATE_MILLIS) {
|
|
209
|
+
return new Date(Number(millis));
|
|
210
|
+
}
|
|
211
|
+
throw new Error(
|
|
212
|
+
`TIMESTAMPTZ value out of range for JS Date: ${value.micros} micros`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
throw new Error(`Expected DuckDBTimestampTZValue`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export function dateFromTimestampTZNanosecondsValue(value: DuckDBValue): Date {
|
|
219
|
+
if (value instanceof DuckDBTimestampTZNanosecondsValue) {
|
|
220
|
+
const millis = value.nanos / 1000000n;
|
|
221
|
+
if (
|
|
222
|
+
isFiniteNanos(value.nanos) &&
|
|
223
|
+
MIN_DATE_MILLIS <= millis &&
|
|
224
|
+
millis <= MAX_DATE_MILLIS
|
|
225
|
+
) {
|
|
226
|
+
return new Date(Number(millis));
|
|
227
|
+
}
|
|
228
|
+
throw new Error(
|
|
229
|
+
`TIMESTAMP_TZ_NS value out of range for JS Date: ${value.nanos} nanos`
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
throw new Error(`Expected DuckDBTimestampTZNanosecondsValue`);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function objectFromIntervalValue(value: DuckDBValue): {
|
|
236
|
+
months: number;
|
|
237
|
+
days: number;
|
|
238
|
+
micros: bigint;
|
|
239
|
+
} {
|
|
240
|
+
if (value instanceof DuckDBIntervalValue) {
|
|
241
|
+
return {
|
|
242
|
+
months: value.months,
|
|
243
|
+
days: value.days,
|
|
244
|
+
micros: value.micros,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
throw new Error(`Expected DuckDBIntervalValue`);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function jsonObjectFromIntervalValue(value: DuckDBValue): {
|
|
251
|
+
months: number;
|
|
252
|
+
days: number;
|
|
253
|
+
micros: string;
|
|
254
|
+
} {
|
|
255
|
+
if (value instanceof DuckDBIntervalValue) {
|
|
256
|
+
return {
|
|
257
|
+
months: value.months,
|
|
258
|
+
days: value.days,
|
|
259
|
+
micros: String(value.micros),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
throw new Error(`Expected DuckDBIntervalValue`);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function doubleFromDecimalValue(value: DuckDBValue): number {
|
|
266
|
+
if (value instanceof DuckDBDecimalValue) {
|
|
267
|
+
return value.toDouble();
|
|
268
|
+
}
|
|
269
|
+
throw new Error(`Expected DuckDBDecimalValue`);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function arrayFromListValue<T>(
|
|
273
|
+
value: DuckDBValue,
|
|
274
|
+
type: DuckDBType,
|
|
275
|
+
converter: DuckDBValueConverter<T>
|
|
276
|
+
): (T | null)[] {
|
|
277
|
+
if (value instanceof DuckDBListValue && type instanceof DuckDBListType) {
|
|
278
|
+
return value.items.map((v) => converter(v, type.valueType, converter));
|
|
279
|
+
}
|
|
280
|
+
throw new Error(`Expected DuckDBListValue and DuckDBListType`);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function objectFromStructValue<T>(
|
|
284
|
+
value: DuckDBValue,
|
|
285
|
+
type: DuckDBType,
|
|
286
|
+
converter: DuckDBValueConverter<T>
|
|
287
|
+
): { [key: string]: T | null } {
|
|
288
|
+
if (value instanceof DuckDBStructValue && type instanceof DuckDBStructType) {
|
|
289
|
+
const result: { [key: string]: T | null } = {};
|
|
290
|
+
for (const key in value.entries) {
|
|
291
|
+
result[key] = converter(
|
|
292
|
+
value.entries[key],
|
|
293
|
+
type.typeForEntry(key),
|
|
294
|
+
converter
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
return result;
|
|
298
|
+
}
|
|
299
|
+
throw new Error(`Expected DuckDBStructValue and DuckDBStructType`);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function arrayFromTupleValue<T>(
|
|
303
|
+
value: DuckDBValue,
|
|
304
|
+
type: DuckDBType,
|
|
305
|
+
converter: DuckDBValueConverter<T>
|
|
306
|
+
): (T | null)[] {
|
|
307
|
+
if (
|
|
308
|
+
value instanceof DuckDBTupleValue &&
|
|
309
|
+
type instanceof DuckDBStructType &&
|
|
310
|
+
type.isTuple
|
|
311
|
+
) {
|
|
312
|
+
return value.items.map((item, i) =>
|
|
313
|
+
converter(item, type.entryTypes[i], converter)
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
throw new Error(`Expected DuckDBTupleValue and tuple DuckDBStructType`);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Converts a STRUCT value: a tuple (see `DuckDBStructType.isTuple`) to an
|
|
321
|
+
* array of its items, and a named struct to an object keyed by entry name.
|
|
322
|
+
*/
|
|
323
|
+
export function objectOrArrayFromStructValue<T>(
|
|
324
|
+
value: DuckDBValue,
|
|
325
|
+
type: DuckDBType,
|
|
326
|
+
converter: DuckDBValueConverter<T>
|
|
327
|
+
): { [key: string]: T | null } | (T | null)[] {
|
|
328
|
+
if (value instanceof DuckDBTupleValue) {
|
|
329
|
+
return arrayFromTupleValue(value, type, converter);
|
|
330
|
+
}
|
|
331
|
+
return objectFromStructValue(value, type, converter);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export function objectArrayFromMapValue<T>(
|
|
335
|
+
value: DuckDBValue,
|
|
336
|
+
type: DuckDBType,
|
|
337
|
+
converter: DuckDBValueConverter<T>
|
|
338
|
+
): { key: T | null; value: T | null }[] {
|
|
339
|
+
if (value instanceof DuckDBMapValue && type instanceof DuckDBMapType) {
|
|
340
|
+
return value.entries.map((entry) => ({
|
|
341
|
+
key: converter(entry.key, type.keyType, converter),
|
|
342
|
+
value: converter(entry.value, type.valueType, converter),
|
|
343
|
+
}));
|
|
344
|
+
}
|
|
345
|
+
throw new Error(`Expected DuckDBMapValue and DuckDBMapType`);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function arrayFromArrayValue<T>(
|
|
349
|
+
value: DuckDBValue,
|
|
350
|
+
type: DuckDBType,
|
|
351
|
+
converter: DuckDBValueConverter<T>
|
|
352
|
+
): (T | null)[] {
|
|
353
|
+
if (value instanceof DuckDBArrayValue && type instanceof DuckDBArrayType) {
|
|
354
|
+
return value.items.map((v) => converter(v, type.valueType, converter));
|
|
355
|
+
}
|
|
356
|
+
throw new Error(`Expected DuckDBArrayValue and DuckDBArrayType`);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function objectFromUnionValue<T>(
|
|
360
|
+
value: DuckDBValue,
|
|
361
|
+
type: DuckDBType,
|
|
362
|
+
converter: DuckDBValueConverter<T>
|
|
363
|
+
): { tag: string; value: T | null } {
|
|
364
|
+
if (value instanceof DuckDBUnionValue && type instanceof DuckDBUnionType) {
|
|
365
|
+
return {
|
|
366
|
+
tag: value.tag,
|
|
367
|
+
value: converter(
|
|
368
|
+
value.value,
|
|
369
|
+
type.memberTypeForTag(value.tag),
|
|
370
|
+
converter
|
|
371
|
+
),
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
throw new Error(`Expected DuckDBUnionValue and DuckDBUnionType`);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Converter for VARIANT values: unwraps the `DuckDBVariantValue` wrapper
|
|
379
|
+
* and recursively walks the inner value, re-dispatching each scalar leaf
|
|
380
|
+
* through the supplied converter.
|
|
381
|
+
*
|
|
382
|
+
* A walker is needed (rather than just dispatching through the converter)
|
|
383
|
+
* because VARIANT containers are *heterogeneous* — items in a list or
|
|
384
|
+
* fields in a struct can have different types — while
|
|
385
|
+
* `arrayFromListValue` / `objectFromStructValue` / `objectArrayFromMapValue`
|
|
386
|
+
* assume a homogeneous element type pulled from the enclosing
|
|
387
|
+
* `DuckDBListType` / `DuckDBStructType` / `DuckDBMapType`. The walker
|
|
388
|
+
* emits the same output shape those converters do, and prefers the
|
|
389
|
+
* `DuckDBVariantValue.type` recorded by the decoder over `typeForValue`
|
|
390
|
+
* inference whenever a wrapper is encountered.
|
|
391
|
+
*/
|
|
392
|
+
export function fromVariantValue<T>(
|
|
393
|
+
value: DuckDBValue,
|
|
394
|
+
_type: DuckDBType,
|
|
395
|
+
converter: DuckDBValueConverter<T>
|
|
396
|
+
): T | null {
|
|
397
|
+
if (!(value instanceof DuckDBVariantValue)) {
|
|
398
|
+
throw new Error(`Expected DuckDBVariantValue`);
|
|
399
|
+
}
|
|
400
|
+
return convertVariantInner(value.value, converter, value.type);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function convertVariantInner<T>(
|
|
404
|
+
value: DuckDBValue,
|
|
405
|
+
converter: DuckDBValueConverter<T>,
|
|
406
|
+
typeHint?: DuckDBType
|
|
407
|
+
): T | null {
|
|
408
|
+
if (value === null) {
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
if (value instanceof DuckDBVariantValue) {
|
|
412
|
+
// Variant-of-variant (or a wrapped child inside a LIST(VARIANT)).
|
|
413
|
+
// Prefer the wrapper's exact type as the hint for further walking.
|
|
414
|
+
return convertVariantInner(value.value, converter, value.type ?? typeHint);
|
|
415
|
+
}
|
|
416
|
+
if (value instanceof DuckDBListValue || value instanceof DuckDBArrayValue) {
|
|
417
|
+
return value.items.map((item) =>
|
|
418
|
+
convertVariantInner(item, converter)
|
|
419
|
+
) as unknown as T;
|
|
420
|
+
}
|
|
421
|
+
if (value instanceof DuckDBStructValue) {
|
|
422
|
+
const result: { [key: string]: T | null } = {};
|
|
423
|
+
for (const key in value.entries) {
|
|
424
|
+
result[key] = convertVariantInner(value.entries[key], converter);
|
|
425
|
+
}
|
|
426
|
+
return result as unknown as T;
|
|
427
|
+
}
|
|
428
|
+
if (value instanceof DuckDBTupleValue) {
|
|
429
|
+
return value.items.map((item) =>
|
|
430
|
+
convertVariantInner(item, converter)
|
|
431
|
+
) as unknown as T;
|
|
432
|
+
}
|
|
433
|
+
if (value instanceof DuckDBMapValue) {
|
|
434
|
+
return value.entries.map((entry) => ({
|
|
435
|
+
key: convertVariantInner(entry.key, converter),
|
|
436
|
+
value: convertVariantInner(entry.value, converter),
|
|
437
|
+
})) as unknown as T;
|
|
438
|
+
}
|
|
439
|
+
if (value instanceof DuckDBUnionValue) {
|
|
440
|
+
return {
|
|
441
|
+
tag: value.tag,
|
|
442
|
+
value: convertVariantInner(value.value, converter),
|
|
443
|
+
} as unknown as T;
|
|
444
|
+
}
|
|
445
|
+
// Scalar (or non-recursable rich value like DECIMAL, BLOB, DATE, etc.):
|
|
446
|
+
// dispatch through the regular converter. Use `typeHint` (carried in
|
|
447
|
+
// from a wrapper) for type fidelity when available; fall back to
|
|
448
|
+
// `typeForValue` otherwise.
|
|
449
|
+
const inferredType = typeHint ?? typeForValue(value);
|
|
450
|
+
// `typeForValue` infers BIGINT for any JS integer number outside INT32
|
|
451
|
+
// range (e.g. a UINT32 VARIANT payload of 4_294_967_295). The BIGINT
|
|
452
|
+
// converters expect a `bigint`, not a `number`, and would otherwise
|
|
453
|
+
// throw. Coerce to bigint so the dispatch lands on a compatible reader.
|
|
454
|
+
if (
|
|
455
|
+
typeof value === 'number' &&
|
|
456
|
+
inferredType.typeId === DuckDBTypeId.BIGINT
|
|
457
|
+
) {
|
|
458
|
+
return converter(BigInt(value), inferredType, converter);
|
|
459
|
+
}
|
|
460
|
+
return converter(value, inferredType, converter);
|
|
461
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './vectors';
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { createDuckDBValueConverter } from './createDuckDBValueConverter';
|
|
2
|
+
import { DuckDBTypeId } from './DuckDBTypeId';
|
|
3
|
+
import { DuckDBValueConverterFor } from './DuckDBValueConverter';
|
|
4
|
+
import {
|
|
5
|
+
arrayFromArrayValue,
|
|
6
|
+
arrayFromListValue,
|
|
7
|
+
bigintFromBigIntValue,
|
|
8
|
+
bigintFromTimeNSValue,
|
|
9
|
+
bigintFromTimeValue,
|
|
10
|
+
booleanFromValue,
|
|
11
|
+
bytesFromBitValue,
|
|
12
|
+
bytesFromBlobValue,
|
|
13
|
+
bytesFromGeometryValue,
|
|
14
|
+
dateFromDateValue,
|
|
15
|
+
dateFromTimestampMillisecondsValue,
|
|
16
|
+
dateFromTimestampNanosecondsValue,
|
|
17
|
+
dateFromTimestampSecondsValue,
|
|
18
|
+
dateFromTimestampTZValue,
|
|
19
|
+
dateFromTimestampTZNanosecondsValue,
|
|
20
|
+
dateFromTimestampValue,
|
|
21
|
+
doubleFromDecimalValue,
|
|
22
|
+
fromVariantValue,
|
|
23
|
+
nullConverter,
|
|
24
|
+
numberFromValue,
|
|
25
|
+
objectArrayFromMapValue,
|
|
26
|
+
objectFromIntervalValue,
|
|
27
|
+
objectOrArrayFromStructValue,
|
|
28
|
+
objectFromTimeTZValue,
|
|
29
|
+
objectFromUnionValue,
|
|
30
|
+
stringFromValue,
|
|
31
|
+
unsupportedConverter,
|
|
32
|
+
} from './DuckDBValueConverters';
|
|
33
|
+
import { JS } from './JS';
|
|
34
|
+
import { JSTypeForTypeId } from './JSTypeForTypeId';
|
|
35
|
+
|
|
36
|
+
const JSConvertersByTypeId: {
|
|
37
|
+
[Id in DuckDBTypeId]: DuckDBValueConverterFor<JSTypeForTypeId[Id], JS>;
|
|
38
|
+
} = {
|
|
39
|
+
[DuckDBTypeId.INVALID]: unsupportedConverter,
|
|
40
|
+
[DuckDBTypeId.BOOLEAN]: booleanFromValue,
|
|
41
|
+
[DuckDBTypeId.TINYINT]: numberFromValue,
|
|
42
|
+
[DuckDBTypeId.SMALLINT]: numberFromValue,
|
|
43
|
+
[DuckDBTypeId.INTEGER]: numberFromValue,
|
|
44
|
+
[DuckDBTypeId.BIGINT]: bigintFromBigIntValue,
|
|
45
|
+
[DuckDBTypeId.UTINYINT]: numberFromValue,
|
|
46
|
+
[DuckDBTypeId.USMALLINT]: numberFromValue,
|
|
47
|
+
[DuckDBTypeId.UINTEGER]: numberFromValue,
|
|
48
|
+
[DuckDBTypeId.UBIGINT]: bigintFromBigIntValue,
|
|
49
|
+
[DuckDBTypeId.FLOAT]: numberFromValue,
|
|
50
|
+
[DuckDBTypeId.DOUBLE]: numberFromValue,
|
|
51
|
+
[DuckDBTypeId.TIMESTAMP]: dateFromTimestampValue,
|
|
52
|
+
[DuckDBTypeId.DATE]: dateFromDateValue,
|
|
53
|
+
[DuckDBTypeId.TIME]: bigintFromTimeValue,
|
|
54
|
+
[DuckDBTypeId.INTERVAL]: objectFromIntervalValue,
|
|
55
|
+
[DuckDBTypeId.HUGEINT]: bigintFromBigIntValue,
|
|
56
|
+
[DuckDBTypeId.UHUGEINT]: bigintFromBigIntValue,
|
|
57
|
+
[DuckDBTypeId.VARCHAR]: stringFromValue,
|
|
58
|
+
[DuckDBTypeId.BLOB]: bytesFromBlobValue,
|
|
59
|
+
[DuckDBTypeId.DECIMAL]: doubleFromDecimalValue,
|
|
60
|
+
[DuckDBTypeId.TIMESTAMP_S]: dateFromTimestampSecondsValue,
|
|
61
|
+
[DuckDBTypeId.TIMESTAMP_MS]: dateFromTimestampMillisecondsValue,
|
|
62
|
+
[DuckDBTypeId.TIMESTAMP_NS]: dateFromTimestampNanosecondsValue,
|
|
63
|
+
[DuckDBTypeId.ENUM]: stringFromValue,
|
|
64
|
+
[DuckDBTypeId.LIST]: arrayFromListValue,
|
|
65
|
+
[DuckDBTypeId.STRUCT]: objectOrArrayFromStructValue,
|
|
66
|
+
[DuckDBTypeId.MAP]: objectArrayFromMapValue,
|
|
67
|
+
[DuckDBTypeId.ARRAY]: arrayFromArrayValue,
|
|
68
|
+
[DuckDBTypeId.UUID]: stringFromValue,
|
|
69
|
+
[DuckDBTypeId.UNION]: objectFromUnionValue,
|
|
70
|
+
[DuckDBTypeId.BIT]: bytesFromBitValue,
|
|
71
|
+
[DuckDBTypeId.TIME_TZ]: objectFromTimeTZValue,
|
|
72
|
+
[DuckDBTypeId.TIMESTAMP_TZ]: dateFromTimestampTZValue,
|
|
73
|
+
[DuckDBTypeId.ANY]: unsupportedConverter,
|
|
74
|
+
[DuckDBTypeId.BIGNUM]: bigintFromBigIntValue,
|
|
75
|
+
[DuckDBTypeId.SQLNULL]: nullConverter,
|
|
76
|
+
[DuckDBTypeId.STRING_LITERAL]: unsupportedConverter,
|
|
77
|
+
[DuckDBTypeId.INTEGER_LITERAL]: unsupportedConverter,
|
|
78
|
+
[DuckDBTypeId.TIME_NS]: bigintFromTimeNSValue,
|
|
79
|
+
[DuckDBTypeId.GEOMETRY]: bytesFromGeometryValue,
|
|
80
|
+
[DuckDBTypeId.VARIANT]: fromVariantValue,
|
|
81
|
+
[DuckDBTypeId.TIMESTAMP_TZ_NS]: dateFromTimestampTZNanosecondsValue,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const JSDuckDBValueConverter =
|
|
85
|
+
createDuckDBValueConverter<JS>(JSConvertersByTypeId);
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { DuckDBType } from './DuckDBType';
|
|
2
|
+
import { DuckDBTypeId } from './DuckDBTypeId';
|
|
3
|
+
import type { JS } from './JS';
|
|
4
|
+
import type { DuckDBVariantValue } from './values';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The JS type accepted by `jsToDuckDBValue` for each DuckDB type id: the write
|
|
8
|
+
* counterpart of `JSTypeForTypeId`.
|
|
9
|
+
*
|
|
10
|
+
* Everything `JSTypeForTypeId` produces, this table accepts — see the
|
|
11
|
+
* round-trip assertion in `test/js-writes.test.ts` — but not the reverse.
|
|
12
|
+
* Writes are deliberately wider where a second spelling is unambiguous: the
|
|
13
|
+
* 64-bit and wider integers take `number` as well as `bigint`, and DECIMAL
|
|
14
|
+
* takes either a double or a raw scaled `bigint`. VARIANT is the one
|
|
15
|
+
* exception in the other direction; see below.
|
|
16
|
+
*
|
|
17
|
+
* NULL is not in the table: `createToDuckDBValueConverter` maps `null` to
|
|
18
|
+
* `null` for every type, so a nullable value is `JSInputTypeForTypeId[Id] |
|
|
19
|
+
* null`.
|
|
20
|
+
*
|
|
21
|
+
* Type ids that cannot be written map to `never`: `INVALID`, `ANY`,
|
|
22
|
+
* `STRING_LITERAL` and `INTEGER_LITERAL`, none of which can appear in a result
|
|
23
|
+
* either.
|
|
24
|
+
*
|
|
25
|
+
* VARIANT is the one place the write side is *narrower* than the read side. A
|
|
26
|
+
* VARIANT holds its own type, so writing one means saying what that type is.
|
|
27
|
+
* A `DuckDBVariantValue` carries it — either explicitly, as
|
|
28
|
+
* `variantValue(structValue({k: 'n'}), STRUCT({k: VARCHAR}))`, or inferred by
|
|
29
|
+
* `typeForValue` from a wrapped inner value — and a bare boolean, number,
|
|
30
|
+
* bigint or string is unambiguous enough to infer on its own. A bare `Date`,
|
|
31
|
+
* `Uint8Array`, array or object is not: each maps to several DuckDB types, so
|
|
32
|
+
* `typeForValue` yields ANY and the write fails. Wrap those to say which.
|
|
33
|
+
*
|
|
34
|
+
* Reading a VARIANT discards that type, which is why the round-trip
|
|
35
|
+
* containment the other ids satisfy does not hold here: the read table gives
|
|
36
|
+
* `JS | null`, and JS alone is not enough to write back.
|
|
37
|
+
*
|
|
38
|
+
* This interface is exhaustive over `DuckDBTypeId`, and is the annotation
|
|
39
|
+
* source for the converter map in `jsToDuckDBValue`; adding a type id without
|
|
40
|
+
* adding it here is a compile error.
|
|
41
|
+
*/
|
|
42
|
+
export interface JSInputTypeForTypeId {
|
|
43
|
+
[DuckDBTypeId.INVALID]: never;
|
|
44
|
+
[DuckDBTypeId.BOOLEAN]: boolean;
|
|
45
|
+
[DuckDBTypeId.TINYINT]: number;
|
|
46
|
+
[DuckDBTypeId.SMALLINT]: number;
|
|
47
|
+
[DuckDBTypeId.INTEGER]: number;
|
|
48
|
+
[DuckDBTypeId.BIGINT]: bigint | number;
|
|
49
|
+
[DuckDBTypeId.UTINYINT]: number;
|
|
50
|
+
[DuckDBTypeId.USMALLINT]: number;
|
|
51
|
+
[DuckDBTypeId.UINTEGER]: number;
|
|
52
|
+
[DuckDBTypeId.UBIGINT]: bigint | number;
|
|
53
|
+
[DuckDBTypeId.FLOAT]: number;
|
|
54
|
+
[DuckDBTypeId.DOUBLE]: number;
|
|
55
|
+
[DuckDBTypeId.TIMESTAMP]: Date;
|
|
56
|
+
[DuckDBTypeId.DATE]: Date;
|
|
57
|
+
[DuckDBTypeId.TIME]: bigint;
|
|
58
|
+
[DuckDBTypeId.INTERVAL]: { months: number; days: number; micros: bigint };
|
|
59
|
+
[DuckDBTypeId.HUGEINT]: bigint | number;
|
|
60
|
+
[DuckDBTypeId.UHUGEINT]: bigint | number;
|
|
61
|
+
[DuckDBTypeId.VARCHAR]: string;
|
|
62
|
+
[DuckDBTypeId.BLOB]: Uint8Array;
|
|
63
|
+
[DuckDBTypeId.DECIMAL]: number | bigint;
|
|
64
|
+
[DuckDBTypeId.TIMESTAMP_S]: Date;
|
|
65
|
+
[DuckDBTypeId.TIMESTAMP_MS]: Date;
|
|
66
|
+
[DuckDBTypeId.TIMESTAMP_NS]: Date;
|
|
67
|
+
[DuckDBTypeId.ENUM]: string;
|
|
68
|
+
[DuckDBTypeId.LIST]: readonly (JS | null)[];
|
|
69
|
+
[DuckDBTypeId.STRUCT]:
|
|
70
|
+
| Readonly<Record<string, JS | null>>
|
|
71
|
+
| readonly (JS | null)[];
|
|
72
|
+
[DuckDBTypeId.MAP]: readonly { key: JS | null; value: JS | null }[];
|
|
73
|
+
[DuckDBTypeId.ARRAY]: readonly (JS | null)[];
|
|
74
|
+
[DuckDBTypeId.UUID]: string;
|
|
75
|
+
[DuckDBTypeId.UNION]: { tag: string; value: JS | null };
|
|
76
|
+
[DuckDBTypeId.BIT]: Uint8Array;
|
|
77
|
+
[DuckDBTypeId.TIME_TZ]: { micros: bigint; offset: number };
|
|
78
|
+
[DuckDBTypeId.TIMESTAMP_TZ]: Date;
|
|
79
|
+
[DuckDBTypeId.ANY]: never;
|
|
80
|
+
[DuckDBTypeId.BIGNUM]: bigint | number;
|
|
81
|
+
[DuckDBTypeId.SQLNULL]: null;
|
|
82
|
+
[DuckDBTypeId.STRING_LITERAL]: never;
|
|
83
|
+
[DuckDBTypeId.INTEGER_LITERAL]: never;
|
|
84
|
+
[DuckDBTypeId.TIME_NS]: bigint;
|
|
85
|
+
[DuckDBTypeId.GEOMETRY]: Uint8Array;
|
|
86
|
+
[DuckDBTypeId.VARIANT]:
|
|
87
|
+
| DuckDBVariantValue
|
|
88
|
+
| boolean
|
|
89
|
+
| number
|
|
90
|
+
| bigint
|
|
91
|
+
| string;
|
|
92
|
+
[DuckDBTypeId.TIMESTAMP_TZ_NS]: Date;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The JS type accepted by `jsToDuckDBValue` for a given `DuckDBType`.
|
|
97
|
+
*
|
|
98
|
+
* Excludes NULL; use `JSInputTypeFor<T> | null` for a nullable value.
|
|
99
|
+
*/
|
|
100
|
+
export type JSInputTypeFor<T extends DuckDBType> =
|
|
101
|
+
JSInputTypeForTypeId[T['typeId']];
|