Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support param_logical_type #152

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api/src/DuckDBPreparedStatement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import duckdb from '@duckdb/node-bindings';
import { createValue } from './createValue';
import { DuckDBLogicalType } from './DuckDBLogicalType';
import { DuckDBMaterializedResult } from './DuckDBMaterializedResult';
import { DuckDBPendingResult } from './DuckDBPendingResult';
import { DuckDBResult } from './DuckDBResult';
Expand Down Expand Up @@ -49,6 +50,12 @@ export class DuckDBPreparedStatement {
parameterIndex
) as number as DuckDBTypeId;
}
public parameterType(parameterIndex: number): DuckDBType {
return DuckDBLogicalType.create(duckdb.param_logical_type(
this.prepared_statement,
parameterIndex
)).asType();
}
public clearBindings() {
duckdb.clear_bindings(this.prepared_statement);
}
Expand Down
18 changes: 18 additions & 0 deletions api/test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
DuckDBTimestampVector,
DuckDBTinyIntVector,
DuckDBType,
DuckDBTypeId,
DuckDBUBigIntVector,
DuckDBUHugeIntVector,
DuckDBUIntegerVector,
Expand Down Expand Up @@ -422,6 +423,23 @@ describe('api', () => {
);
prepared.bindArray(7, arrayValue([100, 200, 300]), ARRAY(INTEGER, 3));
prepared.bindNull(8);
assert.equal(prepared.parameterTypeId(1), DuckDBTypeId.INTEGER);
assert.deepEqual(prepared.parameterType(1), INTEGER);
// See https://github.com/duckdb/duckdb/issues/16137
// assert.equal(prepared.parameterTypeId(2), DuckDBTypeId.VARCHAR);
// assert.deepEqual(prepared.parameterType(2), VARCHAR);
assert.equal(prepared.parameterTypeId(3), DuckDBTypeId.BOOLEAN);
assert.deepEqual(prepared.parameterType(3), BOOLEAN);
assert.equal(prepared.parameterTypeId(4), DuckDBTypeId.TIME_TZ);
assert.deepEqual(prepared.parameterType(4), TIMETZ);
assert.equal(prepared.parameterTypeId(5), DuckDBTypeId.LIST);
assert.deepEqual(prepared.parameterType(5), LIST(INTEGER));
assert.equal(prepared.parameterTypeId(6), DuckDBTypeId.STRUCT);
assert.deepEqual(prepared.parameterType(6), STRUCT({ 'a': INTEGER, 'b': VARCHAR }));
assert.equal(prepared.parameterTypeId(7), DuckDBTypeId.ARRAY);
assert.deepEqual(prepared.parameterType(7), ARRAY(INTEGER, 3));
assert.equal(prepared.parameterTypeId(8), DuckDBTypeId.SQLNULL);
assert.deepEqual(prepared.parameterType(8), SQLNULL);
const result = await prepared.run();
assertColumns(result, [
{ name: 'a', type: INTEGER },
Expand Down
1 change: 1 addition & 0 deletions bindings/pkgs/@duckdb/node-bindings/duckdb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ export function parameter_name(prepared_statement: PreparedStatement, index: num
export function param_type(prepared_statement: PreparedStatement, index: number): Type;

// DUCKDB_API duckdb_logical_type duckdb_param_logical_type(duckdb_prepared_statement prepared_statement, idx_t param_idx);
export function param_logical_type(prepared_statement: PreparedStatement, index: number): LogicalType;

// DUCKDB_API duckdb_state duckdb_clear_bindings(duckdb_prepared_statement prepared_statement);
export function clear_bindings(prepared_statement: PreparedStatement): void;
Expand Down
15 changes: 13 additions & 2 deletions bindings/src/duckdb_node_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ class DuckDBNodeAddon : public Napi::Addon<DuckDBNodeAddon> {
InstanceMethod("nparams", &DuckDBNodeAddon::nparams),
InstanceMethod("parameter_name", &DuckDBNodeAddon::parameter_name),
InstanceMethod("param_type", &DuckDBNodeAddon::param_type),
InstanceMethod("param_logical_type", &DuckDBNodeAddon::param_logical_type),
InstanceMethod("clear_bindings", &DuckDBNodeAddon::clear_bindings),
InstanceMethod("prepared_statement_type", &DuckDBNodeAddon::prepared_statement_type),
InstanceMethod("bind_value", &DuckDBNodeAddon::bind_value),
Expand Down Expand Up @@ -1794,6 +1795,17 @@ class DuckDBNodeAddon : public Napi::Addon<DuckDBNodeAddon> {
}

// DUCKDB_API duckdb_logical_type duckdb_param_logical_type(duckdb_prepared_statement prepared_statement, idx_t param_idx);
// function param_logical_type(prepared_statement: PreparedStatement, index: number): LogicalType
Napi::Value param_logical_type(const Napi::CallbackInfo& info) {
auto env = info.Env();
auto prepared_statement = GetPreparedStatementFromExternal(env, info[0]);
auto index = info[1].As<Napi::Number>().Uint32Value();
auto logical_type = duckdb_param_logical_type(prepared_statement, index);
if (!logical_type) {
throw Napi::Error::New(env, "Failed to get logical type");
}
return CreateExternalForLogicalType(env, logical_type);
}

// DUCKDB_API duckdb_state duckdb_clear_bindings(duckdb_prepared_statement prepared_statement);
// function clear_bindings(prepared_statement: PreparedStatement): void
Expand Down Expand Up @@ -3896,9 +3908,8 @@ NODE_API_ADDON(DuckDBNodeAddon)
---
411 total functions

213 instance methods
214 instance methods
3 unimplemented instance cache functions
1 unimplemented prepared statement function
1 unimplemented logical type function
10 unimplemented value creation functions
13 unimplemented value inspection functions
Expand Down