-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
61 lines (58 loc) · 1.84 KB
/
index.js
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
/**
* Builds a type-2 slowly changing dimensions table and view.
*/
module.exports = (
name,
{ uniqueKey, hash, timestamp, source, tags, incrementalConfig, columns = {} }
) => {
// Create an incremental table with just pure updates, for a full history of the table.
const updates = publish(`${name}_updates`, {
type: "incremental",
tags,
columns,
...incrementalConfig,
}).query(
!!hash ?
(ctx) => `
${ctx.when(
ctx.incremental(), `with ids_to_update as \
(select ${uniqueKey}, ${hash} from ${ctx.ref(source)}\
except distinct \
(select ${uniqueKey}, ${hash} from ${ctx.self()}))`
)}
select * from ${ctx.ref(source)}
${ctx.when(
ctx.incremental(),
`where ${timestamp} > (select max(${timestamp}) from ${ctx.self()})
and ${uniqueKey} in (select ${uniqueKey} from ids_to_update)`
)}`
:
(ctx) => `
select * from ${ctx.ref(source)}
${ctx.when(
ctx.incremental(),
`where ${timestamp} > (select max(${timestamp}) from ${ctx.self()})`
)}`
);
// Create a view on top of the raw updates table that contains computed valid_from and valid_to fields.
const view = publish(name, {
type: "view",
tags,
columns: {
...columns,
scd_valid_from: `The timestamp from which this row is valid for the given ${uniqueKey}.`,
scd_valid_to: `The timestamp until which this row is valid for the given ${uniqueKey}, or null if this it the latest value.`,
},
}).query(
(ctx) => `
select
*,
${timestamp} as scd_valid_from,
lead(${timestamp}) over (partition by ${uniqueKey} order by ${timestamp} asc) as scd_valid_to
from
${ctx.ref(updates.proto.target.schema, `${name}_updates`)}
`
);
// Returns the tables so they can be customized.
return { view, updates };
};