Skip to content

fix: keep probe-side constants in join equivalence properties - #26459

Merged
jayzhan211 merged 5 commits into
apache:mainfrom
jayzhan211:ensure-requirements-unproven-ordering
Oct 23, 2026
Merged

jayzhan211 merged 5 commits into
apache:mainfrom
jayzhan211:ensure-requirements-unproven-ordering

Conversation

@jayzhan211

@jayzhan211 jayzhan211 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

A query that orders by a column that is constant on the probe side of a join fails to plan:

SanityCheckPlan
caused by
Error during planning: Plan: ["SortPreservingMergeExec: [k@0 DESC]", "  HashJoinExec: ..."] does not satisfy order requirements: [k@0 DESC]. Child-0 order: []

EquivalenceClass::try_with_offset builds a new class from the expressions and does not copy constant. EquivalenceGroup::join uses it for every class of the right side, so the join output forgets that k is constant. EnsureRequirements removes the sort because k is constant below the join, and SanityCheckPlan then rejects the plan because the join output no longer says so.

What changes are included in this PR?

  • try_with_offset keeps the class's constant value.
  • EquivalenceGroup::join clears the right side's constants for Left and Full joins. Unmatched rows pad that side with NULLs, so a constant there no longer holds. Without this, copying the constant would drop a sort that is needed.

The first version of this PR added a sort in EnsureRequirements wherever the requirement could not be proven. Following review, it now fixes the join instead. The plan for #26283 then has no sort at all, instead of a sort on a constant column.

The same problem exists the other way round: the left side's constants are kept for Right and Full joins, where that side is padded with NULLs too. For example, this returns 5 before the NULLs:

CREATE TABLE t(a INT) AS VALUES (1), (2), (3);
CREATE TABLE u(a INT) AS VALUES (9), (10), (1);
SELECT s.k, u.a FROM (SELECT a, 5 AS k FROM t) s RIGHT JOIN u ON s.a = u.a
ORDER BY s.k DESC NULLS FIRST;

This PR does not change the left side, because #26042 already rebuilds the classes of the side that is padded with NULLs, which drops these constants as well.

What is the testing strategy for this PR?

  • class.rs: a unit test that a right-side constant stays constant after Inner and Right joins, and not after Left and Full joins.
  • order.slt: both queries from SanityCheckPlan error when ordering by a literal column from the probe side of a hash join #26283, with results and EXPLAIN. Without the change, both fail to plan.
  • order.slt: a LEFT JOIN where the probe-side constant is padded with NULLs. If the constant is not cleared, the sort on it is dropped and the rows come back in the wrong order.

Are there any user-facing changes?

Queries like the one in #26283 plan and run instead of failing. There are no API changes.

@github-actions github-actions Bot added optimizer Optimizer rules core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Oct 18, 2026
@jayzhan211
jayzhan211 marked this pull request as ready for review October 18, 2026 03:12
@codecov-commenter

codecov-commenter commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.93%. Comparing base (290ad15) to head (3319346).
⚠️ Report is 38 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/physical-expr/src/equivalence/class.rs 87.50% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #26459      +/-   ##
==========================================
+ Coverage   82.89%   82.93%   +0.03%     
==========================================
  Files        1154     1154              
  Lines      457656   458025     +369     
  Branches   457656   458025     +369     
==========================================
+ Hits       379391   379865     +474     
+ Misses      55261    55130     -131     
- Partials    23004    23030      +26     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kumarUjjawal kumarUjjawal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jayzhan211

Left few comments please take a look if they make sense.

Comment on lines +79 to +81
//! that forgets a probe-side column is constant). Phase 3 then adds a sort
//! where the requirement cannot be proven, so the rule never emits a plan
//! that it cannot show to be valid.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qualify the guarantee that all ordering requirements are proven

For contradictory equivalence properties, add_sort_for_unproven_ordering deliberately leaves an unsatisfied requirement unchanged when even the candidate sort cannot satisfy it. This contradicts the new claim that the rule never emits an unprovable plan. Document this exception here, consistent with the helper's documentation, so callers do not mistake this pass for successful validation.

Comment on lines +270 to +281
let sort_reqs: Vec<_> = requirement.clone().into();
let sort_exprs = sort_reqs
.into_iter()
.filter(|req| eq_properties.is_expr_constant(&req.expr).is_none())
.map(Into::into);
let Some(ordering) = LexOrdering::new(sort_exprs) else {
continue;
};
let sort: Arc<dyn ExecutionPlan> = Arc::new(
SortExec::new(ordering, Arc::clone(child)).with_preserve_partitioning(
child.output_partitioning().partition_count() > 1,
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Share the existing sort-construction logic

The constant-key filtering, requirement conversion, and partition-preservation logic duplicate add_sort_above and add_sort_above_with_distribution in utils.rs. Adding another implementation means future fixes to these behaviors must reach three separate paths. Extract a shared constructor that accepts an execution plan, while retaining this helper's additional satisfaction check.

Comment on lines +283 to +287
if !sort
.equivalence_properties()
.ordering_satisfy_requirement(requirement)?
{
continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the fallback when a sort cannot prove the requirement

The added tests exercise successful repair, but none covers this explicit fallback for contradictory properties. Add a focused regression asserting that an unsatisfying candidate sort is not installed and that SanityCheckPlan still rejects the unchanged requirement. Otherwise, removing this guard would not be caught despite changing a documented safety behavior.


// 3d: Partial sort
adjusted
// 3d: Sort where an ordering requirement cannot be proven

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix patches the symptom in EnsureRequirements, not the cause: EquivalenceClass::try_with_offset (physical-expr/src/equivalence/class.rs:249) starts from Self::default() and never copies constant, so EquivalenceGroup::join drops every probe-side (right) constant, and is_trivial() then deletes the single-member class.

@jayzhan211 jayzhan211 Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying the constant in try_with_offset fixes both queries, and the plan no longer needs a sort on k at all. One caveat: for Left and Full joins, unmatched rows pad the right side with NULLs, so its constants have to be cleared there, or a sort that is needed gets dropped. There is a test for that case now. The PR now fixes the join instead of adding the EnsureRequirements step.

The left side has the same problem for Right and Full joins: its constants survive the NULL padding and can give wrongly ordered results. #26042 already rebuilds the classes of the side padded with NULLs, which covers that, so I left the left side alone here and noted it in the description.

// 3d: Sort where an ordering requirement cannot be proven
use super::enforce_sorting::add_sort_for_unproven_ordering;
let plan = adjusted
.plan

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_sort_for_unproven_ordering runs after parallelize_sorts (3a) and replace_with_order_preserving_variants (3b), so a sort it adds never gets those rewrites.

if eq_properties.ordering_satisfy_requirement(requirement.clone())? {
continue;
}
let sort_reqs: Vec<_> = requirement.clone().into();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This re-implements add_sort_above / add_sort_above_with_check from physical-optimizer/src/utils.rs:38-128: the same constant-key filter, LexOrdering::new None check, and preserve_partitioning-if-multi-partition logic.

changed = true;
}
if changed {
plan.replace_children(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After replace_children(Recompute), nodes such as AggregateExec and BoundedWindowAggExec recompute required_input_ordering (ordered_partition_by_indices, the soft aggregate requirement) from the new child, but the new node's requirement is never checked again.

let Some(ordering) = LexOrdering::new(sort_exprs) else {
continue;
};
let sort: Arc<dyn ExecutionPlan> = Arc::new(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new SortExec is always stacked on top of the child, even when the child is already a SortExec (without fetch) whose ordering is wrong or too weak, and the misplaced sort left lower in the plan by pushdown_sorts is never removed.

continue;
};
let sort: Arc<dyn ExecutionPlan> = Arc::new(
SortExec::new(ordering, Arc::clone(child)).with_preserve_partitioning(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The added SortExec never carries a fetch, even when the requiring parent (SortPreservingMergeExec / OutputRequirementExec) has one or the sort that pushdown_sorts removed was a TopK.

SET datafusion.execution.target_partitions = 1;

query ITI
SELECT s.a, s.k, row_number() OVER (PARTITION BY s.a ORDER BY s.k) AS r

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second regression query (window over a probe-side constant at target_partitions=1) only checks results, with no EXPLAIN. The Rust test only checks idempotency up to EnsureRequirements and never runs SanityCheckPlan or the later rules.

@jayzhan211 jayzhan211 Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The window query has an EXPLAIN now. The Rust test went away with the step it tested. A unit test in class.rs covers the join change, and the slt queries run the whole optimizer, SanityCheckPlan included.

@zhuqi-lucas zhuqi-lucas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jayzhan211! One thing not covered above: the new pass runs ordering_satisfy_requirement on every node with an ordering requirement for every query, while it only fires in these rare cases. Could you run the sql_planner benchmark to check the planning-time cost?

@jayzhan211 jayzhan211 changed the title fix: keep a sort in EnsureRequirements when the ordering cannot be proven fix: keep probe-side constants in join equivalence properties Oct 20, 2026
@jayzhan211

Copy link
Copy Markdown
Contributor Author

Could you run the sql_planner benchmark to check the planning-time cost?

I ran it for the first version (the table was in the description, all within noise). That pass is gone now. The PR only copies one field per equivalence class when a join builds its properties, so I don't expect a measurable planning cost. I'm happy to rerun it if you'd like numbers for this version.

@jayzhan211

Copy link
Copy Markdown
Contributor Author

run benchmark sql_planner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c6356917032-3786-jmgct 6.12.105+ #1 SMP Sat Sep 5 07:54:11 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 ensure-requirements-unproven-ordering (3319346) to 290ad15 (merge-base) diff

Run configuration
run benchmark sql_planner

Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

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

Comparing ensure-requirements-unproven-ordering (3319346) to 290ad15 (merge-base) diff

Run configuration
run benchmark sql_planner
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                                                 HEAD                                   ensure-requirements-unproven-ordering
-----                                                 ----                                   -------------------------------------
logical_aggregate_with_join                           1.00    342.7±1.89µs        ? ?/sec    1.02    349.5±7.91µs        ? ?/sec
logical_correlated_subquery_exists                    1.00    202.2±0.74µs        ? ?/sec    1.00    201.9±0.59µs        ? ?/sec
logical_correlated_subquery_in                        1.00    204.1±1.34µs        ? ?/sec    1.00    205.1±1.44µs        ? ?/sec
logical_distinct_many_columns                         1.00    287.4±0.82µs        ? ?/sec    1.00    288.1±1.64µs        ? ?/sec
logical_join_4_with_agg_and_filter                    1.00    188.8±1.37µs        ? ?/sec    1.01    190.5±1.23µs        ? ?/sec
logical_join_8_with_agg_sort_limit                    1.00    333.2±0.78µs        ? ?/sec    1.00    333.8±1.29µs        ? ?/sec
logical_join_chain_16                                 1.01    614.7±2.33µs        ? ?/sec    1.00    609.7±1.55µs        ? ?/sec
logical_join_chain_4                                  1.00     72.2±0.19µs        ? ?/sec    1.02     73.5±0.24µs        ? ?/sec
logical_join_chain_8                                  1.00    196.6±0.50µs        ? ?/sec    1.00    196.9±0.40µs        ? ?/sec
logical_multiple_subqueries                           1.01    383.5±1.71µs        ? ?/sec    1.00    381.0±1.15µs        ? ?/sec
logical_nested_cte_4_levels                           1.01    192.6±1.19µs        ? ?/sec    1.00    191.2±0.84µs        ? ?/sec
logical_plan_struct_join_agg_sort                     1.00    120.0±0.67µs        ? ?/sec    1.01    121.7±2.61µs        ? ?/sec
logical_plan_tpcds_all                                1.04     74.7±2.91ms        ? ?/sec    1.00     72.1±0.12ms        ? ?/sec
logical_plan_tpch_all                                 1.04      5.3±0.13ms        ? ?/sec    1.00      5.1±0.02ms        ? ?/sec
logical_scalar_subquery                               1.00    212.1±0.81µs        ? ?/sec    1.00    211.5±0.86µs        ? ?/sec
logical_select_all_from_1000                          1.00      6.9±0.02ms        ? ?/sec    1.00      6.9±0.13ms        ? ?/sec
logical_select_one_from_700                           1.00    225.6±1.64µs        ? ?/sec    1.01    228.3±2.86µs        ? ?/sec
logical_trivial_join_high_numbered_columns            1.00    205.0±1.34µs        ? ?/sec    1.02    208.8±3.60µs        ? ?/sec
logical_trivial_join_low_numbered_columns             1.00    194.5±1.26µs        ? ?/sec    1.00    193.6±1.31µs        ? ?/sec
logical_union_4_branches                              1.00    304.3±1.66µs        ? ?/sec    1.00    305.0±2.03µs        ? ?/sec
logical_union_8_branches                              1.01    618.7±4.69µs        ? ?/sec    1.00    615.1±4.85µs        ? ?/sec
logical_wide_aggregate_1000_exprs                     1.01     41.9±0.28ms        ? ?/sec    1.00     41.3±0.15ms        ? ?/sec
logical_wide_aggregate_100_exprs                      1.00   1531.7±7.81µs        ? ?/sec    1.00   1534.0±3.77µs        ? ?/sec
logical_wide_case_50_exprs                            1.00   1690.3±4.13µs        ? ?/sec    1.00   1694.3±4.65µs        ? ?/sec
logical_wide_filter_200_predicates                    1.00   1292.9±9.09µs        ? ?/sec    1.00   1294.2±4.95µs        ? ?/sec
logical_wide_filter_50_predicates                     1.00    340.6±2.11µs        ? ?/sec    1.00    339.7±1.46µs        ? ?/sec
optimizer_correlated_exists                           1.01    219.0±1.79µs        ? ?/sec    1.00    216.5±0.72µs        ? ?/sec
optimizer_join_4_with_agg_filter                      1.00    393.3±2.90µs        ? ?/sec    1.00    392.2±2.17µs        ? ?/sec
optimizer_join_chain_4                                1.01    156.0±1.47µs        ? ?/sec    1.00    154.2±0.25µs        ? ?/sec
optimizer_join_chain_8                                1.00    533.1±6.80µs        ? ?/sec    1.00    534.0±1.04µs        ? ?/sec
optimizer_select_all_from_1000                        1.03      6.9±0.12ms        ? ?/sec    1.00      6.7±0.01ms        ? ?/sec
optimizer_select_one_from_700                         1.00    237.5±1.25µs        ? ?/sec    1.00    237.1±2.28µs        ? ?/sec
optimizer_tpcds_all                                   1.02    250.5±5.95ms        ? ?/sec    1.00    245.7±0.42ms        ? ?/sec
optimizer_tpch_all                                    1.02     15.2±0.30ms        ? ?/sec    1.00     14.9±0.03ms        ? ?/sec
optimizer_wide_aggregate_100                          1.01  1751.5±25.02µs        ? ?/sec    1.00   1741.7±3.27µs        ? ?/sec
optimizer_wide_filter_200                             1.02      4.1±0.06ms        ? ?/sec    1.00      4.0±0.01ms        ? ?/sec
physical_intersection                                 1.00    504.6±2.77µs        ? ?/sec    1.00    504.8±5.38µs        ? ?/sec
physical_join_consider_sort                           1.00    953.4±1.71µs        ? ?/sec    1.00   957.8±12.59µs        ? ?/sec
physical_join_distinct                                1.00    186.9±1.34µs        ? ?/sec    1.01    188.8±2.90µs        ? ?/sec
physical_many_self_joins                              1.00      7.2±0.01ms        ? ?/sec    1.04      7.5±0.19ms        ? ?/sec
physical_plan_clickbench_all                          1.01    129.8±0.28ms        ? ?/sec    1.00    128.5±0.43ms        ? ?/sec
physical_plan_clickbench_q1                           1.00   1276.0±4.93µs        ? ?/sec    1.01   1293.5±8.56µs        ? ?/sec
physical_plan_clickbench_q10                          1.00   1822.3±5.44µs        ? ?/sec    1.02   1865.1±9.21µs        ? ?/sec
physical_plan_clickbench_q11                          1.00   1972.8±7.48µs        ? ?/sec    1.02      2.0±0.01ms        ? ?/sec
physical_plan_clickbench_q12                          1.00      2.0±0.02ms        ? ?/sec    1.03      2.1±0.01ms        ? ?/sec
physical_plan_clickbench_q13                          1.00   1865.0±6.62µs        ? ?/sec    1.03   1920.5±7.57µs        ? ?/sec
physical_plan_clickbench_q14                          1.00      2.0±0.01ms        ? ?/sec    1.00      2.0±0.01ms        ? ?/sec
physical_plan_clickbench_q15                          1.01   1948.9±6.40µs        ? ?/sec    1.00   1921.8±7.94µs        ? ?/sec
physical_plan_clickbench_q16                          1.00   1650.7±3.95µs        ? ?/sec    1.00   1645.0±7.54µs        ? ?/sec
physical_plan_clickbench_q17                          1.00   1695.4±5.14µs        ? ?/sec    1.02   1734.6±8.33µs        ? ?/sec
physical_plan_clickbench_q18                          1.00   1534.7±4.49µs        ? ?/sec    1.02   1561.1±8.00µs        ? ?/sec
physical_plan_clickbench_q19                          1.00   1866.2±6.07µs        ? ?/sec    1.02   1894.8±8.31µs        ? ?/sec
physical_plan_clickbench_q2                           1.00   1651.7±5.72µs        ? ?/sec    1.02   1684.6±8.02µs        ? ?/sec
physical_plan_clickbench_q20                          1.00   1448.7±4.10µs        ? ?/sec    1.02   1480.7±9.81µs        ? ?/sec
physical_plan_clickbench_q21                          1.00   1660.6±4.55µs        ? ?/sec    1.00  1664.0±11.62µs        ? ?/sec
physical_plan_clickbench_q22                          1.01   1999.6±2.86µs        ? ?/sec    1.00  1986.9±10.98µs        ? ?/sec
physical_plan_clickbench_q23                          1.00      2.1±0.00ms        ? ?/sec    1.01      2.1±0.01ms        ? ?/sec
physical_plan_clickbench_q24                          1.00      4.8±0.01ms        ? ?/sec    1.00      4.8±0.01ms        ? ?/sec
physical_plan_clickbench_q25                          1.00   1767.7±5.30µs        ? ?/sec    1.00   1763.3±7.46µs        ? ?/sec
physical_plan_clickbench_q26                          1.01   1642.8±4.20µs        ? ?/sec    1.00  1623.3±13.81µs        ? ?/sec
physical_plan_clickbench_q27                          1.00   1776.8±5.11µs        ? ?/sec    1.01   1802.8±7.25µs        ? ?/sec
physical_plan_clickbench_q28                          1.00      2.1±0.00ms        ? ?/sec    1.02      2.1±0.01ms        ? ?/sec
physical_plan_clickbench_q29                          1.01      2.2±0.01ms        ? ?/sec    1.00      2.2±0.01ms        ? ?/sec
physical_plan_clickbench_q3                           1.00   1536.5±3.69µs        ? ?/sec    1.01   1552.3±6.96µs        ? ?/sec
physical_plan_clickbench_q30                          1.00     12.4±0.02ms        ? ?/sec    1.00     12.3±0.02ms        ? ?/sec
physical_plan_clickbench_q31                          1.00      2.2±0.01ms        ? ?/sec    1.00      2.2±0.01ms        ? ?/sec
physical_plan_clickbench_q32                          1.00      2.2±0.01ms        ? ?/sec    1.00      2.2±0.01ms        ? ?/sec
physical_plan_clickbench_q33                          1.00   1853.2±4.43µs        ? ?/sec    1.00   1849.4±8.80µs        ? ?/sec
physical_plan_clickbench_q34                          1.00   1644.1±5.07µs        ? ?/sec    1.01   1665.5±7.78µs        ? ?/sec
physical_plan_clickbench_q35                          1.00   1704.6±4.70µs        ? ?/sec    1.00   1712.8±8.51µs        ? ?/sec
physical_plan_clickbench_q36                          1.00   1929.9±4.86µs        ? ?/sec    1.01   1952.9±9.09µs        ? ?/sec
physical_plan_clickbench_q37                          1.02      2.4±0.02ms        ? ?/sec    1.00      2.4±0.01ms        ? ?/sec
physical_plan_clickbench_q38                          1.01      2.4±0.01ms        ? ?/sec    1.00      2.4±0.01ms        ? ?/sec
physical_plan_clickbench_q39                          1.00      2.4±0.01ms        ? ?/sec    1.00      2.4±0.01ms        ? ?/sec
physical_plan_clickbench_q4                           1.00   1387.4±4.29µs        ? ?/sec    1.01   1400.2±6.89µs        ? ?/sec
physical_plan_clickbench_q40                          1.01      3.1±0.01ms        ? ?/sec    1.00      3.0±0.02ms        ? ?/sec
physical_plan_clickbench_q41                          1.00      2.8±0.01ms        ? ?/sec    1.00      2.7±0.01ms        ? ?/sec
physical_plan_clickbench_q42                          1.00      2.7±0.00ms        ? ?/sec    1.01      2.7±0.01ms        ? ?/sec
physical_plan_clickbench_q43                          1.00      2.9±0.01ms        ? ?/sec    1.02      2.9±0.01ms        ? ?/sec
physical_plan_clickbench_q44                          1.00   1469.0±3.78µs        ? ?/sec    1.00   1462.3±6.97µs        ? ?/sec
physical_plan_clickbench_q45                          1.00   1455.4±4.28µs        ? ?/sec    1.02   1480.0±7.75µs        ? ?/sec
physical_plan_clickbench_q46                          1.00   1695.9±4.44µs        ? ?/sec    1.02   1724.6±7.89µs        ? ?/sec
physical_plan_clickbench_q47                          1.00      2.2±0.01ms        ? ?/sec    1.01      2.3±0.01ms        ? ?/sec
physical_plan_clickbench_q48                          1.00      2.4±0.01ms        ? ?/sec    1.02      2.4±0.01ms        ? ?/sec
physical_plan_clickbench_q49                          1.00      2.5±0.02ms        ? ?/sec    1.01      2.5±0.02ms        ? ?/sec
physical_plan_clickbench_q5                           1.00   1479.1±3.80µs        ? ?/sec    1.01   1500.0±5.88µs        ? ?/sec
physical_plan_clickbench_q50                          1.00      2.6±0.01ms        ? ?/sec    1.01      2.6±0.02ms        ? ?/sec
physical_plan_clickbench_q51                          1.00   1820.6±5.29µs        ? ?/sec    1.02   1850.5±8.31µs        ? ?/sec
physical_plan_clickbench_q52                          1.00      2.3±0.00ms        ? ?/sec    1.01      2.3±0.01ms        ? ?/sec
physical_plan_clickbench_q53                          1.00   1636.1±6.21µs        ? ?/sec    1.01   1648.2±7.52µs        ? ?/sec
physical_plan_clickbench_q54                          1.00   1637.6±4.38µs        ? ?/sec    1.04   1701.9±8.37µs        ? ?/sec
physical_plan_clickbench_q55                          1.00   1589.7±4.03µs        ? ?/sec    1.03   1638.0±8.09µs        ? ?/sec
physical_plan_clickbench_q56                          1.00   1592.1±4.55µs        ? ?/sec    1.02   1620.5±7.62µs        ? ?/sec
physical_plan_clickbench_q57                          1.00   1742.3±5.85µs        ? ?/sec    1.00   1733.7±7.63µs        ? ?/sec
physical_plan_clickbench_q58                          1.00      2.1±0.01ms        ? ?/sec    1.01      2.1±0.01ms        ? ?/sec
physical_plan_clickbench_q59                          1.02   1622.7±3.93µs        ? ?/sec    1.00   1596.3±9.02µs        ? ?/sec
physical_plan_clickbench_q6                           1.02   1507.3±4.52µs        ? ?/sec    1.00   1476.5±6.82µs        ? ?/sec
physical_plan_clickbench_q60                          1.00   1600.5±4.98µs        ? ?/sec    1.00   1594.8±7.61µs        ? ?/sec
physical_plan_clickbench_q7                           1.01   1349.7±3.76µs        ? ?/sec    1.00  1335.9±13.06µs        ? ?/sec
physical_plan_clickbench_q8                           1.01   1837.3±5.53µs        ? ?/sec    1.00   1810.3±9.30µs        ? ?/sec
physical_plan_clickbench_q9                           1.00   1783.7±9.49µs        ? ?/sec    1.00   1777.7±8.95µs        ? ?/sec
physical_plan_struct_join_agg_sort                    1.00   1186.7±2.28µs        ? ?/sec    1.03  1223.1±76.73µs        ? ?/sec
physical_plan_tpcds_all                               1.00    557.5±0.40ms        ? ?/sec    1.00    558.3±0.81ms        ? ?/sec
physical_plan_tpch_all                                1.00     39.4±0.06ms        ? ?/sec    1.00     39.2±0.29ms        ? ?/sec
physical_plan_tpch_q1                                 1.00   1375.4±3.14µs        ? ?/sec    1.02  1402.1±66.24µs        ? ?/sec
physical_plan_tpch_q10                                1.00      2.1±0.00ms        ? ?/sec    1.02      2.1±0.04ms        ? ?/sec
physical_plan_tpch_q11                                1.00      2.1±0.00ms        ? ?/sec    1.04      2.1±0.13ms        ? ?/sec
physical_plan_tpch_q12                                1.00   1191.1±2.36µs        ? ?/sec    1.02  1218.0±79.48µs        ? ?/sec
physical_plan_tpch_q13                                1.00    953.8±1.87µs        ? ?/sec    1.01    963.0±9.56µs        ? ?/sec
physical_plan_tpch_q14                                1.03  1249.2±10.78µs        ? ?/sec    1.00   1216.3±2.17µs        ? ?/sec
physical_plan_tpch_q16                                1.04   1559.7±7.05µs        ? ?/sec    1.00   1496.2±3.82µs        ? ?/sec
physical_plan_tpch_q17                                1.01   1525.3±5.60µs        ? ?/sec    1.00   1515.9±3.55µs        ? ?/sec
physical_plan_tpch_q18                                1.01   1699.6±3.52µs        ? ?/sec    1.00   1682.1±3.37µs        ? ?/sec
physical_plan_tpch_q19                                1.02   1985.0±7.17µs        ? ?/sec    1.00   1944.0±5.20µs        ? ?/sec
physical_plan_tpch_q2                                 1.00      3.2±0.00ms        ? ?/sec    1.10      3.5±0.27ms        ? ?/sec
physical_plan_tpch_q20                                1.00   1917.6±4.22µs        ? ?/sec    1.00   1909.0±2.50µs        ? ?/sec
physical_plan_tpch_q21                                1.01      2.4±0.00ms        ? ?/sec    1.00      2.4±0.00ms        ? ?/sec
physical_plan_tpch_q22                                1.02   1482.6±4.19µs        ? ?/sec    1.00   1450.3±2.68µs        ? ?/sec
physical_plan_tpch_q3                                 1.00   1591.3±4.03µs        ? ?/sec    1.03  1641.6±50.23µs        ? ?/sec
physical_plan_tpch_q4                                 1.00   1040.7±2.82µs        ? ?/sec    1.04  1078.3±31.35µs        ? ?/sec
physical_plan_tpch_q5                                 1.00      2.1±0.01ms        ? ?/sec    1.02      2.2±0.10ms        ? ?/sec
physical_plan_tpch_q6                                 1.00    585.0±5.45µs        ? ?/sec    1.01   592.6±13.61µs        ? ?/sec
physical_plan_tpch_q7                                 1.01      2.5±0.02ms        ? ?/sec    1.00      2.4±0.00ms        ? ?/sec
physical_plan_tpch_q8                                 1.01      3.3±0.00ms        ? ?/sec    1.00      3.3±0.02ms        ? ?/sec
physical_plan_tpch_q9                                 1.00      2.5±0.00ms        ? ?/sec    1.08      2.7±0.24ms        ? ?/sec
physical_select_aggregates_from_200                   1.00      7.4±0.01ms        ? ?/sec    1.04      7.7±0.14ms        ? ?/sec
physical_select_all_from_1000                         1.00     17.0±0.09ms        ? ?/sec    1.01     17.1±0.17ms        ? ?/sec
physical_select_one_from_700                          1.00    635.4±3.67µs        ? ?/sec    1.00    637.8±9.01µs        ? ?/sec
physical_sorted_union_order_by_10_int64               1.00      3.5±0.00ms        ? ?/sec    1.05      3.7±0.14ms        ? ?/sec
physical_sorted_union_order_by_10_uint64              1.00      6.7±0.01ms        ? ?/sec    1.03      6.9±0.22ms        ? ?/sec
physical_sorted_union_order_by_50_int64               1.00     77.7±0.32ms        ? ?/sec    1.01     78.4±1.61ms        ? ?/sec
physical_sorted_union_order_by_50_uint64              1.00    252.4±0.48ms        ? ?/sec    1.06   266.8±10.50ms        ? ?/sec
physical_theta_join_consider_sort                     1.01    982.3±2.20µs        ? ?/sec    1.00    974.9±2.31µs        ? ?/sec
physical_unnest_to_join                               1.00    546.6±2.44µs        ? ?/sec    1.00    547.4±8.22µs        ? ?/sec
physical_window_function_partition_by_12_on_values    1.02    632.0±5.57µs        ? ?/sec    1.00    621.1±6.44µs        ? ?/sec
physical_window_function_partition_by_30_on_values    1.00   1208.8±9.55µs        ? ?/sec    1.00  1203.2±32.20µs        ? ?/sec
physical_window_function_partition_by_4_on_values     1.00    385.2±3.30µs        ? ?/sec    1.00    384.2±7.12µs        ? ?/sec
physical_window_function_partition_by_7_on_values     1.01    476.4±1.58µs        ? ?/sec    1.00    469.5±6.95µs        ? ?/sec
physical_window_function_partition_by_8_on_values     1.00    507.6±1.32µs        ? ?/sec    1.00    507.6±6.12µs        ? ?/sec
with_param_values_many_columns                        1.00    367.8±1.53µs        ? ?/sec    1.00    368.3±1.60µs        ? ?/sec

Resource Usage

sql_planner — base (merge-base)

Metric Value
Wall time 1845.4s
Peak memory 145.1 MiB
Avg memory 103.4 MiB
CPU user 1974.6s
CPU sys 1.4s
Peak spill 0 B

sql_planner — branch

Metric Value
Wall time 2080.5s
Peak memory 141.0 MiB
Avg memory 89.4 MiB
CPU user 1974.2s
CPU sys 1.6s
Peak spill 0 B

File an issue against this benchmark runner

@jayzhan211

Copy link
Copy Markdown
Contributor Author

sql_planner looks unchanged

@kumarUjjawal kumarUjjawal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you for the iteration.

limadog9 pushed a commit to limadog9/datafusion that referenced this pull request Oct 23, 2026
…ache#26570)

## Which issue does this PR close?

- None. This PR only adds tests, so there is no bug or feature issue to
close.

## Rationale for this change

The planner skips sorts and keeps streaming execution when it can prove
that the data already has the needed
order. It decides this from what each plan node knows about its output:
which orderings hold, which columns are
constant, and which columns are equal. Several of these decisions have
no test today:

- a union that stays sorted when each side has a cast of the sort key in
a different column,
- a constant from a filter that survives a sort with a limit or a cross
join,
- the order in which GROUP BY keys are matched against the input's
orderings,
- an order-preserving repartition over an unbounded input that keeps a
cast of its sort key sorted.

Two open PRs show why this matters. apache#26459 and apache#26042 change how
constants pass through outer joins. Cross
join derives its output facts as if it were a full outer join, so both
would make cross-join queries sort on
columns that are still constant, and no existing test fails. The
cross-join cases added here fail with either PR
merged in.

Without tests, a change to how the planner tracks these facts could add
sorts, change the aggregation mode, or
make a streaming query fail to plan, and nothing would catch it. This PR
pins the plans that main produces today.

## What changes are included in this PR?

A new `datafusion/sqllogictest/test_files/equivalence_properties.slt`,
with one comment per case that says which
planner decision it pins:

- A union whose sides each have a cast of `a` in a different column is
still known to be sorted by `a`.
- `GROUP BY d, c, b, a` over data sorted by `(a, b)` and by `c` finds
`c, a, b` sorted and runs in partially
  sorted mode on them.
- A filter constant survives an `ORDER BY ... LIMIT`, so an outer sort
that starts with that constant is removed.
- A cross join keeps the constants of its left input.
- A cross join keeps a right-side constant set by an equality filter.
- Over an unbounded input, an order-preserving repartition keeps a cast
of its sort key sorted, so a window
  partitioned by that cast needs no sort.

In `datafusion/core/tests/physical_optimizer/enforce_distribution.rs`, a
new test checks that sort-merge join
keys are realigned to a range partitioning when the second of two
orderings of an unbounded input matches it, not
only the first. The existing `ordered_stream` helper now takes a list of
orderings so the new test can reuse it.

## What is the testing strategy for this PR?

This PR adds tests only. All new cases pass on the current main branch,
and the new sqllogictest file gives the
same result over repeated runs.

## Are there any user-facing changes?

No.
@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates and removed optimizer Optimizer rules core Core DataFusion crate labels Oct 23, 2026
@jayzhan211

Copy link
Copy Markdown
Contributor Author

Thanks @zhuqi-lucas @kumarUjjawal !

@jayzhan211
jayzhan211 enabled auto-merge October 23, 2026 14:02
@jayzhan211
jayzhan211 added this pull request to the merge queue Oct 23, 2026
@jayzhan211
jayzhan211 removed this pull request from the merge queue due to a manual request Oct 23, 2026
@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Oct 23, 2026
@jayzhan211
jayzhan211 enabled auto-merge October 23, 2026 14:51
@jayzhan211
jayzhan211 added this pull request to the merge queue Oct 23, 2026
Merged via the queue into apache:main with commit bd219dc Oct 23, 2026
45 checks passed
@jayzhan211
jayzhan211 deleted the ensure-requirements-unproven-ordering branch October 23, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-expr Changes to the physical-expr crates physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt) v56.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SanityCheckPlan error when ordering by a literal column from the probe side of a hash join

5 participants