PaimonPartitionInfo.java

// 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.

package org.apache.doris.datasource.paimon;

import org.apache.doris.catalog.PartitionItem;
import org.apache.doris.datasource.metacache.MetaCacheWeightUtils;

import org.apache.paimon.partition.Partition;

import java.util.Collections;
import java.util.Map;

/**
 * Snapshot-scoped Paimon partition metadata used by Doris.
 *
 * <p>The map key is the physical partition name generated by Paimon. If that name cannot
 * uniquely represent every typed partition value, the complete result is {@link #UNPRUNABLE}.
 */
public class PaimonPartitionInfo {
    /**
     * Whether the complete snapshot-scoped Paimon partition set can be represented as Doris
     * PartitionItems. UNPRUNABLE keeps queries correct by leaving scan planning to Paimon.
     *
     * <p>This capability state can be removed only after Doris supports every legal Paimon
     * partition type and partition conversion is guaranteed not to fail.
     */
    public enum PruningStatus {
        PRUNABLE,
        UNPRUNABLE
    }

    public static final PaimonPartitionInfo EMPTY = new PaimonPartitionInfo(PruningStatus.PRUNABLE);
    public static final PaimonPartitionInfo UNPRUNABLE = new PaimonPartitionInfo(PruningStatus.UNPRUNABLE);

    private final PruningStatus pruningStatus;
    private final Map<String, PartitionItem> nameToPartitionItem;
    private final Map<String, Partition> nameToPartition;
    private final long retainedPayloadBytes;

    private PaimonPartitionInfo(PruningStatus pruningStatus) {
        this.pruningStatus = pruningStatus;
        this.nameToPartitionItem = Collections.emptyMap();
        this.nameToPartition = Collections.emptyMap();
        this.retainedPayloadBytes = 0L;
    }

    public PaimonPartitionInfo(Map<String, PartitionItem> nameToPartitionItem,
            Map<String, Partition> nameToPartition) {
        this(nameToPartitionItem, nameToPartition, retainedPayloadBytes(nameToPartition));
    }

    public PaimonPartitionInfo(Map<String, PartitionItem> nameToPartitionItem,
            Map<String, Partition> nameToPartition, long retainedPayloadBytes) {
        this.pruningStatus = PruningStatus.PRUNABLE;
        this.nameToPartitionItem = nameToPartitionItem;
        this.nameToPartition = nameToPartition;
        this.retainedPayloadBytes = retainedPayloadBytes;
    }

    public Map<String, PartitionItem> getNameToPartitionItem() {
        return nameToPartitionItem;
    }

    public Map<String, Partition> getNameToPartition() {
        return nameToPartition;
    }

    public PruningStatus getPruningStatus() {
        return pruningStatus;
    }

    public long getRetainedPayloadBytes() {
        return retainedPayloadBytes;
    }

    static long addRetainedStringPayload(long bytes, String value) {
        return addString(bytes, value);
    }

    private static long retainedPayloadBytes(Map<String, Partition> partitions) {
        if (partitions == null) {
            return 0L;
        }
        long bytes = 0L;
        for (Map.Entry<String, Partition> entry : partitions.entrySet()) {
            bytes = addString(bytes, entry.getKey());
            Partition partition = entry.getValue();
            if (partition == null) {
                continue;
            }
            bytes = addStrings(bytes, partition.spec());
            bytes = addString(bytes, partition.createdBy());
            bytes = addString(bytes, partition.updatedBy());
            bytes = addStrings(bytes, partition.options());
        }
        return bytes;
    }

    private static long addStrings(long bytes, Map<String, String> values) {
        if (values == null) {
            return bytes;
        }
        for (Map.Entry<String, String> entry : values.entrySet()) {
            bytes = addString(bytes, entry.getKey());
            bytes = addString(bytes, entry.getValue());
        }
        return bytes;
    }

    private static long addString(long bytes, String value) {
        return MetaCacheWeightUtils.saturatedAdd(
                bytes, MetaCacheWeightUtils.estimatedStringBytes(value));
    }
}