mirror of
https://github.com/apache/superset.git
synced 2026-04-18 07:35:09 +00:00
feat(examples): Transpile virtual dataset SQL on import (#37311)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Beto Dealmeida <roberto@dealmeida.net> Co-authored-by: bito-code-review[bot] <188872107+bito-code-review[bot]@users.noreply.github.com>
This commit is contained in:
@@ -345,3 +345,54 @@ def test_sqlglot_generation_error_raises_exception() -> None:
|
||||
match="Cannot transpile SQL to postgresql",
|
||||
):
|
||||
transpile_to_dialect("name = 'test'", "postgresql")
|
||||
|
||||
|
||||
# Tests for source_engine parameter
|
||||
@pytest.mark.parametrize(
|
||||
("sql", "source_engine", "target_engine", "expected"),
|
||||
[
|
||||
# PostgreSQL to MySQL - should convert :: casting to CAST()
|
||||
(
|
||||
"SELECT created_at::DATE FROM orders",
|
||||
"postgresql",
|
||||
"mysql",
|
||||
"SELECT CAST(created_at AS DATE) FROM orders",
|
||||
),
|
||||
# Same dialect - should preserve SQL
|
||||
(
|
||||
"SELECT * FROM orders",
|
||||
"postgresql",
|
||||
"postgresql",
|
||||
"SELECT * FROM orders",
|
||||
),
|
||||
# PostgreSQL to DuckDB - DuckDB supports similar syntax (uppercases date part)
|
||||
(
|
||||
"SELECT DATE_TRUNC('month', ts) FROM orders",
|
||||
"postgresql",
|
||||
"duckdb",
|
||||
"SELECT DATE_TRUNC('MONTH', ts) FROM orders",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_transpile_with_source_engine(
|
||||
sql: str, source_engine: str, target_engine: str, expected: str
|
||||
) -> None:
|
||||
"""Test transpilation with explicit source engine."""
|
||||
result = transpile_to_dialect(sql, target_engine, source_engine)
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_transpile_source_engine_none_uses_generic() -> None:
|
||||
"""Test that source_engine=None uses generic dialect (backward compatible)."""
|
||||
# Simple SQL that doesn't require dialect-specific parsing
|
||||
result = transpile_to_dialect("SELECT * FROM orders", "postgresql", None)
|
||||
assert result == "SELECT * FROM orders"
|
||||
|
||||
|
||||
def test_transpile_unknown_source_engine_uses_generic() -> None:
|
||||
"""Test that unknown source_engine falls back to generic dialect."""
|
||||
# Unknown engine should be treated as None (generic)
|
||||
result = transpile_to_dialect(
|
||||
"SELECT * FROM orders", "postgresql", "unknown_engine"
|
||||
)
|
||||
assert result == "SELECT * FROM orders"
|
||||
|
||||
Reference in New Issue
Block a user