PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/StandardStructObjectInspector.java

https://github.com/amorton/hive
Java | 201 lines | 133 code | 28 blank | 40 comment | 20 complexity | b9e004a7f912c04204c6886acff74cda 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.serde2.objectinspector;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.List;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. /**
  25. * ListStructObjectInspector works on struct data that is stored as a Java List
  26. * or Java Array object. Basically, the fields are stored sequentially in the
  27. * List object.
  28. *
  29. * The names of the struct fields and the internal structure of the struct
  30. * fields are specified in the ctor of the StructObjectInspector.
  31. *
  32. * Always use the ObjectInspectorFactory to create new ObjectInspector objects,
  33. * instead of directly creating an instance of this class.
  34. */
  35. public class StandardStructObjectInspector extends
  36. SettableStructObjectInspector {
  37. public static final Log LOG = LogFactory
  38. .getLog(StandardStructObjectInspector.class.getName());
  39. protected static class MyField implements StructField {
  40. protected int fieldID;
  41. protected String fieldName;
  42. protected ObjectInspector fieldObjectInspector;
  43. public MyField(int fieldID, String fieldName,
  44. ObjectInspector fieldObjectInspector) {
  45. this.fieldID = fieldID;
  46. this.fieldName = fieldName.toLowerCase();
  47. this.fieldObjectInspector = fieldObjectInspector;
  48. }
  49. public int getFieldID() {
  50. return fieldID;
  51. }
  52. public String getFieldName() {
  53. return fieldName;
  54. }
  55. public ObjectInspector getFieldObjectInspector() {
  56. return fieldObjectInspector;
  57. }
  58. @Override
  59. public String toString() {
  60. return "" + fieldID + ":" + fieldName;
  61. }
  62. }
  63. protected List<MyField> fields;
  64. public String getTypeName() {
  65. return ObjectInspectorUtils.getStandardStructTypeName(this);
  66. }
  67. /**
  68. * Call ObjectInspectorFactory.getStandardListObjectInspector instead.
  69. */
  70. protected StandardStructObjectInspector(List<String> structFieldNames,
  71. List<ObjectInspector> structFieldObjectInspectors) {
  72. init(structFieldNames, structFieldObjectInspectors);
  73. }
  74. protected void init(List<String> structFieldNames,
  75. List<ObjectInspector> structFieldObjectInspectors) {
  76. assert (structFieldNames.size() == structFieldObjectInspectors.size());
  77. fields = new ArrayList<MyField>(structFieldNames.size());
  78. for (int i = 0; i < structFieldNames.size(); i++) {
  79. fields.add(new MyField(i, structFieldNames.get(i),
  80. structFieldObjectInspectors.get(i)));
  81. }
  82. }
  83. protected StandardStructObjectInspector(List<StructField> fields) {
  84. init(fields);
  85. }
  86. protected void init(List<StructField> fields) {
  87. this.fields = new ArrayList<MyField>(fields.size());
  88. for (int i = 0; i < fields.size(); i++) {
  89. this.fields.add(new MyField(i, fields.get(i).getFieldName(), fields
  90. .get(i).getFieldObjectInspector()));
  91. }
  92. }
  93. public final Category getCategory() {
  94. return Category.STRUCT;
  95. }
  96. // Without Data
  97. @Override
  98. public StructField getStructFieldRef(String fieldName) {
  99. return ObjectInspectorUtils.getStandardStructFieldRef(fieldName, fields);
  100. }
  101. @Override
  102. public List<? extends StructField> getAllStructFieldRefs() {
  103. return fields;
  104. }
  105. boolean warned = false;
  106. // With Data
  107. @Override
  108. @SuppressWarnings("unchecked")
  109. public Object getStructFieldData(Object data, StructField fieldRef) {
  110. if (data == null) {
  111. return null;
  112. }
  113. // We support both List<Object> and Object[]
  114. // so we have to do differently.
  115. boolean isArray = ! (data instanceof List);
  116. if (!isArray && !(data instanceof List)) {
  117. return data;
  118. }
  119. int listSize = (isArray ? ((Object[]) data).length : ((List<Object>) data)
  120. .size());
  121. MyField f = (MyField) fieldRef;
  122. if (fields.size() != listSize && !warned) {
  123. // TODO: remove this
  124. warned = true;
  125. LOG.warn("Trying to access " + fields.size()
  126. + " fields inside a list of " + listSize + " elements: "
  127. + (isArray ? Arrays.asList((Object[]) data) : (List<Object>) data));
  128. LOG.warn("ignoring similar errors.");
  129. }
  130. int fieldID = f.getFieldID();
  131. assert (fieldID >= 0 && fieldID < fields.size());
  132. if (fieldID >= listSize) {
  133. return null;
  134. } else if (isArray) {
  135. return ((Object[]) data)[fieldID];
  136. } else {
  137. return ((List<Object>) data).get(fieldID);
  138. }
  139. }
  140. @Override
  141. @SuppressWarnings("unchecked")
  142. public List<Object> getStructFieldsDataAsList(Object data) {
  143. if (data == null) {
  144. return null;
  145. }
  146. // We support both List<Object> and Object[]
  147. // so we have to do differently.
  148. if (! (data instanceof List)) {
  149. data = java.util.Arrays.asList((Object[]) data);
  150. }
  151. List<Object> list = (List<Object>) data;
  152. assert (list.size() == fields.size());
  153. return list;
  154. }
  155. // /////////////////////////////
  156. // SettableStructObjectInspector
  157. @Override
  158. public Object create() {
  159. ArrayList<Object> a = new ArrayList<Object>(fields.size());
  160. for (int i = 0; i < fields.size(); i++) {
  161. a.add(null);
  162. }
  163. return a;
  164. }
  165. @Override
  166. public Object setStructFieldData(Object struct, StructField field,
  167. Object fieldValue) {
  168. ArrayList<Object> a = (ArrayList<Object>) struct;
  169. MyField myField = (MyField) field;
  170. a.set(myField.fieldID, fieldValue);
  171. return a;
  172. }
  173. }