Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
parquet-plain-test.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <iostream>
18 #include <limits.h>
19 #include <gtest/gtest.h>
20 #include "exec/parquet-common.h"
21 #include "runtime/decimal-value.h"
24 
25 #include "common/names.h"
26 
27 namespace impala {
28 
29 template <typename T>
30 void TestType(const T& v, int expected_byte_size) {
31  uint8_t buffer[expected_byte_size];
32  int encoded_size = ParquetPlainEncoder::Encode(buffer, expected_byte_size, v);
33  EXPECT_EQ(encoded_size, expected_byte_size);
34 
35  T result;
36  int decoded_size = ParquetPlainEncoder::Decode(buffer, expected_byte_size, &result);
37  EXPECT_EQ(decoded_size, expected_byte_size);
38  EXPECT_EQ(result, v);
39 }
40 
41 TEST(PlainEncoding, Basic) {
42  int8_t i8 = 12;
43  int16_t i16 = 123;
44  int32_t i32 = 1234;
45  int64_t i64 = 12345;
46  float f = 1.23;
47  double d = 1.23456;
48  StringValue sv("Hello");
49  TimestampValue tv;
50 
51  TestType(i8, sizeof(int32_t));
52  TestType(i16, sizeof(int32_t));
53  TestType(i32, sizeof(int32_t));
54  TestType(i64, sizeof(int64_t));
55  TestType(f, sizeof(float));
56  TestType(d, sizeof(double));
57  TestType(sv, sizeof(int32_t) + sv.len);
58  TestType(tv, 12);
59 
60  TestType(Decimal4Value(1234), sizeof(Decimal4Value));
61  TestType(Decimal4Value(-1234), sizeof(Decimal4Value));
62 
63  TestType(Decimal8Value(1234), sizeof(Decimal8Value));
64  TestType(Decimal8Value(-1234), sizeof(Decimal8Value));
65  TestType(Decimal8Value(std::numeric_limits<int32_t>::max()), sizeof(Decimal8Value));
66  TestType(Decimal8Value(std::numeric_limits<int32_t>::min()), sizeof(Decimal8Value));
67 
68  TestType(Decimal16Value(1234), 16);
69  TestType(Decimal16Value(-1234), 16);
70  TestType(Decimal16Value(std::numeric_limits<int32_t>::max()), sizeof(Decimal16Value));
71  TestType(Decimal16Value(std::numeric_limits<int32_t>::min()), sizeof(Decimal16Value));
72  TestType(Decimal16Value(std::numeric_limits<int64_t>::max()), sizeof(Decimal16Value));
73  TestType(Decimal16Value(std::numeric_limits<int64_t>::min()), sizeof(Decimal16Value));
74 
75  // two digit values can be encoded with any byte size.
76  for (int i = 1; i <=16; ++i) {
77  if (i <= 4) {
78  TestType(Decimal4Value(i), i);
79  TestType(Decimal4Value(-i), i);
80  }
81  if (i <= 8) {
82  TestType(Decimal8Value(i), i);
83  TestType(Decimal8Value(-i), i);
84  }
85  TestType(Decimal16Value(i), i);
86  TestType(Decimal16Value(-i), i);
87  }
88 }
89 
90 TEST(PlainEncoding, DecimalBigEndian) {
91  // Test Basic can pass if we make the same error in encode and decode.
92  // Verify the bytes are actually big endian.
93  uint8_t buffer[] = {
94  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
95  };
96 
97  // Manually generate this to avoid potential bugs in BitUtil
98  uint8_t buffer_swapped[] = {
99  15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
100  };
101  uint8_t result_buffer[16];
102 
103  Decimal4Value d4;
104  Decimal8Value d8;
105  Decimal16Value d16;
106 
107  memcpy(&d4, buffer, sizeof(d4));
108  memcpy(&d8, buffer, sizeof(d8));
109  memcpy(&d16, buffer, sizeof(d16));
110 
111  int size = ParquetPlainEncoder::Encode(result_buffer, sizeof(d4), d4);
112  DCHECK_EQ(size, sizeof(d4));
113  DCHECK_EQ(memcmp(result_buffer, buffer_swapped + 16 - sizeof(d4), sizeof(d4)), 0);
114 
115  size = ParquetPlainEncoder::Encode(result_buffer, sizeof(d8), d8);
116  DCHECK_EQ(size, sizeof(d8));
117  DCHECK_EQ(memcmp(result_buffer, buffer_swapped + 16 - sizeof(d8), sizeof(d8)), 0);
118 
119  size = ParquetPlainEncoder::Encode(result_buffer, sizeof(d16), d16);
120  DCHECK_EQ(size, sizeof(d16));
121  DCHECK_EQ(memcmp(result_buffer, buffer_swapped + 16 - sizeof(d16), sizeof(d16)), 0);
122 }
123 
124 }
125 
126 int main(int argc, char **argv) {
128  ::testing::InitGoogleTest(&argc, argv);
129  return RUN_ALL_TESTS();
130 }
DecimalValue< int128_t > Decimal16Value
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
static int Encode(uint8_t *buffer, int fixed_len_size, const T &t)
DecimalValue< int64_t > Decimal8Value
void TestType(const T &v, int expected_byte_size)
DecimalValue< int32_t > Decimal4Value
int main(int argc, char **argv)
static int Decode(uint8_t *buffer, int fixed_len_size, T *v)
static void Init()
Initialize CpuInfo.
Definition: cpu-info.cc:75