From 90c721ad72f44ff5849e131d813509d1dedbe035 Mon Sep 17 00:00:00 2001 From: Shirly Date: Thu, 19 Jul 2018 18:53:16 +0800 Subject: [PATCH] chunk/codec: adjust decoding offsets in chunk (#7106) --- util/chunk/codec.go | 2 +- util/chunk/codec_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/util/chunk/codec.go b/util/chunk/codec.go index 9f9cb07eebf99..a9a3df7587194 100644 --- a/util/chunk/codec.go +++ b/util/chunk/codec.go @@ -128,7 +128,7 @@ func (c *Codec) decodeColumn(buffer []byte, col *column, ordinal int) (remained numDataBytes := numFixedBytes * col.length if numFixedBytes == -1 { numOffsetBytes := (col.length + 1) * 4 - col.offsets = append(col.offsets[:0], c.bytesToI32Slice(buffer)...) + col.offsets = append(col.offsets[:0], c.bytesToI32Slice(buffer[:numOffsetBytes])...) buffer = buffer[numOffsetBytes:] numDataBytes = int(col.offsets[col.length]) } else if cap(col.elemBuf) < numFixedBytes { diff --git a/util/chunk/codec_test.go b/util/chunk/codec_test.go index dfd237a1bd1d5..99aa4027647d1 100644 --- a/util/chunk/codec_test.go +++ b/util/chunk/codec_test.go @@ -154,3 +154,36 @@ func BenchmarkDecodeToChunk(b *testing.B) { codec.DecodeToChunk(buffer, chk) } } + +func BenchmarkDecodeToChunkWithVariableType(b *testing.B) { + numCols := 6 + numRows := 1024 + + colTypes := make([]*types.FieldType, 0, numCols) + colTypes = append(colTypes, &types.FieldType{Tp: mysql.TypeLonglong}) + colTypes = append(colTypes, &types.FieldType{Tp: mysql.TypeLonglong}) + colTypes = append(colTypes, &types.FieldType{Tp: mysql.TypeVarchar}) + colTypes = append(colTypes, &types.FieldType{Tp: mysql.TypeVarchar}) + colTypes = append(colTypes, &types.FieldType{Tp: mysql.TypeNewDecimal}) + colTypes = append(colTypes, &types.FieldType{Tp: mysql.TypeJSON}) + + chk := NewChunkWithCapacity(colTypes, numRows) + for i := 0; i < numRows; i++ { + str := fmt.Sprintf("%d.12345", i) + chk.AppendNull(0) + chk.AppendInt64(1, int64(i)) + chk.AppendString(2, str) + chk.AppendString(3, str) + chk.AppendMyDecimal(4, types.NewDecFromStringForTest(str)) + chk.AppendJSON(5, json.CreateBinary(str)) + } + codec := &Codec{colTypes} + buffer := codec.Encode(chk) + + chk.Reset() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + codec.DecodeToChunk(buffer, chk) + } +}