-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_duckdb-views.sql
More file actions
187 lines (185 loc) · 4.99 KB
/
_duckdb-views.sql
File metadata and controls
187 lines (185 loc) · 4.99 KB
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
-- Resolver view
CREATE VIEW view_resolver AS
SELECT
_id,
params,
sites,
testUri,
name,
CAST(json_extract(to_json(priority), '$.$numberInt') AS INTEGER) as priority,
pattern,
page,
contentSourceId,
contentConfigMapping,
content2pageMapping,
CAST(json_extract(to_json(__v), '$.$numberInt') AS INTEGER) as __v,
defaultOutputType,
REPLACE(REPLACE(note, CHR(10), ' '), CHR(13), ' ') as note -- replace newlines with spaces
FROM read_json_auto(
'pb-data/resolver_config.json',
format = 'newline_delimited',
ignore_errors=true
);
-- Pages and templates combined view
CREATE VIEW view_page_and_template AS
SELECT
_id as pageOrTemplateId,
'Page' as isPageOrTemplate,
uri,
name,
defaultOutputType,
published
FROM read_json_auto(
'pb-data/page.json',
format = 'newline_delimited',
ignore_errors=true
)
UNION
SELECT
_id as pageOrTemplateId,
'Template' as isPageOrTemplate,
'' as uri,
name,
'' as defaultOutputType,
published
FROM read_json_auto(
'pb-data/template.json',
format = 'newline_delimited',
ignore_errors=true
);
-- Simplified and flattened rendering collection view
CREATE VIEW view_rendering AS
WITH PublishedVersions AS (
SELECT DISTINCT published as versionId
FROM view_page_and_template
WHERE published IS NOT NULL
),
PublishedLayoutItems AS (
SELECT
_id as renderingId,
_version as renderingVersionId,
json_extract_string(creationDate, '$.$numberLong') as creationDate,
layout,
layoutItems
FROM read_json_auto(
'pb-data/rendering.json',
format = 'newline_delimited',
ignore_errors=true
)
WHERE _version IN (SELECT versionId FROM PublishedVersions)
),
LatestLayoutItems AS (
SELECT
renderingId,
renderingVersionId,
creationDate,
layout,
layoutItems
FROM PublishedLayoutItems
-- Select the latest rendering by creation date for each page's published version
QUALIFY ROW_NUMBER() OVER (PARTITION BY renderingVersionId ORDER BY creationDate DESC) = 1
ORDER BY renderingVersionId, creationDate DESC
),
FlattenedLayoutItems AS (
SELECT
renderingId,
renderingVersionId,
creationDate,
layout,
unnest(layoutItems) as layoutItem
FROM LatestLayoutItems
ORDER BY renderingVersionId, creationDate DESC
),
RenderableItems AS (
SELECT
renderingId,
renderingVersionId,
creationDate,
layout,
unnest(layoutItem.renderableItems) as renderableItem
FROM FlattenedLayoutItems
),
ExpandedRenderableItems AS (
SELECT
renderingVersionId,
layout,
renderableItem.fingerprint,
renderableItem.className,
renderableItem.featureConfig,
renderableItem.chainConfig,
renderableItem.displayName,
renderableItem.customFields,
renderableItem.features
FROM RenderableItems
-- WHERE renderableItem.parent IS NULL -- Optionally exclude feature-linked children feature blocks
),
FeaturesFromRenderableItems AS (
SELECT
renderingVersionId,
layout,
fingerprint,
'' as chainName,
'' as chainDisplayName,
featureConfig as featureName,
displayName as featureDisplayName,
(
SELECT string_agg(content_service, '|')
FROM (
SELECT DISTINCT json_extract_string(customFields, '$.' || key.unnest || '.contentService') as content_service
FROM unnest(json_keys(customFields)) as key
WHERE json_extract_string(customFields, '$.' || key.unnest || '.contentService') IS NOT NULL
AND json_extract_string(customFields, '$.' || key.unnest || '.contentService') != ''
)
) as contentService
FROM ExpandedRenderableItems
WHERE className LIKE '%.rendering.Feature'
),
ChainsFromRenderableItems AS (
SELECT
renderingVersionId,
layout,
chainConfig as chainName,
displayName as chainDisplayName,
features
FROM ExpandedRenderableItems
WHERE className LIKE '%.rendering.Chain'
),
UnnestedFeaturesFromChains AS (
SELECT
renderingVersionId,
layout,
chainName,
chainDisplayName,
unnest(features) as feature
FROM ChainsFromRenderableItems
),
FeaturesFromChains AS (
SELECT
renderingVersionId,
layout,
feature.fingerprint,
chainName,
chainDisplayName,
feature.featureConfig as featureName,
feature.displayName as featureDisplayName,
(
SELECT string_agg(content_service, '|')
FROM (
SELECT DISTINCT json_extract_string(feature.customFields, '$.' || key.unnest || '.contentService') as content_service
FROM unnest(json_keys(feature.customFields)) as key
WHERE json_extract_string(feature.customFields, '$.' || key.unnest || '.contentService') IS NOT NULL
AND json_extract_string(feature.customFields, '$.' || key.unnest || '.contentService') != ''
)
) as contentService
FROM UnnestedFeaturesFromChains
-- WHERE feature.parent IS NULL -- Optionally exclude feature-linked children feature blocks
),
FlattenedAllFeatures AS (
SELECT *
FROM (
SELECT * FROM FeaturesFromRenderableItems
UNION ALL
SELECT * FROM FeaturesFromChains
)
)
SELECT * FROM FlattenedAllFeatures