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