diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagateProcFactory.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagateProcFactory.java index c965742c53..acb9788baa 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagateProcFactory.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagateProcFactory.java @@ -1205,11 +1205,17 @@ public Object process(Node nd, Stack stack, NodeProcessorCtx ctx, Object.. if (HiveConf.getPositionFromInternalName(colName) == -1) { // if its not an internal name, this is what we want. ((ExprNodeConstantDesc)newCol).setFoldedFromCol(colName); + // See if we can set the tabAlias this was folded from as well. + ExprNodeDesc colExpr = colList.get(i); + if (colExpr instanceof ExprNodeColumnDesc) { + ((ExprNodeConstantDesc)newCol).setFoldedFromTab(((ExprNodeColumnDesc) colExpr).getTabAlias()); + } } else { // If it was internal column, lets try to get name from columnExprMap ExprNodeDesc desc = columnExprMap.get(colName); if (desc instanceof ExprNodeConstantDesc) { ((ExprNodeConstantDesc)newCol).setFoldedFromCol(((ExprNodeConstantDesc)desc).getFoldedFromCol()); + ((ExprNodeConstantDesc)newCol).setFoldedFromTab(((ExprNodeConstantDesc)desc).getFoldedFromTab()); } } } @@ -1370,7 +1376,7 @@ public Object process(Node nd, Stack stack, NodeProcessorCtx ctx, Object.. for (ExprNodeDesc desc : rsDesc.getKeyCols()) { ExprNodeDesc newDesc = foldExpr(desc, constants, cppCtx, op, 0, false); if (newDesc != desc && desc instanceof ExprNodeColumnDesc && newDesc instanceof ExprNodeConstantDesc) { - ((ExprNodeConstantDesc)newDesc).setFoldedFromCol(((ExprNodeColumnDesc)desc).getColumn()); + ((ExprNodeConstantDesc)newDesc).setFoldedTabCol((ExprNodeColumnDesc)desc); } newKeyEpxrs.add(newDesc); } @@ -1382,7 +1388,7 @@ public Object process(Node nd, Stack stack, NodeProcessorCtx ctx, Object.. ExprNodeDesc expr = foldExpr(desc, constants, cppCtx, op, 0, false); if (expr != desc && desc instanceof ExprNodeColumnDesc && expr instanceof ExprNodeConstantDesc) { - ((ExprNodeConstantDesc) expr).setFoldedFromCol(((ExprNodeColumnDesc) desc).getColumn()); + ((ExprNodeConstantDesc) expr).setFoldedTabCol((ExprNodeColumnDesc) desc); } newPartExprs.add(expr); } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/TypeCheckProcFactory.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/TypeCheckProcFactory.java index 7e804e3c2d..0c81986c84 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/parse/TypeCheckProcFactory.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/TypeCheckProcFactory.java @@ -769,6 +769,7 @@ private static ExprNodeConstantDesc toPrimitiveConstDesc(ColumnInfo colInfo, Obj ExprNodeConstantDesc constantExpr = new ExprNodeConstantDesc(colInfo.getType(), poi.getPrimitiveJavaObject(constant)); constantExpr.setFoldedFromCol(colInfo.getInternalName()); + constantExpr.setFoldedFromTab(colInfo.getTabAlias()); return constantExpr; } @@ -783,6 +784,7 @@ private static ExprNodeConstantDesc toListConstDesc(ColumnInfo colInfo, ObjectIn ExprNodeConstantDesc constantExpr = new ExprNodeConstantDesc(colInfo.getType(), constant); constantExpr.setFoldedFromCol(colInfo.getInternalName()); + constantExpr.setFoldedFromTab(colInfo.getTabAlias()); return constantExpr; } @@ -798,6 +800,7 @@ private static ExprNodeConstantDesc toMapConstDesc(ColumnInfo colInfo, ObjectIns ExprNodeConstantDesc constantExpr = new ExprNodeConstantDesc(colInfo.getType(), constant); constantExpr.setFoldedFromCol(colInfo.getInternalName()); + constantExpr.setFoldedFromTab(colInfo.getTabAlias()); return constantExpr; } @@ -813,6 +816,7 @@ private static ExprNodeConstantDesc toStructConstDesc(ColumnInfo colInfo, Object ExprNodeConstantDesc constantExpr = new ExprNodeConstantDesc(colInfo.getType(), constant); constantExpr.setFoldedFromCol(colInfo.getInternalName()); + constantExpr.setFoldedFromTab(colInfo.getTabAlias()); return constantExpr; } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeConstantDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeConstantDesc.java index 00aff8e7eb..e3324ca220 100755 --- a/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeConstantDesc.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeConstantDesc.java @@ -43,9 +43,17 @@ // If this constant was created while doing constant folding, foldedFromCol holds the name of // original column from which it was folded. private transient String foldedFromCol; + // If this constant was created while doing constant folding, foldedFromTab holds the name of + // the original tabAlias from which it was folded. + private transient String foldedFromTab; // string representation of folding constant. private transient String foldedFromVal; + public void setFoldedTabCol(ExprNodeColumnDesc colDesc) { + setFoldedFromTab(colDesc.getTabAlias()); + setFoldedFromCol(colDesc.getColumn()); + } + public ExprNodeConstantDesc setFoldedFromVal(String foldedFromVal) { this.foldedFromVal = foldedFromVal; return this; @@ -63,6 +71,14 @@ public void setFoldedFromCol(String foldedFromCol) { this.foldedFromCol = foldedFromCol; } + public String getFoldedFromTab() { + return foldedFromTab; + } + + public void setFoldedFromTab(String foldedFromTab) { + this.foldedFromTab = foldedFromTab; + } + public ExprNodeConstantDesc() { } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java b/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java index 80ce787bba..29ceb7f981 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java @@ -18,6 +18,8 @@ package org.apache.hadoop.hive.ql.plan; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.apache.hadoop.hive.ql.exec.ColumnInfo; import org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator; import org.apache.hadoop.hive.ql.exec.ExprNodeEvaluatorFactory; @@ -55,6 +57,8 @@ public class ExprNodeDescUtils { + protected static final Logger LOG = LoggerFactory.getLogger(ExprNodeDescUtils.class); + public static int indexOf(ExprNodeDesc origin, List sources) { for (int i = 0; i < sources.size(); i++) { if (origin.isSame(sources.get(i))) { @@ -448,8 +452,13 @@ public static ExprNodeDesc resolveJoinKeysAsRSColumns(ExprNodeDesc source, Opera // Join key expression is likely some expression involving functions/operators, so there // is no actual table column for this. But the ReduceSink operator should still have an // output column corresponding to this expression, using the columnInternalName. - // TODO: does tableAlias matter for this kind of expression? - return new ExprNodeColumnDesc(source.getTypeInfo(), columnInternalName, "", false); + String tabAlias = ""; + // HIVE-21746: Set tabAlias when possible, such as for constant folded column + // that has foldedFromTab info. + if (source instanceof ExprNodeConstantDesc) { + tabAlias = ((ExprNodeConstantDesc) source).getFoldedFromTab(); + } + return new ExprNodeColumnDesc(source.getTypeInfo(), columnInternalName, tabAlias, false); } } }