Repository navigation
Description
What's wrong
Using the setting prefer_column_name_to_alias=1.
With multiple SELECT sub-queries in which columns are aliased to the same name, using those alias in the top query seems to refer columns of the bottom query.
Does it reproduce on recent release?
I first detected the bug on release v22.7.3.5. I was able to reproduce it on ClickHouse Fiddle using the latest release v23.2.3.17-stable.
How to reproduce
Step by step reproduction : https://fiddle.clickhouse.com/6ae1c08a-ac7c-4b64-8843-cfc9a824c151
- ClickHouse server version to use :
v22.7.3.5orv23.2.3.17-stable(may be any) ; - Interface to use : doesn't seem to matter ;
- Using setting
prefer_column_name_to_alias=1; - Preparation statements :
CREATE TABLE clickhouse_alias_issue_1 (
id bigint,
column_1 Nullable(Float32)
) Engine=Memory;
CREATE TABLE clickhouse_alias_issue_2 (
id bigint,
column_2 Nullable(Float32)
) Engine=Memory;
INSERT INTO `clickhouse_alias_issue_1`
VALUES (1, 100), (2, 200), (3, 300);
INSERT INTO `clickhouse_alias_issue_2`
VALUES (1, 10), (2, 20), (3, 30);- Query to run that lead to the unexpected result :
SELECT `column_1` / `column_2`, `id`
FROM (
SELECT
max(`column_1`) AS `column_1`,
max(`column_2`) AS `column_2`,
`id`
FROM (
SELECT
max(`column_1`) AS `column_1`,
NULL AS `column_2`,
`id`
FROM `clickhouse_alias_issue_1`
GROUP BY
`id`
UNION ALL
SELECT
NULL AS `column_1`,
max(`column_2`) AS `column_2`,
`id`
FROM `clickhouse_alias_issue_2`
GROUP BY
`id`
SETTINGS prefer_column_name_to_alias=1
) as T1
GROUP BY `id`
SETTINGS prefer_column_name_to_alias=1
) as T2
WHERE `column_1` IS NOT NULL AND `column_2` IS NOT NULL
SETTINGS prefer_column_name_to_alias=1;Expected behavior
The expected result is :
| column_1 / column_2 | id |
|---|---|
| 10 | 3 |
| 10 | 2 |
| 10 | 1 |
While the actual result is empty :
| column_1 / column_2 | id |
|---|
It seems like the columns used in the WHERE condition are referencing T1 columns instead of T2.
For your convenience, here are the returned values of T1 and T2 :
T1
| column_1 | column_2 | id |
|---|---|---|
| \N | 30 | 3 |
| \N | 20 | 2 |
| \N | 10 | 1 |
| 300 | \N | 3 |
| 200 | \N | 2 |
| 100 | \N | 1 |
T2
| column_1 | column_2 | id |
|---|---|---|
| 300 | 30 | 3 |
| 200 | 20 | 2 |
| 100 | 10 | 1 |
Activity
eteresh commentedon Jun 16, 2023
I experience the same issue with enabled
prefer_column_name_to_alias. This simple query return 2 rows (as expected), whenprefer_column_name_to_aliasset to zero. Whenprefer_column_name_to_aliasset to 1 query return only single row.This query
returns 1 row:
after changing settings to
SETTINGS prefer_column_name_to_alias = 0the same query returns 2 rows:HSEhomework commentedon Sep 24, 2023
It is expected that prefer_column_name_to_alias has to pick the first alias after group by statements, otherwise it works incorrect.
Any workaround for this please?
eblio commentedon Sep 25, 2023
@HSEhomework The workaround we found is to insert the sub-query content into a
Memory()table, and only then performing the actual computation on that temporary table.We are looking to ditch that setting as it's behavior seems unreliable.
nikitamikhaylov commentedon Apr 9, 2024
Fixed by Analyzer: https://fiddle.clickhouse.com/dba9ab3d-4a6b-4034-91be-ea108a4c1fa1
Close: #47552