-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add ColumnarValue::values_to_arrays
, deprecate columnar_values_to_array
#9114
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
use arrow::array::ArrayRef; | ||
use arrow::array::NullArray; | ||
use arrow::datatypes::DataType; | ||
use datafusion_common::{Result, ScalarValue}; | ||
use datafusion_common::{internal_err, DataFusionError, Result, ScalarValue}; | ||
use std::sync::Arc; | ||
|
||
/// Represents the result of evaluating an expression: either a single | ||
|
@@ -75,4 +75,166 @@ impl ColumnarValue { | |
pub fn create_null_array(num_rows: usize) -> Self { | ||
ColumnarValue::Array(Arc::new(NullArray::new(num_rows))) | ||
} | ||
|
||
/// Converts [`ColumnarValue`]s to [`ArrayRef`]s with the same length. | ||
/// | ||
/// # Performance Note | ||
/// | ||
/// This function expands any [`ScalarValue`] to an array. This expansion | ||
/// permits using a single function in terms of arrays, but it can be | ||
/// inefficient compared to handling the scalar value directly. | ||
/// | ||
/// Thus, it is recommended to provide specialized implementations for | ||
/// scalar values if performance is a concern. | ||
/// | ||
/// # Errors | ||
/// | ||
/// If there are multiple array arguments that have different lengths | ||
pub fn values_to_arrays(args: &[ColumnarValue]) -> Result<Vec<ArrayRef>> { | ||
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. This is a different algorithm than |
||
if args.is_empty() { | ||
return Ok(vec![]); | ||
} | ||
|
||
let mut array_len = None; | ||
for arg in args { | ||
array_len = match (arg, array_len) { | ||
(ColumnarValue::Array(a), None) => Some(a.len()), | ||
(ColumnarValue::Array(a), Some(array_len)) => { | ||
if array_len == a.len() { | ||
Some(array_len) | ||
} else { | ||
return internal_err!( | ||
"Arguments has mixed length. Expected length: {array_len}, found length: {}", a.len() | ||
); | ||
} | ||
} | ||
(ColumnarValue::Scalar(_), array_len) => array_len, | ||
} | ||
} | ||
|
||
// If array_len is none, it means there are only scalars, so make a 1 element array | ||
let inferred_length = array_len.unwrap_or(1); | ||
|
||
let args = args | ||
.iter() | ||
.map(|arg| arg.clone().into_array(inferred_length)) | ||
.collect::<Result<Vec<_>>>()?; | ||
|
||
Ok(args) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn values_to_arrays() { | ||
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. new tests |
||
// (input, expected) | ||
let cases = vec![ | ||
// empty | ||
TestCase { | ||
input: vec![], | ||
expected: vec![], | ||
}, | ||
// one array of length 3 | ||
TestCase { | ||
input: vec![ColumnarValue::Array(make_array(1, 3))], | ||
expected: vec![make_array(1, 3)], | ||
}, | ||
// two arrays length 3 | ||
TestCase { | ||
input: vec![ | ||
ColumnarValue::Array(make_array(1, 3)), | ||
ColumnarValue::Array(make_array(2, 3)), | ||
], | ||
expected: vec![make_array(1, 3), make_array(2, 3)], | ||
}, | ||
// array and scalar | ||
TestCase { | ||
input: vec![ | ||
ColumnarValue::Array(make_array(1, 3)), | ||
ColumnarValue::Scalar(ScalarValue::Int32(Some(100))), | ||
], | ||
expected: vec![ | ||
make_array(1, 3), | ||
make_array(100, 3), // scalar is expanded | ||
], | ||
}, | ||
// scalar and array | ||
TestCase { | ||
input: vec![ | ||
ColumnarValue::Scalar(ScalarValue::Int32(Some(100))), | ||
ColumnarValue::Array(make_array(1, 3)), | ||
], | ||
expected: vec![ | ||
make_array(100, 3), // scalar is expanded | ||
make_array(1, 3), | ||
], | ||
}, | ||
// multiple scalars and array | ||
TestCase { | ||
input: vec![ | ||
ColumnarValue::Scalar(ScalarValue::Int32(Some(100))), | ||
ColumnarValue::Array(make_array(1, 3)), | ||
ColumnarValue::Scalar(ScalarValue::Int32(Some(200))), | ||
], | ||
expected: vec![ | ||
make_array(100, 3), // scalar is expanded | ||
make_array(1, 3), | ||
make_array(200, 3), // scalar is expanded | ||
], | ||
}, | ||
]; | ||
for case in cases { | ||
case.run(); | ||
} | ||
} | ||
|
||
#[test] | ||
#[should_panic( | ||
expected = "Arguments has mixed length. Expected length: 3, found length: 4" | ||
)] | ||
fn values_to_arrays_mixed_length() { | ||
ColumnarValue::values_to_arrays(&[ | ||
ColumnarValue::Array(make_array(1, 3)), | ||
ColumnarValue::Array(make_array(2, 4)), | ||
]) | ||
.unwrap(); | ||
} | ||
|
||
#[test] | ||
#[should_panic( | ||
expected = "Arguments has mixed length. Expected length: 3, found length: 7" | ||
)] | ||
fn values_to_arrays_mixed_length_and_scalar() { | ||
ColumnarValue::values_to_arrays(&[ | ||
ColumnarValue::Array(make_array(1, 3)), | ||
ColumnarValue::Scalar(ScalarValue::Int32(Some(100))), | ||
ColumnarValue::Array(make_array(2, 7)), | ||
]) | ||
.unwrap(); | ||
} | ||
|
||
struct TestCase { | ||
input: Vec<ColumnarValue>, | ||
expected: Vec<ArrayRef>, | ||
} | ||
|
||
impl TestCase { | ||
fn run(self) { | ||
let Self { input, expected } = self; | ||
|
||
assert_eq!( | ||
ColumnarValue::values_to_arrays(&input).unwrap(), | ||
expected, | ||
"\ninput: {input:?}\nexpected: {expected:?}" | ||
); | ||
} | ||
} | ||
|
||
/// Makes an array of length `len` with all elements set to `val` | ||
fn make_array(val: i32, len: usize) -> ArrayRef { | ||
Arc::new(arrow::array::Int32Array::from(vec![val; len])) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is the basic change -- make the logic a function of
ColumnarValue