feiniaofeiafei commented on code in PR #65172:
URL: https://github.com/apache/doris/pull/65172#discussion_r3533497562
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownAggThroughJoinOnPkFk.java:
##########
@@ -340,14 +348,73 @@ boolean isValid() {
continue;
}
if (currentBitset.isEmpty()) {
+ // Start with the first available join that is a
subset of target
+ BitSet entryBitset = entry.getKey();
+ // The join should cover at most what's in the target
+ BitSet outsideTarget = (BitSet) entryBitset.clone();
+ outsideTarget.andNot(bitSet);
+ if (!outsideTarget.isEmpty()) {
+ continue;
+ }
addJoin = true;
- currentBitset.or(entry.getKey());
+ currentBitset.or(entryBitset);
currentPlan = entry.getValue();
forbiddenJoin.add(entry.getKey());
} else if (currentBitset.intersects(entry.getKey())) {
+ // The new join shares leaves with the current
accumulated plan.
+ // We need to replace the shared child with
currentPlan.
+ BitSet entryBitset = entry.getKey();
+
+ // Ensure the new join doesn't introduce leaves
outside the target
+ BitSet outsideTarget = (BitSet) entryBitset.clone();
+ outsideTarget.andNot(bitSet);
+ if (!outsideTarget.isEmpty()) {
+ continue;
+ }
+
+ // Find which leaf is shared and which is new
+ BitSet sharedBits = (BitSet) entryBitset.clone();
+ sharedBits.and(currentBitset);
+
+ BitSet newBits = (BitSet) entryBitset.clone();
+ newBits.andNot(currentBitset);
+
+ if (sharedBits.isEmpty()) {
+ continue;
+ }
+
+ LogicalJoin<?, ?> entryJoin = entry.getValue();
+ int sharedLeaf = sharedBits.nextSetBit(0);
+
+ // Determine the new child: it could be a single leaf
or a sub-plan
+ Plan newChild;
+ if (newBits.cardinality() == 1) {
+ newChild = leaf.get(newBits.nextSetBit(0));
+ } else if (newBits.cardinality() == 0) {
+ // Both leaves already covered, skip
+ continue;
+ } else {
+ // Multiple new leaves, recursively construct
+ newChild = constructPlan(newBits, new
HashSet<>(forbiddenJoin));
+ if (newChild == null) {
+ continue;
+ }
+ }
+
+ // Determine which child of entryJoin covers the
shared leaf
+ int[] childLeaves = joinChildLeaves.get(entryBitset);
+ if (childLeaves != null && childLeaves[0] ==
sharedLeaf) {
+ // Left child of entryJoin covers the shared leaf,
+ // replace left child with currentPlan
+ currentPlan = entryJoin.withChildren(currentPlan,
newChild);
+ } else {
Review Comment:
check "childLeaves != null && childLeaves[1] == sharedLeaf" maybe better
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]