@@ -137,12 +137,23 @@ Status AggregationNode::prepare(RuntimeState* state) {
137137
138138 } else {
139139 _agg_data.init (AggregatedDataVariants::Type::serialized);
140+ if (_is_merge) {
141+ _executor.execute = std::bind<Status>(&AggregationNode::_merge_with_serialized_key,
142+ this , std::placeholders::_1);
143+ } else {
144+ _executor.execute = std::bind<Status>(&AggregationNode::_execute_with_serialized_key,
145+ this , std::placeholders::_1);
146+ }
140147
141- _executor.execute = std::bind<Status>(&AggregationNode::_execute_with_serialized_key, this ,
142- std::placeholders::_1);
143- _executor.get_result = std::bind<Status>(&AggregationNode::_get_with_serialized_key_result,
144- this , std::placeholders::_1, std::placeholders::_2,
145- std::placeholders::_3);
148+ if (_needs_finalize) {
149+ _executor.get_result = std::bind<Status>(
150+ &AggregationNode::_get_with_serialized_key_result, this , std::placeholders::_1,
151+ std::placeholders::_2, std::placeholders::_3);
152+ } else {
153+ _executor.get_result = std::bind<Status>(
154+ &AggregationNode::_serialize_with_serialized_key_result, this ,
155+ std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
156+ }
146157 }
147158
148159 return Status::OK ();
@@ -410,4 +421,130 @@ Status AggregationNode::_get_with_serialized_key_result(RuntimeState* state, Blo
410421 return Status::OK ();
411422}
412423
424+ Status AggregationNode::_serialize_with_serialized_key_result (RuntimeState* state, Block* block,
425+ bool * eos) {
426+ DCHECK (_agg_data.serialized != nullptr );
427+ using Method = AggregationMethodSerialized<AggregatedDataWithStringKey>;
428+ using AggState = Method::State;
429+
430+ auto & method = *_agg_data.serialized ;
431+ auto & data = _agg_data.serialized ->data ;
432+
433+ int key_size = _probe_expr_ctxs.size ();
434+ int agg_size = _aggregate_evaluators.size ();
435+ MutableColumns value_columns (agg_size);
436+ DataTypes value_data_types (agg_size);
437+
438+ MutableColumns key_columns;
439+ for (int i = 0 ; i < key_size; ++i) {
440+ key_columns.emplace_back (_probe_expr_ctxs[i]->root ()->data_type ()->createColumn ());
441+ }
442+
443+ // will serialize data to string column
444+ for (int i = 0 ; i < _aggregate_evaluators.size (); ++i) {
445+ value_data_types[i] = makeNullable (std::make_shared<DataTypeString>());
446+ value_columns[i] = value_data_types[i]->createColumn ();
447+ }
448+
449+ data.forEachValue ([&](const auto & key, auto & mapped) {
450+ // insert keys
451+ method.insertKeyIntoColumns (key, key_columns, {});
452+
453+ std::ostringstream buf;
454+ // serialize values
455+ for (size_t i = 0 ; i < _aggregate_evaluators.size (); ++i) {
456+ _aggregate_evaluators[i]->function ()->serialize (
457+ mapped + _offsets_of_aggregate_states[i], buf);
458+ value_columns[i]->insertData (buf.str ().c_str (), buf.str ().length ());
459+ buf.str ().clear ();
460+ }
461+ });
462+
463+ ColumnsWithTypeAndName columns_with_schema;
464+ for (int i = 0 ; i < key_size; ++i) {
465+ columns_with_schema.emplace_back (std::move (key_columns[i]),
466+ _probe_expr_ctxs[i]->root ()->data_type (), " " );
467+ }
468+
469+ for (int i = 0 ; i < agg_size; ++i) {
470+ columns_with_schema.emplace_back (std::move (value_columns[i]), value_data_types[i], " " );
471+ }
472+
473+ *block = Block (columns_with_schema);
474+ *eos = true ;
475+ return Status::OK ();
476+ }
477+
478+ Status AggregationNode::_merge_with_serialized_key (Block* block) {
479+ DCHECK (!_probe_expr_ctxs.empty ());
480+ // now we only support serialized key
481+ // TODO:
482+ DCHECK (_agg_data.serialized != nullptr );
483+
484+ using Method = AggregationMethodSerialized<AggregatedDataWithStringKey>;
485+ using AggState = Method::State;
486+
487+ auto & method = *_agg_data.serialized ;
488+
489+ size_t key_size = _probe_expr_ctxs.size ();
490+ ColumnRawPtrs key_columns (key_size);
491+
492+ for (size_t i = 0 ; i < key_size; ++i) {
493+ int result_column_id = -1 ;
494+ RETURN_IF_ERROR (_probe_expr_ctxs[i]->execute (block, &result_column_id));
495+ key_columns[i] = block->getByPosition (result_column_id).column .get ();
496+ }
497+
498+ int rows = block->rows ();
499+ PODArray<AggregateDataPtr> places (rows);
500+
501+ AggState state (key_columns, {}, nullptr );
502+
503+ // / For all rows.
504+ for (size_t i = 0 ; i < rows; ++i) {
505+ AggregateDataPtr aggregate_data = nullptr ;
506+
507+ auto emplace_result = state.emplaceKey (method.data , i, _agg_arena_pool);
508+
509+ // / If a new key is inserted, initialize the states of the aggregate functions, and possibly something related to the key.
510+ if (emplace_result.isInserted ()) {
511+ // / exception-safety - if you can not allocate memory or create states, then destructors will not be called.
512+ emplace_result.setMapped (nullptr );
513+
514+ aggregate_data = _agg_arena_pool.alignedAlloc (_total_size_of_aggregate_states,
515+ _align_aggregate_states);
516+ _create_agg_status (aggregate_data);
517+
518+ emplace_result.setMapped (aggregate_data);
519+ } else
520+ aggregate_data = emplace_result.getMapped ();
521+
522+ places[i] = aggregate_data;
523+ assert (places[i] != nullptr );
524+ }
525+
526+ std::unique_ptr<char []> deserialize_buffer (new char [_total_size_of_aggregate_states]);
527+ for (int i = 0 ; i < _aggregate_evaluators.size (); ++i) {
528+ auto column = block->getByPosition (i + key_size).column ;
529+ if (column->isNullable ()) {
530+ column = ((ColumnNullable*)column.get ())->getNestedColumnPtr ();
531+ }
532+ for (int j = 0 ; j < rows; ++j) {
533+ std::string data_buffer;
534+ StringRef ref = column->getDataAt (j);
535+ data_buffer.assign (ref.data , ref.size );
536+ std::istringstream buf (data_buffer);
537+
538+ _aggregate_evaluators[i]->function ()->deserialize (
539+ deserialize_buffer.get () + _offsets_of_aggregate_states[i], buf,
540+ &_agg_arena_pool);
541+
542+ _aggregate_evaluators[i]->function ()->merge (
543+ places.data ()[j] + _offsets_of_aggregate_states[i],
544+ deserialize_buffer.get () + _offsets_of_aggregate_states[i], &_agg_arena_pool);
545+ }
546+ }
547+ return Status::OK ();
548+ }
549+
413550} // namespace doris::vectorized
0 commit comments