This is an automated email from the ASF dual-hosted git repository.

lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 82144effc [core] Support statistics system table for paimon (#3172)
82144effc is described below

commit 82144effcae98607078c0214ae7e4e6100fcf7e5
Author: xuzifu666 <[email protected]>
AuthorDate: Wed Apr 10 14:41:49 2024 +0800

    [core] Support statistics system table for paimon (#3172)
---
 .../apache/paimon/table/system/StatisticTable.java | 228 +++++++++++++++++++++
 .../paimon/table/system/SystemTableLoader.java     |   3 +
 .../paimon/spark/sql/AnalyzeTableTestBase.scala    |  20 ++
 3 files changed, 251 insertions(+)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/system/StatisticTable.java 
b/paimon-core/src/main/java/org/apache/paimon/table/system/StatisticTable.java
new file mode 100644
index 000000000..2f795fd4a
--- /dev/null
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/system/StatisticTable.java
@@ -0,0 +1,228 @@
+/*
+ * 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.paimon.table.system;
+
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.data.GenericRow;
+import org.apache.paimon.data.InternalRow;
+import org.apache.paimon.disk.IOManager;
+import org.apache.paimon.fs.FileIO;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.predicate.Predicate;
+import org.apache.paimon.reader.RecordReader;
+import org.apache.paimon.stats.Statistics;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.table.ReadonlyTable;
+import org.apache.paimon.table.Table;
+import org.apache.paimon.table.source.InnerTableRead;
+import org.apache.paimon.table.source.InnerTableScan;
+import org.apache.paimon.table.source.ReadOnceTableScan;
+import org.apache.paimon.table.source.Split;
+import org.apache.paimon.table.source.TableRead;
+import org.apache.paimon.types.BigIntType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.RowType;
+import org.apache.paimon.utils.IteratorRecordReader;
+import org.apache.paimon.utils.JsonSerdeUtil;
+import org.apache.paimon.utils.ProjectedRow;
+import org.apache.paimon.utils.SerializationUtils;
+import org.apache.paimon.utils.SnapshotManager;
+
+import org.apache.paimon.shade.guava30.com.google.common.collect.Iterators;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import static org.apache.paimon.catalog.Catalog.SYSTEM_TABLE_SPLITTER;
+
+/** A {@link Table} for showing statistic of table. */
+public class StatisticTable implements ReadonlyTable {
+
+    private static final long serialVersionUID = 1L;
+
+    public static final String STATISTICS = "statistics";
+
+    public static final RowType TABLE_TYPE =
+            new RowType(
+                    Arrays.asList(
+                            new DataField(0, "snapshot_id", new 
BigIntType(false)),
+                            new DataField(1, "schema_id", new 
BigIntType(false)),
+                            new DataField(2, "mergedRecordCount", new 
BigIntType(true)),
+                            new DataField(3, "mergedRecordSize", new 
BigIntType(true)),
+                            new DataField(2, "colstat", 
SerializationUtils.newStringType(true))));
+
+    private final FileIO fileIO;
+    private final Path location;
+
+    private final FileStoreTable dataTable;
+
+    public StatisticTable(FileIO fileIO, Path location, FileStoreTable 
dataTable) {
+        this.fileIO = fileIO;
+        this.location = location;
+        this.dataTable = dataTable;
+    }
+
+    @Override
+    public String name() {
+        return dataTable.name() + SYSTEM_TABLE_SPLITTER + STATISTICS;
+    }
+
+    @Override
+    public RowType rowType() {
+        return TABLE_TYPE;
+    }
+
+    @Override
+    public List<String> primaryKeys() {
+        return Collections.singletonList("snapshot_id");
+    }
+
+    @Override
+    public InnerTableScan newScan() {
+        return new StatisticTable.StatisticScan();
+    }
+
+    @Override
+    public InnerTableRead newRead() {
+        return new StatisticRead(fileIO, dataTable);
+    }
+
+    @Override
+    public Table copy(Map<String, String> dynamicOptions) {
+        return new StatisticTable(fileIO, location, 
dataTable.copy(dynamicOptions));
+    }
+
+    private class StatisticScan extends ReadOnceTableScan {
+
+        @Override
+        public InnerTableScan withFilter(Predicate predicate) {
+            // TODO
+            return this;
+        }
+
+        @Override
+        public Plan innerPlan() {
+            long rowCount;
+            try {
+                rowCount = new SnapshotManager(fileIO, 
location).snapshotCount();
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+            return () ->
+                    Collections.singletonList(
+                            new StatisticTable.StatisticSplit(rowCount, 
location));
+        }
+    }
+
+    private static class StatisticSplit implements Split {
+
+        private static final long serialVersionUID = 1L;
+
+        private final long rowCount;
+        private final Path location;
+
+        private StatisticSplit(long rowCount, Path location) {
+            this.location = location;
+            this.rowCount = rowCount;
+        }
+
+        @Override
+        public long rowCount() {
+            return rowCount;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+            StatisticTable.StatisticSplit that = 
(StatisticTable.StatisticSplit) o;
+            return Objects.equals(location, that.location);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(location);
+        }
+    }
+
+    private static class StatisticRead implements InnerTableRead {
+
+        private final FileIO fileIO;
+        private int[][] projection;
+
+        private final FileStoreTable dataTable;
+
+        public StatisticRead(FileIO fileIO, FileStoreTable dataTable) {
+            this.fileIO = fileIO;
+            this.dataTable = dataTable;
+        }
+
+        @Override
+        public InnerTableRead withFilter(Predicate predicate) {
+            // TODO
+            return this;
+        }
+
+        @Override
+        public InnerTableRead withProjection(int[][] projection) {
+            this.projection = projection;
+            return this;
+        }
+
+        @Override
+        public TableRead withIOManager(IOManager ioManager) {
+            return this;
+        }
+
+        @Override
+        public RecordReader<InternalRow> createReader(Split split) throws 
IOException {
+            if (!(split instanceof StatisticTable.StatisticSplit)) {
+                throw new IllegalArgumentException("Unsupported split: " + 
split.getClass());
+            }
+            Statistics statistics = dataTable.statistics().get();
+            Iterator<Statistics> statisticsIterator =
+                    Collections.singletonList(statistics).iterator();
+            Iterator<InternalRow> rows = 
Iterators.transform(statisticsIterator, this::toRow);
+            if (projection != null) {
+                rows =
+                        Iterators.transform(
+                                rows, row -> 
ProjectedRow.from(projection).replaceRow(row));
+            }
+            return new IteratorRecordReader<>(rows);
+        }
+
+        private InternalRow toRow(Statistics statistics) {
+            return GenericRow.of(
+                    statistics.snapshotId(),
+                    statistics.schemaId(),
+                    statistics.mergedRecordCount().getAsLong(),
+                    statistics.mergedRecordSize().getAsLong(),
+                    
BinaryString.fromString(JsonSerdeUtil.toJson(statistics.colStats())));
+        }
+    }
+}
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/system/SystemTableLoader.java
 
b/paimon-core/src/main/java/org/apache/paimon/table/system/SystemTableLoader.java
index dcbe0fed1..8dbd53fe9 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/system/SystemTableLoader.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/system/SystemTableLoader.java
@@ -48,6 +48,7 @@ import static 
org.apache.paimon.table.system.SchemasTable.SCHEMAS;
 import static 
org.apache.paimon.table.system.SinkTableLineageTable.SINK_TABLE_LINEAGE;
 import static org.apache.paimon.table.system.SnapshotsTable.SNAPSHOTS;
 import static 
org.apache.paimon.table.system.SourceTableLineageTable.SOURCE_TABLE_LINEAGE;
+import static org.apache.paimon.table.system.StatisticTable.STATISTICS;
 import static org.apache.paimon.table.system.TagsTable.TAGS;
 import static org.apache.paimon.utils.Preconditions.checkNotNull;
 
@@ -82,6 +83,8 @@ public class SystemTableLoader {
                 return new ReadOptimizedTable(dataTable);
             case AGGREGATION:
                 return new AggregationFieldsTable(fileIO, location);
+            case STATISTICS:
+                return new StatisticTable(fileIO, location, dataTable);
             default:
                 return null;
         }
diff --git 
a/paimon-spark/paimon-spark-common/src/test/scala/org/apache/paimon/spark/sql/AnalyzeTableTestBase.scala
 
b/paimon-spark/paimon-spark-common/src/test/scala/org/apache/paimon/spark/sql/AnalyzeTableTestBase.scala
index b25881b72..dbe069be3 100644
--- 
a/paimon-spark/paimon-spark-common/src/test/scala/org/apache/paimon/spark/sql/AnalyzeTableTestBase.scala
+++ 
b/paimon-spark/paimon-spark-common/src/test/scala/org/apache/paimon/spark/sql/AnalyzeTableTestBase.scala
@@ -50,6 +50,26 @@ abstract class AnalyzeTableTestBase extends 
PaimonSparkTestBase {
     Assertions.assertTrue(stats.colStats().isEmpty)
   }
 
+  test("Paimon analyze: test statistic system table") {
+    spark.sql(s"""
+                 |CREATE TABLE T (id STRING, name STRING, i INT, l LONG)
+                 |USING PAIMON
+                 |TBLPROPERTIES ('primary-key'='id')
+                 |""".stripMargin)
+
+    spark.sql(s"INSERT INTO T VALUES ('1', 'a', 1, 1)")
+    spark.sql(s"INSERT INTO T VALUES ('2', 'aaa', 1, 2)")
+
+    spark.sql(s"ANALYZE TABLE T COMPUTE STATISTICS")
+
+    val df =
+      spark.sql("select snapshot_id, schema_id, mergedRecordCount, colstat 
from `T$statistics`")
+    Assertions.assertEquals(df.collect().size, 1)
+    checkAnswer(
+      spark.sql("SELECT snapshot_id, schema_id, mergedRecordCount, colstat 
from `T$statistics`"),
+      Row(2, 0, 2, "{ }"))
+  }
+
   test("Paimon analyze: analyze no scan") {
     spark.sql(s"CREATE TABLE T (id STRING, name STRING)")
     assertThatThrownBy(() => spark.sql(s"ANALYZE TABLE T COMPUTE STATISTICS 
NOSCAN"))

Reply via email to