Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logical eval ops w/ MISSING arg #457

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Fixed
- Evaluation of logical ops (AND, OR, NOT) with `MISSING` argument

## [0.7.1] - 2024-03-15
### Changed
Expand Down
26 changes: 18 additions & 8 deletions partiql-eval/src/eval/expr/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,25 @@ impl BindEvalExpr for EvalOpUnary {
args: Vec<Box<dyn EvalExpr>>,
) -> Result<Box<dyn EvalExpr>, BindError> {
let any_num = PartiqlType::any_of(TYPE_NUMERIC_TYPES);

let unop = |types, f: fn(&Value) -> Value| {
UnaryValueExpr::create_typed::<{ STRICT }, _>(types, args, f)
};
type CheckNull<const STRICT: bool> = DefaultArgChecker<STRICT, PropagateNull<true>>;
type CheckMissing<const STRICT: bool> = DefaultArgChecker<STRICT, PropagateMissing<true>>;

match self {
EvalOpUnary::Pos => unop([any_num], std::clone::Clone::clone),
EvalOpUnary::Neg => unop([any_num], |operand| -operand),
EvalOpUnary::Not => unop([TYPE_BOOL], |operand| !operand),
EvalOpUnary::Pos => UnaryValueExpr::create_checked::<
{ STRICT },
CheckMissing<STRICT>,
fn(&Value) -> Value,
>([any_num], args, std::clone::Clone::clone),
EvalOpUnary::Neg => UnaryValueExpr::create_checked::<
{ STRICT },
CheckMissing<STRICT>,
fn(&Value) -> Value,
>([any_num], args, |operand| -operand),
EvalOpUnary::Not => UnaryValueExpr::create_checked::<
{ STRICT },
CheckNull<STRICT>,
fn(&Value) -> Value,
>([TYPE_BOOL], args, |operand| !operand),
}
}
}
Expand Down Expand Up @@ -138,7 +148,7 @@ impl<const TARGET: bool, OnMissing: ArgShortCircuit> ArgChecker
) -> ArgCheckControlFlow<Value, Cow<'a, Value>> {
match arg.borrow() {
Boolean(b) if b == &TARGET => ArgCheckControlFlow::ShortCircuit(Value::Boolean(*b)),
Missing => ArgCheckControlFlow::ShortCircuit(OnMissing::propagate()),
Missing => ArgCheckControlFlow::Propagate(OnMissing::propagate()),
Null => ArgCheckControlFlow::Propagate(Null),
_ => ArgCheckControlFlow::Continue(arg),
}
Expand Down
198 changes: 185 additions & 13 deletions partiql-eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ mod tests {
use crate::plan::EvaluationMode;
use partiql_logical::{
BagExpr, BetweenExpr, BinaryOp, BindingsOp, CoalesceExpr, ExprQuery, IsTypeExpr, JoinKind,
ListExpr, LogicalPlan, NullIfExpr, PathComponent, TupleExpr, Type, ValueExpr, VarRefType,
ListExpr, LogicalPlan, NullIfExpr, PathComponent, TupleExpr, Type, UnaryOp, ValueExpr,
VarRefType,
};
use partiql_value as value;
use partiql_value::Value::{Missing, Null};
Expand Down Expand Up @@ -641,9 +642,23 @@ mod tests {
}

#[test]
fn and_or_null() {
fn logical_ops() {
#[track_caller]
fn eval_to_null(op: BinaryOp, lhs: Value, rhs: Value) {
fn eval_unary(op: UnaryOp, expr: Value, expected: Value) {
let mut plan = LogicalPlan::new();
let expq = plan.add_operator(BindingsOp::ExprQuery(ExprQuery {
expr: ValueExpr::UnExpr(op, Box::new(ValueExpr::Lit(Box::new(expr)))),
}));

let sink = plan.add_operator(BindingsOp::Sink);
plan.add_flow(expq, sink);

let actual = evaluate(plan, MapBindings::default());
assert_eq!(expected, actual);
}

#[track_caller]
fn eval_binary(op: BinaryOp, lhs: Value, rhs: Value, expected: Value) {
let mut plan = LogicalPlan::new();
let expq = plan.add_operator(BindingsOp::ExprQuery(ExprQuery {
expr: ValueExpr::BinaryExpr(
Expand All @@ -656,18 +671,175 @@ mod tests {
let sink = plan.add_operator(BindingsOp::Sink);
plan.add_flow(expq, sink);

let result = evaluate(plan, MapBindings::default());
assert_eq!(result, Value::Null);
let actual = evaluate(plan, MapBindings::default());
assert_eq!(expected, actual);
}

eval_to_null(BinaryOp::And, Value::Null, Value::Boolean(true));
eval_to_null(BinaryOp::And, Value::Missing, Value::Boolean(true));
eval_to_null(BinaryOp::And, Value::Boolean(true), Value::Null);
eval_to_null(BinaryOp::And, Value::Boolean(true), Value::Missing);
eval_to_null(BinaryOp::Or, Value::Null, Value::Boolean(false));
eval_to_null(BinaryOp::Or, Value::Missing, Value::Boolean(false));
eval_to_null(BinaryOp::Or, Value::Boolean(false), Value::Null);
eval_to_null(BinaryOp::Or, Value::Boolean(false), Value::Missing);
// NOT bools only
eval_unary(UnaryOp::Not, Value::Boolean(true), Value::Boolean(false));
eval_unary(UnaryOp::Not, Value::Boolean(false), Value::Boolean(true));
// NOT null propagation
eval_unary(UnaryOp::Not, Value::Null, Value::Null);
eval_unary(UnaryOp::Not, Value::Missing, Value::Null);

// AND/OR bools only
eval_binary(
BinaryOp::And,
Value::Boolean(true),
Value::Boolean(true),
Value::Boolean(true),
);
eval_binary(
BinaryOp::And,
Value::Boolean(false),
Value::Boolean(true),
Value::Boolean(false),
);
eval_binary(
BinaryOp::And,
Value::Boolean(true),
Value::Boolean(false),
Value::Boolean(false),
);
eval_binary(
BinaryOp::And,
Value::Boolean(false),
Value::Boolean(false),
Value::Boolean(false),
);
eval_binary(
BinaryOp::Or,
Value::Boolean(true),
Value::Boolean(true),
Value::Boolean(true),
);
eval_binary(
BinaryOp::Or,
Value::Boolean(true),
Value::Boolean(false),
Value::Boolean(true),
);
eval_binary(
BinaryOp::Or,
Value::Boolean(false),
Value::Boolean(true),
Value::Boolean(true),
);
eval_binary(
BinaryOp::Or,
Value::Boolean(false),
Value::Boolean(false),
Value::Boolean(false),
);

// AND/OR short circuit
eval_binary(
BinaryOp::And,
Value::Boolean(false),
Value::Null,
Value::Boolean(false),
);
eval_binary(
BinaryOp::And,
Value::Boolean(false),
Value::Missing,
Value::Boolean(false),
);
eval_binary(
BinaryOp::And,
Value::Null,
Value::Boolean(false),
Value::Boolean(false),
);
eval_binary(
BinaryOp::And,
Value::Missing,
Value::Boolean(false),
Value::Boolean(false),
);
eval_binary(
BinaryOp::Or,
Value::Boolean(true),
Value::Null,
Value::Boolean(true),
);
eval_binary(
BinaryOp::Or,
Value::Boolean(true),
Value::Missing,
Value::Boolean(true),
);
eval_binary(
BinaryOp::Or,
Value::Null,
Value::Boolean(true),
Value::Boolean(true),
);
eval_binary(
BinaryOp::Or,
Value::Missing,
Value::Boolean(true),
Value::Boolean(true),
);

// AND/OR propagate null
eval_binary(
BinaryOp::And,
Value::Boolean(true),
Value::Null,
Value::Null,
);
eval_binary(
BinaryOp::And,
Value::Boolean(true),
Value::Missing,
Value::Null,
);
eval_binary(
BinaryOp::And,
Value::Null,
Value::Boolean(true),
Value::Null,
);
eval_binary(
BinaryOp::And,
Value::Missing,
Value::Boolean(true),
Value::Null,
);
eval_binary(
BinaryOp::Or,
Value::Boolean(false),
Value::Null,
Value::Null,
);
eval_binary(
BinaryOp::Or,
Value::Boolean(false),
Value::Missing,
Value::Null,
);
eval_binary(
BinaryOp::Or,
Value::Null,
Value::Boolean(false),
Value::Null,
);
eval_binary(
BinaryOp::Or,
Value::Missing,
Value::Boolean(false),
Value::Null,
);

eval_binary(BinaryOp::And, Value::Null, Value::Null, Value::Null);
eval_binary(BinaryOp::And, Value::Null, Value::Missing, Value::Null);
eval_binary(BinaryOp::And, Value::Missing, Value::Null, Value::Null);
eval_binary(BinaryOp::And, Value::Missing, Value::Missing, Value::Null);
eval_binary(BinaryOp::Or, Value::Null, Value::Null, Value::Null);
eval_binary(BinaryOp::Or, Value::Null, Value::Missing, Value::Null);
eval_binary(BinaryOp::Or, Value::Missing, Value::Null, Value::Null);
eval_binary(BinaryOp::Or, Value::Missing, Value::Missing, Value::Null);
}

#[test]
Expand Down
Loading