-
-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize boolean operations between all, any, one, none functions (#555)
- Loading branch information
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package optimizer | ||
|
||
import ( | ||
. "github.com/expr-lang/expr/ast" | ||
"github.com/expr-lang/expr/parser/operator" | ||
) | ||
|
||
type predicateCombination struct{} | ||
|
||
func (v *predicateCombination) Visit(node *Node) { | ||
if op, ok := (*node).(*BinaryNode); ok && operator.IsBoolean(op.Operator) { | ||
if left, ok := op.Left.(*BuiltinNode); ok { | ||
if combinedOp, ok := combinedOperator(left.Name, op.Operator); ok { | ||
if right, ok := op.Right.(*BuiltinNode); ok && right.Name == left.Name { | ||
if left.Arguments[0].Type() == right.Arguments[0].Type() && left.Arguments[0].String() == right.Arguments[0].String() { | ||
closure := &ClosureNode{ | ||
Node: &BinaryNode{ | ||
Operator: combinedOp, | ||
Left: left.Arguments[1].(*ClosureNode).Node, | ||
Right: right.Arguments[1].(*ClosureNode).Node, | ||
}, | ||
} | ||
v.Visit(&closure.Node) | ||
Patch(node, &BuiltinNode{ | ||
Name: left.Name, | ||
Arguments: []Node{ | ||
left.Arguments[0], | ||
closure, | ||
}, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func combinedOperator(fn, op string) (string, bool) { | ||
switch fn { | ||
case "all", "any": | ||
return op, true | ||
case "one", "none": | ||
switch op { | ||
case "and": | ||
return "or", true | ||
case "&&": | ||
return "||", true | ||
} | ||
} | ||
return "", false | ||
} |