MehulBatra commented on code in PR #1350: URL: https://github.com/apache/fluss/pull/1350#discussion_r2219710567
########## fluss-common/src/main/java/com/alibaba/fluss/row/encode/iceberg/IcebergBinaryRowWriter.java: ########## @@ -0,0 +1,326 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.fluss.row.encode.iceberg; + +import com.alibaba.fluss.memory.MemorySegment; +import com.alibaba.fluss.row.BinaryString; +import com.alibaba.fluss.row.Decimal; +import com.alibaba.fluss.row.InternalRow; +import com.alibaba.fluss.row.TimestampLtz; +import com.alibaba.fluss.row.TimestampNtz; +import com.alibaba.fluss.types.DataType; +import com.alibaba.fluss.utils.UnsafeUtils; + +import java.io.Serializable; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; + +import static com.alibaba.fluss.types.DataTypeChecks.getPrecision; + +/** + * A writer to encode Fluss's {@link InternalRow} using Iceberg's binary encoding format. + * + * <p>This implementation follows Iceberg's binary encoding specification for partition and bucket + * keys. Reference: https://iceberg.apache.org/spec/#partition-transforms + * + * <p>The encoding logic is based on Iceberg's Conversions.toByteBuffer() implementation: + * https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/types/Conversions.java + * + * <p>Key encoding principles from Iceberg's Conversions class: + * + * <ul> + * <li>All numeric types (int, long, float, double, timestamps) use LITTLE-ENDIAN byte order + * <li>Decimal and UUID types use BIG-ENDIAN byte order + * <li>NO length prefix for any type - the buffer size determines the length + * <li>Strings are encoded as UTF-8 bytes without length prefix + * <li>Timestamps are stored as long values (microseconds since epoch) + * </ul> + * + * <p>Note: This implementation uses Fluss's MemorySegment instead of ByteBuffer for performance, + * but maintains byte-level compatibility with Iceberg's encoding. + */ +class IcebergBinaryRowWriter { + + private final int arity; + private byte[] buffer; + private MemorySegment segment; + private int cursor; + + public IcebergBinaryRowWriter(int arity) { + this.arity = arity; + // Conservative initial size to avoid frequent resizing + int initialSize = 8 + (arity * 8); + setBuffer(new byte[initialSize]); + reset(); + } + + public void reset() { + this.cursor = 0; + // Clear only the used portion for efficiency + if (cursor > 0) { + Arrays.fill(buffer, 0, Math.min(cursor, buffer.length), (byte) 0); + } + } + + public byte[] toBytes() { + byte[] result = new byte[cursor]; + System.arraycopy(buffer, 0, result, 0, cursor); + return result; + } + + public void setNullAt(int pos) { + // For Iceberg key encoding, null values should not occur + // This is validated at the encoder level + throw new UnsupportedOperationException( + "Null values are not supported in Iceberg key encoding"); + } + + public void writeBoolean(boolean value) { + ensureCapacity(1); + UnsafeUtils.putBoolean(buffer, cursor, value); + cursor += 1; + } + + public void writeByte(byte value) { + ensureCapacity(1); + UnsafeUtils.putByte(buffer, cursor, value); + cursor += 1; + } + + public void writeShort(short value) { + ensureCapacity(2); + UnsafeUtils.putShort(buffer, cursor, value); + cursor += 2; + } + + public void writeInt(int value) { + ensureCapacity(4); + UnsafeUtils.putInt(buffer, cursor, value); + cursor += 4; + } + + public void writeLong(long value) { + ensureCapacity(8); + UnsafeUtils.putLong(buffer, cursor, value); + cursor += 8; + } + + public void writeFloat(float value) { + ensureCapacity(4); + UnsafeUtils.putFloat(buffer, cursor, value); + cursor += 4; + } + + public void writeDouble(double value) { + ensureCapacity(8); + UnsafeUtils.putDouble(buffer, cursor, value); + cursor += 8; + } + + public void writeString(BinaryString value) { + // Based on Iceberg's Conversions.toByteBuffer() for STRING type: + // Strings are encoded as UTF-8 bytes without length prefix + // Reference:https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/types/Conversions.java + byte[] bytes = BinaryString.encodeUTF8(value.toString()); + ensureCapacity(bytes.length); + segment.put(cursor, bytes, 0, bytes.length); + cursor += bytes.length; + } + + void writeBytes(byte[] bytes) { + // Based on Iceberg's Conversions.toByteBuffer() for BINARY type: + // Binary data is stored directly without length prefix + ensureCapacity(bytes.length); Review Comment: added length, in this also, but this is adding lenght prefix in decimal also. ########## fluss-common/src/main/java/com/alibaba/fluss/row/encode/iceberg/IcebergBinaryRowWriter.java: ########## @@ -0,0 +1,326 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alibaba.fluss.row.encode.iceberg; + +import com.alibaba.fluss.memory.MemorySegment; +import com.alibaba.fluss.row.BinaryString; +import com.alibaba.fluss.row.Decimal; +import com.alibaba.fluss.row.InternalRow; +import com.alibaba.fluss.row.TimestampLtz; +import com.alibaba.fluss.row.TimestampNtz; +import com.alibaba.fluss.types.DataType; +import com.alibaba.fluss.utils.UnsafeUtils; + +import java.io.Serializable; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; + +import static com.alibaba.fluss.types.DataTypeChecks.getPrecision; + +/** + * A writer to encode Fluss's {@link InternalRow} using Iceberg's binary encoding format. + * + * <p>This implementation follows Iceberg's binary encoding specification for partition and bucket + * keys. Reference: https://iceberg.apache.org/spec/#partition-transforms + * + * <p>The encoding logic is based on Iceberg's Conversions.toByteBuffer() implementation: + * https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/types/Conversions.java + * + * <p>Key encoding principles from Iceberg's Conversions class: + * + * <ul> + * <li>All numeric types (int, long, float, double, timestamps) use LITTLE-ENDIAN byte order + * <li>Decimal and UUID types use BIG-ENDIAN byte order + * <li>NO length prefix for any type - the buffer size determines the length + * <li>Strings are encoded as UTF-8 bytes without length prefix + * <li>Timestamps are stored as long values (microseconds since epoch) + * </ul> + * + * <p>Note: This implementation uses Fluss's MemorySegment instead of ByteBuffer for performance, + * but maintains byte-level compatibility with Iceberg's encoding. + */ +class IcebergBinaryRowWriter { + + private final int arity; + private byte[] buffer; + private MemorySegment segment; + private int cursor; + + public IcebergBinaryRowWriter(int arity) { + this.arity = arity; + // Conservative initial size to avoid frequent resizing + int initialSize = 8 + (arity * 8); + setBuffer(new byte[initialSize]); + reset(); + } + + public void reset() { + this.cursor = 0; + // Clear only the used portion for efficiency + if (cursor > 0) { + Arrays.fill(buffer, 0, Math.min(cursor, buffer.length), (byte) 0); + } + } + + public byte[] toBytes() { + byte[] result = new byte[cursor]; + System.arraycopy(buffer, 0, result, 0, cursor); + return result; + } + + public void setNullAt(int pos) { + // For Iceberg key encoding, null values should not occur + // This is validated at the encoder level + throw new UnsupportedOperationException( + "Null values are not supported in Iceberg key encoding"); + } + + public void writeBoolean(boolean value) { + ensureCapacity(1); + UnsafeUtils.putBoolean(buffer, cursor, value); + cursor += 1; + } + + public void writeByte(byte value) { + ensureCapacity(1); + UnsafeUtils.putByte(buffer, cursor, value); + cursor += 1; + } + + public void writeShort(short value) { + ensureCapacity(2); + UnsafeUtils.putShort(buffer, cursor, value); + cursor += 2; + } + + public void writeInt(int value) { + ensureCapacity(4); + UnsafeUtils.putInt(buffer, cursor, value); + cursor += 4; + } + + public void writeLong(long value) { + ensureCapacity(8); + UnsafeUtils.putLong(buffer, cursor, value); + cursor += 8; + } + + public void writeFloat(float value) { + ensureCapacity(4); + UnsafeUtils.putFloat(buffer, cursor, value); + cursor += 4; + } + + public void writeDouble(double value) { + ensureCapacity(8); + UnsafeUtils.putDouble(buffer, cursor, value); + cursor += 8; + } + + public void writeString(BinaryString value) { + // Based on Iceberg's Conversions.toByteBuffer() for STRING type: + // Strings are encoded as UTF-8 bytes without length prefix + // Reference:https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/types/Conversions.java + byte[] bytes = BinaryString.encodeUTF8(value.toString()); + ensureCapacity(bytes.length); + segment.put(cursor, bytes, 0, bytes.length); + cursor += bytes.length; + } + + void writeBytes(byte[] bytes) { + // Based on Iceberg's Conversions.toByteBuffer() for BINARY type: + // Binary data is stored directly without length prefix + ensureCapacity(bytes.length); Review Comment: added length, in this also, but this is adding lenght prefix in writedecimal also. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
