Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove JS context from SQL files #1672

Merged
merged 6 commits into from Feb 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/actions/assertion.ts
Expand Up @@ -109,7 +109,7 @@ export class Assertion extends ActionBuilder<dataform.Assertion> {
description: config.description
});

this.query(nativeRequire(config.filename).queryAsContextable);
this.query(nativeRequire(config.filename).query);
}

public config(config: IAssertionConfig) {
Expand Down
2 changes: 1 addition & 1 deletion core/actions/operation.ts
Expand Up @@ -108,7 +108,7 @@ export class Operation extends ActionBuilder<dataform.Operation> {
description: config.description
});

this.queries(nativeRequire(config.filename).queryAsContextable);
this.queries(nativeRequire(config.filename).query);
}

public config(config: IOperationConfig) {
Expand Down
2 changes: 1 addition & 1 deletion core/actions/table.ts
Expand Up @@ -425,7 +425,7 @@ export class Table extends ActionBuilder<dataform.Table> {
bigquery: bigqueryOptions
});
}
this.query(nativeRequire(tableTypeConfig.filename).queryAsContextable);
this.query(nativeRequire(tableTypeConfig.filename).query);
if (tableTypeConfig.preOperations) {
this.preOps(tableTypeConfig.preOperations);
}
Expand Down
10 changes: 5 additions & 5 deletions core/compilers.ts
Expand Up @@ -42,11 +42,11 @@ export function compile(code: string, path: string): string {
return `exports.asJson = ${notebookAsJson}`;
}
if (Path.fileExtension(path) === "sql") {
const { sql } = extractSqlxParts(SyntaxTreeNode.create(code));
return `exports.queryAsContextable = (ctx) => {
${CONTEXT_FUNCTIONS}
return \`${sql.join("")}\`;
}`;
const escapedCode = code
.replace(/\\/g, "\\\\")
.replace(/`/g, "\\`")
.replace(/\${/g, "\\${");
return `exports.query = \`${escapedCode}\`;`;
}
return code;
}
Expand Down
1 change: 1 addition & 0 deletions core/main.ts
Expand Up @@ -70,6 +70,7 @@ export function main(coreExecutionRequest: Uint8Array | string): Uint8Array | st
globalAny.operate = session.operate.bind(session);
globalAny.assert = session.assert.bind(session);
globalAny.declare = session.declare.bind(session);
globalAny.notebook = session.notebook.bind(session);
globalAny.test = session.test.bind(session);

loadActionConfigs(session, compileRequest.compileConfig.filePaths);
Expand Down
42 changes: 13 additions & 29 deletions core/main_test.ts
Expand Up @@ -765,10 +765,7 @@ actions:
- operation:
filename: action.sql`
);
fs.writeFileSync(
path.join(projectDir, "definitions/action.sql"),
"SELECT ${database()} AS proofThatContextIsRead"
);
fs.writeFileSync(path.join(projectDir, "definitions/action.sql"), "SELECT 1");

const result = runMainInVm(coreExecutionRequestFromPath(projectDir));

Expand All @@ -780,7 +777,7 @@ actions:
name: "action"
},
fileName: "definitions/action.sql",
queries: ["SELECT dataform AS proofThatContextIsRead"],
queries: ["SELECT 1"],
target: {
database: "dataform",
name: "action"
Expand Down Expand Up @@ -878,10 +875,7 @@ actions:
- table:
filename: action.sql`
);
fs.writeFileSync(
path.join(projectDir, "definitions/action.sql"),
"SELECT ${database()} AS proofThatContextIsRead"
);
fs.writeFileSync(path.join(projectDir, "definitions/action.sql"), "SELECT 1");

const result = runMainInVm(coreExecutionRequestFromPath(projectDir));

Expand All @@ -893,7 +887,7 @@ actions:
name: "action"
},
fileName: "definitions/action.sql",
query: "SELECT dataform AS proofThatContextIsRead",
query: "SELECT 1",
target: {
database: "dataform",
name: "action"
Expand Down Expand Up @@ -924,11 +918,7 @@ actions:
- someKey1
- someKey2`
);
fs.writeFileSync(
path.join(projectDir, "definitions/action.sql"),
"SELECT ${database()} AS ${when(incremental(), `proofThatIncrementalContextIsRead`, " +
"`proofThatContextIsRead`) }"
);
fs.writeFileSync(path.join(projectDir, "definitions/action.sql"), "SELECT 1");

const result = runMainInVm(coreExecutionRequestFromPath(projectDir));

Expand All @@ -940,8 +930,8 @@ actions:
name: "action"
},
fileName: "definitions/action.sql",
query: "SELECT dataform AS proofThatContextIsRead",
incrementalQuery: "SELECT dataform AS proofThatIncrementalContextIsRead",
query: "SELECT 1",
incrementalQuery: "SELECT 1",
target: {
database: "dataform",
name: "action"
Expand Down Expand Up @@ -970,10 +960,7 @@ actions:
- view:
filename: action.sql`
);
fs.writeFileSync(
path.join(projectDir, "definitions/action.sql"),
"SELECT ${database()} AS proofThatContextIsRead"
);
fs.writeFileSync(path.join(projectDir, "definitions/action.sql"), "SELECT 1");

const result = runMainInVm(coreExecutionRequestFromPath(projectDir));

Expand All @@ -985,7 +972,7 @@ actions:
name: "action"
},
fileName: "definitions/action.sql",
query: "SELECT dataform AS proofThatContextIsRead",
query: "SELECT 1",
target: {
database: "dataform",
name: "action"
Expand All @@ -1012,10 +999,7 @@ actions:
- assertion:
filename: action.sql`
);
fs.writeFileSync(
path.join(projectDir, "definitions/action.sql"),
"SELECT ${database()} AS proofThatContextIsRead"
);
fs.writeFileSync(path.join(projectDir, "definitions/action.sql"), "SELECT 1");

const result = runMainInVm(coreExecutionRequestFromPath(projectDir));

Expand All @@ -1027,7 +1011,7 @@ actions:
name: "action"
},
fileName: "definitions/action.sql",
query: "SELECT dataform AS proofThatContextIsRead",
query: "SELECT 1",
target: {
database: "dataform",
name: "action"
Expand Down Expand Up @@ -1094,7 +1078,7 @@ actions:
);
fs.writeFileSync(
path.join(projectDir, "definitions/utf8characters:私🙂 and some spaces.sql"),
"SELECT ${database()} AS proofThatContextIsRead"
"SELECT 1"
);

const result = runMainInVm(coreExecutionRequestFromPath(projectDir));
Expand All @@ -1107,7 +1091,7 @@ actions:
name: "utf8characters:私🙂 and some spaces"
},
fileName: "definitions/utf8characters:私🙂 and some spaces.sql",
queries: ["SELECT dataform AS proofThatContextIsRead"],
queries: ["SELECT 1"],
target: {
database: "dataform",
name: "utf8characters:私🙂 and some spaces"
Expand Down
4 changes: 2 additions & 2 deletions examples/examples_test.ts
Expand Up @@ -10,15 +10,15 @@ suite("examples", () => {

const graph = await compile({ projectDir: "examples/stackoverflow_reporter" });

expect(graph.graphErrors.compilationErrors.length).equals(0);
expect(graph.graphErrors.compilationErrors).deep.equals([]);
});

test("extreme_weather_programming compiles", async () => {
fs.copySync("examples/node_modules", "examples/extreme_weather_programming/node_modules");

const graph = await compile({ projectDir: "examples/extreme_weather_programming" });

expect(graph.graphErrors.compilationErrors.length).equals(0);
expect(graph.graphErrors.compilationErrors).deep.equals([]);
expect(graph.tables.length).equals(3);
expect(graph.notebooks.length).equals(1);
});
Expand Down
5 changes: 5 additions & 0 deletions examples/extreme_weather_programming/definitions/actions.yaml
@@ -1,9 +1,14 @@
actions:
- view:
filename: repositories_created_during_extreme_weather.sql
dependencyTargets:
- name: was_there_extreme_weather
- name: repositories_that_mention_extreme_weather
- view:
filename: repositories_that_mention_extreme_weather.sql
- view:
filename: was_there_extreme_weather.sql
- notebook:
filename: snowy_repository_creation.ipynb
dependencyTargets:
- name: repositories_created_during_extreme_weather
@@ -1,7 +1,7 @@
SELECT
*
FROM
${ ref("was_there_extreme_weather") }
LEFT OUTER JOIN ${ ref("repositories_that_mention_extreme_weather") } USING (date)
`dataform-demos.was_there_extreme_weather`
LEFT OUTER JOIN `dataform-demos.repositories_that_mention_extreme_weather` USING (date)
ORDER BY
date
Expand Up @@ -6,7 +6,7 @@ FROM
WHERE
REGEXP_CONTAINS(
repository_description,
"${constants.WEATHER.join(" | ")}"
"snow | hail | thunder | tornado"
)
GROUP BY
date
Expand Down
Expand Up @@ -5,7 +5,10 @@ FROM
`bigquery-public-data.samples.gsod`
WHERE
(
${ constants.WEATHER.map(weather => `${weather} = TRUE`).join(" OR\n ") }
snow = TRUE
OR hail = TRUE
OR thunder = TRUE
OR tornado = TRUE
) -- The GitHub data only ranges from 2007 to 2012.
-- The weather data is only available up to 2010.
AND year >= 2007
Expand Down
3 changes: 0 additions & 3 deletions examples/extreme_weather_programming/includes/constants.js

This file was deleted.

3 changes: 2 additions & 1 deletion examples/extreme_weather_programming/workflow_settings.yaml
@@ -1,2 +1,3 @@
defaultDataset: "dataform-demos"
defaultProject: "dataform-demos"
defaultLocation: "us"
defaultDataset: "dataform"
4 changes: 4 additions & 0 deletions protos/configs.proto
Expand Up @@ -243,6 +243,10 @@ message ActionConfig {

// is specified, if a row arrives whose key matches an existing row's key,
// then the existing row is overwritten with the new data.

// If set, unique key represents a set of names of columns that will act as
// a the unique key. To enforce this, when updating the incremental
// table, Dataform merges rows with `uniqueKey` instead of appending them.
repeated string unique_key = 11;

// Description of the incremental table.
Expand Down
2 changes: 1 addition & 1 deletion version.bzl
@@ -1,3 +1,3 @@
# NOTE: If you change the format of this line, you must change the bash command
# in /scripts/publish to extract the version string correctly.
DF_VERSION = "3.0.0-alpha.4"
DF_VERSION = "3.0.0-alpha.5"