Skip to content

Commit

Permalink
fix(clickhouse): generate redundant aliases to workaround clickhouse …
Browse files Browse the repository at this point in the history
…naming behavior
  • Loading branch information
cpcloud committed Jul 5, 2024
1 parent 23d9e07 commit 59530bb
Show file tree
Hide file tree
Showing 37 changed files with 307 additions and 249 deletions.
31 changes: 30 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 Mapping


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

return select

def _cleanup_names(self, exprs: Mapping[str, sge.Expression]):
"""Compose `_gen_valid_name` and `_dedup_name` to clean up names in projections.
ClickHouse has a bug where this fails semantic validation:
```sql
SELECT
"o"."a"
FROM (
SELECT
"w"."a"
FROM "{name}" AS "s"
INNER JOIN "{name}" AS "w"
USING ("a")
) AS "o"
```
Adding a redundant aliasing operation here (`"w"."a" AS "a"`) helps
ClickHouse. Not entirely clear whether this is an upstream bug.
"""
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

0 comments on commit 59530bb

Please sign in to comment.