Copilot commented on code in PR #6925:
URL: https://github.com/apache/hive/pull/6925#discussion_r4538623739
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/writer/WriterBuilder.java:
##########
@@ -86,7 +71,6 @@ private WriterBuilder(Table table, UnaryOperator<String> ops)
{
this.context = new Context(table.properties(), ops, tableName);
this.operation = HiveCustomStorageHandlerUtils.getWriteOperation(ops,
tableName);
this.copyOnWrite = HiveCustomStorageHandlerUtils.isCopyOnWrite(ops,
tableName);
Review Comment:
`rewritableDeletes` is no longer initialized in the constructor, which can
lead to a NullPointerException when building delete writers (especially for
DV-enabled tables) unless every call site remembers to set it. Consider
initializing it to a safe default (e.g., a supplier returning `Map.of()`), and
optionally enforce non-null in `rewritableDeletes(...)` to keep behavior
consistent and fail fast for misconfiguration.
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/writer/WriterBuilder.java:
##########
@@ -103,6 +87,11 @@ public WriterBuilder queryId(String newQueryId) {
return this;
}
+ public WriterBuilder rewritableDeletes(Supplier<Map<String, DeleteFileSet>>
newRewritableDeletes) {
+ this.rewritableDeletes = newRewritableDeletes;
+ return this;
+ }
Review Comment:
`rewritableDeletes` is no longer initialized in the constructor, which can
lead to a NullPointerException when building delete writers (especially for
DV-enabled tables) unless every call site remembers to set it. Consider
initializing it to a safe default (e.g., a supplier returning `Map.of()`), and
optionally enforce non-null in `rewritableDeletes(...)` to keep behavior
consistent and fail fast for misconfiguration.
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/writer/WriterBuilder.java:
##########
@@ -138,7 +127,7 @@ public HiveIcebergWriter build() {
} else {
writer = switch (operation) {
case DELETE ->
- new HiveIcebergDeleteWriter(table, rewritableDeletes.get(),
writerFactory, deleteFileFactory, context);
+ new HiveIcebergDeleteWriter(table, rewritableDeletes,
writerFactory, deleteFileFactory, context);
Review Comment:
`rewritableDeletes` is no longer initialized in the constructor, which can
lead to a NullPointerException when building delete writers (especially for
DV-enabled tables) unless every call site remembers to set it. Consider
initializing it to a safe default (e.g., a supplier returning `Map.of()`), and
optionally enforce non-null in `rewritableDeletes(...)` to keep behavior
consistent and fail fast for misconfiguration.
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/writer/WriterBuilder.java:
##########
@@ -31,42 +29,29 @@
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
-import org.apache.commons.lang3.ObjectUtils;
import org.apache.hadoop.hive.ql.Context.Operation;
import
org.apache.hadoop.hive.ql.security.authorization.HiveCustomStorageHandlerUtils;
import org.apache.hadoop.hive.ql.session.SessionStateUtil;
import org.apache.hadoop.mapred.TaskAttemptID;
-import org.apache.iceberg.BatchScan;
-import org.apache.iceberg.DeleteFile;
-import org.apache.iceberg.FileContent;
import org.apache.iceberg.FileFormat;
-import org.apache.iceberg.FileScanTask;
import org.apache.iceberg.MetadataColumns;
-import org.apache.iceberg.ScanTask;
-import org.apache.iceberg.Snapshot;
import org.apache.iceberg.Table;
import org.apache.iceberg.TableProperties;
import org.apache.iceberg.deletes.DeleteGranularity;
-import org.apache.iceberg.expressions.Expression;
-import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.io.OutputFileFactory;
import org.apache.iceberg.mr.Catalogs;
-import org.apache.iceberg.mr.InputFormatConfig;
import org.apache.iceberg.mr.hive.IcebergTableUtil;
-import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
-import org.apache.iceberg.util.ContentFileUtil;
import org.apache.iceberg.util.DeleteFileSet;
import org.apache.iceberg.util.PropertyUtil;
-import org.apache.iceberg.util.SerializationUtil;
import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT;
import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT_DEFAULT;
import static org.apache.iceberg.TableProperties.DELETE_DEFAULT_FILE_FORMAT;
public class WriterBuilder {
private final Table table;
- private final Supplier<Map<String, DeleteFileSet>> rewritableDeletes;
+ private Supplier<Map<String, DeleteFileSet>> rewritableDeletes;
Review Comment:
`rewritableDeletes` is no longer initialized in the constructor, which can
lead to a NullPointerException when building delete writers (especially for
DV-enabled tables) unless every call site remembers to set it. Consider
initializing it to a safe default (e.g., a supplier returning `Map.of()`), and
optionally enforce non-null in `rewritableDeletes(...)` to keep behavior
consistent and fail fast for misconfiguration.
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/writer/HiveIcebergDeleteWriter.java:
##########
@@ -20,47 +20,62 @@
package org.apache.iceberg.mr.hive.writer;
import java.io.IOException;
+import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.function.Supplier;
import org.apache.hadoop.io.Writable;
import org.apache.iceberg.DeleteFile;
-import org.apache.iceberg.PartitionKey;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.StructLike;
import org.apache.iceberg.Table;
-import org.apache.iceberg.data.GenericRecord;
import org.apache.iceberg.data.Record;
-import org.apache.iceberg.deletes.PositionDelete;
import org.apache.iceberg.io.DeleteWriteResult;
import org.apache.iceberg.io.OutputFileFactory;
import org.apache.iceberg.mr.hive.FilesForCommit;
import org.apache.iceberg.mr.hive.IcebergAcidUtil;
import org.apache.iceberg.mr.hive.writer.WriterBuilder.Context;
import org.apache.iceberg.mr.mapred.Container;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.types.Type;
import org.apache.iceberg.util.DeleteFileSet;
class HiveIcebergDeleteWriter extends HiveIcebergWriterBase {
- private final GenericRecord rowDataTemplate;
- private final boolean isMergeTask;
+ // the partitions of the deleted records by spec and serialized partition
+ private final Map<Integer, Map<ByteBuffer, StructLike>> partitions;
HiveIcebergDeleteWriter(
- Table table, Map<String, DeleteFileSet> rewritableDeletes,
+ Table table, Supplier<Map<String, DeleteFileSet>> rewritableDeletes,
HiveFileWriterFactory writerFactory, OutputFileFactory deleteFileFactory,
Context context) {
super(table, newDeleteWriter(table, rewritableDeletes, writerFactory,
deleteFileFactory, context));
-
- this.rowDataTemplate = GenericRecord.create(table.schema());
- this.isMergeTask = context.isMergeTask();
+ this.partitions = Maps.newHashMapWithExpectedSize(specs.size());
}
@Override
public void write(Writable row) throws IOException {
Record rec = ((Container<Record>) row).get();
- PositionDelete<Record> positionDelete =
IcebergAcidUtil.getPositionDelete(rec, rowDataTemplate, isMergeTask);
- int specId = IcebergAcidUtil.parseSpecId(rec);
- PartitionKey partitionKey = isMergeTask ?
IcebergAcidUtil.parsePartitionKey(rec) :
- partition(rowDataTemplate, specId);
- writer.write(positionDelete, specs.get(specId), partitionKey);
+ PartitionSpec spec = specs.get(IcebergAcidUtil.parseSpecId(rec));
+ StructLike partition = partitions.computeIfAbsent(spec.specId(), id ->
newPartitionCache(spec))
+ .computeIfAbsent(IcebergAcidUtil.parseSerializedPartition(rec),
+ serialized -> IcebergAcidUtil.deserializePartition(serialized,
spec));
Review Comment:
Using a `ByteBuffer` returned from a record as a `HashMap` key is fragile if
that buffer’s position/limit can change (ByteBuffer hashCode/equals depend on
remaining bytes). To avoid hard-to-debug cache misses, consider
normalizing/copying the buffer before using it as a key (e.g., wrap a defensive
`byte[]` copy or use an immutable `ByteBuffer` copy) so the key remains stable
for the map’s lifetime.
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/writer/RewritableDeletes.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.mr.hive.writer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.util.Map;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.exec.ObjectCache;
+import org.apache.hadoop.hive.ql.exec.ObjectCacheFactory;
+import org.apache.hadoop.hive.ql.exec.tez.DagUtils;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileContent;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableScan;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.mr.hive.IcebergTableUtil;
+import org.apache.iceberg.relocated.com.google.common.collect.ForwardingMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.util.DeleteFileSet;
+import org.apache.iceberg.util.SerializationUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The previous deletes that the DV writers of a merge-on-read DML merge into
the DVs they write: a DV replaces every
+ * position delete of its data file. They reach the writers in a temp file of
the query that the job's configuration
+ * names, read once per LLAP daemon.
+ */
+public final class RewritableDeletes extends ForwardingMap<String,
DeleteFileSet> {
+ private static final Logger LOG =
LoggerFactory.getLogger(RewritableDeletes.class);
+
+ private static final String LOCATION_PREFIX =
"iceberg.mr.rewritable.deletes.";
+
+ // by data file location
+ private final Map<String, DeleteFileSet> deletes;
+
+ private RewritableDeletes(Map<String, DeleteFileSet> deletes) {
+ this.deletes = deletes;
+ }
+
+ @Override
+ protected Map<String, DeleteFileSet> delegate() {
+ return deletes;
+ }
+
+ /**
+ * Adds the rewritable deletes of a table to the job, if there are any.
+ * @param table The table the DML writes.
+ * @param tableName The name of the table in the job's configuration.
+ * @param ref The branch the DML writes.
+ * @param jobConf The job's configuration, with the temp dir of the query.
+ */
+ public static void addToJobConf(Table table, String tableName, String ref,
JobConf jobConf) {
+ String key = LOCATION_PREFIX + tableName;
+ if (jobConf.get(key) != null) {
+ return;
+ }
+ Snapshot snapshot = IcebergTableUtil.getTableSnapshot(table, ref);
+ if (snapshot == null) {
+ return;
+ }
+ TableScan scan = table.newScan().useSnapshot(snapshot.snapshotId());
+ Map<String, DeleteFileSet> deletes = rewritableDeletes(scan);
+ if (deletes.isEmpty()) {
+ return;
+ }
+ Path path = new Path(jobConf.get(DagUtils.TEZ_TMP_DIR_KEY), tableName +
"-rewritable-deletes");
+ try (OutputStream out = new
GZIPOutputStream(path.getFileSystem(jobConf).create(path))) {
+ out.write(SerializationUtil.serializeToBytes(deletes));
+ } catch (IOException e) {
+ throw new UncheckedIOException(String.format("Failed to write rewritable
deletes to %s", path), e);
+ }
Review Comment:
`addToJobConf` unconditionally uses `DagUtils.TEZ_TMP_DIR_KEY` as the parent
directory. If this code path executes under a non-Tez engine (or if the key
isn't set for some reason), `jobConf.get(DagUtils.TEZ_TMP_DIR_KEY)` may be null
and `new Path(null, ...)` can fail. Please add a guard (e.g., require the key
to be present, or fall back to an engine-appropriate scratch directory) so
planning rewritable deletes is robust across execution engines.
--
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]