seawinde commented on code in PR #31800:
URL: https://github.com/apache/doris/pull/31800#discussion_r1513776993


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/memo/StructInfoMap.java:
##########
@@ -0,0 +1,128 @@
+// 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.memo;
+
+import org.apache.doris.nereids.rules.exploration.mv.StructInfo;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
+
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+
+/**
+ * Representation for group in cascades optimizer.
+ */
+public class StructInfoMap {
+    private final Map<BitSet, GroupExpression> groupExpressionMap = new 
HashMap<>();
+    private final Map<BitSet, StructInfo> infoMap = new HashMap<>();
+
+    /**
+     * get struct info according to table map
+     * @param mvTableMap the original table map
+     * @param foldTableMap the fold table map
+     * @param group the group that the mv matched
+     * @return struct info or null if not found
+     */
+    public @Nullable StructInfo getStructInfo(BitSet mvTableMap, BitSet 
foldTableMap, Group group) {
+        if (!infoMap.containsKey(mvTableMap)) {
+            if ((groupExpressionMap.containsKey(foldTableMap) || 
groupExpressionMap.isEmpty())
+                    && !groupExpressionMap.containsKey(mvTableMap)) {
+                refresh(group);
+            }
+            if (groupExpressionMap.containsKey(mvTableMap)) {
+                StructInfo structInfo = 
constructStructInfo(groupExpressionMap.get(mvTableMap));
+                infoMap.put(mvTableMap, structInfo);
+            }
+        }
+
+        return infoMap.get(mvTableMap);
+    }
+
+    public Set<BitSet> getTableMaps() {
+        return groupExpressionMap.keySet();
+    }
+
+    private StructInfo constructStructInfo(GroupExpression groupExpression) {
+        throw new RuntimeException("has not been implemented for" + 
groupExpression);
+    }
+
+    /**
+     * refresh group expression map
+     * @param group the root group
+     */
+    public void refresh(Group group) {
+        List<Set<BitSet>> childrenTableMap = new ArrayList<>();
+        Set<Group> refreshedGroup = new HashSet<>();
+        for (GroupExpression groupExpression : group.getLogicalExpressions()) {
+            if (groupExpression.children().isEmpty()) {
+                groupExpressionMap.put(constructLeaf(groupExpression), 
groupExpression);
+                continue;
+            }
+            for (Group child : groupExpression.children()) {
+                if (!refreshedGroup.contains(child)) {
+                    StructInfoMap childStructInfoMap = 
child.getstructInfoMap();
+                    childStructInfoMap.refresh(child);
+                }
+                refreshedGroup.add(child);
+                childrenTableMap.add(child.getstructInfoMap().getTableMaps());
+            }
+            Set<BitSet> bitSets = cartesianProduct(childrenTableMap);
+            for (BitSet bitSet : bitSets) {
+                groupExpressionMap.put(bitSet, groupExpression);
+            }
+        }
+    }
+
+    private BitSet constructLeaf(GroupExpression groupExpression) {
+        Plan plan = groupExpression.getPlan();
+        BitSet tableMap = new BitSet();
+        if (plan instanceof LogicalCatalogRelation) {
+            // TODO: Bitmap is not compatible with long, use tree map instead
+            tableMap.set((int) ((LogicalCatalogRelation) 
plan).getTable().getId());
+        }
+        // one row relation / CTE consumer
+        return tableMap;
+    }
+
+    private Set<BitSet> cartesianProduct(List<Set<BitSet>> childrenTableMap) {
+        Set<BitSet> result = new HashSet<>();
+        cartesianProductHelper(childrenTableMap, 0, new BitSet(), result);
+        return result;
+    }
+
+    private void cartesianProductHelper(List<Set<BitSet>> childrenTableMap, 
int currentIndex,
+            BitSet currentTableMap, Set<BitSet> res) {

Review Comment:
   guava has `com.google.common.collect.Sets#cartesianProduct(java.util.Set<? 
extends B>...)` maybe helpful



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to