Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#40736
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
mjonss authored and ti-chi-bot committed Jan 31, 2023
1 parent f54825b commit 3e1a787
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 62 deletions.
41 changes: 10 additions & 31 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand Down Expand Up @@ -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)
}
Expand Down
51 changes: 20 additions & 31 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 3e1a787

Please sign in to comment.