PageRenderTime 80ms CodeModel.GetById 14ms app.highlight 62ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 416 lines | 341 code | 32 blank | 43 comment | 2 complexity | 40e5286e60566a348ab455f19a651890 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 */
 18package org.apache.hadoop.hive.serde2.lazy;
 19
 20import junit.framework.TestCase;
 21
 22import org.apache.hadoop.hive.serde2.ByteStream;
 23import org.apache.hadoop.hive.serde2.io.ByteWritable;
 24import org.apache.hadoop.hive.serde2.io.DoubleWritable;
 25import org.apache.hadoop.hive.serde2.io.ShortWritable;
 26import org.apache.hadoop.hive.serde2.lazy.objectinspector.primitive.LazyPrimitiveObjectInspectorFactory;
 27import org.apache.hadoop.io.IntWritable;
 28import org.apache.hadoop.io.LongWritable;
 29import org.apache.hadoop.io.Text;
 30
 31/**
 32 * TestLazyPrimitive.
 33 *
 34 */
 35public class TestLazyPrimitive extends TestCase {
 36
 37  /**
 38   * Initialize the LazyObject with the parameters, wrapping the byte[]
 39   * automatically.
 40   */
 41  public static void initLazyObject(LazyObject lo, byte[] data, int start,
 42      int length) {
 43    ByteArrayRef b = new ByteArrayRef();
 44    b.setData(data);
 45    lo.init(b, start, length);
 46  }
 47
 48  /**
 49   * Test the LazyByte class.
 50   */
 51  public void testLazyByte() throws Throwable {
 52    try {
 53      LazyByte b = new LazyByte(
 54          LazyPrimitiveObjectInspectorFactory.LAZY_BYTE_OBJECT_INSPECTOR);
 55      initLazyObject(b, new byte[] {'0'}, 0, 0);
 56      assertNull(b.getWritableObject());
 57      initLazyObject(b, new byte[] {'0'}, 0, 1);
 58      assertEquals(new ByteWritable((byte) 0), b.getWritableObject());
 59      initLazyObject(b, new byte[] {'+', '0'}, 0, 2);
 60      assertEquals(new ByteWritable((byte) 0), b.getWritableObject());
 61      initLazyObject(b, new byte[] {'-', '0'}, 0, 2);
 62      assertEquals(new ByteWritable((byte) 0), b.getWritableObject());
 63      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 1);
 64      assertEquals(new ByteWritable((byte) 1), b.getWritableObject());
 65      initLazyObject(b, new byte[] {'a', '-', '1'}, 1, 2);
 66      assertEquals(new ByteWritable((byte) -1), b.getWritableObject());
 67      initLazyObject(b, new byte[] {'a', '+', '1'}, 1, 2);
 68      assertEquals(new ByteWritable((byte) 1), b.getWritableObject());
 69      initLazyObject(b, new byte[] {'-', '1', '2', '8'}, 0, 4);
 70      assertEquals(new ByteWritable((byte) -128), b.getWritableObject());
 71      initLazyObject(b, new byte[] {'+', '1', '2', '7'}, 0, 4);
 72      assertEquals(new ByteWritable((byte) 127), b.getWritableObject());
 73
 74      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 2);
 75      assertNull(b.getWritableObject());
 76      initLazyObject(b, new byte[] {'+', '1', '2', '8'}, 0, 4);
 77      assertNull(b.getWritableObject());
 78      initLazyObject(b, new byte[] {'-', '1', '2', '9'}, 0, 4);
 79      assertNull(b.getWritableObject());
 80      initLazyObject(b, new byte[] {'-', '1', '2', '3'}, 0, 1);
 81      assertNull(b.getWritableObject());
 82      initLazyObject(b, new byte[] {'+', '1', '2', '3'}, 0, 1);
 83      assertNull(b.getWritableObject());
 84
 85    } catch (Throwable e) {
 86      e.printStackTrace();
 87      throw e;
 88    }
 89  }
 90
 91  /**
 92   * Test the LazyShort class.
 93   */
 94  public void testLazyShort() throws Throwable {
 95    try {
 96      LazyShort b = new LazyShort(
 97          LazyPrimitiveObjectInspectorFactory.LAZY_SHORT_OBJECT_INSPECTOR);
 98      initLazyObject(b, new byte[] {'0'}, 0, 0);
 99      assertNull(b.getWritableObject());
100      initLazyObject(b, new byte[] {'0'}, 0, 1);
101      assertEquals(new ShortWritable((short) 0), b.getWritableObject());
102      initLazyObject(b, new byte[] {'+', '0'}, 0, 2);
103      assertEquals(new ShortWritable((short) 0), b.getWritableObject());
104      initLazyObject(b, new byte[] {'-', '0'}, 0, 2);
105      assertEquals(new ShortWritable((short) 0), b.getWritableObject());
106      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 1);
107      assertEquals(new ShortWritable((short) 1), b.getWritableObject());
108      initLazyObject(b, new byte[] {'a', '-', '1'}, 1, 2);
109      assertEquals(new ShortWritable((short) -1), b.getWritableObject());
110      initLazyObject(b, new byte[] {'a', '+', '1'}, 1, 2);
111      assertEquals(new ShortWritable((short) 1), b.getWritableObject());
112      initLazyObject(b, new byte[] {'-', '1', '2', '8'}, 0, 4);
113      assertEquals(new ShortWritable((short) -128), b.getWritableObject());
114      initLazyObject(b, new byte[] {'+', '1', '2', '7'}, 0, 4);
115      assertEquals(new ShortWritable((short) 127), b.getWritableObject());
116      initLazyObject(b, new byte[] {'-', '3', '2', '7', '6', '8'}, 0, 6);
117      assertEquals(new ShortWritable((short) -32768), b.getWritableObject());
118      initLazyObject(b, new byte[] {'+', '3', '2', '7', '6', '7'}, 0, 6);
119      assertEquals(new ShortWritable((short) 32767), b.getWritableObject());
120
121      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 2);
122      assertNull(b.getWritableObject());
123      initLazyObject(b, new byte[] {'-', '3', '2', '7', '6', '9'}, 0, 6);
124      assertNull(b.getWritableObject());
125      initLazyObject(b, new byte[] {'+', '3', '2', '7', '6', '8'}, 0, 6);
126      assertNull(b.getWritableObject());
127      initLazyObject(b, new byte[] {'-', '1', '2', '3'}, 0, 1);
128      assertNull(b.getWritableObject());
129      initLazyObject(b, new byte[] {'+', '1', '2', '3'}, 0, 1);
130      assertNull(b.getWritableObject());
131
132    } catch (Throwable e) {
133      e.printStackTrace();
134      throw e;
135    }
136  }
137
138  /**
139   * Test the LazyInteger class.
140   */
141  public void testLazyInteger() throws Throwable {
142    try {
143      LazyInteger b = new LazyInteger(
144          LazyPrimitiveObjectInspectorFactory.LAZY_INT_OBJECT_INSPECTOR);
145      initLazyObject(b, new byte[] {'0'}, 0, 0);
146      assertNull(b.getWritableObject());
147      initLazyObject(b, new byte[] {'0'}, 0, 1);
148      assertEquals(new IntWritable(0), b.getWritableObject());
149      initLazyObject(b, new byte[] {'+', '0'}, 0, 2);
150      assertEquals(new IntWritable(0), b.getWritableObject());
151      initLazyObject(b, new byte[] {'-', '0'}, 0, 2);
152      assertEquals(new IntWritable(0), b.getWritableObject());
153      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 1);
154      assertEquals(new IntWritable(1), b.getWritableObject());
155      initLazyObject(b, new byte[] {'a', '-', '1'}, 1, 2);
156      assertEquals(new IntWritable(-1), b.getWritableObject());
157      initLazyObject(b, new byte[] {'a', '+', '1'}, 1, 2);
158      assertEquals(new IntWritable(1), b.getWritableObject());
159      initLazyObject(b, new byte[] {'-', '1', '2', '8'}, 0, 4);
160      assertEquals(new IntWritable(-128), b.getWritableObject());
161      initLazyObject(b, new byte[] {'+', '1', '2', '7'}, 0, 4);
162      assertEquals(new IntWritable(127), b.getWritableObject());
163      initLazyObject(b, new byte[] {'-', '3', '2', '7', '6', '8'}, 0, 6);
164      assertEquals(new IntWritable(-32768), b.getWritableObject());
165      initLazyObject(b, new byte[] {'+', '3', '2', '7', '6', '7'}, 0, 6);
166      assertEquals(new IntWritable(32767), b.getWritableObject());
167      initLazyObject(b, new byte[] {'-', '2', '1', '4', '7', '4', '8', '3',
168          '6', '4', '8'}, 0, 11);
169      assertEquals(new IntWritable(-2147483648), b.getWritableObject());
170      initLazyObject(b, new byte[] {'+', '2', '1', '4', '7', '4', '8', '3',
171          '6', '4', '7'}, 0, 11);
172      assertEquals(new IntWritable(2147483647), b.getWritableObject());
173
174      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 2);
175      assertNull(b.getWritableObject());
176      initLazyObject(b, new byte[] {'-', '2', '1', '4', '7', '4', '8', '3',
177          '6', '4', '9'}, 0, 11);
178      assertNull(b.getWritableObject());
179      initLazyObject(b, new byte[] {'+', '2', '1', '4', '7', '4', '8', '3',
180          '6', '4', '8'}, 0, 11);
181      assertNull(b.getWritableObject());
182      initLazyObject(b, new byte[] {'-', '1', '2', '3'}, 0, 1);
183      assertNull(b.getWritableObject());
184      initLazyObject(b, new byte[] {'+', '1', '2', '3'}, 0, 1);
185      assertNull(b.getWritableObject());
186
187    } catch (Throwable e) {
188      e.printStackTrace();
189      throw e;
190    }
191  }
192
193  /**
194   * Test the LazyLong class.
195   */
196  public void testLazyLong() throws Throwable {
197    try {
198      LazyLong b = new LazyLong(
199          LazyPrimitiveObjectInspectorFactory.LAZY_LONG_OBJECT_INSPECTOR);
200      initLazyObject(b, new byte[] {'0'}, 0, 0);
201      assertNull(b.getWritableObject());
202      initLazyObject(b, new byte[] {'0'}, 0, 1);
203      assertEquals(new LongWritable(0), b.getWritableObject());
204      initLazyObject(b, new byte[] {'+', '0'}, 0, 2);
205      assertEquals(new LongWritable(0), b.getWritableObject());
206      initLazyObject(b, new byte[] {'-', '0'}, 0, 2);
207      assertEquals(new LongWritable(0), b.getWritableObject());
208      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 1);
209      assertEquals(new LongWritable(1), b.getWritableObject());
210      initLazyObject(b, new byte[] {'a', '-', '1'}, 1, 2);
211      assertEquals(new LongWritable(-1), b.getWritableObject());
212      initLazyObject(b, new byte[] {'a', '+', '1'}, 1, 2);
213      assertEquals(new LongWritable(1), b.getWritableObject());
214      initLazyObject(b, new byte[] {'-', '1', '2', '8'}, 0, 4);
215      assertEquals(new LongWritable(-128), b.getWritableObject());
216      initLazyObject(b, new byte[] {'+', '1', '2', '7'}, 0, 4);
217      assertEquals(new LongWritable(127), b.getWritableObject());
218      initLazyObject(b, new byte[] {'-', '3', '2', '7', '6', '8'}, 0, 6);
219      assertEquals(new LongWritable(-32768), b.getWritableObject());
220      initLazyObject(b, new byte[] {'+', '3', '2', '7', '6', '7'}, 0, 6);
221      assertEquals(new LongWritable(32767), b.getWritableObject());
222      initLazyObject(b, new byte[] {'-', '2', '1', '4', '7', '4', '8', '3',
223          '6', '4', '8'}, 0, 11);
224      assertEquals(new LongWritable(-2147483648), b.getWritableObject());
225      initLazyObject(b, new byte[] {'+', '2', '1', '4', '7', '4', '8', '3',
226          '6', '4', '7'}, 0, 11);
227      assertEquals(new LongWritable(2147483647), b.getWritableObject());
228      initLazyObject(b, new byte[] {'-', '9', '2', '2', '3', '3', '7', '2',
229          '0', '3', '6', '8', '5', '4', '7', '7', '5', '8', '0', '8'}, 0, 20);
230      assertEquals(new LongWritable(-9223372036854775808L), b
231          .getWritableObject());
232      initLazyObject(b, new byte[] {'+', '9', '2', '2', '3', '3', '7', '2',
233          '0', '3', '6', '8', '5', '4', '7', '7', '5', '8', '0', '7'}, 0, 20);
234      assertEquals(new LongWritable(9223372036854775807L), b
235          .getWritableObject());
236
237      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 2);
238      assertNull(b.getWritableObject());
239      initLazyObject(b, new byte[] {'-', '9', '2', '2', '3', '3', '7', '2',
240          '0', '3', '6', '8', '5', '4', '7', '7', '5', '8', '0', '9'}, 0, 20);
241      assertNull(b.getWritableObject());
242      initLazyObject(b, new byte[] {'+', '9', '2', '2', '3', '3', '7', '2',
243          '0', '3', '6', '8', '5', '4', '7', '7', '5', '8', '0', '8'}, 0, 20);
244      assertNull(b.getWritableObject());
245      initLazyObject(b, new byte[] {'-', '1', '2', '3'}, 0, 1);
246      assertNull(b.getWritableObject());
247      initLazyObject(b, new byte[] {'+', '1', '2', '3'}, 0, 1);
248      assertNull(b.getWritableObject());
249
250    } catch (Throwable e) {
251      e.printStackTrace();
252      throw e;
253    }
254  }
255
256  /**
257   * Test the LazyDouble class.
258   */
259  public void testLazyDouble() throws Throwable {
260    try {
261      LazyDouble b = new LazyDouble(
262          LazyPrimitiveObjectInspectorFactory.LAZY_DOUBLE_OBJECT_INSPECTOR);
263      initLazyObject(b, new byte[] {'0'}, 0, 0);
264      assertNull(b.getWritableObject());
265      initLazyObject(b, new byte[] {'0'}, 0, 1);
266      assertEquals(new DoubleWritable(0), b.getWritableObject());
267      initLazyObject(b, new byte[] {'+', '0'}, 0, 2);
268      assertEquals(new DoubleWritable(0), b.getWritableObject());
269      initLazyObject(b, new byte[] {'-', '0'}, 0, 2);
270      assertEquals(new DoubleWritable(-0.0), b.getWritableObject());
271      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 1);
272      assertEquals(new DoubleWritable(1), b.getWritableObject());
273      initLazyObject(b, new byte[] {'a', '-', '1'}, 1, 2);
274      assertEquals(new DoubleWritable(-1), b.getWritableObject());
275      initLazyObject(b, new byte[] {'a', '+', '1'}, 1, 2);
276      assertEquals(new DoubleWritable(1), b.getWritableObject());
277      initLazyObject(b, new byte[] {'-', '1', '2', '8'}, 0, 4);
278      assertEquals(new DoubleWritable(-128), b.getWritableObject());
279      initLazyObject(b, new byte[] {'+', '1', '2', '7'}, 0, 4);
280      assertEquals(new DoubleWritable(127), b.getWritableObject());
281      initLazyObject(b, new byte[] {'-', '3', '2', '7', '6', '8'}, 0, 6);
282      assertEquals(new DoubleWritable(-32768), b.getWritableObject());
283      initLazyObject(b, new byte[] {'+', '3', '2', '7', '6', '7'}, 0, 6);
284      assertEquals(new DoubleWritable(32767), b.getWritableObject());
285      initLazyObject(b, new byte[] {'-', '2', '1', '4', '7', '4', '8', '3',
286          '6', '4', '8'}, 0, 11);
287      assertEquals(new DoubleWritable(-2147483648), b.getWritableObject());
288      initLazyObject(b, new byte[] {'+', '2', '1', '4', '7', '4', '8', '3',
289          '6', '4', '7'}, 0, 11);
290      assertEquals(new DoubleWritable(2147483647), b.getWritableObject());
291      initLazyObject(b, new byte[] {'-', '9', '2', '2', '3', '3', '7', '2',
292          '0', '3', '6', '8', '5', '4', '7', '7', '5', '8', '0', '8'}, 0, 20);
293      assertEquals(new DoubleWritable(-9223372036854775808L), b
294          .getWritableObject());
295      initLazyObject(b, new byte[] {'+', '9', '2', '2', '3', '3', '7', '2',
296          '0', '3', '6', '8', '5', '4', '7', '7', '5', '8', '0', '7'}, 0, 20);
297      assertEquals(new DoubleWritable(9223372036854775807L), b
298          .getWritableObject());
299
300      initLazyObject(b, new byte[] {'-', '3', '.', '7', '6', '8'}, 0, 6);
301      assertEquals(new DoubleWritable(-3.768), b.getWritableObject());
302      initLazyObject(b, new byte[] {'+', '3', '.', '7', '6', '7'}, 0, 6);
303      assertEquals(new DoubleWritable(3.767), b.getWritableObject());
304      initLazyObject(b, new byte[] {'-', '2', '.', '4', '7', '4', '8', '3',
305          '6', 'e', '8'}, 0, 11);
306      assertEquals(new DoubleWritable(-2.474836e8), b.getWritableObject());
307      initLazyObject(b, new byte[] {'+', '2', '.', '4', '7', '4', '8', '3',
308          'E', '-', '7'}, 0, 11);
309      assertEquals(new DoubleWritable(2.47483E-7), b.getWritableObject());
310      initLazyObject(b, new byte[] {'-', '.', '4', '7', '4', '8', '3', '6',
311          'e', '8'}, 0, 10);
312      assertEquals(new DoubleWritable(-.474836e8), b.getWritableObject());
313      initLazyObject(b, new byte[] {'+', '.', '4', '7', '4', '8', '3', 'E',
314          '-', '7'}, 0, 10);
315      assertEquals(new DoubleWritable(.47483E-7), b.getWritableObject());
316      initLazyObject(b, new byte[] {'-', '2', '1', '4', '7', '4', '8', '3',
317          '6', '4', '.'}, 0, 11);
318      assertEquals(new DoubleWritable(-214748364.), b.getWritableObject());
319      initLazyObject(b, new byte[] {'+', '2', '1', '4', '7', '4', '8', '3',
320          '6', '4', '.'}, 0, 11);
321      assertEquals(new DoubleWritable(+214748364.), b.getWritableObject());
322
323      initLazyObject(b, new byte[] {'.', '0'}, 0, 2);
324      assertEquals(new DoubleWritable(.0), b.getWritableObject());
325      initLazyObject(b, new byte[] {'0', '.'}, 0, 2);
326      assertEquals(new DoubleWritable(0.), b.getWritableObject());
327
328      initLazyObject(b, new byte[] {'a', '1', 'b'}, 1, 2);
329      assertNull(b.getWritableObject());
330      assertNull(b.getWritableObject());
331      initLazyObject(b, new byte[] {'.', '1', '2', '3'}, 0, 1);
332      assertNull(b.getWritableObject());
333      initLazyObject(b, new byte[] {'-', '1', '2', '3'}, 0, 1);
334      assertNull(b.getWritableObject());
335      initLazyObject(b, new byte[] {'+', '1', '2', '3'}, 0, 1);
336      assertNull(b.getWritableObject());
337
338      initLazyObject(b, new byte[] {'-', '1', 'e', '3', '3', '3', '3', '3', '3'}, 0, 9);
339      assertEquals(new DoubleWritable(Double.NEGATIVE_INFINITY), b
340          .getWritableObject());
341      initLazyObject(b, new byte[] {'+', '1', 'e', '3', '3', '3', '3', '3', '3'}, 0, 9);
342      assertEquals(new DoubleWritable(Double.POSITIVE_INFINITY), b
343          .getWritableObject());
344
345      initLazyObject(b, new byte[] {'+', '1', 'e', '-', '3', '3', '3', '3', '3'}, 0, 8);
346      assertEquals(new DoubleWritable(0), b.getWritableObject());
347      initLazyObject(b, new byte[] {'-', '1', 'e', '-', '3', '3', '3', '3', '3'}, 0, 8);
348      assertEquals(new DoubleWritable(-0.0), b.getWritableObject());
349
350    } catch (Throwable e) {
351      e.printStackTrace();
352      throw e;
353    }
354  }
355
356  /**
357   * Test the LazyString class.
358   */
359  public void testLazyString() throws Throwable {
360    try {
361      LazyString b = new LazyString(LazyPrimitiveObjectInspectorFactory
362          .getLazyStringObjectInspector(false, (byte) 0));
363      initLazyObject(b, new byte[] {'0'}, 0, 0);
364      assertEquals(new Text(""), b.getWritableObject());
365      initLazyObject(b, new byte[] {'0'}, 0, 1);
366      assertEquals(new Text("0"), b.getWritableObject());
367      initLazyObject(b, new byte[] {'0', '1', '2'}, 1, 1);
368      assertEquals(new Text("1"), b.getWritableObject());
369
370    } catch (Throwable e) {
371      e.printStackTrace();
372      throw e;
373    }
374  }
375
376  public void testLazyIntegerWrite() throws Throwable {
377    try {
378      ByteStream.Output out = new ByteStream.Output();
379
380      int[] tests = {0, -1, 1, -10, 10, -123, 123, Integer.MIN_VALUE,
381          Integer.MIN_VALUE + 1, Integer.MAX_VALUE, Integer.MAX_VALUE - 1};
382      for (int v : tests) {
383        out.reset();
384        LazyInteger.writeUTF8(out, v);
385        Text t = new Text();
386        t.set(out.getData(), 0, out.getCount());
387        assertEquals(String.valueOf(v), t.toString());
388      }
389
390    } catch (Throwable e) {
391      e.printStackTrace();
392      throw e;
393    }
394  }
395
396  public void testLazyLongWrite() throws Throwable {
397    try {
398      ByteStream.Output out = new ByteStream.Output();
399
400      long[] tests = {0L, -1, 1, -10, 10, -123, 123, Long.MIN_VALUE,
401          Long.MIN_VALUE + 1, Long.MAX_VALUE, Long.MAX_VALUE - 1};
402      for (long v : tests) {
403        out.reset();
404        LazyLong.writeUTF8(out, v);
405        Text t = new Text();
406        t.set(out.getData(), 0, out.getCount());
407        assertEquals(String.valueOf(v), t.toString());
408      }
409
410    } catch (Throwable e) {
411      e.printStackTrace();
412      throw e;
413    }
414  }
415
416}