Skip to content

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

Merged
rusackas merged 1 commit into
masterfrom
fix-31853-backtick-parsing
Dec 15, 2025
Merged

fix(sql): handle backtick-quoted identifiers with base dialect#36545
rusackas merged 1 commit into
masterfrom
fix-31853-backtick-parsing

Conversation

@rusackas
Copy link
Copy Markdown
Member

SUMMARY

When using the "Other" database type (or unknown engines), SQL parsing would fail if the query contained backtick-quoted identifiers like SELECT * FROM database.6``.

Root Cause: The base sqlglot dialect (Dialects.DIALECT) doesn't support backtick-quoted identifiers, but some databases (particularly MySQL-compatible ones like the sqlalchemy-mongobi connector mentioned in the issue) use backticks for quoting table/column names.

Solution: This change adds a fallback mechanism in the _parse method: when parsing fails with the base dialect (or no dialect for unknown engines) and the SQL contains backticks, we retry parsing with the MySQL dialect which supports backticks natively.

The fallback only triggers when:

  1. The initial parse fails
  2. The dialect is the base dialect OR None (unknown engine)
  3. The SQL contains backtick characters

If the MySQL dialect also fails to parse the SQL, the original error is raised to maintain proper error messaging.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Before: Queries with backtick-quoted identifiers fail with:

sqlglot.errors.ParseError: Invalid expression / Unexpected token. Line 1, Col: 24.
SELECT * FROM database.`6` LIMIT 100

After: Queries parse successfully, allowing datasets to be created and SQL Lab queries to run.

TESTING INSTRUCTIONS

  1. Create a database connection with the "Other" database type
  2. Try to create a dataset using SQL with backtick-quoted table names:
    • SELECT * FROM database.6``
    • SELECT * FROM my_table``
    • SELECT col1, col2FROMschema.table``
  3. Verify the queries parse successfully and datasets can be created
  4. Run the unit tests: pytest tests/unit_tests/sql/parse_tests.py -k "backtick" -v

ADDITIONAL INFORMATION

🤖 Generated with Claude Code

When using the "Other" database type (or unknown engines), SQL parsing
would fail if the query contained backtick-quoted identifiers like
`SELECT * FROM database.`6``.

The base sqlglot dialect doesn't support backticks, but MySQL dialect
does. This change adds a fallback mechanism: when parsing fails with
the base dialect (or no dialect for unknown engines) and the SQL
contains backticks, we retry with MySQL dialect.

Fixes #31853

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@dosubot dosubot Bot added the change:backend Requires changing the backend label Dec 11, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes SQL parsing failures for backtick-quoted identifiers when using the "Other" database type or engines with unknown dialects. The fix implements a fallback mechanism that retries parsing with MySQL dialect when the base dialect fails and backticks are detected in the SQL.

Key changes:

  • Added backtick fallback logic in SQLStatement._parse() method
  • Comprehensive test coverage for various backtick-quoted identifier scenarios
  • Maintains backward compatibility with normal SQL parsing

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
superset/sql/parse.py Implements MySQL dialect fallback for backtick-quoted identifiers in base dialect parsing
tests/unit_tests/sql/parse_tests.py Adds comprehensive test cases covering backtick-quoted identifiers with various SQL patterns

Copy link
Copy Markdown
Member

@Antonio-RiveroMartnez Antonio-RiveroMartnez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@rusackas rusackas merged commit c7a4d4f into master Dec 15, 2025
78 of 80 checks passed
@rusackas rusackas deleted the fix-31853-backtick-parsing branch December 15, 2025 21:00
sadpandajoe pushed a commit that referenced this pull request Dec 16, 2025
Co-authored-by: Claude <noreply@anthropic.com>
(cherry picked from commit c7a4d4f)
@sadpandajoe sadpandajoe added the v6.0 Label added by the release manager to track PRs to be included in the 6.0 branch label Dec 16, 2025
betodealmeida pushed a commit that referenced this pull request Dec 19, 2025
isaac-jaynes-imperva pushed a commit to isaac-jaynes-imperva/superset that referenced this pull request Mar 3, 2026
…e#36545)

Co-authored-by: Claude <noreply@anthropic.com>
(cherry picked from commit c7a4d4f)
qfcwell pushed a commit to qfcwell/superset that referenced this pull request May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:backend Requires changing the backend size/L v6.0 Label added by the release manager to track PRs to be included in the 6.0 branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Other database - error on parsing SQL script in Superset 4.1.1

4 participants