PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ppr/OpProcFactory.java

#
Java | 143 lines | 76 code | 21 blank | 46 comment | 9 complexity | 4cf9738a30aa8109adadfe9d5ac83a24 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.ql.optimizer.ppr;
  19. import java.util.Map;
  20. import java.util.Stack;
  21. import org.apache.hadoop.hive.ql.exec.FilterOperator;
  22. import org.apache.hadoop.hive.ql.exec.TableScanOperator;
  23. import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
  24. import org.apache.hadoop.hive.ql.lib.Node;
  25. import org.apache.hadoop.hive.ql.lib.NodeProcessor;
  26. import org.apache.hadoop.hive.ql.lib.NodeProcessorCtx;
  27. import org.apache.hadoop.hive.ql.parse.SemanticException;
  28. import org.apache.hadoop.hive.ql.parse.TypeCheckProcFactory;
  29. import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
  30. /**
  31. * Operator factory for partition pruning processing of operator graph We find
  32. * all the filter operators that appear just beneath the table scan operators.
  33. * We then pass the filter to the partition pruner to construct a pruner for
  34. * that table alias and store a mapping from the table scan operator to that
  35. * pruner. We call that pruner later during plan generation.
  36. */
  37. public final class OpProcFactory {
  38. /**
  39. * Determines the partition pruner for the filter. This is called only when
  40. * the filter follows a table scan operator.
  41. */
  42. public static class FilterPPR implements NodeProcessor {
  43. @Override
  44. public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
  45. Object... nodeOutputs) throws SemanticException {
  46. OpWalkerCtx owc = (OpWalkerCtx) procCtx;
  47. FilterOperator fop = (FilterOperator) nd;
  48. FilterOperator fop2 = null;
  49. // The stack contains either ... TS, Filter or
  50. // ... TS, Filter, Filter with the head of the stack being the rightmost
  51. // symbol. So we just pop out the two elements from the top and if the
  52. // second one of them is not a table scan then the operator on the top of
  53. // the stack is the Table scan operator.
  54. Node tmp = stack.pop();
  55. Node tmp2 = stack.pop();
  56. TableScanOperator top = null;
  57. if (tmp2 instanceof TableScanOperator) {
  58. top = (TableScanOperator) tmp2;
  59. } else {
  60. top = (TableScanOperator) stack.peek();
  61. fop2 = (FilterOperator) tmp2;
  62. }
  63. stack.push(tmp2);
  64. stack.push(tmp);
  65. // If fop2 exists (i.e this is not the top level filter and fop2 is not
  66. // a sampling filter then we ignore the current filter
  67. if (fop2 != null && !fop2.getConf().getIsSamplingPred()) {
  68. return null;
  69. }
  70. // ignore the predicate in case it is not a sampling predicate
  71. if (fop.getConf().getIsSamplingPred()) {
  72. return null;
  73. }
  74. // Otherwise this is not a sampling predicate and we need to
  75. ExprNodeDesc predicate = fop.getConf().getPredicate();
  76. String alias = top.getConf().getAlias();
  77. // Generate the partition pruning predicate
  78. boolean hasNonPartCols = false;
  79. ExprNodeDesc ppr_pred = ExprProcFactory.genPruner(alias, predicate,
  80. hasNonPartCols);
  81. owc.addHasNonPartCols(hasNonPartCols);
  82. // Add the pruning predicate to the table scan operator
  83. addPruningPred(owc.getOpToPartPruner(), top, ppr_pred);
  84. return null;
  85. }
  86. private void addPruningPred(Map<TableScanOperator, ExprNodeDesc> opToPPR,
  87. TableScanOperator top, ExprNodeDesc new_ppr_pred) throws UDFArgumentException {
  88. ExprNodeDesc old_ppr_pred = opToPPR.get(top);
  89. ExprNodeDesc ppr_pred = null;
  90. if (old_ppr_pred != null) {
  91. // or the old_ppr_pred and the new_ppr_pred
  92. ppr_pred = TypeCheckProcFactory.DefaultExprProcessor
  93. .getFuncExprNodeDesc("OR", old_ppr_pred, new_ppr_pred);
  94. } else {
  95. ppr_pred = new_ppr_pred;
  96. }
  97. // Put the mapping from table scan operator to ppr_pred
  98. opToPPR.put(top, ppr_pred);
  99. return;
  100. }
  101. }
  102. /**
  103. * Default processor which just merges its children.
  104. */
  105. public static class DefaultPPR implements NodeProcessor {
  106. @Override
  107. public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
  108. Object... nodeOutputs) throws SemanticException {
  109. // Nothing needs to be done.
  110. return null;
  111. }
  112. }
  113. public static NodeProcessor getFilterProc() {
  114. return new FilterPPR();
  115. }
  116. public static NodeProcessor getDefaultProc() {
  117. return new DefaultPPR();
  118. }
  119. private OpProcFactory() {
  120. // prevent instantiation
  121. }
  122. }