From 3e2012501381bab7f9b10c71d54b54aa2580cc46 Mon Sep 17 00:00:00 2001 From: Calum Murray Date: Wed, 1 May 2024 10:50:23 -0400 Subject: [PATCH] fix: LIKE expression with invalid string literals returns a parse error instead of panic Signed-off-by: Calum Murray --- sql/v2/parser/expression_visitor.go | 7 ++++++- sql/v2/test/tck/like_expression.yaml | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sql/v2/parser/expression_visitor.go b/sql/v2/parser/expression_visitor.go index 8ca5fd07e..4fb863ac2 100644 --- a/sql/v2/parser/expression_visitor.go +++ b/sql/v2/parser/expression_visitor.go @@ -6,6 +6,7 @@ package parser import ( + "fmt" "strconv" "strings" @@ -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) diff --git a/sql/v2/test/tck/like_expression.yaml b/sql/v2/test/tck/like_expression.yaml index b6bc5a18b..31e202638 100644 --- a/sql/v2/test/tck/like_expression.yaml +++ b/sql/v2/test/tck/like_expression.yaml @@ -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"