Skip to content

Commit

Permalink
use safe cast in propagate_constraints (#11297)
Browse files Browse the repository at this point in the history
* use safe cast in propagate_constraints

* add test
  • Loading branch information
Lordworms authored Jul 7, 2024
1 parent 9f8ba6a commit 45599ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
11 changes: 8 additions & 3 deletions datafusion/physical-expr-common/src/expressions/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions datafusion/sqllogictest/test_files/cast.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 45599ce

Please sign in to comment.