diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java index bb10d762081947..cc00cfd87709cf 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java @@ -970,6 +970,8 @@ public void computeSampleTabletIds() { continue; } + // It is assumed here that all tablets row count is uniformly distributed + // TODO Use `p.getBaseIndex().getTablet(n).getRowCount()` to get each tablet row count to compute sample. long avgRowsPerTablet = Math.max(p.getBaseIndex().getRowCount() / ids.size(), 1); long tabletCounts = Math.max( avgRowsPerPartition / avgRowsPerTablet + (avgRowsPerPartition % avgRowsPerTablet != 0 ? 1 : 0), 1); @@ -1026,19 +1028,28 @@ private void computeTabletInfo() throws UserException { final Partition partition = olapTable.getPartition(partitionId); final MaterializedIndex selectedTable = partition.getIndex(selectedIndexId); final List tablets = Lists.newArrayList(); - final Collection tabletIds = distributionPrune(selectedTable, partition.getDistributionInfo()); + Collection tabletIds = distributionPrune(selectedTable, partition.getDistributionInfo()); LOG.debug("distribution prune tablets: {}", tabletIds); - if (tabletIds != null && sampleTabletIds.size() != 0) { - tabletIds.retainAll(sampleTabletIds); + if (sampleTabletIds.size() != 0) { + if (tabletIds != null) { + tabletIds.retainAll(sampleTabletIds); + } else { + tabletIds = sampleTabletIds; + } LOG.debug("after sample tablets: {}", tabletIds); } List allTabletIds = selectedTable.getTabletIdsInOrder(); if (tabletIds != null) { for (Long id : tabletIds) { - tablets.add(selectedTable.getTablet(id)); + if (selectedTable.getTablet(id) != null) { + tablets.add(selectedTable.getTablet(id)); + scanTabletIds.add(id); + } else { + // The tabletID specified in query does not exist in this partition, skip scan partition. + Preconditions.checkState(sampleTabletIds.size() != 0); + } } - scanTabletIds.addAll(tabletIds); } else { tablets.addAll(selectedTable.getTablets()); scanTabletIds.addAll(allTabletIds); 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 29aa6d66445ef2..ea692f28defffd 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 @@ -131,6 +131,20 @@ public static void setUp() throws Exception { + "\"in_memory\" = \"false\",\n" + "\"storage_format\" = \"V2\"\n" + ")"; + String tbl3 = "CREATE TABLE db1.table3 (\n" + + " `siteid` int(11) NULL DEFAULT \"10\" COMMENT \"\",\n" + + " `citycode` smallint(6) NULL COMMENT \"\",\n" + + " `username` varchar(32) NULL DEFAULT \"\" COMMENT \"\",\n" + + " `pv` bigint(20) NULL DEFAULT \"0\" COMMENT \"\"\n" + + ") ENGINE=OLAP\n" + + "UNIQUE KEY(`siteid`, `citycode`, `username`)\n" + + "COMMENT \"OLAP\"\n" + + "DISTRIBUTED BY RANDOM BUCKETS 10\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\",\n" + + "\"in_memory\" = \"false\",\n" + + "\"storage_format\" = \"V2\"\n" + + ")"; dorisAssert = new DorisAssert(); dorisAssert.withDatabase("db1").useDatabase("db1"); dorisAssert.withTable(createTblStmtStr) @@ -138,7 +152,8 @@ public static void setUp() throws Exception { .withTable(createPratitionTableStr) .withTable(createDatePartitionTableStr) .withTable(tbl1) - .withTable(tbl2); + .withTable(tbl2) + .withTable(tbl3); } @Test @@ -822,7 +837,7 @@ public void testSelectOuterJoinSql() throws Exception { } @Test - public void testSelectTablet() throws Exception { + public void testHashBucketSelectTablet() throws Exception { String sql1 = "SELECT * FROM db1.table1 TABLET(10031,10032,10033)"; OriginalPlanner planner = (OriginalPlanner) dorisAssert.query(sql1).internalExecuteOneAndGetPlan(); Set sampleTabletIds = ((OlapScanNode) planner.getScanNodes().get(0)).getSampleTabletIds(); @@ -831,6 +846,16 @@ public void testSelectTablet() throws Exception { Assert.assertTrue(sampleTabletIds.contains(10033L)); } + @Test + public void testRandomBucketSelectTablet() throws Exception { + String sql1 = "SELECT * FROM db1.table3 TABLET(10031,10032,10033)"; + OriginalPlanner planner = (OriginalPlanner) dorisAssert.query(sql1).internalExecuteOneAndGetPlan(); + Set sampleTabletIds = ((OlapScanNode) planner.getScanNodes().get(0)).getSampleTabletIds(); + Assert.assertTrue(sampleTabletIds.contains(10031L)); + Assert.assertTrue(sampleTabletIds.contains(10032L)); + Assert.assertTrue(sampleTabletIds.contains(10033L)); + } + @Test public void testSelectSampleTable() throws Exception { Database db = Env.getCurrentInternalCatalog().getDbOrMetaException("default_cluster:db1");