func (s *testIndexChangeSuite) checkDropDeleteOnly(d *ddl, ctx context.Context, writeTbl, delTbl table.Table) error { // WriteOnlyTable insert t values (9, 9) _, err := writeTbl.AddRecord(ctx, types.MakeDatums(9, 9)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, writeTbl, 9, 9, true) // DeleteOnlyTable insert t values (10, 10) _, err = delTbl.AddRecord(ctx, types.MakeDatums(10, 10)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, writeTbl, 10, 10, false) if err != nil { return errors.Trace(err) } // DeleteOnlyTable update t set c2 = 10 where c1 = 9 err = delTbl.UpdateRecord(ctx, 9, types.MakeDatums(9, 9), types.MakeDatums(9, 10), touchedMap(delTbl)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, writeTbl, 9, 9, false) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, writeTbl, 10, 9, false) if err != nil { return errors.Trace(err) } return nil }
func (e *ShowExec) fetchShowIndex() error { tb, err := e.getTable() if err != nil { return errors.Trace(err) } if tb.Meta().PKIsHandle { var pkCol *table.Column for _, col := range tb.Cols() { if mysql.HasPriKeyFlag(col.Flag) { pkCol = col break } } data := types.MakeDatums( tb.Meta().Name.O, // Table 0, // Non_unique "PRIMARY", // Key_name 1, // Seq_in_index pkCol.Name.O, // Column_name "utf8_bin", // Colation 0, // Cardinality nil, // Sub_part nil, // Packed "", // Null "BTREE", // Index_type "", // Comment "", // Index_comment ) e.rows = append(e.rows, &Row{Data: data}) } for _, idx := range tb.Indices() { for i, col := range idx.Meta().Columns { nonUniq := 1 if idx.Meta().Unique { nonUniq = 0 } var subPart interface{} if col.Length != types.UnspecifiedLength { subPart = col.Length } data := types.MakeDatums( tb.Meta().Name.O, // Table nonUniq, // Non_unique idx.Meta().Name.O, // Key_name i+1, // Seq_in_index col.Name.O, // Column_name "utf8_bin", // Colation 0, // Cardinality subPart, // Sub_part nil, // Packed "YES", // Null idx.Meta().Tp.String(), // Index_type "", // Comment idx.Meta().Comment, // Index_comment ) e.rows = append(e.rows, &Row{Data: data}) } } return nil }
func (ts *testSuite) TestUniqueIndexMultipleNullEntries(c *C) { _, err := ts.se.Execute("CREATE TABLE test.t (a int primary key auto_increment, b varchar(255) unique)") c.Assert(err, IsNil) ctx := ts.se.(context.Context) dom := sessionctx.GetDomain(ctx) tb, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t")) c.Assert(err, IsNil) c.Assert(tb.Meta().ID, Greater, int64(0)) c.Assert(tb.Meta().Name.L, Equals, "t") c.Assert(tb.Meta(), NotNil) c.Assert(tb.Indices(), NotNil) c.Assert(string(tb.FirstKey()), Not(Equals), "") c.Assert(string(tb.IndexPrefix()), Not(Equals), "") c.Assert(string(tb.RecordPrefix()), Not(Equals), "") c.Assert(tables.FindIndexByColName(tb, "b"), NotNil) autoid, err := tb.AllocAutoID() c.Assert(err, IsNil) c.Assert(autoid, Greater, int64(0)) _, err = tb.AddRecord(ctx, types.MakeDatums(1, nil)) c.Assert(err, IsNil) _, err = tb.AddRecord(ctx, types.MakeDatums(2, nil)) c.Assert(err, IsNil) _, err = ts.se.Execute("drop table test.t") c.Assert(err, IsNil) }
func (s *testColumnChangeSuite) checkAddPublic(d *ddl, ctx context.Context, writeOnlyTable, publicTable table.Table) error { // publicTable Insert t values (4, 4, 4) h, err := publicTable.AddRecord(ctx, types.MakeDatums(4, 4, 4)) if err != nil { return errors.Trace(err) } err = ctx.CommitTxn() if err != nil { return errors.Trace(err) } // writeOnlyTable update t set c1 = 3 where c1 = 4 oldRow, err := writeOnlyTable.RowWithCols(ctx, h, writeOnlyTable.WritableCols()) if err != nil { return errors.Trace(err) } if len(oldRow) != 3 { return errors.Errorf("%v", oldRow) } newRow := types.MakeDatums(3, 4, oldRow[2].GetValue()) err = writeOnlyTable.UpdateRecord(ctx, h, oldRow, newRow, touchedMap(writeOnlyTable)) if err != nil { return errors.Trace(err) } err = ctx.CommitTxn() if err != nil { return errors.Trace(err) } // publicTable select * from t, make sure the new c3 value 4 is not overwritten to default value 3. err = checkResult(ctx, publicTable, testutil.RowsWithSep(" ", "2 3 3", "3 4 4")) if err != nil { return errors.Trace(err) } return nil }
func (s *testIndexSuite) TestCombineIndexSeek(c *C) { defer testleak.AfterTest(c)() tblInfo := &model.TableInfo{ ID: 1, Indices: []*model.IndexInfo{ { ID: 2, Name: model.NewCIStr("test"), Columns: []*model.IndexColumn{ {}, {}, }, }, }, } index := tables.NewIndex(tblInfo, tblInfo.Indices[0]) txn, err := s.s.Begin() c.Assert(err, IsNil) values := types.MakeDatums("abc", "def") _, err = index.Create(txn, values, 1) c.Assert(err, IsNil) index2 := tables.NewIndex(tblInfo, tblInfo.Indices[0]) iter, hit, err := index2.Seek(txn, types.MakeDatums("abc", nil)) c.Assert(err, IsNil) defer iter.Close() c.Assert(hit, IsFalse) _, h, err := iter.Next() c.Assert(err, IsNil) c.Assert(h, Equals, int64(1)) }
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 *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 *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 *testIndexChangeSuite) checkDropWriteOnly(d *ddl, ctx context.Context, publicTbl, writeTbl table.Table) error { // WriteOnlyTable insert t values (8, 8) _, err := writeTbl.AddRecord(ctx, types.MakeDatums(8, 8)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, publicTbl, 8, 8, true) if err != nil { return errors.Trace(err) } // WriteOnlyTable update t set c2 = 7 where c1 = 8 and c2 = 8 err = writeTbl.UpdateRecord(ctx, 8, types.MakeDatums(8, 8), types.MakeDatums(8, 7), touchedMap(writeTbl)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, publicTbl, 7, 8, true) if err != nil { return errors.Trace(err) } // WriteOnlyTable delete t where c1 = 8 err = writeTbl.RemoveRecord(ctx, 8, types.MakeDatums(8, 7)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, publicTbl, 7, 8, false) if err != nil { return errors.Trace(err) } return nil }
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 *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 *testTableSuite) TestTable(c *C) { d := s.d ctx := testNewContext(c, d) defer ctx.FinishTxn(true) tblInfo := testTableInfo(c, d, "t", 3) job := testCreateTable(c, ctx, d, s.dbInfo, tblInfo) testCheckTableState(c, d, s.dbInfo, tblInfo, model.StatePublic) testCheckJobDone(c, d, job, true) newTblInfo := testTableInfo(c, d, "t", 3) job = &model.Job{ SchemaID: s.dbInfo.ID, TableID: newTblInfo.ID, Type: model.ActionCreateTable, Args: []interface{}{newTblInfo}, } err := d.startDDLJob(ctx, job) c.Assert(err, NotNil) testCheckJobCancelled(c, d, job) tbl := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID) _, err = tbl.AddRecord(ctx, types.MakeDatums(1, 1, 1)) c.Assert(err, IsNil) _, err = tbl.AddRecord(ctx, types.MakeDatums(2, 2, 2)) c.Assert(err, IsNil) job = testDropTable(c, ctx, d, s.dbInfo, tblInfo) testCheckJobDone(c, d, job, false) }
func (s *testIndexChangeSuite) checkAddPublic(d *ddl, ctx context.Context, writeTbl, publicTbl table.Table) error { // WriteOnlyTable: insert t values (6, 6) _, err := writeTbl.AddRecord(ctx, types.MakeDatums(6, 6)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, publicTbl, 6, 6, true) if err != nil { return errors.Trace(err) } // PublicTable: insert t values (7, 7) _, err = publicTbl.AddRecord(ctx, types.MakeDatums(7, 7)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, publicTbl, 7, 7, true) if err != nil { return errors.Trace(err) } // WriteOnlyTable: update t set c2 = 5 where c1 = 7 and c2 = 7 err = writeTbl.UpdateRecord(ctx, 7, types.MakeDatums(7, 7), types.MakeDatums(7, 5), touchedMap(writeTbl)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, publicTbl, 5, 7, true) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, publicTbl, 7, 7, false) if err != nil { return errors.Trace(err) } // WriteOnlyTable: delete t where c1 = 6 err = writeTbl.RemoveRecord(ctx, 6, types.MakeDatums(6, 6)) if err != nil { return errors.Trace(err) } err = checkIndexExists(ctx, publicTbl, 6, 6, false) var rows [][]types.Datum publicTbl.IterRecords(ctx, publicTbl.FirstKey(), publicTbl.Cols(), func(h int64, data []types.Datum, cols []*table.Column) (bool, error) { rows = append(rows, data) return true, nil }) if len(rows) == 0 { return errors.New("table is empty") } for _, row := range rows { idxVal := row[1].GetInt64() handle := row[0].GetInt64() err = checkIndexExists(ctx, publicTbl, idxVal, handle, true) if err != nil { return errors.Trace(err) } } return nil }
func dataForColltions() (records [][]types.Datum) { records = append(records, types.MakeDatums("ascii_general_ci", "ascii", 1, "Yes", "Yes", 1), types.MakeDatums("binary", "binary", 2, "Yes", "Yes", 1), types.MakeDatums("latin1_swedish_ci", "latin1", 3, "Yes", "Yes", 1), types.MakeDatums("utf8_general_ci", "utf8", 4, "Yes", "Yes", 1), types.MakeDatums("utf8mb4_general_ci", "utf8mb4", 5, "Yes", "Yes", 1), ) return records }
func (s *testColumnSuite) TestCheck(c *C) { col := newCol("a") col.Flag = mysql.AutoIncrementFlag cols := []*Col{col, col} CheckOnce(cols) cols = cols[:1] CheckNotNull(cols, types.MakeDatums(nil)) cols[0].Flag |= mysql.NotNullFlag CheckNotNull(cols, types.MakeDatums(nil)) }
func dataForCharacterSets() (records [][]types.Datum) { records = append(records, types.MakeDatums("ascii", "ascii_general_ci", "US ASCII", 1), types.MakeDatums("binary", "binary", "Binary pseudo charset", 1), types.MakeDatums("latin1", "latin1_swedish_ci", "cp1252 West European", 1), types.MakeDatums("utf8", "utf8_general_ci", "UTF-8 Unicode", 3), types.MakeDatums("utf8mb4", "utf8mb4_general_ci", "UTF-8 Unicode", 4), ) return records }
func (s *testEvaluatorSuite) TestDatabase(c *C) { ctx := mock.NewContext() d, err := builtinDatabase(types.MakeDatums(), ctx) c.Assert(err, IsNil) c.Assert(d.Kind(), Equals, types.KindNull) db.BindCurrentSchema(ctx, "test") d, err = builtinDatabase(types.MakeDatums(), ctx) c.Assert(err, IsNil) c.Assert(d.GetString(), Equals, "test") }
func (s *testEvaluatorSuite) TestFoundRows(c *C) { ctx := mock.NewContext() d, err := builtinFoundRows(types.MakeDatums(), ctx) c.Assert(err, NotNil) variable.BindSessionVars(ctx) d, err = builtinFoundRows(types.MakeDatums(), ctx) c.Assert(err, IsNil) c.Assert(d.GetUint64(), Equals, uint64(0)) }
func (s *testEvaluatorSuite) TestIsNullFunc(c *C) { defer testleak.AfterTest(c)() v, err := builtinIsNull(types.MakeDatums(1), nil) c.Assert(err, IsNil) c.Assert(v.GetInt64(), Equals, int64(0)) v, err = builtinIsNull(types.MakeDatums(nil), nil) c.Assert(err, IsNil) c.Assert(v.GetInt64(), Equals, int64(1)) }
func (s *testEvaluatorSuite) TestCoalesce(c *C) { args := types.MakeDatums(1, nil) v, err := builtinCoalesce(args, nil) c.Assert(err, IsNil) c.Assert(v, DatumEquals, types.NewDatum(1)) args = types.MakeDatums(nil, nil) v, err = builtinCoalesce(args, nil) c.Assert(err, IsNil) c.Assert(v, DatumEquals, types.NewDatum(nil)) }
func (s *testEvaluatorSuite) TestCoalesce(c *C) { defer testleak.AfterTest(c)() args := types.MakeDatums(1, nil) v, err := builtinCoalesce(args, nil) c.Assert(err, IsNil) c.Assert(v, testutil.DatumEquals, types.NewDatum(1)) args = types.MakeDatums(nil, nil) v, err = builtinCoalesce(args, nil) c.Assert(err, IsNil) c.Assert(v, testutil.DatumEquals, types.NewDatum(nil)) }
func (s *testColumnChangeSuite) checkAddWriteOnly(d *ddl, ctx context.Context, deleteOnlyTable, writeOnlyTable table.Table) error { // WriteOnlyTable: insert t values (2, 3) _, err := writeOnlyTable.AddRecord(ctx, types.MakeDatums(2, 3)) if err != nil { return errors.Trace(err) } err = ctx.CommitTxn() if err != nil { return errors.Trace(err) } err = checkResult(ctx, writeOnlyTable, testutil.RowsWithSep(" ", "1 2 <nil>", "2 3 3")) if err != nil { return errors.Trace(err) } // DeleteOnlyTable: select * from t err = checkResult(ctx, deleteOnlyTable, testutil.RowsWithSep(" ", "1 2", "2 3")) if err != nil { return errors.Trace(err) } // WriteOnlyTable: update t set c1 = 2 where c1 = 1 h, _, err := writeOnlyTable.Seek(ctx, 0) if err != nil { return errors.Trace(err) } err = writeOnlyTable.UpdateRecord(ctx, h, types.MakeDatums(1, 2), types.MakeDatums(2, 2), touchedMap(writeOnlyTable)) if err != nil { return errors.Trace(err) } err = ctx.CommitTxn() if err != nil { return errors.Trace(err) } // After we update the first row, its default value is also set. err = checkResult(ctx, writeOnlyTable, testutil.RowsWithSep(" ", "2 2 3", "2 3 3")) if err != nil { return errors.Trace(err) } // DeleteOnlyTable: delete from t where c2 = 2 err = deleteOnlyTable.RemoveRecord(ctx, h, types.MakeDatums(2, 2)) if err != nil { return errors.Trace(err) } err = ctx.CommitTxn() if err != nil { return errors.Trace(err) } // After delete table has deleted the first row, check the WriteOnly table records. err = checkResult(ctx, writeOnlyTable, testutil.RowsWithSep(" ", "2 3 3")) return errors.Trace(err) }
func (s *testSuite) testTableData(c *C, tb table.Table, rs []*RecordData) { txn, err := s.store.Begin() c.Assert(err, IsNil) err = CompareTableRecord(txn, tb, rs, true) c.Assert(err, IsNil) cnt, err := GetTableRecordsCount(txn, tb, 0) c.Assert(err, IsNil) c.Assert(cnt, Equals, int64(len(rs))) records := []*RecordData{ {Handle: rs[0].Handle}, {Handle: rs[1].Handle}, } err = CompareTableRecord(txn, tb, records, false) c.Assert(err, IsNil) record := &RecordData{Handle: rs[1].Handle, Values: types.MakeDatums(int64(30))} err = CompareTableRecord(txn, tb, []*RecordData{rs[0], record}, true) c.Assert(err, NotNil) diffMsg := newDiffRetError("data", record, rs[1]) c.Assert(err.Error(), DeepEquals, diffMsg) record.Handle = 3 err = CompareTableRecord(txn, tb, []*RecordData{rs[0], record, rs[1]}, true) c.Assert(err, NotNil) diffMsg = newDiffRetError("data", record, nil) c.Assert(err.Error(), DeepEquals, diffMsg) err = CompareTableRecord(txn, tb, []*RecordData{rs[0], rs[1], record}, true) c.Assert(err, NotNil) diffMsg = newDiffRetError("data", record, nil) c.Assert(err.Error(), DeepEquals, diffMsg) err = CompareTableRecord(txn, tb, []*RecordData{rs[0]}, true) c.Assert(err, NotNil) diffMsg = newDiffRetError("data", nil, rs[1]) c.Assert(err.Error(), DeepEquals, diffMsg) err = CompareTableRecord(txn, tb, nil, true) c.Assert(err, NotNil) diffMsg = newDiffRetError("data", nil, rs[0]) c.Assert(err.Error(), DeepEquals, diffMsg) errRs := append(rs, &RecordData{Handle: int64(1), Values: types.MakeDatums(int64(3))}) err = CompareTableRecord(txn, tb, errRs, false) c.Assert(err.Error(), DeepEquals, "[inspectkv:2]handle:1 is repeated in data") }
func (s *testEvaluatorSuite) TestSysDate(c *C) { last := time.Now() v, err := builtinSysDate(types.MakeDatums(nil), nil) c.Assert(err, IsNil) n := v.GetMysqlTime() c.Assert(n.String(), GreaterEqual, last.Format(mysql.TimeFormat)) v, err = builtinSysDate(types.MakeDatums(6), nil) c.Assert(err, IsNil) n = v.GetMysqlTime() c.Assert(n.String(), GreaterEqual, last.Format(mysql.TimeFormat)) _, err = builtinSysDate(types.MakeDatums(-2), nil) c.Assert(err, NotNil) }
func (s *testEvaluatorSuite) TestLock(c *C) { defer testleak.AfterTest(c)() v, err := builtinLock(types.MakeDatums(1), s.ctx) c.Assert(err, IsNil) c.Assert(v.GetInt64(), Equals, int64(1)) v, err = builtinLock(types.MakeDatums(nil), s.ctx) c.Assert(err, IsNil) c.Assert(v.GetInt64(), Equals, int64(1)) v, err = builtinReleaseLock(types.MakeDatums(1), s.ctx) c.Assert(err, IsNil) c.Assert(v.GetInt64(), Equals, int64(1)) }
func (ts *testMemoryTableSuite) TestMemoryBasic(c *C) { ctx := ts.se.(context.Context) tb := ts.tbl c.Assert(tb.Meta(), NotNil) c.Assert(tb.Meta().ID, Greater, int64(0)) c.Assert(tb.Meta().Name.L, Equals, "t") c.Assert(tb.Indices(), IsNil) c.Assert(string(tb.FirstKey()), Not(Equals), "") c.Assert(string(tb.RecordPrefix()), Not(Equals), "") autoid, err := tb.AllocAutoID() c.Assert(err, IsNil) c.Assert(autoid, Greater, int64(0)) rid, err := tb.AddRecord(ctx, types.MakeDatums(1, "abc")) c.Assert(err, IsNil) row, err := tb.Row(ctx, rid) c.Assert(err, IsNil) c.Assert(len(row), Equals, 2) c.Assert(row[0].GetInt64(), Equals, int64(1)) _, err = tb.AddRecord(ctx, types.MakeDatums(1, "aba")) c.Assert(err, IsNil) _, err = tb.AddRecord(ctx, types.MakeDatums(2, "abc")) c.Assert(err, IsNil) tb.IterRecords(ctx, tb.FirstKey(), tb.Cols(), func(h int64, data []types.Datum, cols []*table.Column) (bool, error) { return true, nil }) // RowWithCols test vals, err := tb.RowWithCols(ctx, rid, tb.Cols()) c.Assert(err, IsNil) c.Assert(vals, HasLen, 2) c.Assert(vals[0].GetInt64(), Equals, int64(1)) cols := []*table.Column{tb.Cols()[1]} vals, err = tb.RowWithCols(ctx, rid, cols) c.Assert(err, IsNil) c.Assert(vals, HasLen, 1) c.Assert(vals[0].GetString(), Equals, "abc") c.Assert(tb.RemoveRecord(ctx, rid, types.MakeDatums(1, "cba")), IsNil) _, err = tb.AddRecord(ctx, types.MakeDatums(1, "abc")) c.Assert(err, IsNil) c.Assert(tb.Truncate(ctx), IsNil) _, err = tb.Row(ctx, rid) c.Assert(err, NotNil) }
func (e *ExplainExec) fetchRows() { visitor := &explainVisitor{id: 1} visitor.explain(e.StmtPlan) for _, entry := range visitor.entries { row := &Row{} row.Data = types.MakeDatums( entry.ID, entry.selectType, entry.table, entry.joinType, entry.key, entry.key, entry.keyLen, entry.ref, entry.rows, strings.Join(entry.extra, "; "), ) for i := range row.Data { if row.Data[i].Kind() == types.KindString && row.Data[i].GetString() == "" { row.Data[i].SetNull() } } e.rows = append(e.rows, row) } }
func dataForTables(schemas []*model.DBInfo) [][]types.Datum { rows := [][]types.Datum{} for _, schema := range schemas { for _, table := range schema.Tables { record := types.MakeDatums( catalogVal, // TABLE_CATALOG schema.Name.O, // TABLE_SCHEMA table.Name.O, // TABLE_NAME "BASE_TABLE", // TABLE_TYPE "InnoDB", // ENGINE uint64(10), // VERSION "Compact", // ROW_FORMAT uint64(0), // TABLE_ROWS uint64(0), // AVG_ROW_LENGTH uint64(16384), // DATA_LENGTH uint64(0), // MAX_DATA_LENGTH uint64(0), // INDEX_LENGTH uint64(0), // DATA_FREE nil, // AUTO_INCREMENT nil, // CREATE_TIME nil, // UPDATE_TIME nil, // CHECK_TIME "latin1_swedish_ci", // TABLE_COLLATION nil, // CHECKSUM "", // CREATE_OPTIONS "", // TABLE_COMMENT ) rows = append(rows, record) } } return rows }
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 *testColumnSuite) checkWriteOnlyColumn(c *C, ctx context.Context, d *ddl, tblInfo *model.TableInfo, handle int64, col *column.Col, row []types.Datum, columnValue interface{}, isDropped bool) { t := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID) _, err := ctx.GetTxn(true) c.Assert(err, IsNil) i := int64(0) err = t.IterRecords(ctx, t.FirstKey(), t.Cols(), func(h int64, data []types.Datum, cols []*column.Col) (bool, error) { c.Assert(data, DeepEquals, row) i++ return true, nil }) c.Assert(err, IsNil) c.Assert(i, Equals, int64(1)) s.checkColumnKVExist(c, ctx, t, handle, col, columnValue, isDropped) // Test add a new row. _, err = ctx.GetTxn(true) c.Assert(err, IsNil) newRow := types.MakeDatums(int64(11), int64(22), int64(33)) handle, err = t.AddRecord(ctx, newRow) c.Assert(err, IsNil) _, err = ctx.GetTxn(true) c.Assert(err, IsNil) rows := [][]types.Datum{row, newRow} i = int64(0) t.IterRecords(ctx, t.FirstKey(), t.Cols(), func(h int64, data []types.Datum, cols []*column.Col) (bool, error) { c.Assert(data, DeepEquals, rows[i]) i++ return true, nil }) c.Assert(i, Equals, int64(2)) s.checkColumnKVExist(c, ctx, t, handle, col, columnValue, true) // Test remove a row. _, err = ctx.GetTxn(true) c.Assert(err, IsNil) err = t.RemoveRecord(ctx, handle, newRow) c.Assert(err, IsNil) _, err = ctx.GetTxn(true) c.Assert(err, IsNil) i = int64(0) t.IterRecords(ctx, t.FirstKey(), t.Cols(), func(h int64, data []types.Datum, cols []*column.Col) (bool, error) { i++ return true, nil }) c.Assert(i, Equals, int64(1)) s.checkColumnKVExist(c, ctx, t, handle, col, columnValue, false) s.testGetColumn(c, t, col.Name.L, false) }