Skip to content

Commit 3f20a9d

Browse files
Backport sqlparser patch for v15->v19 upgrade: 14763 Fix accepting bind variables in time related function calls (#590)
* Fix accepting bind variables in time related function calls. (vitessio#14763) Signed-off-by: Manan Gupta <manan@planetscale.com> * fix test --------- Signed-off-by: Manan Gupta <manan@planetscale.com> Co-authored-by: Manan Gupta <35839558+GuptaManan100@users.noreply.github.com>
1 parent bb6271b commit 3f20a9d

File tree

16 files changed

+6336
-6308
lines changed

16 files changed

+6336
-6308
lines changed

go/test/endtoend/vtgate/queries/misc/misc_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func TestHighNumberOfParams(t *testing.T) {
214214
}
215215

216216
func TestPrepareStatements(t *testing.T) {
217+
utils.SkipIfBinaryIsBelowVersion(t, 17, "vtgate")
217218
mcmp, closer := start(t)
218219
defer closer()
219220

go/vt/sqlparser/ast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2498,7 +2498,7 @@ type (
24982498
// supported functions are documented in the grammar
24992499
CurTimeFuncExpr struct {
25002500
Name IdentifierCI
2501-
Fsp int // fractional seconds precision, integer from 0 to 6 or an Argument
2501+
Fsp Expr // fractional seconds precision, integer from 0 to 6 or an Argument
25022502
}
25032503

25042504
// JSONPrettyExpr represents the function and argument for JSON_PRETTY()

go/vt/sqlparser/ast_clone.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/ast_copy_on_rewrite.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/ast_equals.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/ast_format.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,8 +1548,8 @@ func (node *WeightStringFuncExpr) Format(buf *TrackedBuffer) {
15481548

15491549
// Format formats the node.
15501550
func (node *CurTimeFuncExpr) Format(buf *TrackedBuffer) {
1551-
if node.Fsp > 0 {
1552-
buf.astPrintf(node, "%#s(%d)", node.Name.String(), node.Fsp)
1551+
if node.Fsp != nil {
1552+
buf.astPrintf(node, "%#s(%v)", node.Name.String(), node.Fsp)
15531553
} else {
15541554
buf.astPrintf(node, "%#s()", node.Name.String())
15551555
}

go/vt/sqlparser/ast_format_fast.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/ast_rewrite.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/ast_visit.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/cached_size.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/normalizer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ func (nz *normalizer) walkStatementUp(cursor *Cursor) bool {
6868
if !isLiteral {
6969
return true
7070
}
71+
_, isCurTimeFunc := cursor.Parent().(*CurTimeFuncExpr)
72+
if isCurTimeFunc {
73+
return true
74+
}
7175
nz.convertLiteral(node, cursor)
7276
return nz.err == nil // only continue if we haven't found any errors
7377
}
@@ -133,6 +137,8 @@ func (nz *normalizer) walkDownSelect(node, parent SQLNode) bool {
133137
case *ConvertType:
134138
// we should not rewrite the type description
135139
return false
140+
case *CurTimeFuncExpr:
141+
return false
136142
}
137143
return nz.err == nil // only continue if we haven't found any errors
138144
}

go/vt/sqlparser/normalizer_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,18 @@ func TestNormalize(t *testing.T) {
421421
"bv1": sqltypes.Int64BindVariable(1),
422422
"bv2": sqltypes.Int64BindVariable(0),
423423
},
424+
}, {
425+
// we don't want to replace literals in cur time function calls.
426+
in: `insert into t1(id2) values (NOW(1))`,
427+
outstmt: `insert into t1(id2) values (now(1))`,
428+
outbv: map[string]*querypb.BindVariable{},
429+
}, {
430+
// we don't want to replace literals in cur time function calls.
431+
in: `select (select now(2)) from (select 1 from dual where NOW(1) < 2) as t`,
432+
outstmt: `select (select now(2) from dual) from (select 1 from dual where now(1) < :bv1 /* INT64 */) as t`,
433+
outbv: map[string]*querypb.BindVariable{
434+
"bv1": sqltypes.Int64BindVariable(2),
435+
},
424436
}}
425437
parser := NewTestParser()
426438
for _, tc := range testcases {

go/vt/sqlparser/parse_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,8 @@ var (
10451045
input: "select /* utc_timestamp as func */ utc_timestamp() from t",
10461046
}, {
10471047
input: "select /* utc_timestamp with fsp */ utc_timestamp(1) from t",
1048+
}, {
1049+
input: "select /* utc_timestamp with fsp */ utc_timestamp(:val1) from t",
10481050
}, {
10491051
input: "select /* utc_time */ utc_time() from t",
10501052
}, {

0 commit comments

Comments
 (0)