Converts an iterator of references [`ScalarValue`] into an [`ArrayRef`] corresponding to those values. For example, an iterator of [`ScalarValue::Int32`] would be converted to an [`Int32Array`]. Returns an error if the iterator is empty or if the [`ScalarValue`]s are not all the same type # Example ``` use arrow::array::{ArrayRef, BooleanArray}; use datafusion_common::ScalarValue; let scalars =
(
scalars: impl IntoIterator<Item = ScalarValue>,
)
| 2691 | /// assert_eq!(&array, &expected); |
| 2692 | /// ``` |
| 2693 | pub fn iter_to_array( |
| 2694 | scalars: impl IntoIterator<Item = ScalarValue>, |
| 2695 | ) -> Result<ArrayRef> { |
| 2696 | let mut scalars = scalars.into_iter().peekable(); |
| 2697 | |
| 2698 | // figure out the type based on the first element |
| 2699 | let data_type = match scalars.peek() { |
| 2700 | None => { |
| 2701 | return _exec_err!("Empty iterator passed to ScalarValue::iter_to_array"); |
| 2702 | } |
| 2703 | Some(sv) => sv.data_type(), |
| 2704 | }; |
| 2705 | |
| 2706 | /// Creates an array of $ARRAY_TY by unpacking values of |
| 2707 | /// SCALAR_TY for primitive types |
| 2708 | macro_rules! build_array_primitive { |
| 2709 | ($ARRAY_TY:ident, $SCALAR_TY:ident) => {{ |
| 2710 | { |
| 2711 | let array = scalars |
| 2712 | .map(|sv| { |
| 2713 | if let ScalarValue::$SCALAR_TY(v) = sv { |
| 2714 | Ok(v) |
| 2715 | } else { |
| 2716 | _exec_err!( |
| 2717 | "Inconsistent types in ScalarValue::iter_to_array. \ |
| 2718 | Expected {:?}, got {:?}", |
| 2719 | data_type, |
| 2720 | sv |
| 2721 | ) |
| 2722 | } |
| 2723 | }) |
| 2724 | .collect::<Result<$ARRAY_TY>>()?; |
| 2725 | Arc::new(array) |
| 2726 | } |
| 2727 | }}; |
| 2728 | } |
| 2729 | |
| 2730 | macro_rules! build_array_primitive_tz { |
| 2731 | ($ARRAY_TY:ident, $SCALAR_TY:ident, $TZ:expr) => {{ |
| 2732 | { |
| 2733 | let array = scalars |
| 2734 | .map(|sv| { |
| 2735 | if let ScalarValue::$SCALAR_TY(v, _) = sv { |
| 2736 | Ok(v) |
| 2737 | } else { |
| 2738 | _exec_err!( |
| 2739 | "Inconsistent types in ScalarValue::iter_to_array. \ |
| 2740 | Expected {:?}, got {:?}", |
| 2741 | data_type, |
| 2742 | sv |
| 2743 | ) |
| 2744 | } |
| 2745 | }) |
| 2746 | .collect::<Result<$ARRAY_TY>>()?; |
| 2747 | Arc::new(array.with_timezone_opt($TZ.clone())) |
| 2748 | } |
| 2749 | }}; |
| 2750 | } |