Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
20 changes: 10 additions & 10 deletions datafusion/proto/src/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,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)?;

Expand Down Expand Up @@ -496,6 +486,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)),
Expand Down
45 changes: 42 additions & 3 deletions datafusion/proto/tests/cases/roundtrip_logical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::{
Expand Down Expand Up @@ -2673,6 +2674,44 @@ async fn roundtrip_custom_listing_tables_schema() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn roundtrip_custom_listing_tables_schema_table_scan_projection() -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I verified this test fails without the code changes in this PR:

Error: ArrowError(SchemaError("Unable to get field named \"part\". Valid fields: [\"id\", \"value\"]"), Some(""))

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();
Expand Down