#9675·jaeger

[Feature]: Route structured query filters to ClickHouse's SQL WHERE (RFC 0005 M3)

Author: om7057Created Sep 28, 2026Updated Sep 29, 2026

Requirement

RFC 0005 (Structured Query Filters) Stage B lists M3 as "ClickHouse: route level-qualified predicates to their typed Map column (query_builder.go) and lower the boolean tree into the SQL WHERE (AND/OR/NOT)". M4 delivered the equivalent for Elasticsearch/OpenSearch in #9350; ClickHouse's M3 has not started.

Today query.Filter never reaches the ClickHouse reader at all: Reader.SearchCapabilities declares no Filter field, so querysvc.queryToReaderCapabilities folds every filter down via TraceQueryParams.ToLegacyShape() before dispatch. That conversion only expresses a conjunctive, eq-only filter over indexed levels; anything with OR/NOT, a comparison other than eq (gt, lt, regex, in, exists, ...), or a level ToLegacyShape cannot carry, is refused outright, regardless of whether ClickHouse's own schema and query builder could answer it. buildFindTraceIDsQuery (internal/storage/v2/clickhouse/tracestore/query_builder.go) only ever reads the legacy scalar fields (ServiceName, OperationName, Attributes, duration/time bounds); it has no path for the filter AST at all.

This also blocks RFC 0016 M5 (ClickHouse span search), which needs an identity filter lowered onto the trace_id skip index and therefore needs some filter-lowering mechanism to exist first.

Proposal

Add native filter routing to the ClickHouse reader, following the same shape M4 used for Elasticsearch (core/filter.go's buildFilterQuery/FilterCapabilities):

  • Declare SearchCapabilities.Filter on Reader (currently absent), naming the levels/operators ClickHouse can serve.
  • Add a lowering function (e.g. buildFilterCondition) that walks an *expression.Call and emits the SQL WHERE tree: and/or/not map directly to SQL boolean structure; comparisons (eq, ne, gt, lt, gte, lte, regex via ClickHouse's match(), exists, in/not_in) map to conditions over the typed Map columns.
  • Reuse the existing typed-attribute machinery rather than duplicating it: attribute_metadata's level-skipping (getAttributeMetadata), buildAttributeConditions's per-level/per-type column selection, and appendArrayExists/appendNestedArrayExists for the array-valued (event/link) columns.
  • Cover the built-in fields the column set already carries: span.name, span.duration, span.status, span.kind (columns name, duration, status_code/status_message, kind), resource.service (service_name). Scope, link, and any level/operator the schema cannot answer are refused via the declared capability, the same posture M4 took for scope/link on Elasticsearch.
  • Wire it into buildFindTraceIDsQuery: once Filter capability is declared, a query carrying query.Filter is no longer folded to legacy shape upstream (per queryToReaderCapabilities), so the builder branches on query.Filter != nil and lowers it directly instead of reading the legacy scalar fields.

Exit

Level-qualified and boolean queries lower to the corresponding SQL; the pre-existing unqualified snapshots are byte-identical. Per M4's own precedent, the filter battery in the shared storage integration suite (internal/storage/integration/filters.go) should run against a live ClickHouse too, holding every declared level/operator to an exact result set and asserting refused shapes (scope, link, unsupported operators) come back as refusals rather than a wider answer.