@@ -44,41 +44,33 @@ struct StringAggFunction {
4444 if (!state.dataptr ) {
4545 finalize_data.ReturnNull ();
4646 } else {
47- target = StringVector::AddString (finalize_data.result , state.dataptr , state.size );
48- }
49- }
50-
51- template <class STATE >
52- static void Destroy (STATE &state, AggregateInputData &aggr_input_data) {
53- if (state.dataptr ) {
54- delete[] state.dataptr ;
47+ target = string_t (state.dataptr , state.size );
5548 }
5649 }
5750
5851 static bool IgnoreNull () {
5952 return true ;
6053 }
6154
62- static inline void PerformOperation (StringAggState &state, const char *str , const char *sep, idx_t str_size ,
63- idx_t sep_size) {
55+ static inline void PerformOperation (StringAggState &state, ArenaAllocator &allocator , const char *str ,
56+ const char *sep, idx_t str_size, idx_t sep_size) {
6457 if (!state.dataptr ) {
6558 // first iteration: allocate space for the string and copy it into the state
6659 state.alloc_size = MaxValue<idx_t >(8 , NextPowerOfTwo (str_size));
67- state.dataptr = new char [ state.alloc_size ] ;
60+ state.dataptr = char_ptr_cast (allocator. Allocate ( state.alloc_size )) ;
6861 state.size = str_size;
6962 memcpy (state.dataptr , str, str_size);
7063 } else {
7164 // subsequent iteration: first check if we have space to place the string and separator
7265 idx_t required_size = state.size + str_size + sep_size;
7366 if (required_size > state.alloc_size ) {
7467 // no space! allocate extra space
68+ const auto old_size = state.alloc_size ;
7569 while (state.alloc_size < required_size) {
7670 state.alloc_size *= 2 ;
7771 }
78- auto new_data = new char [state.alloc_size ];
79- memcpy (new_data, state.dataptr , state.size );
80- delete[] state.dataptr ;
81- state.dataptr = new_data;
72+ state.dataptr =
73+ char_ptr_cast (allocator.Reallocate (data_ptr_cast (state.dataptr ), old_size, state.alloc_size ));
8274 }
8375 // copy the separator
8476 memcpy (state.dataptr + state.size , sep, sep_size);
@@ -89,14 +81,15 @@ struct StringAggFunction {
8981 }
9082 }
9183
92- static inline void PerformOperation (StringAggState &state, string_t str, optional_ptr<FunctionData> data_p) {
84+ static inline void PerformOperation (StringAggState &state, ArenaAllocator &allocator, string_t str,
85+ optional_ptr<FunctionData> data_p) {
9386 auto &data = data_p->Cast <StringAggBindData>();
94- PerformOperation (state, str.GetData (), data.sep .c_str (), str.GetSize (), data.sep .size ());
87+ PerformOperation (state, allocator, str.GetData (), data.sep .c_str (), str.GetSize (), data.sep .size ());
9588 }
9689
9790 template <class INPUT_TYPE , class STATE , class OP >
9891 static void Operation (STATE &state, const INPUT_TYPE &input, AggregateUnaryInput &unary_input) {
99- PerformOperation (state, input, unary_input.input .bind_data );
92+ PerformOperation (state, unary_input. input . allocator , input, unary_input.input .bind_data );
10093 }
10194
10295 template <class INPUT_TYPE , class STATE , class OP >
@@ -113,8 +106,8 @@ struct StringAggFunction {
113106 // source is not set: skip combining
114107 return ;
115108 }
116- PerformOperation (target, string_t (source. dataptr , UnsafeNumericCast< uint32_t >(source. size )) ,
117- aggr_input_data.bind_data );
109+ PerformOperation (target, aggr_input_data. allocator ,
110+ string_t (source. dataptr , UnsafeNumericCast< uint32_t >(source. size )), aggr_input_data.bind_data );
118111 }
119112};
120113
@@ -162,8 +155,7 @@ AggregateFunctionSet StringAggFun::GetFunctions() {
162155 AggregateFunction::UnaryScatterUpdate<StringAggState, string_t , StringAggFunction>,
163156 AggregateFunction::StateCombine<StringAggState, StringAggFunction>,
164157 AggregateFunction::StateFinalize<StringAggState, string_t , StringAggFunction>,
165- AggregateFunction::UnaryUpdate<StringAggState, string_t , StringAggFunction>, StringAggBind,
166- AggregateFunction::StateDestroy<StringAggState, StringAggFunction>);
158+ AggregateFunction::UnaryUpdate<StringAggState, string_t , StringAggFunction>, StringAggBind);
167159 string_agg_param.serialize = StringAggSerialize;
168160 string_agg_param.deserialize = StringAggDeserialize;
169161 string_agg.AddFunction (string_agg_param);
0 commit comments