Skip to content

Commit 45599ce

Browse files
authored
use safe cast in propagate_constraints (#11297)
* use safe cast in propagate_constraints * add test
1 parent 9f8ba6a commit 45599ce

File tree

2 files changed

+28
-3
lines changed
  • datafusion

2 files changed

+28
-3
lines changed

datafusion/physical-expr-common/src/expressions/cast.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ const DEFAULT_CAST_OPTIONS: CastOptions<'static> = CastOptions {
3636
format_options: DEFAULT_FORMAT_OPTIONS,
3737
};
3838

39+
const DEFAULT_SAFE_CAST_OPTIONS: CastOptions<'static> = CastOptions {
40+
safe: true,
41+
format_options: DEFAULT_FORMAT_OPTIONS,
42+
};
43+
3944
/// CAST expression casts an expression to a specific data type and returns a runtime error on invalid cast
4045
#[derive(Debug, Clone)]
4146
pub struct CastExpr {
@@ -150,9 +155,9 @@ impl PhysicalExpr for CastExpr {
150155
let child_interval = children[0];
151156
// Get child's datatype:
152157
let cast_type = child_interval.data_type();
153-
Ok(Some(
154-
vec![interval.cast_to(&cast_type, &self.cast_options)?],
155-
))
158+
Ok(Some(vec![
159+
interval.cast_to(&cast_type, &DEFAULT_SAFE_CAST_OPTIONS)?
160+
]))
156161
}
157162

158163
fn dyn_hash(&self, state: &mut dyn Hasher) {

datafusion/sqllogictest/test_files/cast.slt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,23 @@ query ?
6969
SELECT CAST(MAKE_ARRAY() AS VARCHAR[])
7070
----
7171
[]
72+
73+
statement ok
74+
create table t0(v0 BIGINT);
75+
76+
statement ok
77+
insert into t0 values (1),(2),(3);
78+
79+
query I
80+
select * from t0 where v0>1e100;
81+
----
82+
83+
query I
84+
select * from t0 where v0<1e100;
85+
----
86+
1
87+
2
88+
3
89+
90+
statement ok
91+
drop table t0;

0 commit comments

Comments
 (0)