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

Backport to release-14: Fix parsing of CAST() statements #10512 and #10514 #10517

Merged
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
15 changes: 12 additions & 3 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2350,14 +2350,22 @@ type (
To Expr
}

// ConvertExpr represents a call to CONVERT(expr, type)
// or it's equivalent CAST(expr AS type). Both are rewritten to the former.
ConvertExpr struct {
// CastExpr represents a call to CAST(expr AS type)
// This is separate from CONVERT(expr, type) since there are
// places such as in CREATE TABLE statements where they
// are treated differently.
CastExpr struct {
Expr Expr
Type *ConvertType
Array bool
}

// ConvertExpr represents a call to CONVERT(expr, type)
ConvertExpr struct {
Expr Expr
Type *ConvertType
}

// ConvertUsingExpr represents a call to CONVERT(expr USING charset).
ConvertUsingExpr struct {
Expr Expr
Expand Down Expand Up @@ -2786,6 +2794,7 @@ func (*WeightStringFuncExpr) iExpr() {}
func (*CurTimeFuncExpr) iExpr() {}
func (*CaseExpr) iExpr() {}
func (*ValuesFuncExpr) iExpr() {}
func (*CastExpr) iExpr() {}
func (*ConvertExpr) iExpr() {}
func (*SubstrExpr) iExpr() {}
func (*ConvertUsingExpr) iExpr() {}
Expand Down
17 changes: 17 additions & 0 deletions go/vt/sqlparser/ast_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1676,14 +1676,19 @@ func (node WindowDefinitions) Format(buf *TrackedBuffer) {
}

// Format formats the node.
func (node *ConvertExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "convert(%v, %v", node.Expr, node.Type)
func (node *CastExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "cast(%v as %v", node.Expr, node.Type)
if node.Array {
buf.astPrintf(node, " %s", keywordStrings[ARRAY])
}
buf.astPrintf(node, ")")
}

// Format formats the node.
func (node *ConvertExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "convert(%v, %v)", node.Expr, node.Type)
}

// Format formats the node.
func (node *ConvertUsingExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "convert(%v using %s)", node.Expr, node.Type)
Expand Down
15 changes: 12 additions & 3 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions go/vt/sqlparser/ast_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions go/vt/sqlparser/ast_visit.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion go/vt/sqlparser/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/vt/sqlparser/normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func TestNormalize(t *testing.T) {
}, {
// Do not normalize cast/convert types
in: `select CAST("test" AS CHAR(60))`,
outstmt: `select convert(:bv1, CHAR(60)) from dual`,
outstmt: `select cast(:bv1 as CHAR(60)) from dual`,
outbv: map[string]*querypb.BindVariable{
"bv1": sqltypes.StringBindVariable("test"),
},
Expand Down
10 changes: 3 additions & 7 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3648,8 +3648,7 @@ func TestConvert(t *testing.T) {
input string
output string
}{{
input: "select cast('abc' as date) from t",
output: "select convert('abc', date) from t",
input: "select cast('abc' as date) from t",
}, {
input: "select convert('abc', binary(4)) from t",
}, {
Expand Down Expand Up @@ -3702,10 +3701,7 @@ func TestConvert(t *testing.T) {
}, {
input: "select convert('abc', json) from t",
}, {
input: "select convert(json_keys(c), char(64) array) from t",
}, {
input: "select cast(json_keys(c) as char(64) array) from t",
output: "select convert(json_keys(c), char(64) array) from t",
input: "select cast(json_keys(c) as char(64) array) from t",
}}

for _, tcase := range validSQL {
Expand Down Expand Up @@ -5042,7 +5038,7 @@ partition by range (YEAR(purchased)) subpartition by hash (TO_DAYS(purchased))
},
{
input: "create table t (id int, info JSON, INDEX zips((CAST(info->'$.field' AS unsigned ARRAY))))",
output: "create table t (\n\tid int,\n\tinfo JSON,\n\tINDEX zips ((convert(info -> '$.field', unsigned array)))\n)",
output: "create table t (\n\tid int,\n\tinfo JSON,\n\tINDEX zips ((cast(info -> '$.field' as unsigned array)))\n)",
},
}
for _, test := range createTableQueries {
Expand Down
Loading