-
Notifications
You must be signed in to change notification settings - Fork 148
/
build.ts
176 lines (160 loc) · 6.46 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { Credentials } from "@dataform/api/commands/credentials";
import { state } from "@dataform/api/commands/state";
import * as dbadapters from "@dataform/api/dbadapters";
import { adapters } from "@dataform/core";
import * as utils from "@dataform/core/utils";
import { dataform } from "@dataform/protos";
export function build(
compiledGraph: dataform.ICompiledGraph,
runConfig: dataform.IRunConfig,
credentials: Credentials
) {
return state(
compiledGraph,
dbadapters.create(credentials, compiledGraph.projectConfig.warehouse)
).then(state => {
return new Builder(compiledGraph, runConfig, state).build();
});
}
export class Builder {
private compiledGraph: dataform.ICompiledGraph;
private runConfig: dataform.IRunConfig;
private adapter: adapters.IAdapter;
private state: dataform.IWarehouseState;
constructor(
compiledGraph: dataform.ICompiledGraph,
runConfig: dataform.IRunConfig,
state: dataform.IWarehouseState
) {
this.compiledGraph = compiledGraph;
this.runConfig = runConfig;
this.state = state;
this.adapter = adapters.create(compiledGraph.projectConfig);
}
public build(): dataform.ExecutionGraph {
if (utils.graphHasErrors(this.compiledGraph)) {
throw new Error(`Project has unresolved compilation or validation errors.`);
}
const tableStateByTarget: { [targetJson: string]: dataform.ITableMetadata } = {};
this.state.tables.forEach(tableState => {
tableStateByTarget[JSON.stringify(tableState.target)] = tableState;
});
// Remove inline tables.
const filteredTables = this.compiledGraph.tables.filter(t => t.type !== "inline");
// Firstly, turn every thing into an execution action.
const allActions: dataform.IExecutionAction[] = [].concat(
filteredTables.map(t => this.buildTable(t, tableStateByTarget[JSON.stringify(t.target)])),
this.compiledGraph.operations.map(o => this.buildOperation(o)),
this.compiledGraph.assertions.map(a => this.buildAssertion(a))
);
const allActionNames = allActions.map(n => n.name);
const actionNameMap: { [name: string]: dataform.IExecutionAction } = {};
allActions.forEach(action => (actionNameMap[action.name] = action));
// Determine which action should be included.
let includedActionNames =
this.runConfig.actions && this.runConfig.actions.length > 0
? utils.matchPatterns(this.runConfig.actions, allActionNames)
: allActionNames;
let includedActions = allActions.filter(
action => includedActionNames.indexOf(action.name) >= 0
);
// Determine ations selected with --tag option and update applicable actions
if (this.runConfig.tags && this.runConfig.tags.length > 0) {
const allTaggedActionNames: string[] = [].concat(
filteredTables
.filter(t => t.tags.some(t => this.runConfig.tags.includes(t)))
.map(t => t.name),
this.compiledGraph.operations
.filter(o => o.tags.some(t => this.runConfig.tags.includes(t)))
.map(t => t.name),
this.compiledGraph.assertions
.filter(a => a.tags.some(t => this.runConfig.tags.includes(t)))
.map(t => t.name)
);
const allTaggedActions = allActions.filter(a => allTaggedActionNames.includes(a.name));
if (this.runConfig.actions && this.runConfig.actions.length > 0) {
// Add up the actions specified by the tags to includedAction(Name).
const missingActNames = allTaggedActionNames.filter(n => !includedActionNames.includes(n));
const missingAct = allActions.filter(action => missingActNames.includes(action.name));
missingActNames.forEach(mn => includedActionNames.push(mn));
missingAct.forEach(ma => includedActions.push(ma));
} else {
// Leave only the actions specified by the tags
includedActionNames = allTaggedActionNames;
includedActions = allTaggedActions;
}
}
if (this.runConfig.includeDependencies) {
// Compute all transitive dependencies.
for (let i = 0; i < allActions.length; i++) {
includedActions.forEach(action => {
const matchingActionNames =
action.dependencies && action.dependencies.length > 0
? utils.matchPatterns(action.dependencies, allActionNames)
: [];
// Update included action names.
matchingActionNames.forEach(actionName => {
if (includedActionNames.indexOf(actionName) < 0) {
includedActionNames.push(actionName);
}
});
// Update included actions.
includedActions = allActions.filter(
action => includedActionNames.indexOf(action.name) >= 0
);
});
}
}
// Remove any excluded dependencies.
includedActions.forEach(action => {
action.dependencies = action.dependencies.filter(
dep => includedActionNames.indexOf(dep) >= 0
);
});
return dataform.ExecutionGraph.create({
projectConfig: this.compiledGraph.projectConfig,
runConfig: this.runConfig,
warehouseState: this.state,
actions: includedActions
});
}
public buildTable(t: dataform.ITable, tableMetadata: dataform.ITableMetadata) {
const emptyTasks = [] as dataform.IExecutionTask[];
if (t.protected && this.runConfig.fullRefresh) {
throw new Error("Protected datasets cannot be fully refreshed.");
}
const tasks = t.disabled
? emptyTasks
: emptyTasks.concat(
(t.preOps || []).map(pre => ({ statement: pre })),
this.adapter.publishTasks(t, this.runConfig, tableMetadata).build(),
(t.postOps || []).map(post => ({ statement: post }))
);
return dataform.ExecutionAction.create({
name: t.name,
dependencies: t.dependencies,
type: "table",
target: t.target,
tableType: t.type,
tasks
});
}
public buildOperation(operation: dataform.IOperation) {
return dataform.ExecutionAction.create({
name: operation.name,
dependencies: operation.dependencies,
type: "operation",
target: operation.target,
tasks: operation.queries.map(statement => ({ type: "statement", statement }))
});
}
public buildAssertion(assertion: dataform.IAssertion) {
return dataform.ExecutionAction.create({
name: assertion.name,
dependencies: assertion.dependencies,
type: "assertion",
target: assertion.target,
tasks: this.adapter.assertTasks(assertion, this.compiledGraph.projectConfig).build()
});
}
}