PageRenderTime 60ms CodeModel.GetById 15ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java

#
Java | 678 lines | 492 code | 47 blank | 139 comment | 66 complexity | 62d79abcf6269e9b41a072248279297e 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
 19package org.apache.hadoop.hive.serde2.objectinspector.primitive;
 20
 21import java.io.DataInput;
 22import java.io.DataOutput;
 23import java.io.IOException;
 24import java.util.HashMap;
 25import java.util.Map;
 26
 27import org.apache.hadoop.hive.serde.Constants;
 28import org.apache.hadoop.hive.serde2.io.ByteWritable;
 29import org.apache.hadoop.hive.serde2.io.DoubleWritable;
 30import org.apache.hadoop.hive.serde2.io.ShortWritable;
 31import org.apache.hadoop.hive.serde2.lazy.LazyInteger;
 32import org.apache.hadoop.hive.serde2.lazy.LazyLong;
 33import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
 34import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory;
 35import org.apache.hadoop.io.BooleanWritable;
 36import org.apache.hadoop.io.FloatWritable;
 37import org.apache.hadoop.io.IntWritable;
 38import org.apache.hadoop.io.LongWritable;
 39import org.apache.hadoop.io.NullWritable;
 40import org.apache.hadoop.io.Text;
 41import org.apache.hadoop.io.Writable;
 42import org.apache.hadoop.io.WritableUtils;
 43
 44/**
 45 * ObjectInspectorFactory is the primary way to create new ObjectInspector
 46 * instances.
 47 * 
 48 * SerDe classes should call the static functions in this library to create an
 49 * ObjectInspector to return to the caller of SerDe2.getObjectInspector().
 50 */
 51public final class PrimitiveObjectInspectorUtils {
 52
 53  /**
 54   * TypeEntry stores information about a Hive Primitive TypeInfo.
 55   */
 56  public static class PrimitiveTypeEntry implements Writable{
 57
 58    /**
 59     * The category of the PrimitiveType.
 60     */
 61    public PrimitiveObjectInspector.PrimitiveCategory primitiveCategory;
 62
 63    /**
 64     * primitiveJavaType refers to java types like int, double, etc.
 65     */
 66    public Class<?> primitiveJavaType;
 67    /**
 68     * primitiveJavaClass refers to java classes like Integer, Double, String
 69     * etc.
 70     */
 71    public Class<?> primitiveJavaClass;
 72    /**
 73     * writableClass refers to hadoop Writable classes like IntWritable,
 74     * DoubleWritable, Text etc.
 75     */
 76    public Class<?> primitiveWritableClass;
 77    /**
 78     * typeName is the name of the type as in DDL.
 79     */
 80    public String typeName;
 81
 82    PrimitiveTypeEntry(
 83        PrimitiveObjectInspector.PrimitiveCategory primitiveCategory,
 84        String typeName, Class<?> primitiveType, Class<?> javaClass,
 85        Class<?> hiveClass) {
 86      this.primitiveCategory = primitiveCategory;
 87      primitiveJavaType = primitiveType;
 88      primitiveJavaClass = javaClass;
 89      primitiveWritableClass = hiveClass;
 90      this.typeName = typeName;
 91    }
 92
 93    @Override
 94    public void readFields(DataInput in) throws IOException {
 95      primitiveCategory = WritableUtils.readEnum(in,
 96          PrimitiveObjectInspector.PrimitiveCategory.class);
 97      typeName = WritableUtils.readString(in);
 98      try {
 99        primitiveJavaType = Class.forName(WritableUtils.readString(in));
100        primitiveJavaClass = Class.forName(WritableUtils.readString(in));
101        primitiveWritableClass = Class.forName(WritableUtils.readString(in));
102      } catch (ClassNotFoundException e) {
103        throw new IOException(e);
104      }
105    }
106
107    @Override
108    public void write(DataOutput out) throws IOException {
109      WritableUtils.writeEnum(out, primitiveCategory);
110      WritableUtils.writeString(out, typeName);
111      WritableUtils.writeString(out, primitiveJavaType.getName());
112      WritableUtils.writeString(out, primitiveJavaClass.getName());
113      WritableUtils.writeString(out, primitiveWritableClass.getName());
114    }
115  }
116
117  static final Map<PrimitiveCategory, PrimitiveTypeEntry> primitiveCategoryToTypeEntry = new HashMap<PrimitiveCategory, PrimitiveTypeEntry>();
118  static final Map<Class<?>, PrimitiveTypeEntry> primitiveJavaTypeToTypeEntry = new HashMap<Class<?>, PrimitiveTypeEntry>();
119  static final Map<Class<?>, PrimitiveTypeEntry> primitiveJavaClassToTypeEntry = new HashMap<Class<?>, PrimitiveTypeEntry>();
120  static final Map<Class<?>, PrimitiveTypeEntry> primitiveWritableClassToTypeEntry = new HashMap<Class<?>, PrimitiveTypeEntry>();
121  static final Map<String, PrimitiveTypeEntry> typeNameToTypeEntry = new HashMap<String, PrimitiveTypeEntry>();
122
123  static void registerType(PrimitiveTypeEntry t) {
124    if (t.primitiveCategory != PrimitiveCategory.UNKNOWN) {
125      primitiveCategoryToTypeEntry.put(t.primitiveCategory, t);
126    }
127    if (t.primitiveJavaType != null) {
128      primitiveJavaTypeToTypeEntry.put(t.primitiveJavaType, t);
129    }
130    if (t.primitiveJavaClass != null) {
131      primitiveJavaClassToTypeEntry.put(t.primitiveJavaClass, t);
132    }
133    if (t.primitiveWritableClass != null) {
134      primitiveWritableClassToTypeEntry.put(t.primitiveWritableClass, t);
135    }
136    if (t.typeName != null) {
137      typeNameToTypeEntry.put(t.typeName, t);
138    }
139  }
140
141  public static final PrimitiveTypeEntry stringTypeEntry = new PrimitiveTypeEntry(
142      PrimitiveCategory.STRING, Constants.STRING_TYPE_NAME, null, String.class,
143      Text.class);
144  public static final PrimitiveTypeEntry booleanTypeEntry = new PrimitiveTypeEntry(
145      PrimitiveCategory.BOOLEAN, Constants.BOOLEAN_TYPE_NAME, Boolean.TYPE,
146      Boolean.class, BooleanWritable.class);
147  public static final PrimitiveTypeEntry intTypeEntry = new PrimitiveTypeEntry(
148      PrimitiveCategory.INT, Constants.INT_TYPE_NAME, Integer.TYPE,
149      Integer.class, IntWritable.class);
150  public static final PrimitiveTypeEntry longTypeEntry = new PrimitiveTypeEntry(
151      PrimitiveCategory.LONG, Constants.BIGINT_TYPE_NAME, Long.TYPE,
152      Long.class, LongWritable.class);
153  public static final PrimitiveTypeEntry floatTypeEntry = new PrimitiveTypeEntry(
154      PrimitiveCategory.FLOAT, Constants.FLOAT_TYPE_NAME, Float.TYPE,
155      Float.class, FloatWritable.class);
156  public static final PrimitiveTypeEntry voidTypeEntry = new PrimitiveTypeEntry(
157      PrimitiveCategory.VOID, Constants.VOID_TYPE_NAME, Void.TYPE, Void.class,
158      NullWritable.class);
159
160  // No corresponding Writable classes for the following 3 in hadoop 0.17.0
161  public static final PrimitiveTypeEntry doubleTypeEntry = new PrimitiveTypeEntry(
162      PrimitiveCategory.DOUBLE, Constants.DOUBLE_TYPE_NAME, Double.TYPE,
163      Double.class, DoubleWritable.class);
164  public static final PrimitiveTypeEntry byteTypeEntry = new PrimitiveTypeEntry(
165      PrimitiveCategory.BYTE, Constants.TINYINT_TYPE_NAME, Byte.TYPE,
166      Byte.class, ByteWritable.class);
167  public static final PrimitiveTypeEntry shortTypeEntry = new PrimitiveTypeEntry(
168      PrimitiveCategory.SHORT, Constants.SMALLINT_TYPE_NAME, Short.TYPE,
169      Short.class, ShortWritable.class);
170
171  // The following is a complex type for special handling
172  public static final PrimitiveTypeEntry unknownTypeEntry = new PrimitiveTypeEntry(
173      PrimitiveCategory.UNKNOWN, "unknown", null, Object.class, null);
174
175  static {
176    registerType(stringTypeEntry);
177    registerType(booleanTypeEntry);
178    registerType(intTypeEntry);
179    registerType(longTypeEntry);
180    registerType(floatTypeEntry);
181    registerType(voidTypeEntry);
182    registerType(doubleTypeEntry);
183    registerType(byteTypeEntry);
184    registerType(shortTypeEntry);
185    registerType(unknownTypeEntry);
186  }
187
188  /**
189   * Return Whether the class is a Java Primitive type or a Java Primitive
190   * class.
191   */
192  public static Class<?> primitiveJavaTypeToClass(Class<?> clazz) {
193    PrimitiveTypeEntry t = primitiveJavaTypeToTypeEntry.get(clazz);
194    return t == null ? clazz : t.primitiveJavaClass;
195  }
196
197  /**
198   * Whether the class is a Java Primitive type or a Java Primitive class.
199   */
200  public static boolean isPrimitiveJava(Class<?> clazz) {
201    return primitiveJavaTypeToTypeEntry.get(clazz) != null
202        || primitiveJavaClassToTypeEntry.get(clazz) != null;
203  }
204
205  /**
206   * Whether the class is a Java Primitive type.
207   */
208  public static boolean isPrimitiveJavaType(Class<?> clazz) {
209    return primitiveJavaTypeToTypeEntry.get(clazz) != null;
210  }
211
212  /**
213   * Whether the class is a Java Primitive class.
214   */
215  public static boolean isPrimitiveJavaClass(Class<?> clazz) {
216    return primitiveJavaClassToTypeEntry.get(clazz) != null;
217  }
218
219  /**
220   * Whether the class is a Hive Primitive Writable class.
221   */
222  public static boolean isPrimitiveWritableClass(Class<?> clazz) {
223    return primitiveWritableClassToTypeEntry.get(clazz) != null;
224  }
225
226  /**
227   * Get the typeName from a Java Primitive Type or Java PrimitiveClass.
228   */
229  public static String getTypeNameFromPrimitiveJava(Class<?> clazz) {
230    PrimitiveTypeEntry t = primitiveJavaTypeToTypeEntry.get(clazz);
231    if (t == null) {
232      t = primitiveJavaClassToTypeEntry.get(clazz);
233    }
234    return t == null ? null : t.typeName;
235  }
236
237  /**
238   * Get the typeName from a Primitive Writable Class.
239   */
240  public static String getTypeNameFromPrimitiveWritable(Class<?> clazz) {
241    PrimitiveTypeEntry t = primitiveWritableClassToTypeEntry.get(clazz);
242    return t == null ? null : t.typeName;
243  }
244
245  /**
246   * Get the typeName from a Java Primitive Type or Java PrimitiveClass.
247   */
248  public static PrimitiveTypeEntry getTypeEntryFromPrimitiveCategory(
249      PrimitiveCategory category) {
250    return primitiveCategoryToTypeEntry.get(category);
251  }
252
253  /**
254   * Get the TypeEntry for a Java Primitive Type or Java PrimitiveClass.
255   */
256  public static PrimitiveTypeEntry getTypeEntryFromPrimitiveJava(Class<?> clazz) {
257    PrimitiveTypeEntry t = primitiveJavaTypeToTypeEntry.get(clazz);
258    if (t == null) {
259      t = primitiveJavaClassToTypeEntry.get(clazz);
260    }
261    return t;
262  }
263
264  /**
265   * Get the TypeEntry for a Java Primitive Type or Java PrimitiveClass.
266   */
267  public static PrimitiveTypeEntry getTypeEntryFromPrimitiveJavaType(
268      Class<?> clazz) {
269    return primitiveJavaTypeToTypeEntry.get(clazz);
270  }
271
272  /**
273   * Get the TypeEntry for a Java Primitive Type or Java PrimitiveClass.
274   */
275  public static PrimitiveTypeEntry getTypeEntryFromPrimitiveJavaClass(
276      Class<?> clazz) {
277    return primitiveJavaClassToTypeEntry.get(clazz);
278  }
279
280  /**
281   * Get the TypeEntry for a Primitive Writable Class.
282   */
283  public static PrimitiveTypeEntry getTypeEntryFromPrimitiveWritableClass(
284      Class<?> clazz) {
285    return primitiveWritableClassToTypeEntry.get(clazz);
286  }
287
288  /**
289   * Get the TypeEntry for a Primitive Writable Class.
290   */
291  public static PrimitiveTypeEntry getTypeEntryFromTypeName(String typeName) {
292    return typeNameToTypeEntry.get(typeName);
293  }
294
295  /**
296   * Compare 2 primitive objects. Conversion not allowed. Note that NULL does
297   * not equal to NULL according to SQL standard.
298   */
299  public static boolean comparePrimitiveObjects(Object o1,
300      PrimitiveObjectInspector oi1, Object o2, PrimitiveObjectInspector oi2) {
301    if (o1 == null || o2 == null) {
302      return false;
303    }
304
305    if (oi1.getPrimitiveCategory() != oi2.getPrimitiveCategory()) {
306      return false;
307    }
308    switch (oi1.getPrimitiveCategory()) {
309    case BOOLEAN: {
310      return ((BooleanObjectInspector) oi1).get(o1) == ((BooleanObjectInspector) oi2)
311          .get(o2);
312    }
313    case BYTE: {
314      return ((ByteObjectInspector) oi1).get(o1) == ((ByteObjectInspector) oi2)
315          .get(o2);
316    }
317    case SHORT: {
318      return ((ShortObjectInspector) oi1).get(o1) == ((ShortObjectInspector) oi2)
319          .get(o2);
320    }
321    case INT: {
322      return ((IntObjectInspector) oi1).get(o1) == ((IntObjectInspector) oi2)
323          .get(o2);
324    }
325    case LONG: {
326      return ((LongObjectInspector) oi1).get(o1) == ((LongObjectInspector) oi2)
327          .get(o2);
328    }
329    case FLOAT: {
330      return ((FloatObjectInspector) oi1).get(o1) == ((FloatObjectInspector) oi2)
331          .get(o2);
332    }
333    case DOUBLE: {
334      return ((DoubleObjectInspector) oi1).get(o1) == ((DoubleObjectInspector) oi2)
335          .get(o2);
336    }
337    case STRING: {
338      Writable t1 = ((StringObjectInspector) oi1)
339          .getPrimitiveWritableObject(o1);
340      Writable t2 = ((StringObjectInspector) oi2)
341          .getPrimitiveWritableObject(o2);
342      return t1.equals(t2);
343    }
344    default:
345      return false;
346    }
347  }
348
349  /**
350   * Convert a primitive object to double.
351   */
352  public static double convertPrimitiveToDouble(Object o, PrimitiveObjectInspector oi) {
353    switch (oi.getPrimitiveCategory()) {
354    case BOOLEAN:
355      return ((BooleanObjectInspector) oi).get(o) ? 1 : 0;
356    case BYTE:
357      return ((ByteObjectInspector) oi).get(o);
358    case SHORT:
359      return ((ShortObjectInspector) oi).get(o);
360    case INT:
361      return ((IntObjectInspector) oi).get(o);
362    case LONG:
363      return ((LongObjectInspector) oi).get(o);
364    case FLOAT:
365      return ((FloatObjectInspector) oi).get(o);
366    case DOUBLE:
367      return ((DoubleObjectInspector) oi).get(o);
368    case STRING:
369      return Double.valueOf(((StringObjectInspector) oi).getPrimitiveJavaObject(o));
370    default:
371      throw new NumberFormatException();
372    }
373  }
374
375  /**
376   * Compare 2 Primitive Objects with their Object Inspector, conversions
377   * allowed. Note that NULL does not equal to NULL according to SQL standard.
378   */
379  public static boolean comparePrimitiveObjectsWithConversion(Object o1,
380      PrimitiveObjectInspector oi1, Object o2, PrimitiveObjectInspector oi2) {
381    if (o1 == null || o2 == null) {
382      return false;
383    }
384
385    if (oi1.getPrimitiveCategory() == oi2.getPrimitiveCategory()) {
386      return comparePrimitiveObjects(o1, oi1, o2, oi2);
387    }
388
389    // If not equal, convert all to double and compare
390    try {
391      return convertPrimitiveToDouble(o1, oi1) == convertPrimitiveToDouble(o2,
392          oi2);
393    } catch (NumberFormatException e) {
394      return false;
395    }
396  }
397
398  /**
399   * Get the boolean value out of a primitive object. Note that
400   * NullPointerException will be thrown if o is null. Note that
401   * NumberFormatException will be thrown if o is not a valid number.
402   */
403  public static boolean getBoolean(Object o, PrimitiveObjectInspector oi) {
404    boolean result = false;
405    switch (oi.getPrimitiveCategory()) {
406    case VOID:
407      result = false;
408      break;
409    case BOOLEAN:
410      result = ((BooleanObjectInspector) oi).get(o);
411      break;
412    case BYTE:
413      result = ((ByteObjectInspector) oi).get(o) != 0;
414      break;
415    case SHORT:
416      result = ((ShortObjectInspector) oi).get(o) != 0;
417      break;
418    case INT:
419      result = ((IntObjectInspector) oi).get(o) != 0;
420      break;
421    case LONG:
422      result = (int) ((LongObjectInspector) oi).get(o) != 0;
423      break;
424    case FLOAT:
425      result = (int) ((FloatObjectInspector) oi).get(o) != 0;
426      break;
427    case DOUBLE:
428      result = (int) ((DoubleObjectInspector) oi).get(o) != 0;
429      break;
430    case STRING:
431      StringObjectInspector soi = (StringObjectInspector) oi;
432      if (soi.preferWritable()) {
433        Text t = soi.getPrimitiveWritableObject(o);
434        result = t.getLength() != 0;
435      } else {
436        String s = soi.getPrimitiveJavaObject(o);
437        result = s.length() != 0;
438      }
439      break;
440    default:
441      throw new RuntimeException("Hive 2 Internal error: unknown type: "
442          + oi.getTypeName());
443    }
444    return result;
445  }
446
447  /**
448   * Get the byte value out of a primitive object. Note that
449   * NullPointerException will be thrown if o is null. Note that
450   * NumberFormatException will be thrown if o is not a valid number.
451   */
452  public static byte getByte(Object o, PrimitiveObjectInspector oi) {
453    return (byte) getInt(o, oi);
454  }
455
456  /**
457   * Get the short value out of a primitive object. Note that
458   * NullPointerException will be thrown if o is null. Note that
459   * NumberFormatException will be thrown if o is not a valid number.
460   */
461  public static short getShort(Object o, PrimitiveObjectInspector oi) {
462    return (short) getInt(o, oi);
463  }
464
465  /**
466   * Get the integer value out of a primitive object. Note that
467   * NullPointerException will be thrown if o is null. Note that
468   * NumberFormatException will be thrown if o is not a valid number.
469   */
470  public static int getInt(Object o, PrimitiveObjectInspector oi) {
471    int result = 0;
472    switch (oi.getPrimitiveCategory()) {
473    case VOID: {
474      result = 0;
475      break;
476    }
477    case BOOLEAN: {
478      result = (((BooleanObjectInspector) oi).get(o) ? 1 : 0);
479      break;
480    }
481    case BYTE: {
482      result = ((ByteObjectInspector) oi).get(o);
483      break;
484    }
485    case SHORT: {
486      result = ((ShortObjectInspector) oi).get(o);
487      break;
488    }
489    case INT: {
490      result = ((IntObjectInspector) oi).get(o);
491      break;
492    }
493    case LONG: {
494      result = (int) ((LongObjectInspector) oi).get(o);
495      break;
496    }
497    case FLOAT: {
498      result = (int) ((FloatObjectInspector) oi).get(o);
499      break;
500    }
501    case DOUBLE: {
502      result = (int) ((DoubleObjectInspector) oi).get(o);
503      break;
504    }
505    case STRING: {
506      StringObjectInspector soi = (StringObjectInspector) oi;
507      if (soi.preferWritable()) {
508        Text t = soi.getPrimitiveWritableObject(o);
509        result = LazyInteger.parseInt(t.getBytes(), 0, t.getLength());
510      } else {
511        String s = soi.getPrimitiveJavaObject(o);
512        result = Integer.parseInt(s);
513      }
514      break;
515    }
516    default: {
517      throw new RuntimeException("Hive 2 Internal error: unknown type: "
518          + oi.getTypeName());
519    }
520    }
521    return result;
522  }
523
524  /**
525   * Get the long value out of a primitive object. Note that
526   * NullPointerException will be thrown if o is null. Note that
527   * NumberFormatException will be thrown if o is not a valid number.
528   */
529  public static long getLong(Object o, PrimitiveObjectInspector oi) {
530    long result = 0;
531    switch (oi.getPrimitiveCategory()) {
532    case VOID:
533      result = 0;
534      break;
535    case BOOLEAN:
536      result = (((BooleanObjectInspector) oi).get(o) ? 1 : 0);
537      break;
538    case BYTE:
539      result = ((ByteObjectInspector) oi).get(o);
540      break;
541    case SHORT:
542      result = ((ShortObjectInspector) oi).get(o);
543      break;
544    case INT:
545      result = ((IntObjectInspector) oi).get(o);
546      break;
547    case LONG:
548      result = ((LongObjectInspector) oi).get(o);
549      break;
550    case FLOAT:
551      result = (long) ((FloatObjectInspector) oi).get(o);
552      break;
553    case DOUBLE:
554      result = (long) ((DoubleObjectInspector) oi).get(o);
555      break;
556    case STRING:
557      StringObjectInspector soi = (StringObjectInspector) oi;
558      if (soi.preferWritable()) {
559        Text t = soi.getPrimitiveWritableObject(o);
560        result = LazyLong.parseLong(t.getBytes(), 0, t.getLength());
561      } else {
562        String s = soi.getPrimitiveJavaObject(o);
563        result = Long.parseLong(s);
564      }
565      break;
566    default:
567      throw new RuntimeException("Hive 2 Internal error: unknown type: "
568          + oi.getTypeName());
569    }
570    return result;
571  }
572
573  /**
574   * Get the double value out of a primitive object. Note that
575   * NullPointerException will be thrown if o is null. Note that
576   * NumberFormatException will be thrown if o is not a valid number.
577   */
578  public static double getDouble(Object o, PrimitiveObjectInspector oi) {
579    double result = 0;
580    switch (oi.getPrimitiveCategory()) {
581    case VOID:
582      result = 0;
583      break;
584    case BOOLEAN:
585      result = (((BooleanObjectInspector) oi).get(o) ? 1 : 0);
586      break;
587    case BYTE:
588      result = ((ByteObjectInspector) oi).get(o);
589      break;
590    case SHORT:
591      result = ((ShortObjectInspector) oi).get(o);
592      break;
593    case INT:
594      result = ((IntObjectInspector) oi).get(o);
595      break;
596    case LONG:
597      result = ((LongObjectInspector) oi).get(o);
598      break;
599    case FLOAT:
600      result = ((FloatObjectInspector) oi).get(o);
601      break;
602    case DOUBLE:
603      result = ((DoubleObjectInspector) oi).get(o);
604      break;
605    case STRING:
606      StringObjectInspector soi = (StringObjectInspector) oi;
607      String s = soi.getPrimitiveJavaObject(o);
608      result = Double.parseDouble(s);
609      break;
610    default:
611      throw new RuntimeException("Hive 2 Internal error: unknown type: "
612          + oi.getTypeName());
613    }
614    return result;
615  }
616
617  /**
618   * Get the float value out of a primitive object. Note that
619   * NullPointerException will be thrown if o is null. Note that
620   * NumberFormatException will be thrown if o is not a valid number.
621   */
622  public static float getFloat(Object o, PrimitiveObjectInspector oi) {
623    return (float) getDouble(o, oi);
624  }
625
626  /**
627   * Get the String value out of a primitive object. Note that
628   * NullPointerException will be thrown if o is null. Note that
629   * NumberFormatException will be thrown if o is not a valid number.
630   */
631  public static String getString(Object o, PrimitiveObjectInspector oi) {
632
633    if (o == null) {
634      return null;
635    }
636
637    String result = null;
638    switch (oi.getPrimitiveCategory()) {
639    case VOID:
640      result = null;
641      break;
642    case BOOLEAN:
643      result = String.valueOf((((BooleanObjectInspector) oi).get(o)));
644      break;
645    case BYTE:
646      result = String.valueOf((((ByteObjectInspector) oi).get(o)));
647      break;
648    case SHORT:
649      result = String.valueOf((((ShortObjectInspector) oi).get(o)));
650      break;
651    case INT:
652      result = String.valueOf((((IntObjectInspector) oi).get(o)));
653      break;
654    case LONG:
655      result = String.valueOf((((LongObjectInspector) oi).get(o)));
656      break;
657    case FLOAT:
658      result = String.valueOf((((FloatObjectInspector) oi).get(o)));
659      break;
660    case DOUBLE:
661      result = String.valueOf((((DoubleObjectInspector) oi).get(o)));
662      break;
663    case STRING:
664      StringObjectInspector soi = (StringObjectInspector) oi;
665      result = soi.getPrimitiveJavaObject(o);
666      break;
667    default:
668      throw new RuntimeException("Hive 2 Internal error: unknown type: "
669          + oi.getTypeName());
670    }
671    return result;
672  }
673
674  private PrimitiveObjectInspectorUtils() {
675    // prevent instantiation
676  }
677
678}