Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions datafusion/core/tests/execution/logical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
//! create them and depend on them. Test executable semantics of logical plans.

use arrow::array::Int64Array;
use arrow::datatypes::{DataType, Field};
use arrow::datatypes::{DataType, Field, Schema};
use datafusion::datasource::{provider_as_source, ViewTable};
use datafusion::execution::session_state::SessionStateBuilder;
use datafusion_common::{Column, DFSchema, Result, ScalarValue, Spans};
use datafusion_common::{Column, DFSchema, DFSchemaRef, Result, ScalarValue, Spans};
use datafusion_execution::TaskContext;
use datafusion_expr::expr::{AggregateFunction, AggregateFunctionParams};
use datafusion_expr::logical_plan::{LogicalPlan, Values};
use datafusion_expr::{Aggregate, AggregateUDF, Expr};
use datafusion_expr::{
Aggregate, AggregateUDF, EmptyRelation, Expr, LogicalPlanBuilder, UNNAMED_TABLE,
};
use datafusion_functions_aggregate::count::Count;
use datafusion_physical_plan::collect;
use insta::assert_snapshot;
use std::collections::HashMap;
use std::fmt::Debug;
use std::ops::Deref;
Expand Down Expand Up @@ -96,3 +100,37 @@ where
};
element
}

#[test]
fn inline_scan_projection_test() -> Result<()> {
let name = UNNAMED_TABLE;
let column = "a";

let schema = Schema::new(vec![
Field::new("a", DataType::Int32, false),
Field::new("b", DataType::Int32, false),
]);
let projection = vec![schema.index_of(column)?];

let provider = ViewTable::new(
LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
schema: DFSchemaRef::new(DFSchema::try_from(schema)?),
}),
None,
);
let source = provider_as_source(Arc::new(provider));

let plan = LogicalPlanBuilder::scan(name, source, Some(projection))?.build()?;

assert_snapshot!(
format!("{plan}"),
@r"
Copy link
Contributor

Choose a reason for hiding this comment

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

May be it would be more readable to format like here

let expected = "Union\
\n EmptyRelation\
\n Aggregate: groupBy=[[test.a]], aggr=[[sum(test.b)]]\
\n TableScan: test";
?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you mean to move out the string to expected?

but for insta test, we keep the string inline to update easily

Copy link
Contributor

@vadimpiven vadimpiven Apr 24, 2025

Choose a reason for hiding this comment

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

Actually, I just thought that the correct test would be

assert_eq!(
        plan.schema().field_names(),
        vec![format!("{name}.{column}")]
    );

because what matters is that projection is applied, not how. Updated the code in my PR.

SubqueryAlias: ?table?
Projection: a
EmptyRelation
"
);

Ok(())
}
15 changes: 15 additions & 0 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,21 @@ impl LogicalPlanBuilder {
if table_scan.filters.is_empty() {
if let Some(p) = table_scan.source.get_logical_plan() {
let sub_plan = p.into_owned();

if let Some(proj) = table_scan.projection {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

let projection_exprs = proj
.into_iter()
.map(|i| {
Expr::Column(Column::from(
sub_plan.schema().qualified_field(i),
))
})
.collect::<Vec<_>>();
return Self::new(sub_plan)
.project(projection_exprs)?
.alias(table_scan.table_name);
}

// Ensures that the reference to the inlined table remains the
// same, meaning we don't have to change any of the parent nodes
// that reference this table.
Expand Down