Process a map array by finding matching keys and extracting corresponding values. This function handles both simple (scalar) and nested key types by using appropriate comparison strategies.
(
array: &dyn Array,
key_array: Arc<dyn Array>,
)
| 102 | /// This function handles both simple (scalar) and nested key types by using |
| 103 | /// appropriate comparison strategies. |
| 104 | fn process_map_array( |
| 105 | array: &dyn Array, |
| 106 | key_array: Arc<dyn Array>, |
| 107 | ) -> Result<ColumnarValue> { |
| 108 | let map_array = as_map_array(array)?; |
| 109 | let keys = if key_array.data_type().is_nested() { |
| 110 | let comparator = make_comparator( |
| 111 | map_array.keys().as_ref(), |
| 112 | key_array.as_ref(), |
| 113 | SortOptions::default(), |
| 114 | )?; |
| 115 | let len = map_array.keys().len().min(key_array.len()); |
| 116 | let values = (0..len).map(|i| comparator(i, i).is_eq()).collect(); |
| 117 | let nulls = NullBuffer::union(map_array.keys().nulls(), key_array.nulls()); |
| 118 | BooleanArray::new(values, nulls) |
| 119 | } else { |
| 120 | let be_compared = Scalar::new(key_array); |
| 121 | arrow::compute::kernels::cmp::eq(&be_compared, map_array.keys())? |
| 122 | }; |
| 123 | |
| 124 | let original_data = map_array.entries().column(1).to_data(); |
| 125 | let capacity = Capacities::Array(original_data.len()); |
| 126 | let mut mutable = |
| 127 | MutableArrayData::with_capacities(vec![&original_data], true, capacity); |
| 128 | |
| 129 | for entry in 0..map_array.len() { |
| 130 | let start = map_array.value_offsets()[entry] as usize; |
| 131 | let end = map_array.value_offsets()[entry + 1] as usize; |
| 132 | |
| 133 | let maybe_matched = keys |
| 134 | .slice(start, end - start) |
| 135 | .iter() |
| 136 | .enumerate() |
| 137 | .find(|(_, t)| t.unwrap()); |
| 138 | |
| 139 | if maybe_matched.is_none() { |
| 140 | mutable.extend_nulls(1); |
| 141 | continue; |
| 142 | } |
| 143 | let (match_offset, _) = maybe_matched.unwrap(); |
| 144 | mutable.extend(0, start + match_offset, start + match_offset + 1); |
| 145 | } |
| 146 | |
| 147 | let data = mutable.freeze(); |
| 148 | let data = make_array(data); |
| 149 | Ok(ColumnarValue::Array(data)) |
| 150 | } |
| 151 | |
| 152 | /// Process a map array with a nested key type by iterating through entries |
| 153 | /// and using a comparator for key matching. |
no test coverage detected
searching dependent graphs…