From 3464dae313e37dd72ec593c0c42d49b475dcb221 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Wed, 21 Aug 2024 10:39:11 +0800 Subject: [PATCH] parser: call `SetText` correctly for `CreateViewStmt` (#55520) close pingcap/tidb#55441 --- pkg/parser/parser.go | 6 +++--- pkg/parser/parser.y | 8 ++++---- pkg/parser/parser_test.go | 10 ++++++++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index b1b6d1a956ea8..5c8c5cc545a09 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -16064,8 +16064,8 @@ yynewstate: case 503: { startOffset := parser.startOffset(&yyS[yypt-1]) + endOffset := parser.yylval.offset selStmt := yyS[yypt-1].statement.(ast.StmtNode) - selStmt.SetText(parser.lexer.client, strings.TrimSpace(parser.src[startOffset:])) x := &ast.CreateViewStmt{ OrReplace: yyS[yypt-9].item.(bool), ViewName: yyS[yypt-4].item.(*ast.TableName), @@ -16079,11 +16079,11 @@ yynewstate: } if yyS[yypt-0].item != nil { x.CheckOption = yyS[yypt-0].item.(model.ViewCheckOption) - endOffset := parser.startOffset(&yyS[yypt]) - selStmt.SetText(parser.lexer.client, strings.TrimSpace(parser.src[startOffset:endOffset])) + endOffset = parser.startOffset(&yyS[yypt]) } else { x.CheckOption = model.CheckOptionCascaded } + selStmt.SetText(parser.lexer.client, strings.TrimSpace(parser.src[startOffset:endOffset])) parser.yyVAL.statement = x } case 504: diff --git a/pkg/parser/parser.y b/pkg/parser/parser.y index 415c6e757251d..c5e68b56fd375 100644 --- a/pkg/parser/parser.y +++ b/pkg/parser/parser.y @@ -5002,8 +5002,8 @@ CreateViewStmt: "CREATE" OrReplace ViewAlgorithm ViewDefiner ViewSQLSecurity "VIEW" ViewName ViewFieldList "AS" CreateViewSelectOpt ViewCheckOption { startOffset := parser.startOffset(&yyS[yypt-1]) + endOffset := parser.yylval.offset selStmt := $10.(ast.StmtNode) - selStmt.SetText(parser.lexer.client, strings.TrimSpace(parser.src[startOffset:])) x := &ast.CreateViewStmt{ OrReplace: $2.(bool), ViewName: $7.(*ast.TableName), @@ -5017,11 +5017,11 @@ CreateViewStmt: } if $11 != nil { x.CheckOption = $11.(model.ViewCheckOption) - endOffset := parser.startOffset(&yyS[yypt]) - selStmt.SetText(parser.lexer.client, strings.TrimSpace(parser.src[startOffset:endOffset])) + endOffset = parser.startOffset(&yyS[yypt]) } else { x.CheckOption = model.CheckOptionCascaded } + selStmt.SetText(parser.lexer.client, strings.TrimSpace(parser.src[startOffset:endOffset])) $$ = x } @@ -14033,7 +14033,7 @@ CreateBindingStmt: { x := &ast.CreateBindingStmt{ GlobalScope: $2.(bool), - PlanDigests: $9.([]*ast.StringOrUserVar), + PlanDigests: $9.([]*ast.StringOrUserVar), } $$ = x diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index 11048eb701198..fe0d8ed418e5f 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -5744,6 +5744,16 @@ func TestView(t *testing.T) { require.Equal(t, "select c,d,e from t", v.Select.Text()) require.Equal(t, model.SecurityDefiner, v.Security) require.Equal(t, model.CheckOptionCascaded, v.CheckOption) + + src = ` +CREATE VIEW v1 AS SELECT * FROM t; +CREATE VIEW v2 AS SELECT 123123123123123; +` + nodes, _, err := p.Parse(src, "", "") + require.NoError(t, err) + require.Len(t, nodes, 2) + require.Equal(t, nodes[0].(*ast.CreateViewStmt).Select.Text(), "SELECT * FROM t") + require.Equal(t, nodes[1].(*ast.CreateViewStmt).Select.Text(), "SELECT 123123123123123") } func TestTimestampDiffUnit(t *testing.T) {