Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function createEscapedStatements(nodes: Array<string | SyntaxTreeNode>) {
const SQL_STATEMENT_ESCAPERS = new Map([
[
SyntaxTreeNodeType.SQL_COMMENT,
(str: string) => str.replace(/`/g, "\\`").replace(/\${/g, "\\${"),
(str: string) => str.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\${/g, "\\${"),
],
[
SyntaxTreeNodeType.SQL_LITERAL_STRING,
Expand Down
19 changes: 19 additions & 0 deletions core/main_sqlx_chars_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,23 @@ quotes
expect(result.compile.compiledGraph.tables[0].query.trim()).equals(sqlContents);
expect(result.compile.compiledGraph.tables[0].postOps[0].trim()).equals(sqlContents);
});

test("backticks and dollar signs are escaped in sql comments", () => {
const projectDir = tmpDirFixture.createNewTmpDir();
writeWorkflowSettingsFile(projectDir, WorkflowSettingsTemplates.bigquery);
const sqlContents = `-- Evaluated interpolation inside comment: \${1 + 1}
-- Evaluated interpolation inside comment with backslashes: \\\${1+1}
-- Comment with backticks \`
-- Comment with backslash and backtick \\\`
SELECT 1`;
writeDefinitionFile(
projectDir,
"file.sqlx",
`config { type: "table" }` + sqlContents + `pre_operations { ${sqlContents} }`,
);
const result = runMainInVm(coreExecutionRequestFromPath(projectDir));
expect(result.compile.compiledGraph.graphErrors.compilationErrors).deep.equals([]);
expect(result.compile.compiledGraph.tables[0].query.trim()).equals(sqlContents);
expect(result.compile.compiledGraph.tables[0].preOps[0].trim()).equals(sqlContents);
});
});
6 changes: 3 additions & 3 deletions tests/api/projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ suite("examples", () => {
databaseWithSuffix("tada-analytics"),
schemaWithSuffix("df_integration_test"),
"sample_data",
)}\`\n\n-- here \${"is"} a \`comment\n\n/* \${"another"} \` backtick \` containing \`\`\`comment */`,
)}\`\n\n-- here \${"is"} a \`comment\n\n/* \${"another"} \` backtick \` containing \`\`\`comment */\n\n-- another comment \\\${"with"} \\\` with backslashes`,
);
expect(exampleTable.dependencyTargets).eql([
dataform.Target.create({
Expand Down Expand Up @@ -685,15 +685,15 @@ suite("examples", () => {
test("testcases", () => {
const testCase = graph.tests.find((t) => t.name === "example_test_case");
expect(testCase.testQuery.trim()).equals(
"select * from (\n select 'hi' as faked union all\n select 'ben' as faked union all\n select 'sup?' as faked\n)\n\n-- here ${\"is\"} a `comment\n\n/* ${\"another\"} ` backtick ` containing ```comment */",
"select * from (\n select 'hi' as faked union all\n select 'ben' as faked union all\n select 'sup?' as faked\n)\n\n-- here ${\"is\"} a `comment\n\n/* ${\"another\"} ` backtick ` containing ```comment */\n\n-- another comment \\${\"with\"} \\` with backslashes",
);
expect(testCase.expectedOutputQuery.trim()).equals(
"select 'hi' as faked union all\nselect 'ben' as faked union all\nselect 'sup?' as faked",
);

const testCaseFQ = graph.tests.find((t) => t.name === "example_test_case_fq_ref");
expect(testCaseFQ.testQuery.trim()).equals(
"select * from (\n select 'hi' as faked union all\n select 'ben' as faked union all\n select 'sup?' as faked\n)\n\n-- here ${\"is\"} a `comment\n\n/* ${\"another\"} ` backtick ` containing ```comment */",
"select * from (\n select 'hi' as faked union all\n select 'ben' as faked union all\n select 'sup?' as faked\n)\n\n-- here ${\"is\"} a `comment\n\n/* ${\"another\"} ` backtick ` containing ```comment */\n\n-- another comment \\${\"with\"} \\` with backslashes",
);
expect(testCaseFQ.expectedOutputQuery.trim()).equals(
"select 'hi' as faked union all\nselect 'ben' as faked union all\nselect 'sup?' as faked",
Expand Down
2 changes: 2 additions & 0 deletions tests/api/projects/common_v2/definitions/example_table.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ select * from ${ref("df_integration_test", "sample_data")}

/* ${"another"} ` backtick ` containing ```comment */

-- another comment \${"with"} \` with backslashes

post_operations {
GRANT SELECT ON ${self()} TO GROUP "${constants.allUsersEmailAddress}"
---
Expand Down
Loading