|
| 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::{MapBuilder, StringBuilder}; |
| 19 | +use arrow::datatypes::{DataType, Field, Fields}; |
| 20 | +use datafusion_common::{Result, ScalarValue, exec_err}; |
| 21 | +use datafusion_expr::{ |
| 22 | + ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature, |
| 23 | + Volatility, |
| 24 | +}; |
| 25 | +use datafusion_macros::user_doc; |
| 26 | +use std::any::Any; |
| 27 | +use std::sync::Arc; |
| 28 | + |
| 29 | +#[user_doc( |
| 30 | + doc_section(label = "Other Functions"), |
| 31 | + description = "Returns the metadata of the input expression. If a key is provided, returns the value for that key. If no key is provided, returns a Map of all metadata.", |
| 32 | + syntax_example = "arrow_metadata(expression, [key])", |
| 33 | + sql_example = r#"```sql |
| 34 | +> select arrow_metadata(col) from table; |
| 35 | ++----------------------------+ |
| 36 | +| arrow_metadata(table.col) | |
| 37 | ++----------------------------+ |
| 38 | +| {k: v} | |
| 39 | ++----------------------------+ |
| 40 | +> select arrow_metadata(col, 'k') from table; |
| 41 | ++-------------------------------+ |
| 42 | +| arrow_metadata(table.col, 'k')| |
| 43 | ++-------------------------------+ |
| 44 | +| v | |
| 45 | ++-------------------------------+ |
| 46 | +```"#, |
| 47 | + argument( |
| 48 | + name = "expression", |
| 49 | + description = "The expression to retrieve metadata from. Can be a column or other expression." |
| 50 | + ), |
| 51 | + argument( |
| 52 | + name = "key", |
| 53 | + description = "Optional. The specific metadata key to retrieve." |
| 54 | + ) |
| 55 | +)] |
| 56 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 57 | +pub struct ArrowMetadataFunc { |
| 58 | + signature: Signature, |
| 59 | +} |
| 60 | + |
| 61 | +impl ArrowMetadataFunc { |
| 62 | + pub fn new() -> Self { |
| 63 | + Self { |
| 64 | + signature: Signature::variadic_any(Volatility::Immutable), |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl Default for ArrowMetadataFunc { |
| 70 | + fn default() -> Self { |
| 71 | + Self::new() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl ScalarUDFImpl for ArrowMetadataFunc { |
| 76 | + fn as_any(&self) -> &dyn Any { |
| 77 | + self |
| 78 | + } |
| 79 | + |
| 80 | + fn name(&self) -> &str { |
| 81 | + "arrow_metadata" |
| 82 | + } |
| 83 | + |
| 84 | + fn signature(&self) -> &Signature { |
| 85 | + &self.signature |
| 86 | + } |
| 87 | + |
| 88 | + fn documentation(&self) -> Option<&Documentation> { |
| 89 | + self.doc() |
| 90 | + } |
| 91 | + |
| 92 | + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { |
| 93 | + if arg_types.len() == 2 { |
| 94 | + Ok(DataType::Utf8) |
| 95 | + } else if arg_types.len() == 1 { |
| 96 | + Ok(DataType::Map( |
| 97 | + Arc::new(Field::new( |
| 98 | + "entries", |
| 99 | + DataType::Struct(Fields::from(vec![ |
| 100 | + Field::new("keys", DataType::Utf8, false), |
| 101 | + Field::new("values", DataType::Utf8, true), |
| 102 | + ])), |
| 103 | + false, |
| 104 | + )), |
| 105 | + false, |
| 106 | + )) |
| 107 | + } else { |
| 108 | + exec_err!("arrow_metadata requires 1 or 2 arguments") |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { |
| 113 | + let metadata = args.arg_fields[0].metadata(); |
| 114 | + |
| 115 | + if args.args.len() == 2 { |
| 116 | + let key = match &args.args[1] { |
| 117 | + ColumnarValue::Scalar(ScalarValue::Utf8(Some(k))) => k, |
| 118 | + _ => { |
| 119 | + return exec_err!( |
| 120 | + "Second argument to arrow_metadata must be a string literal key" |
| 121 | + ); |
| 122 | + } |
| 123 | + }; |
| 124 | + let value = metadata.get(key).cloned(); |
| 125 | + Ok(ColumnarValue::Scalar(ScalarValue::Utf8(value))) |
| 126 | + } else if args.args.len() == 1 { |
| 127 | + let mut map_builder = |
| 128 | + MapBuilder::new(None, StringBuilder::new(), StringBuilder::new()); |
| 129 | + |
| 130 | + let mut entries: Vec<_> = metadata.iter().collect(); |
| 131 | + entries.sort_by_key(|(k, _)| *k); |
| 132 | + |
| 133 | + for (k, v) in entries { |
| 134 | + map_builder.keys().append_value(k); |
| 135 | + map_builder.values().append_value(v); |
| 136 | + } |
| 137 | + map_builder.append(true)?; |
| 138 | + |
| 139 | + let map_array = map_builder.finish(); |
| 140 | + |
| 141 | + Ok(ColumnarValue::Scalar(ScalarValue::try_from_array( |
| 142 | + &map_array, 0, |
| 143 | + )?)) |
| 144 | + } else { |
| 145 | + exec_err!("arrow_metadata requires 1 or 2 arguments") |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments