Skip to content

Commit 01ca9da

Browse files
HappenLeelihaopeng
andcommitted
1. Fix the bug of sort node return empty block if child eos is true and add some comment (apache#23)
2. Use SIMD to speed up has_null() in column nullable 3. Support UDAF of avg Change-Id: I13846d7275e1cc37085d3afbf41d60261e296662 Co-authored-by: lihaopeng <[email protected]>
1 parent 76540c9 commit 01ca9da

11 files changed

Lines changed: 212 additions & 37 deletions

‎be/src/vec/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ set(VEC_FILES
2424
aggregate_functions/aggregate_function_null.cpp
2525
aggregate_functions/aggregate_function_sum.cpp
2626
aggregate_functions/aggregate_function_min_max.cpp
27+
aggregate_functions/aggregate_function_avg.cpp
2728
columns/collator.cpp
2829
columns/column.cpp
2930
columns/column_const.cpp
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "vec/aggregate_functions/aggregate_function_simple_factory.h"
2+
#include "vec/aggregate_functions/aggregate_function_avg.h"
3+
#include "vec/aggregate_functions/helpers.h"
4+
#include "vec/aggregate_functions/factory_helpers.h"
5+
6+
namespace doris::vectorized
7+
{
8+
9+
namespace
10+
{
11+
12+
template <typename T>
13+
struct Avg
14+
{
15+
using FieldType = std::conditional_t<IsDecimalNumber<T>, Decimal128, NearestFieldType<T>>;
16+
using Function = AggregateFunctionAvg<T, AggregateFunctionAvgData<FieldType>>;
17+
};
18+
19+
template <typename T>
20+
using AggregateFuncAvg = typename Avg<T>::Function;
21+
22+
AggregateFunctionPtr createAggregateFunctionAvg(const std::string & name, const DataTypes & argument_types, const Array & parameters)
23+
{
24+
assertNoParameters(name, parameters);
25+
assertUnary(name, argument_types);
26+
27+
AggregateFunctionPtr res;
28+
DataTypePtr data_type = argument_types[0];
29+
if (isDecimal(data_type))
30+
res.reset(createWithDecimalType<AggregateFuncAvg>(*data_type, *data_type, argument_types));
31+
else
32+
res.reset(createWithNumericType<AggregateFuncAvg>(*data_type, argument_types));
33+
34+
if (!res)
35+
throw Exception("Illegal type " + argument_types[0]->getName() + " of argument for aggregate function " + name,
36+
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
37+
return res;
38+
}
39+
40+
}
41+
42+
//void registerAggregateFunctionAvg(AggregateFunctionFactory & factory)
43+
//{
44+
// factory.registerFunction("avg", createAggregateFunctionAvg, AggregateFunctionFactory::CaseInsensitive);
45+
//}
46+
47+
void registerAggregateFunctionAvg(AggregateFunctionSimpleFactory& factory) {
48+
factory.registerFunction("avg", createAggregateFunctionAvg);
49+
}
50+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#pragma once
2+
3+
#include "vec/data_types/data_types_number.h"
4+
#include "vec/data_types/data_types_decimal.h"
5+
#include "vec/columns/columns_number.h"
6+
#include "vec/aggregate_functions/aggregate_function.h"
7+
#include "vec/io/io_helper.h"
8+
9+
namespace doris::vectorized
10+
{
11+
12+
namespace ErrorCodes
13+
{
14+
extern const int LOGICAL_ERROR;
15+
}
16+
17+
template <typename T>
18+
struct AggregateFunctionAvgData
19+
{
20+
T sum = 0;
21+
UInt64 count = 0;
22+
23+
template <typename ResultT>
24+
ResultT NO_SANITIZE_UNDEFINED result() const
25+
{
26+
if constexpr (std::is_floating_point_v<ResultT>)
27+
if constexpr (std::numeric_limits<ResultT>::is_iec559)
28+
return static_cast<ResultT>(sum) / count; /// allow division by zero
29+
30+
if (!count)
31+
throw Exception("AggregateFunctionAvg with zero values", ErrorCodes::LOGICAL_ERROR);
32+
return static_cast<ResultT>(sum) / count;
33+
}
34+
35+
void write(std::ostream& buf) const {
36+
writeBinary(sum, buf);
37+
writeBinary(count, buf);
38+
}
39+
40+
void read(std::istream& buf) {
41+
readBinary(sum, buf);
42+
readBinary(count, buf);
43+
}
44+
};
45+
46+
47+
/// Calculates arithmetic mean of numbers.
48+
template <typename T, typename Data>
49+
class AggregateFunctionAvg final : public IAggregateFunctionDataHelper<Data, AggregateFunctionAvg<T, Data>>
50+
{
51+
public:
52+
using ResultType = std::conditional_t<IsDecimalNumber<T>, Decimal128, Float64>;
53+
using ResultDataType = std::conditional_t<IsDecimalNumber<T>, DataTypeDecimal<Decimal128>, DataTypeNumber<Float64>>;
54+
using ColVecType = std::conditional_t<IsDecimalNumber<T>, ColumnDecimal<T>, ColumnVector<T>>;
55+
using ColVecResult = std::conditional_t<IsDecimalNumber<T>, ColumnDecimal<Decimal128>, ColumnVector<Float64>>;
56+
57+
/// ctor for native types
58+
AggregateFunctionAvg(const DataTypes & argument_types_)
59+
: IAggregateFunctionDataHelper<Data, AggregateFunctionAvg<T, Data>>(argument_types_, {})
60+
, scale(0)
61+
{}
62+
63+
/// ctor for Decimals
64+
AggregateFunctionAvg(const IDataType & data_type, const DataTypes & argument_types_)
65+
: IAggregateFunctionDataHelper<Data, AggregateFunctionAvg<T, Data>>(argument_types_, {})
66+
, scale(getDecimalScale(data_type))
67+
{}
68+
69+
String getName() const override { return "avg"; }
70+
71+
DataTypePtr getReturnType() const override
72+
{
73+
if constexpr (IsDecimalNumber<T>)
74+
return std::make_shared<ResultDataType>(ResultDataType::maxPrecision(), scale);
75+
else
76+
return std::make_shared<ResultDataType>();
77+
}
78+
79+
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
80+
{
81+
const auto & column = static_cast<const ColVecType &>(*columns[0]);
82+
this->data(place).sum += column.getData()[row_num];
83+
++this->data(place).count;
84+
}
85+
86+
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override
87+
{
88+
this->data(place).sum += this->data(rhs).sum;
89+
this->data(place).count += this->data(rhs).count;
90+
}
91+
92+
void serialize(ConstAggregateDataPtr place, std::ostream& buf) const override
93+
{
94+
this->data(place).write(buf);
95+
}
96+
97+
void deserialize(AggregateDataPtr place, std::istream& buf, Arena *) const override
98+
{
99+
this->data(place).read(buf);
100+
}
101+
102+
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
103+
{
104+
auto & column = static_cast<ColVecResult &>(to);
105+
column.getData().push_back(this->data(place).template result<ResultType>());
106+
}
107+
108+
const char * getHeaderFilePath() const override { return __FILE__; }
109+
110+
private:
111+
UInt32 scale;
112+
};
113+
114+
115+
}

‎be/src/vec/aggregate_functions/aggregate_function_null.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ void registerAggregateFunctionCombinatorNull(AggregateFunctionSimpleFactory& fac
104104
factory.registerFunction("sum", creator, true);
105105
factory.registerFunction("max", creator, true);
106106
factory.registerFunction("min", creator, true);
107+
factory.registerFunction("avg", creator, true);
107108
}
108109

109110
} // namespace doris::vectorized

‎be/src/vec/aggregate_functions/aggregate_function_simple_factory.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class AggregateFunctionSimpleFactory;
3333
void registerAggregateFunctionSum(AggregateFunctionSimpleFactory& factory);
3434
void registerAggregateFunctionCombinatorNull(AggregateFunctionSimpleFactory& factory);
3535
void registerAggregateFunctionMinMax(AggregateFunctionSimpleFactory& factory);
36+
void registerAggregateFunctionAvg(AggregateFunctionSimpleFactory& factory);
3637

3738
using DataTypePtr = std::shared_ptr<const IDataType>;
3839
using DataTypes = std::vector<DataTypePtr>;
@@ -82,6 +83,7 @@ class AggregateFunctionSimpleFactory {
8283
std::call_once(oc, [&]() {
8384
registerAggregateFunctionSum(instance);
8485
registerAggregateFunctionMinMax(instance);
86+
registerAggregateFunctionAvg(instance);
8587
registerAggregateFunctionCombinatorNull(instance);
8688
});
8789
return instance;

‎be/src/vec/columns/column_nullable.h‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,34 @@ class ColumnNullable final : public COWHelper<IColumn, ColumnNullable> {
159159
void checkConsistency() const;
160160

161161
bool has_null() const {
162-
auto begin = getNullMapData().begin();
163-
auto end = getNullMapData().end();
164-
while (begin < end) {
165-
if (*begin != 0) {
166-
return *begin;
162+
size_t size = getNullMapData().size();
163+
const UInt8* null_pos = getNullMapData().data();
164+
const UInt8* null_pos_end = getNullMapData().data() + size;
165+
#ifdef __SSE2__
166+
/** A slightly more optimized version.
167+
* Based on the assumption that often pieces of consecutive values
168+
* completely pass or do not pass the filter.
169+
* Therefore, we will optimistically check the parts of `SIMD_BYTES` values.
170+
*/
171+
static constexpr size_t SIMD_BYTES = 16;
172+
const __m128i zero16 = _mm_setzero_si128();
173+
const UInt8* null_end_sse = null_pos + size / SIMD_BYTES * SIMD_BYTES;
174+
175+
while (null_pos < null_end_sse) {
176+
int mask = _mm_movemask_epi8(_mm_cmpgt_epi8(
177+
_mm_loadu_si128(reinterpret_cast<const __m128i*>(null_pos)), zero16));
178+
179+
if (0 != mask) {
180+
return true;
167181
}
168-
++begin;
182+
null_pos += SIMD_BYTES;
183+
}
184+
#endif
185+
while (null_pos < null_pos_end) {
186+
if (*null_pos != 0) {
187+
return true;
188+
}
189+
null_pos++;
169190
}
170191
return false;
171192
}

‎be/src/vec/core/sort_cursor.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct ReceiveQueueSortCursorImpl : public SortCursorImpl {
116116
desc[i].direction = is_asc_order[i] ? 1 : -1;
117117
desc[i].nulls_direction = nulls_first[i] ? 1 : -1;
118118
}
119-
has_next_block();
119+
_is_eof = !has_next_block();
120120
}
121121

122122
bool has_next_block() override {

‎be/src/vec/exec/vaggregation_node.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ struct AggregationMethodSerialized {
4747

4848
Data data;
4949

50-
AggregationMethodSerialized() {}
50+
AggregationMethodSerialized() = default;
5151

5252
template <typename Other>
53-
AggregationMethodSerialized(const Other& other) : data(other.data) {}
53+
explicit AggregationMethodSerialized(const Other& other) : data(other.data) {}
5454

5555
using State = ColumnsHashing::HashMethodSerialized<typename Data::value_type, Mapped>;
5656

@@ -153,6 +153,7 @@ class AggregationNode : public ::doris::ExecNode {
153153
using vectorized_execute = std::function<Status(Block* block)>;
154154
using vectorized_get_result =
155155
std::function<Status(RuntimeState* state, Block* block, bool* eos)>;
156+
156157
struct executor {
157158
vectorized_execute execute;
158159
vectorized_get_result get_result;

‎be/src/vec/exec/vsort_node.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Status VSortNode::sort_input(RuntimeState* state) {
124124
do {
125125
Block block;
126126
RETURN_IF_ERROR(child(0)->get_next(state, &block, &eos));
127-
if (!eos && block.rows() != 0) {
127+
if ( block.rows() != 0) {
128128
RETURN_IF_ERROR(pretreat_block(block));
129129
_sorted_blocks.emplace_back(std::move(block));
130130
RETURN_IF_CANCELLED(state);

‎be/src/vec/exec/vsort_node.h‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,9 @@
2828
namespace doris {
2929

3030
namespace vectorized {
31-
// Node that implements a full sort of its input with a fixed memory budget, spilling
32-
// to disk if the input is larger than available memory.
33-
// Uses SpillSorter and BufferedBlockMgr for the external sort implementation.
34-
// Input rows to SortNode are materialized by the SpillSorter into a single tuple
35-
// using the expressions specified in _sort_exec_exprs.
36-
// In get_next(), SortNode passes in the output batch to the sorter instance created
37-
// in open() to fill it with sorted rows.
38-
// If a merge phase was performed in the sort, sorted rows are deep copied into
39-
// the output batch. Otherwise, the sorter instance owns the sorted data.
31+
// Node that implements a full sort of its input with a fixed memory budget
32+
// In open() the input Block to VSortNode will sort firstly, using the expressions specified in _sort_exec_exprs.
33+
// In get_next(), VSortNode do the merge sort to gather data to a new block
4034

4135
// support spill to disk in the future
4236
class VSortNode : public doris::ExecNode {
@@ -75,7 +69,7 @@ class VSortNode : public doris::ExecNode {
7569
// Number of rows to skip.
7670
int64_t _offset;
7771

78-
// Expressions and parameters used for tuple materialization and tuple comparison.
72+
// Expressions and parameters used for build _sort_description
7973
VSortExecExprs _vsort_exec_exprs;
8074
std::vector<bool> _is_asc_order;
8175
std::vector<bool> _nulls_first;
@@ -85,11 +79,9 @@ class VSortNode : public doris::ExecNode {
8579
std::vector<Block> _sorted_blocks;
8680
std::priority_queue<SortCursor> _priority_queue;
8781

82+
// TODO: Not using now, maybe should be delete
8883
// Keeps track of the number of rows skipped for handling _offset.
8984
int64_t _num_rows_skipped;
90-
91-
// END: Members that must be reset()
92-
/////////////////////////////////////////
9385
};
9486

9587
}

0 commit comments

Comments
 (0)