Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions be/test/exec/hash_map/hash_table_method_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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, ColumnPtrs 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, ColumnPtrs 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
39 changes: 39 additions & 0 deletions be/test/testutil/column_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 "column_helper.h"

#include <gtest/gtest.h>

#include "vec/data_types/data_type_number.h"

namespace doris::vectorized {

TEST(ColumnHelperTest, test) {
EXPECT_TRUE(ColumnHelper::column_equal(
ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5}),
ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5})));

EXPECT_FALSE(ColumnHelper::column_equal(
ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5}),
ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5, 6})));

EXPECT_FALSE(ColumnHelper::column_equal(
ColumnHelper::create_column<DataTypeInt32>({1, 1, 3, 4, 5}),
ColumnHelper::create_column<DataTypeInt32>({1, 2, 3, 4, 5})));
}

} // namespace doris::vectorized
55 changes: 55 additions & 0 deletions be/test/testutil/column_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.

#pragma once

#include <type_traits>
#include <vector>

#include "vec/data_types/data_type_string.h"

namespace doris::vectorized {
struct ColumnHelper {
public:
template <typename DataType>
static ColumnPtr create_column(const std::vector<typename DataType::FieldType>& datas) {
auto column = DataType::ColumnType::create();
if constexpr (std::is_same_v<DataTypeString, DataType>) {
for (const auto& data : datas) {
column->insert_data(data.data(), data.size());
}
} else {
for (const auto& data : datas) {
column->insert_value(data);
}
}
return std::move(column);
}

static bool column_equal(const ColumnPtr& column1, const ColumnPtr& column2) {
if (column1->size() != column2->size()) {
return false;
}
for (size_t i = 0; i < column1->size(); i++) {
if (column1->compare_at(i, i, *column2, 1) != 0) {
return false;
}
}
return true;
}
};
} // namespace doris::vectorized
Loading