Skip to content
Merged
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: 0 additions & 1 deletion optimizer/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ func Optimize(node *Node, config *conf.Config) error {
Walk(node, &filterLen{})
Walk(node, &filterLast{})
Walk(node, &filterFirst{})
Walk(node, &predicateCombination{})
return nil
}
122 changes: 0 additions & 122 deletions optimizer/optimizer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package optimizer_test

import (
"fmt"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -340,124 +339,3 @@ func TestOptimize_filter_map_first(t *testing.T) {

assert.Equal(t, ast.Dump(expected), ast.Dump(tree.Node))
}

func TestOptimize_predicate_combination(t *testing.T) {
tests := []struct {
op string
fn string
wantOp string
}{
{"and", "all", "and"},
{"&&", "all", "&&"},
{"or", "all", "or"},
{"||", "all", "||"},
{"and", "any", "and"},
{"&&", "any", "&&"},
{"or", "any", "or"},
{"||", "any", "||"},
{"and", "none", "or"},
{"&&", "none", "||"},
{"and", "one", "or"},
{"&&", "one", "||"},
}

for _, tt := range tests {
rule := fmt.Sprintf(`%s(users, .Age > 18 and .Name != "Bob") %s %s(users, .Age < 30)`, tt.fn, tt.op, tt.fn)
t.Run(rule, func(t *testing.T) {
tree, err := parser.Parse(rule)
require.NoError(t, err)

err = optimizer.Optimize(&tree.Node, nil)
require.NoError(t, err)

expected := &ast.BuiltinNode{
Name: tt.fn,
Arguments: []ast.Node{
&ast.IdentifierNode{Value: "users"},
&ast.ClosureNode{
Node: &ast.BinaryNode{
Operator: tt.wantOp,
Left: &ast.BinaryNode{
Operator: "and",
Left: &ast.BinaryNode{
Operator: ">",
Left: &ast.MemberNode{
Node: &ast.PointerNode{},
Property: &ast.StringNode{Value: "Age"},
},
Right: &ast.IntegerNode{Value: 18},
},
Right: &ast.BinaryNode{
Operator: "!=",
Left: &ast.MemberNode{
Node: &ast.PointerNode{},
Property: &ast.StringNode{Value: "Name"},
},
Right: &ast.StringNode{Value: "Bob"},
},
},
Right: &ast.BinaryNode{
Operator: "<",
Left: &ast.MemberNode{
Node: &ast.PointerNode{},
Property: &ast.StringNode{Value: "Age"},
},
Right: &ast.IntegerNode{Value: 30},
},
},
},
},
}
assert.Equal(t, ast.Dump(expected), ast.Dump(tree.Node))
})
}
}

func TestOptimize_predicate_combination_nested(t *testing.T) {
tree, err := parser.Parse(`any(users, {all(.Friends, {.Age == 18 })}) && any(users, {all(.Friends, {.Name != "Bob" })})`)
require.NoError(t, err)

err = optimizer.Optimize(&tree.Node, nil)
require.NoError(t, err)

expected := &ast.BuiltinNode{
Name: "any",
Arguments: []ast.Node{
&ast.IdentifierNode{Value: "users"},
&ast.ClosureNode{
Node: &ast.BuiltinNode{
Name: "all",
Arguments: []ast.Node{
&ast.MemberNode{
Node: &ast.PointerNode{},
Property: &ast.StringNode{Value: "Friends"},
},
&ast.ClosureNode{
Node: &ast.BinaryNode{
Operator: "&&",
Left: &ast.BinaryNode{
Operator: "==",
Left: &ast.MemberNode{
Node: &ast.PointerNode{},
Property: &ast.StringNode{Value: "Age"},
},
Right: &ast.IntegerNode{Value: 18},
},
Right: &ast.BinaryNode{
Operator: "!=",
Left: &ast.MemberNode{
Node: &ast.PointerNode{},
Property: &ast.StringNode{Value: "Name"},
},
Right: &ast.StringNode{Value: "Bob"},
},
},
},
},
},
},
},
}

assert.Equal(t, ast.Dump(expected), ast.Dump(tree.Node))
}
51 changes: 0 additions & 51 deletions optimizer/predicate_combination.go

This file was deleted.