237289 {
237290 auto &bound_function = child_aggregate->function;
237291 if (!bound_function.HasStateCombineCallback()) {
237292 throw BinderException("Cannot use EXPORT_STATE for non-combinable function %s", bound_function.name);
237293 }
237294 if (bound_function.HasBindCallback()) {
237295 throw BinderException("Cannot use EXPORT_STATE on aggregate functions with custom binders");
237296 }
237297 if (bound_function.HasStateDestructorCallback()) {
237298 throw BinderException("Cannot use EXPORT_STATE on aggregate functions with custom destructors");
237299 }
237300
237301 D_ASSERT(bound_function.HasStateSizeCallback());
237302 D_ASSERT(bound_function.HasStateFinalizeCallback());
237303
237304 D_ASSERT(child_aggregate->function.GetReturnType().id() != LogicalTypeId::INVALID);
237305#ifdef DEBUG
237306 for (auto &arg_type : child_aggregate->function.arguments) {
237307 D_ASSERT(arg_type.id() != LogicalTypeId::INVALID);
237308 }
237309#endif
237310 auto export_bind_data = make_uniq<ExportAggregateFunctionBindData>(child_aggregate->Copy());
237311 aggregate_state_t state_type(child_aggregate->function.name, child_aggregate->function.GetReturnType(),
237312 child_aggregate->function.arguments);
237313 auto return_type = LogicalType::AGGREGATE_STATE(std::move(state_type));
237314
237315 auto export_function =
237316 AggregateFunction("aggregate_state_export_" + bound_function.name, bound_function.arguments, return_type,
237317 bound_function.GetStateSizeCallback(), bound_function.GetStateInitCallback(),
237318 bound_function.GetStateUpdateCallback(), bound_function.GetStateCombineCallback(),
237319 ExportAggregateFinalize, bound_function.GetStateSimpleUpdateCallback(),
237320 nullptr, nullptr,
237321 nullptr, nullptr);
237322 export_function.SetNullHandling(FunctionNullHandling::SPECIAL_HANDLING);
237323 export_function.SetSerializeCallback(ExportStateAggregateSerialize);
237324 export_function.SetDeserializeCallback(ExportStateAggregateDeserialize);
237325
237326 return make_uniq<BoundAggregateExpression>(export_function, std::move(child_aggregate->children),
237327 std::move(child_aggregate->filter), std::move(export_bind_data),
237328 child_aggregate->aggr_type);
237329}