fix(sql): handle backtick-quoted identifiers with base dialect (#36545)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Evan Rusackas
2025-12-15 13:00:13 -08:00
committed by GitHub
parent d6d8e71b71
commit c7a4d4f2cc
2 changed files with 110 additions and 0 deletions

View File

@@ -556,11 +556,28 @@ class SQLStatement(BaseSQLStatement[exp.Expression]):
def _parse(cls, script: str, engine: str) -> list[exp.Expression]:
"""
Parse helper.
When the base dialect (engine="base" or unknown engines) fails to parse SQL
containing backtick-quoted identifiers, we fall back to MySQL dialect which
supports backticks natively. This handles cases like "Other" database type
where users may have MySQL-compatible syntax with backtick-quoted table names.
"""
dialect = SQLGLOT_DIALECTS.get(engine)
try:
statements = sqlglot.parse(script, dialect=dialect)
except sqlglot.errors.ParseError as ex:
# If parsing fails with base dialect (or no dialect for unknown engines)
# and the script contains backticks, retry with MySQL dialect which
# supports backtick-quoted identifiers
if (dialect is None or dialect == Dialects.DIALECT) and "`" in script:
try:
statements = sqlglot.parse(script, dialect=Dialects.MYSQL)
except sqlglot.errors.ParseError:
# If MySQL dialect also fails, raise the original error
pass
else:
return statements
kwargs = (
{
"highlight": ex.errors[0]["highlight"],