Skip to content

Commit

Permalink
fix: LIKE expression with invalid string literals returns a parse err…
Browse files Browse the repository at this point in the history
…or instead of panic

Signed-off-by: Calum Murray <[email protected]>
  • Loading branch information
Cali0707 authored and duglin committed Jun 12, 2024
1 parent aabcd2b commit 3e20125
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 3e20125

Please sign in to comment.