Copilot commented on code in PR #6924:
URL: https://github.com/apache/hive/pull/6924#discussion_r4523496069
##########
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, so callers
that don't explicitly invoke `rewritableDeletes(...)` can trigger a
NullPointerException during DELETE writer creation (DV path calls
`rewritableDeletes.get()`). Initialize it to a safe default (e.g., supplier
returning `Map.of()`) in the constructor and/or guard in `build()` before
passing it downstream.
##########
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");
Review Comment:
`addToJobConf` assumes `DagUtils.TEZ_TMP_DIR_KEY` is always set; if it is
missing, this will fail at runtime when constructing the `Path`. Add an
explicit validation with a clear error (or fall back to an appropriate query
scratch directory from HiveConf) before using the value.
##########
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, so callers
that don't explicitly invoke `rewritableDeletes(...)` can trigger a
NullPointerException during DELETE writer creation (DV path calls
`rewritableDeletes.get()`). Initialize it to a safe default (e.g., supplier
returning `Map.of()`) in the constructor and/or guard in `build()` before
passing it downstream.
##########
ql/src/java/org/apache/hadoop/hive/ql/parse/rewrite/SplitMergeRewriter.java:
##########
@@ -75,6 +109,9 @@ public void
appendWhenMatchedUpdateClause(MergeStatement.UpdateClause updateClau
sqlGenerator.append("\n");
+ if (updateInDeleteBranch != null) {
Review Comment:
`appendWhenMatchedUpdateClause` returns early for *all* UPDATE clauses
whenever `updateInDeleteBranch` is non-null, which will skip generating the
delete-part for other UPDATE clauses (and can change MERGE semantics if
multiple UPDATE clauses exist). Make the early-return conditional on the
current `updateClause` being the one folded into the delete branch (or
track/merge all relevant UPDATE clauses into the single delete branch).
##########
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);
Review Comment:
This plans rewritable deletes by scanning the entire snapshot without
applying any filter expression for the current DML. Compared to the prior
approach (which could use `InputFormatConfig.FILTER_EXPRESSION`), this can
significantly increase planning/serialization size for large tables even if the
DML touches a small subset of files. If the filter expression is available in
the job conf at job-build time, consider applying it to `scan` before
`planFiles()` to reduce overhead.
##########
ql/src/java/org/apache/hadoop/hive/ql/parse/rewrite/sql/NonNativeAcidMultiInsertSqlGenerator.java:
##########
@@ -94,4 +94,13 @@ public List<String> getSortKeys(Operation operation) {
HiveUtils.unparseIdentifier(deletePrefix + fieldSchema.getName(),
this.conf)))
.collect(Collectors.toList());
}
+
+ @Override
+ public List<String> getDistributeKeys(Operation operation) {
+ return targetTable.getStorageHandler().acidDistributeColumns(targetTable,
operation)
+ .stream()
+ .map(fieldSchema -> qualify(
+ HiveUtils.unparseIdentifier(deletePrefix + fieldSchema.getName(),
this.conf)))
+ .toList();
Review Comment:
Using `Stream.toList()` requires newer Java runtimes (Java 16+). If this
module targets Java 8/11 (common for Hive builds), replace `.toList()` with
`collect(Collectors.toList())` to avoid compile/runtime incompatibilities.
--
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]