-
Notifications
You must be signed in to change notification settings - Fork 148
/
table.ts
361 lines (313 loc) · 9.27 KB
/
table.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import {
IColumnsDescriptor,
mapToColumnProtoArray,
Resolvable,
Session
} from "@dataform/core/session";
import * as utils from "@dataform/core/utils";
import { dataform } from "@dataform/protos";
export enum TableTypes {
TABLE = "table",
VIEW = "view",
INCREMENTAL = "incremental",
INLINE = "inline"
}
export enum DistStyleTypes {
EVEN = "even",
KEY = "key",
ALL = "all"
}
export enum SortStyleTypes {
COMPOUND = "compound",
INTERLEAVED = "interleaved"
}
export const ignoredProps: {
[tableType: string]: Array<keyof dataform.ITable>;
} = {
[TableTypes.INLINE]: [
"bigquery",
"redshift",
"sqlDataWarehouse",
"preOps",
"postOps",
"actionDescriptor",
"disabled",
"where"
]
};
type ValueOf<T> = T[keyof T];
export type TContextable<T> = T | ((ctx: TableContext) => T);
export type TableType = ValueOf<TableTypes>;
export interface TConfig {
type?: TableType;
dependencies?: Resolvable | Resolvable[];
tags?: string[];
description?: string;
columns?: IColumnsDescriptor;
disabled?: boolean;
protected?: boolean;
redshift?: dataform.IRedshiftOptions;
bigquery?: dataform.IBigQueryOptions;
sqldatawarehouse?: dataform.ISQLDataWarehouseOptions;
schema?: string;
}
export class Table {
public proto: dataform.Table = dataform.Table.create({
type: "view",
disabled: false,
tags: []
});
// Hold a reference to the Session instance.
public session: Session;
// We delay contextification until the final compile step, so hold these here for now.
public contextableQuery: TContextable<string>;
private contextableWhere: TContextable<string>;
private contextablePreOps: Array<TContextable<string | string[]>> = [];
private contextablePostOps: Array<TContextable<string | string[]>> = [];
public config(config: TConfig) {
if (config.type) {
this.type(config.type);
}
if (config.dependencies) {
this.dependencies(config.dependencies);
}
if (config.disabled) {
this.disabled();
}
if (config.protected) {
this.protected();
}
if (config.redshift) {
this.redshift(config.redshift);
}
if (config.bigquery) {
this.bigquery(config.bigquery);
}
if (config.sqldatawarehouse) {
this.sqldatawarehouse(config.sqldatawarehouse);
}
if (config.tags) {
this.tags(config.tags);
}
if (config.description) {
this.description(config.description);
}
if (config.columns) {
this.columns(config.columns);
}
if (config.schema) {
this.schema(config.schema);
}
return this;
}
public type(type: TableType) {
this.proto.type = type as string;
return this;
}
public query(query: TContextable<string>) {
this.contextableQuery = query;
return this;
}
public where(where: TContextable<string>) {
this.contextableWhere = where;
return this;
}
public preOps(pres: TContextable<string | string[]>) {
this.contextablePreOps.push(pres);
return this;
}
public postOps(posts: TContextable<string | string[]>) {
this.contextablePostOps.push(posts);
return this;
}
public disabled() {
this.proto.disabled = true;
return this;
}
public protected() {
this.proto.protected = true;
return this;
}
public sqldatawarehouse(sqlDataWarehouse: dataform.ISQLDataWarehouseOptions) {
this.proto.sqlDataWarehouse = dataform.SQLDataWarehouseOptions.create(sqlDataWarehouse);
return this;
}
public redshift(redshift: dataform.IRedshiftOptions) {
this.proto.redshift = dataform.RedshiftOptions.create(redshift);
return this;
}
public bigquery(bigquery: dataform.IBigQueryOptions) {
this.proto.bigquery = dataform.BigQueryOptions.create(bigquery);
return this;
}
public dependencies(value: Resolvable | Resolvable[]) {
const newDependencies = utils.isResolvable(value) ? [value] : (value as Resolvable[]);
newDependencies.forEach((d: Resolvable) => {
// TODO: This code fails to function correctly if the inline table has not yet
// been attached to the session. This code probably needs to be moved to compile().
const allResolved = this.session.findActions(d);
const resolved = allResolved.length > 0 ? allResolved[0] : undefined;
if (!!resolved && resolved instanceof Table && resolved.proto.type === "inline") {
resolved.proto.dependencies.forEach(childDep => this.addDependency(childDep));
} else {
this.addDependency(d);
}
});
return this;
}
public tags(value: string | string[]) {
const newTags = typeof value === "string" ? [value] : value;
newTags.forEach(t => {
this.proto.tags.push(t);
});
return this;
}
public description(description: string) {
if (!this.proto.actionDescriptor) {
this.proto.actionDescriptor = {};
}
this.proto.actionDescriptor.description = description;
return this;
}
public columns(columns: IColumnsDescriptor) {
if (!this.proto.actionDescriptor) {
this.proto.actionDescriptor = {};
}
this.proto.actionDescriptor.columns = mapToColumnProtoArray(columns);
return this;
}
public schema(schema: string) {
this.session.setNameAndTarget(this.proto, this.proto.target.name, schema);
return this;
}
public compile() {
const context = new TableContext(this);
const incrementalContext = new TableContext(this, true);
this.proto.query = context.apply(this.contextableQuery);
if (this.proto.type === TableTypes.INCREMENTAL) {
this.proto.incrementalQuery = incrementalContext.apply(this.contextableQuery);
}
if (this.contextableWhere) {
this.proto.where = context.apply(this.contextableWhere);
}
this.contextablePreOps.forEach(contextablePreOps => {
const appliedPres = context.apply(contextablePreOps);
this.proto.preOps = (this.proto.preOps || []).concat(
typeof appliedPres === "string" ? [appliedPres] : appliedPres
);
});
this.contextablePreOps = [];
this.contextablePostOps.forEach(contextablePostOps => {
const appliedPosts = context.apply(contextablePostOps);
this.proto.postOps = (this.proto.postOps || []).concat(
typeof appliedPosts === "string" ? [appliedPosts] : appliedPosts
);
});
this.contextablePostOps = [];
return this.proto;
}
private addDependency(dependency: Resolvable): void {
const depName = utils.stringifyResolvable(dependency);
if (this.proto.dependencies.indexOf(depName) < 0) {
this.proto.dependencies.push(depName);
}
}
}
export interface ITableContext {
config: (config: TConfig) => string;
self: () => string;
name: () => string;
ref: (name: string) => string;
resolve: (name: string) => string;
type: (type: TableType) => string;
where: (where: TContextable<string>) => string;
isIncremental: () => boolean;
ifIncremental: (value: string) => string;
preOps: (statement: TContextable<string | string[]>) => string;
postOps: (statement: TContextable<string | string[]>) => string;
disabled: () => string;
redshift: (redshift: dataform.IRedshiftOptions) => string;
bigquery: (bigquery: dataform.IBigQueryOptions) => string;
dependencies: (name: Resolvable) => string;
apply: <T>(value: TContextable<T>) => T;
tags: (name: string | string[]) => string;
}
export class TableContext implements ITableContext {
constructor(private table: Table, private incremental = false) {}
public config(config: TConfig) {
this.table.config(config);
return "";
}
public self(): string {
return this.resolve({
schema: this.table.proto.target.schema,
name: this.table.proto.target.name
});
}
public name(): string {
return this.table.proto.target.name;
}
public ref(ref: Resolvable) {
const name =
typeof ref === "string" || typeof ref === "undefined" ? ref : `${ref.schema}.${ref.name}`;
if (!name) {
const message = `Action name is not specified`;
this.table.session.compileError(new Error(message));
return "";
}
this.table.dependencies(name);
return this.resolve(ref);
}
public resolve(ref: Resolvable) {
return this.table.session.resolve(ref);
}
public type(type: TableType) {
this.table.type(type);
return "";
}
public where(where: TContextable<string>) {
this.table.where(where);
return "";
}
public isIncremental() {
return !!this.incremental;
}
public ifIncremental(value: string) {
return this.isIncremental() ? value : "";
}
public preOps(statement: TContextable<string | string[]>) {
this.table.preOps(statement);
return "";
}
public postOps(statement: TContextable<string | string[]>) {
this.table.postOps(statement);
return "";
}
public disabled() {
this.table.disabled();
return "";
}
public redshift(redshift: dataform.IRedshiftOptions) {
this.table.redshift(redshift);
return "";
}
public bigquery(bigquery: dataform.IBigQueryOptions) {
this.table.bigquery(bigquery);
return "";
}
public dependencies(res: Resolvable) {
this.table.dependencies(res);
return "";
}
public apply<T>(value: TContextable<T>): T {
if (typeof value === "function") {
return (value as any)(this);
} else {
return value;
}
}
public tags(tags: string[]) {
this.table.tags(tags);
return "";
}
}