Repository navigation
Closed
Description
Describe what's wrong
For some reason, the first column of a select is not returning the scalar result and instead returns something else:
CREATE TABLE t1 (i Int64, j Int64) ENGINE = Memory;
INSERT INTO t1 SELECT number, number FROM system.numbers LIMIT 10;
SELECT
(SELECT max(i) FROM t1) as i,
(SELECT max(i) FROM t1) as j,
(SELECT max(i) FROM t1) as k,
(SELECT max(i) FROM t1) as l
FROM t1
- Result:
┌─i─┬─j─┬─k─┬─l─┐
│ 0 │ 9 │ 9 │ 9 │
│ 1 │ 9 │ 9 │ 9 │
│ 2 │ 9 │ 9 │ 9 │
│ 3 │ 9 │ 9 │ 9 │
│ 4 │ 9 │ 9 │ 9 │
│ 5 │ 9 │ 9 │ 9 │
│ 6 │ 9 │ 9 │ 9 │
│ 7 │ 9 │ 9 │ 9 │
│ 8 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
└───┴───┴───┴───┘
- Expected:
┌─i─┬─j─┬─k─┬─l─┐
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
└───┴───┴───┴───┘
This also happens if I use a CTE to declare once the subquery:
WITH (
SELECT max(i)
FROM t1
) AS value
SELECT
value AS i,
value AS j,
value AS k,
value AS l
FROM t1
Query id: 8a85ed02-0ad7-4fe4-b01f-eabbb7521d52
┌─i─┬─j─┬─k─┬─l─┐
│ 0 │ 9 │ 9 │ 9 │
│ 1 │ 9 │ 9 │ 9 │
│ 2 │ 9 │ 9 │ 9 │
│ 3 │ 9 │ 9 │ 9 │
│ 4 │ 9 │ 9 │ 9 │
│ 5 │ 9 │ 9 │ 9 │
│ 6 │ 9 │ 9 │ 9 │
│ 7 │ 9 │ 9 │ 9 │
│ 8 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
└───┴───┴───┴───┘
Does it reproduce on recent release?
Tested on 20.7 (the oldest I keep around) and master and both exhibit the same behaviour so I guess everything is affected.
On master, describing the table shows that the first column didn't receive the scalar replacement, otherwise it would have been Nullable(Int64):
DESCRIBE TABLE
(
SELECT
(
SELECT max(i)
FROM t1
) AS i,
(
SELECT max(i)
FROM t1
) AS j,
(
SELECT max(i)
FROM t1
) AS k,
(
SELECT max(i)
FROM t1
) AS l
FROM t1
)
Query id: 0f58496d-7d51-40a1-8ae9-fd68b8e431b6
┌─name─┬─type────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ i │ Int64 │ │ │ │ │ │
│ j │ Nullable(Int64) │ │ │ │ │ │
│ k │ Nullable(Int64) │ │ │ │ │ │
│ l │ Nullable(Int64) │ │ │ │ │ │
└──────┴─────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
4 rows in set. Elapsed: 0.002 sec.
Reacted by Nikita Taranov
Activity
Algunenano commentedon Jan 20, 2022
It seems that the issue is because
AS ihas the same alias as the source table column name, and using other name leads to proper results:Now, why that happens and why if only affects
iand notjI still don't know.Algunenano commentedon Jan 20, 2022
Even more weird:
It appears to be matching the first column from the table and only that column, all other seem to be fine.
Algunenano commentedon Jan 20, 2022
This one fails two, so the issue isn't with the first column or something like that:
The issue goes away if I disabled setting
prefer_alias_to_column_nameto 0 for that subquery so something, at some point of the executing, is using the alias as a column name from the source instead of using the AST that has replaced the subquery (a bunch of casts).Algunenano commentedon Jan 21, 2022
I've been trying to understand how CH decides where it should get its data from and just to be nice I'll say it's really complex and messy. I'm not surprised there is a ticket (#23194) to say how things are (or were at some point in time) and how things should be.
There are "a few" hacks, like
prefer_alias_to_column_nameorunique_column_name, to work around some of the issues that come from years of evolution and they are more complexity to the problem so getting a full picture of how things work it's pretty hard. It also seems to follow a bottom-up approach which makes discarding unused columns / subqueries harder.All of this is to say that a) I've been unable to fix this and will leave it here for a while and b) I think that CH would benefit from rethinking / rebuilding the interpreters with a full view of what should be possible and how things should work. If that ever happened I'd also include there the learnings from other databases to optimize queries. I really liked Materialize blog about decorrelation and I'd hope to have an internal structure able to do these kinds of things.
fm4v commentedon Apr 3, 2024
Seems to be fixed with new analyzer. @Algunenano could you please close the task if this is the expected behavior.
Algunenano commentedon Apr 3, 2024
LGTM
den-crane commentedon Apr 3, 2024
It's the interesting case. I would add a test.
Close: #33825