Skip to content

Commit 2d18b61

Browse files
committed
Skip columnar index scan for GROUPING SETS / ROLLUP / CUBE
The Agg rewrite in columnar index scan only updates the main Agg's targetlist and grpColIdx. Plans built for GROUPING SETS, ROLLUP and CUBE attach a chain of additional Aggs that share the same input, so those Aggs would keep pointing at the pre-rewrite column layout. Bail out before rewriting when the Agg has a non-empty grouping sets list or chain.
1 parent 11b0fd1 commit 2d18b61

6 files changed

Lines changed: 511 additions & 0 deletions

File tree

tsl/src/nodes/columnar_index_scan/columnar_index_scan.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,18 @@ insert_columnar_index_scan(Plan *plan, void *context)
738738
return plan;
739739
}
740740

741+
/*
742+
* GROUPING SETS / ROLLUP / CUBE produce an Agg with a non-empty chain of
743+
* additional Aggs (and Sorts) that share the same input. The rewrite below
744+
* remaps only this Agg's targetlist and grpColIdx, so chained Aggs would
745+
* end up referencing the pre-rewrite column layout. Refuse the rewrite in
746+
* that case.
747+
*/
748+
if (agg->groupingSets != NIL || agg->chain != NIL)
749+
{
750+
return plan;
751+
}
752+
741753
Plan *childplan = agg->plan.lefttree;
742754

743755
/*

tsl/test/expected/columnar_index_scan-15.out

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,39 @@ SELECT device, max(time), min(time) FROM metrics GROUP BY device ORDER BY 1,2,3;
538538
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
539539
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
540540

541+
-- GROUPING SETS / ROLLUP / CUBE are not supported by ColumnarIndexScan
542+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY ROLLUP(device, sensor) ORDER BY device, sensor;
543+
--- QUERY PLAN ---
544+
GroupAggregate
545+
Group Key: _hyper_1_1_chunk.device, _hyper_1_1_chunk.sensor
546+
Group Key: _hyper_1_1_chunk.device
547+
Group Key: ()
548+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
549+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
550+
551+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY CUBE(device, sensor) ORDER BY device, sensor;
552+
--- QUERY PLAN ---
553+
Sort
554+
Sort Key: _hyper_1_1_chunk.device, _hyper_1_1_chunk.sensor
555+
-> MixedAggregate
556+
Hash Key: _hyper_1_1_chunk.sensor
557+
Group Key: _hyper_1_1_chunk.device, _hyper_1_1_chunk.sensor
558+
Group Key: _hyper_1_1_chunk.device
559+
Group Key: ()
560+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
561+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
562+
563+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY GROUPING SETS ((device), (sensor), ()) ORDER BY device, sensor;
564+
--- QUERY PLAN ---
565+
Sort
566+
Sort Key: _hyper_1_1_chunk.device, _hyper_1_1_chunk.sensor
567+
-> MixedAggregate
568+
Hash Key: _hyper_1_1_chunk.sensor
569+
Group Key: _hyper_1_1_chunk.device
570+
Group Key: ()
571+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
572+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
573+
541574
\set PREFIX ''
542575
\set ECHO errors
543576
--- Unoptimized results
@@ -1412,6 +1445,47 @@ SELECT device, max(time), min(time) FROM metrics GROUP BY device ORDER BY 1,2,3;
14121445
Group Key: _hyper_1_1_chunk.device
14131446
-> Seq Scan on _hyper_1_1_chunk
14141447

1448+
-- GROUPING SETS / ROLLUP / CUBE are not supported by ColumnarIndexScan
1449+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY ROLLUP(device, sensor) ORDER BY device, sensor;
1450+
--- QUERY PLAN ---
1451+
Sort
1452+
Sort Key: metrics.device, metrics.sensor
1453+
-> MixedAggregate
1454+
Hash Key: metrics.device, metrics.sensor
1455+
Hash Key: metrics.device
1456+
Group Key: ()
1457+
-> Append
1458+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
1459+
-> Seq Scan on compress_hyper_2_2_chunk
1460+
-> Seq Scan on _hyper_1_1_chunk
1461+
1462+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY CUBE(device, sensor) ORDER BY device, sensor;
1463+
--- QUERY PLAN ---
1464+
Sort
1465+
Sort Key: metrics.device, metrics.sensor
1466+
-> MixedAggregate
1467+
Hash Key: metrics.device, metrics.sensor
1468+
Hash Key: metrics.device
1469+
Hash Key: metrics.sensor
1470+
Group Key: ()
1471+
-> Append
1472+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
1473+
-> Seq Scan on compress_hyper_2_2_chunk
1474+
-> Seq Scan on _hyper_1_1_chunk
1475+
1476+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY GROUPING SETS ((device), (sensor), ()) ORDER BY device, sensor;
1477+
--- QUERY PLAN ---
1478+
Sort
1479+
Sort Key: metrics.device, metrics.sensor
1480+
-> MixedAggregate
1481+
Hash Key: metrics.device
1482+
Hash Key: metrics.sensor
1483+
Group Key: ()
1484+
-> Append
1485+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
1486+
-> Seq Scan on compress_hyper_2_2_chunk
1487+
-> Seq Scan on _hyper_1_1_chunk
1488+
14151489
\set PREFIX ''
14161490
\set ECHO errors
14171491
--- Unoptimized results
@@ -2360,6 +2434,50 @@ SELECT device, max(time), min(time) FROM metrics GROUP BY device ORDER BY 1,2,3;
23602434
-> Custom Scan (ColumnarScan) on _hyper_1_3_chunk
23612435
-> Index Scan using compress_hyper_2_4_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_4_chunk
23622436

2437+
-- GROUPING SETS / ROLLUP / CUBE are not supported by ColumnarIndexScan
2438+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY ROLLUP(device, sensor) ORDER BY device, sensor;
2439+
--- QUERY PLAN ---
2440+
GroupAggregate
2441+
Group Key: metrics.device, metrics.sensor
2442+
Group Key: metrics.device
2443+
Group Key: ()
2444+
-> Merge Append
2445+
Sort Key: metrics.device, metrics.sensor
2446+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
2447+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
2448+
-> Custom Scan (ColumnarScan) on _hyper_1_3_chunk
2449+
-> Index Scan using compress_hyper_2_4_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_4_chunk
2450+
2451+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY CUBE(device, sensor) ORDER BY device, sensor;
2452+
--- QUERY PLAN ---
2453+
Sort
2454+
Sort Key: metrics.device, metrics.sensor
2455+
-> MixedAggregate
2456+
Hash Key: metrics.sensor
2457+
Group Key: metrics.device, metrics.sensor
2458+
Group Key: metrics.device
2459+
Group Key: ()
2460+
-> Merge Append
2461+
Sort Key: metrics.device, metrics.sensor
2462+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
2463+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
2464+
-> Custom Scan (ColumnarScan) on _hyper_1_3_chunk
2465+
-> Index Scan using compress_hyper_2_4_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_4_chunk
2466+
2467+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY GROUPING SETS ((device), (sensor), ()) ORDER BY device, sensor;
2468+
--- QUERY PLAN ---
2469+
Sort
2470+
Sort Key: metrics.device, metrics.sensor
2471+
-> MixedAggregate
2472+
Hash Key: metrics.device
2473+
Hash Key: metrics.sensor
2474+
Group Key: ()
2475+
-> Append
2476+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
2477+
-> Seq Scan on compress_hyper_2_2_chunk
2478+
-> Custom Scan (ColumnarScan) on _hyper_1_3_chunk
2479+
-> Seq Scan on compress_hyper_2_4_chunk
2480+
23632481
\set PREFIX ''
23642482
\set ECHO errors
23652483
--- Unoptimized results

tsl/test/expected/columnar_index_scan-16.out

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,39 @@ SELECT device, max(time), min(time) FROM metrics GROUP BY device ORDER BY 1,2,3;
539539
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
540540
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
541541

542+
-- GROUPING SETS / ROLLUP / CUBE are not supported by ColumnarIndexScan
543+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY ROLLUP(device, sensor) ORDER BY device, sensor;
544+
--- QUERY PLAN ---
545+
GroupAggregate
546+
Group Key: _hyper_1_1_chunk.device, _hyper_1_1_chunk.sensor
547+
Group Key: _hyper_1_1_chunk.device
548+
Group Key: ()
549+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
550+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
551+
552+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY CUBE(device, sensor) ORDER BY device, sensor;
553+
--- QUERY PLAN ---
554+
Sort
555+
Sort Key: _hyper_1_1_chunk.device, _hyper_1_1_chunk.sensor
556+
-> MixedAggregate
557+
Hash Key: _hyper_1_1_chunk.sensor
558+
Group Key: _hyper_1_1_chunk.device, _hyper_1_1_chunk.sensor
559+
Group Key: _hyper_1_1_chunk.device
560+
Group Key: ()
561+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
562+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
563+
564+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY GROUPING SETS ((device), (sensor), ()) ORDER BY device, sensor;
565+
--- QUERY PLAN ---
566+
Sort
567+
Sort Key: _hyper_1_1_chunk.device, _hyper_1_1_chunk.sensor
568+
-> MixedAggregate
569+
Hash Key: _hyper_1_1_chunk.sensor
570+
Group Key: _hyper_1_1_chunk.device
571+
Group Key: ()
572+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
573+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
574+
542575
\set PREFIX ''
543576
\set ECHO errors
544577
--- Unoptimized results
@@ -1413,6 +1446,47 @@ SELECT device, max(time), min(time) FROM metrics GROUP BY device ORDER BY 1,2,3;
14131446
Group Key: _hyper_1_1_chunk.device
14141447
-> Seq Scan on _hyper_1_1_chunk
14151448

1449+
-- GROUPING SETS / ROLLUP / CUBE are not supported by ColumnarIndexScan
1450+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY ROLLUP(device, sensor) ORDER BY device, sensor;
1451+
--- QUERY PLAN ---
1452+
Sort
1453+
Sort Key: metrics.device, metrics.sensor
1454+
-> MixedAggregate
1455+
Hash Key: metrics.device, metrics.sensor
1456+
Hash Key: metrics.device
1457+
Group Key: ()
1458+
-> Append
1459+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
1460+
-> Seq Scan on compress_hyper_2_2_chunk
1461+
-> Seq Scan on _hyper_1_1_chunk
1462+
1463+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY CUBE(device, sensor) ORDER BY device, sensor;
1464+
--- QUERY PLAN ---
1465+
Sort
1466+
Sort Key: metrics.device, metrics.sensor
1467+
-> MixedAggregate
1468+
Hash Key: metrics.device, metrics.sensor
1469+
Hash Key: metrics.device
1470+
Hash Key: metrics.sensor
1471+
Group Key: ()
1472+
-> Append
1473+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
1474+
-> Seq Scan on compress_hyper_2_2_chunk
1475+
-> Seq Scan on _hyper_1_1_chunk
1476+
1477+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY GROUPING SETS ((device), (sensor), ()) ORDER BY device, sensor;
1478+
--- QUERY PLAN ---
1479+
Sort
1480+
Sort Key: metrics.device, metrics.sensor
1481+
-> MixedAggregate
1482+
Hash Key: metrics.device
1483+
Hash Key: metrics.sensor
1484+
Group Key: ()
1485+
-> Append
1486+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
1487+
-> Seq Scan on compress_hyper_2_2_chunk
1488+
-> Seq Scan on _hyper_1_1_chunk
1489+
14161490
\set PREFIX ''
14171491
\set ECHO errors
14181492
--- Unoptimized results
@@ -2362,6 +2436,50 @@ SELECT device, max(time), min(time) FROM metrics GROUP BY device ORDER BY 1,2,3;
23622436
-> Custom Scan (ColumnarScan) on _hyper_1_3_chunk
23632437
-> Index Scan using compress_hyper_2_4_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_4_chunk
23642438

2439+
-- GROUPING SETS / ROLLUP / CUBE are not supported by ColumnarIndexScan
2440+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY ROLLUP(device, sensor) ORDER BY device, sensor;
2441+
--- QUERY PLAN ---
2442+
GroupAggregate
2443+
Group Key: metrics.device, metrics.sensor
2444+
Group Key: metrics.device
2445+
Group Key: ()
2446+
-> Merge Append
2447+
Sort Key: metrics.device, metrics.sensor
2448+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
2449+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
2450+
-> Custom Scan (ColumnarScan) on _hyper_1_3_chunk
2451+
-> Index Scan using compress_hyper_2_4_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_4_chunk
2452+
2453+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY CUBE(device, sensor) ORDER BY device, sensor;
2454+
--- QUERY PLAN ---
2455+
Sort
2456+
Sort Key: metrics.device, metrics.sensor
2457+
-> MixedAggregate
2458+
Hash Key: metrics.sensor
2459+
Group Key: metrics.device, metrics.sensor
2460+
Group Key: metrics.device
2461+
Group Key: ()
2462+
-> Merge Append
2463+
Sort Key: metrics.device, metrics.sensor
2464+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
2465+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_2_chunk
2466+
-> Custom Scan (ColumnarScan) on _hyper_1_3_chunk
2467+
-> Index Scan using compress_hyper_2_4_chunk_device_sensor__ts_meta_min_1__ts_m_idx on compress_hyper_2_4_chunk
2468+
2469+
:PREFIX SELECT device, sensor, count(*) FROM metrics GROUP BY GROUPING SETS ((device), (sensor), ()) ORDER BY device, sensor;
2470+
--- QUERY PLAN ---
2471+
Sort
2472+
Sort Key: metrics.device, metrics.sensor
2473+
-> MixedAggregate
2474+
Hash Key: metrics.device
2475+
Hash Key: metrics.sensor
2476+
Group Key: ()
2477+
-> Append
2478+
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
2479+
-> Seq Scan on compress_hyper_2_2_chunk
2480+
-> Custom Scan (ColumnarScan) on _hyper_1_3_chunk
2481+
-> Seq Scan on compress_hyper_2_4_chunk
2482+
23652483
\set PREFIX ''
23662484
\set ECHO errors
23672485
--- Unoptimized results

0 commit comments

Comments
 (0)