Skip to content

fix(mysql): Fix AttributeError crash when transpiling IGNORE/RESPECT NULLS#7361

Merged
geooo109 merged 3 commits intotobymao:mainfrom
ShubhamKapoor992:mysql-crash-ignore-nulls
Mar 24, 2026
Merged

fix(mysql): Fix AttributeError crash when transpiling IGNORE/RESPECT NULLS#7361
geooo109 merged 3 commits intotobymao:mainfrom
ShubhamKapoor992:mysql-crash-ignore-nulls

Conversation

@ShubhamKapoor992
Copy link
Contributor

@ShubhamKapoor992 ShubhamKapoor992 commented Mar 24, 2026

Fixes #7359

Description

Fixes a critical bug where MySQL dialect crashes with AttributeError when transpiling SQL containing IGNORE NULLS or RESPECT NULLS clauses in window functions.

Problem

When transpiling window functions with IGNORE NULLS or RESPECT NULLS from other dialects (e.g., Snowflake, DuckDB) to MySQL, the generator crashes with:

AttributeError: 'IgnoreNulls' object has no attribute 'sql_name'
AttributeError: 'RespectNulls' object has no attribute 'sql_name'

Root Cause

  • MySQL has NULL_ORDERING_SUPPORTED = False, which triggers NULL ordering simulation logic
  • The code in generator.py:ordered_sql() retrieves window.this to check window function properties
  • However, window.this can be an IgnoreNulls or RespectNulls wrapper object (not the actual window function)
  • The code attempts to call .sql_name() on these wrapper objects, which don't have that method

Example

python
import sqlglot

# This crashes before the fix
sql = "SELECT FIRST_VALUE(col1) IGNORE NULLS OVER (ORDER BY col2) FROM table1"
result = sqlglot.transpile(sql, read='snowflake', write='mysql')[0]
# AttributeError: 'IgnoreNulls' object has no attribute 'sql_name'

Solution

Added logic to unwrap IgnoreNulls and RespectNulls wrapper objects before accessing window function properties.

Changes

File: sqlglot/generator.py (lines 2857-2862)

if isinstance(window, exp.Window):
    window_this = window.this
    # Unwrap IgnoreNulls/RespectNulls wrappers to get the actual window function
    while isinstance(window_this, (exp.IgnoreNulls, exp.RespectNulls)):
        window_this = window_this.this
    spec = window.args.get("spec")

Testing

✅ All 1,059 existing tests pass (17,694 subtests)
✅ Tested across 9 database vendors (MySQL, Oracle, PostgreSQL, Snowflake, BigQuery, Redshift, Spark, Hive, DuckDB)
✅ No regressions introduced
✅ MySQL now successfully transpiles IGNORE/RESPECT NULLS without crashing

@geooo109
Copy link
Collaborator

Hello @ShubhamKapoor992 , thanks for the PR.
Let's also include tests for the fix.

Repository owner deleted a comment from ShubhamKapoor992 Mar 24, 2026
Copy link
Collaborator

@geooo109 geooo109 left a comment

Choose a reason for hiding this comment

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

@ShubhamKapoor992 deleted the previous comments to make more clear what we should do at once, thanks.

Copy link
Collaborator

@geooo109 geooo109 left a comment

Choose a reason for hiding this comment

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

Nice work!

@ShubhamKapoor992
Copy link
Contributor Author

Nice work!

@geooo109 Thank you. Any idea why the PR test fails

home/runner/work/sqlglot/sqlglot/sqlglot/generators/bigquery.py: error: Duplicate module named "sqlglot.generators.bigquery" (also at "/home/runner/work/sqlglot/sqlglot/sqlglot/generators/bigquery.py")

@geooo109
Copy link
Collaborator

@ShubhamKapoor992 nothing to worry about, I'm waiting for this #7381 and after I will merge.

@ShubhamKapoor992
Copy link
Contributor Author

@ShubhamKapoor992 nothing to worry about, I'm waiting for this #7381 and after I will merge.

Thank you.

@geooo109 geooo109 merged commit 0624bbf into tobymao:main Mar 24, 2026
2 of 14 checks passed
@ShubhamKapoor992 ShubhamKapoor992 deleted the mysql-crash-ignore-nulls branch March 25, 2026 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MySQL Crashes with IGNORE/RESPECT NULLS

2 participants