weizhengte commented on code in PR #10170: URL: https://github.com/apache/doris/pull/10170#discussion_r983052579
########## 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; Review Comment: Can we use `private final Long seek = -1;` instead of assigning a value here? ########## 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: Is this OK without the if? ########## 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 getSampleValues() { + return sampleValue; + } + + public boolean isPercent() { + return isPercent; + } + + public Long getSeek() { + return seek; + } + + @Override + public void analyze(Analyzer analyzer) throws AnalysisException { + if (sampleValue <= 0) { Review Comment: Can sampleValue be greater than 100 when isPercent is true? ########## 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 getSampleValues() { Review Comment: getSampleValues -> getSampleValue? ########## 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: Should we throw an exception to tell the user that other table type cannot be sampled? ########## 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) { + return; + } + sampleTabletIds.addAll(tabletIds); + if (tableSample == null) { + return; + } + OlapTable olapTable = (OlapTable) table; + long sampleRows; // The total number of sample rows + long hitRows = 1; // The total number of rows hit by the tablet + long totalRows = 0; // The total number of partition rows hit + long totalTablet = 0; // The total number of tablets in the hit partition + if (tableSample.isPercent()) { + sampleRows = (long) Math.max(olapTable.getRowCount() * (tableSample.getSampleValues() / 100.0), 1); Review Comment: As mentioned above, is it possible that sampleValue is greater than 100 here? ########## 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) { + return; + } + sampleTabletIds.addAll(tabletIds); + if (tableSample == null) { + return; + } + OlapTable olapTable = (OlapTable) table; + long sampleRows; // The total number of sample rows + long hitRows = 1; // The total number of rows hit by the tablet + long totalRows = 0; // The total number of partition rows hit + long totalTablet = 0; // The total number of tablets in the hit partition + if (tableSample.isPercent()) { + sampleRows = (long) Math.max(olapTable.getRowCount() * (tableSample.getSampleValues() / 100.0), 1); + } else { + sampleRows = Math.max(tableSample.getSampleValues(), 1); + } + + // calculate the number of tablets by each partition + long avgRowsPerPartition = sampleRows / Math.max(olapTable.getPartitions().size(), 1); + + for (Partition p : olapTable.getPartitions()) { + List<Long> ids = p.getBaseIndex().getTabletIdsInOrder(); + + if (ids.isEmpty()) { + continue; + } + + if (p.getBaseIndex().getRowCount() < (avgRowsPerPartition / 2)) { Review Comment: Why `avgRowsPerPartition / 2`, and will it cause totalRows to be less than sampleRows? -- 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-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org