|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.fluss.client.converter; |
| 19 | + |
| 20 | +import org.apache.fluss.row.BinaryString; |
| 21 | +import org.apache.fluss.row.Decimal; |
| 22 | +import org.apache.fluss.row.GenericRow; |
| 23 | +import org.apache.fluss.row.TimestampLtz; |
| 24 | +import org.apache.fluss.row.TimestampNtz; |
| 25 | +import org.apache.fluss.types.DataType; |
| 26 | +import org.apache.fluss.types.DecimalType; |
| 27 | +import org.apache.fluss.types.RowType; |
| 28 | + |
| 29 | +import javax.annotation.Nullable; |
| 30 | + |
| 31 | +import java.math.BigDecimal; |
| 32 | +import java.time.Instant; |
| 33 | +import java.time.LocalDate; |
| 34 | +import java.time.LocalDateTime; |
| 35 | +import java.time.LocalTime; |
| 36 | +import java.time.OffsetDateTime; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +/** |
| 40 | + * Converter for writer path: converts POJO instances to Fluss InternalRow according to a (possibly |
| 41 | + * projected) RowType. Validation is done against the full table schema. |
| 42 | + */ |
| 43 | +public final class PojoToRowConverter<T> { |
| 44 | + |
| 45 | + private final PojoType<T> pojoType; |
| 46 | + private final RowType tableSchema; |
| 47 | + private final RowType projection; |
| 48 | + private final List<String> projectionFieldNames; |
| 49 | + private final FieldToRow[] fieldConverters; // index corresponds to projection position |
| 50 | + |
| 51 | + private PojoToRowConverter(PojoType<T> pojoType, RowType tableSchema, RowType projection) { |
| 52 | + this.pojoType = pojoType; |
| 53 | + this.tableSchema = tableSchema; |
| 54 | + this.projection = projection; |
| 55 | + this.projectionFieldNames = projection.getFieldNames(); |
| 56 | + ConverterCommons.validatePojoMatchesTable(pojoType, tableSchema); |
| 57 | + ConverterCommons.validateProjectionSubset(projection, tableSchema); |
| 58 | + this.fieldConverters = createFieldConverters(); |
| 59 | + } |
| 60 | + |
| 61 | + public static <T> PojoToRowConverter<T> of( |
| 62 | + Class<T> pojoClass, RowType tableSchema, RowType projection) { |
| 63 | + return new PojoToRowConverter<>(PojoType.of(pojoClass), tableSchema, projection); |
| 64 | + } |
| 65 | + |
| 66 | + public GenericRow toRow(@Nullable T pojo) { |
| 67 | + if (pojo == null) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + GenericRow row = new GenericRow(projection.getFieldCount()); |
| 71 | + for (int i = 0; i < fieldConverters.length; i++) { |
| 72 | + Object v; |
| 73 | + try { |
| 74 | + v = fieldConverters[i].readAndConvert(pojo); |
| 75 | + } catch (RuntimeException re) { |
| 76 | + throw re; |
| 77 | + } catch (Exception e) { |
| 78 | + throw new IllegalStateException( |
| 79 | + "Failed to access field '" |
| 80 | + + projectionFieldNames.get(i) |
| 81 | + + "' from POJO " |
| 82 | + + pojoType.getPojoClass().getName(), |
| 83 | + e); |
| 84 | + } |
| 85 | + row.setField(i, v); |
| 86 | + } |
| 87 | + return row; |
| 88 | + } |
| 89 | + |
| 90 | + private FieldToRow[] createFieldConverters() { |
| 91 | + FieldToRow[] arr = new FieldToRow[projection.getFieldCount()]; |
| 92 | + for (int i = 0; i < projection.getFieldCount(); i++) { |
| 93 | + String fieldName = projectionFieldNames.get(i); |
| 94 | + DataType fieldType = projection.getTypeAt(i); |
| 95 | + PojoType.Property prop = requireProperty(fieldName); |
| 96 | + ConverterCommons.validateCompatibility(fieldType, prop); |
| 97 | + arr[i] = createFieldConverter(prop, fieldType); |
| 98 | + } |
| 99 | + return arr; |
| 100 | + } |
| 101 | + |
| 102 | + private PojoType.Property requireProperty(String fieldName) { |
| 103 | + PojoType.Property p = pojoType.getProperty(fieldName); |
| 104 | + if (p == null) { |
| 105 | + throw new IllegalArgumentException( |
| 106 | + "Field '" |
| 107 | + + fieldName |
| 108 | + + "' not found in POJO class " |
| 109 | + + pojoType.getPojoClass().getName() |
| 110 | + + "."); |
| 111 | + } |
| 112 | + return p; |
| 113 | + } |
| 114 | + |
| 115 | + private static FieldToRow createFieldConverter(PojoType.Property prop, DataType fieldType) { |
| 116 | + switch (fieldType.getTypeRoot()) { |
| 117 | + case BOOLEAN: |
| 118 | + case TINYINT: |
| 119 | + case SMALLINT: |
| 120 | + case INTEGER: |
| 121 | + case BIGINT: |
| 122 | + case FLOAT: |
| 123 | + case DOUBLE: |
| 124 | + case BINARY: |
| 125 | + case BYTES: |
| 126 | + return prop::read; |
| 127 | + case CHAR: |
| 128 | + case STRING: |
| 129 | + return (obj) -> convertTextValue(fieldType, prop, prop.read(obj)); |
| 130 | + case DECIMAL: |
| 131 | + return (obj) -> convertDecimalValue((DecimalType) fieldType, prop, prop.read(obj)); |
| 132 | + case DATE: |
| 133 | + return (obj) -> convertDateValue(prop, prop.read(obj)); |
| 134 | + case TIME_WITHOUT_TIME_ZONE: |
| 135 | + return (obj) -> convertTimeValue(prop, prop.read(obj)); |
| 136 | + case TIMESTAMP_WITHOUT_TIME_ZONE: |
| 137 | + return (obj) -> convertTimestampNtzValue(prop, prop.read(obj)); |
| 138 | + case TIMESTAMP_WITH_LOCAL_TIME_ZONE: |
| 139 | + return (obj) -> convertTimestampLtzValue(prop, prop.read(obj)); |
| 140 | + default: |
| 141 | + throw new UnsupportedOperationException( |
| 142 | + String.format( |
| 143 | + "Unsupported field type %s for field %s.", |
| 144 | + fieldType.getTypeRoot(), prop.name)); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Converts a text value (String or Character) from a POJO property to Fluss BinaryString. |
| 150 | + * |
| 151 | + * <p>For CHAR columns, enforces that the text has exactly one character. Nulls are passed |
| 152 | + * through. |
| 153 | + */ |
| 154 | + private static @Nullable BinaryString convertTextValue( |
| 155 | + DataType fieldType, PojoType.Property prop, @Nullable Object v) { |
| 156 | + if (v == null) { |
| 157 | + return null; |
| 158 | + } |
| 159 | + return ConverterCommons.toBinaryStringForText(v, prop.name, fieldType.getTypeRoot()); |
| 160 | + } |
| 161 | + |
| 162 | + /** Converts a BigDecimal POJO property to Fluss Decimal respecting precision and scale. */ |
| 163 | + private static @Nullable Decimal convertDecimalValue( |
| 164 | + DecimalType decimalType, PojoType.Property prop, @Nullable Object v) { |
| 165 | + if (v == null) { |
| 166 | + return null; |
| 167 | + } |
| 168 | + if (!(v instanceof BigDecimal)) { |
| 169 | + throw new IllegalArgumentException( |
| 170 | + String.format( |
| 171 | + "Field %s is not a BigDecimal. Cannot convert to Decimal.", prop.name)); |
| 172 | + } |
| 173 | + return Decimal.fromBigDecimal( |
| 174 | + (BigDecimal) v, decimalType.getPrecision(), decimalType.getScale()); |
| 175 | + } |
| 176 | + |
| 177 | + /** Converts a LocalDate POJO property to number of days since epoch. */ |
| 178 | + private static @Nullable Integer convertDateValue(PojoType.Property prop, @Nullable Object v) { |
| 179 | + if (v == null) { |
| 180 | + return null; |
| 181 | + } |
| 182 | + if (!(v instanceof LocalDate)) { |
| 183 | + throw new IllegalArgumentException( |
| 184 | + String.format( |
| 185 | + "Field %s is not a LocalDate. Cannot convert to int days.", prop.name)); |
| 186 | + } |
| 187 | + return (int) ((LocalDate) v).toEpochDay(); |
| 188 | + } |
| 189 | + |
| 190 | + /** Converts a LocalTime POJO property to milliseconds of day. */ |
| 191 | + private static @Nullable Integer convertTimeValue(PojoType.Property prop, @Nullable Object v) { |
| 192 | + if (v == null) { |
| 193 | + return null; |
| 194 | + } |
| 195 | + if (!(v instanceof LocalTime)) { |
| 196 | + throw new IllegalArgumentException( |
| 197 | + String.format( |
| 198 | + "Field %s is not a LocalTime. Cannot convert to int millis.", |
| 199 | + prop.name)); |
| 200 | + } |
| 201 | + LocalTime t = (LocalTime) v; |
| 202 | + return (int) (t.toNanoOfDay() / 1_000_000); |
| 203 | + } |
| 204 | + |
| 205 | + /** Converts a LocalDateTime POJO property to Fluss TimestampNtz. */ |
| 206 | + private static @Nullable TimestampNtz convertTimestampNtzValue( |
| 207 | + PojoType.Property prop, @Nullable Object v) { |
| 208 | + if (v == null) { |
| 209 | + return null; |
| 210 | + } |
| 211 | + if (!(v instanceof LocalDateTime)) { |
| 212 | + throw new IllegalArgumentException( |
| 213 | + String.format( |
| 214 | + "Field %s is not a LocalDateTime. Cannot convert to TimestampNtz.", |
| 215 | + prop.name)); |
| 216 | + } |
| 217 | + return TimestampNtz.fromLocalDateTime((LocalDateTime) v); |
| 218 | + } |
| 219 | + |
| 220 | + /** Converts an Instant or OffsetDateTime POJO property to Fluss TimestampLtz (UTC based). */ |
| 221 | + private static @Nullable TimestampLtz convertTimestampLtzValue( |
| 222 | + PojoType.Property prop, @Nullable Object v) { |
| 223 | + if (v == null) { |
| 224 | + return null; |
| 225 | + } |
| 226 | + if (v instanceof Instant) { |
| 227 | + return TimestampLtz.fromInstant((Instant) v); |
| 228 | + } else if (v instanceof OffsetDateTime) { |
| 229 | + return TimestampLtz.fromInstant(((OffsetDateTime) v).toInstant()); |
| 230 | + } |
| 231 | + throw new IllegalArgumentException( |
| 232 | + String.format( |
| 233 | + "Field %s is not an Instant or OffsetDateTime. Cannot convert to TimestampLtz.", |
| 234 | + prop.name)); |
| 235 | + } |
| 236 | + |
| 237 | + private interface FieldToRow { |
| 238 | + Object readAndConvert(Object pojo) throws Exception; |
| 239 | + } |
| 240 | +} |
0 commit comments