Gabriel39 commented on code in PR #71239:
URL: https://github.com/apache/doris/pull/71239#discussion_r4784827777


##########
fe/fe-iceberg-common/src/main/java/org/apache/iceberg/DorisMetadataRows.java:
##########
@@ -0,0 +1,156 @@
+// 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 org.apache.iceberg;
+
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ByteBuffers;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/** Keyless, self-contained metadata batches; no IO or encryption manager is 
serialized to a backend. */
+public final class DorisMetadataRows {
+    public static final long MAX_ROW_BYTES = 4L << 20;
+
+    private DorisMetadataRows() {
+    }
+
+    public static DataTask task(Schema schema, List<StructLike> rows) {
+        return task(schema, rows, 0);
+    }
+
+    /** Keep batch ordinals in task identity even when projected row values 
are identical. */
+    public static DataTask task(Schema schema, List<StructLike> rows, long 
batchOrdinal) {
+        DataFile file = DataFiles.builder(PartitionSpec.unpartitioned())
+                .withPath("iceberg-metadata-batch-" + batchOrdinal)
+                
.withFileSizeInBytes(0).withRecordCount(rows.size()).withFormat(FileFormat.METADATA).build();
+        return new StaticDataTask(file, schema, schema, rows.toArray(new 
StructLike[0]));
+    }
+
+    public static CopiedRow copy(StructLike row, Schema schema) {
+        Budget budget = new Budget();
+        return new CopiedRow(copyStruct(row, schema.asStruct(), budget), 
MAX_ROW_BYTES - budget.remaining);
+    }
+
+    /** A detached row and its conservative allocation charge for bounded 
metadata batching. */
+    public static final class CopiedRow {
+        private final StructLike row;
+        private final long bytes;
+
+        private CopiedRow(StructLike row, long bytes) {
+            this.row = row;
+            this.bytes = bytes;
+        }
+
+        public StructLike row() {
+            return row;
+        }
+
+        public long bytes() {
+            return bytes;
+        }
+    }
+
+    private static StructLike copyStruct(StructLike row, Types.StructType 
type, Budget budget) {
+        budget.charge(32L + 8L * type.fields().size());
+        Object[] values = new Object[type.fields().size()];
+        for (int index = 0; index < values.length; index++) {
+            Types.NestedField field = type.fields().get(index);
+            // These Iceberg IDs identify key carriers even inside 
entries.data_file. A user field
+            // with the same name in readable_metrics must retain its ordinary 
statistics.
+            if (field.fieldId() != DataFile.KEY_METADATA.fieldId()
+                    && field.fieldId() != ManifestFile.KEY_METADATA.fieldId()) 
{

Review Comment:
   Fixed in 780bd93b15da.
   
   Resolve key carriers from their canonical paths in the original 
metadata-table schema, before projection, rather than treating file/manifest 
IDs as globally reserved. The new wide SDK FILES-schema test preserves readable 
metric 519 through serialization. Separate coverage handles ALL_MANIFESTS key 
ID 19 and a promoted ordinary binary partition field named key_metadata.



##########
fe/fe-iceberg-common/src/main/java/org/apache/iceberg/DorisMetadataRows.java:
##########
@@ -0,0 +1,156 @@
+// 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 org.apache.iceberg;
+
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ByteBuffers;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/** Keyless, self-contained metadata batches; no IO or encryption manager is 
serialized to a backend. */
+public final class DorisMetadataRows {
+    public static final long MAX_ROW_BYTES = 4L << 20;
+
+    private DorisMetadataRows() {
+    }
+
+    public static DataTask task(Schema schema, List<StructLike> rows) {
+        return task(schema, rows, 0);
+    }
+
+    /** Keep batch ordinals in task identity even when projected row values 
are identical. */
+    public static DataTask task(Schema schema, List<StructLike> rows, long 
batchOrdinal) {
+        DataFile file = DataFiles.builder(PartitionSpec.unpartitioned())
+                .withPath("iceberg-metadata-batch-" + batchOrdinal)
+                
.withFileSizeInBytes(0).withRecordCount(rows.size()).withFormat(FileFormat.METADATA).build();
+        return new StaticDataTask(file, schema, schema, rows.toArray(new 
StructLike[0]));
+    }
+
+    public static CopiedRow copy(StructLike row, Schema schema) {
+        Budget budget = new Budget();
+        return new CopiedRow(copyStruct(row, schema.asStruct(), budget), 
MAX_ROW_BYTES - budget.remaining);
+    }
+
+    /** A detached row and its conservative allocation charge for bounded 
metadata batching. */
+    public static final class CopiedRow {
+        private final StructLike row;
+        private final long bytes;
+
+        private CopiedRow(StructLike row, long bytes) {
+            this.row = row;
+            this.bytes = bytes;
+        }
+
+        public StructLike row() {
+            return row;
+        }
+
+        public long bytes() {
+            return bytes;
+        }
+    }
+
+    private static StructLike copyStruct(StructLike row, Types.StructType 
type, Budget budget) {
+        budget.charge(32L + 8L * type.fields().size());
+        Object[] values = new Object[type.fields().size()];
+        for (int index = 0; index < values.length; index++) {
+            Types.NestedField field = type.fields().get(index);
+            // These Iceberg IDs identify key carriers even inside 
entries.data_file. A user field
+            // with the same name in readable_metrics must retain its ordinary 
statistics.
+            if (field.fieldId() != DataFile.KEY_METADATA.fieldId()
+                    && field.fieldId() != ManifestFile.KEY_METADATA.fieldId()) 
{
+                values[index] = copyValue(row.get(index, Object.class), 
field.type(), budget);
+            }
+        }
+        return StaticDataTask.Row.of(values);
+    }
+
+    private static Object copyValue(Object value, Type type, Budget budget) {
+        if (value == null) {
+            return null;
+        }
+        switch (type.typeId()) {
+            case STRUCT:
+                return copyStruct((StructLike) value, type.asStructType(), 
budget);
+            case LIST:
+                List<?> list = (List<?>) value;
+                budget.charge(32L + 8L * list.size());
+                List<Object> copiedList = new ArrayList<>(list.size());
+                for (Object element : list) {
+                    copiedList.add(copyValue(element, 
type.asListType().elementType(), budget));
+                }
+                return copiedList;
+            case MAP:
+                Map<?, ?> map = (Map<?, ?>) value;
+                budget.charge(64L + 48L * map.size());
+                Map<Object, Object> copiedMap = new LinkedHashMap<>();
+                for (Map.Entry<?, ?> entry : map.entrySet()) {
+                    copiedMap.put(copyValue(entry.getKey(), 
type.asMapType().keyType(), budget),
+                            copyValue(entry.getValue(), 
type.asMapType().valueType(), budget));
+                }
+                return copiedMap;
+            case STRING:
+                budget.charge(24L + 4L * ((CharSequence) value).length());
+                return value.toString();
+            case BINARY:
+            case FIXED:
+                if (value instanceof ByteBuffer) {
+                    ByteBuffer binary = (ByteBuffer) value;
+                    budget.charge(32L + binary.remaining());
+                    return new 
SerializedBinary(ByteBuffers.toByteArray(binary));

Review Comment:
   Fixed in 780bd93b15da.
   
   Always allocate a detached byte array and copy ByteBuffer.remaining() 
through a duplicate, preserving the original cursor. The regression copies two 
rows sharing one writable backing array, changes the second value before 
serialization, and verifies that the first row keeps its original bounds. This 
assertion fails with the old copier.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to