HappenLee commented on code in PR #57868: URL: https://github.com/apache/doris/pull/57868#discussion_r2554179642
########## be/src/vec/aggregate_functions/aggregate_function_python_udaf.cpp: ########## @@ -0,0 +1,373 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "vec/aggregate_functions/aggregate_function_python_udaf.h" + +#include <arrow/array.h> +#include <arrow/memory_pool.h> +#include <arrow/record_batch.h> +#include <arrow/type.h> +#include <fmt/format.h> + +#include "common/exception.h" +#include "common/logging.h" +#include "runtime/user_function_cache.h" +#include "udf/python/python_env.h" +#include "udf/python/python_udf_runtime.h" +#include "udf/python/python_udf_server.h" +#include "util/arrow/block_convertor.h" +#include "util/arrow/row_batch.h" +#include "util/timezone_utils.h" +#include "vec/columns/column_nullable.h" +#include "vec/columns/column_vector.h" +#include "vec/core/block.h" +#include "vec/data_types/data_type_nullable.h" + +namespace doris::vectorized { + +Status AggregatePythonUDAFData::create(int64_t place) { + DCHECK(client != nullptr) << "Client must be set before calling create"; + RETURN_IF_ERROR(client->create(place)); + return Status::OK(); +} + +Status AggregatePythonUDAFData::add(int64_t place_id, const IColumn** columns, + int64_t row_num_start, int64_t row_num_end, + const DataTypes& argument_types) { + DCHECK(client != nullptr) << "Client must be set before calling add"; + + Block input_block; + for (size_t i = 0; i < argument_types.size(); ++i) { + input_block.insert( + ColumnWithTypeAndName(columns[i]->get_ptr(), argument_types[i], std::to_string(i))); + } + + std::shared_ptr<arrow::Schema> schema; + RETURN_IF_ERROR( + get_arrow_schema_from_block(input_block, &schema, TimezoneUtils::default_time_zone)); + + std::shared_ptr<arrow::RecordBatch> batch; + cctz::time_zone timezone_obj; + TimezoneUtils::find_cctz_time_zone(TimezoneUtils::default_time_zone, timezone_obj); + RETURN_IF_ERROR(convert_to_arrow_batch(input_block, schema, arrow::default_memory_pool(), + &batch, timezone_obj)); + + // Each place has its own client, so we always use is_single_place=true + RETURN_IF_ERROR( + client->accumulate(place_id, true, *batch, row_num_start, row_num_end, nullptr, 0)); + + return Status::OK(); +} + +Status AggregatePythonUDAFData::merge(const AggregatePythonUDAFData& rhs, int64_t place) { + DCHECK(client != nullptr) << "Client must be set before calling merge"; + + // Get serialized state from rhs (already stored in serialize_data by read()) + auto serialized_state = arrow::Buffer::Wrap( + reinterpret_cast<const uint8_t*>(rhs.serialize_data.data()), rhs.serialize_data.size()); + + RETURN_IF_ERROR(client->merge(place, serialized_state)); + return Status::OK(); +} + +Status AggregatePythonUDAFData::write(BufferWritable& buf, int64_t place) const { + DCHECK(client != nullptr) << "Client must be set before calling write"; + + // Serialize state from Python server + std::shared_ptr<arrow::Buffer> serialized_state; + RETURN_IF_ERROR(client->serialize(place, &serialized_state)); + + // Cache in serialize_data and write to buffer + serialize_data.assign(reinterpret_cast<const char*>(serialized_state->data()), Review Comment: better use stringref to reduce the memcpy -- 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]
