You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2019/11/19 14:23:35 UTC

[GitHub] [incubator-iceberg] aokolnychyi commented on a change in pull request #655: Add PartitionsTable for partition metadata

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

 ##########
 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:
   Is it a good idea to extend `StaticTableScan`? Would it be more efficient to run an aggregation query on the files metadata table to compute stats per partition?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org