Skip to content

Commit

Permalink
fix(clickhouse): use backwards compatible methods of getting query me…
Browse files Browse the repository at this point in the history
…tadata
  • Loading branch information
cpcloud authored and gforsyth committed Dec 6, 2023
1 parent a7fd32b commit 975556f
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions ibis/backends/clickhouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ast
import atexit
import glob
import json
from contextlib import closing, suppress
from functools import partial
from typing import TYPE_CHECKING, Any, Literal
Expand Down Expand Up @@ -516,16 +515,16 @@ def get_schema(self, table_name: str, database: str | None = None) -> sch.Schema
return sch.Schema(dict(zip(names, map(ClickhouseType.from_string, types))))

def _get_schema_using_query(self, query: str) -> sch.Schema:
query = f"EXPLAIN json = 1, description = 0, header = 1 {query}"
with closing(self.raw_sql(query)) as results:
[[raw_plans]] = results.result_columns
[plan] = json.loads(raw_plans)
return sch.Schema(
{
field["Name"]: ClickhouseType.from_string(field["Type"])
for field in plan["Plan"]["Header"]
}
)
name = util.gen_name("get_schema_using_query")
with closing(self.raw_sql(f"CREATE VIEW {name} AS {query}")):
pass
try:
with closing(self.raw_sql(f"DESCRIBE {name}")) as results:
names, types, *_ = results.result_columns
finally:
with closing(self.raw_sql(f"DROP VIEW {name}")):
pass
return sch.Schema(dict(zip(names, map(ClickhouseType.from_string, types))))

@classmethod
def has_operation(cls, operation: type[ops.Value]) -> bool:
Expand Down

0 comments on commit 975556f

Please sign in to comment.