Skip to content

Set prefer_column_name_to_alias on ClickHouse connections - #948

Open
smoleyxd wants to merge 1 commit into
PlayPro:masterfrom
smoleyxd:fix-clickhouse-alias-shadowing
Open

Set prefer_column_name_to_alias on ClickHouse connections#948
smoleyxd wants to merge 1 commit into
PlayPro:masterfrom
smoleyxd:fix-clickhouse-alias-shadowing

Conversation

@smoleyxd

@smoleyxd smoleyxd commented Aug 9, 2026

Copy link
Copy Markdown

Fixes #947.

ClickHouse resolves identifiers to select-list aliases before table columns by default (prefer_column_name_to_alias = 0), so lookup queries that reuse a real column name as an alias mis-resolve. The item lookup builds ... data as metadata, 0 as data ... FROM co_item, where data in data as metadata binds to the 0 as data alias instead of the column:

SELECT toTypeName(metadata) FROM (SELECT data as metadata, 0 as data FROM co_item LIMIT 1)
-- UInt8 (should be Array(Int8))

Standalone item lookups return metadata as the literal 0; mixed lookup unions end up as Variant(Array(Int8), UInt8) on ClickHouse 26.x. Either way the JDBC client throws Column is not of array type in DatabaseUtils.getBytes and every /co lookup / /co inspect touching item rows returns nothing (details and full stack trace in #947).

This sets prefer_column_name_to_alias = 1 on the connection, which makes ClickHouse resolve columns first — the same semantics SQLite and MySQL give these queries. I audited the generated queries for the inverse hazard (a query needing the alias where a real column with that name exists) and found none: the setting only changes resolution when both an alias and a column share a name, and the only such collision is the data one this fixes.

Running in production on two servers (ClickHouse 26.7.1, ~85M events) since 2026-08-09: item metadata comes back as the correct blobs, mixed unions unify to Array(Int8), and lookups work again. The WHERE notEmpty(data)-style mis-resolutions on shadowed columns are fixed by the same setting.

ClickHouse resolves identifiers to select-list aliases before table
columns by default, so lookup queries that reuse a column name as an
alias (e.g. 'data as metadata, 0 as data' in the item lookup) return
the alias literal instead of the column. Item lookups then return
metadata as UInt8 0, mixed lookup unions produce
Variant(Array(Int8), UInt8), and the JDBC client fails every /co
lookup with 'Column is not of array type'.

Setting prefer_column_name_to_alias=1 restores the alias semantics
these queries rely on under SQLite and MySQL.

Fixes PlayPro#947
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.

ClickHouse: /co lookup fails when results include item rows (alias 0 as data shadows the data column)

1 participant