|
6 | 6 | import ibis.common.exceptions as com
|
7 | 7 | import ibis.expr.types as ir
|
8 | 8 |
|
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 |
| - |
16 | 9 |
|
17 | 10 | def show_sql(
|
18 | 11 | expr: ir.Expr,
|
@@ -77,30 +70,25 @@ def to_sql(expr: ir.Expr, dialect: str | None = None) -> str:
|
77 | 70 | except com.IbisError:
|
78 | 71 | # default to duckdb for sqlalchemy compilation because it supports
|
79 | 72 | # the widest array of ibis features for SQL backends
|
| 73 | + backend = ibis.duckdb |
80 | 74 | read = "duckdb"
|
81 | 75 | write = ibis.options.sql.default_dialect
|
82 | 76 | else:
|
83 |
| - read = write = backend.name |
| 77 | + read = write = getattr(backend, "_sqlglot_dialect", backend.name) |
84 | 78 | 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) |
88 | 85 |
|
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) |
94 | 87 | try:
|
95 | 88 | sql = str(compiled.compile(compile_kwargs={"literal_binds": True}))
|
96 | 89 | except (AttributeError, TypeError):
|
97 | 90 | sql = compiled
|
98 | 91 |
|
99 | 92 | 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) |
106 | 94 | return pretty
|
0 commit comments