forked from apache/carbondata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultEncodingFactory.java
358 lines (331 loc) · 16.1 KB
/
DefaultEncodingFactory.java
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* 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.
*/
package org.apache.carbondata.core.datastore.page.encoding;
import java.math.BigDecimal;
import org.apache.carbondata.core.datastore.TableSpec;
import org.apache.carbondata.core.datastore.compression.Compressor;
import org.apache.carbondata.core.datastore.compression.CompressorFactory;
import org.apache.carbondata.core.datastore.page.ColumnPage;
import org.apache.carbondata.core.datastore.page.DecimalColumnPage;
import org.apache.carbondata.core.datastore.page.encoding.adaptive.AdaptiveDeltaFloatingCodec;
import org.apache.carbondata.core.datastore.page.encoding.adaptive.AdaptiveDeltaIntegralCodec;
import org.apache.carbondata.core.datastore.page.encoding.adaptive.AdaptiveFloatingCodec;
import org.apache.carbondata.core.datastore.page.encoding.adaptive.AdaptiveIntegralCodec;
import org.apache.carbondata.core.datastore.page.encoding.compress.DirectCompressCodec;
import org.apache.carbondata.core.datastore.page.encoding.dimension.legacy.ComplexDimensionIndexCodec;
import org.apache.carbondata.core.datastore.page.encoding.dimension.legacy.DictDimensionIndexCodec;
import org.apache.carbondata.core.datastore.page.encoding.dimension.legacy.DirectDictDimensionIndexCodec;
import org.apache.carbondata.core.datastore.page.encoding.dimension.legacy.HighCardDictDimensionIndexCodec;
import org.apache.carbondata.core.datastore.page.statistics.PrimitivePageStatsCollector;
import org.apache.carbondata.core.datastore.page.statistics.SimpleStatsResult;
import org.apache.carbondata.core.metadata.datatype.DataType;
import org.apache.carbondata.core.metadata.datatype.DataTypes;
import org.apache.carbondata.core.metadata.datatype.DecimalConverterFactory;
/**
* Default factory will select encoding base on column page data type and statistics
*/
public class DefaultEncodingFactory extends EncodingFactory {
private static final int THREE_BYTES_MAX = (int) Math.pow(2, 23) - 1;
private static final int THREE_BYTES_MIN = - THREE_BYTES_MAX - 1;
private static final boolean newWay = false;
private static EncodingFactory encodingFactory = new DefaultEncodingFactory();
public static EncodingFactory getInstance() {
// TODO: make it configurable after added new encodingFactory
return encodingFactory;
}
@Override
public ColumnPageEncoder createEncoder(TableSpec.ColumnSpec columnSpec, ColumnPage inputPage) {
// TODO: add log
if (columnSpec instanceof TableSpec.MeasureSpec) {
return createEncoderForMeasure(inputPage);
} else {
if (newWay) {
return createEncoderForDimension((TableSpec.DimensionSpec) columnSpec, inputPage);
} else {
assert columnSpec instanceof TableSpec.DimensionSpec;
return createEncoderForDimensionLegacy((TableSpec.DimensionSpec) columnSpec);
}
}
}
private ColumnPageEncoder createEncoderForDimension(TableSpec.DimensionSpec columnSpec,
ColumnPage inputPage) {
Compressor compressor = CompressorFactory.getInstance().getCompressor();
switch (columnSpec.getColumnType()) {
case GLOBAL_DICTIONARY:
case DIRECT_DICTIONARY:
case PLAIN_VALUE:
return new DirectCompressCodec(inputPage.getDataType()).createEncoder(null);
case COMPLEX:
return new ComplexDimensionIndexCodec(false, false, compressor).createEncoder(null);
default:
throw new RuntimeException("unsupported dimension type: " +
columnSpec.getColumnType());
}
}
private ColumnPageEncoder createEncoderForDimensionLegacy(TableSpec.DimensionSpec dimensionSpec) {
Compressor compressor = CompressorFactory.getInstance().getCompressor();
switch (dimensionSpec.getColumnType()) {
case GLOBAL_DICTIONARY:
return new DictDimensionIndexCodec(
dimensionSpec.isInSortColumns(),
dimensionSpec.isInSortColumns() && dimensionSpec.isDoInvertedIndex(),
compressor).createEncoder(null);
case DIRECT_DICTIONARY:
return new DirectDictDimensionIndexCodec(
dimensionSpec.isInSortColumns(),
dimensionSpec.isInSortColumns() && dimensionSpec.isDoInvertedIndex(),
compressor).createEncoder(null);
case PLAIN_VALUE:
return new HighCardDictDimensionIndexCodec(
dimensionSpec.isInSortColumns(),
dimensionSpec.isInSortColumns() && dimensionSpec.isDoInvertedIndex(),
dimensionSpec.getSchemaDataType() == DataTypes.VARCHAR,
compressor).createEncoder(null);
default:
throw new RuntimeException("unsupported dimension type: " +
dimensionSpec.getColumnType());
}
}
private ColumnPageEncoder createEncoderForMeasure(ColumnPage columnPage) {
SimpleStatsResult stats = columnPage.getStatistics();
DataType dataType = stats.getDataType();
if (dataType == DataTypes.BOOLEAN) {
return new DirectCompressCodec(columnPage.getDataType()).createEncoder(null);
} else if (dataType == DataTypes.BYTE ||
dataType == DataTypes.SHORT ||
dataType == DataTypes.INT ||
dataType == DataTypes.LONG) {
return selectCodecByAlgorithmForIntegral(stats, false).createEncoder(null);
} else if (DataTypes.isDecimal(dataType)) {
return createEncoderForDecimalDataTypeMeasure(columnPage);
} else if (dataType == DataTypes.FLOAT || dataType == DataTypes.DOUBLE) {
return selectCodecByAlgorithmForFloating(stats, false).createEncoder(null);
} else if (dataType == DataTypes.BYTE_ARRAY) {
return new DirectCompressCodec(columnPage.getDataType()).createEncoder(null);
} else {
throw new RuntimeException("unsupported data type: " + stats.getDataType());
}
}
private ColumnPageEncoder createEncoderForDecimalDataTypeMeasure(ColumnPage columnPage) {
DecimalConverterFactory.DecimalConverterType decimalConverterType =
((DecimalColumnPage) columnPage).getDecimalConverter().getDecimalConverterType();
switch (decimalConverterType) {
case DECIMAL_INT:
case DECIMAL_LONG:
return selectCodecByAlgorithmForDecimal(columnPage.getStatistics(), decimalConverterType)
.createEncoder(null);
default:
return new DirectCompressCodec(columnPage.getDataType()).createEncoder(null);
}
}
private static DataType fitLongMinMax(long max, long min) {
if (max <= Byte.MAX_VALUE && min >= Byte.MIN_VALUE) {
return DataTypes.BYTE;
} else if (max <= Short.MAX_VALUE && min >= Short.MIN_VALUE) {
return DataTypes.SHORT;
} else if (max <= THREE_BYTES_MAX && min >= THREE_BYTES_MIN) {
return DataTypes.SHORT_INT;
} else if (max <= Integer.MAX_VALUE && min >= Integer.MIN_VALUE) {
return DataTypes.INT;
} else {
return DataTypes.LONG;
}
}
private static DataType fitMinMax(DataType dataType, Object max, Object min) {
if (dataType == DataTypes.BYTE || dataType == DataTypes.BOOLEAN) {
return fitLongMinMax((byte) max, (byte) min);
} else if (dataType == DataTypes.SHORT) {
return fitLongMinMax((short) max, (short) min);
} else if (dataType == DataTypes.INT) {
return fitLongMinMax((int) max, (int) min);
} else if ((dataType == DataTypes.LONG) || (dataType == DataTypes.TIMESTAMP)) {
return fitLongMinMax((long) max, (long) min);
} else if (dataType == DataTypes.DOUBLE) {
return fitLongMinMax((long) (double) max, (long) (double) min);
} else {
throw new RuntimeException("internal error: " + dataType);
}
}
private static DataType fitMinMaxForDecimalType(DataType dataType, Object max, Object min,
DecimalConverterFactory.DecimalConverterType decimalConverterType) {
long maxValue = ((BigDecimal) max).unscaledValue().longValue();
long minValue = ((BigDecimal) min).unscaledValue().longValue();
switch (decimalConverterType) {
case DECIMAL_INT:
return fitLongMinMax((int) maxValue, (int) minValue);
case DECIMAL_LONG:
return fitLongMinMax(maxValue, minValue);
default:
throw new RuntimeException("internal error: " + dataType);
}
}
private static DataType fitDeltaForDecimalType(DataType dataType, Object max, Object min,
DecimalConverterFactory.DecimalConverterType decimalConverterType) {
long maxValue = ((BigDecimal) max).unscaledValue().longValue();
long minValue = ((BigDecimal) min).unscaledValue().longValue();
switch (decimalConverterType) {
case DECIMAL_INT:
long value = maxValue - minValue;
return compareMinMaxAndSelectDataType(value);
case DECIMAL_LONG:
return DataTypes.LONG;
default:
throw new RuntimeException("internal error: " + dataType);
}
}
// fit the long input value into minimum data type
private static DataType fitDelta(DataType dataType, Object max, Object min) {
// use long data type to calculate delta to avoid overflow
long value;
if (dataType == DataTypes.BYTE || dataType == DataTypes.BOOLEAN) {
value = (long) (byte) max - (long) (byte) min;
} else if (dataType == DataTypes.SHORT) {
value = (long) (short) max - (long) (short) min;
} else if (dataType == DataTypes.INT) {
value = (long) (int) max - (long) (int) min;
} else if (dataType == DataTypes.LONG || dataType == DataTypes.TIMESTAMP) {
value = (long) max - (long) min;
// The subtraction overflowed iff the operands have opposing signs
// and the result's sign differs from the minuend.
boolean overflow = (((long) max ^ (long) min) & ((long) max ^ value)) < 0;
if (overflow) {
return DataTypes.LONG;
}
} else if (dataType == DataTypes.DOUBLE) {
return DataTypes.LONG;
} else {
throw new RuntimeException("internal error: " + dataType);
}
return compareMinMaxAndSelectDataType(value);
}
private static DataType compareMinMaxAndSelectDataType(long value) {
if (value <= Byte.MAX_VALUE && value >= Byte.MIN_VALUE) {
return DataTypes.BYTE;
} else if (value <= Short.MAX_VALUE && value >= Short.MIN_VALUE) {
return DataTypes.SHORT;
} else if (value <= THREE_BYTES_MAX && value >= THREE_BYTES_MIN) {
return DataTypes.SHORT_INT;
} else if (value <= Integer.MAX_VALUE && value >= Integer.MIN_VALUE) {
return DataTypes.INT;
} else {
return DataTypes.LONG;
}
}
/**
* choose between adaptive encoder or delta adaptive encoder, based on whose target data type
* size is smaller
*/
static ColumnPageCodec selectCodecByAlgorithmForIntegral(SimpleStatsResult stats,
boolean isComplexPrimitive) {
DataType srcDataType = stats.getDataType();
DataType adaptiveDataType = fitMinMax(stats.getDataType(), stats.getMax(), stats.getMin());
DataType deltaDataType = fitDelta(stats.getDataType(), stats.getMax(), stats.getMin());
// for complex primitive, if source and destination data type is same, use adaptive encoding.
if (!isComplexPrimitive) {
// in case of decimal datatype, check if the decimal converter type is Int or Long and based
// on that get size in bytes
if (Math.min(adaptiveDataType.getSizeInBytes(), deltaDataType.getSizeInBytes()) == srcDataType
.getSizeInBytes()) {
// no effect to use adaptive or delta, use compression only
return new DirectCompressCodec(stats.getDataType());
}
}
if (adaptiveDataType.getSizeInBytes() <= deltaDataType.getSizeInBytes()) {
// choose adaptive encoding
return new AdaptiveIntegralCodec(stats.getDataType(), adaptiveDataType, stats);
} else {
// choose delta adaptive encoding
return new AdaptiveDeltaIntegralCodec(stats.getDataType(), deltaDataType, stats);
}
}
// choose between upscale adaptive encoder or upscale delta adaptive encoder,
// based on whose target data type size is smaller
static ColumnPageCodec selectCodecByAlgorithmForFloating(SimpleStatsResult stats,
boolean isComplexPrimitive) {
DataType srcDataType = stats.getDataType();
double maxValue = (double) stats.getMax();
double minValue = (double) stats.getMin();
int decimalCount = stats.getDecimalCount();
// For Complex Type primitive we should always choose adaptive path
// as LV format will be reduced to only V format. Therefore inorder
// to do that decimal count should be actual count instead of -1.
if (isComplexPrimitive && decimalCount == -1 && stats instanceof PrimitivePageStatsCollector) {
decimalCount = ((PrimitivePageStatsCollector)stats).getDecimalForComplexPrimitive();
}
//Here we should use the Max abs as max to getDatatype, let's say -1 and -10000000, -1 is max,
//but we can't use -1 to getDatatype, we should use -10000000.
double absMaxValue = Math.max(Math.abs(maxValue), Math.abs(minValue));
if (decimalCount == 0) {
// short, int, long
return selectCodecByAlgorithmForIntegral(stats, false);
} else if (decimalCount < 0 && !isComplexPrimitive) {
return new DirectCompressCodec(DataTypes.DOUBLE);
} else {
// double
// If absMaxValue exceeds LONG.MAX_VALUE, then go for direct compression
if ((Math.pow(10, decimalCount) * absMaxValue) > Long.MAX_VALUE) {
return new DirectCompressCodec(DataTypes.DOUBLE);
} else {
long max = (long) (Math.pow(10, decimalCount) * absMaxValue);
DataType adaptiveDataType = fitLongMinMax(max, 0);
DataType deltaDataType = compareMinMaxAndSelectDataType(
(long) (Math.pow(10, decimalCount) * (maxValue - minValue)));
if (adaptiveDataType.getSizeInBytes() > deltaDataType.getSizeInBytes()) {
return new AdaptiveDeltaFloatingCodec(srcDataType, deltaDataType, stats);
} else if (adaptiveDataType.getSizeInBytes() < DataTypes.DOUBLE.getSizeInBytes() || (
(isComplexPrimitive) && (adaptiveDataType.getSizeInBytes() == DataTypes.DOUBLE
.getSizeInBytes()))) {
return new AdaptiveFloatingCodec(srcDataType, adaptiveDataType, stats);
} else {
return new DirectCompressCodec(DataTypes.DOUBLE);
}
}
}
}
/**
* choose between adaptive encoder or delta adaptive encoder, based on whose target data type
* size is smaller for decimal data type
*/
static ColumnPageCodec selectCodecByAlgorithmForDecimal(SimpleStatsResult stats,
DecimalConverterFactory.DecimalConverterType decimalConverterType) {
DataType srcDataType = stats.getDataType();
DataType adaptiveDataType =
fitMinMaxForDecimalType(stats.getDataType(), stats.getMax(), stats.getMin(),
decimalConverterType);
DataType deltaDataType;
if (adaptiveDataType == DataTypes.LONG) {
deltaDataType = DataTypes.LONG;
} else {
deltaDataType = fitDeltaForDecimalType(stats.getDataType(), stats.getMax(), stats.getMin(),
decimalConverterType);
}
// in case of decimal data type check if the decimal converter type is Int or Long and based on
// that get size in bytes
if (Math.min(adaptiveDataType.getSizeInBytes(), deltaDataType.getSizeInBytes()) == srcDataType
.getSizeInBytes()) {
// no effect to use adaptive or delta, use compression only
return new DirectCompressCodec(stats.getDataType());
}
if (adaptiveDataType.getSizeInBytes() <= deltaDataType.getSizeInBytes()) {
// choose adaptive encoding
return new AdaptiveIntegralCodec(stats.getDataType(), adaptiveDataType, stats);
} else {
// choose delta adaptive encoding
return new AdaptiveDeltaIntegralCodec(stats.getDataType(), deltaDataType, stats);
}
}
}