Skip to content

Commit

Permalink
session: fix global sysdate_is_now won't work (#32785)
Browse files Browse the repository at this point in the history
close #32784
  • Loading branch information
Yisaer authored Mar 4, 2022
1 parent 55d0f12 commit 74d74b0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
38 changes: 38 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/expression"
Expand Down Expand Up @@ -2720,6 +2722,42 @@ func TestBitColumnPushDown(t *testing.T) {
tk.MustQuery(fmt.Sprintf("explain analyze %s", sql)).CheckAt([]int{0, 3, 6}, rows)
}

func TestSysdatePushDown(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(id int signed, id2 int unsigned, c varchar(11), d datetime, b double, e bit(10))")
tk.MustExec("insert into t(id, id2, c, d) values (-1, 1, 'abc', '2021-12-12')")
rows := [][]interface{}{
{"TableReader_7", "root", "data:Selection_6"},
{"└─Selection_6", "cop[tikv]", "gt(test.t.d, sysdate())"},
{" └─TableFullScan_5", "cop[tikv]", "keep order:false, stats:pseudo"},
}
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where d > sysdate()").
CheckAt([]int{0, 3, 6}, rows)
// assert sysdate isn't now after set global sysdate_is_now in the same session
tk.MustExec("set global sysdate_is_now='1'")
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where d > sysdate()").
CheckAt([]int{0, 3, 6}, rows)

// assert sysdate is now after set global sysdate_is_now in the new session
tk = testkit.NewTestKit(t, store)
tk.MustExec("use test")
now := time.Now()
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/expression/injectNow", fmt.Sprintf(`return(%d)`, now.Unix())))
rows[1][2] = fmt.Sprintf("gt(test.t.d, %v)", now.Format("2006-01-02 15:04:05"))
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where d > sysdate()").
CheckAt([]int{0, 3, 6}, rows)
failpoint.Disable("github.com/pingcap/tidb/expression/injectNow")

// assert sysdate isn't now after set session sysdate_is_now false in the same session
tk.MustExec("set sysdate_is_now='0'")
rows[1][2] = "gt(test.t.d, sysdate())"
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where d > sysdate()").
CheckAt([]int{0, 3, 6}, rows)
}

func TestScalarFunctionPushDown(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down
9 changes: 1 addition & 8 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1381,18 +1381,11 @@ var defaultSysVars = []*SysVar{
return nil
},
},
{Scope: ScopeGlobal | ScopeSession, Name: SysdateIsNow, Value: BoolToOnOff(DefSysdateIsNow), skipInit: true, Type: TypeBool,
{Scope: ScopeGlobal | ScopeSession, Name: SysdateIsNow, Value: BoolToOnOff(DefSysdateIsNow), Type: TypeBool,
SetSession: func(vars *SessionVars, s string) error {
vars.SysdateIsNow = TiDBOptOn(s)
return nil
},
GetGlobal: func(vars *SessionVars) (s string, err error) {
return strconv.FormatBool(GlobalSysdateIsNow.Load()), nil
},
SetGlobal: func(vars *SessionVars, s string) error {
GlobalSysdateIsNow.Store(TiDBOptOn(s))
return nil
},
},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableMutationChecker, Hidden: true,
Value: BoolToOnOff(DefTiDBEnableMutationChecker), Type: TypeBool,
Expand Down
1 change: 0 additions & 1 deletion sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,5 @@ var (
EnableColumnTracking = atomic.NewBool(DefTiDBEnableColumnTracking)
StatsLoadSyncWait = atomic.NewInt64(DefTiDBStatsLoadSyncWait)
StatsLoadPseudoTimeout = atomic.NewBool(DefTiDBStatsLoadPseudoTimeout)
GlobalSysdateIsNow = atomic.NewBool(DefSysdateIsNow)
MemQuotaBindCache = atomic.NewInt64(DefTiDBMemQuotaBindCache)
)

0 comments on commit 74d74b0

Please sign in to comment.