fix(athena): properly escape identifiers in Iceberg SQL generation - #3423
Merged
Conversation
…ia Glue column names
…ntifier for symmetry with DDL helper
EthanBunce
approved these changes
Jul 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Column and table identifiers are interpolated into the SQL that the Iceberg
write path generates (
awswrangler/athena/_write_iceberg.py) without escaping.Names containing the quoting character aren't quoted correctly, which produces
malformed SQL or unexpected behavior for identifiers that contain special
characters.
This can surface without any unusual caller input. During automatic schema
reconciliation (
to_iceberg(..., fill_missing_columns_in_df=True), thedefault), column names are read back from the AWS Glue Data Catalog and then
interpolated into the generated
MERGE/INSERT. Glue permits identifiers thatthe generation didn't handle safely, so this hardens the path against names
sourced from the catalog rather than only from the DataFrame.
Changes
Athena uses two identifier-quoting families depending on the statement:
Hive-style backticks for DDL (
CREATE TABLE,ALTER TABLE) and Trino-styledouble quotes for DML (
SELECT/INSERT/MERGE/DELETE). This PR adds ahelper for each and applies it consistently:
_escape_athena_identifier()— doubles embedded", for DML identifiers._escape_athena_ddl_identifier()— doubles embedded`, for DDL identifiers.Applied to every identifier splice in the module:
_merge_iceberg(MERGE/INSERT),delete_from_iceberg_table, and themode="overwrite"DELETE FROM._create_iceberg_table(table + column names),_alter_iceberg_table_add_columns_sql,_alter_iceberg_table_change_columns_sql._build_order_by_clausecolumn identifiers.LOCATION '<path>'splice through the existing string-literal escaper.Left unchanged on purpose:
partition_colsare partition transformexpressions (e.g.
day(ts),truncate(10, col)), not plain identifiers, sothey are spliced verbatim rather than quoted.
Testing
contain the relevant quote character.
ruff format --check,ruff check, andmypyclean on the changed files.