-
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
Support Arrays for the Map scalar functions #11712
Changes from all commits
70e46e2
05c8ebe
03b3b4d
119b71d
ea76397
9f43c5e
b97ff6d
0b2afc9
c503287
436ebf4
e4da847
aa52b76
85a2754
4b083b6
5e824d6
9e93537
6f126fc
8998cd8
12e6834
d8d5012
3f9d336
aaf7e4f
857bed6
247c93a
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 |
---|---|---|
|
@@ -15,17 +15,20 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::make_array::make_array; | ||
use std::any::Any; | ||
use std::collections::VecDeque; | ||
use std::sync::Arc; | ||
|
||
use arrow::array::ArrayData; | ||
use arrow_array::{Array, ArrayRef, MapArray, StructArray}; | ||
use arrow_array::{Array, ArrayRef, MapArray, OffsetSizeTrait, StructArray}; | ||
use arrow_buffer::{Buffer, ToByteSlice}; | ||
use arrow_schema::{DataType, Field, SchemaBuilder}; | ||
|
||
use datafusion_common::{exec_err, ScalarValue}; | ||
use datafusion_expr::expr::ScalarFunction; | ||
use datafusion_expr::{ColumnarValue, Expr, ScalarUDFImpl, Signature, Volatility}; | ||
use std::any::Any; | ||
use std::collections::VecDeque; | ||
use std::sync::Arc; | ||
|
||
use crate::make_array::make_array; | ||
|
||
/// Returns a map created from a key list and a value list | ||
pub fn map(keys: Vec<Expr>, values: Vec<Expr>) -> Expr { | ||
|
@@ -56,11 +59,11 @@ fn make_map_batch(args: &[ColumnarValue]) -> datafusion_common::Result<ColumnarV | |
); | ||
} | ||
|
||
let data_type = args[0].data_type(); | ||
let can_evaluate_to_const = can_evaluate_to_const(args); | ||
|
||
let key = get_first_array_ref(&args[0])?; | ||
let value = get_first_array_ref(&args[1])?; | ||
make_map_batch_internal(key, value, can_evaluate_to_const) | ||
make_map_batch_internal(key, value, can_evaluate_to_const, data_type) | ||
} | ||
|
||
fn get_first_array_ref( | ||
|
@@ -73,14 +76,15 @@ fn get_first_array_ref( | |
ScalarValue::FixedSizeList(array) => Ok(array.value(0)), | ||
_ => exec_err!("Expected array, got {:?}", value), | ||
}, | ||
ColumnarValue::Array(array) => exec_err!("Expected scalar, got {:?}", array), | ||
ColumnarValue::Array(array) => Ok(array.to_owned()), | ||
} | ||
} | ||
|
||
fn make_map_batch_internal( | ||
keys: ArrayRef, | ||
values: ArrayRef, | ||
can_evaluate_to_const: bool, | ||
data_type: DataType, | ||
) -> datafusion_common::Result<ColumnarValue> { | ||
if keys.null_count() > 0 { | ||
return exec_err!("map key cannot be null"); | ||
|
@@ -90,6 +94,14 @@ fn make_map_batch_internal( | |
return exec_err!("map requires key and value lists to have the same length"); | ||
} | ||
|
||
if !can_evaluate_to_const { | ||
return if let DataType::LargeList(..) = data_type { | ||
make_map_array_internal::<i64>(keys, values) | ||
} else { | ||
make_map_array_internal::<i32>(keys, values) | ||
}; | ||
} | ||
|
||
let key_field = Arc::new(Field::new("key", keys.data_type().clone(), false)); | ||
let value_field = Arc::new(Field::new("value", values.data_type().clone(), true)); | ||
let mut entry_struct_buffer: VecDeque<(Arc<Field>, ArrayRef)> = VecDeque::new(); | ||
|
@@ -190,7 +202,6 @@ impl ScalarUDFImpl for MapFunc { | |
make_map_batch(args) | ||
} | ||
} | ||
|
||
fn get_element_type(data_type: &DataType) -> datafusion_common::Result<&DataType> { | ||
match data_type { | ||
DataType::List(element) => Ok(element.data_type()), | ||
|
@@ -202,3 +213,115 @@ fn get_element_type(data_type: &DataType) -> datafusion_common::Result<&DataType | |
), | ||
} | ||
} | ||
|
||
/// Helper function to create MapArray from array of values to support arrays for Map scalar function | ||
/// | ||
/// ``` text | ||
/// Format of input KEYS and VALUES column | ||
/// keys values | ||
/// +---------------------+ +---------------------+ | ||
/// | +-----------------+ | | +-----------------+ | | ||
/// | | [k11, k12, k13] | | | | [v11, v12, v13] | | | ||
/// | +-----------------+ | | +-----------------+ | | ||
/// | | | | | ||
/// | +-----------------+ | | +-----------------+ | | ||
/// | | [k21, k22, k23] | | | | [v21, v22, v23] | | | ||
/// | +-----------------+ | | +-----------------+ | | ||
/// | | | | | ||
/// | +-----------------+ | | +-----------------+ | | ||
/// | |[k31, k32, k33] | | | |[v31, v32, v33] | | | ||
/// | +-----------------+ | | +-----------------+ | | ||
/// +---------------------+ +---------------------+ | ||
/// ``` | ||
/// Flattened keys and values array to user create `StructArray`, | ||
/// which serves as inner child for `MapArray` | ||
/// | ||
/// ``` text | ||
/// Flattened Flattened | ||
/// Keys Values | ||
/// +-----------+ +-----------+ | ||
/// | +-------+ | | +-------+ | | ||
/// | | k11 | | | | v11 | | | ||
/// | +-------+ | | +-------+ | | ||
/// | +-------+ | | +-------+ | | ||
/// | | k12 | | | | v12 | | | ||
/// | +-------+ | | +-------+ | | ||
/// | +-------+ | | +-------+ | | ||
/// | | k13 | | | | v13 | | | ||
/// | +-------+ | | +-------+ | | ||
/// | +-------+ | | +-------+ | | ||
/// | | k21 | | | | v21 | | | ||
/// | +-------+ | | +-------+ | | ||
/// | +-------+ | | +-------+ | | ||
/// | | k22 | | | | v22 | | | ||
/// | +-------+ | | +-------+ | | ||
/// | +-------+ | | +-------+ | | ||
/// | | k23 | | | | v23 | | | ||
/// | +-------+ | | +-------+ | | ||
/// | +-------+ | | +-------+ | | ||
/// | | k31 | | | | v31 | | | ||
/// | +-------+ | | +-------+ | | ||
/// | +-------+ | | +-------+ | | ||
/// | | k32 | | | | v32 | | | ||
/// | +-------+ | | +-------+ | | ||
/// | +-------+ | | +-------+ | | ||
/// | | k33 | | | | v33 | | | ||
/// | +-------+ | | +-------+ | | ||
/// +-----------+ +-----------+ | ||
/// ```text | ||
|
||
fn make_map_array_internal<O: OffsetSizeTrait>( | ||
keys: ArrayRef, | ||
values: ArrayRef, | ||
) -> datafusion_common::Result<ColumnarValue> { | ||
let mut offset_buffer = vec![O::zero()]; | ||
let mut running_offset = O::zero(); | ||
|
||
let keys = datafusion_common::utils::list_to_arrays::<O>(keys); | ||
let values = datafusion_common::utils::list_to_arrays::<O>(values); | ||
|
||
let mut key_array_vec = vec![]; | ||
let mut value_array_vec = vec![]; | ||
for (k, v) in keys.iter().zip(values.iter()) { | ||
running_offset = running_offset.add(O::usize_as(k.len())); | ||
offset_buffer.push(running_offset); | ||
key_array_vec.push(k.as_ref()); | ||
value_array_vec.push(v.as_ref()); | ||
} | ||
|
||
// concatenate all the arrays | ||
let flattened_keys = arrow::compute::concat(key_array_vec.as_ref())?; | ||
if flattened_keys.null_count() > 0 { | ||
return exec_err!("keys cannot be null"); | ||
} | ||
let flattened_values = arrow::compute::concat(value_array_vec.as_ref())?; | ||
Comment on lines
+283
to
+297
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. Could we use 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. Under the hood, |
||
|
||
let fields = vec![ | ||
Arc::new(Field::new("key", flattened_keys.data_type().clone(), false)), | ||
Arc::new(Field::new( | ||
"value", | ||
flattened_values.data_type().clone(), | ||
true, | ||
)), | ||
]; | ||
|
||
let struct_data = ArrayData::builder(DataType::Struct(fields.into())) | ||
.len(flattened_keys.len()) | ||
.add_child_data(flattened_keys.to_data()) | ||
.add_child_data(flattened_values.to_data()) | ||
.build()?; | ||
|
||
let map_data = ArrayData::builder(DataType::Map( | ||
Arc::new(Field::new( | ||
"entries", | ||
struct_data.data_type().clone(), | ||
false, | ||
)), | ||
false, | ||
)) | ||
.len(keys.len()) | ||
.add_child_data(struct_data) | ||
.add_buffer(Buffer::from_slice_ref(offset_buffer.as_slice())) | ||
.build()?; | ||
Ok(ColumnarValue::Array(Arc::new(MapArray::from(map_data)))) | ||
} |
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.
😍