@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,274 @@
|
|
|
1
|
+
// Scalar function functions of the @duckdb/node-bindings contract. Semantics
|
|
2
|
+
// and error text follow the official binding (bindings/src/duckdb_node_bindings.cpp
|
|
3
|
+
// and scalar_function_helpers.h at the vendored tag).
|
|
4
|
+
//
|
|
5
|
+
// The callbacks, extra info, bind data and state are JavaScript values; DuckDB
|
|
6
|
+
// holds only ids (native/ABI.md, "Function callbacks"). A function's callbacks
|
|
7
|
+
// and extra info live in one state object per ScalarFunction, which the call
|
|
8
|
+
// handler for its id closes over, so DuckDB's copies of a registered function
|
|
9
|
+
// keep using it after destroy_scalar_function_sync. DuckDB has one extra info
|
|
10
|
+
// slot, so the shim's trampolines are set at registration, once the callbacks
|
|
11
|
+
// are known; the official binding sets each trampoline as its callback is set,
|
|
12
|
+
// with the same effect on what DuckDB registers.
|
|
13
|
+
import type { Pointer } from "bun:ffi";
|
|
14
|
+
import type {
|
|
15
|
+
ClientContext,
|
|
16
|
+
Connection,
|
|
17
|
+
DataChunk,
|
|
18
|
+
LogicalType,
|
|
19
|
+
ScalarFunction,
|
|
20
|
+
ScalarFunctionBindFunction,
|
|
21
|
+
ScalarFunctionBindInfo,
|
|
22
|
+
ScalarFunctionInfo,
|
|
23
|
+
ScalarFunctionInitFunction,
|
|
24
|
+
ScalarFunctionInitInfo,
|
|
25
|
+
ScalarFunctionMainFunction,
|
|
26
|
+
Vector,
|
|
27
|
+
} from "../generated/contract.ts";
|
|
28
|
+
import { expectFunction, expectReferenceable, expectString } from "../runtime/convert.ts";
|
|
29
|
+
import { allocateId, CallKind, setCallHandler } from "../runtime/dispatch.ts";
|
|
30
|
+
import { type Handle, handleOf, type Kind, unwrap } from "../runtime/handle.ts";
|
|
31
|
+
import { borrow, own } from "../runtime/kinds.ts";
|
|
32
|
+
import { cstr, slot, slotValue } from "../runtime/marshal.ts";
|
|
33
|
+
import { duckdb, shim } from "../runtime/native.ts";
|
|
34
|
+
import { RingOp, record } from "../runtime/ring.ts";
|
|
35
|
+
import { CallbackScope, runInScope, storeUserData, userDataOf } from "../runtime/scope.ts";
|
|
36
|
+
|
|
37
|
+
function asHandle<T>(handle: Handle): T {
|
|
38
|
+
return handle as unknown as T;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface ScalarFunctionState {
|
|
42
|
+
extraInfo: unknown;
|
|
43
|
+
bind?: ScalarFunctionBindFunction;
|
|
44
|
+
init?: ScalarFunctionInitFunction;
|
|
45
|
+
main?: ScalarFunctionMainFunction;
|
|
46
|
+
/** The trampolines last given to DuckDB, or undefined before the first registration. */
|
|
47
|
+
applied?: { bind: boolean; init: boolean };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const states = new WeakMap<object, ScalarFunctionState>();
|
|
51
|
+
|
|
52
|
+
/** The function state behind a callback's info handle. */
|
|
53
|
+
const infoStates = new WeakMap<object, ScalarFunctionState>();
|
|
54
|
+
|
|
55
|
+
function stateOf(scalar_function: ScalarFunction): { ptr: Pointer | null; state: ScalarFunctionState } {
|
|
56
|
+
const ptr = unwrap(scalar_function, "scalar function");
|
|
57
|
+
return { ptr, state: states.get(scalar_function as object) as ScalarFunctionState };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function infoState(info: unknown, kind: Kind): { ptr: Pointer | null; state: ScalarFunctionState } {
|
|
61
|
+
const ptr = unwrap(info, kind);
|
|
62
|
+
return { ptr, state: infoStates.get(info as object) as ScalarFunctionState };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// --- calls from DuckDB ------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
function errorSetter(set: (info: Pointer, message: Buffer) => void, info: Pointer): (message: string) => void {
|
|
68
|
+
return (message) => set(info, cstr(message));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function callInfo<K extends Kind>(scope: CallbackScope, kind: K, ptr: Pointer, state: ScalarFunctionState): Handle<K> {
|
|
72
|
+
const info = scope.adopt(borrow(kind, ptr));
|
|
73
|
+
infoStates.set(info, state);
|
|
74
|
+
return info;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function handlerFor(state: ScalarFunctionState) {
|
|
78
|
+
return (kind: number, info: Pointer, input: Pointer | null, output: Pointer | null): void => {
|
|
79
|
+
const scope = new CallbackScope();
|
|
80
|
+
switch (kind) {
|
|
81
|
+
case CallKind.scalarBind: {
|
|
82
|
+
const handle = callInfo(scope, "scalar function bind info", info, state);
|
|
83
|
+
runInScope(
|
|
84
|
+
scope,
|
|
85
|
+
() => state.bind?.(asHandle(handle)),
|
|
86
|
+
errorSetter((i, m) => duckdb.duckdb_scalar_function_bind_set_error(i, m), info),
|
|
87
|
+
);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
case CallKind.scalarInit: {
|
|
91
|
+
const handle = callInfo(scope, "scalar function init info", info, state);
|
|
92
|
+
runInScope(
|
|
93
|
+
scope,
|
|
94
|
+
() => state.init?.(asHandle(handle)),
|
|
95
|
+
errorSetter((i, m) => duckdb.duckdb_scalar_function_init_set_error(i, m), info),
|
|
96
|
+
);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
case CallKind.scalarMain: {
|
|
100
|
+
const handle = callInfo(scope, "scalar function info", info, state);
|
|
101
|
+
const chunk = scope.adopt(borrow("data chunk", input));
|
|
102
|
+
const vector = scope.adopt(borrow("vector", output));
|
|
103
|
+
runInScope(
|
|
104
|
+
scope,
|
|
105
|
+
() => state.main?.(asHandle(handle), asHandle<DataChunk>(chunk), asHandle<Vector>(vector)),
|
|
106
|
+
errorSetter((i, m) => duckdb.duckdb_scalar_function_set_error(i, m), info),
|
|
107
|
+
);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
default:
|
|
111
|
+
scope.close();
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Gives DuckDB the shim's trampolines for the callbacks set so far, under a new
|
|
118
|
+
* id, unless it already has exactly those. Without a main function nothing is
|
|
119
|
+
* set, so DuckDB refuses the registration, as it does in the official binding.
|
|
120
|
+
*/
|
|
121
|
+
function applyCallbacks(ptr: Pointer, state: ScalarFunctionState): void {
|
|
122
|
+
if (!state.main) return;
|
|
123
|
+
const wanted = { bind: state.bind !== undefined, init: state.init !== undefined };
|
|
124
|
+
const applied = state.applied;
|
|
125
|
+
if (applied && applied.bind === wanted.bind && applied.init === wanted.init) return;
|
|
126
|
+
const id = allocateId();
|
|
127
|
+
setCallHandler(id, handlerFor(state));
|
|
128
|
+
shim.dbs_scalar_function_set_callbacks(ptr, id, wanted.bind, wanted.init);
|
|
129
|
+
state.applied = wanted;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// --- the function object ------------------------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
export function create_scalar_function(): ScalarFunction {
|
|
135
|
+
const handle = own("scalar function", duckdb.duckdb_create_scalar_function() as Pointer);
|
|
136
|
+
states.set(handle, { extraInfo: undefined });
|
|
137
|
+
return asHandle(handle);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function destroy_scalar_function_sync(scalar_function: ScalarFunction): void {
|
|
141
|
+
// A no-op once destroyed, as duckdb_destroy_scalar_function is.
|
|
142
|
+
handleOf(scalar_function, "scalar function").requestDestroy();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function scalar_function_set_name(scalar_function: ScalarFunction, name: string): void {
|
|
146
|
+
const ptr = unwrap(scalar_function, "scalar function");
|
|
147
|
+
duckdb.duckdb_scalar_function_set_name(ptr, cstr(expectString(name)));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function scalar_function_set_varargs(scalar_function: ScalarFunction, logical_type: LogicalType): void {
|
|
151
|
+
const ptr = unwrap(scalar_function, "scalar function");
|
|
152
|
+
duckdb.duckdb_scalar_function_set_varargs(ptr, unwrap(logical_type, "logical type"));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function scalar_function_set_special_handling(scalar_function: ScalarFunction): void {
|
|
156
|
+
duckdb.duckdb_scalar_function_set_special_handling(unwrap(scalar_function, "scalar function"));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function scalar_function_set_volatile(scalar_function: ScalarFunction): void {
|
|
160
|
+
duckdb.duckdb_scalar_function_set_volatile(unwrap(scalar_function, "scalar function"));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function scalar_function_add_parameter(scalar_function: ScalarFunction, logical_type: LogicalType): void {
|
|
164
|
+
const ptr = unwrap(scalar_function, "scalar function");
|
|
165
|
+
duckdb.duckdb_scalar_function_add_parameter(ptr, unwrap(logical_type, "logical type"));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function scalar_function_set_return_type(scalar_function: ScalarFunction, logical_type: LogicalType): void {
|
|
169
|
+
const ptr = unwrap(scalar_function, "scalar function");
|
|
170
|
+
duckdb.duckdb_scalar_function_set_return_type(ptr, unwrap(logical_type, "logical type"));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function scalar_function_set_extra_info(scalar_function: ScalarFunction, extra_info: object): void {
|
|
174
|
+
const { state } = stateOf(scalar_function);
|
|
175
|
+
state.extraInfo = expectReferenceable(extra_info);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function scalar_function_set_bind(scalar_function: ScalarFunction, func: ScalarFunctionBindFunction): void {
|
|
179
|
+
stateOf(scalar_function).state.bind = expectFunction(func);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function scalar_function_set_init(scalar_function: ScalarFunction, func: ScalarFunctionInitFunction): void {
|
|
183
|
+
stateOf(scalar_function).state.init = expectFunction(func);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function scalar_function_set_function(scalar_function: ScalarFunction, func: ScalarFunctionMainFunction): void {
|
|
187
|
+
stateOf(scalar_function).state.main = expectFunction(func);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function register_scalar_function(connection: Connection, scalar_function: ScalarFunction): void {
|
|
191
|
+
const connectionPtr = unwrap(connection, "connection");
|
|
192
|
+
const { ptr, state } = stateOf(scalar_function);
|
|
193
|
+
if (ptr !== null) applyCallbacks(ptr, state);
|
|
194
|
+
record(RingOp.registerScalarFunction, ptr);
|
|
195
|
+
if (duckdb.duckdb_register_scalar_function(connectionPtr, ptr)) {
|
|
196
|
+
throw new Error("Failed to register scalar function");
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// --- bind ---------------------------------------------------------------------------------------
|
|
201
|
+
|
|
202
|
+
export function scalar_function_set_bind_data(info: ScalarFunctionBindInfo, bind_data: object): void {
|
|
203
|
+
const ptr = unwrap(info, "scalar function bind info");
|
|
204
|
+
shim.dbs_set_bind_data(ptr, CallKind.scalarBind, storeUserData(bind_data));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function scalar_function_bind_set_error(bind_info: ScalarFunctionBindInfo, error: string): void {
|
|
208
|
+
const ptr = unwrap(bind_info, "scalar function bind info");
|
|
209
|
+
duckdb.duckdb_scalar_function_bind_set_error(ptr, cstr(expectString(error)));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function scalar_function_bind_get_extra_info(bind_info: ScalarFunctionBindInfo): object | undefined {
|
|
213
|
+
return infoState(bind_info, "scalar function bind info").state.extraInfo as object | undefined;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function clientContext(get: (out: BigUint64Array) => void): ClientContext {
|
|
217
|
+
const out = slot();
|
|
218
|
+
get(out);
|
|
219
|
+
const context = slotValue(out);
|
|
220
|
+
if (context === null) throw new Error("Failed to get client context");
|
|
221
|
+
return asHandle(own("client context", context));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function scalar_function_get_client_context(bind_info: ScalarFunctionBindInfo): ClientContext {
|
|
225
|
+
const ptr = unwrap(bind_info, "scalar function bind info");
|
|
226
|
+
return clientContext((out) => duckdb.duckdb_scalar_function_get_client_context(ptr, out));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// --- init ---------------------------------------------------------------------------------------
|
|
230
|
+
|
|
231
|
+
export function scalar_function_init_set_error(init_info: ScalarFunctionInitInfo, error: string): void {
|
|
232
|
+
const ptr = unwrap(init_info, "scalar function init info");
|
|
233
|
+
duckdb.duckdb_scalar_function_init_set_error(ptr, cstr(expectString(error)));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function scalar_function_init_set_state(init_info: ScalarFunctionInitInfo, state: object): void {
|
|
237
|
+
const ptr = unwrap(init_info, "scalar function init info");
|
|
238
|
+
shim.dbs_scalar_init_set_state(ptr, storeUserData(state));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function scalar_function_init_get_client_context(init_info: ScalarFunctionInitInfo): ClientContext {
|
|
242
|
+
const ptr = unwrap(init_info, "scalar function init info");
|
|
243
|
+
return clientContext((out) => duckdb.duckdb_scalar_function_init_get_client_context(ptr, out));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function scalar_function_init_get_bind_data(init_info: ScalarFunctionInitInfo): object | undefined {
|
|
247
|
+
const ptr = unwrap(init_info, "scalar function init info");
|
|
248
|
+
return userDataOf(shim.dbs_get_bind_data(ptr, CallKind.scalarInit)) as object | undefined;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function scalar_function_init_get_extra_info(init_info: ScalarFunctionInitInfo): object | undefined {
|
|
252
|
+
return infoState(init_info, "scalar function init info").state.extraInfo as object | undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// --- main ---------------------------------------------------------------------------------------
|
|
256
|
+
|
|
257
|
+
export function scalar_function_get_extra_info(function_info: ScalarFunctionInfo): object | undefined {
|
|
258
|
+
return infoState(function_info, "scalar function info").state.extraInfo as object | undefined;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function scalar_function_get_bind_data(function_info: ScalarFunctionInfo): object | undefined {
|
|
262
|
+
const ptr = unwrap(function_info, "scalar function info");
|
|
263
|
+
return userDataOf(shim.dbs_get_bind_data(ptr, CallKind.scalarMain)) as object | undefined;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function scalar_function_set_error(function_info: ScalarFunctionInfo, error: string): void {
|
|
267
|
+
const ptr = unwrap(function_info, "scalar function info");
|
|
268
|
+
duckdb.duckdb_scalar_function_set_error(ptr, cstr(expectString(error)));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function scalar_function_get_state(function_info: ScalarFunctionInfo): object | undefined {
|
|
272
|
+
const ptr = unwrap(function_info, "scalar function info");
|
|
273
|
+
return userDataOf(shim.dbs_scalar_get_state(ptr)) as object | undefined;
|
|
274
|
+
}
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
// Table function functions of the @duckdb/node-bindings contract. Semantics
|
|
2
|
+
// and error text follow the official binding (bindings/src/duckdb_node_bindings.cpp
|
|
3
|
+
// and table_function_helpers.h at the vendored tag).
|
|
4
|
+
//
|
|
5
|
+
// As for scalar functions (scalar-function.ts), the callbacks and extra info
|
|
6
|
+
// live in one state object per TableFunction, DuckDB holds only ids, and the
|
|
7
|
+
// shim's trampolines are set at registration, once the callbacks are known.
|
|
8
|
+
import type { Pointer } from "bun:ffi";
|
|
9
|
+
import type {
|
|
10
|
+
ClientContext,
|
|
11
|
+
Connection,
|
|
12
|
+
DataChunk,
|
|
13
|
+
LogicalType,
|
|
14
|
+
TableFunction,
|
|
15
|
+
TableFunctionBindFunction,
|
|
16
|
+
TableFunctionBindInfo,
|
|
17
|
+
TableFunctionInfo,
|
|
18
|
+
TableFunctionInitFunction,
|
|
19
|
+
TableFunctionInitInfo,
|
|
20
|
+
TableFunctionMainFunction,
|
|
21
|
+
Value,
|
|
22
|
+
} from "../generated/contract.ts";
|
|
23
|
+
import {
|
|
24
|
+
expectBoolean,
|
|
25
|
+
expectFunction,
|
|
26
|
+
expectReferenceable,
|
|
27
|
+
expectString,
|
|
28
|
+
int64FromNumber,
|
|
29
|
+
uint32FromNumber,
|
|
30
|
+
} from "../runtime/convert.ts";
|
|
31
|
+
import { allocateId, CallKind, setCallHandler } from "../runtime/dispatch.ts";
|
|
32
|
+
import { type Handle, handleOf, type Kind, unwrap } from "../runtime/handle.ts";
|
|
33
|
+
import { borrow, own } from "../runtime/kinds.ts";
|
|
34
|
+
import { cstr, slot, slotValue } from "../runtime/marshal.ts";
|
|
35
|
+
import { duckdb, shim } from "../runtime/native.ts";
|
|
36
|
+
import { RingOp, record } from "../runtime/ring.ts";
|
|
37
|
+
import { CallbackScope, runInScope, storeUserData, userDataOf } from "../runtime/scope.ts";
|
|
38
|
+
|
|
39
|
+
function asHandle<T>(handle: Handle): T {
|
|
40
|
+
return handle as unknown as T;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface TableFunctionState {
|
|
44
|
+
extraInfo: unknown;
|
|
45
|
+
bind?: TableFunctionBindFunction;
|
|
46
|
+
init?: TableFunctionInitFunction;
|
|
47
|
+
localInit?: TableFunctionInitFunction;
|
|
48
|
+
main?: TableFunctionMainFunction;
|
|
49
|
+
/** The trampolines last given to DuckDB, or undefined before the first registration. */
|
|
50
|
+
applied?: { bind: boolean; init: boolean; localInit: boolean };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const states = new WeakMap<object, TableFunctionState>();
|
|
54
|
+
|
|
55
|
+
/** The function state behind a callback's info handle. */
|
|
56
|
+
const infoStates = new WeakMap<object, TableFunctionState>();
|
|
57
|
+
|
|
58
|
+
function stateOf(table_function: TableFunction): { ptr: Pointer | null; state: TableFunctionState } {
|
|
59
|
+
const ptr = unwrap(table_function, "table function");
|
|
60
|
+
return { ptr, state: states.get(table_function as object) as TableFunctionState };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function extraInfoOf(info: unknown, kind: Kind): object | undefined {
|
|
64
|
+
unwrap(info, kind);
|
|
65
|
+
return infoStates.get(info as object)?.extraInfo as object | undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// --- calls from DuckDB ------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
function callInfo<K extends Kind>(scope: CallbackScope, kind: K, ptr: Pointer, state: TableFunctionState): Handle<K> {
|
|
71
|
+
const info = scope.adopt(borrow(kind, ptr));
|
|
72
|
+
infoStates.set(info, state);
|
|
73
|
+
return info;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function setError(set: (info: Pointer, message: Buffer) => void, info: Pointer): (message: string) => void {
|
|
77
|
+
return (message) => set(info, cstr(message));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function runInit(state: TableFunctionState, info: Pointer, func: TableFunctionInitFunction | undefined): void {
|
|
81
|
+
const scope = new CallbackScope();
|
|
82
|
+
const handle = callInfo(scope, "table function init info", info, state);
|
|
83
|
+
runInScope(
|
|
84
|
+
scope,
|
|
85
|
+
() => func?.(asHandle(handle)),
|
|
86
|
+
setError((i, m) => duckdb.duckdb_init_set_error(i, m), info),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function handlerFor(state: TableFunctionState) {
|
|
91
|
+
return (kind: number, info: Pointer, input: Pointer | null): void => {
|
|
92
|
+
switch (kind) {
|
|
93
|
+
case CallKind.tableBind: {
|
|
94
|
+
const scope = new CallbackScope();
|
|
95
|
+
const handle = callInfo(scope, "table function bind info", info, state);
|
|
96
|
+
runInScope(
|
|
97
|
+
scope,
|
|
98
|
+
() => state.bind?.(asHandle(handle)),
|
|
99
|
+
setError((i, m) => duckdb.duckdb_bind_set_error(i, m), info),
|
|
100
|
+
);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
case CallKind.tableInit:
|
|
104
|
+
runInit(state, info, state.init);
|
|
105
|
+
return;
|
|
106
|
+
case CallKind.tableLocalInit:
|
|
107
|
+
runInit(state, info, state.localInit);
|
|
108
|
+
return;
|
|
109
|
+
case CallKind.tableMain: {
|
|
110
|
+
const scope = new CallbackScope();
|
|
111
|
+
const handle = callInfo(scope, "table function info", info, state);
|
|
112
|
+
// The shim passes the main function's output chunk as the call's input.
|
|
113
|
+
const output = scope.adopt(borrow("data chunk", input));
|
|
114
|
+
runInScope(
|
|
115
|
+
scope,
|
|
116
|
+
() => state.main?.(asHandle(handle), asHandle<DataChunk>(output)),
|
|
117
|
+
setError((i, m) => duckdb.duckdb_function_set_error(i, m), info),
|
|
118
|
+
);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Gives DuckDB the shim's trampolines for the callbacks set so far, under a new
|
|
127
|
+
* id, unless it already has exactly those. Without a main function nothing is
|
|
128
|
+
* set, so DuckDB refuses the registration, as it does in the official binding.
|
|
129
|
+
*/
|
|
130
|
+
function applyCallbacks(ptr: Pointer, state: TableFunctionState): void {
|
|
131
|
+
if (!state.main) return;
|
|
132
|
+
const wanted = {
|
|
133
|
+
bind: state.bind !== undefined,
|
|
134
|
+
init: state.init !== undefined,
|
|
135
|
+
localInit: state.localInit !== undefined,
|
|
136
|
+
};
|
|
137
|
+
const applied = state.applied;
|
|
138
|
+
if (
|
|
139
|
+
applied &&
|
|
140
|
+
applied.bind === wanted.bind &&
|
|
141
|
+
applied.init === wanted.init &&
|
|
142
|
+
applied.localInit === wanted.localInit
|
|
143
|
+
) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const id = allocateId();
|
|
147
|
+
setCallHandler(id, handlerFor(state));
|
|
148
|
+
shim.dbs_table_function_set_callbacks(ptr, id, wanted.bind, wanted.init, wanted.localInit);
|
|
149
|
+
state.applied = wanted;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// --- the function object ------------------------------------------------------------------------
|
|
153
|
+
|
|
154
|
+
export function create_table_function(): TableFunction {
|
|
155
|
+
const handle = own("table function", duckdb.duckdb_create_table_function() as Pointer);
|
|
156
|
+
states.set(handle, { extraInfo: undefined });
|
|
157
|
+
return asHandle(handle);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function destroy_table_function_sync(table_function: TableFunction): void {
|
|
161
|
+
// A no-op once destroyed, as duckdb_destroy_table_function is.
|
|
162
|
+
handleOf(table_function, "table function").requestDestroy();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function table_function_set_name(table_function: TableFunction, name: string): void {
|
|
166
|
+
const ptr = unwrap(table_function, "table function");
|
|
167
|
+
duckdb.duckdb_table_function_set_name(ptr, cstr(expectString(name)));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function table_function_add_parameter(table_function: TableFunction, logical_type: LogicalType): void {
|
|
171
|
+
const ptr = unwrap(table_function, "table function");
|
|
172
|
+
duckdb.duckdb_table_function_add_parameter(ptr, unwrap(logical_type, "logical type"));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function table_function_add_named_parameter(
|
|
176
|
+
table_function: TableFunction,
|
|
177
|
+
name: string,
|
|
178
|
+
logical_type: LogicalType,
|
|
179
|
+
): void {
|
|
180
|
+
const ptr = unwrap(table_function, "table function");
|
|
181
|
+
const parameterName = cstr(expectString(name));
|
|
182
|
+
duckdb.duckdb_table_function_add_named_parameter(ptr, parameterName, unwrap(logical_type, "logical type"));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function table_function_set_extra_info(table_function: TableFunction, extra_info: object): void {
|
|
186
|
+
const { state } = stateOf(table_function);
|
|
187
|
+
state.extraInfo = expectReferenceable(extra_info);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function table_function_set_bind(table_function: TableFunction, func: TableFunctionBindFunction): void {
|
|
191
|
+
stateOf(table_function).state.bind = expectFunction(func);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function table_function_set_init(table_function: TableFunction, func: TableFunctionInitFunction): void {
|
|
195
|
+
stateOf(table_function).state.init = expectFunction(func);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function table_function_set_local_init(table_function: TableFunction, func: TableFunctionInitFunction): void {
|
|
199
|
+
stateOf(table_function).state.localInit = expectFunction(func);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function table_function_set_function(table_function: TableFunction, func: TableFunctionMainFunction): void {
|
|
203
|
+
stateOf(table_function).state.main = expectFunction(func);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function table_function_supports_projection_pushdown(table_function: TableFunction, pushdown: boolean): void {
|
|
207
|
+
const ptr = unwrap(table_function, "table function");
|
|
208
|
+
duckdb.duckdb_table_function_supports_projection_pushdown(ptr, expectBoolean(pushdown));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function register_table_function(connection: Connection, table_function: TableFunction): void {
|
|
212
|
+
const connectionPtr = unwrap(connection, "connection");
|
|
213
|
+
const { ptr, state } = stateOf(table_function);
|
|
214
|
+
if (ptr !== null) applyCallbacks(ptr, state);
|
|
215
|
+
record(RingOp.registerTableFunction, ptr);
|
|
216
|
+
if (duckdb.duckdb_register_table_function(connectionPtr, ptr)) {
|
|
217
|
+
throw new Error("Failed to register table function");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// --- bind ---------------------------------------------------------------------------------------
|
|
222
|
+
|
|
223
|
+
export function bind_get_extra_info(bind_info: TableFunctionBindInfo): object | undefined {
|
|
224
|
+
return extraInfoOf(bind_info, "table function bind info");
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function table_function_get_client_context(bind_info: TableFunctionBindInfo): ClientContext {
|
|
228
|
+
const ptr = unwrap(bind_info, "table function bind info");
|
|
229
|
+
const out = slot();
|
|
230
|
+
duckdb.duckdb_table_function_get_client_context(ptr, out);
|
|
231
|
+
const context = slotValue(out);
|
|
232
|
+
if (context === null) throw new Error("Failed to get client context");
|
|
233
|
+
return asHandle(own("client context", context));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function bind_add_result_column(
|
|
237
|
+
bind_info: TableFunctionBindInfo,
|
|
238
|
+
name: string,
|
|
239
|
+
logical_type: LogicalType,
|
|
240
|
+
): void {
|
|
241
|
+
const ptr = unwrap(bind_info, "table function bind info");
|
|
242
|
+
const columnName = cstr(expectString(name));
|
|
243
|
+
duckdb.duckdb_bind_add_result_column(ptr, columnName, unwrap(logical_type, "logical type"));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function bind_get_parameter_count(bind_info: TableFunctionBindInfo): number {
|
|
247
|
+
return Number(duckdb.duckdb_bind_get_parameter_count(unwrap(bind_info, "table function bind info")));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function bind_get_parameter(bind_info: TableFunctionBindInfo, index: number): Value {
|
|
251
|
+
const ptr = unwrap(bind_info, "table function bind info");
|
|
252
|
+
const value = duckdb.duckdb_bind_get_parameter(ptr, uint32FromNumber(index)) as Pointer | null;
|
|
253
|
+
if (value === null) throw new Error("Failed to get parameter");
|
|
254
|
+
return asHandle(own("value", value));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function bind_get_named_parameter(bind_info: TableFunctionBindInfo, name: string): Value | null {
|
|
258
|
+
const ptr = unwrap(bind_info, "table function bind info");
|
|
259
|
+
const value = duckdb.duckdb_bind_get_named_parameter(ptr, cstr(expectString(name))) as Pointer | null;
|
|
260
|
+
// Null when no parameter with this name was supplied.
|
|
261
|
+
return value === null ? null : asHandle(own("value", value));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function bind_set_bind_data(bind_info: TableFunctionBindInfo, bind_data: object): void {
|
|
265
|
+
const ptr = unwrap(bind_info, "table function bind info");
|
|
266
|
+
shim.dbs_set_bind_data(ptr, CallKind.tableBind, storeUserData(bind_data));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function bind_set_cardinality(bind_info: TableFunctionBindInfo, cardinality: number, is_exact: boolean): void {
|
|
270
|
+
const ptr = unwrap(bind_info, "table function bind info");
|
|
271
|
+
// Read as 64 bits, as the official binding does: an estimate can exceed 32.
|
|
272
|
+
const estimate = BigInt.asUintN(64, int64FromNumber(cardinality));
|
|
273
|
+
duckdb.duckdb_bind_set_cardinality(ptr, estimate, expectBoolean(is_exact));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function bind_set_error(bind_info: TableFunctionBindInfo, error: string): void {
|
|
277
|
+
const ptr = unwrap(bind_info, "table function bind info");
|
|
278
|
+
duckdb.duckdb_bind_set_error(ptr, cstr(expectString(error)));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// --- init ---------------------------------------------------------------------------------------
|
|
282
|
+
|
|
283
|
+
export function init_get_extra_info(init_info: TableFunctionInitInfo): object | undefined {
|
|
284
|
+
return extraInfoOf(init_info, "table function init info");
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function init_get_bind_data(init_info: TableFunctionInitInfo): object | undefined {
|
|
288
|
+
const ptr = unwrap(init_info, "table function init info");
|
|
289
|
+
// Init and local init read bind data the same way.
|
|
290
|
+
return userDataOf(shim.dbs_get_bind_data(ptr, CallKind.tableInit)) as object | undefined;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export function init_set_init_data(init_info: TableFunctionInitInfo, init_data: object): void {
|
|
294
|
+
const ptr = unwrap(init_info, "table function init info");
|
|
295
|
+
shim.dbs_set_init_data(ptr, storeUserData(init_data));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function init_get_column_count(init_info: TableFunctionInitInfo): number {
|
|
299
|
+
return Number(duckdb.duckdb_init_get_column_count(unwrap(init_info, "table function init info")));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function init_get_column_index(init_info: TableFunctionInitInfo, column_index: number): number {
|
|
303
|
+
const ptr = unwrap(init_info, "table function init info");
|
|
304
|
+
return Number(duckdb.duckdb_init_get_column_index(ptr, uint32FromNumber(column_index)));
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function init_set_max_threads(init_info: TableFunctionInitInfo, max_threads: number): void {
|
|
308
|
+
const ptr = unwrap(init_info, "table function init info");
|
|
309
|
+
duckdb.duckdb_init_set_max_threads(ptr, uint32FromNumber(max_threads));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function init_set_error(init_info: TableFunctionInitInfo, error: string): void {
|
|
313
|
+
const ptr = unwrap(init_info, "table function init info");
|
|
314
|
+
duckdb.duckdb_init_set_error(ptr, cstr(expectString(error)));
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// --- main ---------------------------------------------------------------------------------------
|
|
318
|
+
|
|
319
|
+
export function function_get_extra_info(function_info: TableFunctionInfo): object | undefined {
|
|
320
|
+
return extraInfoOf(function_info, "table function info");
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export function function_get_bind_data(function_info: TableFunctionInfo): object | undefined {
|
|
324
|
+
const ptr = unwrap(function_info, "table function info");
|
|
325
|
+
return userDataOf(shim.dbs_get_bind_data(ptr, CallKind.tableMain)) as object | undefined;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function function_get_init_data(function_info: TableFunctionInfo): object | undefined {
|
|
329
|
+
return userDataOf(shim.dbs_get_init_data(unwrap(function_info, "table function info"))) as object | undefined;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export function function_get_local_init_data(function_info: TableFunctionInfo): object | undefined {
|
|
333
|
+
return userDataOf(shim.dbs_get_local_init_data(unwrap(function_info, "table function info"))) as object | undefined;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function function_set_error(function_info: TableFunctionInfo, error: string): void {
|
|
337
|
+
const ptr = unwrap(function_info, "table function info");
|
|
338
|
+
duckdb.duckdb_function_set_error(ptr, cstr(expectString(error)));
|
|
339
|
+
}
|