PageRenderTime 30ms CodeModel.GetById 12ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 317 lines | 234 code | 33 blank | 50 comment | 17 complexity | 337280907158eaa39e6e5e5c0156603e 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 org.apache.hadoop.hive.serde2.ByteStream;
 22import org.apache.hadoop.hive.serde2.lazy.LazyInteger;
 23import org.apache.hadoop.hive.serde2.lazy.LazyLong;
 24import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
 25import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter;
 26import org.apache.hadoop.io.Text;
 27
 28/**
 29 * PrimitiveObjectInspectorConverter.
 30 *
 31 */
 32public class PrimitiveObjectInspectorConverter {
 33
 34  /**
 35   * A converter for the byte type.
 36   */
 37  public static class BooleanConverter implements Converter {
 38    PrimitiveObjectInspector inputOI;
 39    SettableBooleanObjectInspector outputOI;
 40    Object r;
 41
 42    public BooleanConverter(PrimitiveObjectInspector inputOI,
 43        SettableBooleanObjectInspector outputOI) {
 44      this.inputOI = inputOI;
 45      this.outputOI = outputOI;
 46      r = outputOI.create(false);
 47    }
 48
 49    @Override
 50    public Object convert(Object input) {
 51      if (input == null) {
 52        return null;
 53      }
 54      try {
 55        return outputOI.set(r, PrimitiveObjectInspectorUtils.getBoolean(input,
 56            inputOI));
 57      } catch (NumberFormatException e) {
 58        return null;
 59      }
 60    }
 61  }
 62
 63  /**
 64   * A converter for the byte type.
 65   */
 66  public static class ByteConverter implements Converter {
 67    PrimitiveObjectInspector inputOI;
 68    SettableByteObjectInspector outputOI;
 69    Object r;
 70
 71    public ByteConverter(PrimitiveObjectInspector inputOI,
 72        SettableByteObjectInspector outputOI) {
 73      this.inputOI = inputOI;
 74      this.outputOI = outputOI;
 75      r = outputOI.create((byte) 0);
 76    }
 77
 78    @Override
 79    public Object convert(Object input) {
 80      if (input == null) {
 81        return null;
 82      }
 83      try {
 84        return outputOI.set(r, PrimitiveObjectInspectorUtils.getByte(input,
 85            inputOI));
 86      } catch (NumberFormatException e) {
 87        return null;
 88      }
 89    }
 90  }
 91
 92  /**
 93   * A converter for the short type.
 94   */
 95  public static class ShortConverter implements Converter {
 96    PrimitiveObjectInspector inputOI;
 97    SettableShortObjectInspector outputOI;
 98    Object r;
 99
100    public ShortConverter(PrimitiveObjectInspector inputOI,
101        SettableShortObjectInspector outputOI) {
102      this.inputOI = inputOI;
103      this.outputOI = outputOI;
104      r = outputOI.create((short) 0);
105    }
106
107    @Override
108    public Object convert(Object input) {
109      if (input == null) {
110        return null;
111      }
112      try {
113        return outputOI.set(r, PrimitiveObjectInspectorUtils.getShort(input,
114            inputOI));
115      } catch (NumberFormatException e) {
116        return null;
117      }
118    }
119  }
120
121  /**
122   * A converter for the int type.
123   */
124  public static class IntConverter implements Converter {
125    PrimitiveObjectInspector inputOI;
126    SettableIntObjectInspector outputOI;
127    Object r;
128
129    public IntConverter(PrimitiveObjectInspector inputOI,
130        SettableIntObjectInspector outputOI) {
131      this.inputOI = inputOI;
132      this.outputOI = outputOI;
133      r = outputOI.create(0);
134    }
135
136    @Override
137    public Object convert(Object input) {
138      if (input == null) {
139        return null;
140      }
141      try {
142        return outputOI.set(r, PrimitiveObjectInspectorUtils.getInt(input,
143            inputOI));
144      } catch (NumberFormatException e) {
145        return null;
146      }
147    }
148  }
149
150  /**
151   * A converter for the long type.
152   */
153  public static class LongConverter implements Converter {
154    PrimitiveObjectInspector inputOI;
155    SettableLongObjectInspector outputOI;
156    Object r;
157
158    public LongConverter(PrimitiveObjectInspector inputOI,
159        SettableLongObjectInspector outputOI) {
160      this.inputOI = inputOI;
161      this.outputOI = outputOI;
162      r = outputOI.create(0);
163    }
164
165    @Override
166    public Object convert(Object input) {
167      if (input == null) {
168        return null;
169      }
170      try {
171        return outputOI.set(r, PrimitiveObjectInspectorUtils.getLong(input,
172            inputOI));
173      } catch (NumberFormatException e) {
174        return null;
175      }
176    }
177  }
178
179  /**
180   * A converter for the float type.
181   */
182  public static class FloatConverter implements Converter {
183    PrimitiveObjectInspector inputOI;
184    SettableFloatObjectInspector outputOI;
185    Object r;
186
187    public FloatConverter(PrimitiveObjectInspector inputOI,
188        SettableFloatObjectInspector outputOI) {
189      this.inputOI = inputOI;
190      this.outputOI = outputOI;
191      r = outputOI.create(0);
192    }
193
194    @Override
195    public Object convert(Object input) {
196      if (input == null) {
197        return null;
198      }
199      try {
200        return outputOI.set(r, PrimitiveObjectInspectorUtils.getFloat(input,
201            inputOI));
202      } catch (NumberFormatException e) {
203        return null;
204      }
205    }
206  }
207
208  /**
209   * A converter for the double type.
210   */
211  public static class DoubleConverter implements Converter {
212    PrimitiveObjectInspector inputOI;
213    SettableDoubleObjectInspector outputOI;
214    Object r;
215
216    public DoubleConverter(PrimitiveObjectInspector inputOI,
217        SettableDoubleObjectInspector outputOI) {
218      this.inputOI = inputOI;
219      this.outputOI = outputOI;
220      r = outputOI.create(0);
221    }
222
223    @Override
224    public Object convert(Object input) {
225      if (input == null) {
226        return null;
227      }
228      try {
229        return outputOI.set(r, PrimitiveObjectInspectorUtils.getDouble(input,
230            inputOI));
231      } catch (NumberFormatException e) {
232        return null;
233      }
234    }
235  }
236
237  /**
238   * A helper class to convert any primitive to Text.
239   */
240  public static class TextConverter implements Converter {
241    private PrimitiveObjectInspector inputOI;
242    private Text t = new Text();
243    private ByteStream.Output out = new ByteStream.Output();
244
245    private static byte[] trueBytes = {'T', 'R', 'U', 'E'};
246    private static byte[] falseBytes = {'F', 'A', 'L', 'S', 'E'};
247
248    public TextConverter(PrimitiveObjectInspector inputOI) {
249      // The output ObjectInspector is writableStringObjectInspector.
250      this.inputOI = inputOI;
251    }
252
253    public Text convert(Object input) {
254      if (input == null) {
255        return null;
256      }
257
258      switch (inputOI.getPrimitiveCategory()) {
259      case VOID:
260        return null;
261      case BOOLEAN:
262        t.set(((BooleanObjectInspector) inputOI).get(input) ? trueBytes
263            : falseBytes);
264        return t;
265      case BYTE:
266        out.reset();
267        LazyInteger.writeUTF8NoException(out, ((ByteObjectInspector) inputOI).get(input));
268        t.set(out.getData(), 0, out.getCount());
269        return t;
270      case SHORT:
271        out.reset();
272        LazyInteger.writeUTF8NoException(out, ((ShortObjectInspector) inputOI).get(input));
273        t.set(out.getData(), 0, out.getCount());
274        return t;
275      case INT:
276        out.reset();
277        LazyInteger.writeUTF8NoException(out, ((IntObjectInspector) inputOI).get(input));
278        t.set(out.getData(), 0, out.getCount());
279        return t;
280      case LONG:
281        out.reset();
282        LazyLong.writeUTF8NoException(out, ((LongObjectInspector) inputOI).get(input));
283        t.set(out.getData(), 0, out.getCount());
284        return t;
285      case FLOAT:
286        t.set(String.valueOf(((FloatObjectInspector) inputOI).get(input)));
287        return t;
288      case DOUBLE:
289        t.set(String.valueOf(((DoubleObjectInspector) inputOI).get(input)));
290        return t;
291      case STRING:
292        t.set(((StringObjectInspector) inputOI).getPrimitiveJavaObject(input));
293        return t;
294      default:
295        throw new RuntimeException("Hive 2 Internal error: type = " + inputOI.getTypeName());
296      }
297    }
298  }
299
300  /**
301   * A helper class to convert any primitive to String.
302   */
303  public static class StringConverter implements Converter {
304    PrimitiveObjectInspector inputOI;
305
306    public StringConverter(PrimitiveObjectInspector inputOI) {
307      // The output ObjectInspector is writableStringObjectInspector.
308      this.inputOI = inputOI;
309    }
310
311    @Override
312    public Object convert(Object input) {
313      return PrimitiveObjectInspectorUtils.getString(input, inputOI);
314    }
315  }
316
317}