diff --git a/e2e_test/v2/batch/types/cast.slt b/e2e_test/v2/batch/types/cast.slt index 686e98d44eadd..9c596a64ac16e 100644 --- a/e2e_test/v2/batch/types/cast.slt +++ b/e2e_test/v2/batch/types/cast.slt @@ -37,3 +37,23 @@ query T values('1999-01-08 04:05:06'::timestamp::timestamp); ---- 1999-01-08 04:05:06 + +query T +values(10::integer::boolean); +---- +t + +query T +values(0::integer::boolean); +---- +f + +query T +values(false::boolean::integer); +---- +0 + +query T +values(true::boolean::integer); +---- +1 diff --git a/src/expr/src/expr/expr_unary.rs b/src/expr/src/expr/expr_unary.rs index c4002d3bdec1b..8abd2edc90088 100644 --- a/src/expr/src/expr/expr_unary.rs +++ b/src/expr/src/expr/expr_unary.rs @@ -102,6 +102,8 @@ macro_rules! gen_cast { { varchar, boolean, str_to_bool }, { boolean, varchar, bool_to_str }, + { boolean, int32, general_cast }, + { int32, boolean, int32_to_bool }, { int16, int32, general_cast }, { int16, int64, general_cast }, diff --git a/src/expr/src/vector_op/cast.rs b/src/expr/src/vector_op/cast.rs index f9fa0a6ec6e19..5a4f7d80bb0dc 100644 --- a/src/expr/src/vector_op/cast.rs +++ b/src/expr/src/vector_op/cast.rs @@ -213,6 +213,10 @@ pub fn bool_to_str(input: bool) -> Result { } } +pub fn int32_to_bool(input: i32) -> Result { + Ok(input != 0) +} + #[cfg(test)] mod tests { #[test] @@ -238,4 +242,12 @@ mod tests { "Parse error: Can't cast string to time (expected format is HH:MM:SS[.MS])".to_string() ); } + + #[test] + fn integer_cast_to_bool() { + use super::*; + assert!(int32_to_bool(32).unwrap()); + assert!(int32_to_bool(-32).unwrap()); + assert!(!int32_to_bool(0).unwrap()); + } }