From 85a1db4b6c60b96192b01f41adaae5556310f6a1 Mon Sep 17 00:00:00 2001 From: starocean999 <40539150+starocean999@users.noreply.github.com> Date: Mon, 6 Nov 2023 17:57:19 +0800 Subject: [PATCH] [fix](nereids)select base index if select mv fails (#25715) --- .../nereids/pattern/PatternDescriptor.java | 19 ++++++++++++++++++ .../SelectMaterializedIndexWithAggregate.java | 20 +++++++++---------- ...lectMaterializedIndexWithoutAggregate.java | 10 +++++----- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternDescriptor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternDescriptor.java index c00f1711c5..90c143be1a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternDescriptor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternDescriptor.java @@ -21,6 +21,8 @@ import org.apache.doris.nereids.rules.RulePromise; import org.apache.doris.nereids.trees.plans.Plan; import com.google.common.collect.ImmutableList; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.List; import java.util.Objects; @@ -32,6 +34,7 @@ import java.util.function.Predicate; * It can support pattern generic type to MatchedAction. */ public class PatternDescriptor { + private static final Logger LOG = LogManager.getLogger(PatternDescriptor.class); public final Pattern pattern; public final RulePromise defaultPromise; @@ -63,6 +66,22 @@ public class PatternDescriptor { return new PatternMatcher<>(pattern, defaultPromise, matchedAction); } + /** + * Same as thenApply, but catch all exception and return null + */ + public PatternMatcher thenApplyNoThrow( + MatchedAction matchedAction) { + MatchedAction adaptMatchedAction = ctx -> { + try { + return matchedAction.apply(ctx); + } catch (Exception ex) { + LOG.warn("nereids apply rule failed, because {}", ex.getMessage(), ex); + return null; + } + }; + return new PatternMatcher<>(pattern, defaultPromise, adaptMatchedAction); + } + public PatternMatcher thenMulti( Function> matchedAction) { MatchedMultiAction adaptMatchedAction = ctx -> matchedAction.apply(ctx.root); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithAggregate.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithAggregate.java index b41fdc2d4e..abc842cb9a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithAggregate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithAggregate.java @@ -105,7 +105,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial return ImmutableList.of( // only agg above scan // Aggregate(Scan) - logicalAggregate(logicalOlapScan().when(this::shouldSelectIndexWithAgg)).thenApply(ctx -> { + logicalAggregate(logicalOlapScan().when(this::shouldSelectIndexWithAgg)).thenApplyNoThrow(ctx -> { LogicalAggregate agg = ctx.root; LogicalOlapScan scan = agg.child(); SelectResult result = select( @@ -140,7 +140,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial // filter could push down scan. // Aggregate(Filter(Scan)) logicalAggregate(logicalFilter(logicalOlapScan().when(this::shouldSelectIndexWithAgg))) - .thenApply(ctx -> { + .thenApplyNoThrow(ctx -> { LogicalAggregate> agg = ctx.root; LogicalFilter filter = agg.child(); LogicalOlapScan scan = filter.child(); @@ -191,7 +191,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial // column pruning or other projections such as alias, etc. // Aggregate(Project(Scan)) logicalAggregate(logicalProject(logicalOlapScan().when(this::shouldSelectIndexWithAgg))) - .thenApply(ctx -> { + .thenApplyNoThrow(ctx -> { LogicalAggregate> agg = ctx.root; LogicalProject project = agg.child(); LogicalOlapScan scan = project.child(); @@ -240,7 +240,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial // filter could push down and project. // Aggregate(Project(Filter(Scan))) logicalAggregate(logicalProject(logicalFilter(logicalOlapScan() - .when(this::shouldSelectIndexWithAgg)))).thenApply(ctx -> { + .when(this::shouldSelectIndexWithAgg)))).thenApplyNoThrow(ctx -> { LogicalAggregate>> agg = ctx.root; LogicalProject> project = agg.child(); LogicalFilter filter = project.child(); @@ -298,7 +298,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial // filter can't push down // Aggregate(Filter(Project(Scan))) logicalAggregate(logicalFilter(logicalProject(logicalOlapScan() - .when(this::shouldSelectIndexWithAgg)))).thenApply(ctx -> { + .when(this::shouldSelectIndexWithAgg)))).thenApplyNoThrow(ctx -> { LogicalAggregate>> agg = ctx.root; LogicalFilter> filter = agg.child(); LogicalProject project = filter.child(); @@ -354,7 +354,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial // only agg above scan // Aggregate(Repeat(Scan)) logicalAggregate( - logicalRepeat(logicalOlapScan().when(this::shouldSelectIndexWithAgg))).thenApply(ctx -> { + logicalRepeat(logicalOlapScan().when(this::shouldSelectIndexWithAgg))).thenApplyNoThrow(ctx -> { LogicalAggregate> agg = ctx.root; LogicalRepeat repeat = agg.child(); LogicalOlapScan scan = repeat.child(); @@ -396,7 +396,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial // filter could push down scan. // Aggregate(Repeat(Filter(Scan))) logicalAggregate(logicalRepeat(logicalFilter(logicalOlapScan().when(this::shouldSelectIndexWithAgg)))) - .thenApply(ctx -> { + .thenApplyNoThrow(ctx -> { LogicalAggregate>> agg = ctx.root; LogicalRepeat> repeat = agg.child(); LogicalFilter filter = repeat.child(); @@ -454,7 +454,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial // column pruning or other projections such as alias, etc. // Aggregate(Repeat(Project(Scan))) logicalAggregate(logicalRepeat(logicalProject(logicalOlapScan().when(this::shouldSelectIndexWithAgg)))) - .thenApply(ctx -> { + .thenApplyNoThrow(ctx -> { LogicalAggregate>> agg = ctx.root; LogicalRepeat> repeat = agg.child(); LogicalProject project = repeat.child(); @@ -509,7 +509,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial // filter could push down and project. // Aggregate(Repeat(Project(Filter(Scan)))) logicalAggregate(logicalRepeat(logicalProject(logicalFilter(logicalOlapScan() - .when(this::shouldSelectIndexWithAgg))))).thenApply(ctx -> { + .when(this::shouldSelectIndexWithAgg))))).thenApplyNoThrow(ctx -> { LogicalAggregate>>> agg = ctx.root; LogicalRepeat>> repeat = agg.child(); @@ -576,7 +576,7 @@ public class SelectMaterializedIndexWithAggregate extends AbstractSelectMaterial // filter can't push down // Aggregate(Repeat(Filter(Project(Scan)))) logicalAggregate(logicalRepeat(logicalFilter(logicalProject(logicalOlapScan() - .when(this::shouldSelectIndexWithAgg))))).thenApply(ctx -> { + .when(this::shouldSelectIndexWithAgg))))).thenApplyNoThrow(ctx -> { LogicalAggregate>>> agg = ctx.root; LogicalRepeat>> repeat = agg.child(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithoutAggregate.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithoutAggregate.java index 2ad22b0031..7787df22c7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithoutAggregate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithoutAggregate.java @@ -62,7 +62,7 @@ public class SelectMaterializedIndexWithoutAggregate extends AbstractSelectMater // project with pushdown filter. // Project(Filter(Scan)) logicalProject(logicalFilter(logicalOlapScan().when(this::shouldSelectIndexWithoutAgg))) - .thenApply(ctx -> { + .thenApplyNoThrow(ctx -> { LogicalProject> project = ctx.root; LogicalFilter filter = project.child(); LogicalOlapScan scan = filter.child(); @@ -82,7 +82,7 @@ public class SelectMaterializedIndexWithoutAggregate extends AbstractSelectMater // project with filter that cannot be pushdown. // Filter(Project(Scan)) logicalFilter(logicalProject(logicalOlapScan().when(this::shouldSelectIndexWithoutAgg))) - .thenApply(ctx -> { + .thenApplyNoThrow(ctx -> { LogicalFilter> filter = ctx.root; LogicalProject project = filter.child(); LogicalOlapScan scan = project.child(); @@ -101,7 +101,7 @@ public class SelectMaterializedIndexWithoutAggregate extends AbstractSelectMater // scan with filters could be pushdown. // Filter(Scan) logicalFilter(logicalOlapScan().when(this::shouldSelectIndexWithoutAgg)) - .thenApply(ctx -> { + .thenApplyNoThrow(ctx -> { LogicalFilter filter = ctx.root; LogicalOlapScan scan = filter.child(); LogicalOlapScan mvPlan = select( @@ -120,7 +120,7 @@ public class SelectMaterializedIndexWithoutAggregate extends AbstractSelectMater // project and scan. // Project(Scan) logicalProject(logicalOlapScan().when(this::shouldSelectIndexWithoutAgg)) - .thenApply(ctx -> { + .thenApplyNoThrow(ctx -> { LogicalProject project = ctx.root; LogicalOlapScan scan = project.child(); @@ -139,7 +139,7 @@ public class SelectMaterializedIndexWithoutAggregate extends AbstractSelectMater // only scan. logicalOlapScan() .when(this::shouldSelectIndexWithoutAgg) - .thenApply(ctx -> { + .thenApplyNoThrow(ctx -> { LogicalOlapScan scan = ctx.root; LogicalOlapScan mvPlan = select(