From 975556fbb58597e558c04c520c6268b0c0b4a812 Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Wed, 6 Dec 2023 08:14:06 -0500 Subject: [PATCH] fix(clickhouse): use backwards compatible methods of getting query metadata --- ibis/backends/clickhouse/__init__.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/ibis/backends/clickhouse/__init__.py b/ibis/backends/clickhouse/__init__.py index ed19a1c7ce6e..88fd3dd24c84 100644 --- a/ibis/backends/clickhouse/__init__.py +++ b/ibis/backends/clickhouse/__init__.py @@ -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 @@ -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: