1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file |
3 | // distributed with this work for additional information |
4 | // regarding copyright ownership. The ASF licenses this file |
5 | // to you under the Apache License, Version 2.0 (the |
6 | // "License"); you may not use this file except in compliance |
7 | // with the License. You may obtain a copy of the License at |
8 | // |
9 | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | // |
11 | // Unless required by applicable law or agreed to in writing, |
12 | // software distributed under the License is distributed on an |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | // KIND, either express or implied. See the License for the |
15 | // specific language governing permissions and limitations |
16 | // under the License. |
17 | |
18 | #ifndef PARQUET_UTIL_COMPARISON_H |
19 | #define PARQUET_UTIL_COMPARISON_H |
20 | |
21 | #include <algorithm> |
22 | #include <memory> |
23 | |
24 | #include "parquet/exception.h" |
25 | #include "parquet/schema.h" |
26 | #include "parquet/types.h" |
27 | #include "parquet/util/visibility.h" |
28 | |
29 | namespace parquet { |
30 | |
31 | class PARQUET_EXPORT Comparator { |
32 | public: |
33 | virtual ~Comparator() {} |
34 | static std::shared_ptr<Comparator> Make(const ColumnDescriptor* descr); |
35 | }; |
36 | |
37 | // The default comparison is SIGNED |
38 | template <typename DType> |
39 | class PARQUET_TEMPLATE_CLASS_EXPORT CompareDefault : public Comparator { |
40 | public: |
41 | typedef typename DType::c_type T; |
42 | CompareDefault() {} |
43 | virtual bool operator()(const T& a, const T& b) { return a < b; } |
44 | }; |
45 | |
46 | template <> |
47 | class PARQUET_TEMPLATE_CLASS_EXPORT CompareDefault<Int96Type> : public Comparator { |
48 | public: |
49 | CompareDefault() {} |
50 | virtual bool operator()(const Int96& a, const Int96& b) { |
51 | // Only the MSB bit is by Signed comparison |
52 | // For little-endian, this is the last bit of Int96 type |
53 | const int32_t amsb = static_cast<const int32_t>(a.value[2]); |
54 | const int32_t bmsb = static_cast<const int32_t>(b.value[2]); |
55 | if (amsb != bmsb) { |
56 | return (amsb < bmsb); |
57 | } else if (a.value[1] != b.value[1]) { |
58 | return (a.value[1] < b.value[1]); |
59 | } |
60 | return (a.value[0] < b.value[0]); |
61 | } |
62 | }; |
63 | |
64 | template <> |
65 | class PARQUET_TEMPLATE_CLASS_EXPORT CompareDefault<ByteArrayType> : public Comparator { |
66 | public: |
67 | CompareDefault() {} |
68 | virtual bool operator()(const ByteArray& a, const ByteArray& b) { |
69 | const int8_t* aptr = reinterpret_cast<const int8_t*>(a.ptr); |
70 | const int8_t* bptr = reinterpret_cast<const int8_t*>(b.ptr); |
71 | return std::lexicographical_compare(aptr, aptr + a.len, bptr, bptr + b.len); |
72 | } |
73 | }; |
74 | |
75 | template <> |
76 | class PARQUET_TEMPLATE_CLASS_EXPORT CompareDefault<FLBAType> : public Comparator { |
77 | public: |
78 | explicit CompareDefault(int length) : type_length_(length) {} |
79 | virtual bool operator()(const FLBA& a, const FLBA& b) { |
80 | const int8_t* aptr = reinterpret_cast<const int8_t*>(a.ptr); |
81 | const int8_t* bptr = reinterpret_cast<const int8_t*>(b.ptr); |
82 | return std::lexicographical_compare(aptr, aptr + type_length_, bptr, |
83 | bptr + type_length_); |
84 | } |
85 | int32_t type_length_; |
86 | }; |
87 | |
88 | typedef CompareDefault<BooleanType> CompareDefaultBoolean; |
89 | typedef CompareDefault<Int32Type> CompareDefaultInt32; |
90 | typedef CompareDefault<Int64Type> CompareDefaultInt64; |
91 | typedef CompareDefault<Int96Type> CompareDefaultInt96; |
92 | typedef CompareDefault<FloatType> CompareDefaultFloat; |
93 | typedef CompareDefault<DoubleType> CompareDefaultDouble; |
94 | typedef CompareDefault<ByteArrayType> CompareDefaultByteArray; |
95 | typedef CompareDefault<FLBAType> CompareDefaultFLBA; |
96 | |
97 | // Define Unsigned Comparators |
98 | class PARQUET_EXPORT CompareUnsignedInt32 : public CompareDefaultInt32 { |
99 | public: |
100 | bool operator()(const int32_t& a, const int32_t& b) override; |
101 | }; |
102 | |
103 | class PARQUET_EXPORT CompareUnsignedInt64 : public CompareDefaultInt64 { |
104 | public: |
105 | bool operator()(const int64_t& a, const int64_t& b) override; |
106 | }; |
107 | |
108 | class PARQUET_EXPORT CompareUnsignedInt96 : public CompareDefaultInt96 { |
109 | public: |
110 | bool operator()(const Int96& a, const Int96& b) override; |
111 | }; |
112 | |
113 | class PARQUET_EXPORT CompareUnsignedByteArray : public CompareDefaultByteArray { |
114 | public: |
115 | bool operator()(const ByteArray& a, const ByteArray& b) override; |
116 | }; |
117 | |
118 | class PARQUET_EXPORT CompareUnsignedFLBA : public CompareDefaultFLBA { |
119 | public: |
120 | explicit CompareUnsignedFLBA(int length); |
121 | bool operator()(const FLBA& a, const FLBA& b) override; |
122 | }; |
123 | |
124 | PARQUET_EXTERN_TEMPLATE CompareDefault<BooleanType>; |
125 | PARQUET_EXTERN_TEMPLATE CompareDefault<Int32Type>; |
126 | PARQUET_EXTERN_TEMPLATE CompareDefault<Int64Type>; |
127 | PARQUET_EXTERN_TEMPLATE CompareDefault<Int96Type>; |
128 | PARQUET_EXTERN_TEMPLATE CompareDefault<FloatType>; |
129 | PARQUET_EXTERN_TEMPLATE CompareDefault<DoubleType>; |
130 | PARQUET_EXTERN_TEMPLATE CompareDefault<ByteArrayType>; |
131 | PARQUET_EXTERN_TEMPLATE CompareDefault<FLBAType>; |
132 | |
133 | } // namespace parquet |
134 | |
135 | #endif // PARQUET_UTIL_COMPARISON_H |
136 | |