@avidian/duckdb-bun 0.0.0-stage → 1.0.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/LICENSE +22 -0
- package/README.md +284 -2
- package/package.json +95 -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 +200 -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 +150 -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,355 @@
|
|
|
1
|
+
import duckdb, { Value } from "../../../src/bindings/index.ts";
|
|
2
|
+
import { DuckDBType } from './DuckDBType';
|
|
3
|
+
import { DuckDBTypeId } from './DuckDBTypeId';
|
|
4
|
+
import { typeForValue } from './typeForValue';
|
|
5
|
+
import {
|
|
6
|
+
DuckDBArrayValue,
|
|
7
|
+
DuckDBBitValue,
|
|
8
|
+
DuckDBBlobValue,
|
|
9
|
+
DuckDBDateValue,
|
|
10
|
+
DuckDBDecimalValue,
|
|
11
|
+
DuckDBGeometryValue,
|
|
12
|
+
DuckDBIntervalValue,
|
|
13
|
+
DuckDBListValue,
|
|
14
|
+
DuckDBMapValue,
|
|
15
|
+
DuckDBStructValue,
|
|
16
|
+
DuckDBTimeNSValue,
|
|
17
|
+
DuckDBTimestampMillisecondsValue,
|
|
18
|
+
DuckDBTimestampNanosecondsValue,
|
|
19
|
+
DuckDBTimestampSecondsValue,
|
|
20
|
+
DuckDBTimestampTZValue,
|
|
21
|
+
DuckDBTimestampTZNanosecondsValue,
|
|
22
|
+
DuckDBTimestampValue,
|
|
23
|
+
DuckDBTimeTZValue,
|
|
24
|
+
DuckDBTimeValue,
|
|
25
|
+
DuckDBTupleValue,
|
|
26
|
+
DuckDBUnionValue,
|
|
27
|
+
DuckDBUUIDValue,
|
|
28
|
+
DuckDBValue,
|
|
29
|
+
DuckDBVariantValue,
|
|
30
|
+
} from './values';
|
|
31
|
+
|
|
32
|
+
export function createValue(type: DuckDBType, input: DuckDBValue): Value {
|
|
33
|
+
if (input === null) {
|
|
34
|
+
return duckdb.create_null_value();
|
|
35
|
+
}
|
|
36
|
+
const { typeId } = type;
|
|
37
|
+
switch (typeId) {
|
|
38
|
+
case DuckDBTypeId.BOOLEAN:
|
|
39
|
+
if (typeof input === 'boolean') {
|
|
40
|
+
return duckdb.create_bool(input);
|
|
41
|
+
}
|
|
42
|
+
throw new Error(`input is not a boolean`);
|
|
43
|
+
case DuckDBTypeId.TINYINT:
|
|
44
|
+
if (typeof input === 'number') {
|
|
45
|
+
return duckdb.create_int8(input);
|
|
46
|
+
}
|
|
47
|
+
throw new Error(`input is not a number`);
|
|
48
|
+
case DuckDBTypeId.SMALLINT:
|
|
49
|
+
if (typeof input === 'number') {
|
|
50
|
+
return duckdb.create_int16(input);
|
|
51
|
+
}
|
|
52
|
+
throw new Error(`input is not a number`);
|
|
53
|
+
case DuckDBTypeId.INTEGER:
|
|
54
|
+
if (typeof input === 'number') {
|
|
55
|
+
return duckdb.create_int32(input);
|
|
56
|
+
}
|
|
57
|
+
throw new Error(`input is not a number`);
|
|
58
|
+
case DuckDBTypeId.BIGINT:
|
|
59
|
+
if (typeof input === 'bigint' || typeof input === 'number') {
|
|
60
|
+
return duckdb.create_int64(BigInt(input));
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`input is not a bigint`);
|
|
63
|
+
case DuckDBTypeId.UTINYINT:
|
|
64
|
+
if (typeof input === 'number') {
|
|
65
|
+
return duckdb.create_uint8(input);
|
|
66
|
+
}
|
|
67
|
+
throw new Error(`input is not a number`);
|
|
68
|
+
case DuckDBTypeId.USMALLINT:
|
|
69
|
+
if (typeof input === 'number') {
|
|
70
|
+
return duckdb.create_uint16(input);
|
|
71
|
+
}
|
|
72
|
+
throw new Error(`input is not a number`);
|
|
73
|
+
case DuckDBTypeId.UINTEGER:
|
|
74
|
+
if (typeof input === 'number') {
|
|
75
|
+
return duckdb.create_uint32(input);
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`input is not a number`);
|
|
78
|
+
case DuckDBTypeId.UBIGINT:
|
|
79
|
+
if (typeof input === 'bigint' || typeof input === 'number') {
|
|
80
|
+
return duckdb.create_uint64(BigInt(input));
|
|
81
|
+
}
|
|
82
|
+
throw new Error(`input is not a bigint`);
|
|
83
|
+
case DuckDBTypeId.FLOAT:
|
|
84
|
+
if (typeof input === 'number') {
|
|
85
|
+
return duckdb.create_float(input);
|
|
86
|
+
}
|
|
87
|
+
throw new Error(`input is not a number`);
|
|
88
|
+
case DuckDBTypeId.DOUBLE:
|
|
89
|
+
if (typeof input === 'number') {
|
|
90
|
+
return duckdb.create_double(input);
|
|
91
|
+
}
|
|
92
|
+
throw new Error(`input is not a number`);
|
|
93
|
+
case DuckDBTypeId.TIMESTAMP:
|
|
94
|
+
if (input instanceof DuckDBTimestampValue) {
|
|
95
|
+
return duckdb.create_timestamp(input);
|
|
96
|
+
}
|
|
97
|
+
throw new Error(`input is not a DuckDBTimestampValue`);
|
|
98
|
+
case DuckDBTypeId.DATE:
|
|
99
|
+
if (input instanceof DuckDBDateValue) {
|
|
100
|
+
return duckdb.create_date(input);
|
|
101
|
+
}
|
|
102
|
+
throw new Error(`input is not a DuckDBDateValue`);
|
|
103
|
+
case DuckDBTypeId.TIME:
|
|
104
|
+
if (input instanceof DuckDBTimeValue) {
|
|
105
|
+
return duckdb.create_time(input);
|
|
106
|
+
}
|
|
107
|
+
throw new Error(`input is not a DuckDBTimeValue`);
|
|
108
|
+
case DuckDBTypeId.INTERVAL:
|
|
109
|
+
if (input instanceof DuckDBIntervalValue) {
|
|
110
|
+
return duckdb.create_interval(input);
|
|
111
|
+
}
|
|
112
|
+
throw new Error(`input is not a DuckDBIntervalValue`);
|
|
113
|
+
case DuckDBTypeId.HUGEINT:
|
|
114
|
+
if (typeof input === 'bigint') {
|
|
115
|
+
return duckdb.create_hugeint(input);
|
|
116
|
+
}
|
|
117
|
+
throw new Error(`input is not a bigint`);
|
|
118
|
+
case DuckDBTypeId.UHUGEINT:
|
|
119
|
+
if (typeof input === 'bigint') {
|
|
120
|
+
return duckdb.create_uhugeint(input);
|
|
121
|
+
}
|
|
122
|
+
throw new Error(`input is not a bigint`);
|
|
123
|
+
case DuckDBTypeId.VARCHAR:
|
|
124
|
+
if (typeof input === 'string') {
|
|
125
|
+
return duckdb.create_varchar(input);
|
|
126
|
+
}
|
|
127
|
+
throw new Error(`input is not a string`);
|
|
128
|
+
case DuckDBTypeId.BLOB:
|
|
129
|
+
if (input instanceof DuckDBBlobValue) {
|
|
130
|
+
return duckdb.create_blob(input.bytes);
|
|
131
|
+
}
|
|
132
|
+
throw new Error(`input is not a DuckDBBlobValue`);
|
|
133
|
+
case DuckDBTypeId.DECIMAL:
|
|
134
|
+
if (input instanceof DuckDBDecimalValue) {
|
|
135
|
+
return duckdb.create_decimal(input);
|
|
136
|
+
}
|
|
137
|
+
throw new Error(`input is not a DuckDBDecimalValue`);
|
|
138
|
+
case DuckDBTypeId.TIMESTAMP_S:
|
|
139
|
+
if (input instanceof DuckDBTimestampSecondsValue) {
|
|
140
|
+
return duckdb.create_timestamp_s(input);
|
|
141
|
+
}
|
|
142
|
+
throw new Error(`input is not a DuckDBTimestampSecondsValue`);
|
|
143
|
+
case DuckDBTypeId.TIMESTAMP_MS:
|
|
144
|
+
if (input instanceof DuckDBTimestampMillisecondsValue) {
|
|
145
|
+
return duckdb.create_timestamp_ms(input);
|
|
146
|
+
}
|
|
147
|
+
throw new Error(`input is not a DuckDBTimestampMillisecondsValue`);
|
|
148
|
+
case DuckDBTypeId.TIMESTAMP_NS:
|
|
149
|
+
if (input instanceof DuckDBTimestampNanosecondsValue) {
|
|
150
|
+
return duckdb.create_timestamp_ns(input);
|
|
151
|
+
}
|
|
152
|
+
throw new Error(`input is not a DuckDBTimestampNanosecondsValue`);
|
|
153
|
+
case DuckDBTypeId.ENUM:
|
|
154
|
+
if (typeof input === 'string') {
|
|
155
|
+
return duckdb.create_enum_value(
|
|
156
|
+
type.toLogicalType().logical_type,
|
|
157
|
+
type.indexForValue(input)
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
throw new Error(`input is not a string`);
|
|
161
|
+
case DuckDBTypeId.LIST:
|
|
162
|
+
if (input instanceof DuckDBListValue) {
|
|
163
|
+
if (type.valueType.typeId === DuckDBTypeId.ANY) {
|
|
164
|
+
throw new Error(
|
|
165
|
+
'Cannot create lists with item type of ANY. Specify a specific type.'
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return duckdb.create_list_value(
|
|
169
|
+
type.valueType.toLogicalType().logical_type,
|
|
170
|
+
input.items.map((item) => createValue(type.valueType, item))
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
throw new Error(`input is not a DuckDBListValue`);
|
|
174
|
+
case DuckDBTypeId.STRUCT:
|
|
175
|
+
if (type.isTuple) {
|
|
176
|
+
if (input instanceof DuckDBTupleValue) {
|
|
177
|
+
if (
|
|
178
|
+
type.entryTypes.find(
|
|
179
|
+
(entryType) => entryType.typeId === DuckDBTypeId.ANY
|
|
180
|
+
)
|
|
181
|
+
) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
'Cannot create tuples with an entry type of ANY. Specify a specific type.'
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
if (input.items.length !== type.entryCount) {
|
|
187
|
+
throw new Error(
|
|
188
|
+
`Expected ${type.entryCount} items for ${type}, got ${input.items.length}`
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
return duckdb.create_struct_value(
|
|
192
|
+
type.toLogicalType().logical_type,
|
|
193
|
+
input.items.map((item, i) => createValue(type.entryTypes[i], item))
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
throw new Error(`input is not a DuckDBTupleValue`);
|
|
197
|
+
}
|
|
198
|
+
if (input instanceof DuckDBStructValue) {
|
|
199
|
+
if (
|
|
200
|
+
type.entryTypes.find(
|
|
201
|
+
(entryType) => entryType.typeId === DuckDBTypeId.ANY
|
|
202
|
+
)
|
|
203
|
+
) {
|
|
204
|
+
throw new Error(
|
|
205
|
+
'Cannot create structs with an entry type of ANY. Specify a specific type.'
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
// Pair entries by name rather than positionally so that
|
|
209
|
+
// `structValue({b: 'x', a: 1})` against `STRUCT({a: INTEGER, b:
|
|
210
|
+
// VARCHAR})` doesn't silently mispair when the value's insertion
|
|
211
|
+
// order differs from the type's declared field order.
|
|
212
|
+
return duckdb.create_struct_value(
|
|
213
|
+
type.toLogicalType().logical_type,
|
|
214
|
+
type.entryNames.map((name, i) => {
|
|
215
|
+
if (!(name in input.entries)) {
|
|
216
|
+
throw new Error(
|
|
217
|
+
`STRUCT entry '${name}' is missing from input`
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
return createValue(type.entryTypes[i], input.entries[name]);
|
|
221
|
+
})
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
throw new Error(`input is not a DuckDBStructValue`);
|
|
225
|
+
case DuckDBTypeId.MAP:
|
|
226
|
+
if (input instanceof DuckDBMapValue) {
|
|
227
|
+
if (type.keyType.typeId === DuckDBTypeId.ANY) {
|
|
228
|
+
throw new Error(
|
|
229
|
+
'Cannot create maps with key type of ANY. Specify a specific type.'
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
if (type.valueType.typeId === DuckDBTypeId.ANY) {
|
|
233
|
+
throw new Error(
|
|
234
|
+
'Cannot create maps with value type of ANY. Specify a specific type.'
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
return duckdb.create_map_value(
|
|
238
|
+
type.toLogicalType().logical_type,
|
|
239
|
+
input.entries.map((entry) => createValue(type.keyType, entry.key)),
|
|
240
|
+
input.entries.map((entry) => createValue(type.valueType, entry.value))
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
throw new Error(`input is not a DuckDBMapValue`);
|
|
244
|
+
case DuckDBTypeId.ARRAY:
|
|
245
|
+
if (input instanceof DuckDBArrayValue) {
|
|
246
|
+
if (type.valueType.typeId === DuckDBTypeId.ANY) {
|
|
247
|
+
throw new Error(
|
|
248
|
+
'Cannot create arrays with item type of ANY. Specify a specific type.'
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
return duckdb.create_array_value(
|
|
252
|
+
type.valueType.toLogicalType().logical_type,
|
|
253
|
+
input.items.map((item) => createValue(type.valueType, item))
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
throw new Error(`input is not a DuckDBArrayValue`);
|
|
257
|
+
case DuckDBTypeId.UUID:
|
|
258
|
+
if (input instanceof DuckDBUUIDValue) {
|
|
259
|
+
return duckdb.create_uuid(input.toUint128());
|
|
260
|
+
}
|
|
261
|
+
throw new Error(`input is not a bigint`);
|
|
262
|
+
case DuckDBTypeId.UNION:
|
|
263
|
+
if (input instanceof DuckDBUnionValue) {
|
|
264
|
+
const tagIndex = type.memberIndexForTag(input.tag);
|
|
265
|
+
const memberType = type.memberTypes[tagIndex];
|
|
266
|
+
if (memberType.typeId === DuckDBTypeId.ANY) {
|
|
267
|
+
throw new Error('Cannot create union values with type of ANY.');
|
|
268
|
+
}
|
|
269
|
+
return duckdb.create_union_value(
|
|
270
|
+
type.toLogicalType().logical_type,
|
|
271
|
+
tagIndex,
|
|
272
|
+
createValue(memberType, input.value)
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
throw new Error(`input is not a DuckDBUnionValue`);
|
|
276
|
+
case DuckDBTypeId.BIT:
|
|
277
|
+
if (input instanceof DuckDBBitValue) {
|
|
278
|
+
return duckdb.create_bit(input.data);
|
|
279
|
+
}
|
|
280
|
+
throw new Error(`input is not a DuckDBBitValue`);
|
|
281
|
+
case DuckDBTypeId.TIME_TZ:
|
|
282
|
+
if (input instanceof DuckDBTimeTZValue) {
|
|
283
|
+
return duckdb.create_time_tz_value(input);
|
|
284
|
+
}
|
|
285
|
+
throw new Error(`input is not a DuckDBTimeTZValue`);
|
|
286
|
+
case DuckDBTypeId.TIMESTAMP_TZ:
|
|
287
|
+
if (input instanceof DuckDBTimestampTZValue) {
|
|
288
|
+
return duckdb.create_timestamp_tz(input);
|
|
289
|
+
}
|
|
290
|
+
throw new Error(`input is not a DuckDBTimestampTZValue`);
|
|
291
|
+
case DuckDBTypeId.TIMESTAMP_TZ_NS:
|
|
292
|
+
if (input instanceof DuckDBTimestampTZNanosecondsValue) {
|
|
293
|
+
return duckdb.create_timestamp_tz_ns(input);
|
|
294
|
+
}
|
|
295
|
+
throw new Error(`input is not a DuckDBTimestampTZNanosecondsValue`);
|
|
296
|
+
case DuckDBTypeId.ANY:
|
|
297
|
+
throw new Error(
|
|
298
|
+
`Cannot create values of type ANY. Specify a specific type.`
|
|
299
|
+
);
|
|
300
|
+
case DuckDBTypeId.BIGNUM:
|
|
301
|
+
if (typeof input === 'bigint') {
|
|
302
|
+
return duckdb.create_bignum(input);
|
|
303
|
+
}
|
|
304
|
+
throw new Error(`input is not a bigint`);
|
|
305
|
+
case DuckDBTypeId.SQLNULL:
|
|
306
|
+
return duckdb.create_null_value();
|
|
307
|
+
case DuckDBTypeId.STRING_LITERAL:
|
|
308
|
+
throw new Error(`Cannot create values of type STRING_LITERAL.`);
|
|
309
|
+
case DuckDBTypeId.INTEGER_LITERAL:
|
|
310
|
+
throw new Error(`Cannot create values of type INTEGER_LITERAL.`);
|
|
311
|
+
case DuckDBTypeId.TIME_NS:
|
|
312
|
+
if (input instanceof DuckDBTimeNSValue) {
|
|
313
|
+
return duckdb.create_time_ns(input);
|
|
314
|
+
}
|
|
315
|
+
throw new Error(`input is not a DuckDBTimeNSValue`);
|
|
316
|
+
case DuckDBTypeId.GEOMETRY:
|
|
317
|
+
if (input instanceof DuckDBGeometryValue) {
|
|
318
|
+
throw new Error(`Creating values of type GEOMETRY is not yet supported.`);
|
|
319
|
+
// TODO: create_geometry doesn't exist; create_blob results in Failed to cast value: Unimplemented type for cast (BLOB -> GEOMETRY)
|
|
320
|
+
// return duckdb.create_blob(input.bytes);
|
|
321
|
+
}
|
|
322
|
+
throw new Error(`input is not a DuckDBGeometryValue`);
|
|
323
|
+
case DuckDBTypeId.VARIANT: {
|
|
324
|
+
// VARIANT has no direct create primitive in the C API; DuckDB
|
|
325
|
+
// implicitly casts from any compatible type when the target column
|
|
326
|
+
// or parameter is VARIANT. Recurse on the input's natural type so
|
|
327
|
+
// we hand DuckDB a Value it can cast.
|
|
328
|
+
//
|
|
329
|
+
// If the input is a DuckDBVariantValue, prefer its embedded `.type`
|
|
330
|
+
// (set by the decoder from the on-disk variant tag) so heterogeneous
|
|
331
|
+
// content round-trips faithfully. Fall back to `typeForValue` when
|
|
332
|
+
// no type hint is attached. The recursion is wrapped so a
|
|
333
|
+
// mismatched wrapper (e.g. `variantValue('hello', INTEGER)`)
|
|
334
|
+
// surfaces a VARIANT-flavored error rather than a confusing
|
|
335
|
+
// `input is not a number` from a sibling case.
|
|
336
|
+
const inner =
|
|
337
|
+
input instanceof DuckDBVariantValue ? input.value : input;
|
|
338
|
+
const innerType =
|
|
339
|
+
input instanceof DuckDBVariantValue && input.type
|
|
340
|
+
? input.type
|
|
341
|
+
: typeForValue(inner);
|
|
342
|
+
try {
|
|
343
|
+
return createValue(innerType, inner);
|
|
344
|
+
} catch (err) {
|
|
345
|
+
throw new Error(
|
|
346
|
+
`Failed to create VARIANT value (inner type ${innerType}): ${
|
|
347
|
+
(err as Error).message
|
|
348
|
+
}`
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
default:
|
|
353
|
+
throw new Error(`unrecognized type id ${typeId}`);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export {
|
|
2
|
+
double_to_hugeint,
|
|
3
|
+
double_to_uhugeint,
|
|
4
|
+
hugeint_to_double,
|
|
5
|
+
uhugeint_to_double
|
|
6
|
+
} from "../../../src/bindings/index.ts";
|
|
7
|
+
export * from './configurationOptionDescriptions';
|
|
8
|
+
export * from './createDuckDBValueConverter';
|
|
9
|
+
export * from './DuckDBAppender';
|
|
10
|
+
export * from './DuckDBClientContext';
|
|
11
|
+
export * from './DuckDBConnection';
|
|
12
|
+
export * from './DuckDBDataChunk';
|
|
13
|
+
export * from './DuckDBDataChunkWriter';
|
|
14
|
+
export * from './DuckDBExtractedStatements';
|
|
15
|
+
export * from './DuckDBInstance';
|
|
16
|
+
export * from './DuckDBInstanceCache';
|
|
17
|
+
export * from './DuckDBLogicalType';
|
|
18
|
+
export * from './DuckDBMaterializedResult';
|
|
19
|
+
export * from './DuckDBPendingResult';
|
|
20
|
+
export * from './DuckDBPreparedStatement';
|
|
21
|
+
export * from './DuckDBPreparedStatementCollection';
|
|
22
|
+
export * from './DuckDBResult';
|
|
23
|
+
export * from './DuckDBResultReader';
|
|
24
|
+
export * from './DuckDBScalarFunction';
|
|
25
|
+
export * from './DuckDBScalarFunctionBindInfo';
|
|
26
|
+
export * from './DuckDBScalarFunctionInfo';
|
|
27
|
+
export * from './DuckDBScalarFunctionInitInfo';
|
|
28
|
+
export * from './DuckDBTableFunction';
|
|
29
|
+
export * from './DuckDBTableFunctionBindInfo';
|
|
30
|
+
export * from './DuckDBTableFunctionInfo';
|
|
31
|
+
export * from './DuckDBTableFunctionInitInfo';
|
|
32
|
+
export * from './DuckDBType';
|
|
33
|
+
export * from './DuckDBTypeId';
|
|
34
|
+
export * from './DuckDBValueConverter';
|
|
35
|
+
export * from './DuckDBValueConverters';
|
|
36
|
+
export * from './DuckDBVector';
|
|
37
|
+
export * from './ToDuckDBValueConverter';
|
|
38
|
+
export * from './ToDuckDBValueConverters';
|
|
39
|
+
export * from './enums';
|
|
40
|
+
export * from './JS';
|
|
41
|
+
export * from './JSDuckDBValueConverter';
|
|
42
|
+
export * from './JSInputTypeForTypeId';
|
|
43
|
+
export * from './JSToDuckDBValueConverter';
|
|
44
|
+
export * from './JSTypeForTypeId';
|
|
45
|
+
export * from './Json';
|
|
46
|
+
export * from './JsonDuckDBValueConverter';
|
|
47
|
+
export * from './JsonTypeForTypeId';
|
|
48
|
+
export * from './readValue';
|
|
49
|
+
export * from './sql';
|
|
50
|
+
export * from './values';
|
|
51
|
+
export * from './version';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DuckDBDataChunk } from './DuckDBDataChunk';
|
|
2
|
+
import { DuckDBValue } from './values';
|
|
3
|
+
|
|
4
|
+
export function getColumnsFromChunks(
|
|
5
|
+
chunks: readonly DuckDBDataChunk[],
|
|
6
|
+
columnCount: number
|
|
7
|
+
): DuckDBValue[][] {
|
|
8
|
+
const columns: DuckDBValue[][] = Array.from({ length: columnCount }, () => []);
|
|
9
|
+
for (const chunk of chunks) {
|
|
10
|
+
chunk.appendToColumns(columns);
|
|
11
|
+
}
|
|
12
|
+
return columns;
|
|
13
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DuckDBDataChunk } from './DuckDBDataChunk';
|
|
2
|
+
import { DuckDBValue } from './values';
|
|
3
|
+
|
|
4
|
+
export function getColumnsObjectFromChunks(
|
|
5
|
+
chunks: readonly DuckDBDataChunk[],
|
|
6
|
+
columnNames: readonly string[]
|
|
7
|
+
): Record<string, DuckDBValue[]> {
|
|
8
|
+
const columnsObject: Record<string, DuckDBValue[]> = {};
|
|
9
|
+
for (const columnName of columnNames) {
|
|
10
|
+
columnsObject[columnName] = [];
|
|
11
|
+
}
|
|
12
|
+
for (const chunk of chunks) {
|
|
13
|
+
chunk.appendToColumnsObject(columnNames, columnsObject);
|
|
14
|
+
}
|
|
15
|
+
return columnsObject;
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DuckDBDataChunk } from './DuckDBDataChunk';
|
|
2
|
+
import { DuckDBValue } from './values';
|
|
3
|
+
|
|
4
|
+
export function getRowObjectsFromChunks(
|
|
5
|
+
chunks: readonly DuckDBDataChunk[],
|
|
6
|
+
columnNames: readonly string[]
|
|
7
|
+
): Record<string, DuckDBValue>[] {
|
|
8
|
+
const rowObjects: Record<string, DuckDBValue>[] = [];
|
|
9
|
+
for (const chunk of chunks) {
|
|
10
|
+
chunk.appendToRowObjects(columnNames, rowObjects);
|
|
11
|
+
}
|
|
12
|
+
return rowObjects;
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DuckDBDataChunk } from './DuckDBDataChunk';
|
|
2
|
+
import { DuckDBValue } from './values';
|
|
3
|
+
|
|
4
|
+
export function getRowsFromChunks(
|
|
5
|
+
chunks: readonly DuckDBDataChunk[]
|
|
6
|
+
): DuckDBValue[][] {
|
|
7
|
+
const rows: DuckDBValue[][] = [];
|
|
8
|
+
for (const chunk of chunks) {
|
|
9
|
+
chunk.appendToRows(rows);
|
|
10
|
+
}
|
|
11
|
+
return rows;
|
|
12
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import duckdb, { Value } from "../../../src/bindings/index.ts";
|
|
2
|
+
import { DuckDBVector } from './DuckDBVector';
|
|
3
|
+
import { DuckDBValue } from './values';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Converts a `duckdb.Value` into a `DuckDBValue`.
|
|
7
|
+
*
|
|
8
|
+
* Results normally arrive as vectors rather than values, so this is for the
|
|
9
|
+
* places that hand back a value instead: table function parameters, and
|
|
10
|
+
* `duckdb_get_table_names`.
|
|
11
|
+
*
|
|
12
|
+
* It works by referencing the value into a vector of its own and reading the one
|
|
13
|
+
* element back, which reuses the vector reading path rather than duplicating it.
|
|
14
|
+
* That matters for more than tidiness: the value accessors cannot reach inside an
|
|
15
|
+
* ARRAY or a UNION at all, because `duckdb_get_list_child` and
|
|
16
|
+
* `duckdb_get_struct_child` guard on the exact logical type id and return null
|
|
17
|
+
* for both. Going through a vector covers every type, and leaves one
|
|
18
|
+
* implementation of how each type is represented rather than two that can drift.
|
|
19
|
+
*
|
|
20
|
+
* The cost is roughly an extra microsecond per scalar value, which is fine here
|
|
21
|
+
* and would not be in a per-row path. Per-row data already arrives as vectors and
|
|
22
|
+
* never comes through here.
|
|
23
|
+
*/
|
|
24
|
+
export function readValue(value: Value): DuckDBValue {
|
|
25
|
+
// Capacity of one: the vector exists only to hold this single value. It is
|
|
26
|
+
// destroyed when collected, and getItem materializes what it returns, so
|
|
27
|
+
// nothing handed back keeps a reference to the vector's memory.
|
|
28
|
+
const vector = duckdb.create_vector(duckdb.get_value_type(value), 1);
|
|
29
|
+
duckdb.vector_reference_value(vector, value);
|
|
30
|
+
return DuckDBVector.create(vector, 1).getItem(0);
|
|
31
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ANY,
|
|
3
|
+
ARRAY,
|
|
4
|
+
BIGINT,
|
|
5
|
+
BIT,
|
|
6
|
+
BLOB,
|
|
7
|
+
BOOLEAN,
|
|
8
|
+
DATE,
|
|
9
|
+
DECIMAL,
|
|
10
|
+
DOUBLE,
|
|
11
|
+
DuckDBType,
|
|
12
|
+
HUGEINT,
|
|
13
|
+
INTEGER,
|
|
14
|
+
INTERVAL,
|
|
15
|
+
LIST,
|
|
16
|
+
MAP,
|
|
17
|
+
SQLNULL,
|
|
18
|
+
STRUCT,
|
|
19
|
+
TIME,
|
|
20
|
+
TIME_NS,
|
|
21
|
+
TIMESTAMP,
|
|
22
|
+
TIMESTAMP_MS,
|
|
23
|
+
TIMESTAMP_NS,
|
|
24
|
+
TIMESTAMP_S,
|
|
25
|
+
TIMESTAMPTZ,
|
|
26
|
+
TIMESTAMPTZ_NS,
|
|
27
|
+
TIMETZ,
|
|
28
|
+
TUPLE,
|
|
29
|
+
UNION,
|
|
30
|
+
UUID,
|
|
31
|
+
VARCHAR,
|
|
32
|
+
VARIANT,
|
|
33
|
+
} from './DuckDBType';
|
|
34
|
+
import {
|
|
35
|
+
DuckDBArrayValue,
|
|
36
|
+
DuckDBBitValue,
|
|
37
|
+
DuckDBBlobValue,
|
|
38
|
+
DuckDBDateValue,
|
|
39
|
+
DuckDBDecimalValue,
|
|
40
|
+
DuckDBIntervalValue,
|
|
41
|
+
DuckDBListValue,
|
|
42
|
+
DuckDBMapValue,
|
|
43
|
+
DuckDBStructValue,
|
|
44
|
+
DuckDBTimeNSValue,
|
|
45
|
+
DuckDBTimestampMillisecondsValue,
|
|
46
|
+
DuckDBTimestampNanosecondsValue,
|
|
47
|
+
DuckDBTimestampSecondsValue,
|
|
48
|
+
DuckDBTimestampTZValue,
|
|
49
|
+
DuckDBTimestampTZNanosecondsValue,
|
|
50
|
+
DuckDBTimestampValue,
|
|
51
|
+
DuckDBTimeTZValue,
|
|
52
|
+
DuckDBTimeValue,
|
|
53
|
+
DuckDBTupleValue,
|
|
54
|
+
DuckDBUnionValue,
|
|
55
|
+
DuckDBUUIDValue,
|
|
56
|
+
DuckDBValue,
|
|
57
|
+
DuckDBVariantValue,
|
|
58
|
+
} from './values';
|
|
59
|
+
|
|
60
|
+
export function typeForValue(value: DuckDBValue): DuckDBType {
|
|
61
|
+
if (value === null) {
|
|
62
|
+
return SQLNULL;
|
|
63
|
+
} else {
|
|
64
|
+
switch (typeof value) {
|
|
65
|
+
case 'boolean':
|
|
66
|
+
return BOOLEAN;
|
|
67
|
+
case 'number':
|
|
68
|
+
if (Number.isInteger(value)) {
|
|
69
|
+
if (value >= -2_147_483_648 && value <= 2_147_483_647) {
|
|
70
|
+
return INTEGER;
|
|
71
|
+
}
|
|
72
|
+
return BIGINT;
|
|
73
|
+
} else {
|
|
74
|
+
return DOUBLE;
|
|
75
|
+
}
|
|
76
|
+
case 'bigint':
|
|
77
|
+
return HUGEINT;
|
|
78
|
+
case 'string':
|
|
79
|
+
return VARCHAR;
|
|
80
|
+
case 'object':
|
|
81
|
+
if (value instanceof DuckDBArrayValue) {
|
|
82
|
+
return ARRAY(typeForValue(value.items[0]), value.items.length);
|
|
83
|
+
} else if (value instanceof DuckDBBitValue) {
|
|
84
|
+
return BIT;
|
|
85
|
+
} else if (value instanceof DuckDBBlobValue) {
|
|
86
|
+
return BLOB;
|
|
87
|
+
} else if (value instanceof DuckDBDateValue) {
|
|
88
|
+
return DATE;
|
|
89
|
+
} else if (value instanceof DuckDBDecimalValue) {
|
|
90
|
+
return DECIMAL(value.width, value.scale);
|
|
91
|
+
} else if (value instanceof DuckDBIntervalValue) {
|
|
92
|
+
return INTERVAL;
|
|
93
|
+
} else if (value instanceof DuckDBListValue) {
|
|
94
|
+
return LIST(typeForValue(value.items[0]));
|
|
95
|
+
} else if (value instanceof DuckDBMapValue) {
|
|
96
|
+
if (value.entries.length === 0) {
|
|
97
|
+
// An empty map carries no entries to infer key/value types from.
|
|
98
|
+
// Mirror the empty-list path (LIST(ANY)) so createValue raises the
|
|
99
|
+
// same "Cannot create maps with key type of ANY" error, instead of
|
|
100
|
+
// a cryptic "Cannot read properties of undefined (reading 'key')".
|
|
101
|
+
return MAP(ANY, ANY);
|
|
102
|
+
}
|
|
103
|
+
return MAP(
|
|
104
|
+
typeForValue(value.entries[0].key),
|
|
105
|
+
typeForValue(value.entries[0].value)
|
|
106
|
+
);
|
|
107
|
+
} else if (value instanceof DuckDBStructValue) {
|
|
108
|
+
const entryTypes: Record<string, DuckDBType> = {};
|
|
109
|
+
for (const key in value.entries) {
|
|
110
|
+
entryTypes[key] = typeForValue(value.entries[key]);
|
|
111
|
+
}
|
|
112
|
+
return STRUCT(entryTypes);
|
|
113
|
+
} else if (value instanceof DuckDBTimestampMillisecondsValue) {
|
|
114
|
+
return TIMESTAMP_MS;
|
|
115
|
+
} else if (value instanceof DuckDBTimestampNanosecondsValue) {
|
|
116
|
+
return TIMESTAMP_NS;
|
|
117
|
+
} else if (value instanceof DuckDBTimestampSecondsValue) {
|
|
118
|
+
return TIMESTAMP_S;
|
|
119
|
+
} else if (value instanceof DuckDBTimestampTZValue) {
|
|
120
|
+
return TIMESTAMPTZ;
|
|
121
|
+
} else if (value instanceof DuckDBTimestampTZNanosecondsValue) {
|
|
122
|
+
return TIMESTAMPTZ_NS;
|
|
123
|
+
} else if (value instanceof DuckDBTimestampValue) {
|
|
124
|
+
return TIMESTAMP;
|
|
125
|
+
} else if (value instanceof DuckDBTimeTZValue) {
|
|
126
|
+
return TIMETZ;
|
|
127
|
+
} else if (value instanceof DuckDBTimeValue) {
|
|
128
|
+
return TIME;
|
|
129
|
+
} else if (value instanceof DuckDBTimeNSValue) {
|
|
130
|
+
return TIME_NS;
|
|
131
|
+
} else if (value instanceof DuckDBTupleValue) {
|
|
132
|
+
return TUPLE(value.items.map(typeForValue));
|
|
133
|
+
} else if (value instanceof DuckDBUnionValue) {
|
|
134
|
+
return UNION({ [value.tag]: typeForValue(value.value) });
|
|
135
|
+
} else if (value instanceof DuckDBUUIDValue) {
|
|
136
|
+
return UUID;
|
|
137
|
+
} else if (value instanceof DuckDBVariantValue) {
|
|
138
|
+
return VARIANT;
|
|
139
|
+
}
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return ANY;
|
|
144
|
+
}
|