github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/c-deps/libroach/merge_test.cc (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 #include <gtest/gtest.h> 12 #include <utility> 13 #include <vector> 14 #include "merge.h" 15 #include "protos/roachpb/internal.pb.h" 16 17 using namespace cockroach; 18 19 void testAddColumns(roachpb::InternalTimeSeriesData* data, int offset, int value) { 20 data->add_offset(offset); 21 data->add_count(value + 1); 22 data->add_last(value + 2); 23 data->add_first(value + 3); 24 data->add_min(value + 4); 25 data->add_max(value + 5); 26 data->add_sum(value + 6); 27 data->add_variance(value + 7); 28 } 29 30 void testAddColumnsNoRollup(roachpb::InternalTimeSeriesData* data, int offset, int value) { 31 data->add_offset(offset); 32 data->add_last(value); 33 } 34 35 void testAddRows(roachpb::InternalTimeSeriesData* data, int offset, int value) { 36 auto row = data->add_samples(); 37 row->set_offset(offset); 38 row->set_sum(value); 39 row->set_count(1); 40 row->set_max(value + 1); 41 row->set_min(value + 2); 42 } 43 44 TEST(TimeSeriesMerge, SortAndDeduplicate) { 45 struct TestCase { 46 using rowData = std::vector<std::pair<int, int>>; 47 rowData originalRows; 48 int firstUnsorted; 49 rowData expectedRows; 50 TestCase(rowData orig, int first, rowData expected) 51 : originalRows(orig), firstUnsorted(first), expectedRows(expected){}; 52 }; 53 54 std::vector<TestCase> testCases = { 55 // Basic sorting and deduplication. 56 TestCase( 57 { 58 {10, 999}, 59 {8, 1}, 60 {9, 2}, 61 {10, 0}, 62 {4, 0}, 63 {10, 3}, 64 }, 65 0, 66 { 67 {4, 0}, 68 {8, 1}, 69 {9, 2}, 70 {10, 3}, 71 }), 72 // Partial sorting and deduplication. The first index is intentionally out 73 // of order in order strongly demonstrate that only a suffix of the array 74 // is being sorted, anything before firstUnsorted is not modified. 75 TestCase( 76 { 77 {10, 999}, 78 {8, 1}, 79 {9, 2}, 80 {10, 0}, 81 {4, 0}, 82 {10, 3}, 83 }, 84 3, 85 { 86 {10, 999}, 87 {8, 1}, 88 {9, 2}, 89 {4, 0}, 90 {10, 3}, 91 }), 92 // Sort only last sample (common case). 93 TestCase( 94 { 95 {1, 1}, 96 {2, 2}, 97 {3, 3}, 98 {4, 4}, 99 {5, 5}, 100 }, 101 4, 102 { 103 {1, 1}, 104 {2, 2}, 105 {3, 3}, 106 {4, 4}, 107 {5, 5}, 108 }), 109 // Already sorted. 110 TestCase( 111 { 112 {1, 1}, 113 {2, 2}, 114 {3, 3}, 115 {4, 4}, 116 {5, 5}, 117 }, 118 0, 119 { 120 {1, 1}, 121 {2, 2}, 122 {3, 3}, 123 {4, 4}, 124 {5, 5}, 125 }), 126 // Single element shifted forward. 127 TestCase( 128 { 129 {5, 5}, 130 {1, 1}, 131 {2, 2}, 132 {3, 3}, 133 {4, 4}, 134 }, 135 0, 136 { 137 {1, 1}, 138 {2, 2}, 139 {3, 3}, 140 {4, 4}, 141 {5, 5}, 142 }), 143 // Reversed. 144 TestCase( 145 { 146 {5, 5}, 147 {4, 4}, 148 {3, 3}, 149 {2, 2}, 150 {1, 1}, 151 }, 152 0, 153 { 154 {1, 1}, 155 {2, 2}, 156 {3, 3}, 157 {4, 4}, 158 {5, 5}, 159 }), 160 // Element shift with duplicate. 161 TestCase( 162 { 163 {5, 999}, 164 {1, 1}, 165 {2, 2}, 166 {5, 5}, 167 {3, 3}, 168 {4, 4}, 169 }, 170 0, 171 { 172 {1, 1}, 173 {2, 2}, 174 {3, 3}, 175 {4, 4}, 176 {5, 5}, 177 }), 178 // Shift with firstUnsorted. 179 TestCase( 180 { 181 {99, 999}, 182 {88, 888}, 183 {77, 777}, 184 {5, 5}, 185 {1, 1}, 186 {2, 2}, 187 {3, 3}, 188 {4, 4}, 189 }, 190 3, 191 { 192 {99, 999}, 193 {88, 888}, 194 {77, 777}, 195 {1, 1}, 196 {2, 2}, 197 {3, 3}, 198 {4, 4}, 199 {5, 5}, 200 })}; 201 for (auto testCase : testCases) { 202 roachpb::InternalTimeSeriesData orig; 203 for (auto row : testCase.originalRows) { 204 testAddColumns(&orig, row.first, row.second); 205 } 206 roachpb::InternalTimeSeriesData expected; 207 for (auto row : testCase.expectedRows) { 208 testAddColumns(&expected, row.first, row.second); 209 } 210 211 sortAndDeduplicateColumns(&orig, testCase.firstUnsorted); 212 EXPECT_EQ(orig.SerializeAsString(), expected.SerializeAsString()); 213 } 214 } 215 216 TEST(TimeSeriesMerge, ConvertToColumnar) { 217 roachpb::InternalTimeSeriesData orig; 218 testAddRows(&orig, 1, 2); 219 testAddRows(&orig, 2, 4); 220 testAddRows(&orig, 3, 4); 221 222 roachpb::InternalTimeSeriesData expected; 223 testAddColumnsNoRollup(&expected, 1, 2); 224 testAddColumnsNoRollup(&expected, 2, 4); 225 testAddColumnsNoRollup(&expected, 3, 4); 226 227 EXPECT_NE(orig.SerializeAsString(), expected.SerializeAsString()); 228 convertToColumnar(&orig); 229 EXPECT_EQ(orig.SerializeAsString(), expected.SerializeAsString()); 230 }