Skip to content

Commit dc437f5

Browse files
alambappletreeisyellow
authored andcommitted
Support field access for literal Null (apache#10655)
1 parent 3ebc31d commit dc437f5

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

datafusion/core/tests/expr_api/mod.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use arrow_array::builder::{ListBuilder, StringBuilder};
2020
use arrow_array::{ArrayRef, RecordBatch, StringArray, StructArray};
2121
use arrow_schema::{DataType, Field};
2222
use datafusion::prelude::*;
23-
use datafusion_common::DFSchema;
23+
use datafusion_common::{DFSchema, ScalarValue};
2424
use datafusion_functions::core::expr_ext::FieldAccessor;
2525
use datafusion_functions_array::expr_ext::{IndexAccessor, SliceAccessor};
2626
/// Tests of using and evaluating `Expr`s outside the context of a LogicalPlan
@@ -76,6 +76,21 @@ fn test_get_field() {
7676
);
7777
}
7878

79+
#[test]
80+
fn test_get_field_null() {
81+
#[rustfmt::skip]
82+
evaluate_expr_test(
83+
lit(ScalarValue::Null).field("a"),
84+
vec![
85+
"+------+",
86+
"| expr |",
87+
"+------+",
88+
"| |",
89+
"+------+",
90+
],
91+
);
92+
}
93+
7994
#[test]
8095
fn test_nested_get_field() {
8196
evaluate_expr_test(
@@ -96,11 +111,17 @@ fn test_nested_get_field() {
96111
}
97112

98113
#[test]
99-
fn test_list() {
114+
fn test_list_index() {
115+
#[rustfmt::skip]
100116
evaluate_expr_test(
101117
col("list").index(lit(1i64)),
102118
vec![
103-
"+------+", "| expr |", "+------+", "| one |", "| two |", "| five |",
119+
"+------+",
120+
"| expr |",
121+
"+------+",
122+
"| one |",
123+
"| two |",
124+
"| five |",
104125
"+------+",
105126
],
106127
);

datafusion/functions/src/core/getfield.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ impl ScalarUDFImpl for GetFieldFunc {
106106
};
107107
let access_schema = GetFieldAccessSchema::NamedStructField { name: name.clone() };
108108
let arg_dt = args[0].get_type(schema)?;
109+
if arg_dt.is_null() {
110+
return Ok(DataType::Null);
111+
}
109112
access_schema
110113
.get_accessed_field(&arg_dt)
111114
.map(|f| f.data_type().clone())
@@ -119,6 +122,10 @@ impl ScalarUDFImpl for GetFieldFunc {
119122
);
120123
}
121124

125+
if args[0].data_type().is_null() {
126+
return Ok(ColumnarValue::Scalar(ScalarValue::Null));
127+
}
128+
122129
let arrays = ColumnarValue::values_to_arrays(args)?;
123130
let array = arrays[0].clone();
124131

0 commit comments

Comments
 (0)