From 6fe13010a9c19b64e10e9539324cc88db7baeee3 Mon Sep 17 00:00:00 2001 From: jacktengg Date: Tue, 12 Nov 2024 15:06:39 +0800 Subject: [PATCH] add be UT --- be/test/vec/core/column_map_test.cpp | 155 ++++++++++++++++++++++++ be/test/vec/core/column_struct_test.cpp | 71 +++++++++++ 2 files changed, 226 insertions(+) create mode 100644 be/test/vec/core/column_map_test.cpp create mode 100644 be/test/vec/core/column_struct_test.cpp diff --git a/be/test/vec/core/column_map_test.cpp b/be/test/vec/core/column_map_test.cpp new file mode 100644 index 000000000000000..d59247dae33f033 --- /dev/null +++ b/be/test/vec/core/column_map_test.cpp @@ -0,0 +1,155 @@ +// 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 "vec/columns/column_map.h" + +#include +#include + +#include "gtest/gtest_pred_impl.h" +#include "vec/columns/column.h" +#include "vec/columns/column_string.h" +#include "vec/columns/column_vector.h" +#include "vec/core/field.h" + +namespace doris::vectorized { +TEST(ColumnMapTest, StringKeyTest) { + auto col_map_str64 = ColumnMap(ColumnString64::create(), ColumnInt64::create(), + ColumnArray::ColumnOffsets::create()); + Array k1 = {"a", "b", "c"}; + Array v1 = {1, 2, 3}; + { + Map map; + map.push_back(k1); + map.push_back(v1); + col_map_str64.insert(map); + } + Array k2 = {"aa", "bb", "cc"}; + Array v2 = {11, 22, 33}; + { + Map map; + map.push_back(k2); + map.push_back(v2); + col_map_str64.insert(map); + } + Array k3 = {"aaa", "bbb", "ccc"}; + Array v3 = {111, 222, 333}; + { + Map map; + map.push_back(k3); + map.push_back(v3); + col_map_str64.insert(map); + } + + // test insert ColumnMap, Column> into ColumnMap, Column> + auto col_map_str32 = ColumnMap(ColumnString::create(), ColumnInt64::create(), + ColumnArray::ColumnOffsets::create()); + std::vector indices; + indices.push_back(0); + indices.push_back(2); + col_map_str32.insert_indices_from(col_map_str64, indices.data(), + indices.data() + indices.size()); + EXPECT_EQ(col_map_str32.size(), 2); + + auto map = get(col_map_str32[0]); + auto k = get(map[0]); + auto v = get(map[1]); + EXPECT_EQ(k.size(), 3); + for (size_t i = 0; i < k.size(); ++i) { + EXPECT_EQ(k[i], k1[i]); + } + EXPECT_EQ(v.size(), 3); + for (size_t i = 0; i < v.size(); ++i) { + EXPECT_EQ(v[i], v1[i]); + } + + map = get(col_map_str32[1]); + k = get(map[0]); + v = get(map[1]); + EXPECT_EQ(k.size(), 3); + for (size_t i = 0; i < k.size(); ++i) { + EXPECT_EQ(k[i], k3[i]); + } + EXPECT_EQ(v.size(), 3); + for (size_t i = 0; i < v.size(); ++i) { + EXPECT_EQ(v[i], v3[i]); + } +}; + +TEST(ColumnMapTest, StringValueTest) { + auto col_map_str64 = ColumnMap(ColumnInt64::create(), ColumnString64::create(), + ColumnArray::ColumnOffsets::create()); + Array k1 = {1, 2, 3}; + Array v1 = {"a", "b", "c"}; + { + Map map; + map.push_back(k1); + map.push_back(v1); + col_map_str64.insert(map); + } + Array k2 = {11, 22, 33}; + Array v2 = {"aa", "bb", "cc"}; + { + Map map; + map.push_back(k2); + map.push_back(v2); + col_map_str64.insert(map); + } + Array k3 = {111, 222, 333}; + Array v3 = {"aaa", "bbb", "ccc"}; + { + Map map; + map.push_back(k3); + map.push_back(v3); + col_map_str64.insert(map); + } + + // test insert ColumnMap, Column> into ColumnMap, Column> + auto col_map_str32 = ColumnMap(ColumnInt64::create(), ColumnString::create(), + ColumnArray::ColumnOffsets::create()); + std::vector indices; + indices.push_back(0); + indices.push_back(2); + col_map_str32.insert_indices_from(col_map_str64, indices.data(), + indices.data() + indices.size()); + EXPECT_EQ(col_map_str32.size(), 2); + + auto map = get(col_map_str32[0]); + auto k = get(map[0]); + auto v = get(map[1]); + EXPECT_EQ(k.size(), 3); + for (size_t i = 0; i < k.size(); ++i) { + EXPECT_EQ(k[i], k1[i]); + } + EXPECT_EQ(v.size(), 3); + for (size_t i = 0; i < v.size(); ++i) { + EXPECT_EQ(v[i], v1[i]); + } + + map = get(col_map_str32[1]); + k = get(map[0]); + v = get(map[1]); + EXPECT_EQ(k.size(), 3); + for (size_t i = 0; i < k.size(); ++i) { + EXPECT_EQ(k[i], k3[i]); + } + EXPECT_EQ(v.size(), 3); + for (size_t i = 0; i < v.size(); ++i) { + EXPECT_EQ(v[i], v3[i]); + } +}; +} // namespace doris::vectorized \ No newline at end of file diff --git a/be/test/vec/core/column_struct_test.cpp b/be/test/vec/core/column_struct_test.cpp new file mode 100644 index 000000000000000..3265afe24c83d58 --- /dev/null +++ b/be/test/vec/core/column_struct_test.cpp @@ -0,0 +1,71 @@ +// 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 "vec/columns/column_struct.h" + +#include +#include + +#include + +#include "gtest/gtest_pred_impl.h" +#include "vec/columns/column.h" +#include "vec/columns/column_string.h" +#include "vec/columns/columns_number.h" +#include "vec/core/field.h" + +namespace doris::vectorized { +TEST(ColumnStructTest, StringTest) { + auto str64_column = ColumnString64::create(); + auto i32_column = ColumnInt32::create(); + + std::vector str_vals = {"aaa", "bbb", "ccc", "ddd"}; + std::vector int_vals = {111, 222, 333, 444}; + for (size_t i = 0; i < str_vals.size(); ++i) { + str64_column->insert_data(str_vals[i].data(), str_vals[i].size()); + i32_column->insert_data((const char*)(&int_vals[i]), 0); + } + + ColumnStruct str64_struct_column({str64_column->get_ptr(), i32_column->get_ptr()}); + + auto str32_column = ColumnString::create(); + auto i32_column2 = ColumnInt32::create(); + ColumnStruct str32_struct_column({str32_column->get_ptr(), i32_column2->get_ptr()}); + + std::vector indices; + indices.push_back(0); + indices.push_back(2); + indices.push_back(3); + str32_struct_column.insert_indices_from(str64_struct_column, indices.data(), + indices.data() + indices.size()); + EXPECT_EQ(str32_struct_column.size(), indices.size()); + auto t = get(str32_struct_column[0]); + EXPECT_EQ(t.size(), 2); + EXPECT_EQ(t[0], "aaa"); + EXPECT_EQ(t[1], 111); + + t = get(str32_struct_column[1]); + EXPECT_EQ(t.size(), 2); + EXPECT_EQ(t[0], "ccc"); + EXPECT_EQ(t[1], 333); + + t = get(str32_struct_column[2]); + EXPECT_EQ(t.size(), 2); + EXPECT_EQ(t[0], "ddd"); + EXPECT_EQ(t[1], 444); +}; +} // namespace doris::vectorized \ No newline at end of file