-
Notifications
You must be signed in to change notification settings - Fork 415
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: casting when data to be written does not match table schema #1427
Changes from 3 commits
36ba8be
9b95848
d1d0a25
ba83dbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,30 +221,34 @@ pub(crate) async fn write_execution_plan( | |
.current_metadata() | ||
.and_then(|meta| meta.schema.get_invariants().ok()) | ||
.unwrap_or_default(); | ||
|
||
// Use input schema to prevent wrapping partitions columns into a dictionary. | ||
let schema = snapshot.input_schema().unwrap_or(plan.schema()); | ||
|
||
let checker = DeltaDataChecker::new(invariants); | ||
|
||
// Write data to disk | ||
let mut tasks = vec![]; | ||
for i in 0..plan.output_partitioning().partition_count() { | ||
let inner_plan = plan.clone(); | ||
let inner_schema = schema.clone(); | ||
let task_ctx = Arc::new(TaskContext::from(&state)); | ||
let config = WriterConfig::new( | ||
inner_plan.schema(), | ||
inner_schema.clone(), | ||
partition_columns.clone(), | ||
writer_properties.clone(), | ||
target_file_size, | ||
write_batch_size, | ||
); | ||
let mut writer = DeltaWriter::new(object_store.clone(), config); | ||
let checker_stream = checker.clone(); | ||
let schema = inner_plan.schema().clone(); | ||
let mut stream = inner_plan.execute(i, task_ctx)?; | ||
let handle: tokio::task::JoinHandle<DeltaResult<Vec<Add>>> = | ||
tokio::task::spawn(async move { | ||
while let Some(maybe_batch) = stream.next().await { | ||
let batch = maybe_batch?; | ||
checker_stream.check_batch(&batch).await?; | ||
let arr = cast_record_batch(&batch, schema.clone())?; | ||
let arr = cast_record_batch(&batch, inner_schema.clone())?; | ||
writer.write(&arr).await?; | ||
} | ||
writer.close().await | ||
|
@@ -483,7 +487,12 @@ fn cast_record_batch( | |
mod tests { | ||
use super::*; | ||
use crate::operations::DeltaOps; | ||
use crate::writer::test_utils::datafusion::get_data; | ||
use crate::writer::test_utils::{get_delta_schema, get_record_batch}; | ||
use arrow::datatypes::Field; | ||
use arrow::datatypes::Schema as ArrowSchema; | ||
use arrow_array::{Int32Array, StringArray}; | ||
use datafusion::assert_batches_sorted_eq; | ||
use serde_json::json; | ||
|
||
#[tokio::test] | ||
|
@@ -526,6 +535,54 @@ mod tests { | |
assert_eq!(table.get_file_uris().count(), 1) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_write_different_types() { | ||
// Ensure write data is casted when data of a different type from the table is provided. | ||
let schema = Arc::new(ArrowSchema::new(vec![Field::new( | ||
"value", | ||
arrow::datatypes::DataType::Int32, | ||
true, | ||
)])); | ||
|
||
let batch = RecordBatch::try_new( | ||
Arc::clone(&schema), | ||
vec![Arc::new(Int32Array::from(vec![Some(0), None]))], | ||
) | ||
.unwrap(); | ||
let table = DeltaOps::new_in_memory().write(vec![batch]).await.unwrap(); | ||
|
||
let schema = Arc::new(ArrowSchema::new(vec![Field::new( | ||
"value", | ||
arrow::datatypes::DataType::Utf8, | ||
true, | ||
)])); | ||
|
||
let batch = RecordBatch::try_new( | ||
Arc::clone(&schema), | ||
vec![Arc::new(StringArray::from(vec![ | ||
Some("Test123".to_owned()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused, where does this value go in the expected table? 😕 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right below, in line 570? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it cannot be parsed as an int it will result in a null value. Which aligns with ansi sql but maybe we should only allow that if the user opts in? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh nevermind. It seems like this is being inserted as null? That doesn't seem like what a user would want. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think we could disable that by passing |
||
Some("123".to_owned()), | ||
None, | ||
]))], | ||
) | ||
.unwrap(); | ||
|
||
let table = DeltaOps::from(table).write(vec![batch]).await.unwrap(); | ||
let expected = [ | ||
"+-------+", | ||
"| value |", | ||
"+-------+", | ||
"| |", | ||
"| |", | ||
"| |", | ||
"| 123 |", | ||
"| 0 |", | ||
"+-------+", | ||
]; | ||
let actual = get_data(&table).await; | ||
assert_batches_sorted_eq!(&expected, &actual); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_write_nonexistent() { | ||
let batch = get_record_batch(None, false); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good refactor 👍