Skip to content

Commit 7891a1f

Browse files
adriangbclaude
andcommitted
fix: cast_to_type nullability should only depend on the first argument
The second argument (reference) is used solely for its data type, so its nullability should not propagate to the result. Previously `cast_to_type(42, NULL::INTEGER)` was incorrectly marked nullable in the schema even though the input literal is non-null. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 7013469 commit 7891a1f

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎datafusion/functions/src/core/cast_to_type.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ impl ScalarUDFImpl for CastToTypeFunc {
105105
}
106106

107107
fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
108-
let nullable = args.arg_fields.iter().any(|f| f.is_nullable());
109-
let [_, reference_field] = take_function_args(self.name(), args.arg_fields)?;
108+
let [source_field, reference_field] =
109+
take_function_args(self.name(), args.arg_fields)?;
110110
let target_type = reference_field.data_type().clone();
111+
// Nullability is inherited only from the first argument (the value
112+
// being cast). The second argument is used solely for its type, so
113+
// its own nullability is irrelevant. The one exception is when the
114+
// target type is Null – that type is inherently nullable.
115+
let nullable = source_field.is_nullable() || target_type == DataType::Null;
111116
Ok(Field::new(self.name(), target_type, nullable).into())
112117
}
113118

‎datafusion/sqllogictest/test_files/cast_to_type.slt‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,46 @@ SELECT cast_to_type('not_a_number', NULL::INTEGER);
140140
statement ok
141141
DROP TABLE t1;
142142

143+
#######
144+
## Nullability tests for cast_to_type
145+
#######
146+
147+
statement ok
148+
set datafusion.catalog.information_schema = true;
149+
150+
# Non-nullable input -> non-nullable output
151+
statement ok
152+
CREATE VIEW v_cast_nonnull AS SELECT cast_to_type(42, NULL::INTEGER) as a;
153+
154+
query TTT
155+
SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = 'v_cast_nonnull';
156+
----
157+
a Int32 NO
158+
159+
statement ok
160+
DROP VIEW v_cast_nonnull;
161+
162+
# Nullable input -> nullable output
163+
statement ok
164+
CREATE TABLE t_nullable (x INTEGER);
165+
166+
statement ok
167+
INSERT INTO t_nullable VALUES (1), (NULL);
168+
169+
statement ok
170+
CREATE VIEW v_cast_null AS SELECT cast_to_type(x, NULL::DOUBLE) as a FROM t_nullable;
171+
172+
query TTT
173+
SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = 'v_cast_null';
174+
----
175+
a Float64 YES
176+
177+
statement ok
178+
DROP VIEW v_cast_null;
179+
180+
statement ok
181+
DROP TABLE t_nullable;
182+
143183
#######
144184
## Tests for try_cast_to_type function (fallible variant returning NULL)
145185
#######
@@ -255,3 +295,22 @@ NULL
255295

256296
statement ok
257297
DROP TABLE t2;
298+
299+
#######
300+
## Nullability tests for try_cast_to_type
301+
#######
302+
303+
# try_cast_to_type is always nullable (cast can fail)
304+
statement ok
305+
CREATE VIEW v_trycast AS SELECT try_cast_to_type(42, NULL::INTEGER) as a;
306+
307+
query TTT
308+
SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = 'v_trycast';
309+
----
310+
a Int32 YES
311+
312+
statement ok
313+
DROP VIEW v_trycast;
314+
315+
statement ok
316+
set datafusion.catalog.information_schema = false;

0 commit comments

Comments
 (0)