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

fix(clickhouse): generate redundant aliases to workaround clickhouse naming behavior #9525

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion ibis/backends/clickhouse/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import calendar
import math
from typing import Any
from typing import TYPE_CHECKING, Any

import sqlglot as sg
import sqlglot.expressions as sge
Expand All @@ -15,6 +15,9 @@
from ibis.backends.sql.datatypes import ClickHouseType
from ibis.backends.sql.dialects import ClickHouse

if TYPE_CHECKING:
from collections.abc import Iterator, Mapping


class ClickhouseAggGen(AggGen):
def aggregate(self, compiler, name, *args, where=None):
Expand Down Expand Up @@ -705,3 +708,31 @@ def visit_TableUnnest(
)

return select

def _cleanup_names(
self, exprs: Mapping[str, sge.Expression]
) -> Iterator[sge.Expression]:
"""Compose `_gen_valid_name` and `_dedup_name` to clean up names in projections.

ClickHouse has a bug where this fails to find the final `"o"."a"` column:

```sql
SELECT
"o"."a"
FROM (
SELECT
"w"."a"
FROM "t" AS "s"
INNER JOIN "t" AS "w"
USING ("a")
) AS "o"
```

Adding a redundant aliasing operation (`"w"."a" AS "a"`) helps
ClickHouse.
"""
quoted = self.quoted
return (
value.as_(self._gen_valid_name(name), quoted=quoted, copy=False)
for name, value in exprs.items()
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."double_col"
"t0"."double_col" AS "double_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."bigint_col"
"t0"."bigint_col" AS "bigint_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."bool_col"
"t0"."bool_col" AS "bool_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."date_string_col"
"t0"."date_string_col" AS "date_string_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."double_col"
"t0"."double_col" AS "double_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."float_col"
"t0"."float_col" AS "float_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."id"
"t0"."id" AS "id"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."int_col"
"t0"."int_col" AS "int_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."month"
"t0"."month" AS "month"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."smallint_col"
"t0"."smallint_col" AS "smallint_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."string_col"
"t0"."string_col" AS "string_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."timestamp_col"
"t0"."timestamp_col" AS "timestamp_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."tinyint_col"
"t0"."tinyint_col" AS "tinyint_col"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."year"
"t0"."year" AS "year"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SELECT
CAST("t1"."string_col" AS Nullable(Float64)) AS "Cast(string_col, float64)"
FROM (
SELECT
"t0"."string_col",
"t0"."string_col" AS "string_col",
COUNT(*) AS "count"
FROM "functional_alltypes" AS "t0"
GROUP BY
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
SELECT
"t4"."a",
"t4"."b",
"t4"."c",
"t4"."d",
"t4"."a" AS "a",
"t4"."b" AS "b",
"t4"."c" AS "c",
"t4"."d" AS "d",
"t4"."c" / (
"t4"."a" - "t4"."b"
) AS "e"
FROM (
SELECT
"t2"."a",
"t2"."b",
"t3"."c",
"t3"."d"
"t2"."a" AS "a",
"t2"."b" AS "b",
"t3"."c" AS "c",
"t3"."d" AS "d"
FROM "s" AS "t2"
INNER JOIN "t" AS "t3"
ON "t2"."a" = "t3"."c"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
"t0"."a",
"t0"."a" AS "a",
COALESCE(countIf(NOT (
"t0"."b"
)), 0) AS "A",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
SELECT
"t1"."id",
"t1"."bool_col",
"t1"."tinyint_col",
"t1"."smallint_col",
"t1"."int_col",
"t1"."bigint_col",
"t1"."float_col",
"t1"."double_col",
"t1"."date_string_col",
"t1"."string_col",
"t1"."timestamp_col",
"t1"."year",
"t1"."month"
"t1"."id" AS "id",
"t1"."bool_col" AS "bool_col",
"t1"."tinyint_col" AS "tinyint_col",
"t1"."smallint_col" AS "smallint_col",
"t1"."int_col" AS "int_col",
"t1"."bigint_col" AS "bigint_col",
"t1"."float_col" AS "float_col",
"t1"."double_col" AS "double_col",
"t1"."date_string_col" AS "date_string_col",
"t1"."string_col" AS "string_col",
"t1"."timestamp_col" AS "timestamp_col",
"t1"."year" AS "year",
"t1"."month" AS "month"
FROM "functional_alltypes" AS "t1"
INNER JOIN "functional_alltypes" AS "t2"
ON "t1"."id" = "t2"."id"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
"t1"."key",
"t1"."key" AS "key",
SUM((
(
"t1"."value" + 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
"t1"."key",
"t1"."key" AS "key",
SUM((
(
"t1"."value" + 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
SELECT
"t2"."playerID",
"t2"."yearID",
"t2"."stint",
"t2"."teamID",
"t2"."lgID",
"t2"."G",
"t2"."AB",
"t2"."R",
"t2"."H",
"t2"."X2B",
"t2"."X3B",
"t2"."HR",
"t2"."RBI",
"t2"."SB",
"t2"."CS",
"t2"."BB",
"t2"."SO",
"t2"."IBB",
"t2"."HBP",
"t2"."SH",
"t2"."SF",
"t2"."GIDP"
"t2"."playerID" AS "playerID",
"t2"."yearID" AS "yearID",
"t2"."stint" AS "stint",
"t2"."teamID" AS "teamID",
"t2"."lgID" AS "lgID",
"t2"."G" AS "G",
"t2"."AB" AS "AB",
"t2"."R" AS "R",
"t2"."H" AS "H",
"t2"."X2B" AS "X2B",
"t2"."X3B" AS "X3B",
"t2"."HR" AS "HR",
"t2"."RBI" AS "RBI",
"t2"."SB" AS "SB",
"t2"."CS" AS "CS",
"t2"."BB" AS "BB",
"t2"."SO" AS "SO",
"t2"."IBB" AS "IBB",
"t2"."HBP" AS "HBP",
"t2"."SH" AS "SH",
"t2"."SF" AS "SF",
"t2"."GIDP" AS "GIDP"
FROM "batting" AS "t2"
ANY JOIN "awards_players" AS "t3"
ON "t2"."playerID" = "t3"."awardID"
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
SELECT
"t2"."playerID",
"t2"."yearID",
"t2"."stint",
"t2"."teamID",
"t2"."lgID",
"t2"."G",
"t2"."AB",
"t2"."R",
"t2"."H",
"t2"."X2B",
"t2"."X3B",
"t2"."HR",
"t2"."RBI",
"t2"."SB",
"t2"."CS",
"t2"."BB",
"t2"."SO",
"t2"."IBB",
"t2"."HBP",
"t2"."SH",
"t2"."SF",
"t2"."GIDP"
"t2"."playerID" AS "playerID",
"t2"."yearID" AS "yearID",
"t2"."stint" AS "stint",
"t2"."teamID" AS "teamID",
"t2"."lgID" AS "lgID",
"t2"."G" AS "G",
"t2"."AB" AS "AB",
"t2"."R" AS "R",
"t2"."H" AS "H",
"t2"."X2B" AS "X2B",
"t2"."X3B" AS "X3B",
"t2"."HR" AS "HR",
"t2"."RBI" AS "RBI",
"t2"."SB" AS "SB",
"t2"."CS" AS "CS",
"t2"."BB" AS "BB",
"t2"."SO" AS "SO",
"t2"."IBB" AS "IBB",
"t2"."HBP" AS "HBP",
"t2"."SH" AS "SH",
"t2"."SF" AS "SF",
"t2"."GIDP" AS "GIDP"
FROM "batting" AS "t2"
LEFT ANY JOIN "awards_players" AS "t3"
ON "t2"."playerID" = "t3"."awardID"
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
SELECT
"t2"."playerID",
"t2"."yearID",
"t2"."stint",
"t2"."teamID",
"t2"."lgID",
"t2"."G",
"t2"."AB",
"t2"."R",
"t2"."H",
"t2"."X2B",
"t2"."X3B",
"t2"."HR",
"t2"."RBI",
"t2"."SB",
"t2"."CS",
"t2"."BB",
"t2"."SO",
"t2"."IBB",
"t2"."HBP",
"t2"."SH",
"t2"."SF",
"t2"."GIDP"
"t2"."playerID" AS "playerID",
"t2"."yearID" AS "yearID",
"t2"."stint" AS "stint",
"t2"."teamID" AS "teamID",
"t2"."lgID" AS "lgID",
"t2"."G" AS "G",
"t2"."AB" AS "AB",
"t2"."R" AS "R",
"t2"."H" AS "H",
"t2"."X2B" AS "X2B",
"t2"."X3B" AS "X3B",
"t2"."HR" AS "HR",
"t2"."RBI" AS "RBI",
"t2"."SB" AS "SB",
"t2"."CS" AS "CS",
"t2"."BB" AS "BB",
"t2"."SO" AS "SO",
"t2"."IBB" AS "IBB",
"t2"."HBP" AS "HBP",
"t2"."SH" AS "SH",
"t2"."SF" AS "SF",
"t2"."GIDP" AS "GIDP"
FROM "batting" AS "t2"
INNER JOIN "awards_players" AS "t3"
ON "t2"."playerID" = "t3"."awardID"
Loading
Loading