diff --git ql/src/java/org/apache/hadoop/hive/ql/optimizer/optiq/RelOptHiveTable.java ql/src/java/org/apache/hadoop/hive/ql/optimizer/optiq/RelOptHiveTable.java index 8bddb30..bebfb38 100644 --- ql/src/java/org/apache/hadoop/hive/ql/optimizer/optiq/RelOptHiveTable.java +++ ql/src/java/org/apache/hadoop/hive/ql/optimizer/optiq/RelOptHiveTable.java @@ -28,6 +28,7 @@ import org.eigenbase.rel.TableAccessRel; import org.eigenbase.relopt.RelOptAbstractTable; import org.eigenbase.relopt.RelOptSchema; +import org.eigenbase.relopt.RelOptUtil.InputFinder; import org.eigenbase.reltype.RelDataType; import org.eigenbase.rex.RexNode; @@ -142,17 +143,27 @@ private String getColNamesForLogging(Set colLst) { public void computePartitionList(HiveConf conf, RexNode pruneNode) throws HiveException { partitionList = null; - if (pruneNode == null) { + + if (!m_hiveTblMetadata.isPartitioned()) { + // no partitions for unpartitioned tables. + return; + } + + if (pruneNode == null || InputFinder.bits(pruneNode).length() == 0 ) { + // there is no predicate on partitioning column, we need all partitions in this case. + partitionList = PartitionPruner.prune(m_hiveTblMetadata, null, conf, getName(), + new HashMap()); return; } + // We have valid pruning expressions, only retrieve qualifying partitions ExprNodeDesc pruneExpr = pruneNode.accept(new ExprNodeConverter(getName(), getRowType(), true)); partitionList = PartitionPruner.prune(m_hiveTblMetadata, pruneExpr, conf, getName(), new HashMap()); } - private void updateColStats(Set projIndxLst) { + private void updateColStats(Set projIndxLst) throws HiveException { List nonPartColNamesThatRqrStats = new ArrayList(); List nonPartColIndxsThatRqrStats = new ArrayList(); List partColNamesThatRqrStats = new ArrayList(); @@ -182,8 +193,15 @@ private void updateColStats(Set projIndxLst) { if (nonPartColNamesThatRqrStats.size() > 0) { List hiveColStats; - // 2.1 Handle the case where we are scanning only a set of partitions + if (null == partitionList) { + // We could be here either because its an unpartitioned table or because + // there are no pruning predicates on a partitioned table. If its latter, + // we need to fetch all partitions, so do that now. + computePartitionList(m_hiveConf, null); + } + if (partitionList == null) { + // 2.1 Handle the case for unpartitioned table. hiveColStats = StatsUtils.getTableColumnStats(m_hiveTblMetadata, m_hiveNonPartitionCols, nonPartColNamesThatRqrStats); @@ -202,7 +220,7 @@ private void updateColStats(Set projIndxLst) { colNamesFailedStats.addAll(setOfFiledCols); } } else { - // 2.2 Obtain col stats for full table scan + // 2.2 Obtain col stats for partitioned table. try { Statistics stats = StatsUtils.collectStatistics(m_hiveConf, partitionList, m_hiveTblMetadata, m_hiveNonPartitionCols, nonPartColNamesThatRqrStats, true, true); @@ -234,24 +252,7 @@ private void updateColStats(Set projIndxLst) { // TODO: Just using no of partitions for NDV is a gross approximation for // multi col partitions; Hack till HIVE-7392 gets fixed. if (colNamesFailedStats.isEmpty() && !partColNamesThatRqrStats.isEmpty()) { - if (m_numPartitions == null) { - try { - if (partitionList != null) { m_numPartitions = partitionList.getPartitions().size(); - } else { - m_numPartitions = Hive - .get() - .getPartitionNames(m_hiveTblMetadata.getDbName(), m_hiveTblMetadata.getTableName(), - (short) -1).size(); - } - } catch (HiveException e) { - String logMsg = "Could not get stats, number of Partitions for " - + m_hiveTblMetadata.getCompleteName(); - LOG.error(logMsg); - throw new RuntimeException(logMsg); - } - } - ColStatistics cStats = null; for (int i = 0; i < partColNamesThatRqrStats.size(); i++) { cStats = new ColStatistics(m_hiveTblMetadata.getTableName(), @@ -272,7 +273,7 @@ private void updateColStats(Set projIndxLst) { } } - public List getColStat(List projIndxLst) { + public List getColStat(List projIndxLst) throws HiveException { ImmutableList.Builder colStatsBldr = ImmutableList. builder(); if (projIndxLst != null) { diff --git ql/src/java/org/apache/hadoop/hive/ql/optimizer/optiq/rules/HivePartitionPrunerRule.java ql/src/java/org/apache/hadoop/hive/ql/optimizer/optiq/rules/HivePartitionPrunerRule.java index ea96524..6d73c19 100644 --- ql/src/java/org/apache/hadoop/hive/ql/optimizer/optiq/rules/HivePartitionPrunerRule.java +++ ql/src/java/org/apache/hadoop/hive/ql/optimizer/optiq/rules/HivePartitionPrunerRule.java @@ -25,7 +25,6 @@ import org.eigenbase.rel.FilterRelBase; import org.eigenbase.relopt.RelOptRule; import org.eigenbase.relopt.RelOptRuleCall; -import org.eigenbase.relopt.RelOptUtil.InputFinder; import org.eigenbase.rex.RexNode; import org.eigenbase.util.Pair; @@ -58,10 +57,6 @@ protected void perform(RelOptRuleCall call, FilterRelBase filter, remainingExpr = remainingExpr == null ? filter.getCluster().getRexBuilder() .makeLiteral(true) : remainingExpr; - if (partColExpr == null || InputFinder.bits(partColExpr).length() == 0 ) { - return; - } - try { hiveTable.computePartitionList(conf, partColExpr); } catch (HiveException he) {