PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/lib/RuleRegExp.java

#
Java | 81 lines | 28 code | 9 blank | 44 comment | 3 complexity | 70367e71cefd04d77e253b47dc6cc1f0 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.lib;
  19. import java.util.Stack;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. import org.apache.hadoop.hive.ql.parse.SemanticException;
  23. /**
  24. * Rule interface for Nodes Used in Node dispatching to dispatch process/visitor
  25. * functions for Nodes.
  26. */
  27. public class RuleRegExp implements Rule {
  28. private final String ruleName;
  29. private final Pattern pattern;
  30. /**
  31. * The rule specified by the regular expression. Note that, the regular
  32. * expression is specified in terms of Node name. For eg: TS.*RS -> means
  33. * TableScan Node followed by anything any number of times followed by
  34. * ReduceSink
  35. *
  36. * @param ruleName
  37. * name of the rule
  38. * @param regExp
  39. * regular expression for the rule
  40. **/
  41. public RuleRegExp(String ruleName, String regExp) {
  42. this.ruleName = ruleName;
  43. pattern = Pattern.compile(regExp);
  44. }
  45. /**
  46. * This function returns the cost of the rule for the specified stack. Lower
  47. * the cost, the better the rule is matched
  48. *
  49. * @param stack
  50. * Node stack encountered so far
  51. * @return cost of the function
  52. * @throws SemanticException
  53. */
  54. public int cost(Stack<Node> stack) throws SemanticException {
  55. int numElems = (stack != null ? stack.size() : 0);
  56. String name = new String();
  57. for (int pos = numElems - 1; pos >= 0; pos--) {
  58. name = stack.get(pos).getName() + "%" + name;
  59. Matcher m = pattern.matcher(name);
  60. if (m.matches()) {
  61. return m.group().length();
  62. }
  63. }
  64. return -1;
  65. }
  66. /**
  67. * @return the name of the Node
  68. **/
  69. public String getName() {
  70. return ruleName;
  71. }
  72. }