Skip to content

fix(cli): end raw literals and line comments where BigQuery does - #2501

Open
dmargol1 wants to merge 2 commits into
dataform-co:mainfrom
dmargol1:fix/raw-literal-comment-stripper
Open

dmargol1 wants to merge 2 commits into
dataform-co:mainfrom
dmargol1:fix/raw-literal-comment-stripper

Conversation

@dmargol1

Copy link
Copy Markdown
Contributor

Found while investigating #2008.

concatenateQueries() can put a statement's semicolon inside its trailing comment when the statement contains a raw literal with an escaped quote. For example, concatenateQueries(["SELECT r'a\\'' AS s -- comment", "SELECT 2"]) returns:

SELECT r'a\'' AS s -- comment;
SELECT 2

BigQuery rejects this with Syntax error: Expected end of input but got keyword SELECT at [2:1]. With this change it returns:

SELECT r'a\'' AS s; -- comment
SELECT 2

which BigQuery accepts.

The cause is stripCommentsAndQuotedContent() in cli/api/utils/query_transforms.ts, which decides where literals and comments end for concatenateQueries(), terminateStatements(), wrapWithCreateSchemaIfNotExists() and setsDatasetId(). It treated a backslash in a raw literal (r'...', rb'...', br'...') as an ordinary character, so r'a\'' ended at \'. In BigQuery the backslash is kept in raw literals, but it still stops the next quote from closing the literal: SELECT r'a\'b' returns a\'b, and SELECT r'\' fails with Unclosed raw string literal.

Two more fixes to the same scanner:

  • -- and # comments now also end at a lone \r, as in BigQuery: in SELECT 1 -- c\rFROM x, FROM x is code.
  • An unterminated literal keeps the stripped string the same length as the input, as the function's documentation says. Before, '''a came out one character shorter. A statement terminator still goes after an unterminated literal, not inside it.

stripCommentsAndQuotedContent() is now exported, so it can be unit-tested directly.

Some tests from #2408 and #2421 used r'\' and similar inputs as raw literals ending in a backslash. BigQuery rejects these as unclosed literals, so I replaced them with valid inputs for the corrected behaviour (e.g. r'\\', r'a\'', rb'\'', r'''a\''''), each checked with a BigQuery dry run.

Testing

  • ./scripts/run_tests passes (84 tests), including new query_transforms_test cases for raw literals with escaped quotes, comments ending at a lone \r, and unterminated literals.
  • ESLint and Prettier pass on the changed files.
  • The old and new stripper, concatenateQueries() and setsDatasetId() give identical output on 161 texts: the repository's .sqlx files and the compiled SQL of its example and integration projects, plus three small test projects. None of these has a raw literal with a backslash before a quote, so this only shows nothing else changed.

🤖 Generated with Claude Code

The comment and literal stripper used by concatenateQueries(),
terminateStatements(), wrapWithCreateSchemaIfNotExists() and
setsDatasetId() ended a raw literal at a backslash-escaped quote. BigQuery
doesn't: in r'a\'' the backslash is kept, but the quote doesn't close the
literal. A statement like "SELECT r'a\'' AS s -- comment" was then
terminated after the comment, so the generated script failed to parse.

Also end line comments at a lone \r, as BigQuery does, keep the
stripped string the same length as the statement when a literal is
unterminated, and export the stripper so it can be unit-tested directly.
@dmargol1
dmargol1 requested a review from a team as a code owner November 26, 2026 06:31
@dmargol1
dmargol1 requested review from apilaskowski and removed request for a team November 26, 2026 06:31
@apilaskowski

Copy link
Copy Markdown
Collaborator

/gcbrun

i += closing.length;
result += closing + " ".repeat(i - start - 2 * closing.length) + closing;
} else {
// An unterminated literal runs to the end of the statement. It's replaced up to there, and

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit (optional): the function's doc comment still says literal contents are replaced with spaces. With this branch an unterminated literal ends with a quote character instead. Now that the function is exported, it would help to say that in the doc comment as well, not only here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in ece27fe. The doc comment now says that an unterminated literal runs to the end of the statement, and that its last character is replaced with its quote character rather than a space, so it still ends in code.

* with `statement`, while leaving only unquoted SQL tokens and empty quotes.
*/
function stripCommentsAndQuotedContent(statement: string): string {
export function stripCommentsAndQuotedContent(statement: string): string {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit (optional): since this is exported only so it can be unit-tested, a short note in the doc comment saying so would discourage other callers from depending on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in ece27fe. I added a note to the doc comment that it's exported only for unit testing.

@apilaskowski

Copy link
Copy Markdown
Collaborator

The following contributors were found for this pull request:

✅ ece27fe Author: @dmargol1 <dml1​@gmail.com>
❌ ece27fe Co-Author: <no
y​@anthropic.com>

(Only the first commit for a unique contributor is listed.)

@dmargol1
dmargol1 force-pushed the fix/raw-literal-comment-stripper branch from ece27fe to f574e26 Compare November 28, 2026 00:47

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.

2 participants