-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
TypeUtils.java
239 lines (210 loc) · 9.73 KB
/
TypeUtils.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
/*
* Licensed 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 io.prestosql.plugin.postgresql;
import com.google.common.primitives.Shorts;
import com.google.common.primitives.SignedBytes;
import io.airlift.slice.Slice;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.Block;
import io.prestosql.spi.connector.ConnectorSession;
import io.prestosql.spi.type.ArrayType;
import io.prestosql.spi.type.CharType;
import io.prestosql.spi.type.DecimalType;
import io.prestosql.spi.type.LongTimestampWithTimeZone;
import io.prestosql.spi.type.TimestampType;
import io.prestosql.spi.type.TimestampWithTimeZoneType;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.VarcharType;
import org.joda.time.DateTimeZone;
import org.postgresql.util.PGobject;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import static com.google.common.base.Preconditions.checkArgument;
import static io.prestosql.plugin.jdbc.StandardColumnMappings.fromPrestoTimestamp;
import static io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.prestosql.spi.type.BigintType.BIGINT;
import static io.prestosql.spi.type.BooleanType.BOOLEAN;
import static io.prestosql.spi.type.DateTimeEncoding.unpackMillisUtc;
import static io.prestosql.spi.type.DateType.DATE;
import static io.prestosql.spi.type.Decimals.decodeUnscaledValue;
import static io.prestosql.spi.type.DoubleType.DOUBLE;
import static io.prestosql.spi.type.IntegerType.INTEGER;
import static io.prestosql.spi.type.RealType.REAL;
import static io.prestosql.spi.type.SmallintType.SMALLINT;
import static io.prestosql.spi.type.TimeZoneKey.UTC_KEY;
import static io.prestosql.spi.type.Timestamps.MILLISECONDS_PER_SECOND;
import static io.prestosql.spi.type.Timestamps.NANOSECONDS_PER_MILLISECOND;
import static io.prestosql.spi.type.Timestamps.PICOSECONDS_PER_NANOSECOND;
import static io.prestosql.spi.type.TinyintType.TINYINT;
import static io.prestosql.spi.type.TypeUtils.readNativeValue;
import static java.lang.Float.intBitsToFloat;
import static java.lang.Math.toIntExact;
import static java.util.concurrent.TimeUnit.DAYS;
import static org.joda.time.DateTimeZone.UTC;
final class TypeUtils
{
private TypeUtils() {}
// PostgreSQL jdbc array element names understood by org.postgresql.jdbc2.TypeInfoCache#getPGArrayType
// for multidimensional arrays, this should return the base type (e.g. array(array(integer)) returns 'integer')
static String getArrayElementPgTypeName(ConnectorSession session, PostgreSqlClient client, Type elementType)
{
if (DOUBLE.equals(elementType)) {
return "float";
}
if (REAL.equals(elementType)) {
return "float4";
}
// TypeInfoCache#getPGArrayType apparently doesn't allow for specifying variable-length limits.
// Since PostgreSQL char[] can only hold single byte elements, map all CharType to varchar
if (elementType instanceof VarcharType || elementType instanceof CharType) {
return "varchar";
}
// TypeInfoCache#getPGArrayType apparently doesn't allow for specifying variable-length limits.
// Map all timestamptz(x) types to unparametrized one which defaults to highest precision
if (elementType instanceof TimestampWithTimeZoneType) {
return "timestamptz";
}
// TypeInfoCache#getPGArrayType doesn't allow for specifying variable-length limits.
// Map all timestamp(x) types to unparametrized one which defaults to highest precision
if (elementType instanceof TimestampType) {
return "timestamp";
}
if (elementType instanceof DecimalType) {
return "decimal";
}
if (elementType instanceof ArrayType) {
return getArrayElementPgTypeName(session, client, ((ArrayType) elementType).getElementType());
}
return client.toWriteMapping(session, elementType).getDataType();
}
static Object[] getJdbcObjectArray(ConnectorSession session, Type elementType, Block block)
throws SQLException
{
int positionCount = block.getPositionCount();
Object[] valuesArray = new Object[positionCount];
int subArrayLength = 1;
for (int i = 0; i < positionCount; i++) {
Object objectValue = prestoNativeToJdbcObject(session, elementType, readNativeValue(elementType, block, i));
valuesArray[i] = objectValue;
if (objectValue != null && objectValue.getClass().isArray()) {
subArrayLength = Math.max(subArrayLength, Array.getLength(objectValue));
}
}
// PostgreSql requires subarrays with matching dimensions, including arrays of null
if (elementType instanceof ArrayType) {
handleArrayNulls(valuesArray, subArrayLength);
}
return valuesArray;
}
public static int arrayDepth(Object jdbcArray)
{
checkArgument(jdbcArray.getClass().isArray(), "jdbcArray is not an array");
int depth = 0;
while (jdbcArray != null && jdbcArray.getClass().isArray()) {
depth++;
if (Array.getLength(jdbcArray) == 0) {
return depth;
}
jdbcArray = Array.get(jdbcArray, 0);
}
return depth;
}
private static void handleArrayNulls(Object[] valuesArray, int length)
{
for (int i = 0; i < valuesArray.length; i++) {
if (valuesArray[i] == null) {
valuesArray[i] = new Object[length];
}
}
}
private static Object prestoNativeToJdbcObject(ConnectorSession session, Type prestoType, Object prestoNative)
throws SQLException
{
if (prestoNative == null) {
return null;
}
if (DOUBLE.equals(prestoType) || BOOLEAN.equals(prestoType) || BIGINT.equals(prestoType)) {
return prestoNative;
}
if (prestoType instanceof DecimalType) {
DecimalType decimalType = (DecimalType) prestoType;
if (decimalType.isShort()) {
BigInteger unscaledValue = BigInteger.valueOf((long) prestoNative);
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
BigInteger unscaledValue = decodeUnscaledValue((Slice) prestoNative);
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
if (REAL.equals(prestoType)) {
return intBitsToFloat(toIntExact((long) prestoNative));
}
if (TINYINT.equals(prestoType)) {
return SignedBytes.checkedCast((long) prestoNative);
}
if (SMALLINT.equals(prestoType)) {
return Shorts.checkedCast((long) prestoNative);
}
if (INTEGER.equals(prestoType)) {
return toIntExact((long) prestoNative);
}
if (DATE.equals(prestoType)) {
// convert to midnight in default time zone
long millis = DAYS.toMillis((long) prestoNative);
return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
}
if (prestoType instanceof TimestampType && ((TimestampType) prestoType).isShort()) {
TimestampType timestampType = (TimestampType) prestoType;
return toPgTimestamp(fromPrestoTimestamp((long) prestoNative));
}
if (prestoType instanceof TimestampWithTimeZoneType) {
// PostgreSQL does not store zone, only the point in time
int precision = ((TimestampWithTimeZoneType) prestoType).getPrecision();
if (precision <= TimestampWithTimeZoneType.MAX_SHORT_PRECISION) {
long millisUtc = unpackMillisUtc((long) prestoNative);
return new Timestamp(millisUtc);
}
else {
LongTimestampWithTimeZone value = (LongTimestampWithTimeZone) prestoNative;
long epochSeconds = value.getEpochMillis() / MILLISECONDS_PER_SECOND;
long nanosOfSecond = value.getEpochMillis() % MILLISECONDS_PER_SECOND * NANOSECONDS_PER_MILLISECOND
+ value.getPicosOfMilli() / PICOSECONDS_PER_NANOSECOND;
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(epochSeconds, nanosOfSecond), UTC_KEY.getZoneId());
}
}
if (prestoType instanceof VarcharType || prestoType instanceof CharType) {
return ((Slice) prestoNative).toStringUtf8();
}
if (prestoType instanceof ArrayType) {
// process subarray of multi-dimensional array
return getJdbcObjectArray(session, ((ArrayType) prestoType).getElementType(), (Block) prestoNative);
}
throw new PrestoException(NOT_SUPPORTED, "Unsupported type: " + prestoType);
}
static PGobject toPgTimestamp(LocalDateTime localDateTime)
throws SQLException
{
PGobject pgObject = new PGobject();
pgObject.setType("timestamp");
pgObject.setValue(localDateTime.toString());
return pgObject;
}
}