blob: 5ca6a6cca362818e1bf2f3de8a260c30e7f14474 [file] [log] [blame]
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <gtest/gtest.h>
#include "testutil/column_helper.h"
#include "vec/common/columns_hashing.h"
#include "vec/common/hash_table/hash.h"
#include "vec/common/hash_table/hash_map_context.h"
#include "vec/common/hash_table/ph_hash_map.h"
#include "vec/data_types/data_type_number.h"
namespace doris::vectorized {
template <typename HashMethodType>
void test_insert(HashMethodType& method, Columns column) {
using State = typename HashMethodType::State;
ColumnRawPtrs key_raw_columns;
for (auto column : column) {
key_raw_columns.push_back(column.get());
}
State state(key_raw_columns);
const size_t rows = key_raw_columns[0]->size();
method.init_serialized_keys(key_raw_columns, rows);
for (int i = 0; i < rows; i++) {
auto creator = [&](const auto& ctor, auto& key, auto& origin) { ctor(key, i); };
auto creator_for_null_key = [&](auto& mapped) {
throw doris::Exception(ErrorCode::INTERNAL_ERROR,
"no null key"); // NOLINT
};
method.lazy_emplace(state, i, creator, creator_for_null_key);
}
}
template <typename HashMethodType>
void test_find(HashMethodType& method, Columns column, const std::vector<int64_t>& except_result) {
using State = typename HashMethodType::State;
ColumnRawPtrs key_raw_columns;
for (auto column : column) {
key_raw_columns.push_back(column.get());
}
State state(key_raw_columns);
const size_t rows = key_raw_columns[0]->size();
method.init_serialized_keys(key_raw_columns, rows);
for (size_t i = 0; i < rows; ++i) {
auto find_result = method.find(state, i);
if (find_result.is_found()) {
EXPECT_EQ(except_result[i], find_result.get_mapped());
} else {
EXPECT_EQ(except_result[i], -1); // not found
}
}
}
TEST(HashTableMethodTest, testMethodOneNumber) {
MethodOneNumber<UInt32, PHHashMap<UInt32, IColumn::ColumnIndex, HashCRC32<UInt32>>> method;
test_insert(method, {ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5})});
test_find(method, {ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5})},
{0, 1, 2, 3, 4});
test_find(method, {ColumnHelper::create_column<DataTypeInt32>({1, 2, 7, 4, 6, 5})},
{0, 1, -1, 3, -1, 4});
}
TEST(HashTableMethodTest, testMethodFixed) {
MethodKeysFixed<PHHashMap<UInt64, IColumn::ColumnIndex, HashCRC32<UInt64>>> method(
Sizes {sizeof(int), sizeof(int)});
test_insert(method, {ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5}),
ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5})});
test_find(method,
{ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5}),
ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5})},
{0, 1, 2, 3, 4});
test_find(method,
{ColumnHelper::create_column<DataTypeInt32>({1, 2, 7, 4, 6, 5}),
ColumnHelper::create_column<DataTypeInt32>({1, 2, 7, 4, 6, 5})},
{0, 1, -1, 3, -1, 4});
}
TEST(HashTableMethodTest, testMethodSerialized) {
MethodSerialized<StringHashMap<IColumn::ColumnIndex>> method;
test_insert(method, {ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5}),
ColumnHelper::create_column<DataTypeString>({"1", "2", "3", "4", "5"})});
test_find(method,
{ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5}),
ColumnHelper::create_column<DataTypeString>({"1", "2", "3", "4", "5"})},
{0, 1, 2, 3, 4});
test_find(method,
{ColumnHelper::create_column<DataTypeInt32>({1, 2, 7, 4, 6, 5}),
ColumnHelper::create_column<DataTypeString>({"1", "2", "7", "4", "6", "5"})},
{0, 1, -1, 3, -1, 4});
}
TEST(HashTableMethodTest, testMethodStringNoCache) {
MethodStringNoCache<StringHashMap<IColumn::ColumnIndex>> method;
test_insert(method, {ColumnHelper::create_column<DataTypeString>({"1", "2", "3", "4", "5"})});
test_find(method, {ColumnHelper::create_column<DataTypeString>({"1", "2", "3", "4", "5"})},
{0, 1, 2, 3, 4});
test_find(method, {ColumnHelper::create_column<DataTypeString>({"1", "2", "7", "4", "6", "5"})},
{0, 1, -1, 3, -1, 4});
}
} // namespace doris::vectorized