feat(ag-grid): add SQLGlot-based SQL escaping for where and having filter clauses (#36136)

This commit is contained in:
amaannawab923
2025-11-25 13:05:40 +05:30
committed by GitHub
parent ab8352ee66
commit 186693b840
6 changed files with 402 additions and 6 deletions

View File

@@ -1503,3 +1503,31 @@ def sanitize_clause(clause: str, engine: str) -> str:
)
except SupersetParseError as ex:
raise QueryClauseValidationException(f"Invalid SQL clause: {clause}") from ex
def transpile_to_dialect(sql: str, target_engine: str) -> str:
"""
Transpile SQL from "generic SQL" to the target database dialect using SQLGlot.
If the target engine is not in SQLGLOT_DIALECTS, returns the SQL as-is.
"""
target_dialect = SQLGLOT_DIALECTS.get(target_engine)
# If no dialect mapping exists, return as-is
if target_dialect is None:
return sql
try:
parsed = sqlglot.parse_one(sql, dialect=Dialect)
return Dialect.get_or_raise(target_dialect).generate(
parsed,
copy=True,
comments=False,
pretty=False,
)
except ParseError as ex:
raise QueryClauseValidationException(f"Cannot parse SQL clause: {sql}") from ex
except Exception as ex:
raise QueryClauseValidationException(
f"Cannot transpile SQL to {target_engine}: {sql}"
) from ex