soumyakanti3578 commented on code in PR #6315:
URL: https://github.com/apache/hive/pull/6315#discussion_r2829830881


##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRelOptUtil.java:
##########
@@ -1107,7 +1107,14 @@ public static List<RelFieldCollation> 
getNewRelFieldCollations(
     Set<Integer> needed = new HashSet<>();
     for (RelFieldCollation fc : sortCollation.getFieldCollations()) {
       needed.add(fc.getFieldIndex());
-      final RexNode node = 
project.getProjects().get(map.getTarget(fc.getFieldIndex()));
+      int target = map.getTargetOpt(fc.getFieldIndex());
+      if (target == -1) {
+        // If there is no mapping for this field, we cannot push down the sort
+        LOG.debug("Missing target mapping for field index: {}. Cannot apply " +
+            "HiveProjectSort[Exchange]TransposeRule", fc.getFieldIndex());

Review Comment:
   With these configs, we get:
   ```
   2026-02-19T11:40:27,565 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.RuleEventLogger: call#13: Rule [HiveCardinalityPreservingJoinRule] 
produced [rel#48:HiveProject]
   2026-02-19T11:40:27,565 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.RuleEventLogger: call#13: Full plan for [rel#48:HiveProject]:
   HiveProject(col1=[$0])
     HiveSortExchange(distribution=[any], collation=[[0, 1]])
       HiveProject(col1=[$0], col2=[$1])
         HiveTableScan(table=[[default, test]], table:alias=[test])
   
   2026-02-19T11:40:27,565 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.RuleEventLogger: call#17: Apply rule 
[HiveCardinalityPreservingJoinRule] to [rel#43:HiveSortExchange]
   2026-02-19T11:40:27,565 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.RuleEventLogger: call#18: Apply rule 
[HiveCardinalityPreservingJoinRule] to [rel#41:HiveProject]
   2026-02-19T11:40:27,566 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.RuleEventLogger: call#19: Apply rule 
[HiveCardinalityPreservingJoinRule] to [rel#28:HiveTableScan]
   2026-02-19T11:40:27,566 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.RuleEventLogger: call#20: Apply rule [HiveProjectSearchExpandRule] to 
[rel#45:HiveProject]
   2026-02-19T11:40:27,566 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.RuleEventLogger: call#21: Apply rule [HiveProjectSearchExpandRule] to 
[rel#41:HiveProject]
   2026-02-19T11:40:27,566 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.RuleEventLogger: call#22: Apply rule 
[HiveProjectSortExchangeTransposeRule] to 
[rel#45:HiveProject,rel#43:HiveSortExchange]
   2026-02-19T11:40:27,567 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.HiveRelOptUtil: Missing target mapping for field index: 1. Cannot apply 
HiveProjectSort[Exchange]TransposeRule
   2026-02-19T11:40:27,568 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
AbstractRelOptPlanner.rule_execution_summary: Rule Attempts Info for HepPlanner
   2026-02-19T11:40:27,568 DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
AbstractRelOptPlanner.rule_execution_summary: 
   Rules                                                                   
Attempts           Time (us)
   HiveCardinalityPreservingJoinRule                                            
  4               9,111
   HiveProjectSearchExpandRule                                                  
  2                  64
   HiveProjectSortExchangeTransposeRule                                         
  1               1,703
   * Total                                                                      
  7              10,878
   ```
   
   The log says: `DEBUG [836e287e-99c3-4296-8183-264b55c327ad main] 
calcite.RuleEventLogger: call#22: Apply rule 
[HiveProjectSortExchangeTransposeRule] to 
[rel#45:HiveProject,rel#43:HiveSortExchange]`
   
   We can probably infer from the missing CBO plan after the line that the rule 
wasn't applied. So I am happy to remove the debug altogether 👍🏼 



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