/
DecimalSliceStreamReader.java
155 lines (133 loc) · 5.66 KB
/
DecimalSliceStreamReader.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
/*
* 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.presto.readers;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Objects;
import static java.math.RoundingMode.HALF_UP;
import org.apache.carbondata.core.cache.dictionary.Dictionary;
import org.apache.carbondata.core.metadata.datatype.DataTypes;
import org.apache.carbondata.core.scan.result.vector.impl.CarbonColumnVectorImpl;
import org.apache.carbondata.core.util.DataTypeUtil;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.block.BlockBuilder;
import com.facebook.presto.spi.type.DecimalType;
import com.facebook.presto.spi.type.Decimals;
import com.facebook.presto.spi.type.Type;
import io.airlift.slice.Slice;
import static com.facebook.presto.spi.type.Decimals.encodeUnscaledValue;
import static com.facebook.presto.spi.type.Decimals.isShortDecimal;
import static com.facebook.presto.spi.type.Decimals.rescale;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static io.airlift.slice.Slices.utf8Slice;
/**
* Reader for DecimalValues
*/
public class DecimalSliceStreamReader extends CarbonColumnVectorImpl
implements PrestoVectorBlockBuilder {
private final char[] buffer = new char[100];
protected int batchSize;
protected Type type;
protected BlockBuilder builder;
private Dictionary dictionary;
public DecimalSliceStreamReader(int batchSize,
org.apache.carbondata.core.metadata.datatype.DecimalType dataType, Dictionary dictionary) {
super(batchSize, dataType);
this.type = DecimalType.createDecimalType(dataType.getPrecision(), dataType.getScale());
this.batchSize = batchSize;
this.builder = type.createBlockBuilder(null, batchSize);
this.dictionary = dictionary;
}
@Override public Block buildBlock() {
return builder.build();
}
@Override public void setBatchSize(int batchSize) {
this.batchSize = batchSize;
}
@Override public void putInt(int rowId, int value) {
DecimalType decimalType = (DecimalType) type;
Object data = DataTypeUtil.getDataBasedOnDataType(dictionary.getDictionaryValueForKey(value),
DataTypes.createDecimalType(decimalType.getPrecision(), decimalType.getScale()));
if (Objects.isNull(data)) {
builder.appendNull();
} else {
decimalBlockWriter((BigDecimal) data);
}
}
@Override public void putDecimal(int rowId, BigDecimal value, int precision) {
decimalBlockWriter(value);
}
@Override public void putNull(int rowId) {
builder.appendNull();
}
@Override public void reset() {
builder = type.createBlockBuilder(null, batchSize);
}
private void decimalBlockWriter(BigDecimal value) {
if (isShortDecimal(type)) {
long rescaledDecimal = Decimals.rescale(value.unscaledValue().longValue(), value.scale(),
((DecimalType) type).getScale());
type.writeLong(builder, rescaledDecimal);
} else {
Slice slice = getSlice(value, type);
type.writeSlice(builder, parseSlice((DecimalType) type, slice, slice.length()));
}
}
private Slice getSlice(Object value, Type type) {
if (type instanceof DecimalType) {
DecimalType actual = (DecimalType) type;
BigDecimal bigDecimalValue = (BigDecimal) value;
if (isShortDecimal(type)) {
return utf8Slice(value.toString());
} else {
if (bigDecimalValue.scale() > actual.getScale()) {
BigInteger unscaledDecimal =
rescale(bigDecimalValue.unscaledValue(), bigDecimalValue.scale(),
bigDecimalValue.scale());
Slice decimalSlice = Decimals.encodeUnscaledValue(unscaledDecimal);
return utf8Slice(Decimals.toString(decimalSlice, actual.getScale()));
} else {
BigInteger unscaledDecimal =
rescale(bigDecimalValue.unscaledValue(), bigDecimalValue.scale(), actual.getScale());
Slice decimalSlice = Decimals.encodeUnscaledValue(unscaledDecimal);
return utf8Slice(Decimals.toString(decimalSlice, actual.getScale()));
}
}
} else {
return utf8Slice(value.toString());
}
}
private Slice parseSlice(DecimalType type, Slice slice, int length) {
BigDecimal decimal = parseBigDecimal(type, slice, length);
return encodeUnscaledValue(decimal.unscaledValue());
}
private BigDecimal parseBigDecimal(DecimalType type, Slice slice, int length) {
int offset = 0;
checkArgument(length < buffer.length);
for (int i = 0; i < length; i++) {
buffer[i] = (char) slice.getByte(offset + i);
}
BigDecimal decimal = new BigDecimal(buffer, 0, length);
checkState(decimal.scale() <= type.getScale(),
"Read decimal value scale larger than column scale");
decimal = decimal.setScale(type.getScale(), HALF_UP);
checkState(decimal.precision() <= type.getPrecision(),
"Read decimal precision larger than column precision");
return decimal;
}
}