Repository navigation
Conversation
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.
|
/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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Done in ece27fe. I added a note to the doc comment that it's exported only for unit testing.
ece27fe to
f574e26
Compare
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:BigQuery rejects this with
Syntax error: Expected end of input but got keyword SELECT at [2:1]. With this change it returns:which BigQuery accepts.
The cause is
stripCommentsAndQuotedContent()incli/api/utils/query_transforms.ts, which decides where literals and comments end forconcatenateQueries(),terminateStatements(),wrapWithCreateSchemaIfNotExists()andsetsDatasetId(). It treated a backslash in a raw literal (r'...',rb'...',br'...') as an ordinary character, sor'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'returnsa\'b, andSELECT r'\'fails withUnclosed raw string literal.Two more fixes to the same scanner:
--and#comments now also end at a lone\r, as in BigQuery: inSELECT 1 -- c\rFROM x,FROM xis code.'''acame 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_testspasses (84 tests), including newquery_transforms_testcases for raw literals with escaped quotes, comments ending at a lone\r, and unterminated literals.concatenateQueries()andsetsDatasetId()give identical output on 161 texts: the repository's.sqlxfiles 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