MCPcopy
hub / github.com/pingcap/tidb / TestRowCodec

Function TestRowCodec

pkg/tablecodec/tablecodec_test.go:74–168  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

72}
73
74func TestRowCodec(t *testing.T) {
75 c1 := &column{id: 1, tp: types.NewFieldType(mysql.TypeLonglong)}
76 c2 := &column{id: 2, tp: types.NewFieldType(mysql.TypeVarchar)}
77 c3 := &column{id: 3, tp: types.NewFieldType(mysql.TypeNewDecimal)}
78 c4tp := &types.FieldType{}
79 c4tp.SetType(mysql.TypeEnum)
80 c4tp.SetElems([]string{"a"})
81 c4 := &column{id: 4, tp: c4tp}
82 c5tp := &types.FieldType{}
83 c5tp.SetType(mysql.TypeSet)
84 c5tp.SetElems([]string{"a"})
85 c5 := &column{id: 5, tp: c5tp}
86 c6tp := &types.FieldType{}
87 c6tp.SetType(mysql.TypeBit)
88 c6tp.SetFlen(8)
89 c6 := &column{id: 6, tp: c6tp}
90 cols := []*column{c1, c2, c3, c4, c5, c6}
91
92 row := make([]types.Datum, 6)
93 row[0] = types.NewIntDatum(100)
94 row[1] = types.NewBytesDatum([]byte("abc"))
95 row[2] = types.NewDecimalDatum(types.NewDecFromInt(1))
96 row[3] = types.NewMysqlEnumDatum(types.Enum{Name: "a", Value: 1})
97 row[4] = types.NewDatum(types.Set{Name: "a", Value: 1})
98 row[5] = types.NewDatum(types.BinaryLiteral{100})
99 // Encode
100 colIDs := make([]int64, 0, len(row))
101 for _, col := range cols {
102 colIDs = append(colIDs, col.id)
103 }
104 rd := rowcodec.Encoder{Enable: true}
105 sc := stmtctx.NewStmtCtxWithTimeZone(time.Local)
106 bs, err := EncodeRow(sc.TimeZone(), row, colIDs, nil, nil, nil, &rd)
107 require.NoError(t, err)
108 require.NotNil(t, bs)
109
110 // Decode
111 colMap := make(map[int64]*types.FieldType, len(row))
112 for _, col := range cols {
113 colMap[col.id] = col.tp
114 }
115 r, err := DecodeRowToDatumMap(bs, colMap, time.UTC)
116 require.NoError(t, err)
117 require.NotNil(t, r)
118 require.Len(t, r, len(row))
119 // Compare decoded row and original row
120 for i, col := range cols {
121 v, ok := r[col.id]
122 require.True(t, ok)
123 equal, err1 := v.Compare(sc.TypeCtx(), &row[i], collate.GetBinaryCollator())
124 require.NoError(t, err1)
125 require.Equalf(t, 0, equal, "expect: %v, got %v", row[i], v)
126 }
127
128 // colMap may contains more columns than encoded row.
129 // colMap[4] = types.NewFieldType(mysql.TypeFloat)
130 r, err = DecodeRowToDatumMap(bs, colMap, time.UTC)
131 require.NoError(t, err)

Callers

nothing calls this directly

Calls 15

SetTypeMethod · 0.95
SetElemsMethod · 0.95
SetFlenMethod · 0.95
NewFieldTypeFunction · 0.92
NewIntDatumFunction · 0.92
NewBytesDatumFunction · 0.92
NewDecimalDatumFunction · 0.92
NewDecFromIntFunction · 0.92
NewMysqlEnumDatumFunction · 0.92
NewDatumFunction · 0.92
NewStmtCtxWithTimeZoneFunction · 0.92
GetBinaryCollatorFunction · 0.92

Tested by

no test coverage detected