diff --git a/ibis/backends/clickhouse/compiler.py b/ibis/backends/clickhouse/compiler.py index f0f9a866fa7d..760452924b4d 100644 --- a/ibis/backends/clickhouse/compiler.py +++ b/ibis/backends/clickhouse/compiler.py @@ -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 @@ -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): @@ -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() + ) diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_cast_double_col/float/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_cast_double_col/float/out.sql index 6e208faf9e4b..547402f372cf 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_cast_double_col/float/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_cast_double_col/float/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."double_col" + "t0"."double_col" AS "double_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/bigint_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/bigint_col/out.sql index 32ed9ce5dec7..1dfd2cfb826f 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/bigint_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/bigint_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."bigint_col" + "t0"."bigint_col" AS "bigint_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/bool_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/bool_col/out.sql index 581ad8820c60..9f084f794633 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/bool_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/bool_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."bool_col" + "t0"."bool_col" AS "bool_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/date_string_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/date_string_col/out.sql index 95fc17b5c1b5..3a5cd81f5bba 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/date_string_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/date_string_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."date_string_col" + "t0"."date_string_col" AS "date_string_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/double_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/double_col/out.sql index 6e208faf9e4b..547402f372cf 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/double_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/double_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."double_col" + "t0"."double_col" AS "double_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/float_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/float_col/out.sql index 011938c86330..057ea12d1f59 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/float_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/float_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."float_col" + "t0"."float_col" AS "float_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/id/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/id/out.sql index e162ed46b465..35b5cffe9346 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/id/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/id/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."id" + "t0"."id" AS "id" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/int_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/int_col/out.sql index ad1282f2b06b..8ed035148644 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/int_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/int_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."int_col" + "t0"."int_col" AS "int_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/month/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/month/out.sql index 8cf6f8be9ab1..b1980a23b35b 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/month/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/month/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."month" + "t0"."month" AS "month" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/smallint_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/smallint_col/out.sql index 9d35742155b4..a03962bfdcb4 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/smallint_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/smallint_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."smallint_col" + "t0"."smallint_col" AS "smallint_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/string_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/string_col/out.sql index 11a14ce0716c..9ef3d8692960 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/string_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/string_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."string_col" + "t0"."string_col" AS "string_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/timestamp_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/timestamp_col/out.sql index cffb9b1135c6..ac244714ab42 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/timestamp_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/timestamp_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."timestamp_col" + "t0"."timestamp_col" AS "timestamp_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/tinyint_col/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/tinyint_col/out.sql index 8eeeecefaf66..8029faa6182c 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/tinyint_col/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/tinyint_col/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."tinyint_col" + "t0"."tinyint_col" AS "tinyint_col" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/year/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/year/out.sql index 0afd5e4842d5..35f4362e25f9 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/year/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_noop_cast/year/out.sql @@ -1,3 +1,3 @@ SELECT - "t0"."year" + "t0"."year" AS "year" FROM "functional_alltypes" AS "t0" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_complex_array_expr_projection/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_complex_array_expr_projection/out.sql index 54d3826d1847..52daea835479 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_complex_array_expr_projection/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_complex_array_expr_projection/out.sql @@ -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 diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_complex_join/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_complex_join/out.sql index 4e106ac768a3..78947e9897f2 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_complex_join/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_complex_join/out.sql @@ -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" diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_count_name/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_count_name/out.sql index 818766ac3f33..a4a1af469963 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_count_name/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_count_name/out.sql @@ -1,5 +1,5 @@ SELECT - "t0"."a", + "t0"."a" AS "a", COALESCE(countIf(NOT ( "t0"."b" )), 0) AS "A", diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_join_self_reference/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_join_self_reference/out.sql index 059d27e85c4a..42c7ab8b6ab6 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_join_self_reference/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_join_self_reference/out.sql @@ -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" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_named_from_filter_groupby/out1.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_named_from_filter_groupby/out1.sql index 3daf4721d275..a3b900a39f70 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_named_from_filter_groupby/out1.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_named_from_filter_groupby/out1.sql @@ -1,5 +1,5 @@ SELECT - "t1"."key", + "t1"."key" AS "key", SUM(( ( "t1"."value" + 1 diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_named_from_filter_groupby/out2.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_named_from_filter_groupby/out2.sql index adb4473f07dc..0c1b8f8a4a27 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_named_from_filter_groupby/out2.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_named_from_filter_groupby/out2.sql @@ -1,5 +1,5 @@ SELECT - "t1"."key", + "t1"."key" AS "key", SUM(( ( "t1"."value" + 1 diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-any_inner_join/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-any_inner_join/out.sql index 7ea322ab3e40..907184cd665f 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-any_inner_join/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-any_inner_join/out.sql @@ -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" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-any_left_join/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-any_left_join/out.sql index 99c2c8ed05ba..b4927428448a 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-any_left_join/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-any_left_join/out.sql @@ -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" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-inner_join/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-inner_join/out.sql index b0e5b8e38295..ef6d103baf5e 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-inner_join/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-inner_join/out.sql @@ -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" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-left_join/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-left_join/out.sql index 3933c87710b6..bc25c07aedcb 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-left_join/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-awardID-left_join/out.sql @@ -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 OUTER JOIN "awards_players" AS "t3" ON "t2"."playerID" = "t3"."awardID" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-any_inner_join/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-any_inner_join/out.sql index 71a2337a7448..277d2b52a180 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-any_inner_join/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-any_inner_join/out.sql @@ -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"."playerID" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-any_left_join/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-any_left_join/out.sql index 7181dbe8cc83..4eb594f0b0b3 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-any_left_join/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-any_left_join/out.sql @@ -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"."playerID" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-inner_join/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-inner_join/out.sql index fc1eeca36630..cc2e1c1aee2d 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-inner_join/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-inner_join/out.sql @@ -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"."playerID" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-left_join/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-left_join/out.sql index e3d44d128a61..a9fc8c09169b 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-left_join/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_simple_joins/playerID-playerID-left_join/out.sql @@ -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 OUTER JOIN "awards_players" AS "t3" ON "t2"."playerID" = "t3"."playerID" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_table_column_unbox/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_table_column_unbox/out.sql index 9429adae6248..40555aec4c90 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_table_column_unbox/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_table_column_unbox/out.sql @@ -1,8 +1,8 @@ SELECT - "t2"."string_col" + "t2"."string_col" AS "string_col" FROM ( SELECT - "t1"."string_col", + "t1"."string_col" AS "string_col", SUM("t1"."float_col") AS "total" FROM ( SELECT diff --git a/ibis/backends/clickhouse/tests/snapshots/test_select/test_where_with_timestamp/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_select/test_where_with_timestamp/out.sql index 2650818b44ad..147ebfe8ccfd 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_select/test_where_with_timestamp/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_select/test_where_with_timestamp/out.sql @@ -1,5 +1,5 @@ SELECT - "t0"."uuid", + "t0"."uuid" AS "uuid", minIf("t0"."ts", "t0"."search_level" = 1) AS "min_date" FROM "t" AS "t0" GROUP BY diff --git a/ibis/backends/clickhouse/tests/test_client.py b/ibis/backends/clickhouse/tests/test_client.py index 487862bb2e4e..3f1f4c293f23 100644 --- a/ibis/backends/clickhouse/tests/test_client.py +++ b/ibis/backends/clickhouse/tests/test_client.py @@ -20,6 +20,7 @@ CLICKHOUSE_USER, IBIS_TEST_CLICKHOUSE_DB, ) +from ibis.backends.tests.errors import ClickHouseDatabaseError from ibis.util import gen_name cc = pytest.importorskip("clickhouse_connect") @@ -357,7 +358,7 @@ def test_create_table_no_syntax_error(con): def test_password_with_bracket(): - password = f'{os.environ.get("IBIS_TEST_CLICKHOUSE_PASSWORD", "")}[' + password = f'{os.environ.get("IBIS_TEST_CLICKHOUSE_PASSWORD", "")}[]' quoted_pass = quote_plus(password) host = os.environ.get("IBIS_TEST_CLICKHOUSE_HOST", "localhost") user = os.environ.get("IBIS_TEST_CLICKHOUSE_USER", "default") @@ -379,3 +380,32 @@ def test_invalid_port(con): url = f"clickhouse://{CLICKHOUSE_USER}:{CLICKHOUSE_PASS}@{CLICKHOUSE_HOST}:{port}/{IBIS_TEST_CLICKHOUSE_DB}" with pytest.raises(cc.driver.exceptions.DatabaseError): ibis.connect(url) + + +def test_subquery_with_join(con): + name = gen_name("clickhouse_tmp_table") + + s = con.create_table(name, pa.Table.from_pydict({"a": [1, 2, 3]}), temp=True) + + sql = f""" + SELECT + "o"."a" + FROM ( + SELECT + "w"."a" + FROM "{name}" AS "s" + INNER JOIN "{name}" AS "w" + USING ("a") + ) AS "o" + """ + with pytest.raises( + ClickHouseDatabaseError, match="Identifier 'o.a' cannot be resolved" + ): + # https://github.com/ClickHouse/ClickHouse/issues/66133 + con.sql(sql) + + # this works because we add the additional alias in the inner query + w = s.view() + expr = s.join(w, "a").select(a=w.a).select(b=lambda t: t.a + 1) + result = expr.to_pandas() + assert set(result["b"].tolist()) == {2, 3, 4} diff --git a/ibis/backends/tests/snapshots/test_generic/test_many_subqueries/clickhouse/out.sql b/ibis/backends/tests/snapshots/test_generic/test_many_subqueries/clickhouse/out.sql index 9c85ae747f64..0ce14d95ef6e 100644 --- a/ibis/backends/tests/snapshots/test_generic/test_many_subqueries/clickhouse/out.sql +++ b/ibis/backends/tests/snapshots/test_generic/test_many_subqueries/clickhouse/out.sql @@ -1,32 +1,32 @@ WITH "t1" AS ( SELECT - "t0"."street", + "t0"."street" AS "street", ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key" FROM "data" AS "t0" ), "t7" AS ( SELECT - "t6"."street", + "t6"."street" AS "street", ROW_NUMBER() OVER (ORDER BY "t6"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key" FROM ( SELECT - "t3"."street", - "t3"."key" + "t3"."street" AS "street", + "t3"."key" AS "key" FROM "t1" AS "t3" INNER JOIN ( SELECT - "t2"."key" + "t2"."key" AS "key" FROM "t1" AS "t2" ) AS "t5" ON "t3"."key" = "t5"."key" ) AS "t6" ) SELECT - "t9"."street", - "t9"."key" + "t9"."street" AS "street", + "t9"."key" AS "key" FROM "t7" AS "t9" INNER JOIN ( SELECT - "t8"."key" + "t8"."key" AS "key" FROM "t7" AS "t8" ) AS "t11" ON "t9"."key" = "t11"."key" \ No newline at end of file diff --git a/ibis/backends/tests/snapshots/test_sql/test_cte_refs_in_topo_order/clickhouse/out.sql b/ibis/backends/tests/snapshots/test_sql/test_cte_refs_in_topo_order/clickhouse/out.sql index 81f81df663b0..eb2d2fcb71ca 100644 --- a/ibis/backends/tests/snapshots/test_sql/test_cte_refs_in_topo_order/clickhouse/out.sql +++ b/ibis/backends/tests/snapshots/test_sql/test_cte_refs_in_topo_order/clickhouse/out.sql @@ -6,13 +6,13 @@ WITH "t1" AS ( TRUE ) SELECT - "t3"."key" + "t3"."key" AS "key" FROM "t1" AS "t3" INNER JOIN "t1" AS "t4" ON "t3"."key" = "t4"."key" INNER JOIN ( SELECT - "t3"."key" + "t3"."key" AS "key" FROM "t1" AS "t3" INNER JOIN "t1" AS "t4" ON "t3"."key" = "t4"."key" diff --git a/ibis/backends/tests/snapshots/test_sql/test_selects_with_impure_operations_not_merged/clickhouse-random/out.sql b/ibis/backends/tests/snapshots/test_sql/test_selects_with_impure_operations_not_merged/clickhouse-random/out.sql index d8f3ac64531b..585534370aa7 100644 --- a/ibis/backends/tests/snapshots/test_sql/test_selects_with_impure_operations_not_merged/clickhouse-random/out.sql +++ b/ibis/backends/tests/snapshots/test_sql/test_selects_with_impure_operations_not_merged/clickhouse-random/out.sql @@ -1,11 +1,11 @@ SELECT - "t1"."x", - "t1"."y", - "t1"."z", + "t1"."x" AS "x", + "t1"."y" AS "y", + "t1"."z" AS "z", CASE WHEN "t1"."y" = "t1"."z" THEN 'big' ELSE 'small' END AS "size" FROM ( SELECT - "t0"."x", + "t0"."x" AS "x", randCanonical() AS "y", randCanonical() AS "z" FROM "t" AS "t0" diff --git a/ibis/backends/tests/snapshots/test_sql/test_selects_with_impure_operations_not_merged/clickhouse-uuid/out.sql b/ibis/backends/tests/snapshots/test_sql/test_selects_with_impure_operations_not_merged/clickhouse-uuid/out.sql index f2ed2707b0f5..bf6c9272bc2f 100644 --- a/ibis/backends/tests/snapshots/test_sql/test_selects_with_impure_operations_not_merged/clickhouse-uuid/out.sql +++ b/ibis/backends/tests/snapshots/test_sql/test_selects_with_impure_operations_not_merged/clickhouse-uuid/out.sql @@ -1,11 +1,11 @@ SELECT - "t1"."x", - "t1"."y", - "t1"."z", + "t1"."x" AS "x", + "t1"."y" AS "y", + "t1"."z" AS "z", CASE WHEN "t1"."y" = "t1"."z" THEN 'big' ELSE 'small' END AS "size" FROM ( SELECT - "t0"."x", + "t0"."x" AS "x", generateUUIDv4() AS "y", generateUUIDv4() AS "z" FROM "t" AS "t0" diff --git a/ibis/backends/tests/snapshots/test_sql/test_union_aliasing/clickhouse/out.sql b/ibis/backends/tests/snapshots/test_sql/test_union_aliasing/clickhouse/out.sql index 13f05009a8c0..67a22a507cb5 100644 --- a/ibis/backends/tests/snapshots/test_sql/test_union_aliasing/clickhouse/out.sql +++ b/ibis/backends/tests/snapshots/test_sql/test_union_aliasing/clickhouse/out.sql @@ -1,30 +1,30 @@ WITH "t5" AS ( SELECT - "t4"."field_of_study", + "t4"."field_of_study" AS "field_of_study", any("t4"."diff") AS "diff" FROM ( SELECT - "t3"."field_of_study", - "t3"."years", - "t3"."degrees", - "t3"."earliest_degrees", - "t3"."latest_degrees", + "t3"."field_of_study" AS "field_of_study", + "t3"."years" AS "years", + "t3"."degrees" AS "degrees", + "t3"."earliest_degrees" AS "earliest_degrees", + "t3"."latest_degrees" AS "latest_degrees", "t3"."latest_degrees" - "t3"."earliest_degrees" AS "diff" FROM ( SELECT - "t2"."field_of_study", - "t2"."years", - "t2"."degrees", + "t2"."field_of_study" AS "field_of_study", + "t2"."years" AS "years", + "t2"."degrees" AS "degrees", FIRST_VALUE("t2"."degrees") OVER (PARTITION BY "t2"."field_of_study" ORDER BY "t2"."years" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "earliest_degrees", LAST_VALUE("t2"."degrees") OVER (PARTITION BY "t2"."field_of_study" ORDER BY "t2"."years" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "latest_degrees" FROM ( SELECT - "t1"."field_of_study", + "t1"."field_of_study" AS "field_of_study", CAST("t1"."__pivoted__".1 AS Nullable(String)) AS "years", CAST("t1"."__pivoted__".2 AS Nullable(Int64)) AS "degrees" FROM ( SELECT - "t0"."field_of_study", + "t0"."field_of_study" AS "field_of_study", arrayJoin( [ CAST(tuple('1970-71', "t0"."1970-71") AS Tuple("years" Nullable(String), "degrees" Nullable(Int64))),