Zip two arrays by some boolean mask. - Where `mask` is `true`, values of `truthy` are taken - Where `mask` is `false` or `NULL`, values of `falsy` are taken # Example: `zip` two arrays ``` # use std::sync::Arc; # use arrow_array::{ArrayRef, BooleanArray, Int32Array}; # use arrow_select::zip::zip; // mask: [true, true, false, NULL, true] let mask = BooleanArray::from(vec![ Some(true), Some(true),
(
mask: &BooleanArray,
truthy: &dyn Datum,
falsy: &dyn Datum,
)
| 97 | /// assert_eq!(&result, &expected); |
| 98 | /// ``` |
| 99 | pub fn zip( |
| 100 | mask: &BooleanArray, |
| 101 | truthy: &dyn Datum, |
| 102 | falsy: &dyn Datum, |
| 103 | ) -> Result<ArrayRef, ArrowError> { |
| 104 | let (truthy_array, truthy_is_scalar) = truthy.get(); |
| 105 | let (falsy_array, falsy_is_scalar) = falsy.get(); |
| 106 | |
| 107 | if falsy_is_scalar && truthy_is_scalar { |
| 108 | let zipper = ScalarZipper::try_new(truthy, falsy)?; |
| 109 | return zipper.zip_impl.create_output(mask); |
| 110 | } |
| 111 | |
| 112 | let truthy = truthy_array; |
| 113 | let falsy = falsy_array; |
| 114 | |
| 115 | if truthy.data_type() != falsy.data_type() { |
| 116 | return Err(ArrowError::InvalidArgumentError( |
| 117 | "arguments need to have the same data type".into(), |
| 118 | )); |
| 119 | } |
| 120 | |
| 121 | if truthy_is_scalar && truthy.len() != 1 { |
| 122 | return Err(ArrowError::InvalidArgumentError( |
| 123 | "scalar arrays must have 1 element".into(), |
| 124 | )); |
| 125 | } |
| 126 | if !truthy_is_scalar && truthy.len() != mask.len() { |
| 127 | return Err(ArrowError::InvalidArgumentError( |
| 128 | "all arrays should have the same length".into(), |
| 129 | )); |
| 130 | } |
| 131 | if falsy_is_scalar && falsy.len() != 1 { |
| 132 | return Err(ArrowError::InvalidArgumentError( |
| 133 | "scalar arrays must have 1 element".into(), |
| 134 | )); |
| 135 | } |
| 136 | if !falsy_is_scalar && falsy.len() != mask.len() { |
| 137 | return Err(ArrowError::InvalidArgumentError( |
| 138 | "all arrays should have the same length".into(), |
| 139 | )); |
| 140 | } |
| 141 | |
| 142 | let falsy = falsy.to_data(); |
| 143 | let truthy = truthy.to_data(); |
| 144 | |
| 145 | zip_impl(mask, &truthy, truthy_is_scalar, &falsy, falsy_is_scalar) |
| 146 | } |
| 147 | |
| 148 | fn zip_impl( |
| 149 | mask: &BooleanArray, |