Skip to content

Commit

Permalink
fix NOT operator in CEL filtering
Browse files Browse the repository at this point in the history
- NOT is correctly added to unary operators
- remove negate, it is not required/useful in non numeric filters/expressions
- added unit tests for NOT operator
- fixes tektoncd#498

Signed-off-by: Avinal Kumar <avinal@redhat.com>
  • Loading branch information
avinal authored and tekton-robot committed Jul 29, 2023
1 parent 95d3eb8 commit 222fc98
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
15 changes: 15 additions & 0 deletions pkg/api/server/cel2sql/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ func TestConvertRecordExpressions(t *testing.T) {
in: `data.metadata.name.contains("foo")`,
want: "POSITION('foo' IN (data->'metadata'->>'name')) <> 0",
},
{
name: "not contains string function",
in: `!(data.metadata.annotations.contains("foo"))`,
want: "NOT POSITION('foo' IN (data->'metadata'->>'annotations')) <> 0",
},
{
name: "complex not expressions",
in: `!(data.metadata.annotations.contains("foo")) && data.metadata.name.endsWith("bar")`,
want: "NOT POSITION('foo' IN (data->'metadata'->>'annotations')) <> 0 AND (data->'metadata'->>'name') LIKE '%' || 'bar'",
},
{
name: "endsWith string function",
in: `data.metadata.name.endsWith("bar")`,
Expand Down Expand Up @@ -226,6 +236,11 @@ func TestConvertResultExpressions(t *testing.T) {
in: `summary.annotations["branch"] == "main"`,
want: `recordsummary_annotations @> '{"branch":"main"}'::jsonb`,
},
{
name: "not Result.Summary.Annotations",
in: `!(summary.annotations["branch"] == "main")`,
want: `NOT recordsummary_annotations @> '{"branch":"main"}'::jsonb`,
},
{
name: "Result.Summary.Annotations",
in: `"main" == summary.annotations["branch"]`,
Expand Down
1 change: 0 additions & 1 deletion pkg/api/server/cel2sql/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ func (i *interpreter) interpretUnaryCallExpr(expr *exprpb.Expr_Call) error {
if err := i.interpretExpr(expr.Args[0]); err != nil {
return err
}
i.query.WriteString(space)
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/api/server/cel2sql/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import (

var (
unaryOperators = map[string]string{
operators.Negate: "NOT",
operators.LogicalNot: "NOT",
}

binaryOperators = map[string]string{
operators.LogicalAnd: "AND",
operators.LogicalOr: "OR",
operators.LogicalNot: "NOT",
operators.Equals: "=",
operators.NotEquals: "<>",
operators.Less: "<",
Expand Down

0 comments on commit 222fc98

Please sign in to comment.