From 45599ce310aa6270813091a5c3288abcd7541f59 Mon Sep 17 00:00:00 2001 From: Lordworms <48054792+Lordworms@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:39:37 -0700 Subject: [PATCH] use safe cast in propagate_constraints (#11297) * use safe cast in propagate_constraints * add test --- .../src/expressions/cast.rs | 11 +++++++--- datafusion/sqllogictest/test_files/cast.slt | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/datafusion/physical-expr-common/src/expressions/cast.rs b/datafusion/physical-expr-common/src/expressions/cast.rs index 8aba33932c56..dd6131ad65c3 100644 --- a/datafusion/physical-expr-common/src/expressions/cast.rs +++ b/datafusion/physical-expr-common/src/expressions/cast.rs @@ -36,6 +36,11 @@ const DEFAULT_CAST_OPTIONS: CastOptions<'static> = CastOptions { format_options: DEFAULT_FORMAT_OPTIONS, }; +const DEFAULT_SAFE_CAST_OPTIONS: CastOptions<'static> = CastOptions { + safe: true, + format_options: DEFAULT_FORMAT_OPTIONS, +}; + /// CAST expression casts an expression to a specific data type and returns a runtime error on invalid cast #[derive(Debug, Clone)] pub struct CastExpr { @@ -150,9 +155,9 @@ impl PhysicalExpr for CastExpr { let child_interval = children[0]; // Get child's datatype: let cast_type = child_interval.data_type(); - Ok(Some( - vec![interval.cast_to(&cast_type, &self.cast_options)?], - )) + Ok(Some(vec![ + interval.cast_to(&cast_type, &DEFAULT_SAFE_CAST_OPTIONS)? + ])) } fn dyn_hash(&self, state: &mut dyn Hasher) { diff --git a/datafusion/sqllogictest/test_files/cast.slt b/datafusion/sqllogictest/test_files/cast.slt index 4554c9292b6e..3466354e54d7 100644 --- a/datafusion/sqllogictest/test_files/cast.slt +++ b/datafusion/sqllogictest/test_files/cast.slt @@ -69,3 +69,23 @@ query ? SELECT CAST(MAKE_ARRAY() AS VARCHAR[]) ---- [] + +statement ok +create table t0(v0 BIGINT); + +statement ok +insert into t0 values (1),(2),(3); + +query I +select * from t0 where v0>1e100; +---- + +query I +select * from t0 where v0<1e100; +---- +1 +2 +3 + +statement ok +drop table t0;