singhpk234 commented on code in PR #4956: URL: https://github.com/apache/iceberg/pull/4956#discussion_r890338142
########## spark/v3.0/spark/src/main/java/org/apache/iceberg/spark/CallerWithCommitMetadata.java: ########## @@ -0,0 +1,61 @@ +/* + * 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.spark; + +import java.util.Map; +import java.util.concurrent.Callable; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.util.ExceptionUtil; + +/** + * utility class to accept thread local commit properties + */ +public class CallerWithCommitMetadata { + + private CallerWithCommitMetadata() { + + } + + private static final ThreadLocal<Map<String, String>> COMMIT_PROPERTIES = ThreadLocal.withInitial(ImmutableMap::of); + + /** + * running the code wrapped as a caller, and any snapshot committed within the callable object will be attached with + * the metadata defined in properties + * @param properties extra commit metadata to attach to the snapshot committed within callable + * @param callable the code to be executed + * @param exClass the expected type of exception which would be thrown from callable + */ + public static <R, E extends Exception> R withCommitProperties( + Map<String, String> properties, Callable<R> callable, Class<E> exClass) throws E { + COMMIT_PROPERTIES.set(properties); + try { + return callable.call(); + } catch (Throwable e) { + ExceptionUtil.castAndThrow(e, exClass); + return null; Review Comment: [minor] is this required as we throw in the line above ? ########## spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkWrite.java: ########## @@ -192,6 +193,10 @@ private void commitOperation(SnapshotUpdate<?> operation, String description) { extraSnapshotMetadata.forEach(operation::set); } + if (!CallerWithCommitMetadata.commitProperties().isEmpty()) { + CallerWithCommitMetadata.commitProperties().forEach(operation::set); + } Review Comment: [question] Should we also add this to SparkPositionWrite https://github.com/apache/iceberg/blob/b06a89cebd5099b40b188c4c40ea7b1a23d3427a/spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPositionDeltaWrite.java#L250 since starting 3.2 iceberg support MOR with pos deletes ########## spark/v3.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkWrite.java: ########## @@ -174,6 +175,10 @@ private void commitOperation(SnapshotUpdate<?> operation, String description) { extraSnapshotMetadata.forEach(operation::set); } + if (!CallerWithCommitMetadata.commitProperties().isEmpty()) { + CallerWithCommitMetadata.commitProperties().forEach(operation::set); + } Review Comment: [doubt](Probably not in scope of PR) I see there is no validation happening in the keys passed from here doesn't override the keys already present in the summary, should we also add one ? -- 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]
