Counter.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.jobs.joinorder.hypergraphv2.receiver;

import org.apache.doris.nereids.jobs.joinorder.hypergraphv2.HyperGraph;
import org.apache.doris.nereids.jobs.joinorder.hypergraphv2.bitmap.LongBitmap;
import org.apache.doris.nereids.jobs.joinorder.hypergraphv2.edge.Edge;
import org.apache.doris.nereids.memo.Group;

import com.google.common.base.Preconditions;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;

/**
 * The Receiver is used for cached the plan that has been emitted and build the new plan
 */
public class Counter extends AbstractReceiver {
    // limit define the max number of csg-cmp pair in this Receiver
    private final int limit;
    private final HyperGraph hyperGraph;
    private final HashMap<Long, Integer> counter = new HashMap<>();
    private final HashMap<Long, BitSet> usdEdges = new HashMap<>();
    private int emitCount = 0;

    public Counter(HyperGraph hyperGraph) {
        this.hyperGraph = hyperGraph;
        this.limit = Integer.MAX_VALUE;
    }

    public Counter(HyperGraph hyperGraph, int limit) {
        this.hyperGraph = hyperGraph;
        this.limit = limit;
    }

    /**
     * Emit a new plan from bottom to top
     *
     * @param left  the bitmap of left child tree
     * @param right the bitmap of the right child tree
     * @param edges the join operator
     * @return the left and the right can be connected by the edge
     */
    public EmitState emitCsgCmp(long left, long right, List<Edge> edges) {
        Preconditions.checkArgument(counter.containsKey(left));
        Preconditions.checkArgument(counter.containsKey(right));
        // Mirror PlanReceiver.emitCsgCmp: find missed edges first, reject the
        // pair when an enforced-order / unsafe alias edge is found, then count
        // the pair before the conflict-rule and alias-dependency checks (same
        // ordering as PlanReceiver, so GraphSimplifier's limit decision matches
        // what PlanReceiver actually emits).
        List<Edge> missingEdges = new ArrayList<>();
        if (!processMissedEdges(hyperGraph, usdEdges, left, right, edges, missingEdges)) {
            return EmitState.CONTINUE;
        }
        emitCount += 1;
        if (emitCount > limit) {
            return EmitState.FAIL;
        }
        edges.addAll(missingEdges);
        if (!checkConflictRule(left, right, edges)) {
            return EmitState.CONTINUE;
        }
        // Reject cross-bitmap alias layer dependencies, same as PlanReceiver.
        if (hyperGraph.hasUnresolvableAliasDependency(left, right)) {
            return EmitState.CONTINUE;
        }
        // track used edges for the joined bitmap, same as PlanReceiver
        BitSet usedEdgesBitmap = new BitSet();
        usedEdgesBitmap.or(usdEdges.get(left));
        usedEdgesBitmap.or(usdEdges.get(right));
        edges.forEach(edge -> usedEdgesBitmap.set(edge.getIndex()));
        usdEdges.put(LongBitmap.newBitmapUnion(left, right), usedEdgesBitmap);

        long bitmap = LongBitmap.newBitmapUnion(left, right);
        if (!counter.containsKey(bitmap)) {
            counter.put(bitmap, counter.get(left) * counter.get(right));
        } else {
            counter.put(bitmap, counter.get(bitmap) + counter.get(left) * counter.get(right));
        }
        return EmitState.SUCCESS;
    }

    public void addGroup(long bitmap, Group group) {
        counter.put(bitmap, 1);
        usdEdges.put(bitmap, new BitSet());
    }

    public boolean contain(long bitmap) {
        return counter.containsKey(bitmap);
    }

    public void reset() {
        this.counter.clear();
        this.usdEdges.clear();
        emitCount = 0;
    }

    public Group getBestPlan(long bitmap) {
        throw new RuntimeException("Counter does not support getBestPlan()");
    }

    public int getCount(long bitmap) {
        return counter.get(bitmap);
    }

    public HashMap<Long, Integer> getAllCount() {
        return counter;
    }

    public int getLimit() {
        return limit;
    }

    public int getEmitCount() {
        return emitCount;
    }
}