MehulBatra commented on code in PR #1350:
URL: https://github.com/apache/fluss/pull/1350#discussion_r2217452617


##########
fluss-common/src/main/java/com/alibaba/fluss/row/encode/iceberg/IcebergBinaryRowWriter.java:
##########
@@ -0,0 +1,390 @@
+/*
+ * 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.record.ChangeType;
+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 java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+
+import static com.alibaba.fluss.types.DataTypeChecks.getPrecision;
+
+/* This file is based on source code of Apache Iceberg Project 
(https://iceberg.apache.org/), licensed by the Apache
+ * Software Foundation (ASF) under the Apache License, Version 2.0. See the 
NOTICE file distributed with this work for
+ * additional information regarding copyright ownership. */
+
+/**
+ * A writer to encode Fluss's {@link InternalRow} using Iceberg's binary 
encoding way.
+ *
+ * <p>The logic follows Iceberg's binary encoding format for row data using 
MemorySegment.
+ */
+class IcebergBinaryRowWriter {
+
+    private final int arity;
+    private byte[] buffer;
+    private MemorySegment segment;
+    private int cursor;
+
+    public IcebergBinaryRowWriter(int arity) {
+        // Verify MemorySegment uses little-endian for Iceberg compatibility
+        if (!MemorySegment.LITTLE_ENDIAN) {
+            throw new IllegalStateException(
+                    "MemorySegment must use little-endian byte order for 
Iceberg compatibility");
+        }
+
+        this.arity = arity;
+        // Initial buffer size estimation
+        int initialSize = calculateInitialSize(arity);
+        setBuffer(new byte[initialSize]);
+        reset();
+    }
+
+    public void reset() {
+        this.cursor = 0;
+        // Clear buffer
+        Arrays.fill(buffer, 0, Math.min(buffer.length, 
calculateInitialSize(arity)), (byte) 0);
+    }
+
+    public byte[] toBytes() {
+        byte[] result = new byte[cursor];
+        System.arraycopy(buffer, 0, result, 0, cursor);
+        return result;
+    }
+
+    public void setNullAt(int pos) {
+        // Iceberg handles nulls with a null marker byte
+        ensureCapacity(1);
+        segment.put(cursor, (byte) 0x00); // Null marker
+        cursor += 1;
+    }
+
+    public void writeChangeType(ChangeType kind) {
+        // TODO DISCUSS WITH YUXIA
+        // Iceberg doesn't store ChangeType in binary format like Paimon
+        // This is handled at the metadata/manifest level
+        // We skip this for Iceberg binary encoding
+    }
+
+    public void writeBoolean(int pos, boolean value) {
+        ensureCapacity(1);
+        segment.put(cursor, value ? (byte) 1 : (byte) 0);
+        cursor += 1;
+    }
+
+    public void writeByte(int pos, byte value) {
+        ensureCapacity(1);
+        segment.put(cursor, value);
+        cursor += 1;
+    }
+
+    public void writeShort(int pos, short value) {
+        ensureCapacity(2);
+        segment.putShort(cursor, value);
+        cursor += 2;
+    }
+
+    public void writeInt(int pos, int value) {
+        ensureCapacity(4);
+        segment.putInt(cursor, value);
+        cursor += 4;
+    }
+
+    public void writeLong(int pos, long value) {
+        ensureCapacity(8);
+        segment.putLong(cursor, value);
+        cursor += 8;
+    }
+
+    public void writeFloat(int pos, float value) {
+        ensureCapacity(4);
+        segment.putFloat(cursor, value);
+        cursor += 4;
+    }
+
+    public void writeDouble(int pos, double value) {
+        ensureCapacity(8);
+        segment.putDouble(cursor, value);
+        cursor += 8;
+    }
+
+    public void writeString(int pos, BinaryString input) {
+        if (input == null) {

Review Comment:
   Sure!



-- 
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]

Reply via email to