forked from apache/datafusion-comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstantColumnReader.java
More file actions
142 lines (127 loc) · 5.08 KB
/
ConstantColumnReader.java
File metadata and controls
142 lines (127 loc) · 5.08 KB
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
/*
* 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.comet.parquet;
import java.math.BigInteger;
import org.apache.parquet.column.ColumnDescriptor;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.catalyst.util.ResolveDefaultColumns;
import org.apache.spark.sql.types.*;
import org.apache.spark.unsafe.types.UTF8String;
import org.apache.comet.IcebergApi;
/**
* A column reader that always return constant vectors. Used for reading partition columns, for
* instance.
*/
@IcebergApi
public class ConstantColumnReader extends MetadataColumnReader {
/** Whether all the values in this constant column are nulls */
private boolean isNull;
/** The constant value in the format of Object that are used to initialize this column reader. */
private Object value;
ConstantColumnReader(StructField field, int batchSize, boolean useDecimal128) {
this(field.dataType(), TypeUtil.convertToParquet(field), batchSize, useDecimal128);
this.value =
ResolveDefaultColumns.getExistenceDefaultValues(new StructType(new StructField[] {field}))[
0];
init(value);
}
ConstantColumnReader(
StructField field, int batchSize, InternalRow values, int index, boolean useDecimal128) {
this(field.dataType(), TypeUtil.convertToParquet(field), batchSize, useDecimal128);
init(values, index);
}
/**
* @see <a href="https://github.com/apache/datafusion-comet/issues/2079">Comet Issue #2079</a>
*/
@IcebergApi
public ConstantColumnReader(
DataType type, ColumnDescriptor descriptor, Object value, boolean useDecimal128) {
super(type, descriptor, useDecimal128, true);
this.value = value;
}
// Used by Iceberg
@IcebergApi
public ConstantColumnReader(
DataType type, ParquetColumnSpec spec, Object value, boolean useDecimal128) {
super(type, spec, useDecimal128, true);
this.value = value;
}
ConstantColumnReader(
DataType type, ColumnDescriptor descriptor, int batchSize, boolean useDecimal128) {
super(type, descriptor, useDecimal128, true);
this.batchSize = batchSize;
initNative();
}
@Override
public void setBatchSize(int batchSize) {
super.setBatchSize(batchSize);
init(value);
}
@Override
public void readBatch(int total) {
super.readBatch(total);
if (isNull) setNumNulls(total);
}
private void init(InternalRow values, int index) {
Object value = values.get(index, type);
init(value);
}
private void init(Object value) {
if (value == null) {
Native.setNull(nativeHandle);
isNull = true;
} else if (type == DataTypes.BooleanType) {
Native.setBoolean(nativeHandle, (boolean) value);
} else if (type == DataTypes.ByteType) {
Native.setByte(nativeHandle, (byte) value);
} else if (type == DataTypes.ShortType) {
Native.setShort(nativeHandle, (short) value);
} else if (type == DataTypes.IntegerType) {
Native.setInt(nativeHandle, (int) value);
} else if (type == DataTypes.LongType) {
Native.setLong(nativeHandle, (long) value);
} else if (type == DataTypes.FloatType) {
Native.setFloat(nativeHandle, (float) value);
} else if (type == DataTypes.DoubleType) {
Native.setDouble(nativeHandle, (double) value);
} else if (type == DataTypes.BinaryType) {
Native.setBinary(nativeHandle, (byte[]) value);
} else if (type == DataTypes.StringType) {
Native.setBinary(nativeHandle, ((UTF8String) value).getBytes());
} else if (type == DataTypes.DateType) {
Native.setInt(nativeHandle, (int) value);
} else if (type == DataTypes.TimestampType || type == TimestampNTZType$.MODULE$) {
Native.setLong(nativeHandle, (long) value);
} else if (type instanceof DecimalType) {
DecimalType dt = (DecimalType) type;
Decimal d = (Decimal) value;
if (!useDecimal128 && dt.precision() <= Decimal.MAX_INT_DIGITS()) {
Native.setInt(nativeHandle, ((int) d.toUnscaledLong()));
} else if (!useDecimal128 && dt.precision() <= Decimal.MAX_LONG_DIGITS()) {
Native.setLong(nativeHandle, d.toUnscaledLong());
} else {
final BigInteger integer = d.toJavaBigDecimal().unscaledValue();
byte[] bytes = integer.toByteArray();
Native.setDecimal(nativeHandle, bytes);
}
} else {
throw new UnsupportedOperationException("Unsupported Spark type: " + type);
}
}
}