Parcourir la source

fix singlequote

Signed-off-by: Rui-Gan <1171530954@qq.com>
Rui-Gan il y a 1 an
Parent
commit
6adb29fbc0
2 fichiers modifiés avec 33 ajouts et 1 suppressions
  1. 7 1
      internal/xsql/lexical.go
  2. 26 0
      internal/xsql/parser_test.go

+ 7 - 1
internal/xsql/lexical.go

@@ -271,7 +271,13 @@ func (s *Scanner) ScanString(isSingle bool) (tok ast.Token, lit string) {
 			return ast.BADSTRING, buf.String()
 			return ast.BADSTRING, buf.String()
 		} else if ch == '\\' && !escape {
 		} else if ch == '\\' && !escape {
 			escape = true
 			escape = true
-			buf.WriteRune(ch)
+			nextCh := s.read()
+			if nextCh == '\'' && isSingle {
+				buf.WriteRune(nextCh)
+			} else {
+				buf.WriteRune(ch)
+				s.unread()
+			}
 		} else {
 		} else {
 			escape = false
 			escape = false
 			buf.WriteRune(ch)
 			buf.WriteRune(ch)

+ 26 - 0
internal/xsql/parser_test.go

@@ -2987,6 +2987,32 @@ func TestParser_ParseStatement(t *testing.T) {
 			},
 			},
 		},
 		},
 		{
 		{
+			s: `SELECT "a\"b'c" FROM tbl`,
+			stmt: &ast.SelectStatement{
+				Fields: []ast.Field{
+					{
+						Expr:  &ast.StringLiteral{Val: `a"b'c`},
+						Name:  "kuiper_field_0",
+						AName: "",
+					},
+				},
+				Sources: []ast.Source{&ast.Table{Name: "tbl"}},
+			},
+		},
+		{
+			s: `SELECT 'a"b\'c' FROM tbl`,
+			stmt: &ast.SelectStatement{
+				Fields: []ast.Field{
+					{
+						Expr:  &ast.StringLiteral{Val: `a"b'c`},
+						Name:  "kuiper_field_0",
+						AName: "",
+					},
+				},
+				Sources: []ast.Source{&ast.Table{Name: "tbl"}},
+			},
+		},
+		{
 			s:    `SELECT "abc' FROM tbl`,
 			s:    `SELECT "abc' FROM tbl`,
 			stmt: nil,
 			stmt: nil,
 			err:  `found "\"abc' FROM tbl", expected expression.`,
 			err:  `found "\"abc' FROM tbl", expected expression.`,