PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/serde/src/test/org/apache/hadoop/hive/serde2/lazy/TestLazyArrayMapStruct.java

#
Java | 314 lines | 237 code | 38 blank | 39 comment | 0 complexity | c75036783d5b6568ba5c7ea770ef1d72 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.serde2.lazy;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.List;
  22. import junit.framework.TestCase;
  23. import org.apache.hadoop.hive.serde2.SerDeUtils;
  24. import org.apache.hadoop.hive.serde2.io.ByteWritable;
  25. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  26. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  27. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
  28. import org.apache.hadoop.io.IntWritable;
  29. import org.apache.hadoop.io.Text;
  30. /**
  31. * Tests LazyArray, LazyMap, LazyStruct and LazyUnion
  32. *
  33. */
  34. public class TestLazyArrayMapStruct extends TestCase {
  35. /**
  36. * Test the LazyArray class.
  37. */
  38. public void testLazyArray() throws Throwable {
  39. try {
  40. // Array of Byte
  41. Text nullSequence = new Text("\\N");
  42. ObjectInspector oi = LazyFactory.createLazyObjectInspector(TypeInfoUtils
  43. .getTypeInfosFromTypeString("array<tinyint>").get(0),
  44. new byte[] {(byte) 1}, 0, nullSequence, false, (byte) 0);
  45. LazyArray b = (LazyArray) LazyFactory.createLazyObject(oi);
  46. byte[] data = new byte[] {'-', '1', 1, '\\', 'N', 1, '8'};
  47. TestLazyPrimitive.initLazyObject(b, data, 0, data.length);
  48. assertNull(b.getListElementObject(-1));
  49. assertEquals(new ByteWritable((byte) -1), ((LazyByte) b
  50. .getListElementObject(0)).getWritableObject());
  51. assertEquals(new ByteWritable((byte) -1), ((LazyByte) b.getList().get(0))
  52. .getWritableObject());
  53. assertNull(b.getListElementObject(1));
  54. assertNull(b.getList().get(1));
  55. assertEquals(new ByteWritable((byte) 8), ((LazyByte) b
  56. .getListElementObject(2)).getWritableObject());
  57. assertEquals(new ByteWritable((byte) 8), ((LazyByte) b.getList().get(2))
  58. .getWritableObject());
  59. assertNull(b.getListElementObject(3));
  60. assertEquals(3, b.getList().size());
  61. // Array of String
  62. oi = LazyFactory.createLazyObjectInspector(TypeInfoUtils
  63. .getTypeInfosFromTypeString("array<string>").get(0),
  64. new byte[] {(byte) '\t'}, 0, nullSequence, false, (byte) 0);
  65. b = (LazyArray) LazyFactory.createLazyObject(oi);
  66. data = new byte[] {'a', 'b', '\t', 'c', '\t', '\\', 'N', '\t', '\t', 'd'};
  67. // Note: the first and last element of the byte[] are NOT used
  68. TestLazyPrimitive.initLazyObject(b, data, 1, data.length - 2);
  69. assertNull(b.getListElementObject(-1));
  70. assertEquals(new Text("b"), ((LazyString) b.getListElementObject(0))
  71. .getWritableObject());
  72. assertEquals(new Text("b"), ((LazyString) b.getList().get(0))
  73. .getWritableObject());
  74. assertEquals(new Text("c"), ((LazyString) b.getListElementObject(1))
  75. .getWritableObject());
  76. assertEquals(new Text("c"), ((LazyString) b.getList().get(1))
  77. .getWritableObject());
  78. assertNull((b.getListElementObject(2)));
  79. assertNull((b.getList().get(2)));
  80. assertEquals(new Text(""), ((LazyString) b.getListElementObject(3))
  81. .getWritableObject());
  82. assertEquals(new Text(""), ((LazyString) b.getList().get(3))
  83. .getWritableObject());
  84. assertEquals(new Text(""), ((LazyString) b.getListElementObject(4))
  85. .getWritableObject());
  86. assertEquals(new Text(""), ((LazyString) b.getList().get(4))
  87. .getWritableObject());
  88. assertNull((b.getListElementObject(5)));
  89. assertEquals(5, b.getList().size());
  90. } catch (Throwable e) {
  91. e.printStackTrace();
  92. throw e;
  93. }
  94. }
  95. /**
  96. * Test the LazyMap class.
  97. */
  98. public void testLazyMap() throws Throwable {
  99. try {
  100. {
  101. // Map of Integer to String
  102. Text nullSequence = new Text("\\N");
  103. ObjectInspector oi = LazyFactory
  104. .createLazyObjectInspector(TypeInfoUtils
  105. .getTypeInfosFromTypeString("map<int,string>").get(0),
  106. new byte[] {(byte) 1, (byte) 2}, 0, nullSequence, false,
  107. (byte) 0);
  108. LazyMap b = (LazyMap) LazyFactory.createLazyObject(oi);
  109. byte[] data = new byte[] {'2', 2, 'd', 'e', 'f', 1, '-', '1', 2, '\\',
  110. 'N', 1, '0', 2, '0', 1, '8', 2, 'a', 'b', 'c'};
  111. TestLazyPrimitive.initLazyObject(b, data, 0, data.length);
  112. assertEquals(new Text("def"), ((LazyString) b
  113. .getMapValueElement(new IntWritable(2))).getWritableObject());
  114. assertNull(b.getMapValueElement(new IntWritable(-1)));
  115. assertEquals(new Text("0"), ((LazyString) b
  116. .getMapValueElement(new IntWritable(0))).getWritableObject());
  117. assertEquals(new Text("abc"), ((LazyString) b
  118. .getMapValueElement(new IntWritable(8))).getWritableObject());
  119. assertNull(b.getMapValueElement(new IntWritable(12345)));
  120. assertEquals("{2:'def',-1:null,0:'0',8:'abc'}".replace('\'', '\"'),
  121. SerDeUtils.getJSONString(b, oi));
  122. }
  123. {
  124. // Map of String to String
  125. Text nullSequence = new Text("\\N");
  126. ObjectInspector oi = LazyFactory.createLazyObjectInspector(
  127. TypeInfoUtils.getTypeInfosFromTypeString("map<string,string>").get(
  128. 0), new byte[] {(byte) '#', (byte) '\t'}, 0, nullSequence,
  129. false, (byte) 0);
  130. LazyMap b = (LazyMap) LazyFactory.createLazyObject(oi);
  131. byte[] data = new byte[] {'2', '\t', 'd', '\t', 'f', '#', '2', '\t',
  132. 'd', '#', '-', '1', '#', '0', '\t', '0', '#', '8', '\t', 'a', 'b', 'c'};
  133. TestLazyPrimitive.initLazyObject(b, data, 0, data.length);
  134. assertEquals(new Text("d\tf"), ((LazyString) b
  135. .getMapValueElement(new Text("2"))).getWritableObject());
  136. assertNull(b.getMapValueElement(new Text("-1")));
  137. assertEquals(new Text("0"), ((LazyString) b
  138. .getMapValueElement(new Text("0"))).getWritableObject());
  139. assertEquals(new Text("abc"), ((LazyString) b
  140. .getMapValueElement(new Text("8"))).getWritableObject());
  141. assertNull(b.getMapValueElement(new Text("-")));
  142. assertEquals("{'2':'d\\tf','2':'d','-1':null,'0':'0','8':'abc'}"
  143. .replace('\'', '\"'), SerDeUtils.getJSONString(b, oi));
  144. }
  145. } catch (Throwable e) {
  146. e.printStackTrace();
  147. throw e;
  148. }
  149. }
  150. /**
  151. * Test the LazyStruct class.
  152. */
  153. public void testLazyStruct() throws Throwable {
  154. try {
  155. {
  156. ArrayList<TypeInfo> fieldTypeInfos = TypeInfoUtils
  157. .getTypeInfosFromTypeString("int,array<string>,map<string,string>,string");
  158. List<String> fieldNames = Arrays.asList(new String[] {"a", "b", "c",
  159. "d"});
  160. Text nullSequence = new Text("\\N");
  161. ObjectInspector oi = LazyFactory.createLazyStructInspector(fieldNames,
  162. fieldTypeInfos, new byte[] {' ', ':', '='}, nullSequence, false,
  163. false, (byte) 0);
  164. LazyStruct o = (LazyStruct) LazyFactory.createLazyObject(oi);
  165. Text data;
  166. data = new Text("123 a:b:c d=e:f=g hi");
  167. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0, data
  168. .getLength());
  169. assertEquals(
  170. "{'a':123,'b':['a','b','c'],'c':{'d':'e','f':'g'},'d':'hi'}"
  171. .replace("'", "\""), SerDeUtils.getJSONString(o, oi));
  172. data = new Text("123 \\N d=e:f=g \\N");
  173. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0, data
  174. .getLength());
  175. assertEquals("{'a':123,'b':null,'c':{'d':'e','f':'g'},'d':null}"
  176. .replace("'", "\""), SerDeUtils.getJSONString(o, oi));
  177. data = new Text("\\N a d=\\N:f=g:h no tail");
  178. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0, data
  179. .getLength());
  180. assertEquals(
  181. "{'a':null,'b':['a'],'c':{'d':null,'f':'g','h':null},'d':'no'}"
  182. .replace("'", "\""), SerDeUtils.getJSONString(o, oi));
  183. data = new Text("\\N :a:: \\N no tail");
  184. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0, data
  185. .getLength());
  186. assertEquals("{'a':null,'b':['','a','',''],'c':null,'d':'no'}".replace(
  187. "'", "\""), SerDeUtils.getJSONString(o, oi));
  188. data = new Text("123 ");
  189. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0, data
  190. .getLength());
  191. assertEquals("{'a':123,'b':[],'c':{},'d':''}".replace("'", "\""),
  192. SerDeUtils.getJSONString(o, oi));
  193. data = new Text(": : : :");
  194. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0, data
  195. .getLength());
  196. assertEquals("{'a':null,'b':['',''],'c':{'':null,'':null},'d':':'}"
  197. .replace("'", "\""), SerDeUtils.getJSONString(o, oi));
  198. data = new Text("= = = =");
  199. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0, data
  200. .getLength());
  201. assertEquals("{'a':null,'b':['='],'c':{'':''},'d':'='}".replace("'",
  202. "\""), SerDeUtils.getJSONString(o, oi));
  203. // test LastColumnTakesRest
  204. oi = LazyFactory.createLazyStructInspector(Arrays.asList(new String[] {
  205. "a", "b", "c", "d"}), fieldTypeInfos,
  206. new byte[] {' ', ':', '='}, nullSequence, true, false, (byte) 0);
  207. o = (LazyStruct) LazyFactory.createLazyObject(oi);
  208. data = new Text("\\N a d=\\N:f=g:h has tail");
  209. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0, data
  210. .getLength());
  211. assertEquals(
  212. "{'a':null,'b':['a'],'c':{'d':null,'f':'g','h':null},'d':'has tail'}"
  213. .replace("'", "\""), SerDeUtils.getJSONString(o, oi));
  214. }
  215. } catch (Throwable e) {
  216. e.printStackTrace();
  217. throw e;
  218. }
  219. }
  220. /**
  221. * Test the LazyUnion class.
  222. */
  223. public void testLazyUnion() throws Throwable {
  224. try {
  225. {
  226. TypeInfo typeInfo = TypeInfoUtils.getTypeInfoFromTypeString(
  227. "uniontype<int,array<string>,map<string,string>,string>");
  228. Text nullSequence = new Text("\\N");
  229. ObjectInspector oi = LazyFactory.createLazyObjectInspector(typeInfo,
  230. new byte[] {'^', ':', '='}, 0, nullSequence, false, (byte) 0);
  231. LazyUnion o = (LazyUnion) LazyFactory.createLazyObject(oi);
  232. Text data;
  233. data = new Text("0^123");
  234. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0,
  235. data.getLength());
  236. assertEquals("{0:123}", SerDeUtils.getJSONString(o, oi));
  237. data = new Text("1^a:b:c");
  238. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0,
  239. data.getLength());
  240. assertEquals(
  241. "{1:[\"a\",\"b\",\"c\"]}", SerDeUtils.getJSONString(o, oi));
  242. data = new Text("2^d=e:f=g");
  243. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0,
  244. data.getLength());
  245. assertEquals(
  246. "{2:{\"d\":\"e\",\"f\":\"g\"}}", SerDeUtils.getJSONString(o, oi));
  247. data = new Text("3^hi");
  248. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0,
  249. data.getLength());
  250. assertEquals("{3:\"hi\"}", SerDeUtils.getJSONString(o, oi));
  251. data = new Text("0^\\N");
  252. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0,
  253. data.getLength());
  254. assertEquals("{0:null}", SerDeUtils.getJSONString(o, oi));
  255. data = new Text("1^ :a::");
  256. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0,
  257. data.getLength());
  258. assertEquals(
  259. "{1:[\" \",\"a\",\"\",\"\"]}", SerDeUtils.getJSONString(o, oi));
  260. data = new Text("2^d=\\N:f=g:h");
  261. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0,
  262. data.getLength());
  263. assertEquals(
  264. "{2:{\"d\":null,\"f\":\"g\",\"h\":null}}",
  265. SerDeUtils.getJSONString(o, oi));
  266. data = new Text("2^= ");
  267. TestLazyPrimitive.initLazyObject(o, data.getBytes(), 0,
  268. data.getLength());
  269. assertEquals("{2:{\"\":\" \"}}", SerDeUtils.getJSONString(o, oi));
  270. }
  271. } catch (Throwable e) {
  272. e.printStackTrace();
  273. throw e;
  274. }
  275. }
  276. }