Skip to content

Commit 16ab819

Browse files
committed
refactor SQL operation generation for report creation
1 parent 9500b89 commit 16ab819

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

definitions/output/reports/reports_dynamic.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,31 +177,40 @@ function generateOperationSQL(ctx, reportConfig) {
177177
return `
178178
DECLARE job_config JSON;
179179
180-
--/* First report run - uncomment to create table
180+
-- Run analysis once
181+
CREATE TEMP TABLE ${tableName}_temp AS (
182+
${sql.query(ctx, reportConfig)}
183+
);
184+
185+
-- Create table on first run (schema only, no data)
181186
CREATE TABLE IF NOT EXISTS ${EXPORT_CONFIG.dataset}.${tableName}
182187
PARTITION BY date
183188
CLUSTER BY metric, lens, client
184189
AS
185-
--*/
190+
SELECT
191+
client,
192+
DATE('${date}') AS date,
193+
'${metric.id}' AS metric,
194+
'${lens.name}' AS lens,
195+
* EXCEPT(client)
196+
FROM ${tableName}_temp
197+
WHERE FALSE;
186198
187-
/* Subsequent report run
199+
-- Delete existing data for this partition
188200
DELETE FROM ${EXPORT_CONFIG.dataset}.${tableName}
189201
WHERE date = '${date}'
190202
AND metric = '${metric.id}'
191203
AND lens = '${lens.name}';
192204
205+
-- Insert fresh data
193206
INSERT INTO ${EXPORT_CONFIG.dataset}.${tableName}
194-
*/
195-
196207
SELECT
197208
client,
198209
DATE('${date}') AS date,
199210
'${metric.id}' AS metric,
200211
'${lens.name}' AS lens,
201212
* EXCEPT(client)
202-
FROM (
203-
${sql.query(ctx, reportConfig)}
204-
);
213+
FROM ${tableName}_temp;
205214
206215
SET job_config = TO_JSON(
207216
STRUCT(

0 commit comments

Comments
 (0)