| /* | |
| * 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. | |
| */ | |
| #pragma once | |
| #include <map> | |
| #include <memory> | |
| #include <string> | |
| #include <utility> | |
| #include <vector> | |
| #include "arrow/type.h" | |
| #include "paimon/common/utils/data_converter_utils.h" | |
| #include "paimon/result.h" | |
| #include "paimon/type_fwd.h" | |
| namespace paimon { | |
| class BinaryRow; | |
| class MemoryPool; | |
| // TODO(yonghao.fyh): rethink naming of converter/reconverter | |
| struct PartitionConverter { | |
| PartitionConverter(const std::string& _partition_key, | |
| typename DataConverterUtils::StrToBinaryRowConverter&& _converter, | |
| typename DataConverterUtils::BinaryRowFieldToStrConverter&& _reconverter) | |
| : partition_key(_partition_key), converter(_converter), reconverter(_reconverter) {} | |
| std::string partition_key; | |
| typename DataConverterUtils::StrToBinaryRowConverter converter; | |
| typename DataConverterUtils::BinaryRowFieldToStrConverter reconverter; | |
| }; | |
| /// PartitionComputer for `BinaryRow`. | |
| class BinaryRowPartitionComputer { | |
| public: | |
| static Result<std::unique_ptr<BinaryRowPartitionComputer>> Create( | |
| const std::vector<std::string>& partition_keys, | |
| const std::shared_ptr<arrow::Schema>& schema, const std::string& default_part_value, | |
| bool legacy_partition_name_enabled, const std::shared_ptr<MemoryPool>& memory_pool); | |
| Result<BinaryRow> ToBinaryRow(const std::map<std::string, std::string>& partition) const; | |
| /// Converts a partial `partition` into a `BinaryRow`: a key it names is read into that | |
| /// column's type, a key it leaves out is null, and a value equal to the default partition name | |
| /// is the null partition. | |
| /// | |
| /// Nothing is rendered back out, unlike `NormalizePartitionSpec()`, so two values a partition | |
| /// directory's name cannot tell apart - a blank one and the null partition - stay distinct. | |
| /// This is how a partition is compared by what its column holds, which is what Java tests a | |
| /// `PartitionPredicate` against. | |
| Result<BinaryRow> ToPartialBinaryRow(const std::map<std::string, std::string>& partition) const; | |
| Result<std::map<std::string, std::string>> NormalizePartitionSpec( | |
| const std::map<std::string, std::string>& partition) const; | |
| Result<std::vector<std::pair<std::string, std::string>>> GeneratePartitionVector( | |
| const BinaryRow& partition) const; | |
| const std::vector<std::string>& GetPartitionKeys() const { | |
| return partition_keys_; | |
| } | |
| static Result<std::string> PartToSimpleString( | |
| const std::shared_ptr<arrow::Schema>& partition_type, const BinaryRow& partition, | |
| const std::string& delimiter, int32_t max_length, | |
| bool legacy_partition_name_enabled = true); | |
| private: | |
| BinaryRowPartitionComputer(const std::vector<std::string>& partition_keys, | |
| const std::shared_ptr<arrow::Schema>& schema, | |
| const std::string& default_part_value, | |
| const std::vector<PartitionConverter>& partition_converters, | |
| const std::shared_ptr<MemoryPool>& memory_pool); | |
| /// A non-null `included_fields` enables partial partitions and records present fields. | |
| Result<BinaryRow> ConvertToBinaryRow(const std::map<std::string, std::string>& partition, | |
| std::vector<bool>* included_fields) const; | |
| /// Rejects a key that is no partition key of this table, so a typo matches nothing rather than | |
| /// being ignored. | |
| Status CheckPartitionKeys(const std::map<std::string, std::string>& partition) const; | |
| static Result<arrow::Type::type> GetTypeFromArrowSchema( | |
| const std::shared_ptr<arrow::Schema>& schema, const std::string& field_name); | |
| std::shared_ptr<MemoryPool> memory_pool_; | |
| std::vector<std::string> partition_keys_; | |
| std::shared_ptr<arrow::Schema> schema_; | |
| std::string default_part_value_; | |
| std::vector<PartitionConverter> partition_converters_; | |
| }; | |
| } // namespace paimon |