From 3e1a78729f3c075576762b14eb8db36895d2987d Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 31 Jan 2023 12:05:54 +0100 Subject: [PATCH] This is an automated cherry-pick of #40736 Signed-off-by: ti-chi-bot --- parser/parser.go | 41 +++++++++------------------------- parser/parser.y | 51 +++++++++++++++++-------------------------- parser/parser_test.go | 18 +++++++++++++++ 3 files changed, 48 insertions(+), 62 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index e58fd78da70c8..fad3ee60acd61 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -15358,19 +15358,22 @@ yynewstate: { field := yyS[yypt-0].item.(*ast.SelectField) field.Offset = parser.startOffset(&yyS[yypt]) + if field.Expr != nil && field.AsName.O == "" { + endOffset := parser.yylval.offset + field.SetText(parser.lexer.client, strings.TrimSpace(parser.src[field.Offset:endOffset])) + } parser.yyVAL.item = []*ast.SelectField{field} } case 675: { fl := yyS[yypt-2].item.([]*ast.SelectField) - last := fl[len(fl)-1] - if last.Expr != nil && last.AsName.O == "" { - lastEnd := parser.endOffset(&yyS[yypt-1]) - last.SetText(parser.lexer.client, parser.src[last.Offset:lastEnd]) + field := yyS[yypt-0].item.(*ast.SelectField) + field.Offset = parser.startOffset(&yyS[yypt]) + if field.Expr != nil && field.AsName.O == "" { + endOffset := parser.yylval.offset + field.SetText(parser.lexer.client, strings.TrimSpace(parser.src[field.Offset:endOffset])) } - newField := yyS[yypt-0].item.(*ast.SelectField) - newField.Offset = parser.startOffset(&yyS[yypt]) - parser.yyVAL.item = append(fl, newField) + parser.yyVAL.item = append(fl, field) } case 676: { @@ -17305,30 +17308,6 @@ yynewstate: if yyS[yypt-1].item != nil { st.LockInfo = yyS[yypt-1].item.(*ast.SelectLockInfo) } - lastField := st.Fields.Fields[len(st.Fields.Fields)-1] - if lastField.Expr != nil && lastField.AsName.O == "" { - src := parser.src - var lastEnd int - if yyS[yypt-5].item != nil { - lastEnd = yyS[yypt-5].offset - 1 - } else if yyS[yypt-4].item != nil { - lastEnd = yyS[yypt-4].offset - 1 - } else if yyS[yypt-3].item != nil { - lastEnd = yyS[yypt-3].offset - 1 - } else if yyS[yypt-2].item != nil { - lastEnd = yyS[yypt-2].offset - 1 - } else if st.LockInfo != nil && st.LockInfo.LockType != ast.SelectLockNone { - lastEnd = yyS[yypt-1].offset - 1 - } else if yyS[yypt-0].item != nil { - lastEnd = yyS[yypt].offset - 1 - } else { - lastEnd = len(src) - if src[lastEnd-1] == ';' { - lastEnd-- - } - } - lastField.SetText(parser.lexer.client, src[lastField.Offset:lastEnd]) - } if yyS[yypt-5].item != nil { st.Where = yyS[yypt-5].item.(ast.ExprNode) } diff --git a/parser/parser.y b/parser/parser.y index 514103f7196b6..b0447bb4794af 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -5826,19 +5826,22 @@ FieldList: { field := $1.(*ast.SelectField) field.Offset = parser.startOffset(&yyS[yypt]) + if field.Expr != nil && field.AsName.O == "" { + endOffset := parser.yylval.offset + field.SetText(parser.lexer.client, strings.TrimSpace(parser.src[field.Offset:endOffset])) + } $$ = []*ast.SelectField{field} } | FieldList ',' Field { fl := $1.([]*ast.SelectField) - last := fl[len(fl)-1] - if last.Expr != nil && last.AsName.O == "" { - lastEnd := parser.endOffset(&yyS[yypt-1]) - last.SetText(parser.lexer.client, parser.src[last.Offset:lastEnd]) + field := $3.(*ast.SelectField) + field.Offset = parser.startOffset(&yyS[yypt]) + if field.Expr != nil && field.AsName.O == "" { + endOffset := parser.yylval.offset + field.SetText(parser.lexer.client, strings.TrimSpace(parser.src[field.Offset:endOffset])) } - newField := $3.(*ast.SelectField) - newField.Offset = parser.startOffset(&yyS[yypt]) - $$ = append(fl, newField) + $$ = append(fl, field) } GroupByClause: @@ -8576,30 +8579,6 @@ SelectStmt: if $6 != nil { st.LockInfo = $6.(*ast.SelectLockInfo) } - lastField := st.Fields.Fields[len(st.Fields.Fields)-1] - if lastField.Expr != nil && lastField.AsName.O == "" { - src := parser.src - var lastEnd int - if $2 != nil { - lastEnd = yyS[yypt-5].offset - 1 - } else if $3 != nil { - lastEnd = yyS[yypt-4].offset - 1 - } else if $4 != nil { - lastEnd = yyS[yypt-3].offset - 1 - } else if $5 != nil { - lastEnd = yyS[yypt-2].offset - 1 - } else if st.LockInfo != nil && st.LockInfo.LockType != ast.SelectLockNone { - lastEnd = yyS[yypt-1].offset - 1 - } else if $7 != nil { - lastEnd = yyS[yypt].offset - 1 - } else { - lastEnd = len(src) - if src[lastEnd-1] == ';' { - lastEnd-- - } - } - lastField.SetText(parser.lexer.client, src[lastField.Offset:lastEnd]) - } if $2 != nil { st.Where = $2.(ast.ExprNode) } @@ -10691,6 +10670,16 @@ ShowStmt: DBName: $5, } } +<<<<<<< HEAD +======= +| "SHOW" "CREATE" "RESOURCE" "GROUP" ResourceGroupName + { + $$ = &ast.ShowStmt{ + Tp: ast.ShowCreateResourceGroup, + ResourceGroupName: $5, + } + } +>>>>>>> 920f0199c1 (parser: Fix for field name output in multi statements (#40736)) | "SHOW" "CREATE" "USER" Username { // See https://dev.mysql.com/doc/refman/5.7/en/show-create-user.html diff --git a/parser/parser_test.go b/parser/parser_test.go index b5975c475c06a..f0647abcee76e 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -7048,3 +7048,21 @@ func TestTTLTableOption(t *testing.T) { RunTest(t, table, false) } + +func TestMultiStmt(t *testing.T) { + p := parser.New() + stmts, _, err := p.Parse("SELECT 'foo'; SELECT 'foo;bar','baz'; select 'foo' , 'bar' , 'baz' ;select 1", "", "") + require.NoError(t, err) + require.Equal(t, len(stmts), 4) + stmt1 := stmts[0].(*ast.SelectStmt) + stmt2 := stmts[1].(*ast.SelectStmt) + stmt3 := stmts[2].(*ast.SelectStmt) + stmt4 := stmts[3].(*ast.SelectStmt) + require.Equal(t, "'foo'", stmt1.Fields.Fields[0].Text()) + require.Equal(t, "'foo;bar'", stmt2.Fields.Fields[0].Text()) + require.Equal(t, "'baz'", stmt2.Fields.Fields[1].Text()) + require.Equal(t, "'foo'", stmt3.Fields.Fields[0].Text()) + require.Equal(t, "'bar'", stmt3.Fields.Fields[1].Text()) + require.Equal(t, "'baz'", stmt3.Fields.Fields[2].Text()) + require.Equal(t, "1", stmt4.Fields.Fields[0].Text()) +}