|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "kudu/common/columnar_serialization.h" |
| 19 | + |
| 20 | +#include <cstddef> |
| 21 | +#include <cstdint> |
| 22 | +#include <ostream> |
| 23 | +#include <string> |
| 24 | +#include <utility> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include <glog/logging.h> |
| 28 | +#include <gtest/gtest.h> |
| 29 | + |
| 30 | +#include "kudu/util/bitmap.h" |
| 31 | +#include "kudu/util/faststring.h" |
| 32 | +#include "kudu/util/random.h" |
| 33 | +#include "kudu/util/scoped_cleanup.h" |
| 34 | +#include "kudu/util/test_util.h" |
| 35 | + |
| 36 | +using std::vector; |
| 37 | + |
| 38 | +namespace kudu { |
| 39 | + |
| 40 | +class ColumnarSerializationTest : public KuduTest { |
| 41 | + protected: |
| 42 | + ColumnarSerializationTest() : rng_(SeedRandom()) { |
| 43 | + } |
| 44 | + |
| 45 | + // TODO(todd): templatize this test for other types once we have specialized |
| 46 | + // implementations. |
| 47 | + using DataType = uint32_t; |
| 48 | + static constexpr int kTypeSize = sizeof(DataType); |
| 49 | + |
| 50 | + struct RandomCellsAndNulls { |
| 51 | + vector<DataType> vals; |
| 52 | + faststring non_nulls; |
| 53 | + |
| 54 | + void VerifyNullsAreZeroed() { |
| 55 | + for (int i = 0; i < vals.size(); i++) { |
| 56 | + SCOPED_TRACE(i); |
| 57 | + if (BitmapTest(non_nulls.data(), i)) { |
| 58 | + EXPECT_EQ(0xdeadbeef, vals[i]); |
| 59 | + } else { |
| 60 | + EXPECT_EQ(0, vals[i]); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + // Generate a random bitmap with the given number of bits. |
| 67 | + faststring RandomBitmap(int n_bits) { |
| 68 | + faststring bm; |
| 69 | + bm.resize(BitmapSize(n_bits)); |
| 70 | + |
| 71 | + for (int i = 0; i < n_bits; i++) { |
| 72 | + BitmapChange(bm.data(), i, rng_.OneIn(3)); |
| 73 | + } |
| 74 | + return bm; |
| 75 | + } |
| 76 | + |
| 77 | + // Create an array of 0xdeadbeef values and a corresponding |
| 78 | + // null bitmap with random entries set to null. |
| 79 | + RandomCellsAndNulls CreateDeadBeefsWithRandomNulls() { |
| 80 | + auto num_rows = rng_.Uniform(1000) + 1; |
| 81 | + vector<uint32_t> vals(num_rows, 0xdeadbeef); |
| 82 | + faststring non_nulls = RandomBitmap(num_rows); |
| 83 | + return { std::move(vals), std::move(non_nulls) }; |
| 84 | + } |
| 85 | + |
| 86 | + Random rng_; |
| 87 | +}; |
| 88 | + |
| 89 | + |
| 90 | +// Simple test of ZeroNullValues for a whole array. |
| 91 | +TEST_F(ColumnarSerializationTest, TestZeroNullValues) { |
| 92 | + auto data = CreateDeadBeefsWithRandomNulls(); |
| 93 | + |
| 94 | + internal::ZeroNullValues( |
| 95 | + kTypeSize, /* dst_idx= */0, |
| 96 | + data.vals.size(), |
| 97 | + reinterpret_cast<uint8_t*>(data.vals.data()), |
| 98 | + data.non_nulls.data()); |
| 99 | + |
| 100 | + ASSERT_NO_FATAL_FAILURE(data.VerifyNullsAreZeroed()); |
| 101 | +} |
| 102 | + |
| 103 | +// More complex test test of ZeroNullValues which runs on sub-ranges |
| 104 | +// of an array. |
| 105 | +TEST_F(ColumnarSerializationTest, TestZeroNullValuesWithOffset) { |
| 106 | + auto data = CreateDeadBeefsWithRandomNulls(); |
| 107 | + int dst_idx = 0; |
| 108 | + while (dst_idx < data.vals.size()) { |
| 109 | + auto rem = data.vals.size() - dst_idx; |
| 110 | + auto n = rng_.Uniform(rem) + 1; |
| 111 | + internal::ZeroNullValues( |
| 112 | + kTypeSize, dst_idx, n, |
| 113 | + reinterpret_cast<uint8_t*>(data.vals.data()), |
| 114 | + data.non_nulls.data()); |
| 115 | + dst_idx += n; |
| 116 | + } |
| 117 | + ASSERT_NO_FATAL_FAILURE(data.VerifyNullsAreZeroed()); |
| 118 | +} |
| 119 | + |
| 120 | +TEST_F(ColumnarSerializationTest, TestCopyNonNullBitmap) { |
| 121 | + auto save_method = internal::g_pext_method; |
| 122 | + SCOPED_CLEANUP({ internal::g_pext_method = save_method; }); |
| 123 | + // Test using all available methods. Depending on the machine where |
| 124 | + // the test is running we might miss some, but we typically run this |
| 125 | + // test on relatively recent hardware that would support BMI2 (Haswell |
| 126 | + // or later). |
| 127 | + auto available_methods = internal::GetAvailablePextMethods(); |
| 128 | + for (auto m : available_methods) { |
| 129 | + SCOPED_TRACE(static_cast<int>(m)); |
| 130 | + internal::g_pext_method = m; |
| 131 | + auto n_rows = 1 + rng_.Uniform(200); |
| 132 | + faststring non_null_bitmap = RandomBitmap(n_rows); |
| 133 | + faststring sel_bitmap = RandomBitmap(n_rows); |
| 134 | + faststring dst_bitmap; |
| 135 | + dst_bitmap.resize(BitmapSize(n_rows)); |
| 136 | + |
| 137 | + internal::CopyNonNullBitmap( |
| 138 | + non_null_bitmap.data(), sel_bitmap.data(), |
| 139 | + /*dst_idx=*/0, n_rows, |
| 140 | + dst_bitmap.data()); |
| 141 | + |
| 142 | + vector<bool> expected; |
| 143 | + ForEachSetBit(sel_bitmap.data(), n_rows, |
| 144 | + [&](size_t bit) { |
| 145 | + expected.push_back(BitmapTest(non_null_bitmap.data(), bit)); |
| 146 | + }); |
| 147 | + LOG(INFO) << "non-null: " << BitmapToString(non_null_bitmap.data(), n_rows); |
| 148 | + LOG(INFO) << "selection: " << BitmapToString(sel_bitmap.data(), n_rows); |
| 149 | + LOG(INFO) << "result: " << BitmapToString(dst_bitmap.data(), expected.size()); |
| 150 | + for (int i = 0; i < expected.size(); i++) { |
| 151 | + EXPECT_EQ(expected[i], BitmapTest(dst_bitmap.data(), i)); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +TEST_F(ColumnarSerializationTest, TestCopySelectedRows) { |
| 157 | + auto num_rows = rng_.Uniform(1000) + 1; |
| 158 | + vector<uint32_t> vals; |
| 159 | + for (int i = 0; i < num_rows; i++) { |
| 160 | + vals.push_back(rng_.Next()); |
| 161 | + } |
| 162 | + |
| 163 | + vector<uint32_t> expected; |
| 164 | + vector<uint16_t> sel_indexes; |
| 165 | + for (int i = 0; i < num_rows; i++) { |
| 166 | + if (rng_.OneIn(3)) { |
| 167 | + sel_indexes.push_back(i); |
| 168 | + expected.push_back(vals[i]); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + vector<uint32_t> ret(expected.size()); |
| 173 | + internal::CopySelectedRows(sel_indexes, kTypeSize, |
| 174 | + reinterpret_cast<const uint8_t*>(vals.data()), |
| 175 | + reinterpret_cast<uint8_t*>(ret.data())); |
| 176 | + ASSERT_EQ(expected, ret); |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace kudu |
0 commit comments