/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzerFactory.java

https://github.com/steeve/hive · Java · 190 lines · 157 code · 11 blank · 22 comment · 8 complexity · 618376d15e9bfea37740b91b0bfbe046 MD5 · raw file

  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.parse;
  19. import java.util.HashMap;
  20. import org.apache.hadoop.hive.conf.HiveConf;
  21. import org.apache.hadoop.hive.ql.plan.HiveOperation;
  22. import org.apache.hadoop.hive.ql.session.SessionState;
  23. /**
  24. * SemanticAnalyzerFactory.
  25. *
  26. */
  27. public final class SemanticAnalyzerFactory {
  28. static HashMap<Integer, HiveOperation> commandType = new HashMap<Integer, HiveOperation>();
  29. static HashMap<Integer, HiveOperation[]> tablePartitionCommandType = new HashMap<Integer, HiveOperation[]>();
  30. static {
  31. commandType.put(HiveParser.TOK_EXPLAIN, HiveOperation.EXPLAIN);
  32. commandType.put(HiveParser.TOK_LOAD, HiveOperation.LOAD);
  33. commandType.put(HiveParser.TOK_CREATEDATABASE, HiveOperation.CREATEDATABASE);
  34. commandType.put(HiveParser.TOK_DROPDATABASE, HiveOperation.DROPDATABASE);
  35. commandType.put(HiveParser.TOK_SWITCHDATABASE, HiveOperation.SWITCHDATABASE);
  36. commandType.put(HiveParser.TOK_CREATETABLE, HiveOperation.CREATETABLE);
  37. commandType.put(HiveParser.TOK_DROPTABLE, HiveOperation.DROPTABLE);
  38. commandType.put(HiveParser.TOK_DESCTABLE, HiveOperation.DESCTABLE);
  39. commandType.put(HiveParser.TOK_DESCFUNCTION, HiveOperation.DESCFUNCTION);
  40. commandType.put(HiveParser.TOK_MSCK, HiveOperation.MSCK);
  41. commandType.put(HiveParser.TOK_ALTERTABLE_ADDCOLS, HiveOperation.ALTERTABLE_ADDCOLS);
  42. commandType.put(HiveParser.TOK_ALTERTABLE_REPLACECOLS, HiveOperation.ALTERTABLE_REPLACECOLS);
  43. commandType.put(HiveParser.TOK_ALTERTABLE_RENAMECOL, HiveOperation.ALTERTABLE_RENAMECOL);
  44. commandType.put(HiveParser.TOK_ALTERTABLE_RENAME, HiveOperation.ALTERTABLE_RENAME);
  45. commandType.put(HiveParser.TOK_ALTERTABLE_DROPPARTS, HiveOperation.ALTERTABLE_DROPPARTS);
  46. commandType.put(HiveParser.TOK_ALTERTABLE_ADDPARTS, HiveOperation.ALTERTABLE_ADDPARTS);
  47. commandType.put(HiveParser.TOK_ALTERTABLE_TOUCH, HiveOperation.ALTERTABLE_TOUCH);
  48. commandType.put(HiveParser.TOK_ALTERTABLE_ARCHIVE, HiveOperation.ALTERTABLE_ARCHIVE);
  49. commandType.put(HiveParser.TOK_ALTERTABLE_UNARCHIVE, HiveOperation.ALTERTABLE_UNARCHIVE);
  50. commandType.put(HiveParser.TOK_ALTERTABLE_PROPERTIES, HiveOperation.ALTERTABLE_PROPERTIES);
  51. commandType.put(HiveParser.TOK_ALTERTABLE_SERIALIZER, HiveOperation.ALTERTABLE_SERIALIZER);
  52. commandType.put(HiveParser.TOK_ALTERTABLE_SERDEPROPERTIES, HiveOperation.ALTERTABLE_SERDEPROPERTIES);
  53. commandType.put(HiveParser.TOK_ALTERTABLE_CLUSTER_SORT, HiveOperation.ALTERTABLE_CLUSTER_SORT);
  54. commandType.put(HiveParser.TOK_SHOWDATABASES, HiveOperation.SHOWDATABASES);
  55. commandType.put(HiveParser.TOK_SHOWTABLES, HiveOperation.SHOWTABLES);
  56. commandType.put(HiveParser.TOK_SHOW_TABLESTATUS, HiveOperation.SHOW_TABLESTATUS);
  57. commandType.put(HiveParser.TOK_SHOWFUNCTIONS, HiveOperation.SHOWFUNCTIONS);
  58. commandType.put(HiveParser.TOK_SHOWINDEXES, HiveOperation.SHOWINDEXES);
  59. commandType.put(HiveParser.TOK_SHOWPARTITIONS, HiveOperation.SHOWPARTITIONS);
  60. commandType.put(HiveParser.TOK_SHOWLOCKS, HiveOperation.SHOWLOCKS);
  61. commandType.put(HiveParser.TOK_CREATEFUNCTION, HiveOperation.CREATEFUNCTION);
  62. commandType.put(HiveParser.TOK_DROPFUNCTION, HiveOperation.DROPFUNCTION);
  63. commandType.put(HiveParser.TOK_CREATEVIEW, HiveOperation.CREATEVIEW);
  64. commandType.put(HiveParser.TOK_DROPVIEW, HiveOperation.DROPVIEW);
  65. commandType.put(HiveParser.TOK_CREATEINDEX, HiveOperation.CREATEINDEX);
  66. commandType.put(HiveParser.TOK_DROPINDEX, HiveOperation.DROPINDEX);
  67. commandType.put(HiveParser.TOK_ALTERINDEX_REBUILD, HiveOperation.ALTERINDEX_REBUILD);
  68. commandType.put(HiveParser.TOK_ALTERINDEX_PROPERTIES, HiveOperation.ALTERINDEX_PROPS);
  69. commandType.put(HiveParser.TOK_ALTERVIEW_PROPERTIES, HiveOperation.ALTERVIEW_PROPERTIES);
  70. commandType.put(HiveParser.TOK_QUERY, HiveOperation.QUERY);
  71. commandType.put(HiveParser.TOK_LOCKTABLE, HiveOperation.LOCKTABLE);
  72. commandType.put(HiveParser.TOK_UNLOCKTABLE, HiveOperation.UNLOCKTABLE);
  73. commandType.put(HiveParser.TOK_CREATEROLE, HiveOperation.CREATEROLE);
  74. commandType.put(HiveParser.TOK_DROPROLE, HiveOperation.DROPROLE);
  75. commandType.put(HiveParser.TOK_GRANT, HiveOperation.GRANT_PRIVILEGE);
  76. commandType.put(HiveParser.TOK_REVOKE, HiveOperation.REVOKE_PRIVILEGE);
  77. commandType.put(HiveParser.TOK_SHOW_GRANT, HiveOperation.SHOW_GRANT);
  78. commandType.put(HiveParser.TOK_GRANT_ROLE, HiveOperation.GRANT_ROLE);
  79. commandType.put(HiveParser.TOK_REVOKE_ROLE, HiveOperation.REVOKE_ROLE);
  80. commandType.put(HiveParser.TOK_SHOW_ROLE_GRANT, HiveOperation.SHOW_ROLE_GRANT);
  81. commandType.put(HiveParser.TOK_ALTERDATABASE_PROPERTIES, HiveOperation.ALTERDATABASE);
  82. commandType.put(HiveParser.TOK_DESCDATABASE, HiveOperation.DESCDATABASE);
  83. }
  84. static {
  85. tablePartitionCommandType.put(
  86. HiveParser.TOK_ALTERTABLE_ALTERPARTS_PROTECTMODE,
  87. new HiveOperation[] { HiveOperation.ALTERTABLE_PROTECTMODE,
  88. HiveOperation.ALTERPARTITION_PROTECTMODE });
  89. tablePartitionCommandType.put(HiveParser.TOK_ALTERTABLE_FILEFORMAT,
  90. new HiveOperation[] { HiveOperation.ALTERTABLE_FILEFORMAT,
  91. HiveOperation.ALTERPARTITION_FILEFORMAT });
  92. tablePartitionCommandType.put(HiveParser.TOK_ALTERTABLE_LOCATION,
  93. new HiveOperation[] { HiveOperation.ALTERTABLE_LOCATION,
  94. HiveOperation.ALTERPARTITION_LOCATION });
  95. }
  96. public static BaseSemanticAnalyzer get(HiveConf conf, ASTNode tree)
  97. throws SemanticException {
  98. if (tree.getToken() == null) {
  99. throw new RuntimeException("Empty Syntax Tree");
  100. } else {
  101. setSessionCommandType(commandType.get(tree.getToken().getType()));
  102. switch (tree.getToken().getType()) {
  103. case HiveParser.TOK_EXPLAIN:
  104. return new ExplainSemanticAnalyzer(conf);
  105. case HiveParser.TOK_LOAD:
  106. return new LoadSemanticAnalyzer(conf);
  107. case HiveParser.TOK_CREATEDATABASE:
  108. case HiveParser.TOK_DROPDATABASE:
  109. case HiveParser.TOK_SWITCHDATABASE:
  110. case HiveParser.TOK_DROPTABLE:
  111. case HiveParser.TOK_DROPVIEW:
  112. case HiveParser.TOK_DESCDATABASE:
  113. case HiveParser.TOK_DESCTABLE:
  114. case HiveParser.TOK_DESCFUNCTION:
  115. case HiveParser.TOK_MSCK:
  116. case HiveParser.TOK_ALTERTABLE_ADDCOLS:
  117. case HiveParser.TOK_ALTERTABLE_RENAMECOL:
  118. case HiveParser.TOK_ALTERTABLE_REPLACECOLS:
  119. case HiveParser.TOK_ALTERTABLE_RENAME:
  120. case HiveParser.TOK_ALTERTABLE_DROPPARTS:
  121. case HiveParser.TOK_ALTERTABLE_ADDPARTS:
  122. case HiveParser.TOK_ALTERTABLE_PROPERTIES:
  123. case HiveParser.TOK_ALTERTABLE_SERIALIZER:
  124. case HiveParser.TOK_ALTERTABLE_SERDEPROPERTIES:
  125. case HiveParser.TOK_ALTERINDEX_REBUILD:
  126. case HiveParser.TOK_ALTERINDEX_PROPERTIES:
  127. case HiveParser.TOK_ALTERVIEW_PROPERTIES:
  128. case HiveParser.TOK_SHOWDATABASES:
  129. case HiveParser.TOK_SHOWTABLES:
  130. case HiveParser.TOK_SHOW_TABLESTATUS:
  131. case HiveParser.TOK_SHOWFUNCTIONS:
  132. case HiveParser.TOK_SHOWPARTITIONS:
  133. case HiveParser.TOK_SHOWINDEXES:
  134. case HiveParser.TOK_SHOWLOCKS:
  135. case HiveParser.TOK_CREATEINDEX:
  136. case HiveParser.TOK_DROPINDEX:
  137. case HiveParser.TOK_ALTERTABLE_CLUSTER_SORT:
  138. case HiveParser.TOK_ALTERTABLE_TOUCH:
  139. case HiveParser.TOK_ALTERTABLE_ARCHIVE:
  140. case HiveParser.TOK_ALTERTABLE_UNARCHIVE:
  141. case HiveParser.TOK_LOCKTABLE:
  142. case HiveParser.TOK_UNLOCKTABLE:
  143. case HiveParser.TOK_CREATEROLE:
  144. case HiveParser.TOK_DROPROLE:
  145. case HiveParser.TOK_GRANT:
  146. case HiveParser.TOK_REVOKE:
  147. case HiveParser.TOK_SHOW_GRANT:
  148. case HiveParser.TOK_GRANT_ROLE:
  149. case HiveParser.TOK_REVOKE_ROLE:
  150. case HiveParser.TOK_SHOW_ROLE_GRANT:
  151. case HiveParser.TOK_ALTERDATABASE_PROPERTIES:
  152. return new DDLSemanticAnalyzer(conf);
  153. case HiveParser.TOK_ALTERTABLE_PARTITION:
  154. HiveOperation commandType = null;
  155. Integer type = ((ASTNode) tree.getChild(1)).getToken().getType();
  156. if (tree.getChild(0).getChildCount() > 1) {
  157. commandType = tablePartitionCommandType.get(type)[1];
  158. } else {
  159. commandType = tablePartitionCommandType.get(type)[0];
  160. }
  161. setSessionCommandType(commandType);
  162. return new DDLSemanticAnalyzer(conf);
  163. case HiveParser.TOK_CREATEFUNCTION:
  164. case HiveParser.TOK_DROPFUNCTION:
  165. return new FunctionSemanticAnalyzer(conf);
  166. default:
  167. return new SemanticAnalyzer(conf);
  168. }
  169. }
  170. }
  171. private static void setSessionCommandType(HiveOperation commandType) {
  172. if (SessionState.get() != null) {
  173. SessionState.get().setCommandType(commandType);
  174. }
  175. }
  176. private SemanticAnalyzerFactory() {
  177. // prevent instantiation
  178. }
  179. }