Skip to content

Commit b13a4a9

Browse files
committed
1. Fix the core bug of agg query in DEBUG mode (apache#28)
2. Support the count aggregate_function 3. Some code refactor
1 parent cf1c4a9 commit b13a4a9

6 files changed

Lines changed: 37 additions & 28 deletions

File tree

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
// #include <AggregateFunctions/AggregateFunctionFactory.h>
18+
#include "vec/aggregate_functions/aggregate_function_simple_factory.h"
1919
#include <vec/aggregate_functions/aggregate_function_count.h>
2020
#include <vec/aggregate_functions/factory_helpers.h>
2121

22-
namespace DB {
22+
namespace doris::vectorized {
2323

24-
namespace {
24+
AggregateFunctionPtr createAggregateFunctionCount(const std::string & name, const DataTypes & argument_types, const Array & parameters) {
25+
assertNoParameters(name, parameters);
26+
assertArityAtMost<1>(name, argument_types);
2527

26-
// AggregateFunctionPtr createAggregateFunctionCount(const std::string & name, const DataTypes & argument_types, const Array & parameters)
27-
// {
28-
// assertNoParameters(name, parameters);
29-
// assertArityAtMost<1>(name, argument_types);
30-
31-
// return std::make_shared<AggregateFunctionCount>(argument_types);
32-
// }
33-
34-
} // namespace
28+
return std::make_shared<AggregateFunctionCount>(argument_types);
29+
}
3530

3631
// void registerAggregateFunctionCount(AggregateFunctionFactory & factory)
3732
// {
3833
// factory.registerFunction("count", createAggregateFunctionCount, AggregateFunctionFactory::CaseInsensitive);
3934
// }
4035

41-
} // namespace DB
36+
void registerAggregateFunctionCount(AggregateFunctionSimpleFactory& factory) {
37+
factory.registerFunction("count", createAggregateFunctionCount);
38+
}
39+
40+
} // namespace
41+

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AggregateFunctionCount final
4848

4949
String getName() const override { return "count"; }
5050

51-
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeUInt64>(); }
51+
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeInt64>(); }
5252

5353
void add(AggregateDataPtr place, const IColumn**, size_t, Arena*) const override {
5454
++data(place).count;
@@ -67,7 +67,7 @@ class AggregateFunctionCount final
6767
}
6868

6969
void insertResultInto(ConstAggregateDataPtr place, IColumn& to) const override {
70-
assert_cast<ColumnUInt64&>(to).getData().push_back(data(place).count);
70+
assert_cast<ColumnInt64&>(to).getData().push_back(data(place).count);
7171
}
7272

7373
const char* getHeaderFilePath() const override { return __FILE__; }
@@ -90,7 +90,7 @@ class AggregateFunctionCountNotNullUnary final
9090

9191
String getName() const override { return "count"; }
9292

93-
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeUInt64>(); }
93+
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeInt64>(); }
9494

9595
void add(AggregateDataPtr place, const IColumn** columns, size_t row_num,
9696
Arena*) const override {
@@ -110,7 +110,7 @@ class AggregateFunctionCountNotNullUnary final
110110
}
111111

112112
void insertResultInto(ConstAggregateDataPtr place, IColumn& to) const override {
113-
assert_cast<ColumnUInt64&>(to).getData().push_back(data(place).count);
113+
assert_cast<ColumnInt64&>(to).getData().push_back(data(place).count);
114114
}
115115

116116
const char* getHeaderFilePath() const override { return __FILE__; }

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace doris::vectorized {
3131

3232
class AggregateFunctionSimpleFactory;
3333
void registerAggregateFunctionSum(AggregateFunctionSimpleFactory& factory);
34+
void registerAggregateFunctionCount(AggregateFunctionSimpleFactory& factory);
3435
void registerAggregateFunctionCombinatorNull(AggregateFunctionSimpleFactory& factory);
3536
void registerAggregateFunctionMinMax(AggregateFunctionSimpleFactory& factory);
3637
void registerAggregateFunctionAvg(AggregateFunctionSimpleFactory& factory);
@@ -82,6 +83,7 @@ class AggregateFunctionSimpleFactory {
8283
static AggregateFunctionSimpleFactory instance;
8384
std::call_once(oc, [&]() {
8485
registerAggregateFunctionSum(instance);
86+
registerAggregateFunctionCount(instance);
8587
registerAggregateFunctionMinMax(instance);
8688
registerAggregateFunctionAvg(instance);
8789
registerAggregateFunctionCombinatorNull(instance);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include "vec/exec/vaggregation_node.h"
1919

20+
#include <memory>
21+
2022
#include "exec/exec_node.h"
2123
#include "runtime/mem_pool.h"
2224
#include "runtime/row_batch.h"
@@ -40,7 +42,7 @@ AggregationNode::AggregationNode(ObjectPool* pool, const TPlanNode& tnode,
4042
_is_merge(false),
4143
_agg_data() {}
4244

43-
AggregationNode::~AggregationNode() {}
45+
AggregationNode::~AggregationNode() = default;
4446

4547
Status AggregationNode::init(const TPlanNode& tnode, RuntimeState* state) {
4648
RETURN_IF_ERROR(ExecNode::init(tnode, state));
@@ -57,8 +59,8 @@ Status AggregationNode::init(const TPlanNode& tnode, RuntimeState* state) {
5759
_aggregate_evaluators.push_back(evaluator);
5860
}
5961

60-
auto agg_functions = tnode.agg_node.aggregate_functions;
61-
_is_merge = std::any_of(agg_functions.begin(), agg_functions.end(),
62+
const auto& agg_functions = tnode.agg_node.aggregate_functions;
63+
_is_merge = std::any_of(agg_functions.cbegin(), agg_functions.cend(),
6264
[](const auto& e) { return e.nodes[0].agg_expr.is_merge_agg; });
6365
return Status::OK();
6466
}
@@ -73,7 +75,7 @@ Status AggregationNode::prepare(RuntimeState* state) {
7375
RETURN_IF_ERROR(
7476
VExpr::prepare(_probe_expr_ctxs, state, child(0)->row_desc(), expr_mem_tracker()));
7577

76-
_mem_pool.reset(new MemPool(mem_tracker().get()));
78+
_mem_pool = std::make_unique<MemPool>(mem_tracker().get());
7779

7880
int j = _probe_expr_ctxs.size();
7981
for (int i = 0; i < _aggregate_evaluators.size(); ++i, ++j) {
@@ -89,7 +91,7 @@ Status AggregationNode::prepare(RuntimeState* state) {
8991
for (size_t i = 0; i < _aggregate_evaluators.size(); ++i) {
9092
_offsets_of_aggregate_states[i] = _total_size_of_aggregate_states;
9193

92-
const auto agg_function = _aggregate_evaluators[i]->function();
94+
const auto& agg_function = _aggregate_evaluators[i]->function();
9395
// aggreate states are aligned based on maximum requirement
9496
_align_aggregate_states = std::max(_align_aggregate_states, agg_function->alignOfData());
9597
_total_size_of_aggregate_states += agg_function->sizeOfData();

‎be/src/vec/exprs/vectorized_agg_fn.cpp‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,19 @@ void AggFnEvaluator::destroy(AggregateDataPtr place) {
9999
}
100100

101101
void AggFnEvaluator::execute_single_add(Block* block, AggregateDataPtr place, Arena* arena) {
102-
std::vector<const IColumn*> column_arguments(_input_exprs_ctxs.size());
103-
auto columns = block->getColumns();
102+
std::vector<ColumnPtr> columns(_input_exprs_ctxs.size());
104103
for (int i = 0; i < _input_exprs_ctxs.size(); ++i) {
105104
int column_id = -1;
106105
_input_exprs_ctxs[i]->execute(block, &column_id);
107-
column_arguments[i] =
108-
block->getByPosition(column_id).column->convertToFullColumnIfConst().get();
106+
columns[i] =
107+
block->getByPosition(column_id).column->convertToFullColumnIfConst();
109108
}
109+
// Because the `convertToFullColumnIfConst()` may return a temporary variable, so we need keep the reference of it
110+
// to make sure program do not destroy it before we call `addBatchSinglePlace`.
111+
// WARNING:
112+
// There's danger to call `convertToFullColumnIfConst().get()` to get the `const IColumn*` directly.
113+
std::vector<const IColumn*> column_arguments(columns.size());
114+
std::transform(columns.cbegin(), columns.cend(), column_arguments.begin(), [](const auto& ptr) {return ptr.get();});
110115
_function->addBatchSinglePlace(block->rows(), place, column_arguments.data(), nullptr);
111116
}
112117

‎be/src/vec/utils/util.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class VectorizedUtils {
1515

1616
static ColumnsWithTypeAndName create_columns_with_type_and_name(const RowDescriptor& row_desc) {
1717
ColumnsWithTypeAndName columns_with_type_and_name;
18-
for (const auto tuple_desc : row_desc.tuple_descriptors()) {
19-
for (const auto slot_desc : tuple_desc->slots()) {
18+
for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
19+
for (const auto& slot_desc : tuple_desc->slots()) {
2020
columns_with_type_and_name.emplace_back(nullptr, slot_desc->get_data_type_ptr(),
2121
slot_desc->col_name());
2222
}

0 commit comments

Comments
 (0)