From b226fe54e93932db14812abf5548c1e0221a8ee4 Mon Sep 17 00:00:00 2001 From: Jibing-Li <64681310+Jibing-Li@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:07:02 +0800 Subject: [PATCH] [fix](statistics)Fix select mv with specified partitions bug. (#36817) (#36982) There is a bug of direct select mv with specified partitions. Planner will fail to find the mv column name. Because we need to create the LogicalOlapScan object using the given mv instead of the base table. ``` mysql> SELECT mv_id from part8 index mv1 partition p1; ERROR 1105 (HY000): errCode = 2, detailMessage = Unknown column 'mv_id' in 'table list' in PROJECT clause ``` This pr is to fix this. backport: https://github.com/apache/doris/pull/36817 --- .../doris/nereids/rules/analysis/BindRelation.java | 9 ++++++--- .../rules/exploration/mv/MaterializedViewUtils.java | 2 ++ .../nereids/trees/plans/logical/LogicalOlapScan.java | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java index d005641881..e8fd815654 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java @@ -188,12 +188,13 @@ public class BindRelation extends OneAnalysisRuleFactory { LogicalOlapScan scan; List partIds = getPartitionIds(table, unboundRelation); List tabletIds = unboundRelation.getTabletIds(); - if (!CollectionUtils.isEmpty(partIds)) { + if (!CollectionUtils.isEmpty(partIds) && !unboundRelation.getIndexName().isPresent()) { scan = new LogicalOlapScan(unboundRelation.getRelationId(), (OlapTable) table, tableQualifier, partIds, tabletIds, unboundRelation.getHints(), unboundRelation.getTableSample()); } else { Optional indexName = unboundRelation.getIndexName(); + // For direct mv scan. if (indexName.isPresent()) { OlapTable olapTable = (OlapTable) table; Long indexId = olapTable.getIndexIdByName(indexName.get()); @@ -207,8 +208,10 @@ public class BindRelation extends OneAnalysisRuleFactory { : PreAggStatus.off("For direct index scan."); scan = new LogicalOlapScan(unboundRelation.getRelationId(), - (OlapTable) table, tableQualifier, tabletIds, indexId, - preAggStatus, unboundRelation.getHints(), unboundRelation.getTableSample()); + (OlapTable) table, tableQualifier, tabletIds, + CollectionUtils.isEmpty(partIds) ? ((OlapTable) table).getPartitionIds() : partIds, indexId, + preAggStatus, CollectionUtils.isEmpty(partIds) ? ImmutableList.of() : partIds, + unboundRelation.getHints(), unboundRelation.getTableSample()); } else { scan = new LogicalOlapScan(unboundRelation.getRelationId(), (OlapTable) table, tableQualifier, tabletIds, unboundRelation.getHints(), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewUtils.java index 2e45fa4483..53f775e217 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewUtils.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewUtils.java @@ -200,8 +200,10 @@ public class MaterializedViewUtils { materializedView, materializedView.getFullQualifiers(), ImmutableList.of(), + materializedView.getPartitionIds(), materializedView.getBaseIndexId(), PreAggStatus.on(), + ImmutableList.of(), // this must be empty, or it will be used to sample ImmutableList.of(), Optional.empty()); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java index 76d360ed53..2f0b3b0a76 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java @@ -146,12 +146,12 @@ public class LogicalOlapScan extends LogicalCatalogRelation implements OlapScan } public LogicalOlapScan(RelationId id, OlapTable table, List qualifier, List tabletIds, - long selectedIndexId, PreAggStatus preAggStatus, List hints, - Optional tableSample) { + List selectedPartitionIds, long selectedIndexId, PreAggStatus preAggStatus, + List specifiedPartitions, List hints, Optional tableSample) { this(id, table, qualifier, Optional.empty(), Optional.empty(), - table.getPartitionIds(), false, tabletIds, + selectedPartitionIds, false, tabletIds, selectedIndexId, true, preAggStatus, - ImmutableList.of(), hints, Maps.newHashMap(), tableSample, true, ImmutableMap.of()); + specifiedPartitions, hints, Maps.newHashMap(), tableSample, true, ImmutableMap.of()); } /**