Skip to content

fix: prevent duplicate alias collision with user-provided __datafusion_extracted names#20432

Merged
adriangb merged 6 commits intoapache:mainfrom
pydantic:fix-expr-bug
Feb 24, 2026
Merged

fix: prevent duplicate alias collision with user-provided __datafusion_extracted names#20432
adriangb merged 6 commits intoapache:mainfrom
pydantic:fix-expr-bug

Conversation

@adriangb
Copy link
Contributor

@adriangb adriangb commented Feb 19, 2026

Summary

  • Fixes a bug where the optimizer's AliasGenerator could produce alias names that collide with__datafusion_extracted_N aliases, causing a "Schema contains duplicate unqualified field name" error
  • I don't expect users themselves to create these aliases, but if you run the optimizers twice (with different AliasGenerator instances) you'll hit this.
  • Adds AliasGenerator::update_min_id() to advance the counter past existing aliases
  • Scans each plan node's expressions during ExtractLeafExpressions traversal to seed the generator before any extraction occurs
  • Switches to controlling the traversal which also means the config-based short circuit more clearly skips the entire rule.

Closes #20430

Test plan

  • Unit test: test_user_provided_extracted_alias_no_collision in extract_leaf_expressions
  • SLT regression test in projection_pushdown.slt with explicit __datafusion_extracted_2 alias

🤖 Generated with Claude Code

@github-actions github-actions bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) common Related to common crate labels Feb 19, 2026
@adriangb adriangb requested a review from alamb February 19, 2026 14:20
adriangb added a commit to pydantic/datafusion that referenced this pull request Feb 20, 2026
…n_extracted names (apache#20432)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
adriangb added a commit to pydantic/datafusion that referenced this pull request Feb 20, 2026
…n_extracted names (apache#20432)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
&& let Some(id_str) = alias
.name
.strip_prefix(EXTRACTED_EXPR_PREFIX)
.and_then(|s| s.strip_prefix('_'))
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't this be .and_then(|id| id_str.parse().ok())

adriangb and others added 5 commits February 20, 2026 16:48
…n_extracted names (apache#20430)

When a user query contains an explicit alias using the reserved
`__datafusion_extracted` prefix, the optimizer's AliasGenerator could
generate the same alias name, causing a "Schema contains duplicate
unqualified field name" error.

Fix by scanning each plan node's expressions for pre-existing
`__datafusion_extracted_N` aliases during the TopDown traversal in
ExtractLeafExpressions, advancing the generator counter past them
before any extraction occurs.

Closes apache#20430

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Traverse into subqueries when extracting leaf expressions and when
advancing the alias generator past existing extracted aliases. Also
collapse nested if-let to satisfy clippy::collapsible_if.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
FROM t
WHERE COALESCE(get_field(s, 'f1'), get_field(s, 'f2')) = 1;
----
1
Copy link
Contributor

@getChan getChan Feb 20, 2026

Choose a reason for hiding this comment

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

Would it make sense to also add an EXPLAIN assertion here? It could guard the alias allocation behavior directly (e.g. user-provided __datafusion_extracted_2 remains stable while optimizer-generated aliases move to the next IDs), so future optimizer refactors don’t regress silently.

assisted by codex

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

Adds an EXPLAIN query to verify the user-provided __datafusion_extracted_2
alias is preserved while optimizer-generated aliases skip to _3 and _4,
guarding against silent regressions in alias allocation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Looks good to me -- thank you @adriangb @cetra3 and @getChan

@alamb
Copy link
Contributor

alamb commented Feb 24, 2026

run benchmark sql_planner

@alamb-ghbot
Copy link

🤖 ./gh_compare_branch_bench.sh compare_branch_bench.sh Running
Linux aal-dev 6.14.0-1018-gcp #19~24.04.1-Ubuntu SMP Wed Sep 24 23:23:09 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing fix-expr-bug (6378958) to ace9cd4 diff
BENCH_NAME=sql_planner
BENCH_COMMAND=cargo bench --features=parquet --bench sql_planner
BENCH_FILTER=
BENCH_BRANCH_NAME=fix-expr-bug
Results will be posted here when complete

@alamb-ghbot
Copy link

🤖: Benchmark completed

Details

group                                                 fix-expr-bug                           main
-----                                                 ------------                           ----
logical_aggregate_with_join                           1.00    636.3±8.81µs        ? ?/sec    1.00    634.1±5.97µs        ? ?/sec
logical_plan_struct_join_agg_sort                     1.00    286.6±4.49µs        ? ?/sec    1.00    287.3±5.75µs        ? ?/sec
logical_select_all_from_1000                          1.00     10.2±0.04ms        ? ?/sec    1.02     10.4±0.29ms        ? ?/sec
logical_select_one_from_700                           1.00    416.8±2.76µs        ? ?/sec    1.00    415.3±2.40µs        ? ?/sec
logical_trivial_join_high_numbered_columns            1.00    373.2±6.82µs        ? ?/sec    1.00    373.3±1.81µs        ? ?/sec
logical_trivial_join_low_numbered_columns             1.00    360.6±8.46µs        ? ?/sec    1.00    361.6±5.92µs        ? ?/sec
physical_intersection                                 1.00   1581.0±4.70µs        ? ?/sec    1.00   1577.5±8.29µs        ? ?/sec
physical_join_consider_sort                           1.01      2.3±0.05ms        ? ?/sec    1.00      2.2±0.01ms        ? ?/sec
physical_join_distinct                                1.00    350.5±2.96µs        ? ?/sec    1.00    350.1±1.58µs        ? ?/sec
physical_many_self_joins                              1.00     12.4±0.19ms        ? ?/sec    1.00     12.4±0.03ms        ? ?/sec
physical_plan_clickbench_all                          1.01    201.8±2.42ms        ? ?/sec    1.00    200.0±4.47ms        ? ?/sec
physical_plan_clickbench_q1                           1.01      2.1±0.03ms        ? ?/sec    1.00      2.1±0.06ms        ? ?/sec
physical_plan_clickbench_q10                          1.00      3.6±0.02ms        ? ?/sec    1.00      3.6±0.02ms        ? ?/sec
physical_plan_clickbench_q11                          1.01      4.1±0.10ms        ? ?/sec    1.00      4.1±0.12ms        ? ?/sec
physical_plan_clickbench_q12                          1.01      4.3±0.13ms        ? ?/sec    1.00      4.2±0.12ms        ? ?/sec
physical_plan_clickbench_q13                          1.02      3.8±0.12ms        ? ?/sec    1.00      3.7±0.08ms        ? ?/sec
physical_plan_clickbench_q14                          1.00      4.1±0.03ms        ? ?/sec    1.00      4.1±0.10ms        ? ?/sec
physical_plan_clickbench_q15                          1.01      3.8±0.11ms        ? ?/sec    1.00      3.8±0.02ms        ? ?/sec
physical_plan_clickbench_q16                          1.01      3.7±0.04ms        ? ?/sec    1.00      3.6±0.09ms        ? ?/sec
physical_plan_clickbench_q17                          1.01      3.8±0.08ms        ? ?/sec    1.00      3.7±0.06ms        ? ?/sec
physical_plan_clickbench_q18                          1.00      2.7±0.02ms        ? ?/sec    1.00      2.7±0.06ms        ? ?/sec
physical_plan_clickbench_q19                          1.03      4.2±0.08ms        ? ?/sec    1.00      4.1±0.06ms        ? ?/sec
physical_plan_clickbench_q2                           1.00      2.8±0.02ms        ? ?/sec    1.00      2.8±0.05ms        ? ?/sec
physical_plan_clickbench_q20                          1.03      2.2±0.06ms        ? ?/sec    1.00      2.1±0.01ms        ? ?/sec
physical_plan_clickbench_q21                          1.00      2.8±0.03ms        ? ?/sec    1.00      2.8±0.06ms        ? ?/sec
physical_plan_clickbench_q22                          1.01      3.9±0.08ms        ? ?/sec    1.00      3.9±0.05ms        ? ?/sec
physical_plan_clickbench_q23                          1.00      4.2±0.05ms        ? ?/sec    1.00      4.2±0.12ms        ? ?/sec
physical_plan_clickbench_q24                          1.00      4.8±0.07ms        ? ?/sec    1.00      4.8±0.07ms        ? ?/sec
physical_plan_clickbench_q25                          1.00      3.4±0.02ms        ? ?/sec    1.00      3.4±0.07ms        ? ?/sec
physical_plan_clickbench_q26                          1.01      2.9±0.03ms        ? ?/sec    1.00      2.9±0.05ms        ? ?/sec
physical_plan_clickbench_q27                          1.00      3.5±0.01ms        ? ?/sec    1.00      3.5±0.07ms        ? ?/sec
physical_plan_clickbench_q28                          1.00      4.4±0.03ms        ? ?/sec    1.00      4.5±0.11ms        ? ?/sec
physical_plan_clickbench_q29                          1.02      4.7±0.08ms        ? ?/sec    1.00      4.7±0.03ms        ? ?/sec
physical_plan_clickbench_q3                           1.01      2.5±0.04ms        ? ?/sec    1.00      2.5±0.03ms        ? ?/sec
physical_plan_clickbench_q30                          1.00     15.9±0.17ms        ? ?/sec    1.01     16.0±0.32ms        ? ?/sec
physical_plan_clickbench_q31                          1.01      4.4±0.03ms        ? ?/sec    1.00      4.4±0.10ms        ? ?/sec
physical_plan_clickbench_q32                          1.01      4.4±0.03ms        ? ?/sec    1.00      4.4±0.11ms        ? ?/sec
physical_plan_clickbench_q33                          1.03      3.6±0.11ms        ? ?/sec    1.00      3.6±0.02ms        ? ?/sec
physical_plan_clickbench_q34                          1.01      3.2±0.02ms        ? ?/sec    1.00      3.2±0.03ms        ? ?/sec
physical_plan_clickbench_q35                          1.01      3.4±0.06ms        ? ?/sec    1.00      3.3±0.06ms        ? ?/sec
physical_plan_clickbench_q36                          1.01      4.2±0.06ms        ? ?/sec    1.00      4.2±0.02ms        ? ?/sec
physical_plan_clickbench_q37                          1.01      4.7±0.05ms        ? ?/sec    1.00      4.6±0.08ms        ? ?/sec
physical_plan_clickbench_q38                          1.00      4.7±0.04ms        ? ?/sec    1.00      4.6±0.09ms        ? ?/sec
physical_plan_clickbench_q39                          1.00      4.1±0.03ms        ? ?/sec    1.00      4.1±0.09ms        ? ?/sec
physical_plan_clickbench_q4                           1.00      2.2±0.01ms        ? ?/sec    1.00      2.2±0.04ms        ? ?/sec
physical_plan_clickbench_q40                          1.01      4.9±0.11ms        ? ?/sec    1.00      4.9±0.08ms        ? ?/sec
physical_plan_clickbench_q41                          1.01      4.3±0.16ms        ? ?/sec    1.00      4.3±0.07ms        ? ?/sec
physical_plan_clickbench_q42                          1.01      4.2±0.10ms        ? ?/sec    1.00      4.2±0.08ms        ? ?/sec
physical_plan_clickbench_q43                          1.02      4.6±0.11ms        ? ?/sec    1.00      4.5±0.02ms        ? ?/sec
physical_plan_clickbench_q44                          1.01      2.3±0.05ms        ? ?/sec    1.00      2.3±0.04ms        ? ?/sec
physical_plan_clickbench_q45                          1.01      2.3±0.02ms        ? ?/sec    1.00      2.3±0.04ms        ? ?/sec
physical_plan_clickbench_q46                          1.00      3.2±0.06ms        ? ?/sec    1.00      3.2±0.09ms        ? ?/sec
physical_plan_clickbench_q47                          1.01      4.7±0.05ms        ? ?/sec    1.00      4.7±0.12ms        ? ?/sec
physical_plan_clickbench_q48                          1.01      5.1±0.06ms        ? ?/sec    1.00      5.1±0.09ms        ? ?/sec
physical_plan_clickbench_q49                          1.01      5.4±0.07ms        ? ?/sec    1.00      5.4±0.08ms        ? ?/sec
physical_plan_clickbench_q5                           1.01      2.5±0.06ms        ? ?/sec    1.00      2.5±0.03ms        ? ?/sec
physical_plan_clickbench_q50                          1.00      4.2±0.06ms        ? ?/sec    1.00      4.2±0.10ms        ? ?/sec
physical_plan_clickbench_q51                          1.02      3.6±0.10ms        ? ?/sec    1.00      3.5±0.04ms        ? ?/sec
physical_plan_clickbench_q6                           1.00      2.5±0.02ms        ? ?/sec    1.00      2.5±0.05ms        ? ?/sec
physical_plan_clickbench_q7                           1.01      2.1±0.04ms        ? ?/sec    1.00      2.1±0.03ms        ? ?/sec
physical_plan_clickbench_q8                           1.01      3.4±0.07ms        ? ?/sec    1.00      3.4±0.07ms        ? ?/sec
physical_plan_clickbench_q9                           1.01      3.6±0.04ms        ? ?/sec    1.00      3.6±0.05ms        ? ?/sec
physical_plan_struct_join_agg_sort                    1.00      3.3±0.02ms        ? ?/sec    1.00      3.3±0.03ms        ? ?/sec
physical_plan_tpcds_all                               1.01  1909.4±23.73ms        ? ?/sec    1.00  1896.6±28.88ms        ? ?/sec
physical_plan_tpch_all                                1.00    126.5±2.09ms        ? ?/sec    1.01    127.2±5.06ms        ? ?/sec
physical_plan_tpch_q1                                 1.00      3.0±0.03ms        ? ?/sec    1.01      3.1±0.10ms        ? ?/sec
physical_plan_tpch_q10                                1.00      7.2±0.04ms        ? ?/sec    1.01      7.2±0.27ms        ? ?/sec
physical_plan_tpch_q11                                1.00      8.5±0.05ms        ? ?/sec    1.00      8.5±0.34ms        ? ?/sec
physical_plan_tpch_q12                                1.02      3.1±0.05ms        ? ?/sec    1.00      3.1±0.03ms        ? ?/sec
physical_plan_tpch_q13                                1.00      3.0±0.05ms        ? ?/sec    1.03      3.1±0.17ms        ? ?/sec
physical_plan_tpch_q14                                1.00      3.0±0.03ms        ? ?/sec    1.02      3.1±0.13ms        ? ?/sec
physical_plan_tpch_q16                                1.01      5.3±0.19ms        ? ?/sec    1.00      5.2±0.20ms        ? ?/sec
physical_plan_tpch_q17                                1.00      5.6±0.08ms        ? ?/sec    1.01      5.7±0.27ms        ? ?/sec
physical_plan_tpch_q18                                1.01      6.0±0.09ms        ? ?/sec    1.00      5.9±0.21ms        ? ?/sec
physical_plan_tpch_q19                                1.01      5.2±0.09ms        ? ?/sec    1.00      5.1±0.14ms        ? ?/sec
physical_plan_tpch_q2                                 1.01     12.4±0.46ms        ? ?/sec    1.00     12.3±0.32ms        ? ?/sec
physical_plan_tpch_q20                                1.01      8.0±0.23ms        ? ?/sec    1.00      8.0±0.09ms        ? ?/sec
physical_plan_tpch_q21                                1.01     10.1±0.04ms        ? ?/sec    1.00     10.0±0.14ms        ? ?/sec
physical_plan_tpch_q22                                1.02      6.5±0.03ms        ? ?/sec    1.00      6.4±0.04ms        ? ?/sec
physical_plan_tpch_q3                                 1.00      5.6±0.02ms        ? ?/sec    1.00      5.6±0.07ms        ? ?/sec
physical_plan_tpch_q4                                 1.00      3.0±0.01ms        ? ?/sec    1.01      3.0±0.14ms        ? ?/sec
physical_plan_tpch_q5                                 1.01      6.0±0.04ms        ? ?/sec    1.00      5.9±0.11ms        ? ?/sec
physical_plan_tpch_q6                                 1.02  1628.0±48.15µs        ? ?/sec    1.00  1603.8±34.21µs        ? ?/sec
physical_plan_tpch_q7                                 1.00      7.1±0.11ms        ? ?/sec    1.02      7.2±0.22ms        ? ?/sec
physical_plan_tpch_q8                                 1.01      9.3±0.13ms        ? ?/sec    1.00      9.2±0.14ms        ? ?/sec
physical_plan_tpch_q9                                 1.01      6.7±0.21ms        ? ?/sec    1.00      6.6±0.12ms        ? ?/sec
physical_select_aggregates_from_200                   1.00     17.3±0.31ms        ? ?/sec    1.00     17.3±0.88ms        ? ?/sec
physical_select_all_from_1000                         1.01     23.1±0.26ms        ? ?/sec    1.00     22.9±0.16ms        ? ?/sec
physical_select_one_from_700                          1.00   1319.6±7.61µs        ? ?/sec    1.00   1320.1±8.56µs        ? ?/sec
physical_sorted_union_order_by_10_int64               1.01     10.9±0.05ms        ? ?/sec    1.00     10.8±0.04ms        ? ?/sec
physical_sorted_union_order_by_10_uint64              1.02     29.9±1.02ms        ? ?/sec    1.00     29.4±0.35ms        ? ?/sec
physical_sorted_union_order_by_50_int64               1.00    192.8±2.05ms        ? ?/sec    1.00    193.5±2.66ms        ? ?/sec
physical_sorted_union_order_by_50_uint64              1.02  1082.8±12.29ms        ? ?/sec    1.00  1061.1±11.75ms        ? ?/sec
physical_theta_join_consider_sort                     1.01      2.6±0.03ms        ? ?/sec    1.00      2.6±0.02ms        ? ?/sec
physical_unnest_to_join                               1.01      3.1±0.08ms        ? ?/sec    1.00      3.1±0.03ms        ? ?/sec
physical_window_function_partition_by_12_on_values    1.00  1567.1±10.70µs        ? ?/sec    1.00  1563.4±12.13µs        ? ?/sec
physical_window_function_partition_by_30_on_values    1.01      2.9±0.03ms        ? ?/sec    1.00      2.9±0.02ms        ? ?/sec
physical_window_function_partition_by_4_on_values     1.01  1087.3±15.96µs        ? ?/sec    1.00   1074.1±9.41µs        ? ?/sec
physical_window_function_partition_by_7_on_values     1.00  1260.0±35.51µs        ? ?/sec    1.01  1267.6±39.09µs        ? ?/sec
physical_window_function_partition_by_8_on_values     1.00  1324.1±11.49µs        ? ?/sec    1.00  1321.8±25.33µs        ? ?/sec
with_param_values_many_columns                        1.00    573.0±5.35µs        ? ?/sec    1.01    579.5±3.99µs        ? ?/sec

@adriangb
Copy link
Contributor Author

Looks like no major regressions, will go ahead and merge

@adriangb adriangb added this pull request to the merge queue Feb 24, 2026
Merged via the queue into apache:main with commit 670dbf4 Feb 24, 2026
32 checks passed
@adriangb adriangb deleted the fix-expr-bug branch February 24, 2026 15:05
Broshen pushed a commit to Broshen/datafusion that referenced this pull request Feb 24, 2026
…n_extracted names (apache#20432)

- Fixes a bug where the optimizer's `AliasGenerator` could produce alias
names that collide with`__datafusion_extracted_N` aliases, causing a
"Schema contains duplicate unqualified field name" error
- I don't expect users themselves to create these aliases, but if you
run the optimizers twice (with different `AliasGenerator` instances)
you'll hit this.
- Adds `AliasGenerator::update_min_id()` to advance the counter past
existing aliases
- Scans each plan node's expressions during `ExtractLeafExpressions`
traversal to seed the generator before any extraction occurs
- Switches to controlling the traversal which also means the
config-based short circuit more clearly skips the entire rule.

Closes apache#20430

- [x] Unit test: `test_user_provided_extracted_alias_no_collision` in
`extract_leaf_expressions`
- [x] SLT regression test in `projection_pushdown.slt` with explicit
`__datafusion_extracted_2` alias

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimizer rule error for ExtractLeafExpressions

5 participants