|
| 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 com.alibaba.fluss.row.encode.iceberg; |
| 19 | + |
| 20 | +import com.alibaba.fluss.memory.MemorySegment; |
| 21 | +import com.alibaba.fluss.row.BinaryString; |
| 22 | +import com.alibaba.fluss.row.Decimal; |
| 23 | +import com.alibaba.fluss.row.TimestampNtz; |
| 24 | +import com.alibaba.fluss.types.DataType; |
| 25 | +import com.alibaba.fluss.utils.UnsafeUtils; |
| 26 | + |
| 27 | +import java.io.Serializable; |
| 28 | +import java.util.Arrays; |
| 29 | + |
| 30 | +import static com.alibaba.fluss.types.DataTypeChecks.getPrecision; |
| 31 | + |
| 32 | +/** |
| 33 | + * A writer to encode Fluss's {@link com.alibaba.fluss.row.InternalRow} using Iceberg's binary |
| 34 | + * encoding format. |
| 35 | + * |
| 36 | + * <p>The encoding logic is based on Iceberg's Conversions.toByteBuffer() implementation: |
| 37 | + * https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/types/Conversions.java |
| 38 | + * |
| 39 | + * <p>Key encoding principles from Iceberg's Conversions class: |
| 40 | + * |
| 41 | + * <ul> |
| 42 | + * <li>All numeric types (int, long, float, double, timestamps) use LITTLE-ENDIAN byte order |
| 43 | + * <li>Decimal types use BIG-ENDIAN byte order |
| 44 | + * <li>Strings are encoded as UTF-8 bytes |
| 45 | + * <li>Timestamps are stored as long values (microseconds since epoch) |
| 46 | + * </ul> |
| 47 | + * |
| 48 | + * <p>Note: This implementation uses Fluss's MemorySegment instead of ByteBuffer for performance, |
| 49 | + * but maintains byte-level compatibility with Iceberg's encoding. |
| 50 | + */ |
| 51 | +class IcebergBinaryRowWriter { |
| 52 | + |
| 53 | + private final int arity; |
| 54 | + private byte[] buffer; |
| 55 | + private MemorySegment segment; |
| 56 | + private int cursor; |
| 57 | + |
| 58 | + public IcebergBinaryRowWriter(int arity) { |
| 59 | + this.arity = arity; |
| 60 | + // Conservative initial size to avoid frequent resizing |
| 61 | + int initialSize = 8 + (arity * 8); |
| 62 | + setBuffer(new byte[initialSize]); |
| 63 | + reset(); |
| 64 | + } |
| 65 | + |
| 66 | + public void reset() { |
| 67 | + this.cursor = 0; |
| 68 | + // Clear only the used portion for efficiency |
| 69 | + if (cursor > 0) { |
| 70 | + Arrays.fill(buffer, 0, Math.min(cursor, buffer.length), (byte) 0); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public byte[] toBytes() { |
| 75 | + byte[] result = new byte[cursor]; |
| 76 | + System.arraycopy(buffer, 0, result, 0, cursor); |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + public void setNullAt(int pos) { |
| 81 | + // For Iceberg key encoding, null values should not occur |
| 82 | + // This is validated at the encoder level |
| 83 | + throw new UnsupportedOperationException( |
| 84 | + "Null values are not supported in Iceberg key encoding"); |
| 85 | + } |
| 86 | + |
| 87 | + public void writeBoolean(boolean value) { |
| 88 | + ensureCapacity(1); |
| 89 | + UnsafeUtils.putBoolean(buffer, cursor, value); |
| 90 | + cursor += 1; |
| 91 | + } |
| 92 | + |
| 93 | + public void writeByte(byte value) { |
| 94 | + ensureCapacity(1); |
| 95 | + UnsafeUtils.putByte(buffer, cursor, value); |
| 96 | + cursor += 1; |
| 97 | + } |
| 98 | + |
| 99 | + public void writeShort(short value) { |
| 100 | + ensureCapacity(2); |
| 101 | + UnsafeUtils.putShort(buffer, cursor, value); |
| 102 | + cursor += 2; |
| 103 | + } |
| 104 | + |
| 105 | + public void writeInt(int value) { |
| 106 | + ensureCapacity(4); |
| 107 | + UnsafeUtils.putInt(buffer, cursor, value); |
| 108 | + cursor += 4; |
| 109 | + } |
| 110 | + |
| 111 | + public void writeLong(long value) { |
| 112 | + ensureCapacity(8); |
| 113 | + UnsafeUtils.putLong(buffer, cursor, value); |
| 114 | + cursor += 8; |
| 115 | + } |
| 116 | + |
| 117 | + public void writeFloat(float value) { |
| 118 | + ensureCapacity(4); |
| 119 | + UnsafeUtils.putFloat(buffer, cursor, value); |
| 120 | + cursor += 4; |
| 121 | + } |
| 122 | + |
| 123 | + public void writeDouble(double value) { |
| 124 | + ensureCapacity(8); |
| 125 | + UnsafeUtils.putDouble(buffer, cursor, value); |
| 126 | + cursor += 8; |
| 127 | + } |
| 128 | + |
| 129 | + public void writeString(BinaryString value) { |
| 130 | + // Convert to UTF-8 byte array |
| 131 | + byte[] bytes = BinaryString.encodeUTF8(value.toString()); |
| 132 | + // Write length prefix followed by UTF-8 bytes |
| 133 | + writeInt(bytes.length); // 4-byte length prefix |
| 134 | + ensureCapacity(bytes.length); // Ensure space for actual string bytes |
| 135 | + segment.put(cursor, bytes, 0, bytes.length); |
| 136 | + cursor += bytes.length; |
| 137 | + } |
| 138 | + |
| 139 | + public void writeBytes(byte[] bytes) { |
| 140 | + // Write length prefix followed by binary data |
| 141 | + writeInt(bytes.length); // 4-byte length prefix |
| 142 | + ensureCapacity(bytes.length); // Ensure space for actual binary bytes |
| 143 | + segment.put(cursor, bytes, 0, bytes.length); |
| 144 | + cursor += bytes.length; |
| 145 | + } |
| 146 | + |
| 147 | + public void writeDecimal(Decimal value, int precision) { |
| 148 | + byte[] unscaled = value.toUnscaledBytes(); |
| 149 | + writeBytes(unscaled); // Adds 4-byte length prefix before the actual bytes |
| 150 | + } |
| 151 | + |
| 152 | + private void ensureCapacity(int neededSize) { |
| 153 | + if (buffer.length < cursor + neededSize) { |
| 154 | + grow(cursor + neededSize); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private void grow(int minCapacity) { |
| 159 | + int oldCapacity = buffer.length; |
| 160 | + int newCapacity = oldCapacity + (oldCapacity >> 1); // 1.5x growth |
| 161 | + if (newCapacity < minCapacity) { |
| 162 | + newCapacity = minCapacity; |
| 163 | + } |
| 164 | + setBuffer(Arrays.copyOf(buffer, newCapacity)); |
| 165 | + } |
| 166 | + |
| 167 | + private void setBuffer(byte[] buffer) { |
| 168 | + this.buffer = buffer; |
| 169 | + this.segment = MemorySegment.wrap(buffer); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Creates an accessor for writing the elements of an iceberg binary row writer during runtime. |
| 174 | + * |
| 175 | + * @param fieldType the field type to write |
| 176 | + */ |
| 177 | + public static FieldWriter createFieldWriter(DataType fieldType) { |
| 178 | + switch (fieldType.getTypeRoot()) { |
| 179 | + case INTEGER: |
| 180 | + case DATE: |
| 181 | + return (writer, value) -> writer.writeInt((int) value); |
| 182 | + |
| 183 | + case TIME_WITHOUT_TIME_ZONE: |
| 184 | + // Write time as microseconds long (milliseconds * 1000) |
| 185 | + return (writer, value) -> { |
| 186 | + int millis = (int) value; |
| 187 | + long micros = millis * 1000L; |
| 188 | + writer.writeLong(micros); |
| 189 | + }; |
| 190 | + |
| 191 | + case BIGINT: |
| 192 | + return (writer, value) -> writer.writeLong((long) value); |
| 193 | + // support for nanoseconds come check again after #1195 merge |
| 194 | + case TIMESTAMP_WITHOUT_TIME_ZONE: |
| 195 | + return (writer, value) -> { |
| 196 | + TimestampNtz ts = (TimestampNtz) value; |
| 197 | + long micros = ts.getMillisecond() * 1000L + (ts.getNanoOfMillisecond() / 1000L); |
| 198 | + writer.writeLong(micros); |
| 199 | + }; |
| 200 | + |
| 201 | + case DECIMAL: |
| 202 | + final int decimalPrecision = getPrecision(fieldType); |
| 203 | + return (writer, value) -> writer.writeDecimal((Decimal) value, decimalPrecision); |
| 204 | + |
| 205 | + case STRING: |
| 206 | + case CHAR: |
| 207 | + return (writer, value) -> writer.writeString((BinaryString) value); |
| 208 | + |
| 209 | + case BINARY: |
| 210 | + case BYTES: |
| 211 | + return (writer, value) -> writer.writeBytes((byte[]) value); |
| 212 | + |
| 213 | + default: |
| 214 | + throw new IllegalArgumentException( |
| 215 | + "Unsupported type for Iceberg binary row writer: " + fieldType); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + /** Accessor for writing the elements of an iceberg binary row writer during runtime. */ |
| 220 | + interface FieldWriter extends Serializable { |
| 221 | + void writeField(IcebergBinaryRowWriter writer, Object value); |
| 222 | + } |
| 223 | +} |
0 commit comments