Skip to content

fix: escape backslashes in description comments - #1709

Open
maharanay22 wants to merge 1 commit into
databricks:mainfrom
maharanay22:fix-comment-backslash-escaping
Open

maharanay22 wants to merge 1 commit into
databricks:mainfrom
maharanay22:fix-comment-backslash-escaping

Conversation

@maharanay22

Copy link
Copy Markdown

No existing issue (I searched open and closed issues and PRs).

Description

Databricks reads \ in a string literal as an escape character. Descriptions are written into COMMENT '...' literals with only ' escaped, so a description of ^\d+ is stored as ^d+, C:\tmp picks up a tab, and a description ending in \ turns the closing quote into \', which leaves the literal unterminated and the statement fails to parse. Because the stored comment never matches the YAML, comment diffing also keeps re-applying it.

#1613 fixed this in get_create_sql_comment only. This PR moves that escaping (backslash first, then quote) into a shared escape_comment macro and uses it at every other site that writes a description into a comment literal: databricks__comment_clause, alter_relation_comment_sql, databricks__alter_column_comment, alter_column_comments, get_column_comment_sql (views) and the seed column list. DatabricksColumn.render_for_create gets the same escaping. Output for descriptions without backslashes is unchanged, and get_create_sql_comment renders exactly as before.

Unit tests assert the exact SQL for a \d regex, a Windows path, a trailing backslash, and a description with both \ and ' at each site. 13 of the new tests fail on main with the wrong or unterminated literal; all pass with this change. I also rendered the macros through dbt run-operation (dbt 1.12.5): Bob\'s ^\d in C:\tmp, trailing \ now renders as IS 'Bob\\'s ^\d in C:\tmp, trailing \' instead of IS 'Bob\'s ^\d in C:\tmp, trailing \'.

Checklist

  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • I have updated the CHANGELOG.md and added information about my change to the "dbt-databricks next" section.
  • [Optional] I have run the dbt-databricks-pr-ready project skill for this PR and addressed its merge-readiness feedback

Databricks reads a backslash in a string literal as an escape, so a
description like `^\d+` was stored as `^d+`, `C:\tmp` picked up a tab,
and a trailing backslash escaped the closing quote and broke the
statement. Only `get_create_sql_comment` escaped backslashes.

Move its escaping into a shared `escape_comment` macro, use it at every
site that writes a description into a COMMENT literal (relation comment
clause, COMMENT ON TABLE/VIEW/COLUMN, view column list, seed column
list), and apply the same escaping in `DatabricksColumn.render_for_create`.

Signed-off-by: Maha Rana Yadavalli <271375718+maharanay22@users.noreply.github.com>
@maharanay22
maharanay22 force-pushed the fix-comment-backslash-escaping branch from 50e700a to 1c14e9f Compare October 8, 2026 22:35

This branch has not been deployed

No deployments
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.

1 participant