yiguolei commented on code in PR #18051:
URL: https://github.com/apache/doris/pull/18051#discussion_r1156698027
##########
be/src/vec/exec/vaggregation_node.cpp:
##########
@@ -1127,6 +1204,205 @@ Status
AggregationNode::_pre_agg_with_serialized_key(doris::vectorized::Block* i
return Status::OK();
}
+struct PartitionHelper {
+ static constexpr size_t PartitionCountBits = 4;
+ static constexpr size_t PartitionCount = 1 << PartitionCountBits;
+ static constexpr size_t MaxPartitionIndex = PartitionCount - 1;
+
+ static size_t get_index(size_t hash_value) {
+ return (hash_value >> (32 - PartitionCountBits)) & MaxPartitionIndex;
+ }
+};
+
+template <typename HashTableCtxType, typename HashTableType, typename KeyType>
+Status AggregationNode::_serialize_hash_table_to_block(HashTableCtxType&
context,
+ HashTableType&
hash_table, Block& block,
+ std::vector<KeyType>&
keys_) {
+ int key_size = _probe_expr_ctxs.size();
+ int agg_size = _aggregate_evaluators.size();
+
+ MutableColumns value_columns(agg_size);
+ DataTypes value_data_types(agg_size);
+ MutableColumns key_columns;
+
+ for (int i = 0; i < key_size; ++i) {
+
key_columns.emplace_back(_probe_expr_ctxs[i]->root()->data_type()->create_column());
+ }
+
+ if (_use_fixed_length_serialization_opt) {
+ for (size_t i = 0; i < _aggregate_evaluators.size(); ++i) {
+ value_data_types[i] =
_aggregate_evaluators[i]->function()->get_serialized_type();
+ value_columns[i] =
_aggregate_evaluators[i]->function()->create_serialize_column();
+ }
+ } else {
+ auto serialize_string_type = std::make_shared<DataTypeString>();
+ for (int i = 0; i < _aggregate_evaluators.size(); ++i) {
+ value_data_types[i] = serialize_string_type;
+ value_columns[i] = serialize_string_type->create_column();
+ }
+ }
+
+ context.init_once();
+ const auto size = hash_table.size();
+ std::vector<KeyType> keys(size);
+ if (_values.size() < size) {
+ _values.resize(size);
+ }
+
+ size_t num_rows = 0;
+ _aggregate_data_container->init_once();
+ auto& iter = _aggregate_data_container->iterator;
+
+ {
+ while (iter != _aggregate_data_container->end()) {
+ keys[num_rows] = iter.get_key<KeyType>();
+ _values[num_rows] = iter.get_aggregate_data();
+ ++iter;
+ ++num_rows;
+ }
+ }
+
+ { context.insert_keys_into_columns(keys, key_columns, num_rows,
_probe_key_sz); }
+
+ if (hash_table.has_null_key_data()) {
+ // only one key of group by support wrap null key
+ // here need additional processing logic on the null key / value
+ CHECK(key_columns.size() == 1);
+ CHECK(key_columns[0]->is_nullable());
+ key_columns[0]->insert_data(nullptr, 0);
+
+ // Here is no need to set `keys[num_rows]`, keep it as default value.
+ _values[num_rows] = hash_table.get_null_key_data();
+ ++num_rows;
+ }
+
+ if (_use_fixed_length_serialization_opt) {
+ for (size_t i = 0; i < _aggregate_evaluators.size(); ++i) {
+ _aggregate_evaluators[i]->function()->serialize_to_column(
+ _values, _offsets_of_aggregate_states[i],
value_columns[i], num_rows);
+ }
+ } else {
+ std::vector<VectorBufferWriter> value_buffer_writers;
+ for (int i = 0; i < _aggregate_evaluators.size(); ++i) {
+ value_buffer_writers.emplace_back(
+ *reinterpret_cast<ColumnString*>(value_columns[i].get()));
+ }
+ for (size_t i = 0; i < _aggregate_evaluators.size(); ++i) {
+ _aggregate_evaluators[i]->function()->serialize_vec(
+ _values, _offsets_of_aggregate_states[i],
value_buffer_writers[i], num_rows);
+ }
+ }
+
+ ColumnsWithTypeAndName columns_with_schema;
+ for (int i = 0; i < key_size; ++i) {
+ columns_with_schema.emplace_back(std::move(key_columns[i]),
+
_probe_expr_ctxs[i]->root()->data_type(),
+
_probe_expr_ctxs[i]->root()->expr_name());
+ }
+ for (int i = 0; i < agg_size; ++i) {
+ columns_with_schema.emplace_back(std::move(value_columns[i]),
value_data_types[i],
+
_aggregate_evaluators[i]->function()->get_name());
+ }
+
+ block = columns_with_schema;
+ keys_.swap(keys);
+ return Status::OK();
+}
+
+template <typename HashTableCtxType, typename HashTableType>
+Status AggregationNode::_spill_hash_table(HashTableCtxType& agg_method,
HashTableType& hash_table) {
+ Block block;
+ std::vector<typename HashTableType::key_type> keys;
+ RETURN_IF_ERROR(_serialize_hash_table_to_block(agg_method, hash_table,
block, keys));
+ CHECK_EQ(block.rows(), hash_table.size());
+ CHECK_EQ(keys.size(), block.rows());
+
+ if (!_spill_context.has_data) {
+ _spill_context.has_data = true;
+ _spill_context.runtime_profile =
_runtime_profile->create_child("Spill", true, true);
+ }
+
+ BlockSpillWriterUPtr writer;
+ RETURN_IF_ERROR(ExecEnv::GetInstance()->block_spill_mgr()->get_writer(
+ std::numeric_limits<int32_t>::max(), writer,
_spill_context.runtime_profile));
+ Defer defer {[&]() {
+ // redundant call is ok
+ writer->close();
+ }};
+ _spill_context.stream_ids.emplace_back(writer->get_id());
+
+ std::vector<size_t> partitioned_indices(block.rows());
+ std::array<size_t, PartitionHelper::PartitionCount> blocks_rows = {0};
+
+ for (size_t i = 0; i < block.rows(); ++i) {
+ const auto index =
PartitionHelper::get_index(hash_table.hash(keys[i]));
+ partitioned_indices[i] = index;
+ blocks_rows[index]++;
+ }
+
+ for (size_t i = 0; i < PartitionHelper::PartitionCount; ++i) {
+ Block block_to_write = block.clone_empty();
+ if (blocks_rows[i] == 0) {
+ writer->write(block_to_write);
Review Comment:
why write an empty block?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]