1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | ||
7 | package org.opensearch.sql.planner.optimizer; | |
8 | ||
9 | import static com.facebook.presto.matching.DefaultMatcher.DEFAULT_MATCHER; | |
10 | ||
11 | import com.facebook.presto.matching.Match; | |
12 | import java.util.Arrays; | |
13 | import java.util.List; | |
14 | import java.util.stream.Collectors; | |
15 | import org.opensearch.sql.expression.DSL; | |
16 | import org.opensearch.sql.planner.logical.LogicalPlan; | |
17 | import org.opensearch.sql.planner.optimizer.rule.MergeFilterAndFilter; | |
18 | import org.opensearch.sql.planner.optimizer.rule.PushFilterUnderSort; | |
19 | ||
20 | /** | |
21 | * {@link LogicalPlan} Optimizer. | |
22 | * The Optimizer will run in the TopDown manner. | |
23 | * 1> Optimize the current node with all the rules. | |
24 | * 2> Optimize the all the child nodes with all the rules. | |
25 | * 3) In case the child node could change, Optimize the current node again. | |
26 | */ | |
27 | public class LogicalPlanOptimizer { | |
28 | ||
29 | private final List<Rule<?>> rules; | |
30 | ||
31 | /** | |
32 | * Create {@link LogicalPlanOptimizer} with customized rules. | |
33 | */ | |
34 | public LogicalPlanOptimizer(List<Rule<?>> rules) { | |
35 | this.rules = rules; | |
36 | } | |
37 | ||
38 | /** | |
39 | * Create {@link LogicalPlanOptimizer} with pre-defined rules. | |
40 | */ | |
41 | public static LogicalPlanOptimizer create(DSL dsl) { | |
42 |
1
1. create : replaced return value with null for org/opensearch/sql/planner/optimizer/LogicalPlanOptimizer::create → KILLED |
return new LogicalPlanOptimizer(Arrays.asList( |
43 | new MergeFilterAndFilter(dsl), | |
44 | new PushFilterUnderSort())); | |
45 | } | |
46 | ||
47 | /** | |
48 | * Optimize {@link LogicalPlan}. | |
49 | */ | |
50 | public LogicalPlan optimize(LogicalPlan plan) { | |
51 | LogicalPlan optimized = internalOptimize(plan); | |
52 | optimized.replaceChildPlans( | |
53 | optimized.getChild().stream().map(this::optimize).collect( | |
54 | Collectors.toList())); | |
55 |
1
1. optimize : replaced return value with null for org/opensearch/sql/planner/optimizer/LogicalPlanOptimizer::optimize → KILLED |
return internalOptimize(optimized); |
56 | } | |
57 | ||
58 | private LogicalPlan internalOptimize(LogicalPlan plan) { | |
59 | LogicalPlan node = plan; | |
60 | boolean done = false; | |
61 |
1
1. internalOptimize : negated conditional → KILLED |
while (!done) { |
62 | done = true; | |
63 | for (Rule rule : rules) { | |
64 | Match match = DEFAULT_MATCHER.match(rule.pattern(), node); | |
65 |
1
1. internalOptimize : negated conditional → KILLED |
if (match.isPresent()) { |
66 | node = rule.apply(match.value(), match.captures()); | |
67 | done = false; | |
68 | } | |
69 | } | |
70 | } | |
71 |
1
1. internalOptimize : replaced return value with null for org/opensearch/sql/planner/optimizer/LogicalPlanOptimizer::internalOptimize → KILLED |
return node; |
72 | } | |
73 | } | |
Mutations | ||
42 |
1.1 |
|
55 |
1.1 |
|
61 |
1.1 |
|
65 |
1.1 |
|
71 |
1.1 |