Skip to content

Commit

Permalink
executor: set column data to zero value when calling ResizeXXX (#13260
Browse files Browse the repository at this point in the history
)
  • Loading branch information
qw4990 authored and sre-bot committed Nov 9, 2019
1 parent 3987164 commit 03e1510
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions util/chunk/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ const (
sizeTime = int(unsafe.Sizeof(types.Time{}))
)

var (
emptyBuf = make([]byte, 4*1024)
)

// resize resizes the column so that it contains n elements, only valid for fixed-length types.
func (c *Column) resize(n, typeSize int, isNull bool) {
sizeData := n * typeSize
Expand All @@ -251,6 +255,11 @@ func (c *Column) resize(n, typeSize int, isNull bool) {
} else {
c.data = make([]byte, sizeData)
}
if !isNull {
for j := 0; j < sizeData; j += len(emptyBuf) {
copy(c.data[j:], emptyBuf)
}
}

newNulls := false
sizeNulls := (n + 7) >> 3
Expand Down
60 changes: 60 additions & 0 deletions util/chunk/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,66 @@ func (s *testChunkSuite) TestGetRaw(c *check.C) {
}
}

func (s *testChunkSuite) TestResize(c *check.C) {
col := NewColumn(types.NewFieldType(mysql.TypeLonglong), 1024)
for i := 0; i < 1024; i++ {
col.AppendInt64(int64(i))
}
col.ResizeInt64(1024, false)
for i := 0; i < 1024; i++ {
c.Assert(col.Int64s()[i], check.Equals, int64(0))
}

col = NewColumn(types.NewFieldType(mysql.TypeFloat), 1024)
for i := 0; i < 1024; i++ {
col.AppendFloat32(float32(i))
}
col.ResizeFloat32(1024, false)
for i := 0; i < 1024; i++ {
c.Assert(col.Float32s()[i], check.Equals, float32(0))
}

col = NewColumn(types.NewFieldType(mysql.TypeDouble), 1024)
for i := 0; i < 1024; i++ {
col.AppendFloat64(float64(i))
}
col.ResizeFloat64(1024, false)
for i := 0; i < 1024; i++ {
c.Assert(col.Float64s()[i], check.Equals, float64(0))
}

col = NewColumn(types.NewFieldType(mysql.TypeNewDecimal), 1024)
for i := 0; i < 1024; i++ {
col.AppendMyDecimal(new(types.MyDecimal).FromInt(int64(i)))
}
col.ResizeDecimal(1024, false)
for i := 0; i < 1024; i++ {
var d types.MyDecimal
c.Assert(col.Decimals()[i], check.Equals, d)
}

col = NewColumn(types.NewFieldType(mysql.TypeDuration), 1024)
for i := 0; i < 1024; i++ {
col.AppendDuration(types.Duration{Duration: time.Duration(i), Fsp: int8(i)})
}
col.ResizeGoDuration(1024, false)
for i := 0; i < 1024; i++ {
c.Assert(col.GoDurations()[i], check.Equals, time.Duration(0))
}

col = NewColumn(types.NewFieldType(mysql.TypeDatetime), 1024)
for i := 0; i < 1024; i++ {
gt := types.FromDate(rand.Intn(2200), rand.Intn(10)+1, rand.Intn(20)+1, rand.Intn(12), rand.Intn(60), rand.Intn(60), rand.Intn(1000000))
t := types.Time{Time: gt}
col.AppendTime(t)
}
col.ResizeTime(1024, false)
for i := 0; i < 1024; i++ {
var t types.Time
c.Assert(col.Times()[i], check.Equals, t)
}
}

func BenchmarkDurationRow(b *testing.B) {
chk1 := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(mysql.TypeDuration)}, 1024)
col1 := chk1.Column(0)
Expand Down

0 comments on commit 03e1510

Please sign in to comment.