forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
642 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 0 additions & 163 deletions
163
fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/ExpressionPatternMappings.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/ExpressionPatternRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.apache.doris.nereids.pattern; | ||
|
||
import org.apache.doris.nereids.rules.expression.ExpressionMatchingContext; | ||
import org.apache.doris.nereids.rules.expression.ExpressionPatternMatchRule; | ||
import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext; | ||
import org.apache.doris.nereids.trees.expressions.Expression; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** ExpressionPatternMapping */ | ||
public class ExpressionPatternRules extends TypeMappings<Expression, ExpressionPatternMatchRule> { | ||
public ExpressionPatternRules(List<ExpressionPatternMatchRule> typeMappings) { | ||
super(typeMappings); | ||
} | ||
|
||
@Override | ||
protected Set<Class<? extends Expression>> getChildrenClasses(Class<? extends Expression> clazz) { | ||
return org.apache.doris.nereids.pattern.GeneratedExpressionRelations.CHILDREN_CLASS_MAP.get(clazz); | ||
} | ||
|
||
/** matchesAndApply */ | ||
public Expression matchesAndApply(Expression expr, ExpressionRewriteContext context, Expression parent) { | ||
List<ExpressionPatternMatchRule> rules = singleMappings.get(expr.getClass()); | ||
ExpressionMatchingContext<Expression> matchingContext | ||
= new ExpressionMatchingContext<>(expr, parent, context.cascadesContext); | ||
switch (rules.size()) { | ||
case 0: { | ||
for (ExpressionPatternMatchRule multiMatchRule : multiMappings) { | ||
if (multiMatchRule.matchesTypeAndPredicates(matchingContext)) { | ||
return multiMatchRule.apply(matchingContext); | ||
} | ||
} | ||
return expr; | ||
} | ||
case 1: { | ||
ExpressionPatternMatchRule rule = rules.get(0); | ||
if (rule.matchesPredicates(matchingContext)) { | ||
return rule.apply(matchingContext); | ||
} | ||
return expr; | ||
} | ||
default: { | ||
for (ExpressionPatternMatchRule rule : rules) { | ||
if (rule.matchesPredicates(matchingContext)) { | ||
Expression newExpr = rule.apply(matchingContext); | ||
if (!expr.equals(newExpr)) { | ||
return newExpr; | ||
} | ||
} | ||
} | ||
return expr; | ||
} | ||
} | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...re/src/main/java/org/apache/doris/nereids/pattern/ExpressionPatternTraverseListeners.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.apache.doris.nereids.pattern; | ||
|
||
import org.apache.doris.nereids.rules.expression.ExpressionMatchingContext; | ||
import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext; | ||
import org.apache.doris.nereids.rules.expression.ExpressionTraverseListener; | ||
import org.apache.doris.nereids.rules.expression.ExpressionTraverseListenerMapping; | ||
import org.apache.doris.nereids.trees.expressions.Expression; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import javax.annotation.Nullable; | ||
|
||
/** ExpressionPatternTraverseListeners */ | ||
public class ExpressionPatternTraverseListeners | ||
extends TypeMappings<Expression, ExpressionTraverseListenerMapping> { | ||
public ExpressionPatternTraverseListeners( | ||
List<ExpressionTraverseListenerMapping> typeMappings) { | ||
super(typeMappings); | ||
} | ||
|
||
@Override | ||
protected Set<Class<? extends Expression>> getChildrenClasses(Class<? extends Expression> clazz) { | ||
return org.apache.doris.nereids.pattern.GeneratedExpressionRelations.CHILDREN_CLASS_MAP.get(clazz); | ||
} | ||
|
||
/** matchesAndCombineListener */ | ||
public @Nullable CombinedListener matchesAndCombineListeners( | ||
Expression expr, ExpressionRewriteContext context, Expression parent) { | ||
List<ExpressionTraverseListenerMapping> listenerSingleMappings = singleMappings.get(expr.getClass()); | ||
ExpressionMatchingContext<Expression> matchingContext | ||
= new ExpressionMatchingContext<>(expr, parent, context.cascadesContext); | ||
switch (listenerSingleMappings.size()) { | ||
case 0: { | ||
ImmutableList.Builder<ExpressionTraverseListener<Expression>> matchedListeners | ||
= ImmutableList.builder(); | ||
for (ExpressionTraverseListenerMapping multiMapping : multiMappings) { | ||
if (multiMapping.matchesTypeAndPredicates(matchingContext)) { | ||
matchedListeners.add(multiMapping.listener); | ||
} | ||
} | ||
return CombinedListener.tryCombine(matchedListeners.build(), matchingContext); | ||
} | ||
case 1: { | ||
ExpressionTraverseListenerMapping listenerMapping = listenerSingleMappings.get(0); | ||
if (listenerMapping.matchesPredicates(matchingContext)) { | ||
return CombinedListener.tryCombine(ImmutableList.of(listenerMapping.listener), matchingContext); | ||
} | ||
return null; | ||
} | ||
default: { | ||
ImmutableList.Builder<ExpressionTraverseListener<Expression>> matchedListeners | ||
= ImmutableList.builder(); | ||
for (ExpressionTraverseListenerMapping singleMapping : listenerSingleMappings) { | ||
if (singleMapping.matchesPredicates(matchingContext)) { | ||
matchedListeners.add(singleMapping.listener); | ||
} | ||
} | ||
return CombinedListener.tryCombine(matchedListeners.build(), matchingContext); | ||
} | ||
} | ||
} | ||
|
||
public static class CombinedListener { | ||
private final ExpressionMatchingContext<Expression> context; | ||
private final List<ExpressionTraverseListener<Expression>> listeners; | ||
|
||
public static @Nullable CombinedListener tryCombine( | ||
List<ExpressionTraverseListener<Expression>> listenerMappings, | ||
ExpressionMatchingContext<Expression> context) { | ||
return listenerMappings.isEmpty() ? null : new CombinedListener(context, listenerMappings); | ||
} | ||
|
||
public CombinedListener(ExpressionMatchingContext<Expression> context, | ||
List<ExpressionTraverseListener<Expression>> listeners) { | ||
this.context = context; | ||
this.listeners = listeners; | ||
} | ||
|
||
public void onEnter() { | ||
for (ExpressionTraverseListener<Expression> listener : listeners) { | ||
listener.onEnter(context); | ||
} | ||
} | ||
|
||
public void onExit(Expression rewritten) { | ||
for (ExpressionTraverseListener<Expression> listener : listeners) { | ||
listener.onExit(context, rewritten); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.