Skip to content

Commit

Permalink
refactor(optimizer): simplify short circuit optimization (#16861)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzl25 authored May 21, 2024
1 parent 7e7842e commit 4197c23
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
expected_outputs:
- logical_plan
- batch_plan
- id : short_circuit_optimize_time
sql: |
select true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true;
expected_outputs:
- logical_plan
- batch_plan

# should *not* be identified as const
# otherwise the *semantic* will be inconsistent
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/planner_test/tests/testdata/output/expr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
create table t (v1 int);
SELECT 1 in (3, 0.5*2, min(v1)) from t;
batch_plan: |-
BatchProject { exprs: [true:Boolean] }
BatchProject { exprs: [(true:Boolean OR (1:Int32 = min(min(t.v1)))) as $expr1] }
└─BatchSimpleAgg { aggs: [min(min(t.v1))] }
└─BatchExchange { order: [], dist: Single }
└─BatchSimpleAgg { aggs: [min(t.v1)] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@
BatchExchange { order: [], dist: Single }
└─BatchProject { exprs: [false:Boolean] }
└─BatchScan { table: t1, columns: [t1.c1], distribution: SomeShard }
- id: short_circuit_optimize_time
sql: |
select true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true and true;
logical_plan: |-
LogicalProject { exprs: [(((((((((((((((((((((((((true:Boolean AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) AND true:Boolean) as $expr1] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
batch_plan: 'BatchValues { rows: [[true:Boolean]] }'
9 changes: 5 additions & 4 deletions src/frontend/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,11 @@ impl ExprImpl {
fn is_short_circuit(&self, func_call: &FunctionCall) -> bool {
/// evaluate the first parameter of `Or` or `And` function call
fn eval_first(e: &ExprImpl, expect: bool) -> bool {
let Some(Ok(Some(scalar))) = e.try_fold_const() else {
return false;
};
scalar == ScalarImpl::Bool(expect)
if let ExprImpl::Literal(l) = e {
*l.get_data() == Some(ScalarImpl::Bool(expect))
} else {
false
}
}

match func_call.func_type {
Expand Down

0 comments on commit 4197c23

Please sign in to comment.