diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java index 0de4b9497a..713fcc1bdf 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java @@ -646,7 +646,7 @@ public class PhysicalPlanTranslator extends DefaultPlanVisitor hitTabletIds = Sets.newHashSet(); long partitionSeek = tableSample.getSeek() != -1 ? tableSample.getSeek() : (long) (new SecureRandom().nextDouble() * selectedPartitionList.size()); for (int i = 0; i < selectedPartitionList.size(); i++) { @@ -1024,16 +1025,24 @@ public class OlapScanNode extends ScanNode { ? tableSample.getSeek() : (long) (new SecureRandom().nextDouble() * tablets.size()); for (int j = 0; j < tablets.size(); j++) { int seekTid = (int) ((j + tabletSeek) % tablets.size()); + Tablet tablet = tablets.get(seekTid); + if (sampleTabletIds.size() != 0 && !sampleTabletIds.contains(tablet.getId())) { + // After PruneOlapScanTablet, sampleTabletIds.size() != 0, + // continue sampling only in sampleTabletIds. + // If it is percentage sample, the number of sampled rows is a percentage of the + // total number of rows, and It is not related to sampleTabletI after PruneOlapScanTablet. + continue; + } long tabletRowCount; if (!FeConstants.runningUnitTest) { - tabletRowCount = tablets.get(seekTid).getRowCount(true); + tabletRowCount = tablet.getRowCount(true); } else { tabletRowCount = selectedTable.getRowCount() / tablets.size(); } if (tabletRowCount == 0) { continue; } - sampleTabletIds.add(tablets.get(seekTid).getId()); + hitTabletIds.add(tablet.getId()); sampleRows -= tabletRowCount; hitRows += tabletRowCount; if (sampleRows <= 0) { @@ -1044,7 +1053,15 @@ public class OlapScanNode extends ScanNode { break; } } - LOG.debug("after computeSampleTabletIds, hitRows {}, selectedRows {}", hitRows, selectedRows); + if (sampleTabletIds.size() != 0) { + sampleTabletIds.retainAll(hitTabletIds); + LOG.debug("after computeSampleTabletIds, hitRows {}, totalRows {}, selectedTablets {}, sampleRows {}", + hitRows, selectedRows, sampleTabletIds.size(), totalSampleRows); + } else { + sampleTabletIds = hitTabletIds; + LOG.debug("after computeSampleTabletIds, hitRows {}, selectedRows {}, sampleRows {}", hitRows, selectedRows, + totalSampleRows); + } } public boolean isFromPrepareStmt() { diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java index e2ecffaae5..331ee4eb06 100755 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java @@ -961,6 +961,11 @@ public class SelectStmtTest { OriginalPlanner planner16 = (OriginalPlanner) dorisAssert.query(sql16).internalExecuteOneAndGetPlan(); Set sampleTabletIds16 = ((OlapScanNode) planner16.getScanNodes().get(0)).getSampleTabletIds(); Assert.assertEquals(1, sampleTabletIds16.size()); + + String sql17 = "SELECT * FROM db1.table1 TABLESAMPLE(15 PERCENT) where siteid != 0"; + OriginalPlanner planner17 = (OriginalPlanner) dorisAssert.query(sql17).internalExecuteOneAndGetPlan(); + Set sampleTabletIds17 = ((OlapScanNode) planner17.getScanNodes().get(0)).getSampleTabletIds(); + Assert.assertEquals(2, sampleTabletIds17.size()); FeConstants.runningUnitTest = false; }