-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add arrow_field(expr) scalar UDF
#21389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
41f9312
Add `arrow_field(expr)` scalar UDF
adriangb c4e9831
Apply suggestions from code review
adriangb 79148eb
Fix arrow_field CI and expand tests
adriangb 4afd60a
lint
adriangb 98efc8e
Merge branch 'main' into add-arrow-field-udf
martin-g 217e00e
Address review: expand arrow_field doc example and add metadata tests
adriangb ca4a463
add nullability field access
adriangb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow::array::{ | ||
| Array, BooleanArray, MapBuilder, StringArray, StringBuilder, StructArray, | ||
| }; | ||
| use arrow::datatypes::{DataType, Field, Fields}; | ||
| use datafusion_common::{Result, ScalarValue, utils::take_function_args}; | ||
| use datafusion_expr::{ | ||
| ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature, | ||
| Volatility, | ||
| }; | ||
| use datafusion_macros::user_doc; | ||
| use std::sync::Arc; | ||
|
|
||
| #[user_doc( | ||
| doc_section(label = "Other Functions"), | ||
| description = "Returns a struct containing the Arrow field information of the expression, including name, data type, nullability, and metadata.", | ||
| syntax_example = "arrow_field(expression)", | ||
| sql_example = r#"```sql | ||
| > select arrow_field(1); | ||
| +----------------------------------------------+ | ||
| | arrow_field(Int64(1)) | | ||
| +----------------------------------------------+ | ||
| | {name: lit, data_type: Int64, ...} | | ||
| +----------------------------------------------+ | ||
|
|
||
| > select arrow_field(1)['data_type']; | ||
| +-----------------------------------+ | ||
| | arrow_field(Int64(1))[data_type] | | ||
| +-----------------------------------+ | ||
| | Int64 | | ||
| +-----------------------------------+ | ||
| ```"#, | ||
| argument( | ||
| name = "expression", | ||
| description = "Expression to evaluate. The expression can be a constant, column, or function, and any combination of operators." | ||
| ) | ||
| )] | ||
| #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
| pub struct ArrowFieldFunc { | ||
| signature: Signature, | ||
| } | ||
|
|
||
| impl Default for ArrowFieldFunc { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl ArrowFieldFunc { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::any(1, Volatility::Immutable), | ||
| } | ||
| } | ||
|
|
||
| fn return_struct_type() -> DataType { | ||
| DataType::Struct(Fields::from(vec![ | ||
| Field::new("name", DataType::Utf8, false), | ||
| Field::new("data_type", DataType::Utf8, false), | ||
| Field::new("nullable", DataType::Boolean, false), | ||
| Field::new( | ||
| "metadata", | ||
| DataType::Map( | ||
| Arc::new(Field::new( | ||
| "entries", | ||
| DataType::Struct(Fields::from(vec![ | ||
| Field::new("keys", DataType::Utf8, false), | ||
| Field::new("values", DataType::Utf8, true), | ||
| ])), | ||
| false, | ||
| )), | ||
| false, | ||
| ), | ||
| false, | ||
| ), | ||
| ])) | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for ArrowFieldFunc { | ||
| fn name(&self) -> &str { | ||
| "arrow_field" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| Ok(Self::return_struct_type()) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| let return_type = args.return_type().clone(); | ||
| let [field] = take_function_args(self.name(), args.arg_fields)?; | ||
|
|
||
| // Build the name array | ||
| let name_array = | ||
| Arc::new(StringArray::from(vec![field.name().as_str()])) as Arc<dyn Array>; | ||
|
|
||
| // Build the data_type array | ||
| let data_type_str = field.data_type().to_string(); | ||
| let data_type_array = | ||
| Arc::new(StringArray::from(vec![data_type_str.as_str()])) as Arc<dyn Array>; | ||
|
|
||
| // Build the nullable array | ||
| let nullable_array = | ||
| Arc::new(BooleanArray::from(vec![field.is_nullable()])) as Arc<dyn Array>; | ||
|
|
||
| // Build the metadata map array (same pattern as arrow_metadata.rs) | ||
| let metadata = field.metadata(); | ||
| let mut map_builder = | ||
| MapBuilder::new(None, StringBuilder::new(), StringBuilder::new()); | ||
|
|
||
| let mut entries: Vec<_> = metadata.iter().collect(); | ||
| entries.sort_by_key(|(k, _)| *k); | ||
|
|
||
| for (k, v) in entries { | ||
| map_builder.keys().append_value(k); | ||
| map_builder.values().append_value(v); | ||
| } | ||
| map_builder.append(true)?; | ||
|
|
||
| let metadata_array = Arc::new(map_builder.finish()) as Arc<dyn Array>; | ||
|
|
||
| // Build the struct | ||
| let DataType::Struct(fields) = return_type else { | ||
| unreachable!() | ||
| }; | ||
|
|
||
| let struct_array = StructArray::new( | ||
| fields, | ||
| vec![name_array, data_type_array, nullable_array, metadata_array], | ||
| None, | ||
| ); | ||
|
|
||
| Ok(ColumnarValue::Scalar(ScalarValue::try_from_array( | ||
| &struct_array, | ||
| 0, | ||
| )?)) | ||
| } | ||
|
|
||
| fn documentation(&self) -> Option<&Documentation> { | ||
| self.doc() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All tests use empty metadata. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| # arrow_field on integer literal | ||
| query ? | ||
| SELECT arrow_field(1) | ||
| ---- | ||
| {name: lit, data_type: Int64, nullable: false, metadata: {}} | ||
|
|
||
| # arrow_field on null literal | ||
| query ? | ||
| SELECT arrow_field(null) | ||
| ---- | ||
| {name: lit, data_type: Null, nullable: true, metadata: {}} | ||
|
|
||
| # arrow_field on boolean literal | ||
| query ? | ||
| SELECT arrow_field(true) | ||
| ---- | ||
| {name: lit, data_type: Boolean, nullable: false, metadata: {}} | ||
|
|
||
| # arrow_field on string literal | ||
| query ? | ||
| SELECT arrow_field('foo') | ||
| ---- | ||
| {name: lit, data_type: Utf8, nullable: false, metadata: {}} | ||
|
|
||
| # arrow_field on float literal | ||
| query ? | ||
| SELECT arrow_field(1.0) | ||
| ---- | ||
| {name: lit, data_type: Float64, nullable: false, metadata: {}} | ||
|
|
||
| # arrow_field on list | ||
| query ? | ||
| SELECT arrow_field(ARRAY[1,2,3]) | ||
| ---- | ||
| {name: lit, data_type: List(Int64), nullable: false, metadata: {}} | ||
|
martin-g marked this conversation as resolved.
|
||
|
|
||
| # arrow_field on map | ||
| query ? | ||
| SELECT arrow_field(MAP {'a': 1, 'b': 2}) | ||
| ---- | ||
| {name: lit, data_type: Map("entries": non-null Struct("key": non-null Utf8, "value": Int64), unsorted), nullable: false, metadata: {}} | ||
|
|
||
| # arrow_field on struct | ||
| query ? | ||
| SELECT arrow_field({a: 1, b: 'foo'}) | ||
| ---- | ||
| {name: lit, data_type: Struct("a": Int64, "b": Utf8), nullable: false, metadata: {}} | ||
|
|
||
| # arrow_field on dictionary | ||
| query ? | ||
| SELECT arrow_field(arrow_cast('foo', 'Dictionary(Int32, Utf8)')) | ||
| ---- | ||
| {name: lit, data_type: Dictionary(Int32, Utf8), nullable: false, metadata: {}} | ||
|
|
||
| # arrow_field struct field access - data_type | ||
| query T | ||
| SELECT arrow_field(1)['data_type'] | ||
| ---- | ||
| Int64 | ||
|
|
||
| # arrow_field struct field access - nullable | ||
| query B | ||
| SELECT arrow_field(1)['nullable'] | ||
| ---- | ||
| false | ||
|
|
||
| # arrow_field struct field access - name | ||
| query T | ||
| SELECT arrow_field(1)['name'] | ||
| ---- | ||
| lit | ||
|
|
||
| # arrow_field with table columns | ||
| statement ok | ||
| CREATE TABLE arrow_field_test(x INT NOT NULL, y TEXT) AS VALUES (1, 'a'); | ||
|
|
||
| query ? | ||
| SELECT arrow_field(x) FROM arrow_field_test | ||
| ---- | ||
| {name: x, data_type: Int32, nullable: false, metadata: {}} | ||
|
|
||
| query ? | ||
| SELECT arrow_field(y) FROM arrow_field_test | ||
| ---- | ||
| {name: y, data_type: Utf8View, nullable: true, metadata: {}} | ||
|
|
||
| # arrow_field column access - name reflects column name | ||
| query T | ||
| SELECT arrow_field(x)['name'] FROM arrow_field_test | ||
| ---- | ||
| x | ||
|
|
||
| # arrow_field column access - nullability | ||
| query B | ||
| SELECT arrow_field(x)['nullable'] FROM arrow_field_test | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT arrow_field(y)['nullable'] FROM arrow_field_test | ||
| ---- | ||
| true | ||
|
|
||
| statement ok | ||
| DROP TABLE arrow_field_test; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be too verbose to show the full output here (
, nullable: false, metadata: {}) instead of the ellipsis ?