|
| 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, AsArray, DictionaryArray, Int8Array, StringArray}; |
| 19 | +use arrow::datatypes::DataType; |
| 20 | +use datafusion_common::utils::take_function_args; |
| 21 | +use datafusion_common::{exec_datafusion_err, exec_err, Result, ScalarValue}; |
| 22 | +use datafusion_doc::Documentation; |
| 23 | +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs}; |
| 24 | +use datafusion_expr::{ScalarUDFImpl, Signature, Volatility}; |
| 25 | +use datafusion_macros::user_doc; |
| 26 | +use std::sync::Arc; |
| 27 | + |
| 28 | +#[user_doc( |
| 29 | + doc_section(label = "Union Functions"), |
| 30 | + description = "Returns the name of the currently selected field in the union", |
| 31 | + syntax_example = "union_tag(union_expression)", |
| 32 | + sql_example = r#"```sql |
| 33 | +❯ select union_column, union_tag(union_column) from table_with_union; |
| 34 | ++--------------+-------------------------+ |
| 35 | +| union_column | union_tag(union_column) | |
| 36 | ++--------------+-------------------------+ |
| 37 | +| {a=1} | a | |
| 38 | +| {b=3.0} | b | |
| 39 | +| {a=4} | a | |
| 40 | +| {b=} | b | |
| 41 | +| {a=} | a | |
| 42 | ++--------------+-------------------------+ |
| 43 | +```"#, |
| 44 | + standard_argument(name = "union", prefix = "Union") |
| 45 | +)] |
| 46 | +#[derive(Debug)] |
| 47 | +pub struct UnionTagFunc { |
| 48 | + signature: Signature, |
| 49 | +} |
| 50 | + |
| 51 | +impl Default for UnionTagFunc { |
| 52 | + fn default() -> Self { |
| 53 | + Self::new() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl UnionTagFunc { |
| 58 | + pub fn new() -> Self { |
| 59 | + Self { |
| 60 | + signature: Signature::any(1, Volatility::Immutable), |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl ScalarUDFImpl for UnionTagFunc { |
| 66 | + fn as_any(&self) -> &dyn std::any::Any { |
| 67 | + self |
| 68 | + } |
| 69 | + |
| 70 | + fn name(&self) -> &str { |
| 71 | + "union_tag" |
| 72 | + } |
| 73 | + |
| 74 | + fn signature(&self) -> &Signature { |
| 75 | + &self.signature |
| 76 | + } |
| 77 | + |
| 78 | + fn return_type(&self, _: &[DataType]) -> Result<DataType> { |
| 79 | + Ok(DataType::Dictionary( |
| 80 | + Box::new(DataType::Int8), |
| 81 | + Box::new(DataType::Utf8), |
| 82 | + )) |
| 83 | + } |
| 84 | + |
| 85 | + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { |
| 86 | + let [union_] = take_function_args("union_tag", args.args)?; |
| 87 | + |
| 88 | + match union_ { |
| 89 | + ColumnarValue::Array(array) |
| 90 | + if matches!(array.data_type(), DataType::Union(_, _)) => |
| 91 | + { |
| 92 | + let union_array = array.as_union(); |
| 93 | + |
| 94 | + let keys = Int8Array::try_new(union_array.type_ids().clone(), None)?; |
| 95 | + |
| 96 | + let fields = match union_array.data_type() { |
| 97 | + DataType::Union(fields, _) => fields, |
| 98 | + _ => unreachable!(), |
| 99 | + }; |
| 100 | + |
| 101 | + // Union fields type IDs only constraints are being unique and in the 0..128 range: |
| 102 | + // They may not start at 0, be sequential, or even contiguous. |
| 103 | + // Therefore, we allocate a values vector with a length equal to the highest type ID plus one, |
| 104 | + // ensuring that each field's name can be placed at the index corresponding to its type ID. |
| 105 | + let values_len = fields |
| 106 | + .iter() |
| 107 | + .map(|(type_id, _)| type_id + 1) |
| 108 | + .max() |
| 109 | + .unwrap_or_default() as usize; |
| 110 | + |
| 111 | + let mut values = vec![""; values_len]; |
| 112 | + |
| 113 | + for (type_id, field) in fields.iter() { |
| 114 | + values[type_id as usize] = field.name().as_str() |
| 115 | + } |
| 116 | + |
| 117 | + let values = Arc::new(StringArray::from(values)); |
| 118 | + |
| 119 | + // SAFETY: union type_ids are validated to not be smaller than zero. |
| 120 | + // values len is the union biggest type id plus one. |
| 121 | + // keys is built from the union type_ids, which contains only valid type ids |
| 122 | + // therefore, `keys[i] >= values.len() || keys[i] < 0` never occurs |
| 123 | + let dict = unsafe { DictionaryArray::new_unchecked(keys, values) }; |
| 124 | + |
| 125 | + Ok(ColumnarValue::Array(Arc::new(dict))) |
| 126 | + } |
| 127 | + ColumnarValue::Scalar(ScalarValue::Union(value, fields, _)) => match value { |
| 128 | + Some((value_type_id, _)) => fields |
| 129 | + .iter() |
| 130 | + .find(|(type_id, _)| value_type_id == *type_id) |
| 131 | + .map(|(_, field)| { |
| 132 | + ColumnarValue::Scalar(ScalarValue::Dictionary( |
| 133 | + Box::new(DataType::Int8), |
| 134 | + Box::new(field.name().as_str().into()), |
| 135 | + )) |
| 136 | + }) |
| 137 | + .ok_or_else(|| { |
| 138 | + exec_datafusion_err!( |
| 139 | + "union_tag: union scalar with unknow type_id {value_type_id}" |
| 140 | + ) |
| 141 | + }), |
| 142 | + None => Ok(ColumnarValue::Scalar(ScalarValue::try_new_null( |
| 143 | + args.return_field.data_type(), |
| 144 | + )?)), |
| 145 | + }, |
| 146 | + v => exec_err!("union_tag only support unions, got {:?}", v.data_type()), |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + fn documentation(&self) -> Option<&Documentation> { |
| 151 | + self.doc() |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +#[cfg(test)] |
| 156 | +mod tests { |
| 157 | + use super::UnionTagFunc; |
| 158 | + use arrow::datatypes::{DataType, Field, UnionFields, UnionMode}; |
| 159 | + use datafusion_common::ScalarValue; |
| 160 | + use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl}; |
| 161 | + use std::sync::Arc; |
| 162 | + |
| 163 | + // when it becomes possible to construct union scalars in SQL, this should go to sqllogictests |
| 164 | + #[test] |
| 165 | + fn union_scalar() { |
| 166 | + let fields = [(0, Arc::new(Field::new("a", DataType::UInt32, false)))] |
| 167 | + .into_iter() |
| 168 | + .collect(); |
| 169 | + |
| 170 | + let scalar = ScalarValue::Union( |
| 171 | + Some((0, Box::new(ScalarValue::UInt32(Some(0))))), |
| 172 | + fields, |
| 173 | + UnionMode::Dense, |
| 174 | + ); |
| 175 | + |
| 176 | + let return_type = |
| 177 | + DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Utf8)); |
| 178 | + |
| 179 | + let result = UnionTagFunc::new() |
| 180 | + .invoke_with_args(ScalarFunctionArgs { |
| 181 | + args: vec![ColumnarValue::Scalar(scalar)], |
| 182 | + number_rows: 1, |
| 183 | + return_field: &Field::new("res", return_type, true), |
| 184 | + arg_fields: vec![], |
| 185 | + }) |
| 186 | + .unwrap(); |
| 187 | + |
| 188 | + assert_scalar( |
| 189 | + result, |
| 190 | + ScalarValue::Dictionary(Box::new(DataType::Int8), Box::new("a".into())), |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + #[test] |
| 195 | + fn union_scalar_empty() { |
| 196 | + let scalar = ScalarValue::Union(None, UnionFields::empty(), UnionMode::Dense); |
| 197 | + |
| 198 | + let return_type = |
| 199 | + DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Utf8)); |
| 200 | + |
| 201 | + let result = UnionTagFunc::new() |
| 202 | + .invoke_with_args(ScalarFunctionArgs { |
| 203 | + args: vec![ColumnarValue::Scalar(scalar)], |
| 204 | + number_rows: 1, |
| 205 | + return_field: &Field::new("res", return_type, true), |
| 206 | + arg_fields: vec![], |
| 207 | + }) |
| 208 | + .unwrap(); |
| 209 | + |
| 210 | + assert_scalar( |
| 211 | + result, |
| 212 | + ScalarValue::Dictionary( |
| 213 | + Box::new(DataType::Int8), |
| 214 | + Box::new(ScalarValue::Utf8(None)), |
| 215 | + ), |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + fn assert_scalar(value: ColumnarValue, expected: ScalarValue) { |
| 220 | + match value { |
| 221 | + ColumnarValue::Array(array) => panic!("expected scalar got {array:?}"), |
| 222 | + ColumnarValue::Scalar(scalar) => assert_eq!(scalar, expected), |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments