@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.
Files changed (231) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +284 -2
  3. package/package.json +95 -4
  4. package/src/bindings/api/appender.ts +288 -0
  5. package/src/bindings/api/data-chunk.ts +58 -0
  6. package/src/bindings/api/database.ts +232 -0
  7. package/src/bindings/api/datetime.ts +158 -0
  8. package/src/bindings/api/index.ts +13 -0
  9. package/src/bindings/api/logical-types.ts +227 -0
  10. package/src/bindings/api/query.ts +616 -0
  11. package/src/bindings/api/scalar-function.ts +274 -0
  12. package/src/bindings/api/table-function.ts +339 -0
  13. package/src/bindings/api/values.ts +546 -0
  14. package/src/bindings/api/vector.ts +266 -0
  15. package/src/bindings/generated/capi.ts +695 -0
  16. package/src/bindings/generated/contract.ts +162 -0
  17. package/src/bindings/index.ts +10 -0
  18. package/src/bindings/runtime/convert.ts +307 -0
  19. package/src/bindings/runtime/diagnostics.ts +45 -0
  20. package/src/bindings/runtime/dispatch.ts +200 -0
  21. package/src/bindings/runtime/exception.ts +103 -0
  22. package/src/bindings/runtime/flags.ts +19 -0
  23. package/src/bindings/runtime/handle.ts +217 -0
  24. package/src/bindings/runtime/kinds.ts +53 -0
  25. package/src/bindings/runtime/marshal.ts +190 -0
  26. package/src/bindings/runtime/native.ts +150 -0
  27. package/src/bindings/runtime/ring.ts +54 -0
  28. package/src/bindings/runtime/scope.ts +133 -0
  29. package/src/bindings/runtime/trace.ts +65 -0
  30. package/src/connection.ts +35 -0
  31. package/src/database.ts +27 -0
  32. package/src/errors.ts +30 -0
  33. package/src/file-assets.d.ts +10 -0
  34. package/src/index.ts +17 -0
  35. package/src/library.ts +83 -0
  36. package/src/neo.ts +16 -0
  37. package/src/reader.ts +47 -0
  38. package/src/statement.ts +61 -0
  39. package/src/types.ts +52 -0
  40. package/src/version.ts +6 -0
  41. package/vendor/node-api/LICENSE +7 -0
  42. package/vendor/node-api/PROVENANCE.md +54 -0
  43. package/vendor/node-api/node-bindings.d.ts +1669 -0
  44. package/vendor/node-api/src/DuckDBAppender.ts +237 -0
  45. package/vendor/node-api/src/DuckDBClientContext.ts +13 -0
  46. package/vendor/node-api/src/DuckDBConnection.ts +315 -0
  47. package/vendor/node-api/src/DuckDBDataChunk.ts +412 -0
  48. package/vendor/node-api/src/DuckDBDataChunkWriter.ts +144 -0
  49. package/vendor/node-api/src/DuckDBExtractedStatements.ts +37 -0
  50. package/vendor/node-api/src/DuckDBInstance.ts +35 -0
  51. package/vendor/node-api/src/DuckDBInstanceCache.ts +29 -0
  52. package/vendor/node-api/src/DuckDBLogicalType.ts +446 -0
  53. package/vendor/node-api/src/DuckDBMaterializedResult.ts +39 -0
  54. package/vendor/node-api/src/DuckDBPendingResult.ts +83 -0
  55. package/vendor/node-api/src/DuckDBPreparedStatement.ts +376 -0
  56. package/vendor/node-api/src/DuckDBPreparedStatementCollection.ts +5 -0
  57. package/vendor/node-api/src/DuckDBPreparedStatementWeakRefCollection.ts +31 -0
  58. package/vendor/node-api/src/DuckDBResult.ts +403 -0
  59. package/vendor/node-api/src/DuckDBResultReader.ts +283 -0
  60. package/vendor/node-api/src/DuckDBScalarFunction.ts +149 -0
  61. package/vendor/node-api/src/DuckDBScalarFunctionBindInfo.ts +34 -0
  62. package/vendor/node-api/src/DuckDBScalarFunctionInfo.ts +34 -0
  63. package/vendor/node-api/src/DuckDBScalarFunctionInitInfo.ts +35 -0
  64. package/vendor/node-api/src/DuckDBTableFunction.ts +149 -0
  65. package/vendor/node-api/src/DuckDBTableFunctionBindInfo.ts +56 -0
  66. package/vendor/node-api/src/DuckDBTableFunctionInfo.ts +36 -0
  67. package/vendor/node-api/src/DuckDBTableFunctionInitInfo.ts +52 -0
  68. package/vendor/node-api/src/DuckDBType.ts +1 -0
  69. package/vendor/node-api/src/DuckDBTypeId.ts +46 -0
  70. package/vendor/node-api/src/DuckDBValueConverter.ts +23 -0
  71. package/vendor/node-api/src/DuckDBValueConverters.ts +461 -0
  72. package/vendor/node-api/src/DuckDBVector.ts +1 -0
  73. package/vendor/node-api/src/JS.ts +11 -0
  74. package/vendor/node-api/src/JSDuckDBValueConverter.ts +85 -0
  75. package/vendor/node-api/src/JSInputTypeForTypeId.ts +101 -0
  76. package/vendor/node-api/src/JSToDuckDBValueConverter.ts +136 -0
  77. package/vendor/node-api/src/JSTypeForTypeId.ts +78 -0
  78. package/vendor/node-api/src/Json.ts +7 -0
  79. package/vendor/node-api/src/JsonDuckDBValueConverter.ts +72 -0
  80. package/vendor/node-api/src/JsonTypeForTypeId.ts +86 -0
  81. package/vendor/node-api/src/ToDuckDBValueConverter.ts +68 -0
  82. package/vendor/node-api/src/ToDuckDBValueConverters.ts +303 -0
  83. package/vendor/node-api/src/configurationOptionDescriptions.ts +13 -0
  84. package/vendor/node-api/src/conversion/bytesFromString.ts +5 -0
  85. package/vendor/node-api/src/conversion/dateTimeStringConversion.ts +343 -0
  86. package/vendor/node-api/src/conversion/displayStringForDuckDBValue.ts +12 -0
  87. package/vendor/node-api/src/conversion/stringFromBlob.ts +48 -0
  88. package/vendor/node-api/src/conversion/stringFromDecimal.ts +129 -0
  89. package/vendor/node-api/src/conversion/varintDecode.ts +42 -0
  90. package/vendor/node-api/src/convertColumnsFromChunks.ts +28 -0
  91. package/vendor/node-api/src/convertColumnsObjectFromChunks.ts +29 -0
  92. package/vendor/node-api/src/convertRowObjectsFromChunks.ts +21 -0
  93. package/vendor/node-api/src/convertRowsFromChunks.ts +16 -0
  94. package/vendor/node-api/src/createConfig.ts +14 -0
  95. package/vendor/node-api/src/createDuckDBValueConverter.ts +17 -0
  96. package/vendor/node-api/src/createResult.ts +15 -0
  97. package/vendor/node-api/src/createValue.ts +355 -0
  98. package/vendor/node-api/src/duckdb.ts +51 -0
  99. package/vendor/node-api/src/enums.ts +7 -0
  100. package/vendor/node-api/src/getColumnsFromChunks.ts +13 -0
  101. package/vendor/node-api/src/getColumnsObjectFromChunks.ts +16 -0
  102. package/vendor/node-api/src/getRowObjectsFromChunks.ts +13 -0
  103. package/vendor/node-api/src/getRowsFromChunks.ts +12 -0
  104. package/vendor/node-api/src/index.ts +3 -0
  105. package/vendor/node-api/src/readValue.ts +31 -0
  106. package/vendor/node-api/src/sleep.ts +5 -0
  107. package/vendor/node-api/src/sql.ts +7 -0
  108. package/vendor/node-api/src/tsconfig.json +7 -0
  109. package/vendor/node-api/src/typeForValue.ts +144 -0
  110. package/vendor/node-api/src/types/BaseDuckDBType.ts +31 -0
  111. package/vendor/node-api/src/types/DuckDBAnyType.ts +13 -0
  112. package/vendor/node-api/src/types/DuckDBArrayType.ts +43 -0
  113. package/vendor/node-api/src/types/DuckDBBigIntType.ts +21 -0
  114. package/vendor/node-api/src/types/DuckDBBigNumType.ts +21 -0
  115. package/vendor/node-api/src/types/DuckDBBitType.ts +13 -0
  116. package/vendor/node-api/src/types/DuckDBBlobType.ts +13 -0
  117. package/vendor/node-api/src/types/DuckDBBooleanType.ts +13 -0
  118. package/vendor/node-api/src/types/DuckDBDateType.ts +29 -0
  119. package/vendor/node-api/src/types/DuckDBDecimalType.ts +46 -0
  120. package/vendor/node-api/src/types/DuckDBDoubleType.ts +21 -0
  121. package/vendor/node-api/src/types/DuckDBEnumType.ts +111 -0
  122. package/vendor/node-api/src/types/DuckDBFloatType.ts +21 -0
  123. package/vendor/node-api/src/types/DuckDBGeometryType.ts +36 -0
  124. package/vendor/node-api/src/types/DuckDBHugeIntType.ts +21 -0
  125. package/vendor/node-api/src/types/DuckDBIntegerLiteralType.ts +13 -0
  126. package/vendor/node-api/src/types/DuckDBIntegerType.ts +21 -0
  127. package/vendor/node-api/src/types/DuckDBIntervalType.ts +13 -0
  128. package/vendor/node-api/src/types/DuckDBListType.ts +35 -0
  129. package/vendor/node-api/src/types/DuckDBMapType.ts +47 -0
  130. package/vendor/node-api/src/types/DuckDBSQLNullType.ts +17 -0
  131. package/vendor/node-api/src/types/DuckDBSmallIntType.ts +21 -0
  132. package/vendor/node-api/src/types/DuckDBStringLiteralType.ts +13 -0
  133. package/vendor/node-api/src/types/DuckDBStructType.ts +104 -0
  134. package/vendor/node-api/src/types/DuckDBTimeNSType.ts +20 -0
  135. package/vendor/node-api/src/types/DuckDBTimeTZType.ts +23 -0
  136. package/vendor/node-api/src/types/DuckDBTimeType.ts +20 -0
  137. package/vendor/node-api/src/types/DuckDBTimestampMillisecondsType.ts +29 -0
  138. package/vendor/node-api/src/types/DuckDBTimestampNanosecondsType.ts +29 -0
  139. package/vendor/node-api/src/types/DuckDBTimestampSecondsType.ts +29 -0
  140. package/vendor/node-api/src/types/DuckDBTimestampTZNanosecondsType.ts +32 -0
  141. package/vendor/node-api/src/types/DuckDBTimestampTZType.ts +32 -0
  142. package/vendor/node-api/src/types/DuckDBTimestampType.ts +32 -0
  143. package/vendor/node-api/src/types/DuckDBTinyIntType.ts +21 -0
  144. package/vendor/node-api/src/types/DuckDBType.ts +86 -0
  145. package/vendor/node-api/src/types/DuckDBUBigIntType.ts +21 -0
  146. package/vendor/node-api/src/types/DuckDBUHugeIntType.ts +21 -0
  147. package/vendor/node-api/src/types/DuckDBUIntegerType.ts +21 -0
  148. package/vendor/node-api/src/types/DuckDBUSmallIntType.ts +21 -0
  149. package/vendor/node-api/src/types/DuckDBUTinyIntType.ts +21 -0
  150. package/vendor/node-api/src/types/DuckDBUUIDType.ts +20 -0
  151. package/vendor/node-api/src/types/DuckDBUnionType.ts +74 -0
  152. package/vendor/node-api/src/types/DuckDBVarCharType.ts +13 -0
  153. package/vendor/node-api/src/types/DuckDBVariantType.ts +13 -0
  154. package/vendor/node-api/src/types/index.ts +44 -0
  155. package/vendor/node-api/src/values/DuckDBArrayValue.ts +18 -0
  156. package/vendor/node-api/src/values/DuckDBBitValue.ts +117 -0
  157. package/vendor/node-api/src/values/DuckDBBlobValue.ts +26 -0
  158. package/vendor/node-api/src/values/DuckDBDateValue.ts +43 -0
  159. package/vendor/node-api/src/values/DuckDBDecimalValue.ts +44 -0
  160. package/vendor/node-api/src/values/DuckDBGeometryValue.ts +30 -0
  161. package/vendor/node-api/src/values/DuckDBIntervalValue.ts +22 -0
  162. package/vendor/node-api/src/values/DuckDBListValue.ts +18 -0
  163. package/vendor/node-api/src/values/DuckDBMapValue.ts +25 -0
  164. package/vendor/node-api/src/values/DuckDBStructValue.ts +22 -0
  165. package/vendor/node-api/src/values/DuckDBTimeNSValue.ts +23 -0
  166. package/vendor/node-api/src/values/DuckDBTimeTZValue.ts +96 -0
  167. package/vendor/node-api/src/values/DuckDBTimeValue.ts +34 -0
  168. package/vendor/node-api/src/values/DuckDBTimestampMillisecondsValue.ts +29 -0
  169. package/vendor/node-api/src/values/DuckDBTimestampNanosecondsValue.ts +28 -0
  170. package/vendor/node-api/src/values/DuckDBTimestampSecondsValue.ts +28 -0
  171. package/vendor/node-api/src/values/DuckDBTimestampTZNanosecondsValue.ts +44 -0
  172. package/vendor/node-api/src/values/DuckDBTimestampTZValue.ts +54 -0
  173. package/vendor/node-api/src/values/DuckDBTimestampValue.ts +45 -0
  174. package/vendor/node-api/src/values/DuckDBTupleValue.ts +22 -0
  175. package/vendor/node-api/src/values/DuckDBUUIDValue.ts +48 -0
  176. package/vendor/node-api/src/values/DuckDBUnionValue.ts +22 -0
  177. package/vendor/node-api/src/values/DuckDBValue.ts +53 -0
  178. package/vendor/node-api/src/values/DuckDBVariantValue.ts +34 -0
  179. package/vendor/node-api/src/values/index.ts +24 -0
  180. package/vendor/node-api/src/vectors/DuckDBArrayVector.ts +107 -0
  181. package/vendor/node-api/src/vectors/DuckDBBigIntVector.ts +75 -0
  182. package/vendor/node-api/src/vectors/DuckDBBigNumVector.ts +98 -0
  183. package/vendor/node-api/src/vectors/DuckDBBitVector.ts +102 -0
  184. package/vendor/node-api/src/vectors/DuckDBBlobVector.ts +112 -0
  185. package/vendor/node-api/src/vectors/DuckDBBooleanVector.ts +84 -0
  186. package/vendor/node-api/src/vectors/DuckDBDateVector.ts +80 -0
  187. package/vendor/node-api/src/vectors/DuckDBDecimal128Vector.ts +107 -0
  188. package/vendor/node-api/src/vectors/DuckDBDecimal16Vector.ts +104 -0
  189. package/vendor/node-api/src/vectors/DuckDBDecimal32Vector.ts +104 -0
  190. package/vendor/node-api/src/vectors/DuckDBDecimal64Vector.ts +104 -0
  191. package/vendor/node-api/src/vectors/DuckDBDoubleVector.ts +68 -0
  192. package/vendor/node-api/src/vectors/DuckDBEnum16Vector.ts +75 -0
  193. package/vendor/node-api/src/vectors/DuckDBEnum32Vector.ts +75 -0
  194. package/vendor/node-api/src/vectors/DuckDBEnum8Vector.ts +75 -0
  195. package/vendor/node-api/src/vectors/DuckDBFloatVector.ts +68 -0
  196. package/vendor/node-api/src/vectors/DuckDBGeometryVector.ts +97 -0
  197. package/vendor/node-api/src/vectors/DuckDBHugeIntVector.ts +89 -0
  198. package/vendor/node-api/src/vectors/DuckDBIntegerVector.ts +75 -0
  199. package/vendor/node-api/src/vectors/DuckDBIntervalVector.ts +100 -0
  200. package/vendor/node-api/src/vectors/DuckDBListVector.ts +221 -0
  201. package/vendor/node-api/src/vectors/DuckDBMapVector.ts +88 -0
  202. package/vendor/node-api/src/vectors/DuckDBSQLNullVector.ts +53 -0
  203. package/vendor/node-api/src/vectors/DuckDBSmallIntVector.ts +75 -0
  204. package/vendor/node-api/src/vectors/DuckDBStructVector.ts +138 -0
  205. package/vendor/node-api/src/vectors/DuckDBTimeNSVector.ts +80 -0
  206. package/vendor/node-api/src/vectors/DuckDBTimeTZVector.ts +80 -0
  207. package/vendor/node-api/src/vectors/DuckDBTimeVector.ts +80 -0
  208. package/vendor/node-api/src/vectors/DuckDBTimestampMillisecondsVector.ts +88 -0
  209. package/vendor/node-api/src/vectors/DuckDBTimestampNanosecondsVector.ts +88 -0
  210. package/vendor/node-api/src/vectors/DuckDBTimestampSecondsVector.ts +88 -0
  211. package/vendor/node-api/src/vectors/DuckDBTimestampTZNanosecondsVector.ts +88 -0
  212. package/vendor/node-api/src/vectors/DuckDBTimestampTZVector.ts +86 -0
  213. package/vendor/node-api/src/vectors/DuckDBTimestampVector.ts +83 -0
  214. package/vendor/node-api/src/vectors/DuckDBTinyIntVector.ts +75 -0
  215. package/vendor/node-api/src/vectors/DuckDBTupleVector.ts +125 -0
  216. package/vendor/node-api/src/vectors/DuckDBUBigIntVector.ts +75 -0
  217. package/vendor/node-api/src/vectors/DuckDBUHugeIntVector.ts +89 -0
  218. package/vendor/node-api/src/vectors/DuckDBUIntegerVector.ts +75 -0
  219. package/vendor/node-api/src/vectors/DuckDBUSmallIntVector.ts +75 -0
  220. package/vendor/node-api/src/vectors/DuckDBUTinyIntVector.ts +75 -0
  221. package/vendor/node-api/src/vectors/DuckDBUUIDVector.ts +89 -0
  222. package/vendor/node-api/src/vectors/DuckDBUnionVector.ts +84 -0
  223. package/vendor/node-api/src/vectors/DuckDBValidity.ts +104 -0
  224. package/vendor/node-api/src/vectors/DuckDBVarCharVector.ts +100 -0
  225. package/vendor/node-api/src/vectors/DuckDBVariantVector.ts +609 -0
  226. package/vendor/node-api/src/vectors/DuckDBVector.ts +33 -0
  227. package/vendor/node-api/src/vectors/createVector.ts +269 -0
  228. package/vendor/node-api/src/vectors/dataAccessors.ts +387 -0
  229. package/vendor/node-api/src/vectors/index.ts +50 -0
  230. package/vendor/node-api/src/vectors/vectorRegistry.ts +24 -0
  231. package/vendor/node-api/src/version.ts +5 -0
@@ -0,0 +1,546 @@
1
+ // Value functions of the @duckdb/node-bindings contract: duckdb_create_*,
2
+ // duckdb_get_* and the nested value accessors. Semantics, error text and memory
3
+ // ownership follow the official binding (bindings/src/duckdb_node_bindings.cpp
4
+ // at the vendored tag) and the C API docs in vendor/duckdb.h.
5
+ import { type Pointer, ptr } from "bun:ffi";
6
+ import type {
7
+ Date_,
8
+ Decimal,
9
+ Interval,
10
+ LogicalType,
11
+ Time,
12
+ TimeNS,
13
+ Timestamp,
14
+ TimestampMilliseconds,
15
+ TimestampNanoseconds,
16
+ TimestampSeconds,
17
+ TimeTZ,
18
+ Value,
19
+ } from "../generated/contract.ts";
20
+ import {
21
+ bigIntFromBignum,
22
+ bignumFromBigInt,
23
+ dateFromObject,
24
+ decimalFromObject,
25
+ expectArray,
26
+ expectBoolean,
27
+ expectNumber,
28
+ expectString,
29
+ hugeintFromBigInt,
30
+ int64FromBigInt,
31
+ integerFromNumber,
32
+ intervalFromObject,
33
+ timeFromObject,
34
+ timeNsFromObject,
35
+ timestampFromObject,
36
+ timestampMillisecondsFromObject,
37
+ timestampNanosecondsFromObject,
38
+ timestampSecondsFromObject,
39
+ timeTzFromObject,
40
+ uhugeintFromBigInt,
41
+ uint32FromNumber,
42
+ uint64FromBigInt,
43
+ } from "../runtime/convert.ts";
44
+ import { type Handle, handleOf, unwrap } from "../runtime/handle.ts";
45
+ import { borrow, own } from "../runtime/kinds.ts";
46
+ import { copyBytes, decode, encode, type LayoutName, structBuffer, takeCString, utf8 } from "../runtime/marshal.ts";
47
+ import { duckdb, shim } from "../runtime/native.ts";
48
+
49
+ function asValue(handle: Handle): Value {
50
+ return handle as unknown as Value;
51
+ }
52
+
53
+ /** Wraps a value the caller owns; DuckDB's create and get functions return new values. */
54
+ function ownValue(pointer: Pointer | null): Value {
55
+ return asValue(own("value", pointer as Pointer));
56
+ }
57
+
58
+ function valuePointer(value: unknown): Pointer | null {
59
+ return unwrap(value, "value");
60
+ }
61
+
62
+ /**
63
+ * The pointer of a value a scalar getter may read. get_table_names gives a value
64
+ * without a pointer for SQL that does not parse, which libduckdb's scalar
65
+ * getters dereference; the official binding dies with SIGSEGV there, and ours
66
+ * refuses it instead.
67
+ */
68
+ function presentValuePointer(value: unknown): Pointer {
69
+ const pointer = valuePointer(value);
70
+ if (pointer === null) throw new Error("Invalid value argument");
71
+ return pointer;
72
+ }
73
+
74
+ function logicalTypePointer(logical_type: unknown): Pointer | null {
75
+ return unwrap(logical_type, "logical type");
76
+ }
77
+
78
+ /** Wraps a new value, or throws `message` when DuckDB returned null. */
79
+ function ownValueOrThrow(pointer: Pointer | null, message: string): Value {
80
+ if (pointer === null) throw new Error(message);
81
+ return ownValue(pointer);
82
+ }
83
+
84
+ /** Calls a shim `dbs_get_<x>(value, out)` wrapper and decodes the struct it wrote. */
85
+ function getStruct<T>(
86
+ layout: LayoutName,
87
+ call: (value: Pointer | null, out: Uint8Array) => void,
88
+ value: Value,
89
+ pointerOf: (value: unknown) => Pointer | null = presentValuePointer,
90
+ ): T {
91
+ const pointer = pointerOf(value);
92
+ const out = structBuffer(layout);
93
+ call(pointer, out);
94
+ return decode<T>(layout, out);
95
+ }
96
+
97
+ /**
98
+ * The bytes of a TypedArray, as node-addon-api's Uint8Array cast reads them:
99
+ * any TypedArray is accepted by its byte length, and anything else fails in
100
+ * napi_get_typedarray_info with N-API's "Invalid argument".
101
+ */
102
+ function typedArrayBytes(data: unknown): Uint8Array {
103
+ if (!ArrayBuffer.isView(data) || data instanceof DataView) throw new Error("Invalid argument");
104
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
105
+ }
106
+
107
+ /** `bytes`, or one padding byte for an empty view, whose address bun:ffi cannot take. */
108
+ function nonEmpty(bytes: Uint8Array): Uint8Array {
109
+ return bytes.byteLength === 0 ? new Uint8Array(1) : bytes;
110
+ }
111
+
112
+ /**
113
+ * A duckdb_bit or duckdb_bignum struct followed by a copy of its data in one
114
+ * buffer, with the struct's data field pointing just past the struct. Passing
115
+ * the buffer to the C call keeps the data alive for exactly as long as it runs.
116
+ */
117
+ function structWithData(layout: "bit" | "bignum", fields: Record<string, unknown>, bytes: Uint8Array): Uint8Array {
118
+ const size = structBuffer(layout).byteLength;
119
+ const buffer = new Uint8Array(size + Math.max(bytes.byteLength, 1));
120
+ buffer.set(bytes, size);
121
+ buffer.set(encode(layout, { ...fields, data: ptr(buffer, size), size: BigInt(bytes.byteLength) }));
122
+ return buffer;
123
+ }
124
+
125
+ /** A pointer array of `count` slots (at least one, as the official binding allocates) holding `values`. */
126
+ function valuePointers(values: readonly unknown[], count: number): BigUint64Array {
127
+ const pointers = new BigUint64Array(Math.max(count, 1));
128
+ values.forEach((value, i) => {
129
+ pointers[i] = BigInt(valuePointer(value) ?? 0);
130
+ });
131
+ return pointers;
132
+ }
133
+
134
+ /**
135
+ * Copies a blob, bit or bignum `data` field DuckDB allocated with malloc, then
136
+ * frees it with duckdb_free, as vendor/duckdb.h asks for get_bit and
137
+ * get_bignum and DuckDB's source does for get_blob.
138
+ */
139
+ function takeData(data: number, size: bigint): Uint8Array {
140
+ const pointer = data === 0 ? null : (data as Pointer);
141
+ try {
142
+ return copyBytes(pointer, Number(size));
143
+ } finally {
144
+ if (pointer !== null) duckdb.duckdb_free(pointer);
145
+ }
146
+ }
147
+
148
+ /** A Buffer over `bytes`, the type the official binding's Napi::Buffer returns. */
149
+ function toBuffer(bytes: Uint8Array): Buffer {
150
+ return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
151
+ }
152
+
153
+ // --- create -------------------------------------------------------------------------
154
+
155
+ export function create_varchar(text: string): Value {
156
+ const { bytes, length } = utf8(expectString(text));
157
+ return ownValue(duckdb.duckdb_create_varchar_length(bytes, length) as Pointer | null);
158
+ }
159
+
160
+ export function create_bool(input: boolean): Value {
161
+ return ownValue(duckdb.duckdb_create_bool(expectBoolean(input)) as Pointer | null);
162
+ }
163
+
164
+ export function create_int8(input: number): Value {
165
+ return ownValue(duckdb.duckdb_create_int8(integerFromNumber(input, "int8")) as Pointer | null);
166
+ }
167
+
168
+ export function create_uint8(input: number): Value {
169
+ return ownValue(duckdb.duckdb_create_uint8(integerFromNumber(input, "uint8")) as Pointer | null);
170
+ }
171
+
172
+ export function create_int16(input: number): Value {
173
+ return ownValue(duckdb.duckdb_create_int16(integerFromNumber(input, "int16")) as Pointer | null);
174
+ }
175
+
176
+ export function create_uint16(input: number): Value {
177
+ return ownValue(duckdb.duckdb_create_uint16(integerFromNumber(input, "uint16")) as Pointer | null);
178
+ }
179
+
180
+ export function create_int32(input: number): Value {
181
+ return ownValue(duckdb.duckdb_create_int32(integerFromNumber(input, "int32")) as Pointer | null);
182
+ }
183
+
184
+ export function create_uint32(input: number): Value {
185
+ return ownValue(duckdb.duckdb_create_uint32(integerFromNumber(input, "uint32")) as Pointer | null);
186
+ }
187
+
188
+ export function create_uint64(input: bigint): Value {
189
+ return ownValue(duckdb.duckdb_create_uint64(uint64FromBigInt(input, "bigint")) as Pointer | null);
190
+ }
191
+
192
+ export function create_int64(input: bigint): Value {
193
+ return ownValue(duckdb.duckdb_create_int64(int64FromBigInt(input, "bigint")) as Pointer | null);
194
+ }
195
+
196
+ export function create_hugeint(input: bigint): Value {
197
+ return ownValue(shim.dbs_create_hugeint(encode("hugeint", hugeintFromBigInt(input))) as Pointer | null);
198
+ }
199
+
200
+ export function create_uhugeint(input: bigint): Value {
201
+ return ownValue(shim.dbs_create_uhugeint(encode("uhugeint", uhugeintFromBigInt(input))) as Pointer | null);
202
+ }
203
+
204
+ export function create_bignum(input: bigint): Value {
205
+ const { bytes, isNegative } = bignumFromBigInt(input);
206
+ const bignum = structWithData("bignum", { is_negative: isNegative }, bytes);
207
+ return ownValue(shim.dbs_create_bignum(bignum) as Pointer | null);
208
+ }
209
+
210
+ export function create_decimal(input: Decimal): Value {
211
+ return ownValue(shim.dbs_create_decimal(encode("decimal", decimalFromObject(input))) as Pointer | null);
212
+ }
213
+
214
+ /** The double narrows to float as static_cast<float> does. */
215
+ export function create_float(input: number): Value {
216
+ return ownValue(duckdb.duckdb_create_float(expectNumber(input)) as Pointer | null);
217
+ }
218
+
219
+ export function create_double(input: number): Value {
220
+ return ownValue(duckdb.duckdb_create_double(expectNumber(input)) as Pointer | null);
221
+ }
222
+
223
+ export function create_date(input: Date_): Value {
224
+ return ownValue(shim.dbs_create_date(encode("date", dateFromObject(input))) as Pointer | null);
225
+ }
226
+
227
+ export function create_time(input: Time): Value {
228
+ return ownValue(shim.dbs_create_time(encode("time", timeFromObject(input))) as Pointer | null);
229
+ }
230
+
231
+ export function create_time_ns(input: TimeNS): Value {
232
+ return ownValue(shim.dbs_create_time_ns(encode("time_ns", timeNsFromObject(input))) as Pointer | null);
233
+ }
234
+
235
+ export function create_time_tz_value(input: TimeTZ): Value {
236
+ return ownValue(shim.dbs_create_time_tz_value(encode("time_tz", timeTzFromObject(input))) as Pointer | null);
237
+ }
238
+
239
+ export function create_timestamp(input: Timestamp): Value {
240
+ return ownValue(shim.dbs_create_timestamp(encode("timestamp", timestampFromObject(input))) as Pointer | null);
241
+ }
242
+
243
+ export function create_timestamp_tz(input: Timestamp): Value {
244
+ return ownValue(shim.dbs_create_timestamp_tz(encode("timestamp", timestampFromObject(input))) as Pointer | null);
245
+ }
246
+
247
+ export function create_timestamp_tz_ns(input: TimestampNanoseconds): Value {
248
+ const timestamp = encode("timestamp_ns", timestampNanosecondsFromObject(input));
249
+ return ownValue(shim.dbs_create_timestamp_tz_ns(timestamp) as Pointer | null);
250
+ }
251
+
252
+ export function create_timestamp_s(input: TimestampSeconds): Value {
253
+ const timestamp = encode("timestamp_s", timestampSecondsFromObject(input));
254
+ return ownValue(shim.dbs_create_timestamp_s(timestamp) as Pointer | null);
255
+ }
256
+
257
+ export function create_timestamp_ms(input: TimestampMilliseconds): Value {
258
+ const timestamp = encode("timestamp_ms", timestampMillisecondsFromObject(input));
259
+ return ownValue(shim.dbs_create_timestamp_ms(timestamp) as Pointer | null);
260
+ }
261
+
262
+ export function create_timestamp_ns(input: TimestampNanoseconds): Value {
263
+ const timestamp = encode("timestamp_ns", timestampNanosecondsFromObject(input));
264
+ return ownValue(shim.dbs_create_timestamp_ns(timestamp) as Pointer | null);
265
+ }
266
+
267
+ export function create_interval(input: Interval): Value {
268
+ return ownValue(shim.dbs_create_interval(encode("interval", intervalFromObject(input))) as Pointer | null);
269
+ }
270
+
271
+ /** DuckDB copies the bytes, so the array needs to live only for the call. */
272
+ export function create_blob(data: Uint8Array): Value {
273
+ const bytes = typedArrayBytes(data);
274
+ return ownValue(duckdb.duckdb_create_blob(nonEmpty(bytes), bytes.byteLength) as Pointer | null);
275
+ }
276
+
277
+ /**
278
+ * True for the bit strings DuckDB can read: a padding byte below 8 that leaves a bit
279
+ * count of zero or more. DuckDB asserts this only in debug builds; past it, its BIT
280
+ * to VARCHAR cast writes beyond its buffer or throws a C++ exception, and either one
281
+ * ends the process. The official binding passes such bytes on and crashes.
282
+ */
283
+ function validBitString(bytes: Uint8Array): boolean {
284
+ const padding = bytes[0];
285
+ return padding !== undefined && padding < 8 && (bytes.length > 1 || padding === 0);
286
+ }
287
+
288
+ export function create_bit(data: Uint8Array): Value {
289
+ const bytes = typedArrayBytes(data);
290
+ if (!validBitString(bytes)) throw new Error("Failed to create bit value: invalid padding");
291
+ const bit = structWithData("bit", {}, bytes);
292
+ return ownValue(shim.dbs_create_bit(bit) as Pointer | null);
293
+ }
294
+
295
+ export function create_uuid(input: bigint): Value {
296
+ return ownValue(shim.dbs_create_uuid(encode("uhugeint", uhugeintFromBigInt(input))) as Pointer | null);
297
+ }
298
+
299
+ export function create_null_value(): Value {
300
+ return ownValue(duckdb.duckdb_create_null_value() as Pointer | null);
301
+ }
302
+
303
+ // --- get ----------------------------------------------------------------------------
304
+
305
+ export function get_bool(value: Value): boolean {
306
+ return duckdb.duckdb_get_bool(presentValuePointer(value));
307
+ }
308
+
309
+ export function get_int8(value: Value): number {
310
+ return duckdb.duckdb_get_int8(presentValuePointer(value));
311
+ }
312
+
313
+ export function get_uint8(value: Value): number {
314
+ return duckdb.duckdb_get_uint8(presentValuePointer(value));
315
+ }
316
+
317
+ export function get_int16(value: Value): number {
318
+ return duckdb.duckdb_get_int16(presentValuePointer(value));
319
+ }
320
+
321
+ export function get_uint16(value: Value): number {
322
+ return duckdb.duckdb_get_uint16(presentValuePointer(value));
323
+ }
324
+
325
+ export function get_int32(value: Value): number {
326
+ return duckdb.duckdb_get_int32(presentValuePointer(value));
327
+ }
328
+
329
+ export function get_uint32(value: Value): number {
330
+ return duckdb.duckdb_get_uint32(presentValuePointer(value));
331
+ }
332
+
333
+ export function get_int64(value: Value): bigint {
334
+ return BigInt(duckdb.duckdb_get_int64(presentValuePointer(value)));
335
+ }
336
+
337
+ export function get_uint64(value: Value): bigint {
338
+ return BigInt(duckdb.duckdb_get_uint64(presentValuePointer(value)));
339
+ }
340
+
341
+ export function get_hugeint(value: Value): bigint {
342
+ return getStruct<bigint>("hugeint", shim.dbs_get_hugeint, value);
343
+ }
344
+
345
+ export function get_uhugeint(value: Value): bigint {
346
+ return getStruct<bigint>("uhugeint", shim.dbs_get_uhugeint, value);
347
+ }
348
+
349
+ export function get_bignum(value: Value): bigint {
350
+ const bignum = getStruct<{ data: number; size: bigint; is_negative: boolean }>("bignum", shim.dbs_get_bignum, value);
351
+ return bigIntFromBignum(takeData(bignum.data, bignum.size), bignum.is_negative);
352
+ }
353
+
354
+ export function get_decimal(value: Value): Decimal {
355
+ // duckdb_get_decimal reads a NULL value without throwing.
356
+ return getStruct<Decimal>("decimal", shim.dbs_get_decimal, value, presentValuePointer);
357
+ }
358
+
359
+ export function get_float(value: Value): number {
360
+ return duckdb.duckdb_get_float(presentValuePointer(value));
361
+ }
362
+
363
+ export function get_double(value: Value): number {
364
+ return duckdb.duckdb_get_double(presentValuePointer(value));
365
+ }
366
+
367
+ export function get_date(value: Value): Date_ {
368
+ return getStruct<Date_>("date", shim.dbs_get_date, value);
369
+ }
370
+
371
+ export function get_time(value: Value): Time {
372
+ return getStruct<Time>("time", shim.dbs_get_time, value);
373
+ }
374
+
375
+ export function get_time_ns(value: Value): TimeNS {
376
+ return getStruct<TimeNS>("time_ns", shim.dbs_get_time_ns, value);
377
+ }
378
+
379
+ export function get_time_tz(value: Value): TimeTZ {
380
+ return getStruct<TimeTZ>("time_tz", shim.dbs_get_time_tz, value);
381
+ }
382
+
383
+ export function get_timestamp(value: Value): Timestamp {
384
+ return getStruct<Timestamp>("timestamp", shim.dbs_get_timestamp, value);
385
+ }
386
+
387
+ export function get_timestamp_tz(value: Value): Timestamp {
388
+ return getStruct<Timestamp>("timestamp", shim.dbs_get_timestamp_tz, value);
389
+ }
390
+
391
+ export function get_timestamp_tz_ns(value: Value): TimestampNanoseconds {
392
+ return getStruct<TimestampNanoseconds>("timestamp_ns", shim.dbs_get_timestamp_tz_ns, value);
393
+ }
394
+
395
+ export function get_timestamp_s(value: Value): TimestampSeconds {
396
+ return getStruct<TimestampSeconds>("timestamp_s", shim.dbs_get_timestamp_s, value);
397
+ }
398
+
399
+ export function get_timestamp_ms(value: Value): TimestampMilliseconds {
400
+ return getStruct<TimestampMilliseconds>("timestamp_ms", shim.dbs_get_timestamp_ms, value);
401
+ }
402
+
403
+ export function get_timestamp_ns(value: Value): TimestampNanoseconds {
404
+ return getStruct<TimestampNanoseconds>("timestamp_ns", shim.dbs_get_timestamp_ns, value);
405
+ }
406
+
407
+ export function get_interval(value: Value): Interval {
408
+ return getStruct<Interval>("interval", shim.dbs_get_interval, value);
409
+ }
410
+
411
+ /**
412
+ * The value owns its type and frees it with itself, so the handle borrows the
413
+ * pointer and keeps the value reachable for as long as the type is.
414
+ */
415
+ export function get_value_type(value: Value): LogicalType {
416
+ const handle = handleOf(value, "value");
417
+ const type = duckdb.duckdb_get_value_type(handle.destroyRequested ? null : handle.ptr) as Pointer | null;
418
+ return borrow("logical type", type, handle) as unknown as LogicalType;
419
+ }
420
+
421
+ export function get_blob(value: Value): Uint8Array {
422
+ const blob = getStruct<{ data: number; size: bigint }>("blob", shim.dbs_get_blob, value);
423
+ return toBuffer(takeData(blob.data, blob.size));
424
+ }
425
+
426
+ export function get_bit(value: Value): Uint8Array {
427
+ const bit = getStruct<{ data: number; size: bigint }>("bit", shim.dbs_get_bit, value);
428
+ return toBuffer(takeData(bit.data, bit.size));
429
+ }
430
+
431
+ export function get_uuid(value: Value): bigint {
432
+ return getStruct<bigint>("uhugeint", shim.dbs_get_uuid, value);
433
+ }
434
+
435
+ export function get_varchar(value: Value): string {
436
+ const text = takeCString(duckdb.duckdb_get_varchar(presentValuePointer(value)) as Pointer | null);
437
+ // Napi::String::New throws this for the null DuckDB returns when malloc fails.
438
+ if (text === null) throw new Error("Invalid argument");
439
+ return text;
440
+ }
441
+
442
+ export function is_null_value(value: Value): boolean {
443
+ return duckdb.duckdb_is_null_value(valuePointer(value));
444
+ }
445
+
446
+ // --- nested values ------------------------------------------------------------------
447
+
448
+ /**
449
+ * duckdb_create_struct_value reads one value per struct child. The official
450
+ * binding passes only as many slots as the caller gave values, so too few
451
+ * values read past the array; here the array has a slot per child, and a
452
+ * missing value is a null slot, which DuckDB reports as a failure.
453
+ */
454
+ export function create_struct_value(logical_type: LogicalType, values: readonly Value[]): Value {
455
+ const type = logicalTypePointer(logical_type);
456
+ const valuesArray = expectArray(values);
457
+ const childCount = Number(duckdb.duckdb_struct_type_child_count(type));
458
+ const pointers = valuePointers(valuesArray, Math.max(valuesArray.length, childCount));
459
+ const struct = duckdb.duckdb_create_struct_value(type, pointers) as Pointer | null;
460
+ return ownValueOrThrow(struct, "Failed to create struct value");
461
+ }
462
+
463
+ export function create_list_value(logical_type: LogicalType, values: readonly Value[]): Value {
464
+ const type = logicalTypePointer(logical_type);
465
+ const valuesArray = expectArray(values);
466
+ const pointers = valuePointers(valuesArray, valuesArray.length);
467
+ const list = duckdb.duckdb_create_list_value(type, pointers, valuesArray.length) as Pointer | null;
468
+ return ownValueOrThrow(list, "Failed to create list value");
469
+ }
470
+
471
+ export function create_array_value(logical_type: LogicalType, values: readonly Value[]): Value {
472
+ const type = logicalTypePointer(logical_type);
473
+ const valuesArray = expectArray(values);
474
+ const pointers = valuePointers(valuesArray, valuesArray.length);
475
+ const array = duckdb.duckdb_create_array_value(type, pointers, valuesArray.length) as Pointer | null;
476
+ return ownValueOrThrow(array, "Failed to create array value");
477
+ }
478
+
479
+ export function create_map_value(map_type: LogicalType, keys: readonly Value[], values: readonly Value[]): Value {
480
+ const type = logicalTypePointer(map_type);
481
+ const keysArray = expectArray(keys);
482
+ const valuesArray = expectArray(values);
483
+ if (keysArray.length !== valuesArray.length) {
484
+ throw new Error("Failed to create map value: must have same number of keys and values");
485
+ }
486
+ const count = keysArray.length;
487
+ const keyPointers = new BigUint64Array(Math.max(count, 1));
488
+ const valuePointerArray = new BigUint64Array(Math.max(count, 1));
489
+ for (let i = 0; i < count; i++) {
490
+ keyPointers[i] = BigInt(valuePointer(keysArray[i]) ?? 0);
491
+ valuePointerArray[i] = BigInt(valuePointer(valuesArray[i]) ?? 0);
492
+ }
493
+ const map = duckdb.duckdb_create_map_value(type, keyPointers, valuePointerArray, count) as Pointer | null;
494
+ return ownValueOrThrow(map, "Failed to create map value");
495
+ }
496
+
497
+ export function create_union_value(union_type: LogicalType, tag_index: number, value: Value): Value {
498
+ const type = logicalTypePointer(union_type);
499
+ const tag = uint32FromNumber(tag_index);
500
+ const member = valuePointer(value);
501
+ const union = duckdb.duckdb_create_union_value(type, tag, member) as Pointer | null;
502
+ return ownValueOrThrow(union, "Failed to create union value");
503
+ }
504
+
505
+ export function create_enum_value(logical_type: LogicalType, value: number): Value {
506
+ const type = logicalTypePointer(logical_type);
507
+ const index = integerFromNumber(value, "uint32");
508
+ return ownValueOrThrow(duckdb.duckdb_create_enum_value(type, index) as Pointer | null, "Failed to create enum value");
509
+ }
510
+
511
+ export function get_enum_value(value: Value): number {
512
+ return Number(duckdb.duckdb_get_enum_value(valuePointer(value)));
513
+ }
514
+
515
+ export function get_map_size(value: Value): number {
516
+ return Number(duckdb.duckdb_get_map_size(valuePointer(value)));
517
+ }
518
+
519
+ export function get_map_key(value: Value, index: number): Value {
520
+ const pointer = valuePointer(value);
521
+ const position = uint32FromNumber(index);
522
+ return ownValueOrThrow(duckdb.duckdb_get_map_key(pointer, position) as Pointer | null, "Failed to get map key");
523
+ }
524
+
525
+ export function get_map_value(value: Value, index: number): Value {
526
+ const pointer = valuePointer(value);
527
+ const position = uint32FromNumber(index);
528
+ return ownValueOrThrow(duckdb.duckdb_get_map_value(pointer, position) as Pointer | null, "Failed to get map value");
529
+ }
530
+
531
+ export function get_list_size(value: Value): number {
532
+ return Number(duckdb.duckdb_get_list_size(valuePointer(value)));
533
+ }
534
+
535
+ export function get_list_child(value: Value, index: number): Value {
536
+ const pointer = valuePointer(value);
537
+ const position = uint32FromNumber(index);
538
+ return ownValueOrThrow(duckdb.duckdb_get_list_child(pointer, position) as Pointer | null, "Failed to get list child");
539
+ }
540
+
541
+ export function get_struct_child(value: Value, index: number): Value {
542
+ const pointer = valuePointer(value);
543
+ const position = uint32FromNumber(index);
544
+ const child = duckdb.duckdb_get_struct_child(pointer, position) as Pointer | null;
545
+ return ownValueOrThrow(child, "Failed to get struct child");
546
+ }