MutableState.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.nereids.util;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/** MutableState */
public interface MutableState {
    String KEY_GROUP = "group";
    String KEY_FRAGMENT = "fragment";
    String KEY_PARENT = "parent";
    String KEY_RF_JUMP = "rf-jump";
    String KEY_PUSH_TOPN_TO_AGG = "pushTopnToAgg";
    /** hbo fingerprint attached at planning time for explain printing */
    String KEY_HBO_FP = "hbo-fingerprint";
    /**
     * hbo constant agnostic fingerprint attached at planning time for explain printing; only filter
     * nodes carry both forms (exact + shape), join / aggregation carry the constant agnostic one
     */
    String KEY_HBO_FP_NO_LITERAL = "hbo-fingerprint-no-literal";
    /** hbo simplified struct info canonical string attached at planning time for explain printing */
    String KEY_HBO_STRUCT = "hbo-struct";
    /**
     * hbo struct info of the same node in the constant agnostic form, built by the group traversal
     * (never by folding the literal carrying form of {@link #KEY_HBO_STRUCT} as text)
     */
    String KEY_HBO_STRUCT_NO_LITERAL = "hbo-struct-no-literal";
    /**
     * why no struct info was attached to this node, when it was refused by a limit instead of being
     * unsupported (e.g. its sub tree reads more scans than {@code hbo_max_scans_per_group}); printed
     * by the explain annotation so a missing fingerprint can be told from a broken one
     */
    String KEY_HBO_STRUCT_SKIP = "hbo-struct-skip";
    /** canonical string of the join equality conditions (join nodes, for HBO SET EXPANSION) */
    String KEY_HBO_COND = "hbo-cond";
    /** fingerprint of the join equality conditions (join nodes, the expansion injection key) */
    String KEY_HBO_COND_FP = "hbo-cond-fp";
    /** applied injected join expansion, e.g. {@code exp=200x} */
    String KEY_HBO_EXPANSION = "hbo-expansion";

    <T> Optional<T> get(String key);

    MutableState set(String key, Object value);

    /** EmptyMutableState */
    class EmptyMutableState implements MutableState {
        public static final EmptyMutableState INSTANCE = new EmptyMutableState();

        private EmptyMutableState() {}

        @Override
        public <T> Optional<T> get(String key) {
            return Optional.empty();
        }

        @Override
        public MutableState set(String key, Object value) {
            MultiMutableState state = new MultiMutableState();
            state.set(key, value);
            return state;
        }

        @Override
        public String toString() {
            return "[]";
        }
    }

    /** MultiMutableState */
    class MultiMutableState implements MutableState {
        private final Map<String, Object> states = new LinkedHashMap<>();

        @Override
        public <T> Optional<T> get(String key) {
            return (Optional<T>) Optional.ofNullable(states.get(key));
        }

        @Override
        public MutableState set(String key, Object value) {
            states.put(key, value);
            return this;
        }

        @Override
        public String toString() {
            return states.entrySet()
                    .stream()
                    .map(kv -> kv.getKey() + ": " + (kv.getValue() == null ? "null" : kv.getValue()))
                    .collect(Collectors.joining(", ", "[", "]"));
        }
    }
}