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: failed to create ValuesExec with non-nullable schema #8776

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 22 additions & 12 deletions datafusion/physical-plan/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ use crate::{
PhysicalExpr,
};

use arrow::array::new_null_array;
use arrow::datatypes::SchemaRef;
use arrow::record_batch::RecordBatch;
use arrow::datatypes::{Schema, SchemaRef};
use arrow::record_batch::{RecordBatch, RecordBatchOptions};
use datafusion_common::{internal_err, plan_err, DataFusionError, Result, ScalarValue};
use datafusion_execution::TaskContext;

Expand All @@ -53,15 +52,14 @@ impl ValuesExec {
}
let n_row = data.len();
let n_col = schema.fields().len();
// we have this single row, null, typed batch as a placeholder to satisfy evaluation argument
let batch = RecordBatch::try_new(
schema.clone(),
schema
.fields()
.iter()
.map(|field| new_null_array(field.data_type(), 1))
.collect::<Vec<_>>(),
// we have this single row batch as a placeholder to satisfy evaluation argument
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 this code to use a null array probably pre-dates the ability to create zero column record batches

// and generate a single output row
let batch = RecordBatch::try_new_with_options(
Arc::new(Schema::empty()),
vec![],
&RecordBatchOptions::new().with_row_count(Some(1)),
)?;

let arr = (0..n_col)
.map(|j| {
(0..n_row)
Expand All @@ -71,7 +69,7 @@ impl ValuesExec {
match r {
Ok(ColumnarValue::Scalar(scalar)) => Ok(scalar),
Ok(ColumnarValue::Array(a)) if a.len() == 1 => {
Ok(ScalarValue::List(a))
ScalarValue::try_from_array(&a, 0)
Copy link
Member Author

Choose a reason for hiding this comment

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

Returning ScalarValue::List(a) makes the following query fail.

DataFusion CLI v34.0.0
❯ select * from (values(random()));
Internal error: Inconsistent types in ScalarValue::iter_to_array. Expected Float64, got List(0.5174512019067328).
This was likely caused by a bug in DataFusion's code and we would welcome that you file an bug report in our issue tracker

Related to pr #7629. I think it's a bug, so I fixed it.

}
Ok(ColumnarValue::Array(a)) => {
plan_err!(
Expand Down Expand Up @@ -201,6 +199,7 @@ impl ExecutionPlan for ValuesExec {
#[cfg(test)]
mod tests {
use super::*;
use crate::expressions::lit;
use crate::test::{self, make_partition};

use arrow_schema::{DataType, Field, Schema};
Expand Down Expand Up @@ -240,4 +239,15 @@ mod tests {
]));
let _ = ValuesExec::try_new_from_batches(invalid_schema, batches).unwrap_err();
}

// Test issue: https://github.com/apache/arrow-datafusion/issues/8763
#[test]
fn new_exec_with_non_nullable_schema() {
let schema = Arc::new(Schema::new(vec![Field::new(
"col0",
DataType::UInt32,
false,
)]));
let _ = ValuesExec::try_new(schema, vec![vec![lit(1u32)]]).unwrap();
}
}
8 changes: 8 additions & 0 deletions datafusion/sqllogictest/test_files/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ VALUES (1,2,3,4,5,6,7,8,9,10,11,12,13,NULL,'F',3.5)
----
1 2 3 4 5 6 7 8 9 10 11 12 13 NULL F 3.5

# Test non-literal expressions in VALUES
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

query II
VALUES (1, CASE WHEN RANDOM() > 0.5 THEN 1 ELSE 1 END),
(2, CASE WHEN RANDOM() > 0.5 THEN 2 ELSE 2 END);
----
1 1
2 2

query IT
SELECT * FROM (VALUES (1,'a'),(2,NULL)) AS t(c1, c2)
----
Expand Down
Loading