rdblue commented on a change in pull request #655: Add PartitionsTable for 
partition metadata
URL: https://github.com/apache/incubator-iceberg/pull/655#discussion_r348072523
 
 

 ##########
 File path: core/src/main/java/org/apache/iceberg/PartitionsTable.java
 ##########
 @@ -0,0 +1,137 @@
+/*
+ * 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;
+
+import com.google.common.collect.Maps;
+import java.util.Map;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.StructLikeWrapper;
+
+/**
+ * A {@link Table} implementation that exposes a table's partitions as rows.
+ */
+public class PartitionsTable extends BaseMetadataTable {
+
+  private final TableOperations ops;
+  private final Table table;
+  private final Schema schema;
+
+  public PartitionsTable(TableOperations ops, Table table) {
+    this.ops = ops;
+    this.table = table;
+    this.schema = new Schema(
+        Types.NestedField.required(1, "partition", 
table.spec().partitionType()),
+        Types.NestedField.required(2, "record_count", Types.LongType.get()),
+        Types.NestedField.required(3, "file_count", Types.IntegerType.get())
+    );
+  }
+
+  @Override
+  Table table() {
+    return table;
+  }
+
+  @Override
+  String metadataTableName() {
+    return "partitions";
+  }
+
+  @Override
+  public TableScan newScan() {
+    return new PartitionsScan();
+  }
+
+  @Override
+  public String location() {
+    return ops.current().file().location();
+  }
+
+  @Override
+  public Schema schema() {
+    return schema;
+  }
+
+  private DataTask task(TableScan scan) {
+    return StaticDataTask.of(
+        ops.current().file(),
+        partitions(table, scan.snapshot().snapshotId()),
+        PartitionsTable::convertPartition);
+  }
+
+  private static StaticDataTask.Row convertPartition(Partition partition) {
+    return StaticDataTask.Row.of(partition.key, partition.recordCount, 
partition.fileCount);
+  }
+
+  private static Iterable<Partition> partitions(Table table, Long snapshotId) {
+    PartitionSet partitions = new PartitionSet();
+    TableScan scan = table.newScan();
+
+    if (snapshotId != null) {
+      scan = scan.useSnapshot(snapshotId);
+    }
+
+    for (FileScanTask task : scan.planFiles()) {
+      partitions.get(task.file().partition()).update(task.file());
+    }
+
+    return partitions.all();
+  }
+
+  private class PartitionsScan extends StaticTableScan {
 
 Review comment:
   Eventually, that's what we'd like to do. But running a query on top of the 
files table requires a catalog that supports views, and Spark doesn't support 
loading views from a v2 catalog yet. This gets the partitions table working in 
the mean time so users can run queries to find out rows per partition, or 
min/max of partition values.

----------------------------------------------------------------
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]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to