Files
doris/fe
924060929 99b8e08a5f [Enhancement](Optimizer) Nereids pattern matching base framework (#9474)
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<Plan> 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 'thenApply()' function to combine a MatchedAction, return a new LogicalJoin with exchange children.
3. invoke 'toRule()' function 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 inferred type to LogicalJoin<LogicalJoin<Plan, Plan>, Plan>
     // so j.left() can be inferred type to LogicalJoin<Plan, Plan>,
     // so you don't need to cast j.left() from 'Plan' to 'LogicalJoin'
     var node = j.left().left();
})
```
2022-05-10 10:06:04 +08:00
..

# 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.

# fe-common

This module is used to store some common classes of other modules.

# spark-dpp

This module is Spark DPP program, used for Spark Load function.
Depends: fe-common

# fe-core

This module is the main process module of FE.
Depends: fe-common, spark-dpp