Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 57 additions & 11 deletions src/types/evaluator/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
// limitations under the License.

use crate::errors::DatabaseError;
use crate::expression::UnaryOperator;
use crate::types::evaluator::cast::{to_char, to_varbinary, to_varchar};
use crate::types::evaluator::DataValue;
use crate::types::CharLengthUnits;
use ordered_float::OrderedFloat;
use std::hint;
pub fn boolean_not_unary_eval(value: &DataValue) -> DataValue {
match value {
pub fn boolean_not_unary_eval(value: &DataValue) -> Result<DataValue, DatabaseError> {
Ok(match value {
DataValue::Boolean(value) => DataValue::Boolean(!value),
DataValue::Null => DataValue::Null,
_ => unsafe { hint::unreachable_unchecked() },
}
_ => {
return Err(DatabaseError::UnsupportedUnaryOperator(
value.logical_type(),
UnaryOperator::Not,
))
}
})
}
pub fn boolean_and_binary_eval(
left: &DataValue,
Expand All @@ -36,7 +41,12 @@ pub fn boolean_and_binary_eval(
(DataValue::Null, DataValue::Null)
| (DataValue::Boolean(true), DataValue::Null)
| (DataValue::Null, DataValue::Boolean(true)) => DataValue::Null,
_ => unsafe { hint::unreachable_unchecked() },
_ => {
return Err(DatabaseError::Incomparable(
left.logical_type(),
right.logical_type(),
))
}
})
}
pub fn boolean_or_binary_eval(
Expand All @@ -50,7 +60,12 @@ pub fn boolean_or_binary_eval(
(DataValue::Null, DataValue::Null)
| (DataValue::Boolean(false), DataValue::Null)
| (DataValue::Null, DataValue::Boolean(false)) => DataValue::Null,
_ => unsafe { hint::unreachable_unchecked() },
_ => {
return Err(DatabaseError::Incomparable(
left.logical_type(),
right.logical_type(),
))
}
})
}
pub fn boolean_eq_binary_eval(
Expand All @@ -62,7 +77,12 @@ pub fn boolean_eq_binary_eval(
(DataValue::Null, DataValue::Boolean(_))
| (DataValue::Boolean(_), DataValue::Null)
| (DataValue::Null, DataValue::Null) => DataValue::Null,
_ => unsafe { hint::unreachable_unchecked() },
_ => {
return Err(DatabaseError::Incomparable(
left.logical_type(),
right.logical_type(),
))
}
})
}

Expand Down Expand Up @@ -123,7 +143,12 @@ pub fn boolean_not_eq_binary_eval(
(DataValue::Null, DataValue::Boolean(_))
| (DataValue::Boolean(_), DataValue::Null)
| (DataValue::Null, DataValue::Null) => DataValue::Null,
_ => unsafe { hint::unreachable_unchecked() },
_ => {
return Err(DatabaseError::Incomparable(
left.logical_type(),
right.logical_type(),
))
}
})
}

Expand All @@ -132,15 +157,19 @@ pub fn boolean_not_eq_binary_eval(
#[maybe_async]
mod test {
use super::*;
use crate::expression::UnaryOperator;
use crate::types::value::Utf8Type;

#[test]
fn test_boolean_binary_evaluators() {
assert_eq!(
boolean_not_unary_eval(&DataValue::Boolean(true)),
boolean_not_unary_eval(&DataValue::Boolean(true)).unwrap(),
DataValue::Boolean(false)
);
assert_eq!(boolean_not_unary_eval(&DataValue::Null), DataValue::Null);
assert_eq!(
boolean_not_unary_eval(&DataValue::Null).unwrap(),
DataValue::Null
);
assert_eq!(
boolean_and_binary_eval(&DataValue::Boolean(true), &DataValue::Boolean(true)).unwrap(),
DataValue::Boolean(true)
Expand Down Expand Up @@ -184,6 +213,23 @@ mod test {
);
}

#[test]
fn test_boolean_not_on_non_boolean_returns_error() {
let result = boolean_not_unary_eval(&DataValue::Int32(42));
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
DatabaseError::UnsupportedUnaryOperator(_, UnaryOperator::Not)
));

let result = boolean_not_unary_eval(&DataValue::Utf8 {
value: "test".into(),
ty: Utf8Type::Variable(None),
unit: CharLengthUnits::Characters,
});
assert!(result.is_err());
}

#[test]
fn test_boolean_cast_evaluators() {
let value = DataValue::Boolean(true);
Expand Down
2 changes: 1 addition & 1 deletion src/types/evaluator/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(crate) fn eval_unary(
UNARY_INT16_MINUS => int16_minus_unary_eval(value)?,
UNARY_INT32_MINUS => int32_minus_unary_eval(value)?,
UNARY_INT64_MINUS => int64_minus_unary_eval(value)?,
UNARY_BOOLEAN_NOT => boolean_not_unary_eval(value),
UNARY_BOOLEAN_NOT => boolean_not_unary_eval(value)?,
UNARY_FLOAT32_MINUS => float32_minus_unary_eval(value),
UNARY_FLOAT64_MINUS => float64_minus_unary_eval(value),
UNARY_SQL_NULL => crate::types::value::DataValue::Null,
Expand Down
60 changes: 60 additions & 0 deletions tests/slt/boolean_type_safety.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Boolean operators reject non-boolean operands instead of invoking undefined behaviour.

query B
SELECT NOT true
----
false

query B
SELECT NOT false
----
true

query B
SELECT NOT NULL
----
NULL

query error
SELECT NOT 42

query error
SELECT NOT 0

query error
SELECT NOT 'test'

query error
SELECT NOT 'false'

query error
SELECT NOT 1.5

query error
SELECT NOT CURRENT_TIMESTAMP

query error
SELECT NOT (1 + 1)

query error
SELECT NOT ('a' || 'b')

query B
SELECT NOT (1 = 1)
----
false

query B
SELECT NOT (1 <> 1)
----
true

query B
SELECT NOT (NULL IS NULL)
----
false

query B
SELECT NOT (1 IS NULL)
----
true
Loading