2121#pragma once
2222
2323#include < fmt/format.h>
24+ #include < glog/logging.h>
2425#include < string.h>
2526
2627#include < memory>
28+ #include < string>
29+ #include < type_traits>
2730#include < vector>
2831
2932#include " common/logging.h"
@@ -519,18 +522,104 @@ struct AggregateFunctionMinData : Data {
519522 static const char * name () { return " min" ; }
520523};
521524
525+ // this is used for plain type about any_value function
522526template <typename Data>
523527struct AggregateFunctionAnyData : Data {
524528 using Self = AggregateFunctionAnyData;
525529 using Data::IsFixedLength;
530+ static const char * name () { return " any" ; }
526531 constexpr static bool IS_ANY = true ;
527-
528532 void change_if_better (const IColumn& column, size_t row_num, Arena*) {
529533 this ->change_first_time (column, row_num, nullptr );
530534 }
535+
531536 void change_if_better (const Self& to, Arena*) { this ->change_first_time (to, nullptr ); }
537+ };
532538
539+ // this is used for complex type about any_value function
540+ struct SingleValueDataComplexType {
533541 static const char * name () { return " any" ; }
542+ constexpr static bool IS_ANY = true ;
543+ constexpr static bool IsFixedLength = false ;
544+ using Self = SingleValueDataComplexType;
545+
546+ SingleValueDataComplexType () = default ;
547+
548+ SingleValueDataComplexType (const DataTypes& argument_types, int be_version) {
549+ column_type = argument_types[0 ];
550+ column_data = column_type->create_column ();
551+ be_exec_version = be_version;
552+ }
553+
554+ bool has () const { return has_value; }
555+
556+ void change_first_time (const IColumn& column, size_t row_num, Arena*) {
557+ if (UNLIKELY (!has ())) {
558+ change_impl (column, row_num);
559+ }
560+ }
561+
562+ void change_first_time (const Self& to, Arena*) {
563+ if (UNLIKELY (!has () && to.has ())) {
564+ change_impl (*to.column_data , 0 );
565+ }
566+ }
567+
568+ void change_impl (const IColumn& column, size_t row_num) {
569+ DCHECK_EQ (column_data->size (), 0 );
570+ column_data->insert_from (column, row_num);
571+ has_value = true ;
572+ }
573+
574+ void insert_result_into (IColumn& to) const {
575+ if (has ()) {
576+ to.insert_from (*column_data, 0 );
577+ } else {
578+ to.insert_default ();
579+ }
580+ }
581+
582+ void reset () {
583+ column_data->clear ();
584+ has_value = false ;
585+ }
586+
587+ void write (BufferWritable& buf) const {
588+ write_binary (has (), buf);
589+ if (!has ()) {
590+ return ;
591+ }
592+ auto size_bytes =
593+ column_type->get_uncompressed_serialized_bytes (*column_data, be_exec_version);
594+ std::string memory_buffer (size_bytes, ' 0' );
595+ auto * p = column_type->serialize (*column_data, memory_buffer.data (), be_exec_version);
596+ write_binary (memory_buffer, buf);
597+ DCHECK_EQ (p, memory_buffer.data () + size_bytes);
598+ }
599+
600+ void read (BufferReadable& buf, Arena* arena) {
601+ read_binary (has_value, buf);
602+ if (!has ()) {
603+ return ;
604+ }
605+ std::string memory_buffer;
606+ read_binary (memory_buffer, buf);
607+ const auto * p =
608+ column_type->deserialize (memory_buffer.data (), &column_data, be_exec_version);
609+ DCHECK_EQ (p, memory_buffer.data () + memory_buffer.size ());
610+ }
611+
612+ void change_if_better (const IColumn& column, size_t row_num, Arena* arena) {
613+ this ->change_first_time (column, row_num, nullptr );
614+ }
615+
616+ void change_if_better (const Self& to, Arena* arena) { this ->change_first_time (to, nullptr ); }
617+
618+ private:
619+ bool has_value = false ;
620+ MutableColumnPtr column_data;
621+ DataTypePtr column_type;
622+ int be_exec_version = -1 ;
534623};
535624
536625template <typename Data>
@@ -539,6 +628,7 @@ class AggregateFunctionsSingleValue final
539628private:
540629 DataTypePtr& type;
541630 using Base = IAggregateFunctionDataHelper<Data, AggregateFunctionsSingleValue<Data>>;
631+ using IAggregateFunction::argument_types;
542632
543633public:
544634 AggregateFunctionsSingleValue (const DataTypes& arguments)
@@ -555,6 +645,14 @@ class AggregateFunctionsSingleValue final
555645 }
556646 }
557647
648+ void create (AggregateDataPtr __restrict place) const override {
649+ if constexpr (std::is_same_v<Data, SingleValueDataComplexType>) {
650+ new (place) Data (argument_types, IAggregateFunction::version);
651+ } else {
652+ new (place) Data;
653+ }
654+ }
655+
558656 String get_name () const override { return Data::name (); }
559657
560658 DataTypePtr get_return_type () const override { return type; }
@@ -716,4 +814,9 @@ AggregateFunctionPtr create_aggregate_function_single_value(const String& name,
716814 const DataTypes& argument_types,
717815 const bool result_is_nullable,
718816 const AggregateFunctionAttr& attr = {});
817+
818+ template <template <typename > class Data >
819+ AggregateFunctionPtr create_aggregate_function_single_value_any_value_function (
820+ const String& name, const DataTypes& argument_types, const bool result_is_nullable,
821+ const AggregateFunctionAttr& attr = {});
719822} // namespace doris::vectorized
0 commit comments