PageRenderTime 29ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ql/src/java/org/apache/hadoop/hive/ql/exec/OperatorFactory.java

https://github.com/pkalmegh/hive
Java | 336 lines | 244 code | 33 blank | 59 comment | 24 complexity | 4b0a595723884601dd2f1caee8f372d1 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.exec;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Map;
  22. import org.apache.hadoop.hive.ql.exec.vector.VectorFileSinkOperator;
  23. import org.apache.hadoop.hive.ql.exec.vector.VectorFilterOperator;
  24. import org.apache.hadoop.hive.ql.exec.vector.VectorGroupByOperator;
  25. import org.apache.hadoop.hive.ql.exec.vector.VectorLimitOperator;
  26. import org.apache.hadoop.hive.ql.exec.vector.VectorMapJoinOperator;
  27. import org.apache.hadoop.hive.ql.exec.vector.VectorReduceSinkOperator;
  28. import org.apache.hadoop.hive.ql.exec.vector.VectorSelectOperator;
  29. import org.apache.hadoop.hive.ql.exec.vector.VectorSMBMapJoinOperator;
  30. import org.apache.hadoop.hive.ql.exec.vector.VectorizationContext;
  31. import org.apache.hadoop.hive.ql.metadata.HiveException;
  32. import org.apache.hadoop.hive.ql.plan.CollectDesc;
  33. import org.apache.hadoop.hive.ql.plan.DemuxDesc;
  34. import org.apache.hadoop.hive.ql.plan.DummyStoreDesc;
  35. import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
  36. import org.apache.hadoop.hive.ql.plan.ExtractDesc;
  37. import org.apache.hadoop.hive.ql.plan.FileSinkDesc;
  38. import org.apache.hadoop.hive.ql.plan.FilterDesc;
  39. import org.apache.hadoop.hive.ql.plan.ForwardDesc;
  40. import org.apache.hadoop.hive.ql.plan.GroupByDesc;
  41. import org.apache.hadoop.hive.ql.plan.HashTableDummyDesc;
  42. import org.apache.hadoop.hive.ql.plan.HashTableSinkDesc;
  43. import org.apache.hadoop.hive.ql.plan.JoinDesc;
  44. import org.apache.hadoop.hive.ql.plan.LateralViewForwardDesc;
  45. import org.apache.hadoop.hive.ql.plan.LateralViewJoinDesc;
  46. import org.apache.hadoop.hive.ql.plan.LimitDesc;
  47. import org.apache.hadoop.hive.ql.plan.MapJoinDesc;
  48. import org.apache.hadoop.hive.ql.plan.MuxDesc;
  49. import org.apache.hadoop.hive.ql.plan.OperatorDesc;
  50. import org.apache.hadoop.hive.ql.plan.PTFDesc;
  51. import org.apache.hadoop.hive.ql.plan.ReduceSinkDesc;
  52. import org.apache.hadoop.hive.ql.plan.SMBJoinDesc;
  53. import org.apache.hadoop.hive.ql.plan.ScriptDesc;
  54. import org.apache.hadoop.hive.ql.plan.SelectDesc;
  55. import org.apache.hadoop.hive.ql.plan.TableScanDesc;
  56. import org.apache.hadoop.hive.ql.plan.UDTFDesc;
  57. import org.apache.hadoop.hive.ql.plan.UnionDesc;
  58. /**
  59. * OperatorFactory.
  60. *
  61. */
  62. public final class OperatorFactory {
  63. /**
  64. * OpTuple.
  65. *
  66. * @param <T>
  67. */
  68. public static final class OpTuple<T extends OperatorDesc> {
  69. public Class<T> descClass;
  70. public Class<? extends Operator<T>> opClass;
  71. public OpTuple(Class<T> descClass, Class<? extends Operator<T>> opClass) {
  72. this.descClass = descClass;
  73. this.opClass = opClass;
  74. }
  75. }
  76. public static ArrayList<OpTuple> opvec;
  77. static {
  78. opvec = new ArrayList<OpTuple>();
  79. opvec.add(new OpTuple<FilterDesc>(FilterDesc.class, FilterOperator.class));
  80. opvec.add(new OpTuple<SelectDesc>(SelectDesc.class, SelectOperator.class));
  81. opvec.add(new OpTuple<ForwardDesc>(ForwardDesc.class, ForwardOperator.class));
  82. opvec.add(new OpTuple<FileSinkDesc>(FileSinkDesc.class, FileSinkOperator.class));
  83. opvec.add(new OpTuple<CollectDesc>(CollectDesc.class, CollectOperator.class));
  84. opvec.add(new OpTuple<ScriptDesc>(ScriptDesc.class, ScriptOperator.class));
  85. opvec.add(new OpTuple<PTFDesc>(PTFDesc.class, PTFOperator.class));
  86. opvec.add(new OpTuple<ReduceSinkDesc>(ReduceSinkDesc.class, ReduceSinkOperator.class));
  87. opvec.add(new OpTuple<ExtractDesc>(ExtractDesc.class, ExtractOperator.class));
  88. opvec.add(new OpTuple<GroupByDesc>(GroupByDesc.class, GroupByOperator.class));
  89. opvec.add(new OpTuple<JoinDesc>(JoinDesc.class, JoinOperator.class));
  90. opvec.add(new OpTuple<MapJoinDesc>(MapJoinDesc.class, MapJoinOperator.class));
  91. opvec.add(new OpTuple<SMBJoinDesc>(SMBJoinDesc.class, SMBMapJoinOperator.class));
  92. opvec.add(new OpTuple<LimitDesc>(LimitDesc.class, LimitOperator.class));
  93. opvec.add(new OpTuple<TableScanDesc>(TableScanDesc.class, TableScanOperator.class));
  94. opvec.add(new OpTuple<UnionDesc>(UnionDesc.class, UnionOperator.class));
  95. opvec.add(new OpTuple<UDTFDesc>(UDTFDesc.class, UDTFOperator.class));
  96. opvec.add(new OpTuple<LateralViewJoinDesc>(LateralViewJoinDesc.class,
  97. LateralViewJoinOperator.class));
  98. opvec.add(new OpTuple<LateralViewForwardDesc>(LateralViewForwardDesc.class,
  99. LateralViewForwardOperator.class));
  100. opvec.add(new OpTuple<HashTableDummyDesc>(HashTableDummyDesc.class,
  101. HashTableDummyOperator.class));
  102. opvec.add(new OpTuple<HashTableSinkDesc>(HashTableSinkDesc.class,
  103. HashTableSinkOperator.class));
  104. opvec.add(new OpTuple<DummyStoreDesc>(DummyStoreDesc.class,
  105. DummyStoreOperator.class));
  106. opvec.add(new OpTuple<DemuxDesc>(DemuxDesc.class,
  107. DemuxOperator.class));
  108. opvec.add(new OpTuple<MuxDesc>(MuxDesc.class,
  109. MuxOperator.class));
  110. }
  111. public static ArrayList<OpTuple> vectorOpvec;
  112. static {
  113. vectorOpvec = new ArrayList<OpTuple>();
  114. vectorOpvec.add(new OpTuple<SelectDesc>(SelectDesc.class, VectorSelectOperator.class));
  115. vectorOpvec.add(new OpTuple<GroupByDesc>(GroupByDesc.class, VectorGroupByOperator.class));
  116. vectorOpvec.add(new OpTuple<MapJoinDesc>(MapJoinDesc.class, VectorMapJoinOperator.class));
  117. vectorOpvec.add(new OpTuple<SMBJoinDesc>(SMBJoinDesc.class, VectorSMBMapJoinOperator.class));
  118. vectorOpvec.add(new OpTuple<ReduceSinkDesc>(ReduceSinkDesc.class,
  119. VectorReduceSinkOperator.class));
  120. vectorOpvec.add(new OpTuple<FileSinkDesc>(FileSinkDesc.class, VectorFileSinkOperator.class));
  121. vectorOpvec.add(new OpTuple<FilterDesc>(FilterDesc.class, VectorFilterOperator.class));
  122. vectorOpvec.add(new OpTuple<LimitDesc>(LimitDesc.class, VectorLimitOperator.class));
  123. }
  124. public static <T extends OperatorDesc> Operator<T> getVectorOperator(T conf,
  125. VectorizationContext vContext) throws HiveException {
  126. Class<T> descClass = (Class<T>) conf.getClass();
  127. for (OpTuple o : vectorOpvec) {
  128. if (o.descClass == descClass) {
  129. try {
  130. Operator<T> op = (Operator<T>) o.opClass.getDeclaredConstructor(
  131. VectorizationContext.class, OperatorDesc.class).newInstance(
  132. vContext, conf);
  133. return op;
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. throw new HiveException(e);
  137. }
  138. }
  139. }
  140. throw new HiveException("No vector operator for descriptor class "
  141. + descClass.getName());
  142. }
  143. public static <T extends OperatorDesc> Operator<T> get(Class<T> opClass) {
  144. for (OpTuple o : opvec) {
  145. if (o.descClass == opClass) {
  146. try {
  147. Operator<T> op = (Operator<T>) o.opClass.newInstance();
  148. return op;
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. throw new RuntimeException(e);
  152. }
  153. }
  154. }
  155. throw new RuntimeException("No operator for descriptor class "
  156. + opClass.getName());
  157. }
  158. public static <T extends OperatorDesc> Operator<T> get(Class<T> opClass,
  159. RowSchema rwsch) {
  160. Operator<T> ret = get(opClass);
  161. ret.setSchema(rwsch);
  162. return ret;
  163. }
  164. /**
  165. * Returns an operator given the conf and a list of children operators.
  166. */
  167. public static <T extends OperatorDesc> Operator<T> get(T conf,
  168. Operator<? extends OperatorDesc>... oplist) {
  169. Operator<T> ret = get((Class<T>) conf.getClass());
  170. ret.setConf(conf);
  171. makeChild(ret, oplist);
  172. return (ret);
  173. }
  174. /**
  175. * Returns an operator given the conf and a list of children operators.
  176. */
  177. public static void makeChild(
  178. Operator<? extends OperatorDesc> ret,
  179. Operator<? extends OperatorDesc>... oplist) {
  180. if (oplist.length == 0) {
  181. return;
  182. }
  183. ArrayList<Operator<? extends OperatorDesc>> clist =
  184. new ArrayList<Operator<? extends OperatorDesc>>();
  185. for (Operator<? extends OperatorDesc> op : oplist) {
  186. clist.add(op);
  187. }
  188. ret.setChildOperators(clist);
  189. // Add this parent to the children
  190. for (Operator<? extends OperatorDesc> op : oplist) {
  191. List<Operator<? extends OperatorDesc>> parents = op.getParentOperators();
  192. if (parents == null) {
  193. parents = new ArrayList<Operator<? extends OperatorDesc>>();
  194. }
  195. parents.add(ret);
  196. op.setParentOperators(parents);
  197. }
  198. }
  199. /**
  200. * Returns an operator given the conf and a list of children operators.
  201. */
  202. public static <T extends OperatorDesc> Operator<T> get(T conf,
  203. RowSchema rwsch, Operator... oplist) {
  204. Operator<T> ret = get(conf, oplist);
  205. ret.setSchema(rwsch);
  206. return (ret);
  207. }
  208. /**
  209. * Returns an operator given the conf and a list of parent operators.
  210. */
  211. public static <T extends OperatorDesc> Operator<T> getAndMakeChild(T conf,
  212. Operator... oplist) {
  213. Operator<T> ret = get((Class<T>) conf.getClass());
  214. ret.setConf(conf);
  215. if (oplist.length == 0) {
  216. return (ret);
  217. }
  218. // Add the new operator as child of each of the passed in operators
  219. for (Operator op : oplist) {
  220. List<Operator> children = op.getChildOperators();
  221. if (children == null) {
  222. children = new ArrayList<Operator>();
  223. }
  224. children.add(ret);
  225. op.setChildOperators(children);
  226. }
  227. // add parents for the newly created operator
  228. List<Operator<? extends OperatorDesc>> parent =
  229. new ArrayList<Operator<? extends OperatorDesc>>();
  230. for (Operator op : oplist) {
  231. parent.add(op);
  232. }
  233. ret.setParentOperators(parent);
  234. return (ret);
  235. }
  236. /**
  237. * Returns an operator given the conf and a list of parent operators.
  238. */
  239. public static <T extends OperatorDesc> Operator<T> getAndMakeChild(T conf,
  240. List<Operator<? extends OperatorDesc>> oplist) {
  241. Operator<T> ret = get((Class<T>) conf.getClass());
  242. ret.setConf(conf);
  243. if (oplist.size() == 0) {
  244. return (ret);
  245. }
  246. // Add the new operator as child of each of the passed in operators
  247. for (Operator op : oplist) {
  248. List<Operator> children = op.getChildOperators();
  249. if (children == null) {
  250. children = new ArrayList<Operator>();
  251. }
  252. children.add(ret);
  253. op.setChildOperators(children);
  254. }
  255. // add parents for the newly created operator
  256. List<Operator<? extends OperatorDesc>> parent =
  257. new ArrayList<Operator<? extends OperatorDesc>>();
  258. for (Operator op : oplist) {
  259. parent.add(op);
  260. }
  261. ret.setParentOperators(parent);
  262. return (ret);
  263. }
  264. /**
  265. * Returns an operator given the conf and a list of parent operators.
  266. */
  267. public static <T extends OperatorDesc> Operator<T> getAndMakeChild(T conf,
  268. RowSchema rwsch, Operator... oplist) {
  269. Operator<T> ret = getAndMakeChild(conf, oplist);
  270. ret.setSchema(rwsch);
  271. return (ret);
  272. }
  273. /**
  274. * Returns an operator given the conf and a list of parent operators.
  275. */
  276. public static <T extends OperatorDesc> Operator<T> getAndMakeChild(T conf,
  277. RowSchema rwsch, Map<String, ExprNodeDesc> colExprMap, Operator... oplist) {
  278. Operator<T> ret = getAndMakeChild(conf, rwsch, oplist);
  279. ret.setColumnExprMap(colExprMap);
  280. return (ret);
  281. }
  282. /**
  283. * Returns an operator given the conf and a list of parent operators.
  284. */
  285. public static <T extends OperatorDesc> Operator<T> getAndMakeChild(T conf,
  286. RowSchema rwsch, List<Operator<? extends OperatorDesc>> oplist) {
  287. Operator<T> ret = getAndMakeChild(conf, oplist);
  288. ret.setSchema(rwsch);
  289. return (ret);
  290. }
  291. /**
  292. * Returns an operator given the conf and a list of parent operators.
  293. */
  294. public static <T extends OperatorDesc> Operator<T> getAndMakeChild(T conf,
  295. RowSchema rwsch, Map<String, ExprNodeDesc> colExprMap, List<Operator<? extends OperatorDesc>> oplist) {
  296. Operator<T> ret = getAndMakeChild(conf, rwsch, oplist);
  297. ret.setColumnExprMap(colExprMap);
  298. return (ret);
  299. }
  300. private OperatorFactory() {
  301. // prevent instantiation
  302. }
  303. }