Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ public long getRowCountForIndex(long indexId, boolean strict) {
long rowCount = 0;
for (Map.Entry<Long, Partition> entry : idToPartition.entrySet()) {
MaterializedIndex index = entry.getValue().getIndex(indexId);
if (strict && !index.getRowCountReported()) {
if (index == null || strict && !index.getRowCountReported()) {
return -1;
}
rowCount += (index == null || index.getRowCount() == -1) ? 0 : index.getRowCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,27 @@ private ColumnStatistic getColumnStatistic(TableIf table, String colName, long i
}
}

/**
* if the table is not analyzed and BE does not report row count, return -1
*/
private double getOlapTableRowCount(OlapScan olapScan) {
OlapTable olapTable = olapScan.getTable();
AnalysisManager analysisManager = Env.getCurrentEnv().getAnalysisManager();
TableStatsMeta tableMeta = analysisManager.findTableStatsStatus(olapScan.getTable().getId());
double rowCount = -1;
if (tableMeta != null && tableMeta.userInjected) {
rowCount = tableMeta.getRowCount(olapScan.getSelectedIndexId());
} else {
rowCount = olapTable.getRowCountForIndex(olapScan.getSelectedIndexId(), true);
if (rowCount == -1) {
if (tableMeta != null) {
rowCount = tableMeta.getRowCount(olapScan.getSelectedIndexId());
}
}
}
return rowCount;
}

// TODO: 1. Subtract the pruned partition
// 2. Consider the influence of runtime filter
// 3. Get NDV and column data size from StatisticManger, StatisticManager doesn't support it now.
Expand All @@ -644,7 +665,16 @@ private Statistics computeCatalogRelation(CatalogRelation catalogRelation) {
TableStatsMeta tableMeta = analysisManager.findTableStatsStatus(table.getId());
// rows newly updated after last analyze
long deltaRowCount = tableMeta == null ? 0 : tableMeta.updatedRows.get();
double rowCount = catalogRelation.getTable().getRowCountForNereids();
double rowCount;
if (catalogRelation instanceof OlapScan) {
rowCount = getOlapTableRowCount((OlapScan) catalogRelation);
if (rowCount == -1) {
// if reported table row count is not valid, use 1
rowCount = 1;
}
} else {
rowCount = catalogRelation.getTable().getRowCountForNereids();
}
boolean hasUnknownCol = false;
long idxId = -1;
if (catalogRelation instanceof OlapScan) {
Expand All @@ -653,10 +683,7 @@ private Statistics computeCatalogRelation(CatalogRelation catalogRelation) {
idxId = olapScan.getSelectedIndexId();
}
}
// if (deltaRowCount > 0 && LOG.isDebugEnabled()) {
// LOG.debug("{} is partially analyzed, clear min/max values in column stats",
// catalogRelation.getTable().getName());
// }

for (SlotReference slotReference : slotSet) {
String colName = slotReference.getColumn().isPresent()
? slotReference.getColumn().get().getName()
Expand Down Expand Up @@ -684,20 +711,10 @@ private Statistics computeCatalogRelation(CatalogRelation catalogRelation) {
colStatsBuilder.setMinExpr(null);
colStatsBuilder.setMaxExpr(null);
}
if (!cache.isUnKnown) {
rowCount = Math.max(rowCount, cache.count + deltaRowCount);
} else {
if (cache.isUnKnown) {
hasUnknownCol = true;
}
if (ConnectContext.get() != null && ConnectContext.get().getSessionVariable().enableStats) {
// if (deltaRowCount > 0) {
// // clear min-max to avoid error estimation
// // for example, after yesterday data loaded, user send query about yesterday immediately.
// // since yesterday data are not analyzed, the max date is before yesterday, and hence optimizer
// // estimates the filter result is zero
// colStatsBuilder.setMinExpr(null).setMinValue(Double.NEGATIVE_INFINITY)
// .setMaxExpr(null).setMaxValue(Double.POSITIVE_INFINITY);
// }
columnStatisticBuilderMap.put(slotReference, colStatsBuilder);
} else {
columnStatisticBuilderMap.put(slotReference, new ColumnStatisticBuilder(ColumnStatistic.UNKNOWN));
Expand Down Expand Up @@ -1138,27 +1155,6 @@ public Statistics visitPhysicalCTEAnchor(
return groupExpression.childStatistics(1);
}

/**
* if the table is not analyzed and BE does not report row count, return -1
*/
private double getOlapTableRowCount(OlapScan olapScan) {
OlapTable olapTable = olapScan.getTable();
AnalysisManager analysisManager = Env.getCurrentEnv().getAnalysisManager();
TableStatsMeta tableMeta = analysisManager.findTableStatsStatus(olapScan.getTable().getId());
double rowCount = -1;
if (tableMeta != null && tableMeta.userInjected) {
rowCount = tableMeta.getRowCount(olapScan.getSelectedIndexId());
} else {
rowCount = olapTable.getRowCountForIndex(olapScan.getSelectedIndexId(), true);
if (rowCount == -1) {
if (tableMeta != null) {
rowCount = tableMeta.getRowCount(olapScan.getSelectedIndexId());
}
}
}
return rowCount;
}

/**
* disable join reorder if any table row count is not available.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testExecute() throws Exception {
}
Statistics statistics = cascadesContext.getMemo().getRoot().getStatistics();
Assertions.assertNotNull(statistics);
Assertions.assertTrue(Precision.equals(1, statistics.getRowCount(), 0.1));
Assertions.assertTrue(Precision.equals(0, statistics.getRowCount(), 0.1));
}

private LogicalOlapScan constructOlapSCan() {
Expand Down