[fix](planner) Fix table sample not take effect if exist conjunct #29814
This commit is contained in:
@ -646,7 +646,7 @@ public class PhysicalPlanTranslator extends DefaultPlanVisitor<PlanFragment, Pla
|
||||
BaseTableRef tableRef = new BaseTableRef(ref, olapTable, tableName);
|
||||
tupleDescriptor.setRef(tableRef);
|
||||
olapScanNode.setSelectedPartitionIds(olapScan.getSelectedPartitionIds());
|
||||
olapScanNode.setSampleTabletIds(olapScan.getSelectedTabletIds()); // TODO
|
||||
olapScanNode.setSampleTabletIds(olapScan.getSelectedTabletIds());
|
||||
if (olapScan.getTableSample().isPresent()) {
|
||||
olapScanNode.setTableSample(new TableSample(olapScan.getTableSample().get().isPercent,
|
||||
olapScan.getTableSample().get().sampleValue, olapScan.getTableSample().get().seek));
|
||||
|
||||
@ -999,6 +999,7 @@ public class OlapScanNode extends ScanNode {
|
||||
|
||||
// 3. Sampling partition. If Seek is specified, the partition will be the same for each sampling.
|
||||
long hitRows = 0; // The number of rows hit by the tablet
|
||||
Set<Long> 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() {
|
||||
|
||||
@ -961,6 +961,11 @@ public class SelectStmtTest {
|
||||
OriginalPlanner planner16 = (OriginalPlanner) dorisAssert.query(sql16).internalExecuteOneAndGetPlan();
|
||||
Set<Long> 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<Long> sampleTabletIds17 = ((OlapScanNode) planner17.getScanNodes().get(0)).getSampleTabletIds();
|
||||
Assert.assertEquals(2, sampleTabletIds17.size());
|
||||
FeConstants.runningUnitTest = false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user