Skip to content

Structured query: identifiers containing '?' are rejected (clickhouse-go positional binder miscounts '?') #279

Open

Description

@EricAndrechek

Summary

Structured queries (POST /v1/query) accept any identifier ClickHouse accepts — except one whose name contains a literal ?. Such identifiers are rejected fail-closed by bindUnsafe() in internal/query/builder.go. This issue tracks why, and what it would take to lift the restriction.

Root cause — clickhouse-go positional binder is not quoting-aware

We bind filter / time-range values as positional ? parameters (the driver escapes them safely). The driver's ?-binding scans the query string and counts every ? to match against the supplied args — it is not quoting-aware, so it also counts ? characters inside string literals and inside backtick-quoted identifiers.

Because the builder backtick-quotes identifiers, a column named a?b becomes `a?b` in the SQL — which still contains a ?. The driver then miscounts that ? against our value args.

First-party repro (native driver, clickhouse-go v2.46)

SELECT '?' AS lit, ? AS bound          + 1 arg  → ERROR: have no arg for param ? at last 1 positions
SELECT `?` FROM t WHERE `?` = ?         + 1 arg  → ERROR: have no arg for param ? at last 2 positions

The driver counted the ? inside the '?' literal and inside the `?` identifiers as parameter placeholders. Best case it errors; worst case an off-by-one shifts a value into the wrong slot — including, in principle, a row-level-security filter value — which is why we fail closed rather than emit the query.

Corroborating (same class, different driver)

metabase/metabase#56364 — a column named field? with a WHERE filter fails as Unknown … identifier 'fieldnull' (the ? in the identifier is consumed by positional binding). Same root behavior in a different ClickHouse driver.

Current mitigation

internal/query/builder.go bindUnsafe() rejects any identifier (column, table, alias, group_by/order_by/time_range) containing ? with a clear 4xx before the query is built. Pathological — no real schema names a column ? — and it fails closed (never mis-binds).

Path to removal

  • Server-side {name:Identifier} params would avoid ? entirely (the identifier becomes param data, never query text). Blocked today: clickhouse-go cannot mix {name:…} params with positional ? value binding (unsupported query parameter type), so adopting it means converting all values to typed named params — a larger change to the value path, deferred.
  • Upstream fix: a quoting-aware positional binder in clickhouse-go removes the need for the restriction. No existing clickhouse-go issue found tracking this — worth filing upstream. Relevant code: clickhouse-go/bind.go.

Acceptance

Either adopt server-side {name:Identifier} params for identifiers, or land an upstream quote-aware binder; then drop the ? rejection in bindUnsafe() and add a real-ClickHouse round-trip test for a ?-named column.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/queryStructured query AST, SQL builderbugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions