Skip to content

Commit

Permalink
Fix #8507: Non-null sub-field on nullable struct-field has wrong null…
Browse files Browse the repository at this point in the history
…ity (#8623)

* added test

* added guard clause

* rename schema fields

* clippy

---------

Co-authored-by: mlanhenke <Marvin.Lanhenke@Berief-Food.de>
  • Loading branch information
marvinlanhenke and mlanhenke authored Dec 23, 2023
1 parent 03c2ef4 commit df2e1e2
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ impl ExprSchemable for Expr {
"Wildcard expressions are not valid in a logical query plan"
),
Expr::GetIndexedField(GetIndexedField { expr, field }) => {
// If schema is nested, check if parent is nullable
// if it is, return early
if let Expr::Column(col) = expr.as_ref() {
if input_schema.nullable(col)? {
return Ok(true);
}
}
field_for_index(expr, field, input_schema).map(|x| x.is_nullable())
}
Expr::GroupingSet(_) => {
Expand Down Expand Up @@ -411,8 +418,8 @@ pub fn cast_subquery(subquery: Subquery, cast_to_type: &DataType) -> Result<Subq
mod tests {
use super::*;
use crate::{col, lit};
use arrow::datatypes::DataType;
use datafusion_common::{Column, ScalarValue};
use arrow::datatypes::{DataType, Fields};
use datafusion_common::{Column, ScalarValue, TableReference};

macro_rules! test_is_expr_nullable {
($EXPR_TYPE:ident) => {{
Expand Down Expand Up @@ -548,6 +555,27 @@ mod tests {
assert_eq!(&meta, expr.to_field(&schema).unwrap().metadata());
}

#[test]
fn test_nested_schema_nullability() {
let fields = DFField::new(
Some(TableReference::Bare {
table: "table_name".into(),
}),
"parent",
DataType::Struct(Fields::from(vec![Field::new(
"child",
DataType::Int64,
false,
)])),
true,
);

let schema = DFSchema::new_with_metadata(vec![fields], HashMap::new()).unwrap();

let expr = col("parent").field("child");
assert!(expr.nullable(&schema).unwrap());
}

#[derive(Debug)]
struct MockExprSchema {
nullable: bool,
Expand Down

0 comments on commit df2e1e2

Please sign in to comment.