Skip to content

perf: Optimize logical optimizer's OptimizeProjections pass#21726

Open
neilconway wants to merge 4 commits intoapache:mainfrom
neilconway:neilc/perf-planner-optimize-projections
Open

perf: Optimize logical optimizer's OptimizeProjections pass#21726
neilconway wants to merge 4 commits intoapache:mainfrom
neilconway:neilc/perf-planner-optimize-projections

Conversation

@neilconway
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Some profiling suggested that OptimizeProjections was among the most heavyweight of the logical optimizer passes for TPC-DS. This PR implements two distinct optimizations:

  1. In RequiredIndices::add_expr, the previous implementation created a HashSet and walked the expression tree twice, adding reference columns to the HashSet. Finally, members of the HashSet were converted to indices. It is faster to just walk the expression tree once ourselves and convert column references to indices. This saves the HashSet allocation and insertions, plus one redundant tree walk.

  2. In optimize_projections, we computed the minimal required set of GROUP BY columns, based on functional dependencies. This was relatively expensive; when there are no functional dependencies (common), this was still quite expensive but will always be a no-op. Add a short-circuit to skip the redundant computation in this scenario.

Results on a newly added optimize_projections microbenchmark:

  - tpch_q3: 14.6 µs → 11.9 µs (−18.5%)
  - tpch_q5: 17.4 µs → 14.0 µs (−19.4%)
  - clickbench_groupby: 10.3 µs → 6.8 µs (−34.1%)
  - tpcds_subquery: 11.2 µs → 8.7 µs (−22.1%)
  - small_schema: 1.87 µs → 1.68 µs (−10.3%)

What changes are included in this PR?

  • Add microbenchmark for optimize_projections
  • Implement two optimizations

Are these changes tested?

Yes.

Are there any user-facing changes?

No.

@github-actions github-actions bot added the optimizer Optimizer rules label Apr 18, 2026
@neilconway neilconway marked this pull request as ready for review April 18, 2026 21:52
@neilconway
Copy link
Copy Markdown
Contributor Author

run benchmark sql_planner

@adriangbot
Copy link
Copy Markdown

🤖 Criterion benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c4274629729-1487-z6rbb 6.12.55+ #1 SMP Sun Feb 1 08:59:41 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing neilc/perf-planner-optimize-projections (8788049) to b75df6f (merge-base) diff
BENCH_NAME=sql_planner
BENCH_COMMAND=cargo bench --features=parquet --bench sql_planner
BENCH_FILTER=
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot
Copy link
Copy Markdown

🤖 Criterion benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                 main                                   neilc_perf-planner-optimize-projections
-----                                                 ----                                   ---------------------------------------
logical_aggregate_with_join                           1.00    437.3±1.30µs        ? ?/sec    1.00    436.6±1.05µs        ? ?/sec
logical_plan_struct_join_agg_sort                     1.03    169.0±0.92µs        ? ?/sec    1.00    163.3±0.57µs        ? ?/sec
logical_select_all_from_1000                          1.00      8.4±0.05ms        ? ?/sec    1.00      8.4±0.03ms        ? ?/sec
logical_select_one_from_700                           1.00    321.9±0.96µs        ? ?/sec    1.00    321.4±1.88µs        ? ?/sec
logical_trivial_join_high_numbered_columns            1.00    266.2±0.94µs        ? ?/sec    1.01    268.1±0.53µs        ? ?/sec
logical_trivial_join_low_numbered_columns             1.00    253.9±0.78µs        ? ?/sec    1.00    254.9±0.74µs        ? ?/sec
physical_intersection                                 1.02    596.1±2.07µs        ? ?/sec    1.00    584.3±1.57µs        ? ?/sec
physical_join_consider_sort                           1.00   1024.8±2.05µs        ? ?/sec    1.00   1023.4±2.52µs        ? ?/sec
physical_join_distinct                                1.00    248.9±0.69µs        ? ?/sec    1.00    248.6±0.57µs        ? ?/sec
physical_many_self_joins                              1.00      7.8±0.02ms        ? ?/sec    1.00      7.9±0.01ms        ? ?/sec
physical_plan_clickbench_all                          1.02    132.5±0.75ms        ? ?/sec    1.00    129.4±0.45ms        ? ?/sec
physical_plan_clickbench_q1                           1.03  1399.2±23.56µs        ? ?/sec    1.00   1363.1±6.05µs        ? ?/sec
physical_plan_clickbench_q10                          1.02      2.1±0.01ms        ? ?/sec    1.00      2.0±0.01ms        ? ?/sec
physical_plan_clickbench_q11                          1.02      2.3±0.01ms        ? ?/sec    1.00      2.2±0.01ms        ? ?/sec
physical_plan_clickbench_q12                          1.02      2.4±0.01ms        ? ?/sec    1.00      2.3±0.01ms        ? ?/sec
physical_plan_clickbench_q13                          1.02      2.1±0.01ms        ? ?/sec    1.00      2.1±0.01ms        ? ?/sec
physical_plan_clickbench_q14                          1.02      2.3±0.01ms        ? ?/sec    1.00      2.2±0.01ms        ? ?/sec
physical_plan_clickbench_q15                          1.02      2.2±0.01ms        ? ?/sec    1.00      2.1±0.01ms        ? ?/sec
physical_plan_clickbench_q16                          1.01  1783.3±10.63µs        ? ?/sec    1.00   1762.8±6.14µs        ? ?/sec
physical_plan_clickbench_q17                          1.01  1831.2±10.50µs        ? ?/sec    1.00  1819.8±10.16µs        ? ?/sec
physical_plan_clickbench_q18                          1.01  1669.8±17.77µs        ? ?/sec    1.00   1657.9±8.26µs        ? ?/sec
physical_plan_clickbench_q19                          1.02      2.1±0.02ms        ? ?/sec    1.00      2.0±0.01ms        ? ?/sec
physical_plan_clickbench_q2                           1.02   1819.1±9.92µs        ? ?/sec    1.00   1791.5±6.26µs        ? ?/sec
physical_plan_clickbench_q20                          1.00   1538.9±6.06µs        ? ?/sec    1.01   1547.3±5.71µs        ? ?/sec
physical_plan_clickbench_q21                          1.00   1815.3±5.81µs        ? ?/sec    1.00   1806.9±4.58µs        ? ?/sec
physical_plan_clickbench_q22                          1.01      2.3±0.01ms        ? ?/sec    1.00      2.2±0.02ms        ? ?/sec
physical_plan_clickbench_q23                          1.01      2.5±0.01ms        ? ?/sec    1.00      2.4±0.01ms        ? ?/sec
physical_plan_clickbench_q24                          1.00      5.8±0.02ms        ? ?/sec    1.01      5.8±0.04ms        ? ?/sec
physical_plan_clickbench_q25                          1.01   1948.2±5.27µs        ? ?/sec    1.00   1927.3±4.40µs        ? ?/sec
physical_plan_clickbench_q26                          1.01   1762.2±9.17µs        ? ?/sec    1.00   1746.1±5.59µs        ? ?/sec
physical_plan_clickbench_q27                          1.02   1966.3±5.00µs        ? ?/sec    1.00   1933.0±5.71µs        ? ?/sec
physical_plan_clickbench_q28                          1.02      2.5±0.01ms        ? ?/sec    1.00      2.4±0.01ms        ? ?/sec
physical_plan_clickbench_q29                          1.00      2.7±0.01ms        ? ?/sec    1.02      2.7±0.02ms        ? ?/sec
physical_plan_clickbench_q3                           1.04  1703.9±57.97µs        ? ?/sec    1.00   1637.9±6.99µs        ? ?/sec
physical_plan_clickbench_q30                          1.01     16.9±0.03ms        ? ?/sec    1.00     16.7±0.04ms        ? ?/sec
physical_plan_clickbench_q31                          1.04      2.6±0.01ms        ? ?/sec    1.00      2.5±0.01ms        ? ?/sec
physical_plan_clickbench_q32                          1.02      2.5±0.01ms        ? ?/sec    1.00      2.5±0.02ms        ? ?/sec
physical_plan_clickbench_q33                          1.03      2.1±0.02ms        ? ?/sec    1.00      2.0±0.01ms        ? ?/sec
physical_plan_clickbench_q34                          1.04  1824.2±13.56µs        ? ?/sec    1.00   1753.7±6.20µs        ? ?/sec
physical_plan_clickbench_q35                          1.03  1878.6±11.42µs        ? ?/sec    1.00   1825.4±6.58µs        ? ?/sec
physical_plan_clickbench_q36                          1.02      2.2±0.01ms        ? ?/sec    1.00      2.2±0.01ms        ? ?/sec
physical_plan_clickbench_q37                          1.02      2.7±0.01ms        ? ?/sec    1.00      2.6±0.02ms        ? ?/sec
physical_plan_clickbench_q38                          1.02      2.7±0.01ms        ? ?/sec    1.00      2.6±0.02ms        ? ?/sec
physical_plan_clickbench_q39                          1.02      2.8±0.01ms        ? ?/sec    1.00      2.7±0.01ms        ? ?/sec
physical_plan_clickbench_q4                           1.02   1466.6±6.36µs        ? ?/sec    1.00   1438.8±5.43µs        ? ?/sec
physical_plan_clickbench_q40                          1.01      3.5±0.02ms        ? ?/sec    1.00      3.5±0.02ms        ? ?/sec
physical_plan_clickbench_q41                          1.00      3.0±0.01ms        ? ?/sec    1.02      3.1±0.07ms        ? ?/sec
physical_plan_clickbench_q42                          1.00      3.2±0.01ms        ? ?/sec    1.02      3.3±0.03ms        ? ?/sec
physical_plan_clickbench_q43                          1.05      3.4±0.08ms        ? ?/sec    1.00      3.3±0.01ms        ? ?/sec
physical_plan_clickbench_q44                          1.02   1561.6±6.56µs        ? ?/sec    1.00   1529.4±6.09µs        ? ?/sec
physical_plan_clickbench_q45                          1.01   1552.3±6.49µs        ? ?/sec    1.00   1532.3±5.92µs        ? ?/sec
physical_plan_clickbench_q46                          1.02  1891.0±10.22µs        ? ?/sec    1.00   1862.4±6.22µs        ? ?/sec
physical_plan_clickbench_q47                          1.02      2.7±0.02ms        ? ?/sec    1.00      2.6±0.01ms        ? ?/sec
physical_plan_clickbench_q48                          1.03      2.9±0.02ms        ? ?/sec    1.00      2.8±0.01ms        ? ?/sec
physical_plan_clickbench_q49                          1.03      3.0±0.02ms        ? ?/sec    1.00      2.9±0.01ms        ? ?/sec
physical_plan_clickbench_q5                           1.01  1616.0±12.52µs        ? ?/sec    1.00  1592.9±15.17µs        ? ?/sec
physical_plan_clickbench_q50                          1.03      3.0±0.02ms        ? ?/sec    1.00      2.9±0.01ms        ? ?/sec
physical_plan_clickbench_q51                          1.03      2.0±0.01ms        ? ?/sec    1.00   1963.0±6.23µs        ? ?/sec
physical_plan_clickbench_q6                           1.02  1660.9±19.24µs        ? ?/sec    1.00  1624.7±13.22µs        ? ?/sec
physical_plan_clickbench_q7                           1.03   1656.0±9.63µs        ? ?/sec    1.00   1604.6±6.07µs        ? ?/sec
physical_plan_clickbench_q8                           1.02  1984.2±10.47µs        ? ?/sec    1.00   1944.3±6.28µs        ? ?/sec
physical_plan_clickbench_q9                           1.02  1955.2±10.36µs        ? ?/sec    1.00   1909.1±7.16µs        ? ?/sec
physical_plan_struct_join_agg_sort                    1.01   1367.5±2.58µs        ? ?/sec    1.00   1355.2±4.03µs        ? ?/sec
physical_plan_tpcds_all                               1.01    768.2±5.30ms        ? ?/sec    1.00    760.0±4.83ms        ? ?/sec
physical_plan_tpch_all                                1.01     49.3±0.16ms        ? ?/sec    1.00     48.8±0.10ms        ? ?/sec
physical_plan_tpch_q1                                 1.01   1586.5±3.31µs        ? ?/sec    1.00   1574.6±2.28µs        ? ?/sec
physical_plan_tpch_q10                                1.02      3.1±0.02ms        ? ?/sec    1.00      3.0±0.01ms        ? ?/sec
physical_plan_tpch_q11                                1.03      2.6±0.01ms        ? ?/sec    1.00      2.5±0.02ms        ? ?/sec
physical_plan_tpch_q12                                1.01   1317.7±3.86µs        ? ?/sec    1.00   1309.8±3.15µs        ? ?/sec
physical_plan_tpch_q13                                1.01    991.8±2.87µs        ? ?/sec    1.00    986.8±2.10µs        ? ?/sec
physical_plan_tpch_q14                                1.00   1436.1±3.89µs        ? ?/sec    1.00   1432.4±2.29µs        ? ?/sec
physical_plan_tpch_q16                                1.01   1668.2±2.59µs        ? ?/sec    1.00   1646.1±3.71µs        ? ?/sec
physical_plan_tpch_q17                                1.02   1825.8±3.09µs        ? ?/sec    1.00   1783.0±3.23µs        ? ?/sec
physical_plan_tpch_q18                                1.02   1917.8±3.72µs        ? ?/sec    1.00   1887.4±2.66µs        ? ?/sec
physical_plan_tpch_q19                                1.02      2.5±0.00ms        ? ?/sec    1.00      2.5±0.00ms        ? ?/sec
physical_plan_tpch_q2                                 1.01      4.4±0.01ms        ? ?/sec    1.00      4.4±0.02ms        ? ?/sec
physical_plan_tpch_q20                                1.02      2.4±0.00ms        ? ?/sec    1.00      2.4±0.00ms        ? ?/sec
physical_plan_tpch_q21                                1.01      3.2±0.01ms        ? ?/sec    1.00      3.2±0.01ms        ? ?/sec
physical_plan_tpch_q22                                1.02      2.1±0.00ms        ? ?/sec    1.00      2.0±0.00ms        ? ?/sec
physical_plan_tpch_q3                                 1.02      2.0±0.00ms        ? ?/sec    1.00      2.0±0.00ms        ? ?/sec
physical_plan_tpch_q4                                 1.01   1165.6±2.15µs        ? ?/sec    1.00   1150.2±4.70µs        ? ?/sec
physical_plan_tpch_q5                                 1.01      2.7±0.01ms        ? ?/sec    1.00      2.6±0.01ms        ? ?/sec
physical_plan_tpch_q6                                 1.02    669.0±2.62µs        ? ?/sec    1.00    654.4±1.53µs        ? ?/sec
physical_plan_tpch_q7                                 1.02      3.3±0.03ms        ? ?/sec    1.00      3.3±0.02ms        ? ?/sec
physical_plan_tpch_q8                                 1.01      4.3±0.04ms        ? ?/sec    1.00      4.3±0.03ms        ? ?/sec
physical_plan_tpch_q9                                 1.01      3.0±0.01ms        ? ?/sec    1.00      2.9±0.01ms        ? ?/sec
physical_select_aggregates_from_200                   1.01     14.7±0.03ms        ? ?/sec    1.00     14.6±0.02ms        ? ?/sec
physical_select_all_from_1000                         1.01     18.4±0.08ms        ? ?/sec    1.00     18.2±0.08ms        ? ?/sec
physical_select_one_from_700                          1.01    799.2±4.08µs        ? ?/sec    1.00    791.7±2.32µs        ? ?/sec
physical_sorted_union_order_by_10_int64               1.00      4.8±0.01ms        ? ?/sec    1.00      4.8±0.02ms        ? ?/sec
physical_sorted_union_order_by_10_uint64              1.00     11.7±0.04ms        ? ?/sec    1.00     11.7±0.03ms        ? ?/sec
physical_sorted_union_order_by_50_int64               1.00    116.1±0.32ms        ? ?/sec    1.01    116.8±0.43ms        ? ?/sec
physical_sorted_union_order_by_50_uint64              1.00    623.4±5.51ms        ? ?/sec    1.00    623.4±4.70ms        ? ?/sec
physical_theta_join_consider_sort                     1.00   1059.7±2.61µs        ? ?/sec    1.00   1058.7±2.76µs        ? ?/sec
physical_unnest_to_join                               1.01   1234.2±2.55µs        ? ?/sec    1.00   1227.8±4.31µs        ? ?/sec
physical_window_function_partition_by_12_on_values    1.02    748.7±1.74µs        ? ?/sec    1.00    733.0±1.82µs        ? ?/sec
physical_window_function_partition_by_30_on_values    1.02   1505.9±3.77µs        ? ?/sec    1.00   1483.3±3.54µs        ? ?/sec
physical_window_function_partition_by_4_on_values     1.03    457.8±1.79µs        ? ?/sec    1.00    445.6±1.46µs        ? ?/sec
physical_window_function_partition_by_7_on_values     1.02    557.4±1.28µs        ? ?/sec    1.00    544.1±1.04µs        ? ?/sec
physical_window_function_partition_by_8_on_values     1.02    599.8±1.92µs        ? ?/sec    1.00    585.6±1.22µs        ? ?/sec
with_param_values_many_columns                        1.00    464.6±2.15µs        ? ?/sec    1.00    464.1±2.11µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 1240.7s
Peak memory 18.5 GiB
Avg memory 18.5 GiB
CPU user 1484.0s
CPU sys 1.7s
Peak spill 0 B

branch

Metric Value
Wall time 1254.2s
Peak memory 18.5 GiB
Avg memory 18.5 GiB
CPU user 1498.4s
CPU sys 1.3s
Peak spill 0 B

File an issue against this benchmark runner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize OptimizeProjections in the logical query optimizer

2 participants