Skip to content

Commit

Permalink
fix(clickhouse): do not always prefix the table name with database, b…
Browse files Browse the repository at this point in the history
…ecause temp tables cannot be assigned a database
  • Loading branch information
cpcloud authored and kszucs committed Aug 13, 2023
1 parent fe7ba24 commit 5f88102
Show file tree
Hide file tree
Showing 22 changed files with 32 additions and 48 deletions.
4 changes: 1 addition & 3 deletions ibis/backends/clickhouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,7 @@ def close(self) -> None:
self.con.close()

def _fully_qualified_name(self, name: str, database: str | None) -> str:
return sg.table(name, db=database or self.current_database or None).sql(
dialect="clickhouse"
)
return sg.table(name, db=database).sql(dialect="clickhouse")

def get_schema(self, table_name: str, database: str | None = None) -> sch.Schema:
"""Return a Schema object for the indicated table and database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM (
SELECT
t0.string_col,
count(*) AS count
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
GROUP BY
t0.string_col
) AS t1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
*
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
WHERE
t0.string_col IN ('foo', 'bar')
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
*
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
WHERE
NOT t0.string_col IN ('foo', 'bar')
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
sum(CASE WHEN isNull(t0.string_col) THEN 1 ELSE 0 END) AS "Sum(Where(IsNull(string_col), 1, 0))"
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
t0.*
FROM ibis_testing.functional_alltypes AS t0
INNER JOIN ibis_testing.functional_alltypes AS t1
FROM functional_alltypes AS t0
INNER JOIN functional_alltypes AS t1
ON t0.id = t1.id
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
*
FROM ibis_testing.functional_alltypes
FROM functional_alltypes
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
*
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
t0.*
FROM ibis_testing.batting AS t0
ANY JOIN ibis_testing.awards_players AS t1
FROM batting AS t0
ANY JOIN awards_players AS t1
ON t0.playerID = t1.awardID
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
t0.*
FROM ibis_testing.batting AS t0
LEFT ANY JOIN ibis_testing.awards_players AS t1
FROM batting AS t0
LEFT ANY JOIN awards_players AS t1
ON t0.playerID = t1.awardID
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
t0.*
FROM ibis_testing.batting AS t0
INNER JOIN ibis_testing.awards_players AS t1
FROM batting AS t0
INNER JOIN awards_players AS t1
ON t0.playerID = t1.awardID
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
t0.*
FROM ibis_testing.batting AS t0
LEFT OUTER JOIN ibis_testing.awards_players AS t1
FROM batting AS t0
LEFT OUTER JOIN awards_players AS t1
ON t0.playerID = t1.awardID
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
t0.*
FROM ibis_testing.batting AS t0
ANY JOIN ibis_testing.awards_players AS t1
FROM batting AS t0
ANY JOIN awards_players AS t1
ON t0.playerID = t1.playerID
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
t0.*
FROM ibis_testing.batting AS t0
LEFT ANY JOIN ibis_testing.awards_players AS t1
FROM batting AS t0
LEFT ANY JOIN awards_players AS t1
ON t0.playerID = t1.playerID
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
t0.*
FROM ibis_testing.batting AS t0
INNER JOIN ibis_testing.awards_players AS t1
FROM batting AS t0
INNER JOIN awards_players AS t1
ON t0.playerID = t1.playerID
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
t0.*
FROM ibis_testing.batting AS t0
LEFT OUTER JOIN ibis_testing.awards_players AS t1
FROM batting AS t0
LEFT OUTER JOIN awards_players AS t1
ON t0.playerID = t1.playerID
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
sum(t0.float_col) AS "Sum(float_col)"
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
WHERE
t0.int_col > 0
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM (
SELECT
t0.string_col,
sum(t0.float_col) AS total
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
WHERE
t0.int_col > 0
GROUP BY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ SELECT
toHour(t0.timestamp_col) AS hour,
toMinute(t0.timestamp_col) AS minute,
toSecond(t0.timestamp_col) AS second
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SELECT
*
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
WHERE
t0.float_col > 0 AND t0.int_col < (
t0.float_col * 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
*
FROM ibis_testing.functional_alltypes AS t0
FROM functional_alltypes AS t0
WHERE
t0.int_col > 0 AND t0.float_col BETWEEN 0 AND 1
18 changes: 2 additions & 16 deletions ibis/backends/clickhouse/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import ibis.expr.datatypes as dt
import ibis.expr.types as ir
from ibis import config
from ibis.common.exceptions import IbisError
from ibis.util import gen_name

cc = pytest.importorskip("clickhouse_connect")
Expand Down Expand Up @@ -72,7 +71,7 @@ def logger(x):
with config.option_context("verbose_log", logger):
con.table("functional_alltypes")

expected = "DESCRIBE ibis_testing.functional_alltypes"
expected = "DESCRIBE functional_alltypes"

# might be other queries in there, we only check that a describe table
# query was logged
Expand Down Expand Up @@ -198,20 +197,7 @@ def test_list_tables_empty_database(con, temp_db):
assert not con.list_tables(database=temp_db)


@pytest.mark.parametrize(
"temp",
[
param(
True,
marks=pytest.mark.xfail(
reason="Ibis is likely making incorrect assumptions about object lifetime and cursors",
raises=IbisError,
),
),
False,
],
ids=["temp", "no_temp"],
)
@pytest.mark.parametrize("temp", [True, False], ids=["temp", "no_temp"])
def test_create_table_no_data(con, temp, temp_table):
schema = ibis.schema(dict(a="!int", b="string"))
t = con.create_table(temp_table, schema=schema, temp=temp, engine="Memory")
Expand Down

0 comments on commit 5f88102

Please sign in to comment.