| 110 | } |
| 111 | |
| 112 | fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { |
| 113 | let [array, target_name] = take_function_args("union_extract", args.args)?; |
| 114 | |
| 115 | let target_name = match target_name { |
| 116 | ColumnarValue::Scalar(ScalarValue::Utf8(Some(target_name))) => { |
| 117 | Ok(target_name) |
| 118 | } |
| 119 | ColumnarValue::Scalar(ScalarValue::Utf8(None)) => exec_err!( |
| 120 | "union_extract second argument must be a non-null string literal, got a null instead" |
| 121 | ), |
| 122 | _ => exec_err!( |
| 123 | "union_extract second argument must be a non-null string literal, got {} instead", |
| 124 | target_name.data_type() |
| 125 | ), |
| 126 | }?; |
| 127 | |
| 128 | match array { |
| 129 | ColumnarValue::Array(array) => { |
| 130 | let union_array = as_union_array(&array).map_err(|_| { |
| 131 | exec_datafusion_err!( |
| 132 | "union_extract first argument must be a union, got {} instead", |
| 133 | array.data_type() |
| 134 | ) |
| 135 | })?; |
| 136 | |
| 137 | Ok(ColumnarValue::Array( |
| 138 | arrow::compute::kernels::union_extract::union_extract( |
| 139 | union_array, |
| 140 | &target_name, |
| 141 | )?, |
| 142 | )) |
| 143 | } |
| 144 | ColumnarValue::Scalar(ScalarValue::Union(value, fields, _)) => { |
| 145 | let (target_type_id, target) = find_field(&fields, &target_name)?; |
| 146 | |
| 147 | let result = match value { |
| 148 | Some((type_id, value)) if target_type_id == type_id => *value, |
| 149 | _ => ScalarValue::try_new_null(target.data_type())?, |
| 150 | }; |
| 151 | |
| 152 | Ok(ColumnarValue::Scalar(result)) |
| 153 | } |
| 154 | other => exec_err!( |
| 155 | "union_extract first argument must be a union, got {} instead", |
| 156 | other.data_type() |
| 157 | ), |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | fn documentation(&self) -> Option<&Documentation> { |
| 162 | self.doc() |