|
| 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.lake.iceberg.source; |
| 19 | + |
| 20 | +import org.apache.fluss.row.InternalRow; |
| 21 | +import org.apache.fluss.types.BigIntType; |
| 22 | +import org.apache.fluss.types.BinaryType; |
| 23 | +import org.apache.fluss.types.BooleanType; |
| 24 | +import org.apache.fluss.types.BytesType; |
| 25 | +import org.apache.fluss.types.CharType; |
| 26 | +import org.apache.fluss.types.DataType; |
| 27 | +import org.apache.fluss.types.DateType; |
| 28 | +import org.apache.fluss.types.DecimalType; |
| 29 | +import org.apache.fluss.types.DoubleType; |
| 30 | +import org.apache.fluss.types.FloatType; |
| 31 | +import org.apache.fluss.types.IntType; |
| 32 | +import org.apache.fluss.types.LocalZonedTimestampType; |
| 33 | +import org.apache.fluss.types.RowType; |
| 34 | +import org.apache.fluss.types.SmallIntType; |
| 35 | +import org.apache.fluss.types.StringType; |
| 36 | +import org.apache.fluss.types.TimeType; |
| 37 | +import org.apache.fluss.types.TimestampType; |
| 38 | +import org.apache.fluss.types.TinyIntType; |
| 39 | +import org.apache.fluss.utils.DateTimeUtils; |
| 40 | + |
| 41 | +import org.apache.iceberg.data.Record; |
| 42 | +import org.apache.iceberg.types.Types; |
| 43 | + |
| 44 | +import java.nio.ByteBuffer; |
| 45 | +import java.time.Instant; |
| 46 | +import java.time.OffsetDateTime; |
| 47 | +import java.time.ZoneOffset; |
| 48 | +import java.util.Map; |
| 49 | + |
| 50 | +/** Wrap Fluss {@link InternalRow} as Iceberg {@link Record}. */ |
| 51 | +public class FlussRowAsIcebergRecord implements Record { |
| 52 | + |
| 53 | + protected InternalRow internalRow; |
| 54 | + protected final Types.StructType structType; |
| 55 | + protected final RowType flussRowType; |
| 56 | + private final FlussRowToIcebergFieldConverter[] fieldConverters; |
| 57 | + |
| 58 | + public FlussRowAsIcebergRecord(Types.StructType structType, RowType flussRowType) { |
| 59 | + this.structType = structType; |
| 60 | + this.flussRowType = flussRowType; |
| 61 | + fieldConverters = new FlussRowToIcebergFieldConverter[flussRowType.getFieldCount()]; |
| 62 | + for (int pos = 0; pos < flussRowType.getFieldCount(); pos++) { |
| 63 | + DataType flussType = flussRowType.getTypeAt(pos); |
| 64 | + fieldConverters[pos] = createTypeConverter(flussType, pos); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public FlussRowAsIcebergRecord( |
| 69 | + Types.StructType structType, RowType flussRowType, InternalRow internalRow) { |
| 70 | + this(structType, flussRowType); |
| 71 | + this.internalRow = internalRow; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public Types.StructType struct() { |
| 76 | + return structType; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Object getField(String name) { |
| 81 | + return get(structType.fields().indexOf(structType.field(name))); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void setField(String name, Object value) { |
| 86 | + throw new UnsupportedOperationException("method setField is not supported."); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Object get(int pos) { |
| 91 | + // handle normal columns |
| 92 | + if (internalRow.isNullAt(pos)) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + return fieldConverters[pos].convert(internalRow); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Record copy() { |
| 100 | + throw new UnsupportedOperationException("method copy is not supported."); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Record copy(Map<String, Object> overwriteValues) { |
| 105 | + throw new UnsupportedOperationException("method copy is not supported."); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public int size() { |
| 110 | + return structType.fields().size(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public <T> T get(int pos, Class<T> javaClass) { |
| 115 | + Object value = get(pos); |
| 116 | + if (value == null || javaClass.isInstance(value)) { |
| 117 | + return javaClass.cast(value); |
| 118 | + } else { |
| 119 | + throw new IllegalStateException( |
| 120 | + "Not an instance of " + javaClass.getName() + ": " + value); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public <T> void set(int pos, T value) { |
| 126 | + throw new UnsupportedOperationException("method set is not supported."); |
| 127 | + } |
| 128 | + |
| 129 | + private interface FlussRowToIcebergFieldConverter { |
| 130 | + Object convert(InternalRow value); |
| 131 | + } |
| 132 | + |
| 133 | + private FlussRowToIcebergFieldConverter createTypeConverter(DataType flussType, int pos) { |
| 134 | + if (flussType instanceof BooleanType) { |
| 135 | + return row -> row.getBoolean(pos); |
| 136 | + } else if (flussType instanceof TinyIntType) { |
| 137 | + return row -> (int) row.getByte(pos); |
| 138 | + } else if (flussType instanceof SmallIntType) { |
| 139 | + return row -> (int) row.getShort(pos); |
| 140 | + } else if (flussType instanceof IntType) { |
| 141 | + return row -> row.getInt(pos); |
| 142 | + } else if (flussType instanceof BigIntType) { |
| 143 | + return row -> row.getLong(pos); |
| 144 | + } else if (flussType instanceof FloatType) { |
| 145 | + return row -> row.getFloat(pos); |
| 146 | + } else if (flussType instanceof DoubleType) { |
| 147 | + return row -> row.getDouble(pos); |
| 148 | + } else if (flussType instanceof StringType) { |
| 149 | + return row -> row.getString(pos).toString(); |
| 150 | + } else if (flussType instanceof CharType) { |
| 151 | + CharType charType = (CharType) flussType; |
| 152 | + return row -> row.getChar(pos, charType.getLength()).toString(); |
| 153 | + } else if (flussType instanceof BytesType || flussType instanceof BinaryType) { |
| 154 | + return row -> ByteBuffer.wrap(row.getBytes(pos)); |
| 155 | + } else if (flussType instanceof DecimalType) { |
| 156 | + DecimalType decimalType = (DecimalType) flussType; |
| 157 | + return row -> |
| 158 | + row.getDecimal(pos, decimalType.getPrecision(), decimalType.getScale()) |
| 159 | + .toBigDecimal(); |
| 160 | + } else if (flussType instanceof LocalZonedTimestampType) { |
| 161 | + LocalZonedTimestampType ltzType = (LocalZonedTimestampType) flussType; |
| 162 | + return row -> |
| 163 | + toIcebergTimestampLtz( |
| 164 | + row.getTimestampLtz(pos, ltzType.getPrecision()).toInstant()); |
| 165 | + } else if (flussType instanceof TimestampType) { |
| 166 | + TimestampType tsType = (TimestampType) flussType; |
| 167 | + return row -> row.getTimestampNtz(pos, tsType.getPrecision()).toLocalDateTime(); |
| 168 | + } else if (flussType instanceof DateType) { |
| 169 | + return row -> DateTimeUtils.toLocalDate(row.getInt(pos)); |
| 170 | + } else if (flussType instanceof TimeType) { |
| 171 | + return row -> DateTimeUtils.toLocalTime(row.getInt(pos)); |
| 172 | + } else { |
| 173 | + throw new UnsupportedOperationException( |
| 174 | + "Unsupported data type conversion for Fluss type: " |
| 175 | + + flussType.getClass().getSimpleName()); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private OffsetDateTime toIcebergTimestampLtz(Instant instant) { |
| 180 | + return OffsetDateTime.ofInstant(instant, ZoneOffset.UTC); |
| 181 | + } |
| 182 | +} |
0 commit comments