aokolnychyi commented on a change in pull request #2210:
URL: https://github.com/apache/iceberg/pull/2210#discussion_r599051198
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/Spark3Util.java
##########
@@ -790,4 +802,64 @@ public Identifier identifier() {
public static TableIdentifier identifierToTableIdentifier(Identifier
identifier) {
return TableIdentifier.of(Namespace.of(identifier.namespace()),
identifier.name());
}
+
+ /**
+ * Use Spark to list all partitions in the table.
+ *
+ * @param spark a Spark session
+ * @param rootPath a table identifier
+ * @param format format of the file
+ * @return all table's partitions
+ */
+ public static List<SparkPartition> getPartitions(SparkSession spark, Path
rootPath, String format) {
+ FileStatusCache fileStatusCache = FileStatusCache.getOrCreate(spark);
+ Map<String, String> emptyMap = Collections.emptyMap();
+
+ InMemoryFileIndex fileIndex = new InMemoryFileIndex(
+ spark,
+ JavaConverters
+ .collectionAsScalaIterableConverter(ImmutableList.of(rootPath))
+ .asScala()
+ .toSeq(),
+ JavaConverters
+ .mapAsScalaMapConverter(emptyMap)
+ .asScala()
+ .toMap(Predef.<Tuple2<String, String>>conforms()),
+ Option.empty(),
+ fileStatusCache,
+ Option.empty(),
+ Option.empty());
+
+ org.apache.spark.sql.execution.datasources.PartitionSpec spec =
fileIndex.partitionSpec();
+ StructType schema = spec.partitionColumns();
+ if (spec.partitions().isEmpty()) {
+ return ImmutableList.of(new SparkPartition(Collections.emptyMap(),
rootPath.toString(), format));
Review comment:
Well, if we want to validate that explicitly, we can do something like
this.
```
if (table.spec().isUnpartitioned()) {
List<SparkPartition> partitions = Spark3Util.getPartitions(spark(),
tableLocation, format);
Preconditions.checkArgument(partitions.size() == 0, ...);
SparkPartition partition = new SparkPartition(Collections.emptyMap(),
tableLocation.toString(), format);
importPartitions(table, ImmutableList.of(partition));
} else {
List<SparkPartition> partitions = Spark3Util.getPartitions(spark(),
tableLocation, format);
Preconditions.checkArgument(partitions.size() > 0, ...);
List<SparkPartition> filteredPartitions =
SparkTableUtil.filterPartitions(partitions, partitionFilter);
Preconditions.checkArgument(...);
importPartitions(table, filteredPartitions);
}
```
I am just thinking out loud here. The validation logic in `importFileTable`
was a bit hard to follow.
--
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]