PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ql/src/test/org/apache/hadoop/hive/ql/exec/TestExpressionEvaluator.java

https://github.com/amorton/hive
Java | 299 lines | 248 code | 23 blank | 28 comment | 1 complexity | 5ca7686343b94841ede7b8fe74202957 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 junit.framework.TestCase;
  21. import org.apache.hadoop.hive.ql.metadata.HiveException;
  22. import org.apache.hadoop.hive.ql.parse.TypeCheckProcFactory;
  23. import org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc;
  24. import org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc;
  25. import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
  26. import org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc;
  27. import org.apache.hadoop.hive.serde.Constants;
  28. import org.apache.hadoop.hive.serde2.objectinspector.InspectableObject;
  29. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  30. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
  31. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils.ObjectInspectorCopyOption;
  32. import org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo;
  33. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  34. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
  35. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
  36. import org.apache.hadoop.io.Text;
  37. /**
  38. * TestExpressionEvaluator.
  39. *
  40. */
  41. public class TestExpressionEvaluator extends TestCase {
  42. // this is our row to test expressions on
  43. protected InspectableObject r;
  44. ArrayList<Text> col1;
  45. TypeInfo col1Type;
  46. ArrayList<Text> cola;
  47. TypeInfo colaType;
  48. ArrayList<Object> data;
  49. ArrayList<String> names;
  50. ArrayList<TypeInfo> typeInfos;
  51. TypeInfo dataType;
  52. public TestExpressionEvaluator() {
  53. col1 = new ArrayList<Text>();
  54. col1.add(new Text("0"));
  55. col1.add(new Text("1"));
  56. col1.add(new Text("2"));
  57. col1.add(new Text("3"));
  58. col1Type = TypeInfoFactory.getListTypeInfo(TypeInfoFactory.stringTypeInfo);
  59. cola = new ArrayList<Text>();
  60. cola.add(new Text("a"));
  61. cola.add(new Text("b"));
  62. cola.add(new Text("c"));
  63. colaType = TypeInfoFactory.getListTypeInfo(TypeInfoFactory.stringTypeInfo);
  64. try {
  65. data = new ArrayList<Object>();
  66. data.add(col1);
  67. data.add(cola);
  68. names = new ArrayList<String>();
  69. names.add("col1");
  70. names.add("cola");
  71. typeInfos = new ArrayList<TypeInfo>();
  72. typeInfos.add(col1Type);
  73. typeInfos.add(colaType);
  74. dataType = TypeInfoFactory.getStructTypeInfo(names, typeInfos);
  75. r = new InspectableObject();
  76. r.o = data;
  77. r.oi = TypeInfoUtils
  78. .getStandardWritableObjectInspectorFromTypeInfo(dataType);
  79. } catch (Throwable e) {
  80. e.printStackTrace();
  81. throw new RuntimeException(e);
  82. }
  83. }
  84. @Override
  85. protected void setUp() {
  86. }
  87. public void testExprNodeColumnEvaluator() throws Throwable {
  88. try {
  89. // get a evaluator for a simple field expression
  90. ExprNodeDesc exprDesc = new ExprNodeColumnDesc(colaType, "cola", "",
  91. false);
  92. ExprNodeEvaluator eval = ExprNodeEvaluatorFactory.get(exprDesc);
  93. // evaluate on row
  94. ObjectInspector resultOI = eval.initialize(r.oi);
  95. Object resultO = eval.evaluate(r.o);
  96. Object standardResult = ObjectInspectorUtils.copyToStandardObject(
  97. resultO, resultOI, ObjectInspectorCopyOption.WRITABLE);
  98. assertEquals(cola, standardResult);
  99. System.out.println("ExprNodeColumnEvaluator ok");
  100. } catch (Throwable e) {
  101. e.printStackTrace();
  102. throw e;
  103. }
  104. }
  105. private static ExprNodeDesc getListIndexNode(ExprNodeDesc node, int index) {
  106. return getListIndexNode(node, new ExprNodeConstantDesc(index));
  107. }
  108. private static ExprNodeDesc getListIndexNode(ExprNodeDesc node,
  109. ExprNodeDesc index) {
  110. ArrayList<ExprNodeDesc> children = new ArrayList<ExprNodeDesc>(2);
  111. children.add(node);
  112. children.add(index);
  113. return new ExprNodeGenericFuncDesc(((ListTypeInfo) node.getTypeInfo())
  114. .getListElementTypeInfo(), FunctionRegistry.getGenericUDFForIndex(),
  115. children);
  116. }
  117. public void testExprNodeFuncEvaluator() throws Throwable {
  118. try {
  119. // get a evaluator for a string concatenation expression
  120. ExprNodeDesc col1desc = new ExprNodeColumnDesc(col1Type, "col1", "",
  121. false);
  122. ExprNodeDesc coladesc = new ExprNodeColumnDesc(colaType, "cola", "",
  123. false);
  124. ExprNodeDesc col11desc = getListIndexNode(col1desc, 1);
  125. ExprNodeDesc cola0desc = getListIndexNode(coladesc, 0);
  126. ExprNodeDesc func1 = TypeCheckProcFactory.DefaultExprProcessor
  127. .getFuncExprNodeDesc("concat", col11desc, cola0desc);
  128. ExprNodeEvaluator eval = ExprNodeEvaluatorFactory.get(func1);
  129. // evaluate on row
  130. ObjectInspector resultOI = eval.initialize(r.oi);
  131. Object resultO = eval.evaluate(r.o);
  132. assertEquals("1a", ObjectInspectorUtils.copyToStandardObject(resultO,
  133. resultOI, ObjectInspectorCopyOption.JAVA));
  134. System.out.println("ExprNodeFuncEvaluator ok");
  135. } catch (Throwable e) {
  136. e.printStackTrace();
  137. throw e;
  138. }
  139. }
  140. public void testExprNodeConversionEvaluator() throws Throwable {
  141. try {
  142. // get a evaluator for a string concatenation expression
  143. ExprNodeDesc col1desc = new ExprNodeColumnDesc(col1Type, "col1", "",
  144. false);
  145. ExprNodeDesc col11desc = getListIndexNode(col1desc, 1);
  146. ExprNodeDesc func1 = TypeCheckProcFactory.DefaultExprProcessor
  147. .getFuncExprNodeDesc(Constants.DOUBLE_TYPE_NAME, col11desc);
  148. ExprNodeEvaluator eval = ExprNodeEvaluatorFactory.get(func1);
  149. // evaluate on row
  150. ObjectInspector resultOI = eval.initialize(r.oi);
  151. Object resultO = eval.evaluate(r.o);
  152. assertEquals(Double.valueOf("1"), ObjectInspectorUtils
  153. .copyToStandardObject(resultO, resultOI,
  154. ObjectInspectorCopyOption.JAVA));
  155. System.out.println("testExprNodeConversionEvaluator ok");
  156. } catch (Throwable e) {
  157. e.printStackTrace();
  158. throw e;
  159. }
  160. }
  161. private static void measureSpeed(String expr, int times,
  162. ExprNodeEvaluator eval, InspectableObject input, Object standardJavaOutput)
  163. throws HiveException {
  164. System.out.println("Evaluating " + expr + " for " + times + " times");
  165. new InspectableObject();
  166. ObjectInspector resultOI = eval.initialize(input.oi);
  167. Object resultO = null;
  168. long start = System.currentTimeMillis();
  169. for (int i = 0; i < times; i++) {
  170. resultO = eval.evaluate(input.o);
  171. }
  172. long end = System.currentTimeMillis();
  173. assertEquals(standardJavaOutput, ObjectInspectorUtils.copyToStandardObject(
  174. resultO, resultOI, ObjectInspectorCopyOption.JAVA));
  175. System.out.println("Evaluation finished: "
  176. + String.format("%2.3f", (end - start) * 0.001) + " seconds, "
  177. + String.format("%2.3f", (end - start) * 1000.0 / times)
  178. + " seconds/million call.");
  179. }
  180. public void testExprNodeSpeed() throws Throwable {
  181. try {
  182. int basetimes = 100000;
  183. measureSpeed("1 + 2", basetimes * 100, ExprNodeEvaluatorFactory
  184. .get(TypeCheckProcFactory.DefaultExprProcessor.getFuncExprNodeDesc(
  185. "+", new ExprNodeConstantDesc(1), new ExprNodeConstantDesc(2))),
  186. r, Integer.valueOf(1 + 2));
  187. measureSpeed("1 + 2 - 3", basetimes * 100, ExprNodeEvaluatorFactory
  188. .get(TypeCheckProcFactory.DefaultExprProcessor
  189. .getFuncExprNodeDesc("-",
  190. TypeCheckProcFactory.DefaultExprProcessor
  191. .getFuncExprNodeDesc("+", new ExprNodeConstantDesc(1),
  192. new ExprNodeConstantDesc(2)),
  193. new ExprNodeConstantDesc(3))), r, Integer.valueOf(1 + 2 - 3));
  194. measureSpeed("1 + 2 - 3 + 4", basetimes * 100, ExprNodeEvaluatorFactory
  195. .get(TypeCheckProcFactory.DefaultExprProcessor
  196. .getFuncExprNodeDesc("+",
  197. TypeCheckProcFactory.DefaultExprProcessor
  198. .getFuncExprNodeDesc("-",
  199. TypeCheckProcFactory.DefaultExprProcessor
  200. .getFuncExprNodeDesc("+",
  201. new ExprNodeConstantDesc(1),
  202. new ExprNodeConstantDesc(2)),
  203. new ExprNodeConstantDesc(3)),
  204. new ExprNodeConstantDesc(4))), r, Integer
  205. .valueOf(1 + 2 - 3 + 4));
  206. measureSpeed("concat(\"1\", \"2\")", basetimes * 100,
  207. ExprNodeEvaluatorFactory
  208. .get(TypeCheckProcFactory.DefaultExprProcessor
  209. .getFuncExprNodeDesc("concat", new ExprNodeConstantDesc("1"),
  210. new ExprNodeConstantDesc("2"))), r, "12");
  211. measureSpeed("concat(concat(\"1\", \"2\"), \"3\")", basetimes * 100,
  212. ExprNodeEvaluatorFactory
  213. .get(TypeCheckProcFactory.DefaultExprProcessor
  214. .getFuncExprNodeDesc("concat",
  215. TypeCheckProcFactory.DefaultExprProcessor
  216. .getFuncExprNodeDesc("concat",
  217. new ExprNodeConstantDesc("1"),
  218. new ExprNodeConstantDesc("2")),
  219. new ExprNodeConstantDesc("3"))), r, "123");
  220. measureSpeed("concat(concat(concat(\"1\", \"2\"), \"3\"), \"4\")",
  221. basetimes * 100, ExprNodeEvaluatorFactory
  222. .get(TypeCheckProcFactory.DefaultExprProcessor
  223. .getFuncExprNodeDesc("concat",
  224. TypeCheckProcFactory.DefaultExprProcessor
  225. .getFuncExprNodeDesc("concat",
  226. TypeCheckProcFactory.DefaultExprProcessor
  227. .getFuncExprNodeDesc("concat",
  228. new ExprNodeConstantDesc("1"),
  229. new ExprNodeConstantDesc("2")),
  230. new ExprNodeConstantDesc("3")),
  231. new ExprNodeConstantDesc("4"))), r, "1234");
  232. ExprNodeDesc constant1 = new ExprNodeConstantDesc(1);
  233. ExprNodeDesc constant2 = new ExprNodeConstantDesc(2);
  234. measureSpeed("concat(col1[1], cola[1])", basetimes * 10,
  235. ExprNodeEvaluatorFactory
  236. .get(TypeCheckProcFactory.DefaultExprProcessor
  237. .getFuncExprNodeDesc("concat", getListIndexNode(
  238. new ExprNodeColumnDesc(col1Type, "col1", "", false),
  239. constant1), getListIndexNode(new ExprNodeColumnDesc(
  240. colaType, "cola", "", false), constant1))), r, "1b");
  241. measureSpeed("concat(concat(col1[1], cola[1]), col1[2])", basetimes * 10,
  242. ExprNodeEvaluatorFactory
  243. .get(TypeCheckProcFactory.DefaultExprProcessor
  244. .getFuncExprNodeDesc("concat",
  245. TypeCheckProcFactory.DefaultExprProcessor
  246. .getFuncExprNodeDesc("concat", getListIndexNode(
  247. new ExprNodeColumnDesc(col1Type, "col1", "",
  248. false), constant1), getListIndexNode(
  249. new ExprNodeColumnDesc(colaType, "cola", "",
  250. false), constant1)), getListIndexNode(
  251. new ExprNodeColumnDesc(col1Type, "col1", "", false),
  252. constant2))), r, "1b2");
  253. measureSpeed(
  254. "concat(concat(concat(col1[1], cola[1]), col1[2]), cola[2])",
  255. basetimes * 10, ExprNodeEvaluatorFactory
  256. .get(TypeCheckProcFactory.DefaultExprProcessor
  257. .getFuncExprNodeDesc("concat",
  258. TypeCheckProcFactory.DefaultExprProcessor
  259. .getFuncExprNodeDesc("concat",
  260. TypeCheckProcFactory.DefaultExprProcessor
  261. .getFuncExprNodeDesc("concat",
  262. getListIndexNode(new ExprNodeColumnDesc(
  263. col1Type, "col1", "", false),
  264. constant1), getListIndexNode(
  265. new ExprNodeColumnDesc(colaType,
  266. "cola", "", false), constant1)),
  267. getListIndexNode(new ExprNodeColumnDesc(col1Type,
  268. "col1", "", false), constant2)),
  269. getListIndexNode(new ExprNodeColumnDesc(colaType, "cola",
  270. "", false), constant2))), r, "1b2c");
  271. } catch (Throwable e) {
  272. e.printStackTrace();
  273. throw e;
  274. }
  275. }
  276. }