Skip to content

Commit

Permalink
feat: add way to specify sqlglot dialect on backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and cpcloud committed Oct 27, 2022
1 parent 6861347 commit f1c0608
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
2 changes: 2 additions & 0 deletions ibis/backends/clickhouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

class Backend(BaseSQLBackend):
name = 'clickhouse'
# for now map clickhouse to mysql so that _something_ works
_sqlglot_dialect = "mysql"
table_expr_class = ClickhouseTable
compiler = ClickhouseCompiler

Expand Down
2 changes: 2 additions & 0 deletions ibis/backends/impala/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ def _column_batches_to_dataframe(names, batches):

class Backend(BaseSQLBackend):
name = 'impala'
# not 100% accurate, but very close
_sqlglot_dialect = "hive"
database_class = ImpalaDatabase
table_expr_class = ImpalaTable
compiler = ImpalaCompiler
Expand Down
32 changes: 10 additions & 22 deletions ibis/common/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
import ibis.common.exceptions as com
import ibis.expr.types as ir

_IBIS_TO_SQLGLOT_NAME_MAP = {
# not 100% accurate, but very close
"impala": "hive",
# for now map clickhouse to Hive so that _something_ works
"clickhouse": "mysql",
}


def show_sql(
expr: ir.Expr,
Expand Down Expand Up @@ -77,30 +70,25 @@ def to_sql(expr: ir.Expr, dialect: str | None = None) -> str:
except com.IbisError:
# default to duckdb for sqlalchemy compilation because it supports
# the widest array of ibis features for SQL backends
backend = ibis.duckdb
read = "duckdb"
write = ibis.options.sql.default_dialect
else:
read = write = backend.name
read = write = getattr(backend, "_sqlglot_dialect", backend.name)
else:
read = write = dialect

write = _IBIS_TO_SQLGLOT_NAME_MAP.get(write, write)
try:
backend = getattr(ibis, dialect)
except AttributeError:
raise ValueError(f"Unknown dialect {dialect}")
else:
read = write = getattr(backend, "_sqlglot_dialect", dialect)

try:
compiled = expr.compile()
except com.IbisError:
backend = getattr(ibis, read)
compiled = backend.compile(expr)
compiled = backend.compile(expr)
try:
sql = str(compiled.compile(compile_kwargs={"literal_binds": True}))
except (AttributeError, TypeError):
sql = compiled

assert isinstance(sql, str), f"expected `str`, got `{sql.__class__.__name__}`"
(pretty,) = sqlglot.transpile(
sql,
read=_IBIS_TO_SQLGLOT_NAME_MAP.get(read, read),
write=write,
pretty=True,
)
(pretty,) = sqlglot.transpile(sql, read=read, write=write, pretty=True)
return pretty

0 comments on commit f1c0608

Please sign in to comment.