fix: s3 sql parse error for colums as with quotes (#18765)

This commit is contained in:
jiuker 2024-01-10 01:19:11 +08:00 committed by GitHub
parent 3a90af0bcd
commit a89e0bab7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View File

@ -128,7 +128,7 @@ type JSONPath struct {
// AliasedExpression is an expression that can be optionally named // AliasedExpression is an expression that can be optionally named
type AliasedExpression struct { type AliasedExpression struct {
Expression *Expression `parser:"@@"` Expression *Expression `parser:"@@"`
As string `parser:"[ \"AS\" @Ident ]"` As string `parser:"[ \"AS\" @Ident | \"AS\" @LitString ]"`
} }
// Grammar for Expression // Grammar for Expression

View File

@ -383,3 +383,13 @@ func TestSqlLexerArithOps(t *testing.T) {
// fmt.Printf("%d: %#v\n", i, t) // fmt.Printf("%d: %#v\n", i, t)
// } // }
} }
func TestParseSelectStatement(t *testing.T) {
exp, err := ParseSelectStatement("select _3,_1,_2 as 'mytest' from S3object")
if err != nil {
t.Fatalf("parse alias sql error: %v", err)
}
if exp.selectAST.Expression.Expressions[2].As != "mytest" {
t.Fatalf("parse alias sql error: %s not equal %s", exp.selectAST.Expression.Expressions[2].As, err)
}
}

View File

@ -112,6 +112,14 @@ func ParseSelectStatement(s string) (stmt SelectStatement, err error) {
// Set table alias // Set table alias
stmt.tableAlias = selectAST.From.As stmt.tableAlias = selectAST.From.As
// Remove quotes from column aliases
if selectAST.Expression != nil {
for _, exp := range selectAST.Expression.Expressions {
if strings.HasSuffix(exp.As, "'") && strings.HasPrefix(exp.As, "'") && len(exp.As) >= 2 {
exp.As = exp.As[1 : len(exp.As)-1]
}
}
}
return return
} }