github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/chunk/chunk_util_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package chunk 15 16 import ( 17 "math/rand" 18 "reflect" 19 "testing" 20 21 "github.com/whtcorpsinc/milevadb/types" 22 ) 23 24 // getChk generate a chunk of data, isLast3DefCausTheSame means the last three defCausumns are the same. 25 func getChk(isLast3DefCausTheSame bool) (*Chunk, *Chunk, []bool) { 26 numRows := 1024 27 srcChk := newChunkWithInitCap(numRows, 0, 0, 8, 8, sizeTime, 0) 28 selected := make([]bool, numRows) 29 var event Row 30 for j := 0; j < numRows; j++ { 31 if isLast3DefCausTheSame { 32 if j%7 == 0 { 33 event = MutRowFromValues("abc", "abcdefg", nil, 123, types.ZeroDatetime, "abcdefg").ToRow() 34 } else { 35 event = MutRowFromValues("abc", "abcdefg", j, 123, types.ZeroDatetime, "abcdefg").ToRow() 36 } 37 } else { 38 if j%7 == 0 { 39 event = MutRowFromValues("abc", "abcdefg", nil, rand.Int(), types.ZeroDatetime, "abcdefg").ToRow() 40 } else { 41 event = MutRowFromValues("aabc", "ab234fg", j, 123, types.ZeroDatetime, "abcdefg").ToRow() 42 } 43 } 44 if j%7 != 0 { 45 selected[j] = true 46 } 47 srcChk.AppendPartialRow(0, event) 48 } 49 dstChk := newChunkWithInitCap(numRows, 0, 0, 8, 8, sizeTime, 0) 50 return srcChk, dstChk, selected 51 } 52 53 func TestCopySelectedJoinRows(t *testing.T) { 54 srcChk, dstChk, selected := getChk(true) 55 numRows := srcChk.NumRows() 56 for i := 0; i < numRows; i++ { 57 if !selected[i] { 58 continue 59 } 60 dstChk.AppendRow(srcChk.GetRow(i)) 61 } 62 // batch copy 63 dstChk2 := newChunkWithInitCap(numRows, 0, 0, 8, 8, sizeTime, 0) 64 CopySelectedJoinRowsWithSameOuterRows(srcChk, 0, 3, 3, 3, selected, dstChk2) 65 66 if !reflect.DeepEqual(dstChk, dstChk2) { 67 t.Fatal() 68 } 69 numSelected := 0 70 for i := range selected { 71 if selected[i] { 72 numSelected++ 73 } 74 } 75 if dstChk2.numVirtualRows != numSelected || dstChk2.NumRows() != numSelected { 76 t.Fatal(dstChk2.numVirtualRows, dstChk2.NumRows(), numSelected) 77 } 78 } 79 80 func TestCopySelectedJoinRowsWithoutSameOuters(t *testing.T) { 81 srcChk, dstChk, selected := getChk(false) 82 numRows := srcChk.NumRows() 83 for i := 0; i < numRows; i++ { 84 if !selected[i] { 85 continue 86 } 87 dstChk.AppendRow(srcChk.GetRow(i)) 88 } 89 // batch copy 90 dstChk2 := newChunkWithInitCap(numRows, 0, 0, 8, 8, sizeTime, 0) 91 CopySelectedJoinRowsWithSameOuterRows(srcChk, 0, 6, 0, 0, selected, dstChk2) 92 93 if !reflect.DeepEqual(dstChk, dstChk2) { 94 t.Fatal() 95 } 96 numSelected := 0 97 for i := range selected { 98 if selected[i] { 99 numSelected++ 100 } 101 } 102 if dstChk2.numVirtualRows != numSelected || dstChk2.NumRows() != numSelected { 103 t.Fatal(dstChk2.numVirtualRows, dstChk2.NumRows(), numSelected) 104 } 105 } 106 107 func TestCopySelectedJoinRowsDirect(t *testing.T) { 108 srcChk, dstChk, selected := getChk(false) 109 numRows := srcChk.NumRows() 110 for i := 0; i < numRows; i++ { 111 if !selected[i] { 112 continue 113 } 114 dstChk.AppendRow(srcChk.GetRow(i)) 115 } 116 // batch copy 117 dstChk2 := newChunkWithInitCap(numRows, 0, 0, 8, 8, sizeTime, 0) 118 CopySelectedJoinRowsDirect(srcChk, selected, dstChk2) 119 120 if !reflect.DeepEqual(dstChk, dstChk2) { 121 t.Fatal() 122 } 123 numSelected := 0 124 for i := range selected { 125 if selected[i] { 126 numSelected++ 127 } 128 } 129 if dstChk2.numVirtualRows != numSelected || dstChk2.NumRows() != numSelected { 130 t.Fatal(dstChk2.numVirtualRows, dstChk2.NumRows(), numSelected) 131 } 132 } 133 134 func TestCopySelectedVirtualNum(t *testing.T) { 135 // srcChk does not contain defCausumns 136 srcChk := newChunk() 137 srcChk.TruncateTo(3) 138 dstChk := newChunk() 139 selected := []bool{true, false, true} 140 ok, err := CopySelectedJoinRowsDirect(srcChk, selected, dstChk) 141 if err != nil || !ok { 142 t.Fatal(ok, err) 143 } 144 if dstChk.numVirtualRows != 2 { 145 t.Fatal(dstChk.numVirtualRows) 146 } 147 148 dstChk = newChunk() 149 ok, err = CopySelectedJoinRowsWithSameOuterRows(srcChk, 0, 0, 0, 0, selected, dstChk) 150 if err != nil || !ok { 151 t.Fatal(ok, err) 152 } 153 if dstChk.numVirtualRows != 2 { 154 t.Fatal(dstChk.numVirtualRows) 155 } 156 157 srcChk = newChunk(8) 158 srcChk.TruncateTo(0) 159 srcChk.AppendInt64(0, 0) 160 srcChk.AppendInt64(0, 1) 161 srcChk.AppendInt64(0, 2) 162 dstChk = newChunkWithInitCap(0, 8) 163 ok, err = CopySelectedJoinRowsWithSameOuterRows(srcChk, 0, 1, 1, 0, selected, dstChk) 164 if err != nil || !ok { 165 t.Fatal(ok, err) 166 } 167 if dstChk.numVirtualRows != 2 || dstChk.NumRows() != 2 { 168 t.Fatal(dstChk.numVirtualRows, dstChk.NumRows()) 169 } 170 if row0, row1 := dstChk.GetRow(0).GetInt64(0), dstChk.GetRow(1).GetInt64(0); row0 != 0 || row1 != 2 { 171 t.Fatal(row0, row1) 172 } 173 174 srcChk = newChunk(8) 175 srcChk.TruncateTo(0) 176 srcChk.AppendInt64(0, 3) 177 srcChk.AppendInt64(0, 3) 178 srcChk.AppendInt64(0, 3) 179 dstChk = newChunkWithInitCap(0, 8) 180 ok, err = CopySelectedJoinRowsWithSameOuterRows(srcChk, 1, 0, 0, 1, selected, dstChk) 181 if err != nil || !ok { 182 t.Fatal(ok, err) 183 } 184 if dstChk.numVirtualRows != 2 || dstChk.NumRows() != 2 { 185 t.Fatal(dstChk.numVirtualRows, dstChk.NumRows()) 186 } 187 if row0, row1 := dstChk.GetRow(0).GetInt64(0), dstChk.GetRow(1).GetInt64(0); row0 != 3 || row1 != 3 { 188 t.Fatal(row0, row1) 189 } 190 } 191 192 func BenchmarkCopySelectedJoinRows(b *testing.B) { 193 b.ReportAllocs() 194 srcChk, dstChk, selected := getChk(true) 195 b.ResetTimer() 196 for i := 0; i < b.N; i++ { 197 dstChk.Reset() 198 CopySelectedJoinRowsWithSameOuterRows(srcChk, 0, 3, 3, 3, selected, dstChk) 199 } 200 } 201 func BenchmarkCopySelectedJoinRowsDirect(b *testing.B) { 202 b.ReportAllocs() 203 srcChk, dstChk, selected := getChk(false) 204 b.ResetTimer() 205 for i := 0; i < b.N; i++ { 206 dstChk.Reset() 207 CopySelectedJoinRowsDirect(srcChk, selected, dstChk) 208 } 209 } 210 func BenchmarkAppendSelectedRow(b *testing.B) { 211 b.ReportAllocs() 212 srcChk, dstChk, selected := getChk(true) 213 numRows := srcChk.NumRows() 214 b.ResetTimer() 215 for i := 0; i < b.N; i++ { 216 dstChk.Reset() 217 for j := 0; j < numRows; j++ { 218 if !selected[j] { 219 continue 220 } 221 dstChk.AppendRow(srcChk.GetRow(j)) 222 } 223 } 224 }