Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CountDistinct cannot find column #59999

Closed
blaird1923 opened this issue Feb 14, 2024 · 2 comments · Fixed by #60026
Closed

CountDistinct cannot find column #59999

blaird1923 opened this issue Feb 14, 2024 · 2 comments · Fixed by #60026
Assignees
Labels
backward compatibility bug Confirmed user-visible misbehaviour in official release

Comments

@blaird1923
Copy link

(you don't have to strictly follow this form)

Describe the issue
This query used to work but now returns Unknown column: total_devtags.
Here is a minimal reproduction.
Note that I've included very similar queries which do work.
With the simplest possible query, there is no problem.

How to reproduce
create table tags (dev_tag String) ENGINE = Memory;

insert into tags (*) values ('a');

SELECT *
FROM
(
SELECT countDistinct(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
GROUP BY dev_tag
) AS t
)

SELECT *
FROM
(
SELECT count(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
GROUP BY dev_tag
) AS t
)

SELECT *
FROM
(
SELECT countDistinct(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
) AS t
)

  • Which ClickHouse server versions are incompatible
    23.x vs. 22.8

  • Which interface to use, if matters

  • Non-default settings, if any

  • CREATE TABLE statements for all tables involved

  • Sample data for all these tables, use clickhouse-obfuscator if necessary

  • Queries to run that lead to unexpected result
    SELECT *
    FROM
    (
    SELECT countDistinct(dev_tag) AS total_devtags
    FROM
    (
    SELECT dev_tag
    FROM
    (
    SELECT *
    FROM tags
    ) AS t
    GROUP BY dev_tag
    ) AS t
    )
    Error message and/or stacktrace
    If applicable, add screenshots to help explain your problem.
    FAZVM64 :) SELECT *
    FROM
    (
    SELECT countDistinct(dev_tag) AS total_devtags
    FROM
    (
    SELECT dev_tag
    FROM
    (
    SELECT *
    FROM tags
    ) AS t
    GROUP BY dev_tag
    ) AS t
    )

SELECT *
FROM
(
SELECT countDistinct(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
GROUP BY dev_tag
) AS t
)

Query id: 0a82feec-afea-4c0c-9d31-9b2ba65608f5

Elapsed: 0.004 sec.

Received exception from server (version 23.12.1):
Code: 47. DB::Exception: Received from localhost:9000. DB::Exception: Unknown column: total_devtags, there are only columns . (UNKNOWN_IDENTIFIER)

FAZVM64 :) SELECT *
FROM
(
SELECT count(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
GROUP BY dev_tag
) AS t
)

SELECT *
FROM
(
SELECT count(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
GROUP BY dev_tag
) AS t
)

Query id: 542aee51-4244-43c9-ad67-ef190d6708cf

┌─total_devtags─┐
│ 1 │
└───────────────┘

1 row in set. Elapsed: 0.004 sec.

FAZVM64 :)
FAZVM64 :)
FAZVM64 :)
FAZVM64 :)
FAZVM64 :) SELECT *
FROM
(
SELECT countDistinct(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
) AS t
)

SELECT *
FROM
(
SELECT countDistinct(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
) AS t
)

Query id: f4e85d28-4c40-4caa-866f-4418276c345d

┌─total_devtags─┐
│ 1 │
└───────────────┘

1 row in set. Elapsed: 0.004 sec.

Additional context
Add any other context about the problem here.

@Algunenano
Copy link
Member

Algunenano commented Feb 15, 2024

  • Works fine with the (new/experimental) analyzer
  • Works fine if you replace countDistinct with uniqExact.
  • Seems like an AST pass in the old analyzer is losing aliases.

@Algunenano Algunenano added the bug Confirmed user-visible misbehaviour in official release label Feb 15, 2024
@Algunenano
Copy link
Member

Temporal fix: set optimize_uniq_to_count=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backward compatibility bug Confirmed user-visible misbehaviour in official release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants