func (s *testKVSuite) TestSeekMin(c *C) { defer testleak.AfterTest(c)() kvs := []struct { key string value string }{ {"DATA_test_main_db_tbl_tbl_test_record__00000000000000000001", "lock-version"}, {"DATA_test_main_db_tbl_tbl_test_record__00000000000000000001_0002", "1"}, {"DATA_test_main_db_tbl_tbl_test_record__00000000000000000001_0003", "hello"}, {"DATA_test_main_db_tbl_tbl_test_record__00000000000000000002", "lock-version"}, {"DATA_test_main_db_tbl_tbl_test_record__00000000000000000002_0002", "2"}, {"DATA_test_main_db_tbl_tbl_test_record__00000000000000000002_0003", "hello"}, } txn, err := s.s.Begin() c.Assert(err, IsNil) for _, kv := range kvs { txn.Set([]byte(kv.key), []byte(kv.value)) } it, err := txn.Seek(nil) for it.Valid() { fmt.Printf("%s, %s\n", it.Key(), it.Value()) it.Next() } it, err = txn.Seek([]byte("DATA_test_main_db_tbl_tbl_test_record__00000000000000000000")) c.Assert(err, IsNil) c.Assert(string(it.Key()), Equals, "DATA_test_main_db_tbl_tbl_test_record__00000000000000000001") for _, kv := range kvs { txn.Delete([]byte(kv.key)) } }
func (s *testSuite) TestGetDDLInfo(c *C) { defer testleak.AfterTest(c)() txn, err := s.store.Begin() c.Assert(err, IsNil) t := meta.NewMeta(txn) owner := &model.Owner{OwnerID: "owner"} err = t.SetDDLJobOwner(owner) c.Assert(err, IsNil) dbInfo2 := &model.DBInfo{ ID: 2, Name: model.NewCIStr("b"), State: model.StateNone, } job := &model.Job{ SchemaID: dbInfo2.ID, Type: model.ActionCreateSchema, } err = t.EnQueueDDLJob(job) c.Assert(err, IsNil) info, err := GetDDLInfo(txn) c.Assert(err, IsNil) c.Assert(info.Owner, DeepEquals, owner) c.Assert(info.Job, DeepEquals, job) c.Assert(info.ReorgHandle, Equals, int64(0)) err = txn.Commit() c.Assert(err, IsNil) }
func (s *testPlanSuite) TestNullRejectFinder(c *C) { defer testleak.AfterTest(c)() cases := []struct { expr string notNull bool }{ {"a = 1", true}, {"a != 100 and a > 0", true}, {"a is null", false}, {"a is not null", true}, {"a is true", true}, {"a is not true", false}, {"a is false", true}, {"a is not false", false}, {"a != 0 and a is not false", true}, {"a > 0 or true", false}, } for _, ca := range cases { sql := "select * from t where " + ca.expr comment := Commentf("for expr %s", ca.expr) s, err := s.ParseOneStmt(sql, "", "") c.Assert(err, IsNil, comment) finder := &nullRejectFinder{nullRejectTables: map[*ast.TableName]bool{}} stmt := s.(*ast.SelectStmt) mockResolve(stmt) stmt.Where.Accept(finder) if ca.notNull { c.Assert(finder.nullRejectTables, HasLen, 1, comment) } else { c.Assert(finder.nullRejectTables, HasLen, 0, comment) } } }
func (s *testTimeSuite) TestDate(c *C) { defer testleak.AfterTest(c)() table := []struct { Input string Expect string }{ {"2012-12-31", "2012-12-31"}, {"00-12-31", "2000-12-31"}, {"20121231", "2012-12-31"}, {"121231", "2012-12-31"}, {"2015-06-01 12:12:12", "2015-06-01"}, {"0001-01-01 00:00:00", "0001-01-01"}, {"0001-01-01", "0001-01-01"}, } for _, test := range table { t, err := ParseDate(test.Input) c.Assert(err, IsNil) c.Assert(t.String(), Equals, test.Expect) } errTable := []string{ "0121231", } for _, test := range errTable { _, err := ParseDate(test) c.Assert(err, NotNil) } }
func (s *testTimeSuite) TestTimestamp(c *C) { defer testleak.AfterTest(c)() table := []struct { Input string Expect string }{ {"2012-12-31 11:30:45", "2012-12-31 11:30:45"}, } for _, test := range table { t, err := ParseTimestamp(test.Input) c.Assert(err, IsNil) c.Assert(t.String(), Equals, test.Expect) } errTable := []string{ "2048-12-31 11:30:45", "1969-12-31 11:30:45", } for _, test := range errTable { _, err := ParseTimestamp(test) c.Assert(err, NotNil) } }
func (s *testEvaluatorSuite) TestHexFunc(c *C) { defer testleak.AfterTest(c)() tbl := []struct { Input interface{} Expect string }{ {12, "C"}, {12.3, "C"}, {12.5, "D"}, {-12.3, "FFFFFFFFFFFFFFF4"}, {-12.5, "FFFFFFFFFFFFFFF3"}, {"12", "3132"}, {0x12, "12"}, {"", ""}, } dtbl := tblToDtbl(tbl) for _, t := range dtbl { d, err := builtinHex(t["Input"], nil) c.Assert(err, IsNil) c.Assert(d, testutil.DatumEquals, t["Expect"][0]) } }
func (p *testPerfSchemaSuit) TestInsert(c *C) { defer testleak.AfterTest(c)() store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) defer store.Close() se := newSession(c, store, "") defer se.Close() mustExec(c, se, `insert into performance_schema.setup_actors values("localhost", "nieyy", "contributor", "NO", "NO");`) checkResult(c, se, 0, 0) cnt := mustQuery(c, se, "select * from performance_schema.setup_actors") c.Assert(cnt, Equals, 2) mustExec(c, se, `insert into performance_schema.setup_actors (host, user, role) values ("localhost", "lijian", "contributor");`) checkResult(c, se, 0, 0) cnt = mustQuery(c, se, "select * from performance_schema.setup_actors") c.Assert(cnt, Equals, 3) mustExec(c, se, `insert into performance_schema.setup_objects values("EVENT", "test", "%", "NO", "NO")`) checkResult(c, se, 0, 0) cnt = mustQuery(c, se, "select * from performance_schema.setup_objects") c.Assert(cnt, Equals, 13) mustExec(c, se, `insert into performance_schema.setup_objects (object_schema, object_name) values ("test1", "%")`) checkResult(c, se, 0, 0) cnt = mustQuery(c, se, "select * from performance_schema.setup_objects") c.Assert(cnt, Equals, 14) mustFailExec(c, se, `insert into performance_schema.setup_actors (host) values (null);`) mustFailExec(c, se, `insert into performance_schema.setup_objects (object_type) values (null);`) mustFailExec(c, se, `insert into performance_schema.setup_instruments values("select", "N/A", "N/A");`) mustFailExec(c, se, `insert into performance_schema.setup_consumers values("events_stages_current", "N/A");`) mustFailExec(c, se, `insert into performance_schema.setup_timers values("timer1", "XXXSECOND");`) }
func (s *testSuite) TestSetVar(c *C) { plan.UseNewPlanner = true defer testleak.AfterTest(c)() tk := testkit.NewTestKit(c, s.store) testSQL := "SET @a = 1;" tk.MustExec(testSQL) testSQL = `SET @a = "1";` tk.MustExec(testSQL) testSQL = "SET @a = null;" tk.MustExec(testSQL) testSQL = "SET @@global.autocommit = 1;" tk.MustExec(testSQL) // TODO: this test case should returns error. // testSQL = "SET @@global.autocommit = null;" // _, err := tk.Exec(testSQL) // c.Assert(err, NotNil) testSQL = "SET @@autocommit = 1;" tk.MustExec(testSQL) testSQL = "SET @@autocommit = null;" _, err := tk.Exec(testSQL) c.Assert(err, NotNil) errTestSql := "SET @@date_format = 1;" _, err = tk.Exec(errTestSql) c.Assert(err, NotNil) errTestSql = "SET @@rewriter_enabled = 1;" _, err = tk.Exec(errTestSql) c.Assert(err, NotNil) errTestSql = "SET xxx = abcd;" _, err = tk.Exec(errTestSql) c.Assert(err, NotNil) errTestSql = "SET @@global.a = 1;" _, err = tk.Exec(errTestSql) c.Assert(err, NotNil) errTestSql = "SET @@global.timestamp = 1;" _, err = tk.Exec(errTestSql) c.Assert(err, NotNil) // For issue 998 testSQL = "SET @issue998a=1, @issue998b=5;" tk.MustExec(testSQL) tk.MustQuery(`select @issue998a, @issue998b;`).Check(testkit.Rows("1 5")) testSQL = "SET @@autocommit=0, @issue998a=2;" tk.MustExec(testSQL) tk.MustQuery(`select @issue998a, @@autocommit;`).Check(testkit.Rows("2 0")) testSQL = "SET @@global.autocommit=1, @issue998b=6;" tk.MustExec(testSQL) tk.MustQuery(`select @issue998b, @@global.autocommit;`).Check(testkit.Rows("6 1")) plan.UseNewPlanner = false }
func (s *testPlanSuite) TestTableScanWithOrder(c *C) { defer testleak.AfterTest(c)() // Sort result by scanning PKHandle column. sql := "select * from t order by a limit 1;" stmt, err := s.ParseOneStmt(sql, "", "") c.Assert(err, IsNil) ast.SetFlag(stmt) err = newMockResolve(stmt) c.Assert(err, IsNil) builder := &planBuilder{ allocator: new(idAllocator), ctx: mock.NewContext(), colMapper: make(map[*ast.ColumnNameExpr]int), } p := builder.build(stmt) c.Assert(builder.err, IsNil) logic, ok := p.(LogicalPlan) c.Assert(ok, IsTrue) // Get physical plan. _, pp, _, err := logic.convert2PhysicalPlan(nil) c.Assert(err, IsNil) // Limit->Projection->PhysicalTableScan // Get PhysicalTableScan plan. cpp, ok := pp.p.GetChildByIndex(0).GetChildByIndex(0).(*PhysicalTableScan) c.Assert(cpp, NotNil) c.Assert(ok, IsTrue) // Make sure KeepOrder is true. c.Assert(cpp.KeepOrder, IsTrue) }
func (s *testSchemaSuite) TestSchemaResume(c *C) { defer testleak.AfterTest(c)() store := testCreateStore(c, "test_schema_resume") defer store.Close() d1 := newDDL(store, nil, nil, testLease) defer d1.close() testCheckOwner(c, d1, true, ddlJobFlag) dbInfo := testSchemaInfo(c, d1, "test") job := &model.Job{ SchemaID: dbInfo.ID, Type: model.ActionCreateSchema, Args: []interface{}{dbInfo}, } testRunInterruptedJob(c, d1, job) testCheckSchemaState(c, d1, dbInfo, model.StatePublic) job = &model.Job{ SchemaID: dbInfo.ID, Type: model.ActionDropSchema, } testRunInterruptedJob(c, d1, job) testCheckSchemaState(c, d1, dbInfo, model.StateNone) }
func (s *testDDLSuite) TestCallback(c *C) { defer testleak.AfterTest(c)() cb := &BaseCallback{} c.Assert(cb.OnChanged(nil), IsNil) cb.OnJobRunBefore(nil) cb.OnJobUpdated(nil) }
func (s *testSchemaSuite) TestSchema(c *C) { defer testleak.AfterTest(c)() store := testCreateStore(c, "test_schema") defer store.Close() d1 := newDDL(store, nil, nil, testLease) defer d1.close() ctx := mock.NewContext() dbInfo := testSchemaInfo(c, d1, "test") job := testCreateSchema(c, ctx, d1, dbInfo) testCheckSchemaState(c, d1, dbInfo, model.StatePublic) testCheckJobDone(c, d1, job, true) job = testDropSchema(c, ctx, d1, dbInfo) testCheckSchemaState(c, d1, dbInfo, model.StateNone) testCheckJobDone(c, d1, job, false) job = &model.Job{ SchemaID: dbInfo.ID, Type: model.ActionDropSchema, } err := d1.doDDLJob(ctx, job) c.Assert(terror.ErrorEqual(err, infoschema.ErrDatabaseDropExists), IsTrue) }
func (s *testDDLSuite) TestDropTableError(c *C) { defer testleak.AfterTest(c)() store := testCreateStore(c, "test_drop_table") defer store.Close() lease := 50 * time.Millisecond d := newDDL(store, nil, nil, lease) defer d.close() dbInfo := testSchemaInfo(c, d, "test") testCreateSchema(c, mock.NewContext(), d, dbInfo) job := &model.Job{ SchemaID: dbInfo.ID, Type: model.ActionDropTable, Args: []interface{}{&model.TableInfo{ ID: 1, Name: model.CIStr{O: "t"}, }}, } d.prepareBgJob(job) d.startBgJob(job.Type) time.Sleep(lease) verifyBgJobState(c, d, job, model.JobDone) }
func (s *testKVSuite) TestConditionIfNotExist(c *C) { defer testleak.AfterTest(c)() var success int64 cnt := 100 b := []byte("1") var wg sync.WaitGroup wg.Add(cnt) for i := 0; i < cnt; i++ { go func() { defer wg.Done() txn, err := s.s.Begin() c.Assert(err, IsNil) err = txn.Set(b, b) if err != nil { return } err = txn.Commit() if err == nil { atomic.AddInt64(&success, 1) } }() } wg.Wait() // At least one txn can success. c.Assert(success, Greater, int64(0)) // Clean up txn, err := s.s.Begin() c.Assert(err, IsNil) err = txn.Delete(b) c.Assert(err, IsNil) err = txn.Commit() c.Assert(err, IsNil) }
func (s *testEvaluatorSuite) TestLength(c *C) { defer testleak.AfterTest(c)() d, err := builtinLength(types.MakeDatums([]interface{}{nil}...), nil) c.Assert(err, IsNil) c.Assert(d.Kind(), Equals, types.KindNull) tbl := []struct { Input interface{} Expected int64 }{ {"abc", 3}, {1, 1}, {3.14, 4}, {mysql.Time{Time: time.Now(), Fsp: 6, Type: mysql.TypeDatetime}, 26}, {mysql.Bit{Value: 1, Width: 8}, 1}, {mysql.Hex{Value: 1}, 1}, {mysql.Set{Value: 1, Name: "abc"}, 3}, } dtbl := tblToDtbl(tbl) for _, t := range dtbl { d, err = builtinLength(t["Input"], nil) c.Assert(err, IsNil) c.Assert(d, testutil.DatumEquals, t["Expected"][0]) } }
func (s *testSegmentMapSuite) TestSegment(c *C) { defer testleak.AfterTest(c)() segs := int64(2) m, err := NewSegmentMap(segs) c.Assert(err, IsNil) c.Assert(m.SegmentCount(), Equals, segs) k := []byte("k") v := []byte("v") val, exist := m.Get(k) c.Assert(exist, IsFalse) exist = m.Set(k, v, false) c.Assert(exist, IsFalse) val, exist = m.Get(k) c.Assert(v, DeepEquals, val.([]byte)) c.Assert(exist, IsTrue) m0, err := m.GetSegment(0) c.Assert(err, IsNil) m1, err := m.GetSegment(1) c.Assert(err, IsNil) c.Assert(len(m0)+len(m1), Equals, 1) _, err = m.GetSegment(3) c.Assert(err, NotNil) }
func (s *testEvaluatorSuite) TestASCII(c *C) { defer testleak.AfterTest(c)() v, err := builtinASCII(types.MakeDatums([]interface{}{nil}...), nil) c.Assert(err, IsNil) c.Assert(v.Kind(), Equals, types.KindNull) for _, t := range []struct { Input interface{} Expected int64 }{ {"", 0}, {"A", 65}, {"你好", 228}, {1, 49}, {1.2, 49}, {true, 49}, {false, 48}, } { v, err = builtinASCII(types.MakeDatums(t.Input), nil) c.Assert(err, IsNil) c.Assert(v.GetInt64(), Equals, t.Expected) } v, err = builtinASCII(types.MakeDatums([]interface{}{errors.New("must error")}...), nil) c.Assert(err, NotNil) }
func (s *testCompareSuite) TestCompareDatum(c *C) { defer testleak.AfterTest(c)() cmpTbl := []struct { lhs Datum rhs Datum ret int // 0, 1, -1 }{ {MaxValueDatum(), NewDatum("00:00:00"), 1}, {MinNotNullDatum(), NewDatum("00:00:00"), -1}, {Datum{}, NewDatum("00:00:00"), -1}, {Datum{}, Datum{}, 0}, {MinNotNullDatum(), MinNotNullDatum(), 0}, {MaxValueDatum(), MaxValueDatum(), 0}, {Datum{}, MinNotNullDatum(), -1}, {MinNotNullDatum(), MaxValueDatum(), -1}, } for i, t := range cmpTbl { comment := Commentf("%d %v %v", i, t.lhs, t.rhs) ret, err := t.lhs.CompareDatum(t.rhs) c.Assert(err, IsNil) c.Assert(ret, Equals, t.ret, comment) ret, err = t.rhs.CompareDatum(t.lhs) c.Assert(err, IsNil) c.Assert(ret, Equals, -t.ret, comment) } }
func (p *testPerfSchemaSuit) TestConcurrentStatement(c *C) { defer testleak.AfterTest(c)() store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory + "/test_con_stmt") c.Assert(err, IsNil) defer store.Close() se := newSession(c, store, "test_con_stmt") mustExec(c, se, "drop table if exists test") mustExec(c, se, "create table test (a int, b int)") var wg sync.WaitGroup iFunc := func(a, b int) { defer wg.Done() sess := newSession(c, store, "test_con_stmt") mustExec(c, sess, fmt.Sprintf(`INSERT INTO test VALUES (%d, %d);`, a, b)) } sFunc := func() { defer wg.Done() sess := newSession(c, store, "test_con_stmt") mustQuery(c, sess, "select * from performance_schema.events_statements_current") mustQuery(c, sess, "select * from performance_schema.events_statements_history") } cnt := 10 for i := 0; i < cnt; i++ { wg.Add(2) go iFunc(i, i) go sFunc() } wg.Wait() }
func (s *testEvaluatorSuite) TestCurrentTime(c *C) { defer testleak.AfterTest(c)() tfStr := "15:04:05" last := time.Now() v, err := builtinCurrentTime(types.MakeDatums(nil), nil) c.Assert(err, IsNil) n := v.GetMysqlDuration() c.Assert(n.String(), HasLen, 8) c.Assert(n.String(), GreaterEqual, last.Format(tfStr)) v, err = builtinCurrentTime(types.MakeDatums(3), nil) c.Assert(err, IsNil) n = v.GetMysqlDuration() c.Assert(n.String(), HasLen, 12) c.Assert(n.String(), GreaterEqual, last.Format(tfStr)) v, err = builtinCurrentTime(types.MakeDatums(6), nil) c.Assert(err, IsNil) n = v.GetMysqlDuration() c.Assert(n.String(), HasLen, 15) c.Assert(n.String(), GreaterEqual, last.Format(tfStr)) v, err = builtinCurrentTime(types.MakeDatums(-1), nil) c.Assert(err, NotNil) v, err = builtinCurrentTime(types.MakeDatums(7), nil) c.Assert(err, NotNil) }
func (s *testFieldTypeSuite) TestDefaultTypeForValue(c *C) { defer testleak.AfterTest(c)() cases := []struct { value interface{} tp byte }{ {nil, mysql.TypeNull}, {1, mysql.TypeLonglong}, {uint64(1), mysql.TypeLonglong}, {"abc", mysql.TypeVarString}, {1.1, mysql.TypeNewDecimal}, {[]byte("abc"), mysql.TypeBlob}, {mysql.Bit{}, mysql.TypeBit}, {mysql.Hex{}, mysql.TypeVarchar}, {mysql.Time{Type: mysql.TypeDatetime}, mysql.TypeDatetime}, {mysql.Duration{}, mysql.TypeDuration}, {mysql.Decimal{}, mysql.TypeNewDecimal}, {mysql.Enum{}, mysql.TypeEnum}, {mysql.Set{}, mysql.TypeSet}, {nil, mysql.TypeNull}, } for _, ca := range cases { ft := DefaultTypeForValue(ca.value) c.Assert(ft.Tp, Equals, ca.tp, Commentf("%v %v", ft, ca)) } }
func (s *testEvaluatorSuite) TestLeft(c *C) { defer testleak.AfterTest(c)() args := types.MakeDatums([]interface{}{"abcdefg", int64(2)}...) v, err := builtinLeft(args, nil) c.Assert(err, IsNil) c.Assert(v.GetString(), Equals, "ab") args = types.MakeDatums([]interface{}{"abcdefg", int64(-1)}...) v, err = builtinLeft(args, nil) c.Assert(err, IsNil) c.Assert(v.GetString(), Equals, "") args = types.MakeDatums([]interface{}{"abcdefg", int64(100)}...) v, err = builtinLeft(args, nil) c.Assert(err, IsNil) c.Assert(v.GetString(), Equals, "abcdefg") args = types.MakeDatums([]interface{}{1, int64(1)}...) _, err = builtinLeft(args, nil) c.Assert(err, IsNil) args = types.MakeDatums([]interface{}{"abcdefg", "xxx"}...) _, err = builtinLeft(args, nil) c.Assert(err, NotNil) }
func (s *testTimeSuite) TestTimeFsp(c *C) { defer testleak.AfterTest(c)() table := []struct { Input string Fsp int Expect string }{ {"00:00:00.1", 0, "00:00:00"}, {"00:00:00.1", 1, "00:00:00.1"}, {"00:00:00.777777", 2, "00:00:00.78"}, {"00:00:00.777777", 6, "00:00:00.777777"}, // fsp -1 use default 0 {"00:00:00.777777", -1, "00:00:01"}, {"00:00:00.001", 3, "00:00:00.001"}, } for _, test := range table { t, err := ParseDuration(test.Input, test.Fsp) c.Assert(err, IsNil) c.Assert(t.String(), Equals, test.Expect) } errTable := []struct { Input string Fsp int }{ {"00:00:00.1", -2}, {"00:00:00.1", 7}, } for _, test := range errTable { _, err := ParseDuration(test.Input, test.Fsp) c.Assert(err, NotNil) } }
func (s *testEvaluatorSuite) TestRepeat(c *C) { defer testleak.AfterTest(c)() args := []interface{}{"a", int64(2)} v, err := builtinRepeat(types.MakeDatums(args...), nil) c.Assert(err, IsNil) c.Assert(v.GetString(), Equals, "aa") args = []interface{}{"a", uint64(2)} v, err = builtinRepeat(types.MakeDatums(args...), nil) c.Assert(err, IsNil) c.Assert(v.GetString(), Equals, "aa") args = []interface{}{"a", int64(-1)} v, err = builtinRepeat(types.MakeDatums(args...), nil) c.Assert(err, IsNil) c.Assert(v.GetString(), Equals, "") args = []interface{}{"a", int64(0)} v, err = builtinRepeat(types.MakeDatums(args...), nil) c.Assert(err, IsNil) c.Assert(v.GetString(), Equals, "") args = []interface{}{"a", uint64(0)} v, err = builtinRepeat(types.MakeDatums(args...), nil) c.Assert(err, IsNil) c.Assert(v.GetString(), Equals, "") }
func (s *testDDLSuite) TestDropTableError(c *C) { defer testleak.AfterTest(c)() store := testCreateStore(c, "test_drop_table") defer store.Close() d := newDDL(store, nil, nil, testLease) defer d.close() dbInfo := testSchemaInfo(c, d, "test") testCreateSchema(c, mock.NewContext(), d, dbInfo) job := &model.Job{ SchemaID: dbInfo.ID, Type: model.ActionDropTable, Args: []interface{}{&model.TableInfo{ ID: 1, Name: model.CIStr{O: "t"}, }}, } err := kv.RunInNewTxn(store, false, func(txn kv.Transaction) error { t := meta.NewMeta(txn) return d.prepareBgJob(t, job) }) c.Check(err, IsNil) d.startBgJob(job.Type) time.Sleep(testLease * 3) verifyBgJobState(c, d, job, model.JobDone) }
func (s *testEvaluatorSuite) TestLowerAndUpper(c *C) { defer testleak.AfterTest(c)() d, err := builtinLower(types.MakeDatums([]interface{}{nil}...), nil) c.Assert(err, IsNil) c.Assert(d.Kind(), Equals, types.KindNull) d, err = builtinUpper(types.MakeDatums([]interface{}{nil}...), nil) c.Assert(err, IsNil) c.Assert(d.Kind(), Equals, types.KindNull) tbl := []struct { Input interface{} Expect string }{ {"abc", "abc"}, {1, "1"}, } dtbl := tblToDtbl(tbl) for _, t := range dtbl { d, err = builtinLower(t["Input"], nil) c.Assert(err, IsNil) c.Assert(d, testutil.DatumEquals, t["Expect"][0]) d, err = builtinUpper(t["Input"], nil) c.Assert(err, IsNil) c.Assert(d.GetString(), Equals, strings.ToUpper(t["Expect"][0].GetString())) } }
func (s *testPlanSuite) TestFilterRate(c *C) { defer testleak.AfterTest(c)() cases := []struct { expr string rate float64 }{ {expr: "a = 1", rate: rateEqual}, {expr: "a > 1", rate: rateGreaterOrLess}, {expr: "a between 1 and 100", rate: rateBetween}, {expr: "a is null", rate: rateIsNull}, {expr: "a is not null", rate: rateFull - rateIsNull}, {expr: "a is true", rate: rateFull - rateIsNull - rateIsFalse}, {expr: "a is not true", rate: rateIsNull + rateIsFalse}, {expr: "a is false", rate: rateIsFalse}, {expr: "a is not false", rate: rateFull - rateIsFalse}, {expr: "a like 'a'", rate: rateLike}, {expr: "a not like 'a'", rate: rateFull - rateLike}, {expr: "a in (1, 2, 3)", rate: rateEqual * 3}, {expr: "a not in (1, 2, 3)", rate: rateFull - rateEqual*3}, {expr: "a > 1 and a < 9", rate: float64(rateGreaterOrLess) * float64(rateGreaterOrLess)}, {expr: "a = 1 or a = 2", rate: rateEqual + rateEqual - rateEqual*rateEqual}, {expr: "a != 1", rate: rateNotEqual}, } for _, ca := range cases { sql := "select 1 from dual where " + ca.expr s, err := s.ParseOneStmt(sql, "", "") c.Assert(err, IsNil, Commentf("for expr %s", ca.expr)) stmt := s.(*ast.SelectStmt) rate := guesstimateFilterRate(stmt.Where) c.Assert(rate, Equals, ca.rate, Commentf("for expr %s", ca.expr)) } }
func (s *testEvaluatorSuite) TestStrcmp(c *C) { defer testleak.AfterTest(c)() tbl := []struct { Input []interface{} Expect interface{} }{ {[]interface{}{"1", "2"}, -1}, {[]interface{}{"2", "1"}, 1}, {[]interface{}{"123", "2"}, -1}, {[]interface{}{"1", "213"}, -1}, {[]interface{}{"123", "123"}, 0}, {[]interface{}{"", "123"}, -1}, {[]interface{}{"123", ""}, 1}, {[]interface{}{"", ""}, 0}, {[]interface{}{nil, "123"}, nil}, {[]interface{}{"123", nil}, nil}, {[]interface{}{nil, nil}, nil}, {[]interface{}{"", nil}, nil}, {[]interface{}{nil, ""}, nil}, } dtbl := tblToDtbl(tbl) for _, t := range dtbl { d, err := builtinStrcmp(t["Input"], nil) c.Assert(err, IsNil) c.Assert(d, testutil.DatumEquals, t["Expect"][0]) } }
func (s *testPlanSuite) TestMultiColumnIndex(c *C) { defer testleak.AfterTest(c)() cases := []struct { sql string accessEqualCount int usedColumnCount int }{ {"select * from t where c = 0 and d = 0 and e = 0", 3, 3}, {"select * from t where c = 0 and d = 0 and e > 0", 2, 3}, {"select * from t where d > 0 and e = 0 and c = 0", 1, 2}, } for _, ca := range cases { comment := Commentf("for %s", ca.sql) s, err := s.ParseOneStmt(ca.sql, "", "") c.Assert(err, IsNil, comment) stmt := s.(*ast.SelectStmt) ast.SetFlag(stmt) mockResolve(stmt) b := &planBuilder{} p := b.buildFrom(stmt) err = Refine(p) c.Assert(err, IsNil) idxScan, ok := p.(*IndexScan) c.Assert(ok, IsTrue) c.Assert(idxScan.AccessEqualCount, Equals, ca.accessEqualCount) c.Assert(idxScan.Ranges[0].LowVal, HasLen, ca.usedColumnCount) } }
func (s *testKVSuite) TestRollback(c *C) { defer testleak.AfterTest(c)() txn, err := s.s.Begin() c.Assert(err, IsNil) err = txn.Rollback() c.Assert(err, IsNil) txn, err = s.s.Begin() c.Assert(err, IsNil) insertData(c, txn) mustGet(c, txn) err = txn.Rollback() c.Assert(err, IsNil) txn, err = s.s.Begin() c.Assert(err, IsNil) defer txn.Commit() for i := startIndex; i < testCount; i++ { _, err := txn.Get([]byte(strconv.Itoa(i))) c.Assert(err, NotNil) } }