Skip to content

Commit

Permalink
feat: support single element array in "IN" (#12)
Browse files Browse the repository at this point in the history
* Fix single element "in" clauses

* Guard against invalid slice access

* Always return a slice

---------

Co-authored-by: Daniel Richter <[email protected]>
  • Loading branch information
Arthur-Sk and Richtermeister authored Dec 28, 2024
1 parent 00136f1 commit 31caa3f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions evaluationStage.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,16 @@ func makeAccessorStage(pair []string) evaluationOperator {
}
}

func ensureSliceStage(op evaluationOperator) evaluationOperator {
return func(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
orig, err := op(left, right, parameters)
if err != nil {
return orig, err
}
return []interface{}{orig}, nil
}
}

func separatorStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {

var ret []interface{}
Expand Down
6 changes: 6 additions & 0 deletions evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ func TestNoParameterEvaluation(test *testing.T) {
Input: "!(1 in (1, 2, 3))",
Expected: false,
},
EvaluationTest{

Name: "Single Element Array membership literal",
Input: "1 in (1)",
Expected: true,
},
EvaluationTest{

Name: "Logical operator reordering (#30)",
Expand Down
10 changes: 10 additions & 0 deletions stagePlanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,22 @@ func planValue(stream *tokenStream) (*evaluationStage, error) {
switch token.Kind {

case CLAUSE:
var prev ExpressionToken
if stream.index > 1 {
prev = stream.tokens[stream.index-2]
}

ret, err = planTokens(stream)
if err != nil {
return nil, err
}

// clauses with single elements don't trigger SEPARATE stage planner
// this ensures that when used as part of an "in" comparison, the array requirement passes
if prev.Kind == COMPARATOR && prev.Value == "in" && ret.symbol == LITERAL {
ret.operator = ensureSliceStage(ret.operator)
}

// advance past the CLAUSE_CLOSE token. We know that it's a CLAUSE_CLOSE, because at parse-time we check for unbalanced parens.
stream.next()

Expand Down

0 comments on commit 31caa3f

Please sign in to comment.