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

/tags/release-0.1-rc2/hive/external/serde/src/test/org/apache/hadoop/hive/serde2/objectinspector/TestUnionStructObjectInspector.java

#
Java | 121 lines | 84 code | 13 blank | 24 comment | 5 complexity | ca0950cc32111a840eb3300235c802dd 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.objectinspector;
  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.objectinspector.ObjectInspector.Category;
  24. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
  25. /**
  26. * TestUnionStructObjectInspector.
  27. *
  28. */
  29. public class TestUnionStructObjectInspector extends TestCase {
  30. public void testUnionStructObjectInspector() throws Throwable {
  31. try {
  32. ArrayList<String> fieldNames1 = new ArrayList<String>();
  33. fieldNames1.add("firstInteger");
  34. fieldNames1.add("secondString");
  35. fieldNames1.add("thirdBoolean");
  36. ArrayList<ObjectInspector> fieldObjectInspectors1 = new ArrayList<ObjectInspector>();
  37. fieldObjectInspectors1
  38. .add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
  39. fieldObjectInspectors1
  40. .add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
  41. fieldObjectInspectors1
  42. .add(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
  43. StandardStructObjectInspector soi1 = ObjectInspectorFactory
  44. .getStandardStructObjectInspector(fieldNames1, fieldObjectInspectors1);
  45. ArrayList<String> fieldNames2 = new ArrayList<String>();
  46. fieldNames2.add("fourthDouble");
  47. fieldNames2.add("fifthLong");
  48. ArrayList<ObjectInspector> fieldObjectInspectors2 = new ArrayList<ObjectInspector>();
  49. fieldObjectInspectors2
  50. .add(PrimitiveObjectInspectorFactory.javaDoubleObjectInspector);
  51. fieldObjectInspectors2
  52. .add(PrimitiveObjectInspectorFactory.javaLongObjectInspector);
  53. StandardStructObjectInspector soi2 = ObjectInspectorFactory
  54. .getStandardStructObjectInspector(fieldNames2, fieldObjectInspectors2);
  55. UnionStructObjectInspector usoi1 = ObjectInspectorFactory
  56. .getUnionStructObjectInspector(Arrays
  57. .asList(new StructObjectInspector[] {soi1, soi2}));
  58. UnionStructObjectInspector usoi2 = ObjectInspectorFactory
  59. .getUnionStructObjectInspector(Arrays
  60. .asList(new StructObjectInspector[] {soi1, soi2}));
  61. assertEquals(usoi1, usoi2);
  62. // metadata
  63. assertEquals(Category.STRUCT, usoi1.getCategory());
  64. List<? extends StructField> fields = usoi1.getAllStructFieldRefs();
  65. assertEquals(5, fields.size());
  66. for (int i = 0; i < 5; i++) {
  67. if (i <= 2) {
  68. assertEquals(fieldNames1.get(i).toLowerCase(), fields.get(i)
  69. .getFieldName());
  70. assertEquals(fieldObjectInspectors1.get(i), fields.get(i)
  71. .getFieldObjectInspector());
  72. } else {
  73. assertEquals(fieldNames2.get(i - 3).toLowerCase(), fields.get(i)
  74. .getFieldName());
  75. assertEquals(fieldObjectInspectors2.get(i - 3), fields.get(i)
  76. .getFieldObjectInspector());
  77. }
  78. }
  79. assertEquals(fields.get(1), usoi1.getStructFieldRef("secondString"));
  80. assertEquals(fields.get(4), usoi1.getStructFieldRef("fifthLong"));
  81. // null
  82. for (int i = 0; i < 5; i++) {
  83. assertNull(usoi1.getStructFieldData(null, fields.get(i)));
  84. }
  85. // real struct
  86. ArrayList<Object> struct1 = new ArrayList<Object>(3);
  87. struct1.add(1);
  88. struct1.add("two");
  89. struct1.add(true);
  90. ArrayList<Object> struct2 = new ArrayList<Object>(2);
  91. struct2.add(1.0);
  92. struct2.add(new Long(111));
  93. ArrayList<Object> struct = new ArrayList<Object>(2);
  94. struct.add(struct1);
  95. struct.add(struct2);
  96. ArrayList<Object> all = new ArrayList<Object>(5);
  97. all.addAll(struct1);
  98. all.addAll(struct2);
  99. for (int i = 0; i < 5; i++) {
  100. assertEquals(all.get(i), usoi1
  101. .getStructFieldData(struct, fields.get(i)));
  102. }
  103. } catch (Throwable e) {
  104. e.printStackTrace();
  105. throw e;
  106. }
  107. }
  108. }