karuppayya commented on code in PR #4652:
URL: https://github.com/apache/iceberg/pull/4652#discussion_r925012655
##########
spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/actions/DeleteOrphanFilesSparkAction.java:
##########
@@ -326,6 +344,96 @@ private static FlatMapFunction<Iterator<String>, String>
listDirsRecursively(
};
}
+ @VisibleForTesting
+ static List<String> findOrphanFiles(SparkSession spark,
+ Dataset<Row> actualFileDF,
+ Dataset<Row> validFileDF,
+ Map<String, String> equalSchemes,
+ Map<String, String> equalAuthorities,
+ PrefixMismatchMode prefixMismatchMode) {
+ Map<String, String> equalSchemesMap = flattenMap(equalSchemes);
+ Map<String, String> equalAuthoritiesMap = flattenMap(equalAuthorities);
+
+ Dataset<PathProxy> normalizedActualFileDF = actualFileDF.mapPartitions(
+ toFileMetadata(equalSchemesMap, equalAuthoritiesMap),
+ Encoders.bean(PathProxy.class)).as("actual");
+ Dataset<PathProxy> normalizedValidFileDF = validFileDF.mapPartitions(
+ toFileMetadata(equalSchemesMap, equalAuthoritiesMap),
+ Encoders.bean(PathProxy.class)).as("valid");
+
+ Column actualFileName = normalizedActualFileDF.col("path");
+ Column validFileName = normalizedValidFileDF.col("path");
+
+ SetAccumulator<Pair<String, String>> setAccumulator = new
SetAccumulator<>();
+ spark.sparkContext().register(setAccumulator);
+
+ List<String> orphanFiles =
normalizedActualFileDF.joinWith(normalizedValidFileDF,
+ actualFileName.equalTo(validFileName), "leftouter")
+ .mapPartitions(findOrphanFilesMapPartitions(prefixMismatchMode,
setAccumulator), Encoders.STRING())
+ .collectAsList();
+
+ if (prefixMismatchMode == PrefixMismatchMode.ERROR &&
!setAccumulator.value().isEmpty()) {
+ throw new ValidationException("Unable to deterministically find all
orphan files." +
+ " Found file paths that have same file path but different
authorities/schemes. Conflicting" +
+ " authorities/schemes found: %s", setAccumulator.value().toString());
+ }
+ return orphanFiles;
+ }
+
+ private static Map<String, String> flattenMap(Map<String, String>
toBeFlattenedMap) {
+ Map<String, String> flattenedMap = Maps.newHashMap();
+ if (toBeFlattenedMap != null) {
+ for (String key : toBeFlattenedMap.keySet()) {
+ String value = toBeFlattenedMap.get(key);
+ Arrays.stream(key.split(",")).map(String::trim)
+ .forEach(splitKey -> flattenedMap.put(splitKey, value));
+ }
+ }
+ return flattenedMap;
+ }
+
+ private static MapPartitionsFunction<Tuple2<PathProxy, PathProxy>, String>
findOrphanFilesMapPartitions(
Review Comment:
removed
##########
spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/actions/DeleteOrphanFilesSparkAction.java:
##########
@@ -326,6 +344,96 @@ private static FlatMapFunction<Iterator<String>, String>
listDirsRecursively(
};
}
+ @VisibleForTesting
+ static List<String> findOrphanFiles(SparkSession spark,
+ Dataset<Row> actualFileDF,
+ Dataset<Row> validFileDF,
+ Map<String, String> equalSchemes,
+ Map<String, String> equalAuthorities,
+ PrefixMismatchMode prefixMismatchMode) {
+ Map<String, String> equalSchemesMap = flattenMap(equalSchemes);
+ Map<String, String> equalAuthoritiesMap = flattenMap(equalAuthorities);
+
+ Dataset<PathProxy> normalizedActualFileDF = actualFileDF.mapPartitions(
+ toFileMetadata(equalSchemesMap, equalAuthoritiesMap),
+ Encoders.bean(PathProxy.class)).as("actual");
+ Dataset<PathProxy> normalizedValidFileDF = validFileDF.mapPartitions(
+ toFileMetadata(equalSchemesMap, equalAuthoritiesMap),
+ Encoders.bean(PathProxy.class)).as("valid");
+
+ Column actualFileName = normalizedActualFileDF.col("path");
+ Column validFileName = normalizedValidFileDF.col("path");
+
+ SetAccumulator<Pair<String, String>> setAccumulator = new
SetAccumulator<>();
+ spark.sparkContext().register(setAccumulator);
+
+ List<String> orphanFiles =
normalizedActualFileDF.joinWith(normalizedValidFileDF,
+ actualFileName.equalTo(validFileName), "leftouter")
+ .mapPartitions(findOrphanFilesMapPartitions(prefixMismatchMode,
setAccumulator), Encoders.STRING())
+ .collectAsList();
+
+ if (prefixMismatchMode == PrefixMismatchMode.ERROR &&
!setAccumulator.value().isEmpty()) {
+ throw new ValidationException("Unable to deterministically find all
orphan files." +
+ " Found file paths that have same file path but different
authorities/schemes. Conflicting" +
+ " authorities/schemes found: %s", setAccumulator.value().toString());
+ }
+ return orphanFiles;
+ }
+
+ private static Map<String, String> flattenMap(Map<String, String>
toBeFlattenedMap) {
+ Map<String, String> flattenedMap = Maps.newHashMap();
+ if (toBeFlattenedMap != null) {
+ for (String key : toBeFlattenedMap.keySet()) {
+ String value = toBeFlattenedMap.get(key);
+ Arrays.stream(key.split(",")).map(String::trim)
+ .forEach(splitKey -> flattenedMap.put(splitKey, value));
+ }
+ }
+ return flattenedMap;
+ }
+
+ private static MapPartitionsFunction<Tuple2<PathProxy, PathProxy>, String>
findOrphanFilesMapPartitions(
+ PrefixMismatchMode mode,
+ SetAccumulator<Pair<String, String>> conflicts) {
+ return rows -> {
+ Iterator<String> transformed = Iterators.transform(rows, row -> {
+ PathProxy actual = row._1;
+ PathProxy valid = row._2;
+ if (valid == null) {
+ return actual.getFilePath();
+ }
+ boolean schemeMatch = Strings.isNullOrEmpty(valid.getScheme()) ||
Review Comment:
done
--
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]