Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: schema adapter doesn't map partial batches correctly #2735

Merged
merged 2 commits into from
Aug 6, 2024
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
58 changes: 58 additions & 0 deletions crates/core/src/delta_datafusion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,64 @@ mod tests {
assert_batches_sorted_eq!(&expected, &actual);
}

#[tokio::test]
async fn delta_scan_supports_pushdown() {
let schema = Arc::new(ArrowSchema::new(vec![
Field::new("col_1", DataType::Utf8, false),
Field::new("col_2", DataType::Utf8, false),
]));

let batch = RecordBatch::try_new(
schema.clone(),
vec![
Arc::new(arrow::array::StringArray::from(vec![
Some("A"),
Some("B"),
Some("C"),
])),
Arc::new(arrow::array::StringArray::from(vec![
Some("A2"),
Some("B2"),
Some("C2"),
])),
],
)
.unwrap();

let table = crate::DeltaOps::new_in_memory()
.write(vec![batch])
.with_save_mode(crate::protocol::SaveMode::Append)
.await
.unwrap();

let config = DeltaScanConfigBuilder::new()
.build(table.snapshot().unwrap())
.unwrap();
let log = table.log_store();

let provider =
DeltaTableProvider::try_new(table.snapshot().unwrap().clone(), log, config).unwrap();

let mut cfg = SessionConfig::default();
cfg.options_mut().execution.parquet.pushdown_filters = true;
let ctx = SessionContext::new_with_config(cfg);
ctx.register_table("test", Arc::new(provider)).unwrap();

let df = ctx
.sql("select col_1, col_2 from test WHERE col_1 = 'A'")
.await
.unwrap();
let actual = df.collect().await.unwrap();
let expected = vec![
"+-------+-------+",
"| col_1 | col_2 |",
"+-------+-------+",
"| A | A2 |",
"+-------+-------+",
];
assert_batches_sorted_eq!(&expected, &actual);
}

#[tokio::test]
async fn delta_scan_supports_nested_missing_columns() {
let column1_schema1: arrow::datatypes::Fields =
Expand Down
14 changes: 13 additions & 1 deletion crates/core/src/delta_datafusion/schema_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ impl SchemaMapper for SchemaMapping {
}

fn map_partial_batch(&self, batch: RecordBatch) -> datafusion_common::Result<RecordBatch> {
let record_batch = cast_record_batch(&batch, self.table_schema.clone(), false, true)?;
let partial_table_schema = Arc::new(Schema::new(
batch
.schema()
.fields()
.iter()
.filter_map(|batch_field| {
self.table_schema.field_with_name(batch_field.name()).ok()
})
.cloned()
.collect::<Vec<_>>(),
));

let record_batch = cast_record_batch(&batch, partial_table_schema, false, true)?;
Ok(record_batch)
}
}
Loading