Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions core/actions/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ export class AssertionContext implements IActionContext {
return this.resolve(ref);
}

public param(paramName: string): string {
return ""
}

public resolve(ref: Resolvable | string[], ...rest: string[]) {
return this.assertion.session.resolve(ref, ...rest);
}
Expand Down
4 changes: 4 additions & 0 deletions core/actions/data_preparation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ export class DataPreparationContext implements ITableContext {
return this.resolve(ref);
}

public param(paramName: string): string {
return ""
}

public resolve(ref: Resolvable | string[], ...rest: string[]) {
return this.dataPreparation.session.resolve(ref, ...rest);
}
Expand Down
4 changes: 4 additions & 0 deletions core/actions/incremental_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ export class IncrementalTableContext implements ITableContext {
return this.incrementalTable.session.finalizeName(this.incrementalTable.getTarget().name);
}

public param(paramName: string): string {
return ""
}

public ref(ref: Resolvable | string[], ...rest: string[]): string {
ref = toResolvable(ref, rest);
if (!resolvableAsTarget(ref)) {
Expand Down
4 changes: 4 additions & 0 deletions core/actions/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ export class OperationContext implements IActionContext {
return this.resolve(ref);
}

public param(paramName: string): string {
return ""
}

public resolve(ref: Resolvable | string[], ...rest: string[]) {
return this.operation.session.resolve(ref, ...rest);
}
Expand Down
7 changes: 6 additions & 1 deletion core/actions/table.ts
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should we also add 'inputParams: [],' in proto in core/actions/incremental_table.ts?

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.

Yes, it would be better to define a default as empty array on every action.

Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export class Table extends ActionBuilder<dataform.Table> {
type: "table",
enumType: dataform.TableType.TABLE,
disabled: false,
tags: []
tags: [],
inputParams: [],
});

/** @hidden */
Expand Down Expand Up @@ -611,6 +612,10 @@ export class TableContext implements ITableContext {
return this.table.session.finalizeName(this.table.getTarget().name);
}

public param(paramName: string): string {
return ""
}

public ref(ref: Resolvable | string[], ...rest: string[]): string {
ref = toResolvable(ref, rest);
if (!resolvableAsTarget(ref)) {
Expand Down
6 changes: 5 additions & 1 deletion core/actions/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ class RefReplacingContext implements ITableContext {
public ref(ref: Resolvable | string[], ...rest: string[]) {
return this.resolve(ref, ...rest);
}


public param(paramName: string): string {
return ""
}

public resolve(ref: Resolvable | string[], ...rest: string[]) {
const target = resolvableAsTarget(toResolvable(ref, rest));
if (!this.testContext.test.contextableInputs.has(targetStringifier.stringify(target))) {
Expand Down
14 changes: 13 additions & 1 deletion core/actions/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export class View extends ActionBuilder<dataform.Table> {
type: "view",
enumType: dataform.TableType.VIEW,
disabled: false,
tags: []
tags: [],
inputParams: [],
});

/** @hidden */
Expand Down Expand Up @@ -486,6 +487,12 @@ export class View extends ActionBuilder<dataform.Table> {
return dataform.Target.create(this.proto.target);
}

public addInputParam(paramName: string) {
if (!this.proto.inputParams.includes(paramName)) {
this.proto.inputParams.push(paramName);
}
}

/** @hidden */
public compile() {
const context = new ViewContext(this);
Expand Down Expand Up @@ -650,6 +657,11 @@ export class ViewContext implements ITableContext {
return this.resolve(ref);
}

public param(paramName: string): string {
this.view.addInputParam(paramName);
return `\{${paramName}\}`;
}

public resolve(ref: Resolvable | string[], ...rest: string[]) {
return this.view.session.resolve(ref, ...rest);
}
Expand Down
3 changes: 2 additions & 1 deletion core/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const CONTEXT_FUNCTIONS = [
"when",
"incremental",
"schema",
"database"
"database",
"param",
]
.map(name => `const ${name} = ctx.${name} ? ctx.${name}.bind(ctx) : undefined;`)
.join("\n");
Expand Down
2 changes: 2 additions & 0 deletions core/contextables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export interface IActionContext {
* Returns the database of this dataset, if applicable.
*/
database: () => string;

param: (paramName: string) => string;
}

/**
Expand Down
40 changes: 40 additions & 0 deletions core/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,46 @@ suite("@dataform/core", ({ afterEach }) => {
const tmpDirFixture = new TmpDirFixture(afterEach);

suite("session", () => {
test("param resolved correctly", () => {
const projectDir = tmpDirFixture.createNewTmpDir();
fs.writeFileSync(
path.join(projectDir, "workflow_settings.yaml"),
VALID_WORKFLOW_SETTINGS_YAML
);
fs.mkdirSync(path.join(projectDir, "definitions"));
fs.writeFileSync(
path.join(projectDir, "definitions/view.sqlx"),
`
config { type: "view" }
SELECT 1 AS col WHERE \${param("myvar")} == 1`
);

const result = runMainInVm(coreExecutionRequestFromPath(projectDir));

expect(result.compile.compiledGraph.graphErrors.compilationErrors).deep.equals([]);
expect(asPlainObject(result.compile.compiledGraph.tables)).deep.equals([
{
canonicalTarget: {
database: "defaultProject",
name: "view",
schema: "defaultDataset"
},
disabled: false,
enumType: "VIEW",
fileName: "definitions/view.sqlx",
hermeticity: "NON_HERMETIC",
query: "\n\nSELECT 1 AS col WHERE {myvar} == 1",
inputParams: ["myvar"],
target: {
database: "defaultProject",
name: "view",
schema: "defaultDataset"
},
type: "view",
}
]);
});

suite("resolve succeeds", () => {
[
WorkflowSettingsTemplates.bigquery,
Expand Down
6 changes: 5 additions & 1 deletion core/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export class Session {

public graphErrors: dataform.IGraphErrors;

private inputParams: Set<string> = new Set();

constructor(
rootDir?: string,
projectConfig?: dataform.ProjectConfig,
Expand Down Expand Up @@ -466,7 +468,8 @@ export class Session {
),
graphErrors: this.graphErrors,
dataformCoreVersion,
targets: this.actions.map(action => action.getTarget())
targets: this.actions.map(action => action.getTarget()),
inputParams: Array.from(this.inputParams).sort(),
});

this.fullyQualifyDependencies(
Expand Down Expand Up @@ -545,6 +548,7 @@ export class Session {
actions.forEach(action => {
try {
const compiledChunk = action.compile();
compiledChunk.inputParams.forEach(param => this.inputParams.add(param));
compiledChunks.push(compiledChunk as any);
} catch (e) {
this.compileError(e, action.getFileName(), action.getTarget());
Expand Down
16 changes: 16 additions & 0 deletions protos/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ message Table {
// Generated.
string file_name = 18;

repeated string input_params = 38;

reserved 1, 2, 7, 12, 16;
}

Expand All @@ -186,6 +188,8 @@ message Operation {
// Generated.
string file_name = 7;

repeated string input_params = 15;

reserved 1, 2, 4, 5;
}

Expand All @@ -210,6 +214,8 @@ message Assertion {
// Generated.
string file_name = 7;

repeated string input_params = 16;

reserved 1, 2, 4, 5, 6;
}

Expand All @@ -225,6 +231,8 @@ message Declaration {

ActionDescriptor action_descriptor = 3;

repeated string input_params = 6;

// Generated.
string file_name = 4;
}
Expand All @@ -235,6 +243,8 @@ message Test {
string test_query = 2;
string expected_output_query = 3;

repeated string input_params = 6;

// Generated.
string file_name = 4;
}
Expand All @@ -253,6 +263,8 @@ message Notebook {
bool disabled = 6;

string notebook_contents = 7;

repeated string input_params = 8;
}

message NotebookRuntimeOptions {
Expand Down Expand Up @@ -298,6 +310,8 @@ message DataPreparation {

LoadConfiguration load = 16;

repeated string input_params = 14;

reserved 7, 10;
}

Expand Down Expand Up @@ -337,6 +351,8 @@ message CompiledGraph {
repeated Target targets = 11;

reserved 5, 6;

repeated string input_params = 14;
}

message CoreExecutionRequest {
Expand Down