|
18 | 18 |
|
19 | 19 | package org.apache.flink.connector.redshift.converter;
|
20 | 20 |
|
21 |
| -import org.apache.flink.annotation.Internal; |
22 | 21 | import org.apache.flink.table.data.ArrayData;
|
23 | 22 | import org.apache.flink.table.data.DecimalData;
|
24 |
| -import org.apache.flink.table.data.GenericArrayData; |
25 |
| -import org.apache.flink.table.data.GenericMapData; |
26 | 23 | import org.apache.flink.table.data.MapData;
|
27 |
| -import org.apache.flink.table.data.StringData; |
28 | 24 | import org.apache.flink.table.data.TimestampData;
|
29 | 25 | import org.apache.flink.table.types.logical.ArrayType;
|
30 |
| -import org.apache.flink.table.types.logical.DecimalType; |
31 | 26 | import org.apache.flink.table.types.logical.LogicalType;
|
32 | 27 | import org.apache.flink.table.types.logical.MapType;
|
33 | 28 |
|
34 |
| -import java.math.BigDecimal; |
35 |
| -import java.math.BigInteger; |
36 |
| -import java.sql.Array; |
37 | 29 | import java.sql.Date;
|
38 |
| -import java.sql.SQLException; |
39 |
| -import java.sql.Time; |
40 | 30 | import java.sql.Timestamp;
|
41 | 31 | import java.time.LocalDate;
|
42 | 32 | import java.time.LocalDateTime;
|
43 | 33 | import java.time.LocalTime;
|
44 | 34 | import java.util.HashMap;
|
45 | 35 | import java.util.Map;
|
46 | 36 |
|
47 |
| -/** Utility Class to map Flink Rich DataTypes with Redshift Limited DataType Support. */ |
48 |
| -@Internal |
49 |
| -public class RedshiftConverterUtil { |
50 |
| - |
51 |
| - /** Restrict Object Creation for the Utility Class. */ |
52 |
| - private RedshiftConverterUtil() {} |
53 |
| - |
54 |
| - public static final String UNKNOWN_ARRAY_ELEMENT_TYPE = "Unknown array element type"; |
55 |
| - public static final int BOOLEAN_TRUE = 1; |
| 37 | +/** Row converter. */ |
| 38 | +public class RedshiftConverterUtils { |
56 | 39 |
|
57 | 40 | public static Object toExternal(Object value, LogicalType type) {
|
58 | 41 | switch (type.getTypeRoot()) {
|
@@ -91,7 +74,7 @@ public static Object toExternal(Object value, LogicalType type) {
|
91 | 74 | .orElseThrow(
|
92 | 75 | () ->
|
93 | 76 | new RuntimeException(
|
94 |
| - UNKNOWN_ARRAY_ELEMENT_TYPE)); |
| 77 | + "Unknown array element type")); |
95 | 78 | ArrayData.ElementGetter elementGetter = ArrayData.createElementGetter(elementType);
|
96 | 79 | ArrayData arrayData = ((ArrayData) value);
|
97 | 80 | Object[] objectArray = new Object[arrayData.size()];
|
@@ -123,83 +106,8 @@ public static Object toExternal(Object value, LogicalType type) {
|
123 | 106 | }
|
124 | 107 | }
|
125 | 108 |
|
126 |
| - static Object toInternal(Object value, LogicalType type) throws SQLException { |
127 |
| - switch (type.getTypeRoot()) { |
128 |
| - case NULL: |
129 |
| - return null; |
130 |
| - case BOOLEAN: |
131 |
| - return BOOLEAN_TRUE == ((Number) value).intValue(); |
132 |
| - case FLOAT: |
133 |
| - case DOUBLE: |
134 |
| - case INTERVAL_YEAR_MONTH: |
135 |
| - case INTERVAL_DAY_TIME: |
136 |
| - case INTEGER: |
137 |
| - case BIGINT: |
138 |
| - case BINARY: |
139 |
| - case VARBINARY: |
140 |
| - return value; |
141 |
| - case TINYINT: |
142 |
| - return ((Integer) value).byteValue(); |
143 |
| - case SMALLINT: |
144 |
| - return value instanceof Integer ? ((Integer) value).shortValue() : value; |
145 |
| - case DECIMAL: |
146 |
| - final int precision = ((DecimalType) type).getPrecision(); |
147 |
| - final int scale = ((DecimalType) type).getScale(); |
148 |
| - return value instanceof BigInteger |
149 |
| - ? DecimalData.fromBigDecimal( |
150 |
| - new BigDecimal((BigInteger) value, 0), precision, scale) |
151 |
| - : DecimalData.fromBigDecimal((BigDecimal) value, precision, scale); |
152 |
| - case DATE: |
153 |
| - return (int) (((Date) value).toLocalDate().toEpochDay()); |
154 |
| - case TIME_WITHOUT_TIME_ZONE: |
155 |
| - return (int) (((Time) value).toLocalTime().toNanoOfDay() / 1_000_000L); |
156 |
| - case TIMESTAMP_WITH_TIME_ZONE: |
157 |
| - case TIMESTAMP_WITHOUT_TIME_ZONE: |
158 |
| - return TimestampData.fromTimestamp((Timestamp) value); |
159 |
| - case TIMESTAMP_WITH_LOCAL_TIME_ZONE: |
160 |
| - return TimestampData.fromInstant(((Timestamp) value).toInstant()); |
161 |
| - case CHAR: |
162 |
| - case VARCHAR: |
163 |
| - return StringData.fromString((String) value); |
164 |
| - case ARRAY: |
165 |
| - LogicalType elementType = |
166 |
| - type.getChildren().stream() |
167 |
| - .findFirst() |
168 |
| - .orElseThrow( |
169 |
| - () -> new RuntimeException(UNKNOWN_ARRAY_ELEMENT_TYPE)); |
170 |
| - Object externalArray = ((Array) value).getArray(); |
171 |
| - int externalArrayLength = java.lang.reflect.Array.getLength(externalArray); |
172 |
| - Object[] internalArray = new Object[externalArrayLength]; |
173 |
| - for (int i = 0; i < externalArrayLength; i++) { |
174 |
| - internalArray[i] = |
175 |
| - toInternal(java.lang.reflect.Array.get(externalArray, i), elementType); |
176 |
| - } |
177 |
| - return new GenericArrayData(internalArray); |
178 |
| - case MAP: |
179 |
| - LogicalType keyType = ((MapType) type).getKeyType(); |
180 |
| - LogicalType valueType = ((MapType) type).getValueType(); |
181 |
| - Map<?, ?> externalMap = (Map<?, ?>) value; |
182 |
| - Map<Object, Object> internalMap = new HashMap<>(externalMap.size()); |
183 |
| - for (Map.Entry<?, ?> entry : externalMap.entrySet()) { |
184 |
| - internalMap.put( |
185 |
| - toInternal(entry.getKey(), keyType), |
186 |
| - toInternal(entry.getValue(), valueType)); |
187 |
| - } |
188 |
| - return new GenericMapData(internalMap); |
189 |
| - case ROW: |
190 |
| - // todo: Implement Support for ROW. |
191 |
| - case MULTISET: |
192 |
| - // todo: Implement Support for MultiSet. |
193 |
| - case RAW: |
194 |
| - // todo: Implement Support for RAW. |
195 |
| - default: |
196 |
| - throw new UnsupportedOperationException("Unsupported type:" + type); |
197 |
| - } |
198 |
| - } |
199 |
| - |
200 | 109 | public static Timestamp toEpochDayOneTimestamp(LocalTime localTime) {
|
201 |
| - final LocalDate datePrefix = LocalDate.ofEpochDay(1); |
202 |
| - LocalDateTime localDateTime = localTime.atDate(datePrefix); |
| 110 | + LocalDateTime localDateTime = localTime.atDate(LocalDate.ofEpochDay(1)); |
203 | 111 | return Timestamp.valueOf(localDateTime);
|
204 | 112 | }
|
205 | 113 | }
|
0 commit comments