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 operator folding that only involve literals #833

Merged
merged 1 commit into from
Sep 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions cel/folding.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ func maybeShortcircuitLogic(ctx *OptimizerContext, function string, args []ast.E
return true
}
}
if len(newArgs) == 0 {
newArgs = append(newArgs, args[0])
expr.SetKindCase(newArgs[0])
return true
}
if len(newArgs) == 1 {
expr.SetKindCase(newArgs[0])
return true
Expand Down
24 changes: 24 additions & 0 deletions cel/folding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ func TestConstantFoldingOptimizer(t *testing.T) {
expr: `false || x || false || x`,
folded: `x || x`,
},
{
expr: `true && true`,
folded: `true`,
},
{
expr: `true && false`,
folded: `false`,
},
{
expr: `true || false`,
folded: `true`,
},
{
expr: `false || false`,
folded: `false`,
},
{
expr: `true && false || true`,
folded: `true`,
},
{
expr: `false && true || false`,
folded: `false`,
},
{
expr: `null`,
folded: `null`,
Expand Down