Skip to content

Commit a181e1d

Browse files
authored
Remove inline table scan analyzer rule (#15201)
* add inline table scan * fix test * rm rule * rm code * dataframe * remove logic in plan_from_tables * fix query
1 parent 8c8b245 commit a181e1d

File tree

6 files changed

+74
-225
lines changed

6 files changed

+74
-225
lines changed

datafusion/core/tests/dataframe/mod.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,14 +1581,18 @@ async fn with_column_join_same_columns() -> Result<()> {
15811581

15821582
assert_snapshot!(
15831583
df_with_column.logical_plan(),
1584-
@r###"
1584+
@r"
15851585
Projection: t1.c1, t2.c1, Boolean(true) AS new_column
15861586
Limit: skip=0, fetch=1
15871587
Sort: t1.c1 ASC NULLS FIRST
15881588
Inner Join: t1.c1 = t2.c1
1589-
TableScan: t1
1590-
TableScan: t2
1591-
"###
1589+
SubqueryAlias: t1
1590+
Projection: aggregate_test_100.c1
1591+
TableScan: aggregate_test_100
1592+
SubqueryAlias: t2
1593+
Projection: aggregate_test_100.c1
1594+
TableScan: aggregate_test_100
1595+
"
15921596
);
15931597

15941598
assert_snapshot!(
@@ -1748,14 +1752,18 @@ async fn with_column_renamed_join() -> Result<()> {
17481752

17491753
assert_snapshot!(
17501754
df_renamed.logical_plan(),
1751-
@r###"
1755+
@r"
17521756
Projection: t1.c1 AS AAA, t1.c2, t1.c3, t2.c1, t2.c2, t2.c3
17531757
Limit: skip=0, fetch=1
17541758
Sort: t1.c1 ASC NULLS FIRST, t1.c2 ASC NULLS FIRST, t1.c3 ASC NULLS FIRST, t2.c1 ASC NULLS FIRST, t2.c2 ASC NULLS FIRST, t2.c3 ASC NULLS FIRST
17551759
Inner Join: t1.c1 = t2.c1
1756-
TableScan: t1
1757-
TableScan: t2
1758-
"###
1760+
SubqueryAlias: t1
1761+
Projection: aggregate_test_100.c1, aggregate_test_100.c2, aggregate_test_100.c3
1762+
TableScan: aggregate_test_100
1763+
SubqueryAlias: t2
1764+
Projection: aggregate_test_100.c1, aggregate_test_100.c2, aggregate_test_100.c3
1765+
TableScan: aggregate_test_100
1766+
"
17591767
);
17601768

17611769
assert_snapshot!(

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,7 @@ impl LogicalPlanBuilder {
467467
projection: Option<Vec<usize>>,
468468
filters: Vec<Expr>,
469469
) -> Result<Self> {
470-
TableScan::try_new(table_name, table_source, projection, filters, None)
471-
.map(LogicalPlan::TableScan)
472-
.map(Self::new)
470+
Self::scan_with_filters_inner(table_name, table_source, projection, filters, None)
473471
}
474472

475473
/// Convert a table provider into a builder with a TableScan with filter and fetch
@@ -480,9 +478,37 @@ impl LogicalPlanBuilder {
480478
filters: Vec<Expr>,
481479
fetch: Option<usize>,
482480
) -> Result<Self> {
483-
TableScan::try_new(table_name, table_source, projection, filters, fetch)
484-
.map(LogicalPlan::TableScan)
485-
.map(Self::new)
481+
Self::scan_with_filters_inner(
482+
table_name,
483+
table_source,
484+
projection,
485+
filters,
486+
fetch,
487+
)
488+
}
489+
490+
fn scan_with_filters_inner(
491+
table_name: impl Into<TableReference>,
492+
table_source: Arc<dyn TableSource>,
493+
projection: Option<Vec<usize>>,
494+
filters: Vec<Expr>,
495+
fetch: Option<usize>,
496+
) -> Result<Self> {
497+
let table_scan =
498+
TableScan::try_new(table_name, table_source, projection, filters, fetch)?;
499+
500+
// Inline TableScan
501+
if table_scan.filters.is_empty() {
502+
if let Some(p) = table_scan.source.get_logical_plan() {
503+
let sub_plan = p.into_owned();
504+
// Ensures that the reference to the inlined table remains the
505+
// same, meaning we don't have to change any of the parent nodes
506+
// that reference this table.
507+
return Self::new(sub_plan).alias(table_scan.table_name);
508+
}
509+
}
510+
511+
Ok(Self::new(LogicalPlan::TableScan(table_scan)))
486512
}
487513

488514
/// Wrap a plan in a window

datafusion/optimizer/src/analyzer/inline_table_scan.rs

Lines changed: 0 additions & 207 deletions
This file was deleted.

datafusion/optimizer/src/analyzer/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ use datafusion_common::Result;
2828
use datafusion_expr::expr_rewriter::FunctionRewrite;
2929
use datafusion_expr::{InvariantLevel, LogicalPlan};
3030

31-
use crate::analyzer::inline_table_scan::InlineTableScan;
3231
use crate::analyzer::resolve_grouping_function::ResolveGroupingFunction;
3332
use crate::analyzer::type_coercion::TypeCoercion;
3433
use crate::utils::log_plan;
3534

3635
use self::function_rewrite::ApplyFunctionRewrites;
3736

3837
pub mod function_rewrite;
39-
pub mod inline_table_scan;
4038
pub mod resolve_grouping_function;
4139
pub mod type_coercion;
4240

@@ -96,7 +94,6 @@ impl Analyzer {
9694
/// Create a new analyzer using the recommended list of rules
9795
pub fn new() -> Self {
9896
let rules: Vec<Arc<dyn AnalyzerRule + Send + Sync>> = vec![
99-
Arc::new(InlineTableScan::new()),
10097
Arc::new(ResolveGroupingFunction::new()),
10198
Arc::new(TypeCoercion::new()),
10299
];

datafusion/sqllogictest/test_files/ddl.slt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,29 @@ DROP TABLE t1;
855855

856856
statement ok
857857
DROP TABLE t2;
858+
859+
statement count 0
860+
create table t(a int) as values (1), (2), (3);
861+
862+
statement count 0
863+
create view v as select a, count(a) from t group by a;
864+
865+
query II rowsort
866+
select * from v;
867+
----
868+
1 1
869+
2 1
870+
3 1
871+
872+
query II rowsort
873+
select "count(t.a)", a from v;
874+
----
875+
1 1
876+
1 2
877+
1 3
878+
879+
statement count 0
880+
drop view v;
881+
882+
statement count 0
883+
drop table t;

datafusion/sqllogictest/test_files/explain.slt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ EXPLAIN VERBOSE SELECT a, b, c FROM simple_explain_test
174174
initial_logical_plan
175175
01)Projection: simple_explain_test.a, simple_explain_test.b, simple_explain_test.c
176176
02)--TableScan: simple_explain_test
177-
logical_plan after inline_table_scan SAME TEXT AS ABOVE
178177
logical_plan after resolve_grouping_function SAME TEXT AS ABOVE
179178
logical_plan after type_coercion SAME TEXT AS ABOVE
180179
analyzed_logical_plan SAME TEXT AS ABOVE

0 commit comments

Comments
 (0)