Index: ql/src/java/org/apache/hadoop/hive/ql/parse/QB.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/parse/QB.java (revision 1028618) +++ ql/src/java/org/apache/hadoop/hive/ql/parse/QB.java (working copy) @@ -18,7 +18,9 @@ package org.apache.hadoop.hive.ql.parse; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Set; import org.apache.commons.logging.Log; @@ -40,6 +42,7 @@ private int numSelDi = 0; private HashMap aliasToTabs; private HashMap aliasToSubq; + private List aliases; private QBParseInfo qbp; private QBMetaData qbm; private QBJoinTree qbjoin; @@ -65,6 +68,7 @@ public QB(String outer_id, String alias, boolean isSubQ) { aliasToTabs = new HashMap(); aliasToSubq = new HashMap(); + aliases = new ArrayList(); if (alias != null) { alias = alias.toLowerCase(); } @@ -110,6 +114,12 @@ aliasToSubq.put(alias.toLowerCase(), qbexpr); } + public void addAlias(String alias) { + if (!aliases.contains(alias.toLowerCase())) { + aliases.add(alias.toLowerCase()); + } + } + public String getId() { return id; } @@ -138,6 +148,10 @@ return aliasToTabs.keySet(); } + public List getAliases() { + return aliases; + } + public QBExpr getSubqForAlias(String alias) { return aliasToSubq.get(alias.toLowerCase()); } Index: ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java (revision 1028618) +++ ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java (working copy) @@ -422,6 +422,7 @@ // Insert this map into the stats String table_name = unescapeIdentifier(tabref.getChild(0).getText()); qb.setTabAlias(alias, table_name); + qb.addAlias(alias); qb.getParseInfo().setSrcForAlias(alias, tableTree); @@ -455,6 +456,7 @@ } // Insert this map into the stats qb.setSubqAlias(alias, qbexpr); + qb.addAlias(alias); unparseTranslator.addIdentifierTranslation((ASTNode) subq.getChild(1)); @@ -544,6 +546,7 @@ .getMsg(lateralView)); } qb.getParseInfo().addLateralViewForAlias(alias, lateralView); + qb.addAlias(alias); return alias; } @@ -694,6 +697,7 @@ // Case of analyze command String table_name = unescapeIdentifier(ast.getChild(0).getChild(0).getText()); qb.setTabAlias(table_name, table_name); + qb.addAlias(table_name); qb.getParseInfo().setIsAnalyzeCommand(true); // Allow analyze the whole table and dynamic partitions HiveConf.setVar(conf, HiveConf.ConfVars.DYNAMICPARTITIONINGMODE, "nonstrict"); @@ -1304,8 +1308,8 @@ @SuppressWarnings("nls") private Integer genColListRegex(String colRegex, String tabAlias, - String alias, ASTNode sel, ArrayList col_list, - RowResolver input, Integer pos, RowResolver output) + ASTNode sel, ArrayList col_list, + RowResolver input, Integer pos, RowResolver output, List aliases) throws SemanticException { // The table alias should exist @@ -1324,43 +1328,57 @@ StringBuilder replacementText = new StringBuilder(); int matched = 0; - // This is the tab.* case - // In this case add all the columns to the fieldList - // from the input schema - for (ColumnInfo colInfo : input.getColumnInfos()) { - String name = colInfo.getInternalName(); - String[] tmp = input.reverseLookup(name); - - // Skip the colinfos which are not for this particular alias - if (tabAlias != null && !tmp[0].equalsIgnoreCase(tabAlias)) { + // add empty string to the list of aliases. Some operators (ex. GroupBy) add + // ColumnInfos for table alias "". + if (!aliases.contains("")) { + aliases.add(""); + } + // For expr "*", aliases should be iterated in the order they are specified + // in the query. + for (String alias : aliases) { + HashMap fMap = input.getFieldMap(alias); + if (fMap == null) { continue; } + // For the tab.* case, add all the columns to the fieldList + // from the input schema + for (Map.Entry entry : fMap.entrySet()) { + ColumnInfo colInfo = entry.getValue(); + String name = colInfo.getInternalName(); + String[] tmp = input.reverseLookup(name); - if(colInfo.getIsVirtualCol() && colInfo.isHiddenVirtualCol()) { - continue; - } + // Skip the colinfos which are not for this particular alias + if (tabAlias != null && !tmp[0].equalsIgnoreCase(tabAlias)) { + continue; + } - // Not matching the regex? - if (!regex.matcher(tmp[1]).matches()) { - continue; - } + if (colInfo.getIsVirtualCol() && colInfo.isHiddenVirtualCol()) { + continue; + } - ExprNodeColumnDesc expr = new ExprNodeColumnDesc(colInfo.getType(), name, - colInfo.getTabAlias(), colInfo.getIsVirtualCol()); - col_list.add(expr); - output.put(tmp[0], tmp[1], - new ColumnInfo(getColumnInternalName(pos), colInfo.getType(), colInfo - .getTabAlias(), colInfo.getIsVirtualCol(), colInfo.isHiddenVirtualCol())); - pos = Integer.valueOf(pos.intValue() + 1); - matched++; + // Not matching the regex? + if (!regex.matcher(tmp[1]).matches()) { + continue; + } - if (unparseTranslator.isEnabled()) { - if (replacementText.length() > 0) { - replacementText.append(", "); + ExprNodeColumnDesc expr = new ExprNodeColumnDesc(colInfo.getType(), + name, colInfo.getTabAlias(), colInfo.getIsVirtualCol()); + col_list.add(expr); + output.put(tmp[0], tmp[1], + new ColumnInfo(getColumnInternalName(pos), colInfo.getType(), + colInfo.getTabAlias(), colInfo.getIsVirtualCol(), + colInfo.isHiddenVirtualCol())); + pos = Integer.valueOf(pos.intValue() + 1); + matched++; + + if (unparseTranslator.isEnabled()) { + if (replacementText.length() > 0) { + replacementText.append(", "); + } + replacementText.append(HiveUtils.unparseIdentifier(tmp[0])); + replacementText.append("."); + replacementText.append(HiveUtils.unparseIdentifier(tmp[1])); } - replacementText.append(HiveUtils.unparseIdentifier(tmp[0])); - replacementText.append("."); - replacementText.append(HiveUtils.unparseIdentifier(tmp[1])); } } if (matched == 0) { @@ -1881,6 +1899,7 @@ assert (selExprChild.getChildCount() == 1); udtfTableAlias = unescapeIdentifier(selExprChild.getChild(0) .getText()); + qb.addAlias(udtfTableAlias); unparseTranslator.addIdentifierTranslation((ASTNode) selExprChild .getChild(0)); break; @@ -1952,7 +1971,7 @@ if (expr.getType() == HiveParser.TOK_ALLCOLREF) { pos = genColListRegex(".*", expr.getChildCount() == 0 ? null : unescapeIdentifier(expr.getChild(0).getText().toLowerCase()), - alias, expr, col_list, inputRR, pos, out_rwsch); + expr, col_list, inputRR, pos, out_rwsch, qb.getAliases()); selectStar = true; } else if (expr.getType() == HiveParser.TOK_TABLE_OR_COL && !hasAsClause && !inputRR.getIsExprResolver() @@ -1961,7 +1980,7 @@ // This can only happen without AS clause // We don't allow this for ExprResolver - the Group By case pos = genColListRegex(unescapeIdentifier(expr.getChild(0).getText()), - null, alias, expr, col_list, inputRR, pos, out_rwsch); + null, expr, col_list, inputRR, pos, out_rwsch, qb.getAliases()); } else if (expr.getType() == HiveParser.DOT && expr.getChild(0).getType() == HiveParser.TOK_TABLE_OR_COL && inputRR.hasTableAlias(unescapeIdentifier(expr.getChild(0) @@ -1973,7 +1992,8 @@ // We don't allow this for ExprResolver - the Group By case pos = genColListRegex(unescapeIdentifier(expr.getChild(1).getText()), unescapeIdentifier(expr.getChild(0).getChild(0).getText() - .toLowerCase()), alias, expr, col_list, inputRR, pos, out_rwsch); + .toLowerCase()), expr, col_list, inputRR, pos, out_rwsch, + qb.getAliases()); } else { // Case when this is an expression ExprNodeDesc exp = genExprNodeDesc(expr, inputRR); @@ -5971,6 +5991,10 @@ QB blankQb = new QB(null, null, false); Operator udtfPath = genSelectPlan((ASTNode) lateralViewTree .getChild(0), blankQb, lvForward); + // add udtf aliases to QB + for (String udtfAlias : blankQb.getAliases()) { + qb.addAlias(udtfAlias); + } RowResolver udtfPathRR = opParseCtx.get(udtfPath).getRR(); // Merge the two into the lateral view join Index: ql/src/test/results/clientpositive/join_filters.q.out =================================================================== --- ql/src/test/results/clientpositive/join_filters.q.out (revision 1028618) +++ ql/src/test/results/clientpositive/join_filters.q.out (working copy) @@ -324,10 +324,10 @@ POSTHOOK: query: SELECT * from myinput1 a RIGHT OUTER JOIN myinput1 b ON (a.value=b.value AND a.key > 40 AND a.value > 50 AND a.key = a.value AND b.key > 40 AND b.value > 50 AND b.key = b.value) LEFT OUTER JOIN myinput1 c ON (b.key=c.key AND c.key > 40 AND c.value > 50 AND c.key = c.value AND b.key > 40 AND b.value > 50 AND b.key = b.value) POSTHOOK: type: QUERY POSTHOOK: Input: default@myinput1 -POSTHOOK: Output: file:/tmp/amarsri/hive_2010-09-21_00-47-42_197_5979333762407595753/-mr-10000 -NULL 40 NULL NULL NULL NULL -12 35 NULL NULL NULL NULL -48 NULL NULL NULL NULL NULL +POSTHOOK: Output: file:/tmp/amarsri/hive_2010-10-27_03-21-12_815_3004255014715798791/-mr-10000 +NULL NULL NULL 40 NULL NULL +NULL NULL 12 35 NULL NULL +NULL NULL 48 NULL NULL NULL 100 100 100 100 100 100 PREHOOK: query: SELECT * FROM myinput1 a LEFT OUTER JOIN myinput1 b RIGHT OUTER JOIN myinput1 c ON a.value = b.value and b.key = c.key AND a.key > 40 AND a.value > 50 AND a.key = a.value AND b.key > 40 AND b.value > 50 AND b.key = b.value AND c.key > 40 AND c.value > 50 AND c.key = c.value PREHOOK: type: QUERY Index: ql/src/test/results/clientpositive/join_reorder2.q.out =================================================================== --- ql/src/test/results/clientpositive/join_reorder2.q.out (revision 1028618) +++ ql/src/test/results/clientpositive/join_reorder2.q.out (working copy) @@ -378,6 +378,10 @@ outputColumnNames: _col0, _col1, _col4, _col5, _col8, _col9, _col12, _col13 Select Operator expressions: + expr: _col8 + type: string + expr: _col9 + type: string expr: _col0 type: string expr: _col1 @@ -386,10 +390,6 @@ type: string expr: _col5 type: string - expr: _col8 - type: string - expr: _col9 - type: string expr: _col12 type: string expr: _col13 @@ -426,4 +426,4 @@ POSTHOOK: Input: default@t3 POSTHOOK: Input: default@t4 POSTHOOK: Output: file:/tmp/jsichi/hive_2010-08-26_16-06-33_450_777508846599090366/-mr-10000 -2 22 2 12 2 12 2 12 +2 12 2 22 2 12 2 12 Index: ql/src/test/results/clientpositive/join_reorder3.q.out =================================================================== --- ql/src/test/results/clientpositive/join_reorder3.q.out (revision 1028618) +++ ql/src/test/results/clientpositive/join_reorder3.q.out (working copy) @@ -378,6 +378,10 @@ outputColumnNames: _col0, _col1, _col4, _col5, _col8, _col9, _col12, _col13 Select Operator expressions: + expr: _col8 + type: string + expr: _col9 + type: string expr: _col0 type: string expr: _col1 @@ -386,10 +390,6 @@ type: string expr: _col5 type: string - expr: _col8 - type: string - expr: _col9 - type: string expr: _col12 type: string expr: _col13 @@ -426,4 +426,4 @@ POSTHOOK: Input: default@t3 POSTHOOK: Input: default@t4 POSTHOOK: Output: file:/tmp/jsichi/hive_2010-08-26_16-06-51_531_8070318925118385343/-mr-10000 -2 22 2 12 2 12 2 12 +2 12 2 22 2 12 2 12