lszskye commented on code in PR #594: URL: https://github.com/apache/paimon-cpp/pull/594#discussion_r4678620729
########## src/paimon/core/append/append_file_compaction_planner.cpp: ########## @@ -0,0 +1,441 @@ +/* + * 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/append/append_file_compaction_planner.h" + +#include <algorithm> +#include <iterator> +#include <limits> +#include <unordered_map> +#include <utility> + +#include "fmt/format.h" +#include "paimon/common/file_index/file_index_format_utils.h" +#include "paimon/common/types/data_field.h" +#include "paimon/common/utils/path_util.h" +#include "paimon/common/utils/scope_guard.h" +#include "paimon/common/utils/string_utils.h" +#include "paimon/core/core_options.h" +#include "paimon/core/deletionvectors/deletion_vector.h" +#include "paimon/core/deletionvectors/deletion_vectors_index_file.h" +#include "paimon/core/index/index_file_handler.h" +#include "paimon/core/io/data_file_meta.h" +#include "paimon/core/io/data_file_path_factory.h" +#include "paimon/core/manifest/index_manifest_file.h" +#include "paimon/core/manifest/manifest_file.h" +#include "paimon/core/manifest/manifest_list.h" +#include "paimon/core/operation/append_only_file_store_scan.h" +#include "paimon/core/schema/schema_manager.h" +#include "paimon/core/schema/table_schema.h" +#include "paimon/core/table/bucket_mode.h" +#include "paimon/core/utils/branch_manager.h" +#include "paimon/core/utils/field_mapping.h" +#include "paimon/core/utils/file_store_path_factory.h" +#include "paimon/core/utils/index_file_path_factories.h" +#include "paimon/core/utils/snapshot_manager.h" +#include "paimon/defs.h" +#include "paimon/executor.h" +#include "paimon/file_index/file_index_format.h" +#include "paimon/fs/file_system.h" +#include "paimon/io/byte_array_input_stream.h" +#include "paimon/memory/bytes.h" +#include "paimon/memory/memory_pool.h" +#include "paimon/scan_context.h" +#include "paimon/status.h" + +namespace paimon { +namespace { + +// Keep the pool alive until the last embedded-index reference releases its bytes. +struct EmbeddedIndexOwner { + std::shared_ptr<MemoryPool> pool; + std::shared_ptr<Bytes> bytes; +}; + +Result<std::optional<FileIndexMeta>> ReadFileIndexMeta( + const std::shared_ptr<DataFileMeta>& file, + const std::shared_ptr<DataFilePathFactory>& path_factory, + const std::shared_ptr<FileSystem>& file_system, const std::shared_ptr<MemoryPool>& pool) { + FileIndexMeta meta; + std::shared_ptr<InputStream> input; + if (file->embedded_index) { + auto owner = + std::make_shared<EmbeddedIndexOwner>(EmbeddedIndexOwner{pool, file->embedded_index}); + meta.embedded_index = std::shared_ptr<const Bytes>(owner, owner->bytes.get()); + input = std::make_shared<ByteArrayInputStream>(meta.embedded_index->data(), + meta.embedded_index->size()); + } else { + for (const auto& extra_file : file->extra_files) { + if (!extra_file || !StringUtils::EndsWith(extra_file.value(), + DataFilePathFactory::INDEX_PATH_SUFFIX)) { + continue; + } + if (meta.index_file_path) { + return Status::Invalid("multiple file index containers for ", file->file_name); + } + meta.index_file_path = path_factory->ToAlignedPath(extra_file.value(), file); + } + if (!meta.index_file_path) { + return std::optional<FileIndexMeta>(); + } + PAIMON_ASSIGN_OR_RAISE(input, file_system->Open(meta.index_file_path.value())); + } + ScopeGuard close_guard([&input]() { (void)input->Close(); }); + PAIMON_ASSIGN_OR_RAISE(FileIndexFormatUtils::FileIndexEntries entries, + FileIndexFormatUtils::ReadIndexEntries(input, pool)); + PAIMON_ASSIGN_OR_RAISE(int64_t container_length, input->Length()); + for (const auto& [column, indexes] : entries) { + auto& ranges = meta.indexes[column]; + for (const auto& [type, range] : indexes) { + const auto& [offset, length] = range; + if (offset == FileIndexFormat::EMPTY_INDEX_FLAG) { + if (length != 0) { + return Status::Invalid("invalid empty index range for ", file->file_name); + } + } else if (offset < 0 || length < 0 || offset > container_length || + length > container_length - offset) { + return Status::Invalid("index range is outside container for ", file->file_name); + } + ranges.emplace(type, FileIndexRange{range.first, range.second}); + } + } + close_guard.Release(); + PAIMON_RETURN_NOT_OK(input->Close()); + return std::optional<FileIndexMeta>(std::move(meta)); +} + +Status BuildRowIdMapping(const std::shared_ptr<DeletionVector>& dv, int64_t target_start, + FileCompactionInfo* info) { + if (info->source_row_count < 0) { + return Status::Invalid("negative row count for ", info->file_name); + } + int64_t deleted_count = 0; + if (dv) { + PAIMON_ASSIGN_OR_RAISE(deleted_count, dv->GetCardinality()); + } + if (deleted_count < 0 || deleted_count > info->source_row_count) { + return Status::Invalid("invalid deletion vector cardinality for ", info->file_name); + } + info->live_row_count = info->source_row_count - deleted_count; + if (info->live_row_count > std::numeric_limits<int64_t>::max() - target_start) { Review Comment: This check only guards against int64_t overflow, but existing file indexes use signed 32-bit row counts. Multiple individually valid input files can therefore produce an output exceeding INT32_MAX rows. -- 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]
