|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use arrow::array::Array; |
| 19 | +use arrow::datatypes::{DataType, FieldRef, UnionFields}; |
| 20 | +use datafusion_common::cast::as_union_array; |
| 21 | +use datafusion_common::{ |
| 22 | + exec_datafusion_err, exec_err, internal_err, Result, ScalarValue, |
| 23 | +}; |
| 24 | +use datafusion_doc::Documentation; |
| 25 | +use datafusion_expr::{ColumnarValue, ReturnInfo, ReturnTypeArgs, ScalarFunctionArgs}; |
| 26 | +use datafusion_expr::{ScalarUDFImpl, Signature, Volatility}; |
| 27 | +use datafusion_macros::user_doc; |
| 28 | + |
| 29 | +#[user_doc( |
| 30 | + doc_section(label = "Union Functions"), |
| 31 | + description = "Returns the value of the given field in the union when selected, or NULL otherwise.", |
| 32 | + syntax_example = "union_extract(union, field_name)", |
| 33 | + sql_example = r#"```sql |
| 34 | +❯ select union_column, union_extract(union_column, 'a'), union_extract(union_column, 'b') from table_with_union; |
| 35 | ++--------------+----------------------------------+----------------------------------+ |
| 36 | +| union_column | union_extract(union_column, 'a') | union_extract(union_column, 'b') | |
| 37 | ++--------------+----------------------------------+----------------------------------+ |
| 38 | +| {a=1} | 1 | | |
| 39 | +| {b=3.0} | | 3.0 | |
| 40 | +| {a=4} | 4 | | |
| 41 | +| {b=} | | | |
| 42 | +| {a=} | | | |
| 43 | ++--------------+----------------------------------+----------------------------------+ |
| 44 | +```"#, |
| 45 | + standard_argument(name = "union", prefix = "Union"), |
| 46 | + argument( |
| 47 | + name = "field_name", |
| 48 | + description = "String expression to operate on. Must be a constant." |
| 49 | + ) |
| 50 | +)] |
| 51 | +#[derive(Debug)] |
| 52 | +pub struct UnionExtractFun { |
| 53 | + signature: Signature, |
| 54 | +} |
| 55 | + |
| 56 | +impl Default for UnionExtractFun { |
| 57 | + fn default() -> Self { |
| 58 | + Self::new() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl UnionExtractFun { |
| 63 | + pub fn new() -> Self { |
| 64 | + Self { |
| 65 | + signature: Signature::any(2, Volatility::Immutable), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl ScalarUDFImpl for UnionExtractFun { |
| 71 | + fn as_any(&self) -> &dyn std::any::Any { |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + fn name(&self) -> &str { |
| 76 | + "union_extract" |
| 77 | + } |
| 78 | + |
| 79 | + fn signature(&self) -> &Signature { |
| 80 | + &self.signature |
| 81 | + } |
| 82 | + |
| 83 | + fn return_type(&self, _: &[DataType]) -> Result<DataType> { |
| 84 | + // should be using return_type_from_exprs and not calling the default implementation |
| 85 | + internal_err!("union_extract should return type from exprs") |
| 86 | + } |
| 87 | + |
| 88 | + fn return_type_from_args(&self, args: ReturnTypeArgs) -> Result<ReturnInfo> { |
| 89 | + if args.arg_types.len() != 2 { |
| 90 | + return exec_err!( |
| 91 | + "union_extract expects 2 arguments, got {} instead", |
| 92 | + args.arg_types.len() |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + let DataType::Union(fields, _) = &args.arg_types[0] else { |
| 97 | + return exec_err!( |
| 98 | + "union_extract first argument must be a union, got {} instead", |
| 99 | + args.arg_types[0] |
| 100 | + ); |
| 101 | + }; |
| 102 | + |
| 103 | + let Some(ScalarValue::Utf8(Some(field_name))) = &args.scalar_arguments[1] else { |
| 104 | + return exec_err!( |
| 105 | + "union_extract second argument must be a non-null string literal, got {} instead", |
| 106 | + args.arg_types[1] |
| 107 | + ); |
| 108 | + }; |
| 109 | + |
| 110 | + let field = find_field(fields, field_name)?.1; |
| 111 | + |
| 112 | + Ok(ReturnInfo::new_nullable(field.data_type().clone())) |
| 113 | + } |
| 114 | + |
| 115 | + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { |
| 116 | + let args = args.args; |
| 117 | + |
| 118 | + if args.len() != 2 { |
| 119 | + return exec_err!( |
| 120 | + "union_extract expects 2 arguments, got {} instead", |
| 121 | + args.len() |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + let target_name = match &args[1] { |
| 126 | + ColumnarValue::Scalar(ScalarValue::Utf8(Some(target_name))) => Ok(target_name), |
| 127 | + ColumnarValue::Scalar(ScalarValue::Utf8(None)) => exec_err!("union_extract second argument must be a non-null string literal, got a null instead"), |
| 128 | + _ => exec_err!("union_extract second argument must be a non-null string literal, got {} instead", &args[1].data_type()), |
| 129 | + }; |
| 130 | + |
| 131 | + match &args[0] { |
| 132 | + ColumnarValue::Array(array) => { |
| 133 | + let union_array = as_union_array(&array).map_err(|_| { |
| 134 | + exec_datafusion_err!( |
| 135 | + "union_extract first argument must be a union, got {} instead", |
| 136 | + array.data_type() |
| 137 | + ) |
| 138 | + })?; |
| 139 | + |
| 140 | + Ok(ColumnarValue::Array( |
| 141 | + arrow::compute::kernels::union_extract::union_extract( |
| 142 | + union_array, |
| 143 | + target_name?, |
| 144 | + )?, |
| 145 | + )) |
| 146 | + } |
| 147 | + ColumnarValue::Scalar(ScalarValue::Union(value, fields, _)) => { |
| 148 | + let target_name = target_name?; |
| 149 | + let (target_type_id, target) = find_field(fields, target_name)?; |
| 150 | + |
| 151 | + let result = match value { |
| 152 | + Some((type_id, value)) if target_type_id == *type_id => { |
| 153 | + *value.clone() |
| 154 | + } |
| 155 | + _ => ScalarValue::try_from(target.data_type())?, |
| 156 | + }; |
| 157 | + |
| 158 | + Ok(ColumnarValue::Scalar(result)) |
| 159 | + } |
| 160 | + other => exec_err!( |
| 161 | + "union_extract first argument must be a union, got {} instead", |
| 162 | + other.data_type() |
| 163 | + ), |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + fn documentation(&self) -> Option<&Documentation> { |
| 168 | + self.doc() |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +fn find_field<'a>(fields: &'a UnionFields, name: &str) -> Result<(i8, &'a FieldRef)> { |
| 173 | + fields |
| 174 | + .iter() |
| 175 | + .find(|field| field.1.name() == name) |
| 176 | + .ok_or_else(|| exec_datafusion_err!("field {name} not found on union")) |
| 177 | +} |
| 178 | + |
| 179 | +#[cfg(test)] |
| 180 | +mod tests { |
| 181 | + |
| 182 | + use arrow::datatypes::{DataType, Field, UnionFields, UnionMode}; |
| 183 | + use datafusion_common::{Result, ScalarValue}; |
| 184 | + use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl}; |
| 185 | + |
| 186 | + use super::UnionExtractFun; |
| 187 | + |
| 188 | + // when it becomes possible to construct union scalars in SQL, this should go to sqllogictests |
| 189 | + #[test] |
| 190 | + fn test_scalar_value() -> Result<()> { |
| 191 | + let fun = UnionExtractFun::new(); |
| 192 | + |
| 193 | + let fields = UnionFields::new( |
| 194 | + vec![1, 3], |
| 195 | + vec![ |
| 196 | + Field::new("str", DataType::Utf8, false), |
| 197 | + Field::new("int", DataType::Int32, false), |
| 198 | + ], |
| 199 | + ); |
| 200 | + |
| 201 | + let result = fun.invoke_with_args(ScalarFunctionArgs { |
| 202 | + args: vec![ |
| 203 | + ColumnarValue::Scalar(ScalarValue::Union( |
| 204 | + None, |
| 205 | + fields.clone(), |
| 206 | + UnionMode::Dense, |
| 207 | + )), |
| 208 | + ColumnarValue::Scalar(ScalarValue::new_utf8("str")), |
| 209 | + ], |
| 210 | + number_rows: 1, |
| 211 | + return_type: &DataType::Utf8, |
| 212 | + })?; |
| 213 | + |
| 214 | + assert_scalar(result, ScalarValue::Utf8(None)); |
| 215 | + |
| 216 | + let result = fun.invoke_with_args(ScalarFunctionArgs { |
| 217 | + args: vec![ |
| 218 | + ColumnarValue::Scalar(ScalarValue::Union( |
| 219 | + Some((3, Box::new(ScalarValue::Int32(Some(42))))), |
| 220 | + fields.clone(), |
| 221 | + UnionMode::Dense, |
| 222 | + )), |
| 223 | + ColumnarValue::Scalar(ScalarValue::new_utf8("str")), |
| 224 | + ], |
| 225 | + number_rows: 1, |
| 226 | + return_type: &DataType::Utf8, |
| 227 | + })?; |
| 228 | + |
| 229 | + assert_scalar(result, ScalarValue::Utf8(None)); |
| 230 | + |
| 231 | + let result = fun.invoke_with_args(ScalarFunctionArgs { |
| 232 | + args: vec![ |
| 233 | + ColumnarValue::Scalar(ScalarValue::Union( |
| 234 | + Some((1, Box::new(ScalarValue::new_utf8("42")))), |
| 235 | + fields.clone(), |
| 236 | + UnionMode::Dense, |
| 237 | + )), |
| 238 | + ColumnarValue::Scalar(ScalarValue::new_utf8("str")), |
| 239 | + ], |
| 240 | + number_rows: 1, |
| 241 | + return_type: &DataType::Utf8, |
| 242 | + })?; |
| 243 | + |
| 244 | + assert_scalar(result, ScalarValue::new_utf8("42")); |
| 245 | + |
| 246 | + Ok(()) |
| 247 | + } |
| 248 | + |
| 249 | + fn assert_scalar(value: ColumnarValue, expected: ScalarValue) { |
| 250 | + match value { |
| 251 | + ColumnarValue::Array(array) => panic!("expected scalar got {array:?}"), |
| 252 | + ColumnarValue::Scalar(scalar) => assert_eq!(scalar, expected), |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments