924060929 opened a new pull request, #9474:
URL: https://github.com/apache/incubator-doris/pull/9474
# Proposed changes
Issue Number: close #6483
## Problem Summary:
This pr provide a new pattern matching framework for Nereids optimizer.
The new pattern matching framework contains this concepts:
1. `Pattern`/`PatternDescriptor`: the tree node's multiple hierarchy shape,
e.g. `logicalJoin(logicalJoin(), any()` pattern describe a plan that root is a
`LogicalJoin` and the left child is `LogicalJoin` too.
2. `MatchedAction`: a callback function when the pattern matched, usually
you can create new plan to replace the origin matched plan.
3. `MatchingContext`: the param pass through MatchedAction, contains the
matched plan root and the PlannerContext.
4. `PatternMatcher`: contains PatternDescriptor and MatchedAction
5. `Rule`: a rewrite rule contains RuleType, PatternPromise, Pattern and
transform function(equals to MatchedAction)
6. `RuleFactory`: the factory can help us build Rules easily. RuleFactory
extends Patterns interface, and have some predefined pattern descriptors.
for example, Join commutative:
```java
public class JoinCommutative extends OneExplorationRuleFactory {
@Override
public Rule build() {
return innerLogicalJoin().thenApply(ctx -> {
return new LogicalJoin(
JoinType.INNER_JOIN,
ctx.root.getOnClause(),
ctx.root.right(),
ctx.root.left()
);
}).toRule(RuleType.LOGICAL_JOIN_COMMUTATIVE);
}
}
```
the code above show the three step to create a Rule
1. 'innerLogicalJoin()' declare pattern is an inner logical join.
'innerLogicalJoin' is a predefined pattern.
2. invoke 'then' function to combine a MatchedAction, return a new
LogicalJoin with exchange children.
3. invoke 'toRule' to convert to Rule
You can think the Rule contains three parts:
1. Pattern
2. transform function / MatchedAction
3. RuleType and RulePromise
So
1. `innerLogicalJoin()` create a `PatternDescriptor`, which contains a
`Pattern`
2. `PatternDescriptor.then()` convert `PatternDescriptor` to
`PatternMatcher,` witch contains Pattern and MatchedAction
3. `PatternMatcher.toRule()` convert `PatternMatcher` to a Rule
This three step inspired by the currying in function programing.
It should be noted, #9446 provide a generic type for TreeNode's children, so
we can infer multiple hierarchy type in this pattern matching framework, so you
can get the really tree node type without unsafely cast. like this:
```java
logicalJoin(logicalJoin(), any()).then(j -> {
// j can be infer type to LogicalJoin<LogicalJoin<Plan, Plan>, Plan>
// so j.left() can be infer type to LogicalJoin<Plan, Plan>,
// so you don't need to cast j.left() from 'Plan' to 'LogicalJoin'
var node = j.left().left();
})
```
## Checklist(Required)
1. Does it affect the original behavior: No
5. Has unit tests been added: No Need
7. Has document been added or modified: No Need
8. Does it need to update dependencies: No
9. Are there any changes that cannot be rolled back: Yes
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]