Skip to content

Commit 24ec3ff

Browse files
committed
Fix projection-map refresh and aggregate recompute classification
1 parent 974a7b2 commit 24ec3ff

18 files changed

Lines changed: 963 additions & 142 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
-- {"operators": "INNER_JOIN,AGGREGATE,ORDER,WINDOW,CTE,SUBQUERY", "complexity": "high", "is_incremental": true, "has_nulls": false, "has_cast": false, "has_case": false, "tables": "ITEM,STOCK", "ducklake": true}
2-
WITH stock_val AS (SELECT s.S_W_ID, s.S_I_ID, s.S_QUANTITY, i.I_PRICE, (s.S_QUANTITY * i.I_PRICE) AS val FROM dl.STOCK s JOIN dl.ITEM i ON s.S_I_ID = i.I_ID) SELECT S_W_ID, S_I_ID, val, SUM(val) OVER (PARTITION BY S_W_ID) AS w_total, ROW_NUMBER() OVER (PARTITION BY S_W_ID ORDER BY val DESC) AS rk FROM stock_val;
2+
WITH stock_val AS (SELECT s.S_W_ID, s.S_I_ID, s.S_QUANTITY, i.I_PRICE, (s.S_QUANTITY * i.I_PRICE) AS val FROM dl.STOCK s JOIN dl.ITEM i ON s.S_I_ID = i.I_ID) SELECT S_W_ID, S_I_ID, val, SUM(val) OVER (PARTITION BY S_W_ID) AS w_total, ROW_NUMBER() OVER (PARTITION BY S_W_ID ORDER BY val DESC, S_I_ID ASC) AS rk FROM stock_val;

benchmark/queries/tpcc/ducklake_0312.sql

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ qualify_seed AS MATERIALIZED (
11031103
ff.order_id,
11041104
ff.line_number,
11051105
ff.item_id,
1106+
ff.line_key,
11061107
ff.credit_code,
11071108
ff.fulfillment_state,
11081109
ff.account_state,
@@ -1124,6 +1125,7 @@ qualify_ranked AS (
11241125
qs.order_id,
11251126
qs.line_number,
11261127
qs.item_id,
1128+
qs.line_key,
11271129
qs.credit_code,
11281130
qs.fulfillment_state,
11291131
qs.account_state,
@@ -1145,15 +1147,18 @@ qualify_ranked AS (
11451147
qs.stock_quantity ASC,
11461148
qs.total_history_amount ASC,
11471149
qs.order_entry_date ASC,
1148-
qs.delivery_date ASC
1150+
qs.delivery_date ASC,
1151+
qs.line_key ASC
11491152
) AS fulfillment_rank,
11501153
RANK() OVER (
11511154
PARTITION BY qs.warehouse_id, qs.credit_code
1152-
ORDER BY qs.customer_balance DESC, qs.customer_id ASC, qs.order_id ASC
1155+
ORDER BY qs.customer_balance DESC, qs.customer_id ASC, qs.order_id ASC, qs.line_number ASC,
1156+
qs.item_id ASC, qs.fulfillment_state ASC, qs.account_state ASC, qs.line_key ASC
11531157
) AS balance_rank,
11541158
DENSE_RANK() OVER (
11551159
PARTITION BY qs.warehouse_id, qs.district_id, qs.account_state
1156-
ORDER BY qs.stock_quantity ASC, qs.item_id ASC
1160+
ORDER BY qs.stock_quantity ASC, qs.item_id ASC, qs.customer_id ASC, qs.order_id ASC,
1161+
qs.line_number ASC, qs.credit_code ASC, qs.fulfillment_state ASC, qs.line_key ASC
11571162
) AS stock_rank,
11581163
SUM(qs.extended_amount) OVER (
11591164
PARTITION BY qs.warehouse_id, qs.district_id, qs.customer_id
@@ -1177,11 +1182,13 @@ qualify_ranked AS (
11771182
qs.stock_quantity ASC,
11781183
qs.total_history_amount ASC,
11791184
qs.order_entry_date ASC,
1180-
qs.delivery_date ASC
1185+
qs.delivery_date ASC,
1186+
qs.line_key ASC
11811187
) <= 5
11821188
OR DENSE_RANK() OVER (
11831189
PARTITION BY qs.warehouse_id, qs.district_id, qs.account_state
1184-
ORDER BY qs.stock_quantity ASC, qs.item_id ASC
1190+
ORDER BY qs.stock_quantity ASC, qs.item_id ASC, qs.customer_id ASC, qs.order_id ASC,
1191+
qs.line_number ASC, qs.credit_code ASC, qs.fulfillment_state ASC, qs.line_key ASC
11851192
) <= 4
11861193

11871194
),
@@ -1193,6 +1200,7 @@ qualify_window_pass_2 AS (
11931200
qr.order_id,
11941201
qr.line_number,
11951202
qr.item_id,
1203+
qr.line_key,
11961204
qr.credit_code,
11971205
qr.fulfillment_state,
11981206
qr.account_state,
@@ -1222,8 +1230,9 @@ qualify_window_pass_2 AS (
12221230
qr.fulfillment_rank ASC,
12231231
qr.balance_rank ASC,
12241232
qr.stock_rank ASC,
1225-
qr.customer_amount_window ASC,
1226-
qr.customer_history_window ASC
1233+
ROUND(qr.customer_amount_window, 10) ASC,
1234+
ROUND(qr.customer_history_window, 10) ASC,
1235+
qr.line_key ASC
12271236
) AS amount_tile,
12281237
LAG(qr.extended_amount, 1, 0) OVER (
12291238
PARTITION BY qr.warehouse_id, qr.district_id, qr.customer_id
@@ -1241,8 +1250,9 @@ qualify_window_pass_2 AS (
12411250
qr.fulfillment_rank ASC,
12421251
qr.balance_rank ASC,
12431252
qr.stock_rank ASC,
1244-
qr.customer_amount_window ASC,
1245-
qr.customer_history_window ASC
1253+
ROUND(qr.customer_amount_window, 10) ASC,
1254+
ROUND(qr.customer_history_window, 10) ASC,
1255+
qr.line_key ASC
12461256
) AS previous_customer_amount,
12471257
COUNT(*) OVER (
12481258
PARTITION BY qr.warehouse_id, qr.credit_code, qr.fulfillment_state
@@ -1258,6 +1268,7 @@ qualify_window_pass_3 AS (
12581268
qp2.order_id,
12591269
qp2.line_number,
12601270
qp2.item_id,
1271+
qp2.line_key,
12611272
qp2.credit_code,
12621273
qp2.fulfillment_state,
12631274
qp2.account_state,
@@ -1275,7 +1286,9 @@ qualify_window_pass_3 AS (
12751286
qp2.credit_fulfillment_rows,
12761287
DENSE_RANK() OVER (
12771288
PARTITION BY qp2.warehouse_id, qp2.amount_tile
1278-
ORDER BY qp2.customer_amount_window DESC, qp2.customer_id ASC
1289+
ORDER BY ROUND(qp2.customer_amount_window, 10) DESC, qp2.customer_id ASC, qp2.order_id ASC,
1290+
qp2.line_number ASC, qp2.item_id ASC, qp2.credit_code ASC, qp2.fulfillment_state ASC,
1291+
qp2.account_state ASC, qp2.line_key ASC
12791292
) AS tile_customer_rank,
12801293
SUM(qp2.extended_amount - qp2.previous_customer_amount) OVER (
12811294
PARTITION BY qp2.warehouse_id, qp2.district_id

benchmark/queries/tpcc/ducklake_0315.sql

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@ tmp_fact AS MATERIALIZED (
11071107
ff.fulfillment_state,
11081108
ff.account_state,
11091109
ff.item_bucket,
1110+
ff.line_key,
11101111
ff.extended_amount,
11111112
ff.customer_balance,
11121113
ff.stock_quantity,
@@ -1129,6 +1130,7 @@ tmp_customer_window AS (
11291130
tf.fulfillment_state,
11301131
tf.account_state,
11311132
tf.item_bucket,
1133+
tf.line_key,
11321134
tf.extended_amount,
11331135
tf.customer_balance,
11341136
tf.stock_quantity,
@@ -1137,11 +1139,11 @@ tmp_customer_window AS (
11371139
tf.order_line_count,
11381140
ROW_NUMBER() OVER (
11391141
PARTITION BY tf.warehouse_id, tf.district_id, tf.customer_id
1140-
ORDER BY tf.extended_amount DESC, tf.order_id ASC, tf.line_number ASC
1142+
ORDER BY tf.extended_amount DESC, tf.order_id ASC, tf.line_number ASC, tf.item_id ASC, tf.line_key ASC
11411143
) AS customer_line_rank,
1142-
SUM(tf.extended_amount) OVER (
1144+
CAST(SUM(CAST(ROUND(tf.extended_amount, 4) AS DECIMAL(38, 4))) OVER (
11431145
PARTITION BY tf.warehouse_id, tf.district_id, tf.customer_id
1144-
) AS customer_total_amount,
1146+
) AS DOUBLE) AS customer_total_amount,
11451147
MAX(tf.stock_quantity) OVER (
11461148
PARTITION BY tf.warehouse_id, tf.item_id
11471149
) AS warehouse_item_peak
@@ -1155,12 +1157,13 @@ tmp_order_window AS (
11551157
tf.order_id,
11561158
tf.line_number,
11571159
tf.item_id,
1160+
tf.line_key,
11581161
COUNT(*) OVER (
11591162
PARTITION BY tf.warehouse_id, tf.district_id, tf.order_id
11601163
) AS order_rows_window,
1161-
SUM(tf.extended_amount) OVER (
1164+
CAST(SUM(CAST(ROUND(tf.extended_amount, 4) AS DECIMAL(38, 4))) OVER (
11621165
PARTITION BY tf.warehouse_id, tf.district_id, tf.order_id
1163-
) AS order_amount_window,
1166+
) AS DOUBLE) AS order_amount_window,
11641167
MIN(tf.line_number) OVER (
11651168
PARTITION BY tf.warehouse_id, tf.district_id, tf.order_id
11661169
) AS first_line_window
@@ -1174,6 +1177,7 @@ tmp_stock_window AS (
11741177
tf.credit_code,
11751178
tf.fulfillment_state,
11761179
tf.account_state,
1180+
tf.line_key,
11771181
tf.stock_quantity,
11781182
DENSE_RANK() OVER (
11791183
PARTITION BY tf.warehouse_id, tf.credit_code
@@ -1197,6 +1201,8 @@ inner_join_chain AS (
11971201
cw.fulfillment_state,
11981202
cw.account_state,
11991203
cw.item_bucket,
1204+
cw.line_key,
1205+
sw.line_key AS stock_line_key,
12001206
cw.extended_amount,
12011207
cw.customer_balance,
12021208
cw.stock_quantity,
@@ -1218,6 +1224,7 @@ inner_join_chain AS (
12181224
AND ow.order_id = cw.order_id
12191225
AND ow.line_number = cw.line_number
12201226
AND ow.item_id = cw.item_id
1227+
AND ow.line_key = cw.line_key
12211228
INNER JOIN tmp_stock_window sw
12221229
ON sw.warehouse_id = cw.warehouse_id
12231230
AND sw.item_id = cw.item_id
@@ -1232,7 +1239,7 @@ credit_summary AS (
12321239
district_id,
12331240
credit_code,
12341241
COUNT(*) AS credit_rows,
1235-
SUM(extended_amount) AS credit_amount,
1242+
CAST(SUM(CAST(ROUND(extended_amount, 4) AS DECIMAL(38, 4))) AS DOUBLE) AS credit_amount,
12361243
MAX(customer_total_amount) AS max_customer_total_amount,
12371244
COUNT(*) FILTER (WHERE payment_history_rows > 0) AS paid_history_rows
12381245
FROM inner_join_chain
@@ -1246,7 +1253,9 @@ fulfillment_summary AS (
12461253
fulfillment_state,
12471254
account_state,
12481255
COUNT(*) AS fulfillment_rows,
1249-
SUM(extended_amount) AS fulfillment_amount,
1256+
CAST(SUM(CAST(ROUND(extended_amount, 4) AS DECIMAL(38, 4))) AS DOUBLE) AS fulfillment_amount,
1257+
MIN(line_key) AS summary_line_key,
1258+
MIN(line_key) AS summary_stock_line_key,
12501259
MAX(stock_quantity) AS max_fulfillment_stock_window
12511260
FROM tmp_fact
12521261
GROUP BY warehouse_id, district_id, fulfillment_state, account_state
@@ -1264,6 +1273,8 @@ left_join_chain AS (
12641273
ijc.fulfillment_state,
12651274
ijc.account_state,
12661275
ijc.item_bucket,
1276+
ijc.line_key,
1277+
ijc.stock_line_key,
12671278
ijc.extended_amount,
12681279
ijc.customer_balance,
12691280
ijc.stock_quantity,
@@ -1298,6 +1309,8 @@ full_outer_chain AS (
12981309
COALESCE(ljc.fulfillment_state, fs.fulfillment_state, 'NO_FULFILLMENT') AS fulfillment_state,
12991310
COALESCE(ljc.account_state, fs.account_state, 'NO_ACCOUNT') AS account_state,
13001311
COALESCE(ljc.item_bucket, 'NO_BUCKET') AS item_bucket,
1312+
COALESCE(ljc.line_key, fs.summary_line_key) AS line_key,
1313+
COALESCE(ljc.stock_line_key, fs.summary_stock_line_key) AS stock_line_key,
13011314
COALESCE(ljc.extended_amount, 0) AS extended_amount,
13021315
COALESCE(ljc.customer_balance, 0) AS customer_balance,
13031316
COALESCE(ljc.stock_quantity, 0) AS stock_quantity,
@@ -1336,6 +1349,8 @@ semi_join_chain AS (
13361349
foc.fulfillment_state,
13371350
foc.account_state,
13381351
foc.item_bucket,
1352+
foc.line_key,
1353+
foc.stock_line_key,
13391354
foc.extended_amount,
13401355
foc.customer_balance,
13411356
foc.stock_quantity,
@@ -1377,6 +1392,8 @@ anti_join_chain AS (
13771392
sjc.fulfillment_state,
13781393
sjc.account_state,
13791394
sjc.item_bucket,
1395+
sjc.line_key,
1396+
sjc.stock_line_key,
13801397
sjc.extended_amount,
13811398
sjc.customer_balance,
13821399
sjc.stock_quantity,
@@ -1419,6 +1436,8 @@ join_window_pass_1 AS (
14191436
ajc.fulfillment_state,
14201437
ajc.account_state,
14211438
ajc.item_bucket,
1439+
ajc.line_key,
1440+
ajc.stock_line_key,
14221441
ajc.extended_amount,
14231442
ajc.customer_balance,
14241443
ajc.stock_quantity,
@@ -1439,11 +1458,13 @@ join_window_pass_1 AS (
14391458
ajc.max_fulfillment_stock_window,
14401459
ROW_NUMBER() OVER (
14411460
PARTITION BY ajc.warehouse_id, ajc.district_id
1442-
ORDER BY ajc.extended_amount DESC, ajc.order_id ASC, ajc.line_number ASC
1461+
ORDER BY ajc.extended_amount DESC, ajc.order_id ASC, ajc.line_number ASC, ajc.item_id ASC,
1462+
ajc.credit_code ASC, ajc.fulfillment_state ASC, ajc.account_state ASC, ajc.line_key ASC,
1463+
ajc.stock_line_key ASC
14431464
) AS chain_rank,
1444-
SUM(ajc.extended_amount + ajc.credit_amount + ajc.fulfillment_amount) OVER (
1465+
CAST(SUM(CAST(ROUND(ajc.extended_amount + ajc.credit_amount + ajc.fulfillment_amount, 4) AS DECIMAL(38, 4))) OVER (
14451466
PARTITION BY ajc.warehouse_id
1446-
) AS warehouse_chain_amount
1467+
) AS DOUBLE) AS warehouse_chain_amount
14471468
FROM anti_join_chain ajc
14481469

14491470
),
@@ -1459,6 +1480,8 @@ join_window_pass_2 AS (
14591480
jwp1.fulfillment_state,
14601481
jwp1.account_state,
14611482
jwp1.item_bucket,
1483+
jwp1.line_key,
1484+
jwp1.stock_line_key,
14621485
jwp1.extended_amount,
14631486
jwp1.customer_balance,
14641487
jwp1.stock_quantity,
@@ -1481,7 +1504,7 @@ join_window_pass_2 AS (
14811504
jwp1.warehouse_chain_amount,
14821505
DENSE_RANK() OVER (
14831506
PARTITION BY jwp1.warehouse_id, jwp1.credit_code
1484-
ORDER BY jwp1.warehouse_chain_amount DESC, jwp1.district_id ASC
1507+
ORDER BY ROUND(jwp1.warehouse_chain_amount, 4) DESC, jwp1.district_id ASC
14851508
) AS credit_chain_rank,
14861509
COUNT(*) OVER (
14871510
PARTITION BY jwp1.warehouse_id, jwp1.fulfillment_state, jwp1.account_state
@@ -1498,10 +1521,10 @@ join_chain_rollup AS (
14981521
account_state,
14991522
item_bucket,
15001523
COUNT(*) AS chain_rows,
1501-
SUM(extended_amount) AS chain_amount,
1502-
SUM(credit_amount) AS chain_credit_amount,
1503-
SUM(fulfillment_amount) AS chain_fulfillment_amount,
1504-
MAX(warehouse_chain_amount) AS max_warehouse_chain_amount,
1524+
CAST(SUM(CAST(ROUND(extended_amount, 4) AS DECIMAL(38, 4))) AS DOUBLE) AS chain_amount,
1525+
CAST(SUM(CAST(ROUND(credit_amount, 4) AS DECIMAL(38, 4))) AS DOUBLE) AS chain_credit_amount,
1526+
CAST(SUM(CAST(ROUND(fulfillment_amount, 4) AS DECIMAL(38, 4))) AS DOUBLE) AS chain_fulfillment_amount,
1527+
CAST(MAX(CAST(ROUND(warehouse_chain_amount, 4) AS DECIMAL(38, 4))) AS DOUBLE) AS max_warehouse_chain_amount,
15051528
MAX(chain_rank) AS max_chain_rank,
15061529
MAX(credit_chain_rank) AS max_credit_chain_rank,
15071530
MAX(fulfillment_account_rows) AS max_fulfillment_account_rows,
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
-- {"operators": "INNER_JOIN", "complexity": "high", "is_incremental": true, "has_nulls": false, "has_cast": false, "has_case": false, "tables": "CUSTOMER,OORDER,ORDER_LINE"}
2-
SELECT * FROM CUSTOMER c JOIN OORDER o ON c.C_ID = o.O_C_ID JOIN ORDER_LINE ol ON o.O_ID = ol.OL_O_ID;
1+
-- {"operators": "INNER_JOIN,FILTER", "complexity": "high", "is_incremental": true, "has_nulls": false, "has_cast": false, "has_case": false, "tables": "CUSTOMER,OORDER,ORDER_LINE"}
2+
SELECT *
3+
FROM CUSTOMER c
4+
JOIN OORDER o ON c.C_W_ID = o.O_W_ID AND c.C_D_ID = o.O_D_ID AND c.C_ID = o.O_C_ID
5+
JOIN ORDER_LINE ol ON o.O_W_ID = ol.OL_W_ID AND o.O_D_ID = ol.OL_D_ID AND o.O_ID = ol.OL_O_ID
6+
WHERE c.C_W_ID = 1 AND c.C_D_ID <= 2 AND c.C_ID <= 20;

0 commit comments

Comments
 (0)