nooneuse commented on code in PR #66307:
URL: https://github.com/apache/doris/pull/66307#discussion_r3703690768


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/LogicalOlapScanToPhysicalOlapScan.java:
##########
@@ -132,12 +140,63 @@ public static DistributionSpec 
convertDistribution(LogicalOlapScan olapScan) {
                         }
                     }
                 }
-                return new DistributionSpecHash(hashColumns, 
ShuffleType.NATURAL, olapScan.getTable().getId(),
-                        olapScan.getSelectedIndexId(), 
Sets.newLinkedHashSet(olapScan.getSelectedPartitionIds()));
+                return createNaturalHashSpec(olapScan, hashDistributionInfo, 
hashColumns, output);
             }
         } else {
             // RandomDistributionInfo
             return DistributionSpecStorageAny.INSTANCE;
         }
     }
+
+    private static DistributionSpecHash createNaturalHashSpec(LogicalOlapScan 
olapScan,
+            HashDistributionInfo hashDistributionInfo, List<ExprId> 
hashColumns, List<Slot> output) {
+        return new DistributionSpecHash(hashColumns, ShuffleType.NATURAL, 
olapScan.getTable().getId(),
+                olapScan.getSelectedIndexId(), 
Sets.newLinkedHashSet(olapScan.getSelectedPartitionIds()),
+                buildDistributionMappings(olapScan.getTable(), 
hashDistributionInfo, output));
+    }
+
+    private static List<DistributionMapping> 
buildDistributionMappings(OlapTable table,
+            HashDistributionInfo hashDistributionInfo, List<Slot> output) {
+        ConnectContext context = ConnectContext.get();
+        if (context == null || 
!context.getSessionVariable().isEnableColocateMappingConstraint()) {
+            return ImmutableList.of();
+        }
+
+        Map<String, ExprId> columnExprIds = new HashMap<>();
+        for (Slot slot : output) {
+            SlotReference slotReference = (SlotReference) slot;
+            slotReference.getOriginalColumn().ifPresent(
+                    column -> 
columnExprIds.put(column.getName().toLowerCase(), slot.getExprId()));
+        }
+        Map<String, Integer> distributionIndices = new HashMap<>();
+        List<Column> distributionColumns = 
hashDistributionInfo.getDistributionColumns();
+        for (int i = 0; i < distributionColumns.size(); i++) {
+            
distributionIndices.put(distributionColumns.get(i).getName().toLowerCase(), i);
+        }
+
+        TableNameInfo tableNameInfo = 
TableNameInfoUtils.fromTableOrNull(table);
+        ImmutableList.Builder<DistributionMapping> mappings = 
ImmutableList.builder();
+        for (DistributionMappingConstraint constraint : 
Env.getCurrentEnv().getConstraintManager()
+                .getDistributionMappingConstraints(tableNameInfo)) {
+            ImmutableList.Builder<ExprId> determinants = 
ImmutableList.builder();
+            boolean allDeterminantsAvailable = true;
+            for (String column : constraint.getDeterminantColumnNames()) {
+                ExprId exprId = columnExprIds.get(column.toLowerCase());
+                if (exprId == null) {
+                    allDeterminantsAvailable = false;
+                    break;
+                }
+                determinants.add(exprId);
+            }
+            if (!allDeterminantsAvailable) {
+                continue;
+            }
+            ImmutableList<Integer> targetIndices = 
constraint.getDistributionColumnNames().stream()

Review Comment:
   alright, I'll fix it.(rename db\column)



-- 
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]

Reply via email to