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

parse: fix the type of date/time parameters #48237

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix the type of date/time parameters
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
  • Loading branch information
YangKeao committed Nov 2, 2023
commit c68f290a86ae3ec6ae7a585c89119facda359bda
2 changes: 1 addition & 1 deletion pkg/server/conn_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (cc *clientConn) handleStmtExecute(ctx context.Context, data []byte) (err e
paramValues = data[pos+1:]
}

err = parse.ExecArgs(cc.ctx.GetSessionVars().StmtCtx, args, stmt.BoundParams(), nullBitmaps, stmt.GetParamsType(), paramValues, cc.inputDecoder)
err = parse.ExecArgs(cc.ctx.GetSessionVars().StmtCtx.TypeCtx(), args, stmt.BoundParams(), nullBitmaps, stmt.GetParamsType(), paramValues, cc.inputDecoder)
// This `.Reset` resets the arguments, so it's fine to just ignore the error (and the it'll be reset again in the following routine)
errReset := stmt.Reset()
if errReset != nil {
Expand Down
2 changes: 0 additions & 2 deletions pkg/server/internal/parse/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ go_library(
"//pkg/parser/mysql",
"//pkg/server/internal/handshake",
"//pkg/server/internal/util",
"//pkg/sessionctx/stmtctx",
"//pkg/types",
"//pkg/util/dbterror",
"//pkg/util/hack",
Expand All @@ -38,7 +37,6 @@ go_test(
"//pkg/parser/terror",
"//pkg/server/internal/handshake",
"//pkg/server/internal/util",
"//pkg/sessionctx/stmtctx",
"//pkg/types",
"@com_github_stretchr_testify//require",
],
Expand Down
34 changes: 29 additions & 5 deletions pkg/server/internal/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/server/internal/handshake"
util2 "github.com/pingcap/tidb/pkg/server/internal/util"
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/dbterror"
"github.com/pingcap/tidb/pkg/util/hack"
Expand All @@ -44,7 +43,7 @@ const (
)

// ExecArgs parse execute arguments to datum slice.
func ExecArgs(sc *stmtctx.StatementContext, params []expression.Expression, boundParams [][]byte,
func ExecArgs(typectx types.Context, params []expression.Expression, boundParams [][]byte,
nullBitmap, paramTypes, paramValues []byte, enc *util2.InputDecoder) (err error) {
pos := 0
var (
Expand Down Expand Up @@ -193,7 +192,26 @@ func ExecArgs(sc *stmtctx.StatementContext, params []expression.Expression, boun
err = mysql.ErrMalformPacket
return
}
args[i] = types.NewDatum(tmp) // FIXME: After check works!!!!!!
// TODO: generate the time datum directly
var parseTime func(types.Context, string) (types.Time, error)
switch tp {
case mysql.TypeDate:
parseTime = types.ParseDate
case mysql.TypeDatetime:
parseTime = types.ParseDatetime
case mysql.TypeTimestamp:
// To be compatible with MySQL, even the type of parameter is
// TypeTimestamp, the return type should also be `Datetime`.
parseTime = types.ParseDatetime
}
var time types.Time
time, err = parseTime(typectx, tmp.(string))
err = typectx.HandleTruncate(err)
if err != nil {
// TODO: handle the warning and errors
return
}
args[i] = types.NewDatum(time)
continue

case mysql.TypeDuration:
Expand Down Expand Up @@ -228,7 +246,13 @@ func ExecArgs(sc *stmtctx.StatementContext, params []expression.Expression, boun
err = mysql.ErrMalformPacket
return
}
args[i] = types.NewDatum(tmp)
var dur types.Duration
dur, _, err = types.ParseDuration(typectx, tmp.(string), types.MaxFsp)
err = typectx.HandleTruncate(err)
if err != nil {
return
}
args[i] = types.NewDatum(dur)
continue
case mysql.TypeNewDecimal:
if len(paramValues) < (pos + 1) {
Expand All @@ -246,7 +270,7 @@ func ExecArgs(sc *stmtctx.StatementContext, params []expression.Expression, boun
args[i] = types.NewDecimalDatum(nil)
} else {
var dec types.MyDecimal
err = sc.HandleTruncate(dec.FromString(v))
err = typectx.HandleTruncate(dec.FromString(v))
if err != nil {
return err
}
Expand Down
50 changes: 36 additions & 14 deletions pkg/server/internal/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package parse

import (
"testing"
"time"

"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/parser/terror"
"github.com/pingcap/tidb/pkg/server/internal/util"
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
"github.com/pingcap/tidb/pkg/types"
"github.com/stretchr/testify/require"
)
Expand All @@ -37,6 +37,7 @@ func TestParseExecArgs(t *testing.T) {
tests := []struct {
args args
err error
warn error
expect interface{}
}{
// Tests for int overflow
Expand All @@ -49,6 +50,7 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0xff},
},
nil,
nil,
int64(-1),
},
{
Expand All @@ -60,6 +62,7 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0xff, 0xff},
},
nil,
nil,
int64(-1),
},
{
Expand All @@ -71,6 +74,7 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0xff, 0xff, 0xff, 0xff},
},
nil,
nil,
int64(-1),
},
// Tests for date/datetime/timestamp
Expand All @@ -83,7 +87,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x0b, 0xda, 0x07, 0x0a, 0x11, 0x13, 0x1b, 0x1e, 0x01, 0x00, 0x00, 0x00},
},
nil,
"2010-10-17 19:27:30.000001",
nil,
types.NewTime(types.FromDate(2010, 10, 17, 19, 27, 30, 1), mysql.TypeDatetime, types.MaxFsp),
},
{
args{
Expand All @@ -94,7 +99,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x04, 0xda, 0x07, 0x0a, 0x11},
},
nil,
"2010-10-17",
nil,
types.NewTime(types.FromDate(2010, 10, 17, 0, 0, 0, 0), mysql.TypeDate, types.DefaultFsp),
},
{
args{
Expand All @@ -105,7 +111,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x0b, 0xda, 0x07, 0x0a, 0x11, 0x13, 0x1b, 0x1e, 0x01, 0x00, 0x00, 0x00},
},
nil,
"2010-10-17 19:27:30.000001",
nil,
types.NewTime(types.FromDate(2010, 10, 17, 19, 27, 30, 1), mysql.TypeDatetime, types.MaxFsp),
},
{
args{
Expand All @@ -116,7 +123,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x07, 0xda, 0x07, 0x0a, 0x11, 0x13, 0x1b, 0x1e},
},
nil,
"2010-10-17 19:27:30",
nil,
types.NewTime(types.FromDate(2010, 10, 17, 19, 27, 30, 0), mysql.TypeDatetime, types.DefaultFsp),
},
{
args{
Expand All @@ -127,7 +135,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x0d, 0xdb, 0x07, 0x02, 0x03, 0x04, 0x05, 0x06, 0x40, 0xe2, 0x01, 0x00, 0xf2, 0x02},
},
nil,
"2011-02-03 04:05:06.123456+12:34",
nil,
types.NewTime(types.FromDate(2011, 02, 02, 15, 31, 06, 123456), mysql.TypeDatetime, types.MaxFsp),
},
{
args{
Expand All @@ -138,7 +147,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x0d, 0xdb, 0x07, 0x02, 0x03, 0x04, 0x05, 0x06, 0x40, 0xe2, 0x01, 0x00, 0x0e, 0xfd},
},
nil,
"2011-02-03 04:05:06.123456-12:34",
nil,
types.NewTime(types.FromDate(2011, 02, 03, 16, 39, 06, 123456), mysql.TypeDatetime, types.MaxFsp),
},
{
args{
Expand All @@ -149,7 +159,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x00},
},
nil,
types.ZeroDatetimeStr,
nil,
types.NewTime(types.ZeroCoreTime, mysql.TypeDatetime, types.DefaultFsp),
},
// Tests for time
{
Expand All @@ -161,7 +172,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x0c, 0x01, 0x78, 0x00, 0x00, 0x00, 0x13, 0x1b, 0x1e, 0x01, 0x00, 0x00, 0x00},
},
nil,
"-120 19:27:30.000001",
types.ErrTruncatedWrongVal,
types.Duration{types.MinTime, types.MaxFsp},
},
{
args{
Expand All @@ -172,7 +184,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x08, 0x01, 0x78, 0x00, 0x00, 0x00, 0x13, 0x1b, 0x1e},
},
nil,
"-120 19:27:30",
types.ErrTruncatedWrongVal,
types.Duration{types.MinTime, types.MaxFsp},
},
{
args{
Expand All @@ -183,7 +196,8 @@ func TestParseExecArgs(t *testing.T) {
[]byte{0x00},
},
nil,
"0",
nil,
types.Duration{time.Duration(0), types.MaxFsp},
},
// For error test
{
Expand All @@ -196,6 +210,7 @@ func TestParseExecArgs(t *testing.T) {
},
mysql.ErrMalformPacket,
nil,
nil,
},
{
args{
Expand All @@ -207,6 +222,7 @@ func TestParseExecArgs(t *testing.T) {
},
mysql.ErrMalformPacket,
nil,
nil,
},
{
args{
Expand All @@ -218,11 +234,17 @@ func TestParseExecArgs(t *testing.T) {
},
mysql.ErrMalformPacket,
nil,
nil,
},
}
for _, tt := range tests {
err := ExecArgs(stmtctx.NewStmtCtx(), tt.args.args, tt.args.boundParams, tt.args.nullBitmap, tt.args.paramTypes, tt.args.paramValues, nil)
var warn error
typectx := types.NewContext(types.DefaultStmtFlags.WithTruncateAsWarning(true), time.UTC, func(err error) {
warn = err
})
err := ExecArgs(typectx, tt.args.args, tt.args.boundParams, tt.args.nullBitmap, tt.args.paramTypes, tt.args.paramValues, nil)
require.Truef(t, terror.ErrorEqual(err, tt.err), "err %v", err)
require.Truef(t, terror.ErrorEqual(warn, tt.warn), "warn %v", warn)
if err == nil {
require.Equal(t, tt.expect, tt.args.args[0].(*expression.Constant).Value.GetValue())
}
Expand All @@ -231,7 +253,7 @@ func TestParseExecArgs(t *testing.T) {

func TestParseExecArgsAndEncode(t *testing.T) {
dt := expression.Args2Expressions4Test(1)
err := ExecArgs(stmtctx.NewStmtCtx(),
err := ExecArgs(types.DefaultStmtNoWarningContext,
dt,
[][]byte{nil},
[]byte{0x0},
Expand All @@ -241,7 +263,7 @@ func TestParseExecArgsAndEncode(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "测试", dt[0].(*expression.Constant).Value.GetValue())

err = ExecArgs(stmtctx.NewStmtCtx(),
err = ExecArgs(types.DefaultStmtNoWarningContext,
dt,
[][]byte{{178, 226, 202, 212}},
[]byte{0x0},
Expand Down