You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/10/09 11:17:52 UTC

[GitHub] [doris] morrySnow commented on a diff in pull request #10170: [Enhancement](optimizer) Support select table sample

morrySnow commented on code in PR #10170:
URL: https://github.com/apache/doris/pull/10170#discussion_r990772731


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/TableRef.java:
##########
@@ -197,17 +205,49 @@ protected TableRef(TableRef other) {
                 lateralViewRefs.add((LateralViewRef) viewRef.clone());
             }
         }
+        if (other.sampleTabletIds.size() != 0) {

Review Comment:
   why not copy if size is 0?



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/TableSample.java:
##########
@@ -0,0 +1,101 @@
+// 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.doris.analysis;
+
+import org.apache.doris.common.AnalysisException;
+
+/*
+ * To represent following stmt:
+ *      TABLESAMPLE (10 PERCENT)
+ *      TABLESAMPLE (100 ROWS)
+ *      TABLESAMPLE (10 PERCENT) REPEATABLE (123)
+ *      TABLESAMPLE (100 ROWS) REPEATABLE (123)R
+ *
+ * references:
+ *      https://simplebiinsights.com/sql-server-tablesample-retrieving-random-data-from-sql-server/
+ *      https://sqlrambling.net/2018/01/24/tablesample-basic-examples/
+ */
+public class TableSample implements ParseNode {
+
+    private final Long sampleValue;
+    private final boolean isPercent;
+    private final Long seek;
+
+    public TableSample(boolean isPercent, Long sampleValue) {
+        this.sampleValue = sampleValue;
+        this.isPercent = isPercent;
+        this.seek = -1L;
+    }
+
+    public TableSample(boolean isPercent, Long sampleValue, Long seek) {
+        this.sampleValue = sampleValue;
+        this.isPercent = isPercent;
+        this.seek = seek;
+    }
+
+    public TableSample(TableSample other) {
+        this.sampleValue = other.sampleValue;
+        this.isPercent = other.isPercent;
+        this.seek = other.seek;
+    }
+
+    public Long getSampleValue() {
+        return sampleValue;
+    }
+
+    public boolean isPercent() {
+        return isPercent;
+    }
+
+    public Long getSeek() {
+        return seek;
+    }
+
+    @Override
+    public void analyze(Analyzer analyzer) throws AnalysisException {
+        if (sampleValue <= 0 || (isPercent && sampleValue > 100)) {
+            throw new AnalysisException("table sample value must be greater than 0, percent need less than 100.");
+        }
+    }
+
+    @Override
+    public String toSql() {
+        if (sampleValue == null) {
+            return "";
+        }
+        StringBuilder sb = new StringBuilder();
+        sb.append("TABLESAMPLE ( ");
+        sb.append(sampleValue);
+        if (isPercent) {
+            sb.append(" PERCENT ");
+        } else {
+            sb.append(" ROWS ");
+        }
+        sb.append(")");
+        if (seek != 0) {
+            sb.append(" REPEATABLE ");
+            sb.append(seek);

Review Comment:
   miss parentheses



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/TableRef.java:
##########
@@ -160,6 +161,12 @@ public TableRef(TableName name, String alias, PartitionNames partitionNames, Arr
             hasExplicitAlias = false;
         }
         this.partitionNames = partitionNames;
+        if (sampleTabletIds != null) {
+            this.sampleTabletIds = sampleTabletIds;
+        }
+        if (tableSample != null) {

Review Comment:
   i think zhengte is right, even if tableSample is null, it has no side effect.



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/TupleDescriptor.java:
##########
@@ -159,6 +164,80 @@ public void setTable(TableIf tbl) {
         table = tbl;
     }
 
+    public Set<Long> getSampleTabletIds() {
+        return sampleTabletIds;
+    }
+
+    /**
+     * First, determine how many rows to sample from each partition according to the number of partitions.
+     * Then determine the number of Tablets to be selected for each partition according to the average number
+     * of rows of Tablet,
+     * If seek is not specified, the specified number of Tablets are pseudo-randomly selected from each partition.
+     * If seek is specified, it will be selected sequentially from the seek tablet of the partition.
+     * And add the manually specified Tablet id to the selected Tablet.
+     * simpleTabletNums = simpleRows / partitionNums / (partitionRows / partitionTabletNums)
+     */
+    public void computeSampleTabletIds(List<Long> tabletIds, TableSample tableSample) {
+        if (table.getType() != TableType.OLAP) {

Review Comment:
   i agree, do it during analysis



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/TupleDescriptor.java:
##########
@@ -159,6 +164,84 @@ public void setTable(TableIf tbl) {
         table = tbl;
     }
 
+    public Set<Long> getSampleTabletIds() {
+        return sampleTabletIds;
+    }
+
+    /**
+     * First, determine how many rows to sample from each partition according to the number of partitions.
+     * Then determine the number of Tablets to be selected for each partition according to the average number
+     * of rows of Tablet,
+     * If seek is not specified, the specified number of Tablets are pseudo-randomly selected from each partition.
+     * If seek is specified, it will be selected sequentially from the seek tablet of the partition.
+     * And add the manually specified Tablet id to the selected Tablet.
+     * simpleTabletNums = simpleRows / partitionNums / (partitionRows / partitionTabletNums)
+     */
+    public void computeSampleTabletIds(List<Long> tabletIds, TableSample tableSample) {

Review Comment:
   why impl this function in TupleDescriptor? since TupleDescriptor is use for describe a tuple, this is very weired.



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org