Skip to content

Commit f1c0608

Browse files
jcristcpcloud
authored andcommitted
feat: add way to specify sqlglot dialect on backend
1 parent 6861347 commit f1c0608

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

ibis/backends/clickhouse/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
class Backend(BaseSQLBackend):
3131
name = 'clickhouse'
32+
# for now map clickhouse to mysql so that _something_ works
33+
_sqlglot_dialect = "mysql"
3234
table_expr_class = ClickhouseTable
3335
compiler = ClickhouseCompiler
3436

ibis/backends/impala/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ def _column_batches_to_dataframe(names, batches):
167167

168168
class Backend(BaseSQLBackend):
169169
name = 'impala'
170+
# not 100% accurate, but very close
171+
_sqlglot_dialect = "hive"
170172
database_class = ImpalaDatabase
171173
table_expr_class = ImpalaTable
172174
compiler = ImpalaCompiler

ibis/common/pretty.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
import ibis.common.exceptions as com
77
import ibis.expr.types as ir
88

9-
_IBIS_TO_SQLGLOT_NAME_MAP = {
10-
# not 100% accurate, but very close
11-
"impala": "hive",
12-
# for now map clickhouse to Hive so that _something_ works
13-
"clickhouse": "mysql",
14-
}
15-
169

1710
def show_sql(
1811
expr: ir.Expr,
@@ -77,30 +70,25 @@ def to_sql(expr: ir.Expr, dialect: str | None = None) -> str:
7770
except com.IbisError:
7871
# default to duckdb for sqlalchemy compilation because it supports
7972
# the widest array of ibis features for SQL backends
73+
backend = ibis.duckdb
8074
read = "duckdb"
8175
write = ibis.options.sql.default_dialect
8276
else:
83-
read = write = backend.name
77+
read = write = getattr(backend, "_sqlglot_dialect", backend.name)
8478
else:
85-
read = write = dialect
86-
87-
write = _IBIS_TO_SQLGLOT_NAME_MAP.get(write, write)
79+
try:
80+
backend = getattr(ibis, dialect)
81+
except AttributeError:
82+
raise ValueError(f"Unknown dialect {dialect}")
83+
else:
84+
read = write = getattr(backend, "_sqlglot_dialect", dialect)
8885

89-
try:
90-
compiled = expr.compile()
91-
except com.IbisError:
92-
backend = getattr(ibis, read)
93-
compiled = backend.compile(expr)
86+
compiled = backend.compile(expr)
9487
try:
9588
sql = str(compiled.compile(compile_kwargs={"literal_binds": True}))
9689
except (AttributeError, TypeError):
9790
sql = compiled
9891

9992
assert isinstance(sql, str), f"expected `str`, got `{sql.__class__.__name__}`"
100-
(pretty,) = sqlglot.transpile(
101-
sql,
102-
read=_IBIS_TO_SQLGLOT_NAME_MAP.get(read, read),
103-
write=write,
104-
pretty=True,
105-
)
93+
(pretty,) = sqlglot.transpile(sql, read=read, write=write, pretty=True)
10694
return pretty

0 commit comments

Comments
 (0)