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
33 changes: 33 additions & 0 deletions datafusion/core/tests/physical_optimizer/projection_pushdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::collections::HashMap;
use std::sync::Arc;

use arrow::compute::SortOptions;
Expand Down Expand Up @@ -529,6 +530,38 @@ fn test_memory_after_projection() -> Result<()> {
Ok(())
}

#[test]
fn test_memory_projection_preserves_field_metadata() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new(
"value",
DataType::Int32,
true,
)]));
let memory = MemorySourceConfig::try_new_exec(&[], Arc::clone(&schema), None)?;
let projected_schema = Schema::new(vec![
Field::new("value", DataType::Int32, true).with_metadata(HashMap::from([(
"semantic_type".to_string(),
"example".to_string(),
)])),
]);
let projection: Arc<dyn ExecutionPlan> =
Arc::new(ProjectionExec::try_new_with_schema_metadata(
vec![ProjectionExpr::new(
Arc::new(Column::new("value", 0)),
"value",
)],
memory,
&projected_schema,
)?);

let optimized =
ProjectionPushdown::new().optimize(projection, &ConfigOptions::new())?;

assert!(optimized.is::<ProjectionExec>());
assert_eq!(optimized.schema().as_ref(), &projected_schema);
Ok(())
}

#[test]
fn test_streaming_table_after_projection() -> Result<()> {
#[derive(Debug)]
Expand Down
8 changes: 7 additions & 1 deletion datafusion/datasource/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,13 @@ impl ExecutionPlan for DataSourceExec {
.try_swapping_with_projection(projection.projection_expr())?
{
Some(new_data_source) => {
Ok(Some(Arc::new(DataSourceExec::new(new_data_source))))
let new_exec = Arc::new(DataSourceExec::new(new_data_source));
// The data source API does not receive the projection's output metadata.
if projection.schema() == new_exec.schema() {
Ok(Some(new_exec))
} else {
Ok(None)
}
}
None => Ok(None),
}
Expand Down
Loading