Skip to content

Conversation

staticlibs
Copy link
Collaborator

This PR adds new optional params argument to sqlite_query function that allows to pass query parameters to an underlying SQLite prepared statement.

Parameters are specified as a STRUCT, that can be created inline using row() function:

SELECT * from sqlite_query(sakila, 'SELECT ? || ? a', params=row('foo', 'bar'))
----
foobar

When the whole DuckDB SQL statement, that contains sqlite_query() function, is used with PREPARE + EXECUTE (for example, from Python or Java client), then the external client-provided parameters will be forwarded to SQLite:

PREPARE p1 as SELECT * from sqlite_query(sakila, 'SELECT ? || ? a', params=row(?, ?))

EXECUTE p1('foo', 'bar')
----
foobar

EXECUTE p1('baz', 'boo')
----
bazboo

DEALLOCATE p1

The following parameter types are supported: BIGINT, DOUBLE, BLOB, VARCHAR.

Only positional parameters are supported - STRUCT field names are ignored.

Testing: existing sqlite_query test is updated with parameters checks.

This PR adds new optional `params` argument to `sqlite_query` function
that allows to pass query parameters to an underlying SQLite prepared
statement.

Parameters are specified as a `STRUCT`, that can be created inline using
`row()` function:

```sql
SELECT * from sqlite_query(sakila, 'SELECT ? || ? a', params=row('foo', 'bar'))
----
foobar
```

When the whole DuckDB SQL statement, that contains `sqlite_query()`
function, is used with `PREPARE` + `EXECUTE` (for example, from Python
or Java client), then the external client-provided parameters will be
forwarded to SQLite:

```sql
PREPARE p1 as SELECT * from sqlite_query(sakila, 'SELECT ? || ? a', params=row(?, ?))

EXECUTE p1('foo', 'bar')
----
foobar

EXECUTE p1('baz', 'boo')
----
bazboo

DEALLOCATE p1
```

The following parameter types are supported: `BIGINT`, `DOUBLE`, `BLOB`,
`VARCHAR`.

Only positional parameters are supported - `STRUCT` field names are
ignored.

Testing: existing `sqlite_query` test is updated with parameters checks.
@staticlibs staticlibs merged commit 0c93d61 into duckdb:main Sep 28, 2025
22 checks passed
@staticlibs staticlibs deleted the query_params branch September 28, 2025 07:22
@staticlibs
Copy link
Collaborator Author

Merging this for API parity with duckdb/duckdb-mysql#158.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant