Skip to content

Conversation

findepi
Copy link
Member

@findepi findepi commented Aug 13, 2025

Before the changes, PartialOrd could return Some(Equal) for two
functions that are not equal in PartialEq sense. This is violation of
PartialOrd contract. This was possible e.g. when

  • two functions have same name, but are of different types (e.g. when
    someone constructs DataFusion LogicalPlan and mixes DataFusion builtin
    functions with their own)
  • a function has a parameter (e.g. "safe" attribute for a CAST). The
    parameter is honored by PartialEq comparison but was transparent to
    PartialOrd ordering.

The fix is to consult eq inside ord implementation. If ord thinks two
instances are equal, but they are not equal in Eq sense, they are
considered incomparable.

@findepi findepi marked this pull request as draft August 13, 2025 20:44
@github-actions github-actions bot added the logical-expr Logical plan and expressions label Aug 13, 2025
@findepi findepi force-pushed the findepi/fix-partialord-for-scalarudf-dfb3ab branch from 7ab8252 to 2093f4c Compare August 14, 2025 06:24
Before the changes, `PartialOrd` could return `Some(Equal)` for two
functions that are not equal in `PartialEq` sense.  This is violation of
`PartialOrd` contract.  This was possible e.g. when

- two functions have same name, but are of different types (e.g. when
  someone constructs DataFusion LogicalPlan and mixes DataFusion builtin
  functions with their own)
- a function has a parameter (e.g. "safe" attribute for a CAST). The
  parameter is honored by `PartialEq` comparison but was transparent to
  `PartialOrd` ordering.

The fix is to consult eq inside ord implementation. If ord thinks two
instances are equal, but they are not equal in Eq sense, they are
considered incomparable.
@findepi findepi force-pushed the findepi/fix-partialord-for-scalarudf-dfb3ab branch from 2093f4c to 98c404f Compare August 14, 2025 06:31
@findepi findepi marked this pull request as ready for review August 14, 2025 06:31
@findepi
Copy link
Member Author

findepi commented Aug 14, 2025

cc @ngli-me @jayzhan211 @alamb

@findepi findepi requested a review from alamb August 14, 2025 10:40
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks @findepi -- I think we should be doing short circuiting, but otherwise this looks great

let mut cmp = self.name().cmp(other.name());
if cmp == Ordering::Equal {
cmp = self.signature().partial_cmp(other.signature())?;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could short circut the comparison if the ordering is not equal

Something like this, which should avoid computation in many cases

       if cmp != Ordering::Equal {
          return cmp
        }

Copy link
Member Author

Choose a reason for hiding this comment

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

That would make debug_assert below unreachable. Do you want me to delete it?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I think the debug assert is good. Sorry for not being clear

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

I thought about it some more and this PR already does short circuiting most likely (returning early would for sure avoid extra comparisions, but the compiler probably does the same thing with the way this PR is structured too)

@alamb
Copy link
Contributor

alamb commented Aug 21, 2025

🤖 ./gh_compare_branch_bench.sh Benchmark Script Running
Linux aal-dev 6.14.0-1014-gcp #15~24.04.1-Ubuntu SMP Fri Jul 25 23:26:08 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing findepi/fix-partialord-for-scalarudf-dfb3ab (e2968e7) to 541fdfb diff
BENCH_NAME=sql_planner
BENCH_COMMAND=cargo bench --bench sql_planner
BENCH_FILTER=
BENCH_BRANCH_NAME=findepi_fix-partialord-for-scalarudf-dfb3ab
Results will be posted here when complete

@alamb
Copy link
Contributor

alamb commented Aug 21, 2025

🤖: Benchmark completed

Details

group                                         findepi_fix-partialord-for-scalarudf-dfb3ab    main
-----                                         -------------------------------------------    ----
logical_aggregate_with_join                   1.00    638.0±3.46µs        ? ?/sec            1.00    635.2±2.29µs        ? ?/sec
logical_select_all_from_1000                  1.00     11.1±0.04ms        ? ?/sec            1.00     11.1±0.11ms        ? ?/sec
logical_select_one_from_700                   1.03    435.1±1.40µs        ? ?/sec            1.00    420.9±1.52µs        ? ?/sec
logical_trivial_join_high_numbered_columns    1.02    385.4±1.11µs        ? ?/sec            1.00    377.4±2.64µs        ? ?/sec
logical_trivial_join_low_numbered_columns     1.02    371.2±0.78µs        ? ?/sec            1.00    363.5±2.15µs        ? ?/sec
physical_intersection                         1.02    852.2±2.60µs        ? ?/sec            1.00    838.4±4.69µs        ? ?/sec
physical_join_consider_sort                   1.02   1429.1±5.16µs        ? ?/sec            1.00  1404.4±11.62µs        ? ?/sec
physical_join_distinct                        1.02    362.0±1.21µs        ? ?/sec            1.00    354.9±1.08µs        ? ?/sec
physical_many_self_joins                      1.00     10.5±0.02ms        ? ?/sec            1.00     10.4±0.03ms        ? ?/sec
physical_plan_clickbench_all                  1.00    192.4±0.96ms        ? ?/sec            1.00    192.5±0.96ms        ? ?/sec
physical_plan_clickbench_q1                   1.00      2.6±0.01ms        ? ?/sec            1.00      2.6±0.01ms        ? ?/sec
physical_plan_clickbench_q10                  1.00      3.5±0.02ms        ? ?/sec            1.00      3.5±0.01ms        ? ?/sec
physical_plan_clickbench_q11                  1.00      3.7±0.02ms        ? ?/sec            1.00      3.7±0.01ms        ? ?/sec
physical_plan_clickbench_q12                  1.01      3.8±0.03ms        ? ?/sec            1.00      3.8±0.02ms        ? ?/sec
physical_plan_clickbench_q13                  1.00      3.5±0.03ms        ? ?/sec            1.00      3.5±0.02ms        ? ?/sec
physical_plan_clickbench_q14                  1.00      3.7±0.02ms        ? ?/sec            1.01      3.7±0.03ms        ? ?/sec
physical_plan_clickbench_q15                  1.00      3.5±0.02ms        ? ?/sec            1.01      3.6±0.03ms        ? ?/sec
physical_plan_clickbench_q16                  1.03      3.5±0.11ms        ? ?/sec            1.00      3.4±0.02ms        ? ?/sec
physical_plan_clickbench_q17                  1.01      3.5±0.02ms        ? ?/sec            1.00      3.5±0.04ms        ? ?/sec
physical_plan_clickbench_q18                  1.00      3.0±0.01ms        ? ?/sec            1.00      3.0±0.02ms        ? ?/sec
physical_plan_clickbench_q19                  1.01      3.9±0.02ms        ? ?/sec            1.00      3.9±0.02ms        ? ?/sec
physical_plan_clickbench_q2                   1.00      3.1±0.02ms        ? ?/sec            1.01      3.1±0.02ms        ? ?/sec
physical_plan_clickbench_q20                  1.00      2.8±0.02ms        ? ?/sec            1.00      2.7±0.02ms        ? ?/sec
physical_plan_clickbench_q21                  1.01      3.1±0.02ms        ? ?/sec            1.00      3.1±0.02ms        ? ?/sec
physical_plan_clickbench_q22                  1.00      3.7±0.04ms        ? ?/sec            1.00      3.7±0.02ms        ? ?/sec
physical_plan_clickbench_q23                  1.00      3.9±0.02ms        ? ?/sec            1.00      3.9±0.02ms        ? ?/sec
physical_plan_clickbench_q24                  1.00      4.5±0.02ms        ? ?/sec            1.01      4.5±0.02ms        ? ?/sec
physical_plan_clickbench_q25                  1.00      3.2±0.03ms        ? ?/sec            1.00      3.3±0.02ms        ? ?/sec
physical_plan_clickbench_q26                  1.00      3.0±0.01ms        ? ?/sec            1.00      3.0±0.03ms        ? ?/sec
physical_plan_clickbench_q27                  1.01      3.3±0.09ms        ? ?/sec            1.00      3.3±0.03ms        ? ?/sec
physical_plan_clickbench_q28                  1.01      4.0±0.03ms        ? ?/sec            1.00      4.0±0.04ms        ? ?/sec
physical_plan_clickbench_q29                  1.02      4.8±0.08ms        ? ?/sec            1.00      4.7±0.02ms        ? ?/sec
physical_plan_clickbench_q3                   1.00      3.0±0.01ms        ? ?/sec            1.00      3.0±0.03ms        ? ?/sec
physical_plan_clickbench_q30                  1.00     13.2±0.08ms        ? ?/sec            1.00     13.1±0.23ms        ? ?/sec
physical_plan_clickbench_q31                  1.00      4.0±0.03ms        ? ?/sec            1.00      4.0±0.04ms        ? ?/sec
physical_plan_clickbench_q32                  1.00      4.0±0.02ms        ? ?/sec            1.00      4.0±0.03ms        ? ?/sec
physical_plan_clickbench_q33                  1.01      3.5±0.03ms        ? ?/sec            1.00      3.5±0.02ms        ? ?/sec
physical_plan_clickbench_q34                  1.01      3.2±0.02ms        ? ?/sec            1.00      3.2±0.02ms        ? ?/sec
physical_plan_clickbench_q35                  1.00      3.3±0.02ms        ? ?/sec            1.00      3.3±0.02ms        ? ?/sec
physical_plan_clickbench_q36                  1.01      4.0±0.02ms        ? ?/sec            1.00      4.0±0.02ms        ? ?/sec
physical_plan_clickbench_q37                  1.00      4.0±0.03ms        ? ?/sec            1.00      4.0±0.03ms        ? ?/sec
physical_plan_clickbench_q38                  1.00      4.0±0.03ms        ? ?/sec            1.00      4.0±0.02ms        ? ?/sec
physical_plan_clickbench_q39                  1.01      3.8±0.02ms        ? ?/sec            1.00      3.8±0.02ms        ? ?/sec
physical_plan_clickbench_q4                   1.00      2.7±0.02ms        ? ?/sec            1.01      2.7±0.01ms        ? ?/sec
physical_plan_clickbench_q40                  1.00      4.4±0.02ms        ? ?/sec            1.01      4.5±0.02ms        ? ?/sec
physical_plan_clickbench_q41                  1.01      4.0±0.04ms        ? ?/sec            1.00      4.0±0.03ms        ? ?/sec
physical_plan_clickbench_q42                  1.00      3.9±0.02ms        ? ?/sec            1.00      3.9±0.03ms        ? ?/sec
physical_plan_clickbench_q43                  1.01      4.3±0.03ms        ? ?/sec            1.00      4.2±0.03ms        ? ?/sec
physical_plan_clickbench_q44                  1.00      2.8±0.02ms        ? ?/sec            1.00      2.8±0.02ms        ? ?/sec
physical_plan_clickbench_q45                  1.00      2.8±0.02ms        ? ?/sec            1.00      2.8±0.02ms        ? ?/sec
physical_plan_clickbench_q46                  1.01      3.3±0.02ms        ? ?/sec            1.00      3.3±0.02ms        ? ?/sec
physical_plan_clickbench_q47                  1.00      3.9±0.03ms        ? ?/sec            1.00      3.9±0.03ms        ? ?/sec
physical_plan_clickbench_q48                  1.00      4.6±0.02ms        ? ?/sec            1.00      4.6±0.03ms        ? ?/sec
physical_plan_clickbench_q49                  1.01      4.9±0.06ms        ? ?/sec            1.00      4.8±0.03ms        ? ?/sec
physical_plan_clickbench_q5                   1.01      2.9±0.02ms        ? ?/sec            1.00      2.9±0.02ms        ? ?/sec
physical_plan_clickbench_q50                  1.00      4.3±0.02ms        ? ?/sec            1.00      4.3±0.03ms        ? ?/sec
physical_plan_clickbench_q51                  1.00      3.4±0.03ms        ? ?/sec            1.00      3.4±0.02ms        ? ?/sec
physical_plan_clickbench_q6                   1.00      2.9±0.01ms        ? ?/sec            1.00      2.9±0.02ms        ? ?/sec
physical_plan_clickbench_q7                   1.01      2.6±0.02ms        ? ?/sec            1.00      2.6±0.02ms        ? ?/sec
physical_plan_clickbench_q8                   1.01      3.5±0.02ms        ? ?/sec            1.00      3.5±0.02ms        ? ?/sec
physical_plan_clickbench_q9                   1.00      3.4±0.03ms        ? ?/sec            1.00      3.3±0.03ms        ? ?/sec
physical_plan_tpcds_all                       1.01   1021.7±2.17ms        ? ?/sec            1.00   1015.6±2.47ms        ? ?/sec
physical_plan_tpch_all                        1.00     62.1±0.23ms        ? ?/sec            1.00     62.1±0.25ms        ? ?/sec
physical_plan_tpch_q1                         1.00      2.0±0.01ms        ? ?/sec            1.00      2.0±0.01ms        ? ?/sec
physical_plan_tpch_q10                        1.00      3.8±0.01ms        ? ?/sec            1.00      3.8±0.01ms        ? ?/sec
physical_plan_tpch_q11                        1.01      3.3±0.01ms        ? ?/sec            1.00      3.3±0.01ms        ? ?/sec
physical_plan_tpch_q12                        1.00   1812.5±8.84µs        ? ?/sec            1.00   1804.6±5.72µs        ? ?/sec
physical_plan_tpch_q13                        1.00   1449.8±7.39µs        ? ?/sec            1.00   1444.7±7.83µs        ? ?/sec
physical_plan_tpch_q14                        1.00  1960.4±11.34µs        ? ?/sec            1.00   1962.8±6.71µs        ? ?/sec
physical_plan_tpch_q16                        1.01      2.5±0.01ms        ? ?/sec            1.00      2.5±0.00ms        ? ?/sec
physical_plan_tpch_q17                        1.01      2.4±0.01ms        ? ?/sec            1.00      2.4±0.01ms        ? ?/sec
physical_plan_tpch_q18                        1.00      2.7±0.01ms        ? ?/sec            1.00      2.7±0.01ms        ? ?/sec
physical_plan_tpch_q19                        1.01      3.2±0.01ms        ? ?/sec            1.00      3.2±0.01ms        ? ?/sec
physical_plan_tpch_q2                         1.00      5.5±0.02ms        ? ?/sec            1.00      5.5±0.02ms        ? ?/sec
physical_plan_tpch_q20                        1.01      3.1±0.01ms        ? ?/sec            1.00      3.1±0.01ms        ? ?/sec
physical_plan_tpch_q21                        1.00      4.1±0.01ms        ? ?/sec            1.00      4.1±0.01ms        ? ?/sec
physical_plan_tpch_q22                        1.01      2.7±0.00ms        ? ?/sec            1.00      2.7±0.01ms        ? ?/sec
physical_plan_tpch_q3                         1.01      2.6±0.01ms        ? ?/sec            1.00      2.6±0.01ms        ? ?/sec
physical_plan_tpch_q4                         1.00   1512.1±4.57µs        ? ?/sec            1.00  1511.2±10.09µs        ? ?/sec
physical_plan_tpch_q5                         1.00      3.1±0.01ms        ? ?/sec            1.00      3.1±0.01ms        ? ?/sec
physical_plan_tpch_q6                         1.01    860.2±4.61µs        ? ?/sec            1.00    855.7±3.95µs        ? ?/sec
physical_plan_tpch_q7                         1.00      4.2±0.01ms        ? ?/sec            1.00      4.2±0.02ms        ? ?/sec
physical_plan_tpch_q8                         1.00      5.2±0.01ms        ? ?/sec            1.00      5.1±0.01ms        ? ?/sec
physical_plan_tpch_q9                         1.01      4.1±0.02ms        ? ?/sec            1.00      4.1±0.01ms        ? ?/sec
physical_select_aggregates_from_200           1.00     16.6±0.12ms        ? ?/sec            1.00     16.7±0.05ms        ? ?/sec
physical_select_all_from_1000                 1.00     24.5±0.12ms        ? ?/sec            1.00     24.4±0.05ms        ? ?/sec
physical_select_one_from_700                  1.02   1126.0±4.54µs        ? ?/sec            1.00   1099.3±5.89µs        ? ?/sec
physical_sorted_union_orderby                 1.00     41.6±0.09ms        ? ?/sec            1.00     41.5±0.12ms        ? ?/sec
physical_theta_join_consider_sort             1.01   1780.2±4.55µs        ? ?/sec            1.00   1768.3±8.61µs        ? ?/sec
physical_unnest_to_join                       1.02   1332.6±4.74µs        ? ?/sec            1.00   1308.2±5.55µs        ? ?/sec
with_param_values_many_columns                1.00    141.7±0.86µs        ? ?/sec            1.00    141.5±0.76µs        ? ?/sec

@alamb
Copy link
Contributor

alamb commented Aug 21, 2025

Look s good to me -- thanks @findepi

@alamb alamb merged commit a0248a9 into apache:main Aug 21, 2025
27 checks passed
@findepi findepi deleted the findepi/fix-partialord-for-scalarudf-dfb3ab branch September 3, 2025 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logical-expr Logical plan and expressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants