Skip to content

Commit c16f168

Browse files
JiT context - Add Running execution metadata (#2132)
Extend metadata of JiT context with information about the current execution run.
1 parent 906158f commit c16f168

5 files changed

Lines changed: 60 additions & 6 deletions

File tree

core/BUILD

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ ts_library(
6565
ts_test_suite(
6666
name = "tests",
6767
srcs = [
68-
"main_test.ts",
69-
"utils_test.ts",
70-
"compilers_test.ts",
7168
"actions/assertion_test.ts",
7269
"actions/data_preparation_test.ts",
7370
"actions/declaration_test.ts",
@@ -79,8 +76,11 @@ ts_test_suite(
7976
"actions/table_test.ts",
8077
"actions/test_test.ts",
8178
"actions/view_test.ts",
79+
"compilers_test.ts",
8280
"jit_compiler_test.ts",
8381
"jit_context_test.ts",
82+
"main_test.ts",
83+
"utils_test.ts",
8484
],
8585
data = [
8686
":node_modules",
@@ -94,10 +94,12 @@ ts_test_suite(
9494
"@npm//@types/chai",
9595
"@npm//@types/fs-extra",
9696
"@npm//@types/js-yaml",
97+
"@npm//@types/long",
9798
"@npm//@types/node",
9899
"@npm//chai",
99100
"@npm//fs-extra",
100101
"@npm//js-yaml",
102+
"@npm//long",
101103
],
102104
)
103105

core/contextables.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ export type JitContext<T> = T & {
9494
data?: { [k: string]: any },
9595
/** Original JiT compilation request. */
9696
request: dataform.IJitCompilationRequest,
97+
/** Current execution information for introspection. */
98+
executionData: dataform.IRunningExecutionData,
9799
};
98100

99101
/**

core/jit_compiler_test.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from "chai";
2+
import Long from "long";
23

34
import { jitCompile } from "df/core/jit_compiler";
45
import { dataform } from "df/protos/ts";
@@ -121,20 +122,54 @@ suite("jit_compiler", () => {
121122

122123
suite("jitCompileIncrementalTable", () => {
123124
test("compiles incremental table", async () => {
124-
const request = dataform.JitCompilationRequest.create({
125+
const request = dataform.JitCompilationRequest.create({
125126
jitCode: `async (ctx) => {
126127
if (ctx.incremental()) {
127128
return { query: "SELECT INC" };
128129
}
129130
return { query: "SELECT REG" };
130131
}`,
131132
target,
132-
jitData: {},
133-
compilationTargetType: dataform.JitCompilationTargetType.JIT_COMPILATION_TARGET_TYPE_INCREMENTAL_TABLE,
133+
jitData: {},
134+
compilationTargetType: dataform.JitCompilationTargetType.JIT_COMPILATION_TARGET_TYPE_INCREMENTAL_TABLE,
134135
});
135136
const result = await jitCompile(request, rpcCallback);
136137
expect(result.incrementalTable.incremental?.query).to.equal("SELECT INC");
137138
expect(result.incrementalTable.regular?.query).to.equal("SELECT REG");
138139
});
139140
});
141+
142+
suite("jitCompileContext", () => {
143+
test("can reference self and other tables", async () => {
144+
const request = dataform.JitCompilationRequest.create({
145+
jitCode: `async (jctx) => \`$\{jctx.self()\}\n$\{jctx.ref('other')\}\``,
146+
target,
147+
jitData: {},
148+
dependencies: [dataform.Target.create({
149+
database: "db",
150+
schema: "schema",
151+
name: "other",
152+
})],
153+
compilationTargetType: dataform.JitCompilationTargetType.JIT_COMPILATION_TARGET_TYPE_TABLE,
154+
155+
});
156+
const result = await jitCompile(request, rpcCallback);
157+
expect(result.table.query).to.equal("`db.schema.name`\n`db.schema.other`");
158+
});
159+
160+
test("can reference execution info data", async () => {
161+
const request = dataform.JitCompilationRequest.create({
162+
jitCode: `async (jctx) => \`$\{jctx.executionData.executionStartTime.seconds\}\n$\{jctx.executionData.executionId\}\``,
163+
target,
164+
jitData: {},
165+
compilationTargetType: dataform.JitCompilationTargetType.JIT_COMPILATION_TARGET_TYPE_TABLE,
166+
executionData: {
167+
executionId: "test-id",
168+
executionStartTime: {seconds: Long.fromNumber(1774974514), nanos: 481},
169+
}
170+
});
171+
const result = await jitCompile(request, rpcCallback);
172+
expect(result.table.query).to.equal("1774974514\ntest-id");
173+
});
174+
});
140175
});

core/jit_context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function canonicalTargetValue(target: dataform.ITarget): string {
99
/** Generate SQL action JiT context. */
1010
export class SqlActionJitContext implements JitContext<IActionContext> {
1111
public readonly data: { [k: string]: any } | undefined;
12+
public readonly executionData: dataform.IRunningExecutionData;
1213

1314
private readonly target: dataform.ITarget;
1415
private readonly resolvableMap: ResolvableMap<string>;
@@ -25,6 +26,7 @@ export class SqlActionJitContext implements JitContext<IActionContext> {
2526
value: canonicalTargetValue(dep)
2627
})));
2728
this.data = jitDataToJsValue(request.jitData);
29+
this.executionData = request.executionData;
2830
}
2931

3032
public self(): string {

protos/jit.proto

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package dataform;
44

55
import "google/protobuf/empty.proto";
66
import "google/protobuf/struct.proto";
7+
import "google/protobuf/timestamp.proto";
78
import "protos/core.proto";
89
import "protos/db_adapter.proto";
910

@@ -83,6 +84,16 @@ message DeleteTableRequest {
8384
Target target = 1;
8485
}
8586

87+
// Execution information for introspection.
88+
message RunningExecutionData {
89+
// Targets that are being run in the current execution.
90+
repeated Target run_targets = 1;
91+
// ID of the current execution.
92+
string execution_id = 2;
93+
// Start time of the current execution.
94+
google.protobuf.Timestamp execution_start_time = 3;
95+
}
96+
8697
// JiT compilation target type.
8798
enum JitCompilationTargetType {
8899
// Unspecified - will result in error.
@@ -109,6 +120,8 @@ message JitCompilationRequest {
109120
JitCompilationTargetType compilation_target_type = 5;
110121
// List of additional file paths accessible at compilation.
111122
repeated string file_paths = 6;
123+
// Current execution information for introspection.
124+
RunningExecutionData execution_data = 7;
112125
}
113126

114127
// JiT compilation response.

0 commit comments

Comments
 (0)