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 regression of cost behavior for has() #651

Merged
merged 3 commits into from
Mar 6, 2023
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
5 changes: 5 additions & 0 deletions checker/cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ func (c *coster) costSelect(e *exprpb.Expr) CostEstimate {
sel := e.GetSelectExpr()
var sum CostEstimate
if sel.GetTestOnly() {
// recurse, but do not add any cost
// this is equivalent to how evalTestOnly increments the runtime cost counter
// but does not add any additional cost for the qualifier, except here we do
// the reverse (ident adds cost)
sum = sum.Add(c.cost(sel.GetOperand()))
return sum
}
sum = sum.Add(c.cost(sel.GetOperand()))
Expand Down
2 changes: 1 addition & 1 deletion checker/cost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestCost(t *testing.T) {
name: "select: field test only",
expr: `has(input.single_int32)`,
decls: []*exprpb.Decl{decls.NewVar("input", decls.NewObjectType("google.expr.proto3.test.TestAllTypes"))},
wanted: zeroCost,
wanted: CostEstimate{Min: 1, Max: 1},
},
{
name: "estimated function call",
Expand Down
2 changes: 2 additions & 0 deletions interpreter/runtimecost.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func CostObserver(tracker *CostTracker) EvalObserver {
tracker.stack.drop(t.rhs.ID(), t.lhs.ID())
case *evalFold:
tracker.stack.drop(t.iterRange.ID())
case *evalTestOnly:
tracker.cost += common.SelectAndIdentCost
case Qualifier:
tracker.cost++
case InterpretableCall:
Expand Down
15 changes: 14 additions & 1 deletion interpreter/runtimecost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestRuntimeCost(t *testing.T) {
name: "select: field test only",
DangerOnTheRanger marked this conversation as resolved.
Show resolved Hide resolved
expr: `has(input.single_int32)`,
decls: []*exprpb.Decl{decls.NewVar("input", decls.NewObjectType("google.expr.proto3.test.TestAllTypes"))},
want: 0,
want: 1,
in: map[string]any{
"input": &proto3pb.TestAllTypes{
RepeatedBool: []bool{false},
Expand All @@ -317,6 +317,19 @@ func TestRuntimeCost(t *testing.T) {
},
},
},
{
name: "select: non-proto field test",
expr: `has(input.testAttr.nestedAttr)`,
decls: []*exprpb.Decl{decls.NewVar("input", nestedMap)},
want: 2,
in: map[string]any{
"input": map[string]any{
"testAttr": map[string]any{
"nestedAttr": "0",
},
},
},
},
{
name: "estimated function call",
expr: `input.getFullYear()`,
Expand Down