Skip to content

Apache DataFusion SQL Query Engine

Resolve `ListingScan` projection against table schema including partition columns (#17911)

David Stancu1cc4dafbf916
diff --git a/datafusion/proto/src/logical_plan/mod.rs b/datafusion/proto/src/logical_plan/mod.rs
index 5cfcbc459b..fd9e07914b 100644
--- a/datafusion/proto/src/logical_plan/mod.rs
+++ b/datafusion/proto/src/logical_plan/mod.rs
@@ -377,16 +377,6 @@ impl AsLogicalPlan for LogicalPlanNode {
LogicalPlanType::ListingScan(scan) => {
let schema: Schema = convert_required!(scan.schema)?;
- let mut projection = None;
- if let Some(columns) = &scan.projection {
- let column_indices = columns
- .columns
- .iter()
- .map(|name| schema.index_of(name))
- .collect::<Result<Vec<usize>, _>>()?;
- projection = Some(column_indices);
- }
-
let filters =
from_proto::parse_exprs(&scan.filters, ctx, extension_codec)?;
@@ -494,6 +484,16 @@ impl AsLogicalPlan for LogicalPlanNode {
let table_name =
from_table_reference(scan.table_name.as_ref(), "ListingTableScan")?;
+ let mut projection = None;
+ if let Some(columns) = &scan.projection {
+ let column_indices = columns
+ .columns
+ .iter()
+ .map(|name| provider.schema().index_of(name))
+ .collect::<Result<Vec<usize>, _>>()?;
+ projection = Some(column_indices);
+ }
+
LogicalPlanBuilder::scan_with_filters(
table_name,
provider_as_source(Arc::new(provider)),
diff --git a/datafusion/proto/tests/cases/roundtrip_logical_plan.rs b/datafusion/proto/tests/cases/roundtrip_logical_plan.rs
index 72e9123b29..5080a33e03 100644
--- a/datafusion/proto/tests/cases/roundtrip_logical_plan.rs
+++ b/datafusion/proto/tests/cases/roundtrip_logical_plan.rs
@@ -46,6 +46,7 @@ use datafusion::datasource::file_format::arrow::ArrowFormatFactory;
use datafusion::datasource::file_format::csv::CsvFormatFactory;
use datafusion::datasource::file_format::parquet::ParquetFormatFactory;
use datafusion::datasource::file_format::{format_as_file_type, DefaultFileType};
+use datafusion::datasource::DefaultTableSource;
use datafusion::execution::session_state::SessionStateBuilder;
use datafusion::execution::FunctionRegistry;
use datafusion::functions_aggregate::count::count_udaf;
@@ -77,9 +78,9 @@ use datafusion_expr::expr::{
use datafusion_expr::logical_plan::{Extension, UserDefinedLogicalNodeCore};
use datafusion_expr::{
Accumulator, AggregateUDF, ColumnarValue, ExprFunctionExt, ExprSchemable, Literal,
- LogicalPlan, Operator, PartitionEvaluator, ScalarUDF, Signature, TryCast, Volatility,
- WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition, WindowUDF,
- WindowUDFImpl,
+ LogicalPlan, LogicalPlanBuilder, Operator, PartitionEvaluator, ScalarUDF, Signature,
+ TryCast, Volatility, WindowFrame, WindowFrameBound, WindowFrameUnits,
+ WindowFunctionDefinition, WindowUDF, WindowUDFImpl,
};
use datafusion_functions_aggregate::average::avg_udaf;
use datafusion_functions_aggregate::expr_fn::{
@@ -2669,6 +2670,44 @@ async fn roundtrip_custom_listing_tables_schema() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn roundtrip_custom_listing_tables_schema_table_scan_projection() -> Result<()> {
+ let ctx = SessionContext::new();
+ // Make sure during round-trip, constraint information is preserved
+ let file_format = JsonFormat::default();
+ let table_partition_cols = vec![("part".to_owned(), DataType::Int64)];
+ let data = "../core/tests/data/partitioned_table_json";
+ let listing_table_url = ListingTableUrl::parse(data)?;
+ let listing_options = ListingOptions::new(Arc::new(file_format))
+ .with_table_partition_cols(table_partition_cols);
+
+ let config = ListingTableConfig::new(listing_table_url)
+ .with_listing_options(listing_options)
+ .infer_schema(&ctx.state())
+ .await?;
+
+ let listing_table: Arc<dyn TableProvider> = Arc::new(ListingTable::try_new(config)?);
+
+ let projection = ["part", "value"]
+ .iter()
+ .map(|field_name| listing_table.schema().index_of(field_name))
+ .collect::<Result<Vec<_>, _>>()?;
+
+ let plan = LogicalPlanBuilder::scan(
+ "hive_style",
+ Arc::new(DefaultTableSource::new(listing_table)),
+ Some(projection),
+ )?
+ .limit(0, Some(1))?
+ .build()?;
+
+ let bytes = logical_plan_to_bytes(&plan)?;
+ let new_plan = logical_plan_from_bytes(&bytes, &ctx)?;
+
+ assert_eq!(plan, new_plan);
+ Ok(())
+}
+
#[tokio::test]
async fn roundtrip_arrow_scan() -> Result<()> {
let ctx = SessionContext::new();