Skip to content

Commit

Permalink
Merge pull request #1046 from Cali0707/fix-like-expression-panic-on-p…
Browse files Browse the repository at this point in the history
…arse

fix: LIKE expression with invalid string literals returns a parse error instead of panicking
  • Loading branch information
duglin committed Jun 12, 2024
2 parents aabcd2b + 3e20125 commit e8fccd2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion sql/v2/parser/expression_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package parser

import (
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -175,9 +176,13 @@ func (v *expressionVisitor) VisitLikeExpression(ctx *gen.LikeExpressionContext)
if patternContext.DQUOTED_STRING_LITERAL() != nil {
// Parse double quoted string
pattern = dQuotedStringToString(patternContext.DQUOTED_STRING_LITERAL().GetText())
} else {
} else if patternContext.SQUOTED_STRING_LITERAL() != nil {
// Parse single quoted string
pattern = sQuotedStringToString(patternContext.SQUOTED_STRING_LITERAL().GetText())
} else {
// not a string, return an error
v.parsingErrors = append(v.parsingErrors, fmt.Errorf("failed to parse LIKE expression: the pattern was not a string literal"))
return noopExpression{}
}

likeExpression, err := expression.NewLikeExpression(v.Visit(ctx.Expression()).(cesql.Expression), pattern)
Expand Down
9 changes: 8 additions & 1 deletion sql/v2/test/tck/like_expression.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,11 @@ tests:
result: false
- name: With type coercion from bool (4)
expression: "FALSE LIKE 'fal%'"
result: true
result: true

- name: Invalid string literal in comparison causes parse error
expression: "x LIKE 123"
result: false
error: parse
eventOverrides:
x: "123"

0 comments on commit e8fccd2

Please sign in to comment.