Files
doris/be/src/vec/aggregate_functions/aggregate_function_stddev.h
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

286 lines
8.9 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.
#pragma once
#include "common/status.h"
#include "vec/aggregate_functions/aggregate_function.h"
#include "vec/columns/columns_number.h"
#include "vec/data_types/data_type_decimal.h"
#include "vec/data_types/data_type_number.h"
#include "vec/io/io_helper.h"
namespace doris::vectorized {
template <typename T, bool is_stddev>
struct BaseData {
BaseData() : mean(0.0), m2(0.0), count(0) {}
void write(BufferWritable& buf) const {
write_binary(mean, buf);
write_binary(m2, buf);
write_binary(count, buf);
}
void read(BufferReadable& buf) {
read_binary(mean, buf);
read_binary(m2, buf);
read_binary(count, buf);
}
void reset() {
mean = 0.0;
m2 = 0.0;
count = 0;
}
double get_result(double res) const {
if constexpr (is_stddev) {
return std::sqrt(res);
} else {
return res;
}
}
double get_pop_result() const {
if (count == 1) {
return 0.0;
}
double res = m2 / count;
return get_result(res);
}
double get_samp_result() const {
double res = m2 / (count - 1);
return get_result(res);
}
static const DataTypePtr get_return_type() {
return make_nullable(std::make_shared<DataTypeNumber<Float64>>());
}
void merge(const BaseData& rhs) {
if (rhs.count == 0) {
return;
}
double delta = mean - rhs.mean;
double sum_count = count + rhs.count;
mean = rhs.mean + delta * count / sum_count;
m2 = rhs.m2 + m2 + (delta * delta) * rhs.count * count / sum_count;
count = sum_count;
}
void add(const IColumn** columns, size_t row_num) {
const auto& sources = static_cast<const ColumnVector<T>&>(*columns[0]);
double source_data = sources.get_data()[row_num];
double delta = source_data - mean;
double r = delta / (1 + count);
mean += r;
m2 += count * delta * r;
count += 1;
}
double mean;
double m2;
int64_t count;
};
template <bool is_stddev>
struct BaseDatadecimal {
BaseDatadecimal() : mean(0), m2(0), count(0) {}
void write(BufferWritable& buf) const {
write_binary(mean, buf);
write_binary(m2, buf);
write_binary(count, buf);
}
void read(BufferReadable& buf) {
read_binary(mean, buf);
read_binary(m2, buf);
read_binary(count, buf);
}
void reset() {
mean = DecimalV2Value();
m2 = DecimalV2Value();
count = {};
}
DecimalV2Value get_result(DecimalV2Value res) const {
if constexpr (is_stddev) {
return DecimalV2Value::sqrt(res);
} else {
return res;
}
}
DecimalV2Value get_pop_result() const {
DecimalV2Value new_count = DecimalV2Value();
if (count == 1) {
return new_count;
}
DecimalV2Value res = m2 / new_count.assign_from_double(count);
return get_result(res);
}
DecimalV2Value get_samp_result() const {
DecimalV2Value new_count = DecimalV2Value();
DecimalV2Value res = m2 / new_count.assign_from_double(count - 1);
return get_result(res);
}
static const DataTypePtr get_return_type() {
return make_nullable(std::make_shared<DataTypeDecimal<Decimal128>>(27, 9));
}
void merge(const BaseDatadecimal& rhs) {
if (rhs.count == 0) {
return;
}
DecimalV2Value new_count = DecimalV2Value();
new_count.assign_from_double(count);
DecimalV2Value rhs_count = DecimalV2Value();
rhs_count.assign_from_double(rhs.count);
DecimalV2Value delta = mean - rhs.mean;
DecimalV2Value sum_count = new_count + rhs_count;
mean = rhs.mean + delta * (new_count / sum_count);
m2 = rhs.m2 + m2 + (delta * delta) * (rhs_count * new_count / sum_count);
count += rhs.count;
}
void add(const IColumn** columns, size_t row_num) {
DecimalV2Value source_data = DecimalV2Value();
const auto& sources = static_cast<const ColumnDecimal<Decimal128>&>(*columns[0]);
source_data = (DecimalV2Value)sources.get_data()[row_num];
DecimalV2Value new_count = DecimalV2Value();
new_count.assign_from_double(count);
DecimalV2Value increase_count = DecimalV2Value();
increase_count.assign_from_double(1 + count);
DecimalV2Value delta = source_data - mean;
DecimalV2Value r = delta / increase_count;
mean += r;
m2 += new_count * delta * r;
count += 1;
}
DecimalV2Value mean;
DecimalV2Value m2;
int64_t count;
};
template <typename T, typename Data>
struct PopData : Data {
using ColVecResult = std::conditional_t<IsDecimalNumber<T>, ColumnDecimal<Decimal128>, ColumnVector<Float64>>;
void insert_result_into(IColumn& to) const {
ColumnNullable& nullable_column = assert_cast<ColumnNullable&>(to);
auto& col = static_cast<ColVecResult&>(nullable_column.get_nested_column());
if constexpr (IsDecimalNumber<T>) {
col.get_data().push_back(this->get_pop_result().value());
} else {
col.get_data().push_back(this->get_pop_result());
}
nullable_column.get_null_map_data().push_back(0);
}
};
template <typename T, typename Data>
struct SampData : Data {
using ColVecResult = std::conditional_t<IsDecimalNumber<T>, ColumnDecimal<Decimal128>, ColumnVector<Float64>>;
void insert_result_into(IColumn& to) const {
ColumnNullable& nullable_column = assert_cast<ColumnNullable&>(to);
if (this->count == 1) {
nullable_column.insert_default();
} else {
auto& col = static_cast<ColVecResult&>(nullable_column.get_nested_column());
if constexpr (IsDecimalNumber<T>) {
col.get_data().push_back(this->get_samp_result().value());
} else {
col.get_data().push_back(this->get_samp_result());
}
nullable_column.get_null_map_data().push_back(0);
}
}
};
template <typename Data>
struct StddevData : Data {
static const char* name() { return "stddev"; }
};
template <typename Data>
struct VarianceData : Data {
static const char* name() { return "variance"; }
};
template <typename Data>
struct VarianceSampData : Data {
static const char* name() { return "variance_samp"; }
};
template <typename Data>
struct StddevSampData : Data {
static const char* name() { return "stddev_samp"; }
};
template <typename Data>
class AggregateFunctionStddevSamp final
: public IAggregateFunctionDataHelper<Data, AggregateFunctionStddevSamp<Data>> {
public:
AggregateFunctionStddevSamp(const DataTypes& argument_types_)
: IAggregateFunctionDataHelper<Data, AggregateFunctionStddevSamp<Data>>(argument_types_,
{}) {}
String get_name() const override { return Data::name(); }
bool insert_to_null_default() const override { return false; }
DataTypePtr get_return_type() const override { return Data::get_return_type(); }
void add(AggregateDataPtr __restrict place, const IColumn** columns, size_t row_num,
Arena*) const override {
this->data(place).add(columns, row_num);
}
void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
Arena*) const override {
this->data(place).merge(this->data(rhs));
}
void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
this->data(place).write(buf);
}
void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
Arena*) const override {
this->data(place).read(buf);
}
void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
this->data(place).insert_result_into(to);
}
const char* get_header_file_path() const override { return __FILE__; }
};
} // namespace doris::vectorized