Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: Fix for field name output in multi statements #40736

Merged
merged 3 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 10 additions & 31 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15582,19 +15582,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 689:
{
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 690:
{
Expand Down Expand Up @@ -17543,30 +17546,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
43 changes: 11 additions & 32 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -5903,19 +5903,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 @@ -8677,30 +8680,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 @@ -10795,7 +10774,7 @@ ShowStmt:
| "SHOW" "CREATE" "RESOURCE" "GROUP" ResourceGroupName
{
$$ = &ast.ShowStmt{
Tp: ast.ShowCreateResourceGroup,
Tp: ast.ShowCreateResourceGroup,
ResourceGroupName: $5,
}
}
Expand Down
18 changes: 18 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7142,3 +7142,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())
}