dalingmeng commented on code in PR #238: URL: https://github.com/apache/paimon-cpp/pull/238#discussion_r3850585380
########## src/paimon/format/mosaic/mosaic_file_batch_reader.cpp: ########## @@ -0,0 +1,306 @@ +/* + * 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 "paimon/format/mosaic/mosaic_file_batch_reader.h" + +#include <algorithm> +#include <string> +#include <utility> +#include <vector> + +#include "arrow/api.h" +#include "arrow/c/bridge.h" +#include "fmt/format.h" +#include "paimon/common/metrics/metrics_impl.h" +#include "paimon/common/predicate/predicate_filter.h" +#include "paimon/common/utils/arrow/arrow_utils.h" +#include "paimon/common/utils/arrow/mem_utils.h" +#include "paimon/common/utils/arrow/status_utils.h" +#include "paimon/common/utils/checked_cast.h" +#include "paimon/common/utils/math.h" +#include "paimon/core/stats/simple_stats.h" +#include "paimon/core/stats/simple_stats_converter.h" +#include "paimon/format/mosaic/mosaic_stats.h" +#include "paimon/fs/file_system.h" + +namespace paimon::mosaic { + +MosaicFileBatchReader::MosaicFileBatchReader( + const std::shared_ptr<InputStream>& input, int32_t batch_size, + std::unique_ptr<MosaicInputContext> input_context, MosaicReaderHandle* reader, + const std::shared_ptr<arrow::Schema>& file_schema, uint32_t num_row_groups, uint64_t total_rows, + const std::shared_ptr<MemoryPool>& pool, const std::shared_ptr<arrow::MemoryPool>& arrow_pool) + : input_(input), + batch_size_(batch_size), + input_context_(std::move(input_context)), + reader_(reader), + file_schema_(file_schema), + num_row_groups_(num_row_groups), + total_rows_(total_rows), + pool_(pool), + arrow_pool_(arrow_pool), + metrics_(std::make_shared<MetricsImpl>()) {} + +Result<std::unique_ptr<MosaicFileBatchReader>> MosaicFileBatchReader::Create( + const std::shared_ptr<InputStream>& input, int32_t batch_size, + const std::shared_ptr<MemoryPool>& pool) { + if (input == nullptr || pool == nullptr || batch_size <= 0) { + return Status::Invalid( + "Mosaic reader requires non-null input and memory pool, and positive batch size"); + } + std::shared_ptr<arrow::MemoryPool> arrow_pool = GetArrowPool(pool); + PAIMON_ASSIGN_OR_RAISE(int64_t signed_length, input->Length()); + PAIMON_RETURN_NOT_OK(ValidateValueNonNegative(signed_length, "Mosaic input length")); + auto length = static_cast<uint64_t>(signed_length); + auto input_context = std::make_unique<MosaicInputContext>(input, length); + MosaicInputFile input_file = {}; + input_file.ctx = input_context.get(); + input_file.read_at_fn = MosaicInputContext::ReadAt; + input_file.length_fn = MosaicInputContext::Length; + MosaicReaderHandle* reader = mosaic_reader_open(input_file); + if (reader == nullptr) { + return MosaicFfiError("open Mosaic reader", input_context->GetCallbackStatus()); + } + + ::ArrowSchema ffi_schema = {}; + if (mosaic_reader_export_schema(reader, &ffi_schema) != 0) { + Status status = MosaicFfiError("read Mosaic schema", input_context->GetCallbackStatus()); + mosaic_reader_free(reader); + return status; + } + arrow::Result<std::shared_ptr<arrow::Schema>> schema_result = arrow::ImportSchema(&ffi_schema); + if (!schema_result.ok()) { + mosaic_reader_free(reader); + return ToPaimonStatus(schema_result.status()); + } + std::shared_ptr<arrow::Schema> file_schema = std::move(schema_result).ValueOrDie(); Review Comment: use schema_result.MoveValueUnsafe() to replace ValueOrDie better -- 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]
