Files
doris/be/test/vec/function/function_comparison_test.cpp
HappenLee e1d7233e9c [feature](vectorization) Support Vectorized Exec Engine In Doris (#7785)
# Proposed changes

Issue Number: close #6238

    Co-authored-by: HappenLee <happenlee@hotmail.com>
    Co-authored-by: stdpain <34912776+stdpain@users.noreply.github.com>
    Co-authored-by: Zhengguo Yang <yangzhgg@gmail.com>
    Co-authored-by: wangbo <506340561@qq.com>
    Co-authored-by: emmymiao87 <522274284@qq.com>
    Co-authored-by: Pxl <952130278@qq.com>
    Co-authored-by: zhangstar333 <87313068+zhangstar333@users.noreply.github.com>
    Co-authored-by: thinker <zchw100@qq.com>
    Co-authored-by: Zeno Yang <1521564989@qq.com>
    Co-authored-by: Wang Shuo <wangshuo128@gmail.com>
    Co-authored-by: zhoubintao <35688959+zbtzbtzbt@users.noreply.github.com>
    Co-authored-by: Gabriel <gabrielleebuaa@gmail.com>
    Co-authored-by: xinghuayu007 <1450306854@qq.com>
    Co-authored-by: weizuo93 <weizuo@apache.org>
    Co-authored-by: yiguolei <guoleiyi@tencent.com>
    Co-authored-by: anneji-dev <85534151+anneji-dev@users.noreply.github.com>
    Co-authored-by: awakeljw <993007281@qq.com>
    Co-authored-by: taberylyang <95272637+taberylyang@users.noreply.github.com>
    Co-authored-by: Cui Kaifeng <48012748+azurenake@users.noreply.github.com>


## Problem Summary:

### 1. Some code from clickhouse

**ClickHouse is an excellent implementation of the vectorized execution engine database,
so here we have referenced and learned a lot from its excellent implementation in terms of
data structure and function implementation.
We are based on ClickHouse v19.16.2.2 and would like to thank the ClickHouse community and developers.**

The following comment has been added to the code from Clickhouse, eg:
// This file is copied from
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Interpreters/AggregationCommon.h
// and modified by Doris

### 2. Support exec node and query:
* vaggregation_node
* vanalytic_eval_node
* vassert_num_rows_node
* vblocking_join_node
* vcross_join_node
* vempty_set_node
* ves_http_scan_node
* vexcept_node
* vexchange_node
* vintersect_node
* vmysql_scan_node
* vodbc_scan_node
* volap_scan_node
* vrepeat_node
* vschema_scan_node
* vselect_node
* vset_operation_node
* vsort_node
* vunion_node
* vhash_join_node

You can run exec engine of SSB/TPCH and 70% TPCDS stand query test set.

### 3. Data Model

Vec Exec Engine Support **Dup/Agg/Unq** table, Support Block Reader Vectorized.
Segment Vec is working in process.

### 4. How to use

1. Set the environment variable `set enable_vectorized_engine = true; `(required)
2. Set the environment variable `set batch_size = 4096; ` (recommended)

### 5. Some diff from origin exec engine

https://github.com/doris-vectorized/doris-vectorized/issues/294

## Checklist(Required)

1. Does it affect the original behavior: (No)
2. Has unit tests been added: (Yes)
3. Has document been added or modified: (No)
4. Does it need to update dependencies: (No)
5. Are there any changes that cannot be rolled back: (Yes)
2022-01-18 10:07:15 +08:00

151 lines
6.1 KiB
C++

// 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 <iostream>
#include <string>
#include "exec/schema_scanner.h"
#include "runtime/row_batch.h"
#include "runtime/tuple_row.h"
#include "vec/functions/simple_function_factory.h"
namespace doris {
TEST(ComparisonTest, ComparisonFunctionTest) {
SchemaScanner::ColumnDesc column_descs[] = {{"k1", TYPE_SMALLINT, sizeof(int16_t), false},
{"k2", TYPE_INT, sizeof(int32_t), false},
{"k3", TYPE_DOUBLE, sizeof(double), false}};
SchemaScanner schema_scanner(column_descs, 3);
ObjectPool object_pool;
SchemaScannerParam param;
schema_scanner.init(&param, &object_pool);
auto tuple_desc = const_cast<TupleDescriptor*>(schema_scanner.tuple_desc());
RowDescriptor row_desc(tuple_desc, false);
auto tracker_ptr = MemTracker::CreateTracker(-1, "BlockTest", nullptr, false);
RowBatch row_batch(row_desc, 1024, tracker_ptr.get());
int16_t k1 = -100;
int32_t k2 = 100;
double k3 = 7.7;
for (int i = 0; i < 1024; ++i, k1++, k2--, k3 += 0.1) {
auto idx = row_batch.add_row();
TupleRow* tuple_row = row_batch.get_row(idx);
auto tuple = (Tuple*)(row_batch.tuple_data_pool()->allocate(tuple_desc->byte_size()));
auto slot_desc = tuple_desc->slots()[0];
memcpy(tuple->get_slot(slot_desc->tuple_offset()), &k1, column_descs[0].size);
slot_desc = tuple_desc->slots()[1];
memcpy(tuple->get_slot(slot_desc->tuple_offset()), &k2, column_descs[1].size);
slot_desc = tuple_desc->slots()[2];
memcpy(tuple->get_slot(slot_desc->tuple_offset()), &k3, column_descs[2].size);
tuple_row->set_tuple(0, tuple);
row_batch.commit_last_row();
}
vectorized::Block block = row_batch.convert_to_vec_block();
// 1. compute the k1 > k2
vectorized::ColumnNumbers arguments;
arguments.emplace_back(block.get_position_by_name("k1"));
arguments.emplace_back(block.get_position_by_name("k2"));
size_t num_columns_without_result = block.columns();
block.insert({nullptr, std::make_shared<vectorized::DataTypeUInt8>(), "k1 > k2"});
vectorized::ColumnsWithTypeAndName ctn = {block.get_by_position(arguments[0]),
block.get_by_position(arguments[1])};
auto greater_function_ptr = vectorized::SimpleFunctionFactory::instance().get_function(
"gt", ctn, std::make_shared<vectorized::DataTypeUInt8>());
greater_function_ptr->execute(nullptr, block, arguments, num_columns_without_result, 1024,
false);
k1 = -100;
k2 = 100;
for (int i = 0; i < 1024; ++i, k1++, k2--) {
vectorized::ColumnPtr column = block.get_columns()[3];
ASSERT_EQ(column->get_bool(i), k1 > k2);
}
// 2. compute the k2 <= k3
num_columns_without_result = block.columns();
block.insert({nullptr, std::make_shared<vectorized::DataTypeUInt8>(), "k2 <= k3"});
auto less_or_equals_function_ptr = vectorized::SimpleFunctionFactory::instance().get_function(
"le", ctn, std::make_shared<vectorized::DataTypeUInt8>());
arguments[0] = 1;
arguments[1] = 2;
less_or_equals_function_ptr->execute(nullptr, block, arguments, num_columns_without_result,
1024, false);
k2 = 100;
k3 = 7.7;
for (int i = 0; i < 1024; ++i, k3 += 0.1, k2--) {
vectorized::ColumnPtr column = block.get_columns()[4];
ASSERT_EQ(column->get_bool(i), k2 <= k3);
}
num_columns_without_result = block.columns();
block.insert({nullptr, std::make_shared<vectorized::DataTypeUInt8>(), "k1 > k2 and k2 <= k3"});
arguments[0] = 3;
arguments[1] = 4;
vectorized::ColumnsWithTypeAndName ctn2 = {block.get_by_position(arguments[0]),
block.get_by_position(arguments[1])};
auto and_function_ptr = vectorized::SimpleFunctionFactory::instance().get_function(
"and", ctn2, std::make_shared<vectorized::DataTypeUInt8>());
and_function_ptr->execute(nullptr, block, arguments, num_columns_without_result, 1024, false);
k1 = -100;
k2 = 100;
k3 = 7.7;
for (int i = 0; i < 1024; ++i, k1++, k3 += 0.1, k2--) {
vectorized::ColumnPtr column = block.get_columns()[5];
ASSERT_EQ(column->get_bool(i), k1 > k2 and k2 <= k3);
}
num_columns_without_result = block.columns();
block.insert({nullptr, std::make_shared<vectorized::DataTypeUInt8>(), "k1 > k2 or k2 <= k3"});
arguments[0] = 3;
arguments[1] = 4;
// vectorized::ColumnsWithTypeAndName ctn2 = { block.get_by_position(arguments[0]), block.get_by_position(arguments[1]) };
auto or_function_ptr = vectorized::SimpleFunctionFactory::instance().get_function(
"or", ctn2, std::make_shared<vectorized::DataTypeUInt8>());
or_function_ptr->execute(nullptr, block, arguments, num_columns_without_result, 1024, false);
k1 = -100;
k2 = 100;
k3 = 7.7;
for (int i = 0; i < 1024; ++i, k1++, k3 += 0.1, k2--) {
vectorized::ColumnPtr column = block.get_columns()[6];
ASSERT_EQ(column->get_bool(i), k1 > k2 or k2 <= k3);
}
}
} // namespace doris
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}