Skip to content

Conversation

@shifluxxc
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

LogicalPlan::TableScan is currently treated as a leaf node in map_children, but some table providers (such as views) expose their own logical plan via TableSource::get_logical_plan().
Because of this incorrect leaf assumption, logical plan visitors and optimizer passes fail to recurse into the underlying logical plan. This leads to missed optimizations and incomplete rewrites when a scan represents a view or other provider-defined logical plan.

What changes are included in this PR?

  • Updated LogicalPlan::map_children to:
    • Detect when a TableSource returns a logical plan
    • Recurse into that inner plan
    • Reconstruct the TableSource using replace_logical_plan after transformation
  • Added two unit tests:
    • test_table_scan_with_inner_plan_is_visited ensures recursion occurs for providers with an inner plan
    • test_table_scan_without_inner_plan_is_not_visited ensures behavior remains unchanged when no inner plan exists

Are these changes tested?

Yes.
Two dedicated unit tests validate:

  1. The inner plan is visited and rewritten correctly when the provider exposes one.
  2. No recursion happens when the provider does not expose a logical plan.

Are there any user-facing changes?

No

@github-actions github-actions bot added the logical-expr Logical plan and expressions label Dec 11, 2025
Copy link
Contributor

@leoyvens leoyvens left a comment

Choose a reason for hiding this comment

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

Thanks for jumping onto this issue so quickly! I just have a concern about the fn replace_logical_plan, I'm hoping there is a simpler way.

None
}

fn replace_logical_plan(
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to avoid adding a method to the TableSource trait?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @leoyvens , can u please review . I removed any changes made to TableSource trait .

@shifluxxc shifluxxc force-pushed the tableScan-Logical-plan-update branch from 588864a to e74ca7a Compare December 11, 2025 17:22
Copy link
Contributor

@leoyvens leoyvens left a comment

Choose a reason for hiding this comment

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

This addresses my issue

Copy link
Member

@martin-g martin-g left a comment

Choose a reason for hiding this comment

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

I am not sure how problematic this is but now the visit method (apply_children) is not symmetrical with the rewrite method (map_children).
apply_children does not visit the inner plan of TableScan.

@alamb
Copy link
Contributor

alamb commented Dec 11, 2025

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 tableScan-Logical-plan-update (e74ca7a) to bde1608 diff
BENCH_NAME=sql_planner
BENCH_COMMAND=cargo bench --all-features --bench sql_planner
BENCH_FILTER=
BENCH_BRANCH_NAME=tableScan-Logical-plan-update
Results will be posted here when complete

@alamb-ghbot
Copy link

🤖: Benchmark completed

Details

group                                                 main                                   tableScan-Logical-plan-update
-----                                                 ----                                   -----------------------------
logical_aggregate_with_join                           1.00    626.4±9.84µs        ? ?/sec    1.01   631.3±10.78µs        ? ?/sec
logical_select_all_from_1000                          1.02     10.5±0.18ms        ? ?/sec    1.00     10.3±0.17ms        ? ?/sec
logical_select_one_from_700                           1.00    412.1±4.75µs        ? ?/sec    1.01    414.8±3.46µs        ? ?/sec
logical_trivial_join_high_numbered_columns            1.00    368.4±3.02µs        ? ?/sec    1.01    370.8±4.96µs        ? ?/sec
logical_trivial_join_low_numbered_columns             1.00    356.1±4.33µs        ? ?/sec    1.00    356.5±3.72µs        ? ?/sec
physical_intersection                                 1.00    826.0±5.73µs        ? ?/sec    1.00   829.7±15.53µs        ? ?/sec
physical_join_consider_sort                           1.00  1371.8±18.75µs        ? ?/sec    1.00  1375.5±16.83µs        ? ?/sec
physical_join_distinct                                1.00    347.3±3.52µs        ? ?/sec    1.00    345.9±2.77µs        ? ?/sec
physical_many_self_joins                              1.01      9.4±0.10ms        ? ?/sec    1.00      9.3±0.10ms        ? ?/sec
physical_plan_clickbench_all                          1.00    192.3±3.07ms        ? ?/sec    1.00    192.8±3.54ms        ? ?/sec
physical_plan_clickbench_q1                           1.00      2.4±0.03ms        ? ?/sec    1.00      2.4±0.04ms        ? ?/sec
physical_plan_clickbench_q10                          1.00      3.3±0.05ms        ? ?/sec    1.01      3.3±0.07ms        ? ?/sec
physical_plan_clickbench_q11                          1.00      3.5±0.06ms        ? ?/sec    1.00      3.5±0.07ms        ? ?/sec
physical_plan_clickbench_q12                          1.00      3.7±0.05ms        ? ?/sec    1.00      3.7±0.05ms        ? ?/sec
physical_plan_clickbench_q13                          1.00      3.3±0.08ms        ? ?/sec    1.00      3.3±0.08ms        ? ?/sec
physical_plan_clickbench_q14                          1.00      3.6±0.12ms        ? ?/sec    1.00      3.6±0.05ms        ? ?/sec
physical_plan_clickbench_q15                          1.00      3.4±0.05ms        ? ?/sec    1.00      3.4±0.05ms        ? ?/sec
physical_plan_clickbench_q16                          1.00      3.2±0.04ms        ? ?/sec    1.00      3.2±0.05ms        ? ?/sec
physical_plan_clickbench_q17                          1.00      3.3±0.04ms        ? ?/sec    1.00      3.3±0.06ms        ? ?/sec
physical_plan_clickbench_q18                          1.00      2.8±0.04ms        ? ?/sec    1.00      2.8±0.05ms        ? ?/sec
physical_plan_clickbench_q19                          1.00      3.7±0.07ms        ? ?/sec    1.00      3.7±0.08ms        ? ?/sec
physical_plan_clickbench_q2                           1.00      2.9±0.02ms        ? ?/sec    1.01      2.9±0.07ms        ? ?/sec
physical_plan_clickbench_q20                          1.00      2.6±0.05ms        ? ?/sec    1.00      2.6±0.03ms        ? ?/sec
physical_plan_clickbench_q21                          1.00      2.9±0.04ms        ? ?/sec    1.00      2.9±0.04ms        ? ?/sec
physical_plan_clickbench_q22                          1.00      3.5±0.09ms        ? ?/sec    1.00      3.5±0.06ms        ? ?/sec
physical_plan_clickbench_q23                          1.00      3.8±0.12ms        ? ?/sec    1.01      3.8±0.08ms        ? ?/sec
physical_plan_clickbench_q24                          1.00      4.9±0.09ms        ? ?/sec    1.00      4.9±0.07ms        ? ?/sec
physical_plan_clickbench_q25                          1.00      3.1±0.03ms        ? ?/sec    1.01      3.1±0.04ms        ? ?/sec
physical_plan_clickbench_q26                          1.00      2.8±0.04ms        ? ?/sec    1.01      2.9±0.05ms        ? ?/sec
physical_plan_clickbench_q27                          1.00      3.1±0.05ms        ? ?/sec    1.00      3.1±0.07ms        ? ?/sec
physical_plan_clickbench_q28                          1.00      4.0±0.08ms        ? ?/sec    1.00      4.0±0.07ms        ? ?/sec
physical_plan_clickbench_q29                          1.00      4.3±0.08ms        ? ?/sec    1.01      4.3±0.07ms        ? ?/sec
physical_plan_clickbench_q3                           1.00      2.8±0.06ms        ? ?/sec    1.00      2.8±0.05ms        ? ?/sec
physical_plan_clickbench_q30                          1.00     15.4±0.23ms        ? ?/sec    1.01     15.5±0.64ms        ? ?/sec
physical_plan_clickbench_q31                          1.00      3.9±0.10ms        ? ?/sec    1.10      4.3±0.59ms        ? ?/sec
physical_plan_clickbench_q32                          1.00      3.9±0.09ms        ? ?/sec    1.01      3.9±0.10ms        ? ?/sec
physical_plan_clickbench_q33                          1.00      3.3±0.09ms        ? ?/sec    1.01      3.4±0.07ms        ? ?/sec
physical_plan_clickbench_q34                          1.00      3.0±0.06ms        ? ?/sec    1.01      3.0±0.07ms        ? ?/sec
physical_plan_clickbench_q35                          1.00      3.1±0.09ms        ? ?/sec    1.03      3.2±0.12ms        ? ?/sec
physical_plan_clickbench_q36                          1.00      3.8±0.13ms        ? ?/sec    1.01      3.9±0.08ms        ? ?/sec
physical_plan_clickbench_q37                          1.00      4.1±0.08ms        ? ?/sec    1.05      4.3±0.17ms        ? ?/sec
physical_plan_clickbench_q38                          1.00      4.0±0.06ms        ? ?/sec    1.21      4.9±0.15ms        ? ?/sec
physical_plan_clickbench_q39                          1.00      3.9±0.07ms        ? ?/sec    1.06      4.1±0.20ms        ? ?/sec
physical_plan_clickbench_q4                           1.00      2.5±0.04ms        ? ?/sec    1.01      2.5±0.05ms        ? ?/sec
physical_plan_clickbench_q40                          1.00      4.7±0.17ms        ? ?/sec    1.06      5.0±0.24ms        ? ?/sec
physical_plan_clickbench_q41                          1.00      4.1±0.08ms        ? ?/sec    1.10      4.5±0.31ms        ? ?/sec
physical_plan_clickbench_q42                          1.00      4.1±0.07ms        ? ?/sec    1.01      4.1±0.10ms        ? ?/sec
physical_plan_clickbench_q43                          1.00      4.5±0.10ms        ? ?/sec    1.02      4.6±0.10ms        ? ?/sec
physical_plan_clickbench_q44                          1.00      2.7±0.03ms        ? ?/sec    1.02      2.7±0.10ms        ? ?/sec
physical_plan_clickbench_q45                          1.00      2.7±0.04ms        ? ?/sec    1.02      2.7±0.08ms        ? ?/sec
physical_plan_clickbench_q46                          1.00      3.1±0.09ms        ? ?/sec    1.01      3.1±0.08ms        ? ?/sec
physical_plan_clickbench_q47                          1.00      3.8±0.05ms        ? ?/sec    1.04      3.9±0.15ms        ? ?/sec
physical_plan_clickbench_q48                          1.00      4.6±0.17ms        ? ?/sec    1.00      4.6±0.08ms        ? ?/sec
physical_plan_clickbench_q49                          1.00      4.9±0.10ms        ? ?/sec    1.00      4.9±0.10ms        ? ?/sec
physical_plan_clickbench_q5                           1.00      2.7±0.02ms        ? ?/sec    1.01      2.8±0.07ms        ? ?/sec
physical_plan_clickbench_q50                          1.00      4.4±0.19ms        ? ?/sec    1.00      4.4±0.11ms        ? ?/sec
physical_plan_clickbench_q51                          1.00      3.3±0.06ms        ? ?/sec    1.00      3.3±0.05ms        ? ?/sec
physical_plan_clickbench_q6                           1.00      2.8±0.03ms        ? ?/sec    1.00      2.8±0.04ms        ? ?/sec
physical_plan_clickbench_q7                           1.00      2.4±0.07ms        ? ?/sec    1.00      2.4±0.04ms        ? ?/sec
physical_plan_clickbench_q8                           1.00      3.3±0.05ms        ? ?/sec    1.01      3.4±0.05ms        ? ?/sec
physical_plan_clickbench_q9                           1.00      3.2±0.04ms        ? ?/sec    1.01      3.2±0.04ms        ? ?/sec
physical_plan_tpcds_all                               1.00   1055.5±6.34ms        ? ?/sec    1.01   1063.7±7.82ms        ? ?/sec
physical_plan_tpch_all                                1.00     63.9±0.57ms        ? ?/sec    1.01     64.4±0.64ms        ? ?/sec
physical_plan_tpch_q1                                 1.00      2.1±0.02ms        ? ?/sec    1.01      2.2±0.03ms        ? ?/sec
physical_plan_tpch_q10                                1.00      3.9±0.04ms        ? ?/sec    1.01      3.9±0.03ms        ? ?/sec
physical_plan_tpch_q11                                1.00      3.6±0.03ms        ? ?/sec    1.01      3.6±0.05ms        ? ?/sec
physical_plan_tpch_q12                                1.00  1838.3±15.16µs        ? ?/sec    1.01  1852.6±15.60µs        ? ?/sec
physical_plan_tpch_q13                                1.00  1433.1±15.91µs        ? ?/sec    1.01  1451.9±14.58µs        ? ?/sec
physical_plan_tpch_q14                                1.00      2.0±0.03ms        ? ?/sec    1.01      2.0±0.02ms        ? ?/sec
physical_plan_tpch_q16                                1.00      2.4±0.02ms        ? ?/sec    1.01      2.4±0.02ms        ? ?/sec
physical_plan_tpch_q17                                1.00      2.6±0.02ms        ? ?/sec    1.01      2.6±0.02ms        ? ?/sec
physical_plan_tpch_q18                                1.00      2.7±0.03ms        ? ?/sec    1.01      2.7±0.02ms        ? ?/sec
physical_plan_tpch_q19                                1.00      3.2±0.02ms        ? ?/sec    1.01      3.2±0.03ms        ? ?/sec
physical_plan_tpch_q2                                 1.00      5.8±0.06ms        ? ?/sec    1.00      5.7±0.04ms        ? ?/sec
physical_plan_tpch_q20                                1.00      3.1±0.03ms        ? ?/sec    1.01      3.2±0.02ms        ? ?/sec
physical_plan_tpch_q21                                1.00      4.1±0.05ms        ? ?/sec    1.01      4.1±0.03ms        ? ?/sec
physical_plan_tpch_q22                                1.00      2.9±0.03ms        ? ?/sec    1.00      2.9±0.07ms        ? ?/sec
physical_plan_tpch_q3                                 1.00      2.7±0.09ms        ? ?/sec    1.00      2.7±0.03ms        ? ?/sec
physical_plan_tpch_q4                                 1.00  1461.0±14.02µs        ? ?/sec    1.00  1465.7±10.36µs        ? ?/sec
physical_plan_tpch_q5                                 1.00      3.3±0.04ms        ? ?/sec    1.00      3.3±0.03ms        ? ?/sec
physical_plan_tpch_q6                                 1.00    881.9±9.18µs        ? ?/sec    1.01   894.1±10.73µs        ? ?/sec
physical_plan_tpch_q7                                 1.00      4.2±0.07ms        ? ?/sec    1.00      4.1±0.03ms        ? ?/sec
physical_plan_tpch_q8                                 1.00      5.4±0.06ms        ? ?/sec    1.00      5.5±0.09ms        ? ?/sec
physical_plan_tpch_q9                                 1.00      4.0±0.12ms        ? ?/sec    1.00      4.0±0.07ms        ? ?/sec
physical_select_aggregates_from_200                   1.03     17.1±0.47ms        ? ?/sec    1.00     16.6±0.17ms        ? ?/sec
physical_select_all_from_1000                         1.02     23.1±0.60ms        ? ?/sec    1.00     22.8±0.48ms        ? ?/sec
physical_select_one_from_700                          1.00  1051.5±15.58µs        ? ?/sec    1.01  1056.7±11.84µs        ? ?/sec
physical_sorted_union_order_by_10_int64               1.01      6.0±0.06ms        ? ?/sec    1.00      5.9±0.06ms        ? ?/sec
physical_sorted_union_order_by_10_uint64              1.02     15.3±0.14ms        ? ?/sec    1.00     15.0±0.20ms        ? ?/sec
physical_sorted_union_order_by_50_int64               1.02    166.2±1.59ms        ? ?/sec    1.00    162.5±1.54ms        ? ?/sec
physical_sorted_union_order_by_50_uint64              1.03    892.3±5.08ms        ? ?/sec    1.00    866.5±6.39ms        ? ?/sec
physical_theta_join_consider_sort                     1.00  1734.9±11.39µs        ? ?/sec    1.01  1748.0±14.05µs        ? ?/sec
physical_unnest_to_join                               1.00  1778.4±24.19µs        ? ?/sec    1.01  1790.8±29.70µs        ? ?/sec
physical_window_function_partition_by_12_on_values    1.00  1103.3±11.66µs        ? ?/sec    1.00  1103.7±25.56µs        ? ?/sec
physical_window_function_partition_by_30_on_values    1.00      2.3±0.02ms        ? ?/sec    1.00      2.3±0.02ms        ? ?/sec
physical_window_function_partition_by_4_on_values     1.00   676.7±12.84µs        ? ?/sec    1.00    674.6±4.93µs        ? ?/sec
physical_window_function_partition_by_7_on_values     1.00    825.2±7.44µs        ? ?/sec    1.00    825.9±8.20µs        ? ?/sec
physical_window_function_partition_by_8_on_values     1.00   891.0±14.64µs        ? ?/sec    1.00    886.9±6.60µs        ? ?/sec
with_param_values_many_columns                        1.00   585.8±22.47µs        ? ?/sec    1.01   590.7±25.64µs        ? ?/sec

@shifluxxc
Copy link
Contributor Author

I am not sure how problematic this is but now the visit method (apply_children) is not symmetrical with the rewrite method (map_children). apply_children does not visit the inner plan of TableScan.

Should i rewrite the apply_children method to actually visit the inner plan @martin-g ?

@martin-g
Copy link
Member

I'm not the best person to answer this question.
Let's wait for others reviews!

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.

LogicalPlan::map_children should recurse into table scans defined by a plan

5 participants