-
Notifications
You must be signed in to change notification settings - Fork 0
21387: feat: add is_nullable scalar UDF #305
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| The AI agents should never link to any issue or a pull request | ||
| in any GitHub repository in the code reviews! | ||
|
|
||
| The AI agents should not review AI agents' config files like CLAUDE.md or AGENTS.md! | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,83 @@ | ||||||
| // 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::datatypes::DataType; | ||||||
| use datafusion_common::{Result, ScalarValue, utils::take_function_args}; | ||||||
| use datafusion_expr::{ColumnarValue, Documentation, ScalarFunctionArgs}; | ||||||
| use datafusion_expr::{ScalarUDFImpl, Signature, Volatility}; | ||||||
| use datafusion_macros::user_doc; | ||||||
|
|
||||||
| #[user_doc( | ||||||
| doc_section(label = "Other Functions"), | ||||||
| description = "Returns true if the expression's field is nullable, false otherwise. This reflects the schema-level nullability, not whether a specific runtime value is NULL.", | ||||||
| syntax_example = "is_nullable(expression)", | ||||||
| sql_example = r#"```sql | ||||||
| > select is_nullable(name), is_nullable(ts) from table_with_metadata limit 1; | ||||||
| +----------------------------+------------------------+ | ||||||
| | is_nullable(table_with_metadata.name) | is_nullable(table_with_metadata.ts) | | ||||||
| +----------------------------+------------------------+ | ||||||
| | true | false | | ||||||
| +----------------------------+------------------------+ | ||||||
| ``` | ||||||
| "#, | ||||||
| argument( | ||||||
| name = "expression", | ||||||
| description = "Expression to evaluate. The expression can be a constant, column, or function, and any combination of operators." | ||||||
| ) | ||||||
| )] | ||||||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||||||
|
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. The
Suggested change
|
||||||
| pub struct IsNullableFunc { | ||||||
| signature: Signature, | ||||||
| } | ||||||
|
|
||||||
| impl Default for IsNullableFunc { | ||||||
| fn default() -> Self { | ||||||
| Self::new() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl IsNullableFunc { | ||||||
| pub fn new() -> Self { | ||||||
| Self { | ||||||
| signature: Signature::any(1, Volatility::Immutable), | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl ScalarUDFImpl for IsNullableFunc { | ||||||
| fn name(&self) -> &str { | ||||||
| "is_nullable" | ||||||
| } | ||||||
|
|
||||||
| fn signature(&self) -> &Signature { | ||||||
| &self.signature | ||||||
| } | ||||||
|
|
||||||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||||||
|
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. datafusion/functions/src/core/is_nullable.rs:70: Severity: medium 🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. |
||||||
| Ok(DataType::Boolean) | ||||||
| } | ||||||
|
|
||||||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||||||
| let [_arg] = take_function_args(self.name(), args.args)?; | ||||||
| let nullable = args.arg_fields[0].is_nullable(); | ||||||
| Ok(ColumnarValue::Scalar(ScalarValue::Boolean(Some(nullable)))) | ||||||
| } | ||||||
|
|
||||||
| fn documentation(&self) -> Option<&Documentation> { | ||||||
| self.doc() | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ pub mod expr_ext; | |
| pub mod getfield; | ||
| pub mod greatest; | ||
| mod greatest_least_utils; | ||
| pub mod is_nullable; | ||
| pub mod least; | ||
| pub mod named_struct; | ||
| pub mod nullif; | ||
|
|
@@ -59,6 +60,7 @@ make_udf_function!(union_extract::UnionExtractFun, union_extract); | |
| make_udf_function!(union_tag::UnionTagFunc, union_tag); | ||
| make_udf_function!(version::VersionFunc, version); | ||
| make_udf_function!(arrow_metadata::ArrowMetadataFunc, arrow_metadata); | ||
| make_udf_function!(is_nullable::IsNullableFunc, is_nullable); | ||
|
|
||
| pub mod expr_fn { | ||
| use datafusion_expr::{Expr, Literal}; | ||
|
|
@@ -119,6 +121,10 @@ pub mod expr_fn { | |
| union_tag, | ||
| "Returns the name of the currently selected field in the union", | ||
| arg1 | ||
| ),( | ||
| is_nullable, | ||
| "Returns whether the input expression is nullable", | ||
|
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. datafusion/functions/src/core/mod.rs:126: The Severity: low 🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. |
||
| arg1 | ||
| )); | ||
|
|
||
| #[doc = "Returns the value of the field with the given name from the struct"] | ||
|
|
@@ -168,5 +174,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> { | |
| union_tag(), | ||
| version(), | ||
| r#struct(), | ||
| is_nullable(), | ||
| ] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # 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. | ||
|
|
||
| ########## | ||
| ## Tests for the is_nullable function | ||
| ########## | ||
|
|
||
| # Literals are not nullable | ||
| query B | ||
| select is_nullable(1); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| select is_nullable('hello'); | ||
| ---- | ||
| false | ||
|
|
||
| # NULL literal is nullable | ||
| query B | ||
| select is_nullable(NULL); | ||
| ---- | ||
| true | ||
|
|
||
| # Nullable columns from table_with_metadata | ||
| query B | ||
| select is_nullable(id) from table_with_metadata limit 1; | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| select is_nullable(name) from table_with_metadata limit 1; | ||
| ---- | ||
| true | ||
|
|
||
| # Non-nullable columns | ||
| query B | ||
| select is_nullable(ts) from table_with_metadata limit 1; | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| select is_nullable(nonnull_name) from table_with_metadata limit 1; | ||
| ---- | ||
| false | ||
|
|
||
| # Expressions propagate nullability | ||
| query B | ||
| select is_nullable(id + 1) from table_with_metadata limit 1; | ||
| ---- | ||
| true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5286,6 +5286,7 @@ union_tag(union_expression) | |
| - [arrow_try_cast](#arrow_try_cast) | ||
| - [arrow_typeof](#arrow_typeof) | ||
| - [get_field](#get_field) | ||
| - [is_nullable](#is_nullable) | ||
|
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. This file is automatically generated by the |
||
| - [version](#version) | ||
|
|
||
| ### `arrow_cast` | ||
|
|
@@ -5457,6 +5458,29 @@ get_field(expression, field_name[, field_name2, ...]) | |
| +--------+ | ||
| ``` | ||
|
|
||
| ### `is_nullable` | ||
|
|
||
| Returns true if the expression's field is nullable, false otherwise. This reflects the schema-level nullability, not whether a specific runtime value is NULL. | ||
|
|
||
| ```sql | ||
| is_nullable(expression) | ||
| ``` | ||
|
|
||
| #### Arguments | ||
|
|
||
| - **expression**: Expression to evaluate. The expression can be a constant, column, or function, and any combination of operators. | ||
|
|
||
| #### Example | ||
|
|
||
| ```sql | ||
| > select is_nullable(name), is_nullable(ts) from table_with_metadata limit 1; | ||
| +----------------------------+------------------------+ | ||
| | is_nullable(table_with_metadata.name) | is_nullable(table_with_metadata.ts) | | ||
| +----------------------------+------------------------+ | ||
| | true | false | | ||
| +----------------------------+------------------------+ | ||
| ``` | ||
|
|
||
| ### `version` | ||
|
|
||
| Returns the version of DataFusion. | ||
|
|
||
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.
The ASCII table in the SQL example is misaligned. The column headers are significantly longer than the separator lines and the data rows, which makes the documentation harder to read.