-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCompressedStoreUtil.hpp
262 lines (238 loc) · 9.97 KB
/
CompressedStoreUtil.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_STORAGE_COMPRESSED_STORE_UTIL_HPP_
#define QUICKSTEP_STORAGE_COMPRESSED_STORE_UTIL_HPP_
#include <cstddef>
#include <cstdint>
#include "types/operations/comparisons/ComparisonID.hpp"
#include "utility/Macros.hpp"
namespace quickstep {
class CatalogRelationSchema;
class ComparisonPredicate;
class CompressionDictionary;
class Type;
class TypedValue;
/** \addtogroup Storage
* @{
*/
enum class PredicateTransformResultType {
kAll = 0,
kNone,
kBasicComparison,
kRangeComparison
};
/**
* @brief The return value of a call to TransformPredicateOnCompressedAttribute.
* This structure represents a transformed predicate, which might
* indicate to get all (non-null) values, get no values, or use a
* transformed comparison predicate.
*/
struct PredicateTransformResult {
/**
* @brief The constructor used for constructing a kAll or kNone result.
*
* @param type_arg The transform result type.
**/
explicit PredicateTransformResult(PredicateTransformResultType type_arg)
: type(type_arg),
comp(ComparisonID::kNumComparisonIDs),
first_literal(0),
second_literal(0),
exclude_nulls(false) {
DEBUG_ASSERT(type_arg == PredicateTransformResultType::kAll
|| type_arg == PredicateTransformResultType::kNone);
}
/**
* @brief The constructor used for constructing a kBasicComparison result.
*
* @param type_arg The transform result type.
* @param comp_arg The comparator of the transformed predicate.
* @param literal_arg The code of the literal in the transformed predicate.
**/
PredicateTransformResult(PredicateTransformResultType type_arg,
ComparisonID comp_arg,
std::uint32_t literal_arg)
: type(type_arg),
comp(comp_arg),
first_literal(literal_arg),
second_literal(0),
exclude_nulls(false) {
DEBUG_ASSERT(type_arg == PredicateTransformResultType::kBasicComparison);
}
/**
* @brief The constructor used for constructing a kRangeComparison result.
* The range can be represented as [first_literal, second_literal).
*
* @param type_arg The transform result type.
* @param first_literal_arg The code of first literal in the transformed
* predicate.
* @param second_literal_arg The code of the second literal in the
* transformed predicate.
**/
PredicateTransformResult(PredicateTransformResultType type_arg,
std::uint32_t first_literal_arg,
std::uint32_t second_literal_arg)
: type(type_arg),
comp(ComparisonID::kNumComparisonIDs),
first_literal(first_literal_arg),
second_literal(second_literal_arg),
exclude_nulls(false) {
DEBUG_ASSERT(type_arg == PredicateTransformResultType::kRangeComparison);
}
/**
* @brief The constructor used for constructing a kBasicComparison result
* with a Comparison::kNotEqual comparator.
*
* @param type_arg The transform result type.
* @param comp_arg The comparator of the transformed predicate.
* @param literal_arg The code of the literal in the kNotEqual comparator.
* @param null_arg The code of NULL.
* @param exclude_null_arg True if the transformed predicate excludes NULLs.
**/
PredicateTransformResult(PredicateTransformResultType type_arg,
ComparisonID comp_arg,
std::uint32_t literal_arg,
std::uint32_t null_arg,
bool exclude_nulls_arg)
: type(type_arg),
comp(comp_arg),
first_literal(literal_arg),
second_literal(null_arg),
exclude_nulls(exclude_nulls_arg) {
DEBUG_ASSERT(type_arg == PredicateTransformResultType::kBasicComparison);
DEBUG_ASSERT(comp_arg == ComparisonID::kNotEqual);
}
PredicateTransformResultType type;
ComparisonID comp;
std::uint32_t first_literal;
std::uint32_t second_literal;
bool exclude_nulls;
};
/**
* @brief A class which contains static helper methods for transforming
* a comparison predicate on a compressed attribute. This class is
* used by CompressedTupleStorageSubBlock.
**/
class CompressedAttributePredicateTransformer {
public:
/**
* @brief Transform a predicate on a compressed attribute of
* a CompressedTupleStorageSubBlock.
* @note Currently this method can only support a simple comparison of a
* literal value with a compressed attribute.
*
* @param relation The relation which the comparison attribute belongs to.
* @param predicate A predicate to attempt to transform.
* @param dictionary A dictionary used to compress this attribute. If nullptr,
* we use the truncation compression.
* @param maximum_truncated_value The maximum truncated value in the
* compressed attribute. This argument is useful only when dictionary
* is nullptr.
* @return A PredicateTransformResult that contains the transformed predicate.
**/
static PredicateTransformResult TransformPredicateOnCompressedAttribute(
const CatalogRelationSchema &relation,
const ComparisonPredicate &predicate,
const CompressionDictionary *dictionary,
const std::int64_t maximum_truncated_value);
/**
* @brief Get a long value for a comparison of a truncated attribute with a
* literal TypedValue. If the literal is a Float or Double with a
* fractional part, the value will be adjusted so that the comparison
* will still evaluate correctly.
*
* @param comp The ID of the comparison being evaluated (the order is
* 'attribute comp literal'). Must be kLess, kLessOrEqual, kGreater,
* or kGreaterOrEqual (kEqual and kNotEqual are not supported).
* @param right_literal A literal of a numeric type (Int, Long, Float, or
* Double) to get the effective truncated value for.
* @param right_literal_type The specific concrete Type that right_literal
* belongs to.
* @return The value of right_literal as a long, adjusted as necessary so
* that the specified comparison still evaluates correctly.
**/
static std::int64_t GetEffectiveLiteralValueForComparisonWithTruncatedAttribute(
const ComparisonID comp,
const TypedValue &right_literal,
const Type &right_literal_type);
/**
* @brief Determine if a comparison must always be true for any possible
* value of a truncated attribute.
* @warning The attribute must be truncation compressed.
*
* @param comp The comparison to evaluate.
* @param right_literal The literal value on the right side of the
* comparison.
* @param right_literal_type The specific concrete Type that right_literal
* belongs to.
* @param maximum_truncated_value The maximum truncated value in the
* attribute on the left side of the comparison.
* @return true if the comparison must always be true for all possible
* values of the attribute.
**/
static bool CompressedComparisonIsAlwaysTrueForTruncatedAttribute(
const ComparisonID comp,
const TypedValue &right_literal,
const Type &right_literal_type,
const std::int64_t maximum_truncated_value);
/**
* @brief Determine if a comparison must always be false for any possible
* value of a truncated attribute.
* @warning The attribute must be truncation compressed.
*
* @param comp The comparison to evaluate.
* @param right_literal The literal value on the right side of the
* comparison.
* @param right_literal_type The specific concrete Type that right_literal
* belongs to.
* @param maximum_truncated_value The maximum truncated value in the
* attribute on the left side of the comparison.
* @return true if the comparison must always be false for all possible
* values of the attribute.
**/
static bool CompressedComparisonIsAlwaysFalseForTruncatedAttribute(
const ComparisonID comp,
const TypedValue &right_literal,
const Type &right_literal_type,
const std::int64_t maximum_truncated_value);
private:
// Undefined default constructor - class is all static and should not be
// instantiated.
CompressedAttributePredicateTransformer();
static PredicateTransformResult TransformEqualPredicateOnCompressedAttribute(
const TypedValue &right_literal,
const Type &right_literal_type,
const CompressionDictionary *dictionary,
const std::int64_t maximum_truncated_value);
static PredicateTransformResult TransformNotEqualPredicateOnCompressedAttribute(
const TypedValue &right_literal,
const Type &right_literal_type,
const CompressionDictionary *dictionary,
const std::int64_t maximum_truncated_value);
static PredicateTransformResult TransformOtherComparisonPredicateOnCompressedAttribute(
const ComparisonID comp,
const TypedValue &right_literal,
const Type &right_literal_type,
const CompressionDictionary *dictionary,
const std::int64_t maximum_truncated_value);
DISALLOW_COPY_AND_ASSIGN(CompressedAttributePredicateTransformer);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_STORAGE_COLUMN_STORE_UTIL_HPP_