Skip to content

Support ClickHouse's query parameters #436

@niyue

Description

@niyue

Use Case

ClickHouse natively supports Query Parameters feature [1][2], which allow users to write generic SQL templates using typed placeholders. This avoids manual string interpolation, improves readability, and reduces the risk of SQL injection.

Sample usage

import chdb

df = chdb.query(
    "SELECT toDate({base_date:String}) + number AS date "
    "FROM numbers({total_days:UInt64}) "
    "LIMIT {items_per_page:UInt64}",
    "DataFrame",
    params={"base_date": "2025-01-01", "total_days": 10, "items_per_page": 2},
)
print(df)

# expected output:
#         date
# 0 2025-01-01
# 1 2025-01-02

This is standard ClickHouse syntax, but chdb does not yet support passing query parameters.

Describe the solution you'd like

Enable query parameters in chdb by wiring through ClickHouse’s native setQueryParameters C++ API.
Expose this capability across chdb’s language bindings (Python, Node.js, etc.) so end users can easily supply a params dictionary when executing a query.

Additional context

This would bring chdb to parity with clickhouse client CLI and clickhouse-connect Python library

  • ClickHouse client CLI supports this feature [2], e.g.
clickhouse-client --param_message='hello' --query="SELECT {message: String}"
hello
  • clickhouse-connect, the official ClickHouse Python client library, supports similar features in its QueryContext API [3]
client.create_query_context(query='SELECT value1, value2 FROM data_table WHERE key = {k:Int32}',
                            parameters={'k': 2},
                            column_oriented=True)
result = client.query(context=qc)

References

[1] ClickHouse SQL syntax: defining and using query parameters, https://clickhouse.com/docs/sql-reference/syntax#defining-and-using-query-parameters
[2] How to Use Query Parameters in ClickHouse, https://clickhouse.com/videos/how-to-use-query-parameters-in-clickhouse
[3] clickhouse-connect client library's support for query parameters, https://clickhouse.com/docs/integrations/language-clients/python/advanced-querying#querycontexts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions