Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cursor/rules.md
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!

5 changes: 5 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ When creating a PR, you MUST follow the [PR template](.github/pull_request_templ

See the [Testing Quick Start](docs/source/contributor-guide/testing.md#testing-quick-start)
for the recommended pre-PR test commands.
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!

83 changes: 83 additions & 0 deletions datafusion/functions/src/core/is_nullable.rs
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 |
+----------------------------+------------------------+
Comment on lines +30 to +34
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
+----------------------------+------------------------+
| is_nullable(table_with_metadata.name) | is_nullable(table_with_metadata.ts) |
+----------------------------+------------------------+
| true | false |
+----------------------------+------------------------+
+---------------------------------------+-------------------------------------+
| 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)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The Hash trait cannot be derived for IsNullableFunc because the signature field of type Signature does not implement Hash. Since ScalarUDFImpl does not require the implementation to be Hash, and ScalarUDF hashes based on the function name, you should remove Hash from the derive list to avoid a compilation error.

Suggested change
#[derive(Debug, PartialEq, Eq, Hash)]
#[derive(Debug, PartialEq, Eq)]

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> {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

datafusion/functions/src/core/is_nullable.rs:70: ScalarUDFImpl’s default return_field_from_args marks outputs as nullable, but is_nullable always returns a non-NULL boolean (ScalarValue::Boolean(Some(...))), so the planned output field metadata may be incorrect.

Severity: medium

Fix This in Augment

🤖 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()
}
}
7 changes: 7 additions & 0 deletions datafusion/functions/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

datafusion/functions/src/core/mod.rs:126: The expr_fn docs say “Returns whether the input expression is nullable”, which could be read as runtime NULL-ness; the function actually reports schema/field nullability as documented elsewhere.

Severity: low

Fix This in Augment

🤖 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"]
Expand Down Expand Up @@ -168,5 +174,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
union_tag(),
version(),
r#struct(),
is_nullable(),
]
}
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/src/test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl TestContext {
info!("Registering table with many types");
register_table_with_many_types(test_ctx.session_ctx()).await;
}
"metadata.slt" => {
"metadata.slt" | "is_nullable.slt" => {
info!("Registering metadata table tables");
register_metadata_tables(test_ctx.session_ctx()).await;
}
Expand Down
65 changes: 65 additions & 0 deletions datafusion/sqllogictest/test_files/is_nullable.slt
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
24 changes: 24 additions & 0 deletions docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This file is automatically generated by the dev/update_function_docs.sh script, as noted in the header of the file. You should avoid editing it manually. Instead, ensure the documentation in the ScalarUDFImpl implementation is correct and run the update script to regenerate this file. This ensures consistency and prevents your changes from being overwritten in the future.

- [version](#version)

### `arrow_cast`
Expand Down Expand Up @@ -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.
Expand Down
Loading