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

expression: fix issue that TimeIsNull is not compatible with MySQL #10957

Closed
wants to merge 10 commits into from
Closed
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
17 changes: 14 additions & 3 deletions expression/builtin_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,12 @@ func (c *isNullFunctionClass) getFunction(ctx sessionctx.Context, args []Express
sig = &builtinRealIsNullSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_RealIsNull)
case types.ETDatetime:
sig = &builtinTimeIsNullSig{bf}
isNotNull := false
_, isCol := args[0].(*Column)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_, isCol := args[0].(*Column)
col, isCol := args[0].(*Column)

if isCol && mysql.HasNotNullFlag(args[0].GetType().Flag) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isCol && mysql.HasNotNullFlag(args[0].GetType().Flag) {
if isCol && mysql.HasNotNullFlag(col.GetType().Flag) {

There is no need to index the slice again.

isNotNull = true
}
sig = &builtinTimeIsNullSig{bf, isNotNull}
sig.setPbCode(tipb.ScalarFuncSig_TimeIsNull)
case types.ETDuration:
sig = &builtinDurationIsNullSig{bf}
Expand Down Expand Up @@ -900,15 +905,21 @@ func (b *builtinStringIsNullSig) evalInt(row chunk.Row) (int64, bool, error) {

type builtinTimeIsNullSig struct {
baseBuiltinFunc
isNotNull bool
}

func (b *builtinTimeIsNullSig) Clone() builtinFunc {
newSig := &builtinTimeIsNullSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
newSig.isNotNull = b.isNotNull
return newSig
}

func (b *builtinTimeIsNullSig) evalInt(row chunk.Row) (int64, bool, error) {
_, isNull, err := b.args[0].EvalTime(b.ctx, row)
return evalIsNull(isNull, err)
// Description below are from MySQL document:
// For DATE and DATETIME columns that are declared as NOT NULL,
// you can find the special date '0000-00-00' by using a statement like this:
// "SELECT * FROM tbl_name WHERE date_column IS NULL"
t, isNull, err := b.args[0].EvalTime(b.ctx, row)
return evalIsNull(isNull || (b.isNotNull && t.IsZero()), err)
}
22 changes: 14 additions & 8 deletions expression/distsql_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func pbTypeToFieldType(tp *tipb.FieldType) *types.FieldType {
}
}

func getSignatureByPB(ctx sessionctx.Context, sigCode tipb.ScalarFuncSig, tp *tipb.FieldType, args []Expression) (f builtinFunc, e error) {
fieldTp := pbTypeToFieldType(tp)
func getSignatureByPB(ctx sessionctx.Context, expr *tipb.Expr, args []Expression) (f builtinFunc, e error) {
fieldTp := pbTypeToFieldType(expr.FieldType)
base := newBaseBuiltinFunc(ctx, args)
base.tp = fieldTp
switch sigCode {
switch expr.GetSig() {
case tipb.ScalarFuncSig_CastIntAsInt:
f = &builtinCastIntAsIntSig{newBaseBuiltinCastFunc(base, false)}
case tipb.ScalarFuncSig_CastRealAsInt:
Expand Down Expand Up @@ -336,7 +336,12 @@ func getSignatureByPB(ctx sessionctx.Context, sigCode tipb.ScalarFuncSig, tp *ti
case tipb.ScalarFuncSig_RealIsNull:
f = &builtinRealIsNullSig{base}
case tipb.ScalarFuncSig_TimeIsNull:
f = &builtinTimeIsNullSig{base}
isNotNull := false
if len(expr.Children) > 0 && expr.Children[0].Tp == tipb.ExprType_ColumnRef && mysql.HasNotNullFlag(uint(expr.Children[0].FieldType.Flag)) {
isNotNull = true
}

f = &builtinTimeIsNullSig{base, isNotNull}
case tipb.ScalarFuncSig_StringIsNull:
f = &builtinStringIsNullSig{base}
case tipb.ScalarFuncSig_IntIsNull:
Expand Down Expand Up @@ -466,16 +471,16 @@ func getSignatureByPB(ctx sessionctx.Context, sigCode tipb.ScalarFuncSig, tp *ti
f = &builtinDateFormatSig{base}

default:
e = errFunctionNotExists.GenWithStackByArgs("FUNCTION", sigCode)
e = errFunctionNotExists.GenWithStackByArgs("FUNCTION", expr.GetSig())
return nil, e
}
return f, nil
}

func newDistSQLFunctionBySig(sc *stmtctx.StatementContext, sigCode tipb.ScalarFuncSig, tp *tipb.FieldType, args []Expression) (Expression, error) {
func newDistSQLFunctionBySig(sc *stmtctx.StatementContext, expr *tipb.Expr, args []Expression) (Expression, error) {
ctx := mock.NewContext()
ctx.GetSessionVars().StmtCtx = sc
f, err := getSignatureByPB(ctx, sigCode, tp, args)
f, err := getSignatureByPB(ctx, expr, args)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -541,7 +546,8 @@ func PBToExpr(expr *tipb.Expr, tps []*types.FieldType, sc *stmtctx.StatementCont
}
args = append(args, arg)
}
return newDistSQLFunctionBySig(sc, expr.Sig, expr.FieldType, args)

return newDistSQLFunctionBySig(sc, expr, args)
}

func convertTime(data []byte, ftPB *tipb.FieldType, tz *time.Location) (*Constant, error) {
Expand Down
25 changes: 25 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4404,6 +4404,31 @@ func (s *testIntegrationSuite) TestExprPushdownBlacklist(c *C) {
tk.MustQuery(`select * from mysql.expr_pushdown_blacklist`).Check(testkit.Rows())
}

func (s *testIntegrationSuite) TestIssue9763(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

tk.MustExec(`drop table if exists t1;`)
tk.MustExec(`CREATE TABLE t1 (a datetime not null);`)
tk.MustExec(`insert ignore into t1 values (0);`)
tk.MustQuery(`select * from t1 where a is null;`).Check(testkit.Rows(`0000-00-00 00:00:00`))

tk.MustExec(`drop table if exists t2;`)
tk.MustExec(`CREATE TABLE t2 (a date not null);`)
tk.MustExec(`insert ignore into t2 values (0);`)
tk.MustQuery(`select * from t2 where a is null;`).Check(testkit.Rows(`0000-00-00`))

tk.MustExec(`drop table if exists t3;`)
tk.MustExec(`CREATE TABLE t3 (a datetime);`)
tk.MustExec(`insert ignore into t3 values (0);`)
c.Assert(len(tk.MustQuery(`select * from t3 where a is null;`).Rows()), Equals, 0)

tk.MustExec(`drop table if exists t4;`)
tk.MustExec(`CREATE TABLE t4 (a date);`)
tk.MustExec(`insert ignore into t4 values (0);`)
c.Assert(len(tk.MustQuery(`select * from t4 where a is null;`).Rows()), Equals, 0)
}

func (s *testIntegrationSuite) TestIssue10804(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustQuery(`SELECT @@information_schema_stats_expiry`).Check(testkit.Rows(`86400`))
Expand Down