33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6-
76package org .opensearch .sql .analysis ;
87
98import java .util .HashMap ;
2625import org .opensearch .sql .planner .logical .LogicalWindow ;
2726
2827/**
29- * The optimizer used to replace the expression referred in the SelectClause</br>
30- * e.g. The query SELECT abs(name), sum(age)-avg(age) FROM test GROUP BY abs(name).<br>
28+ * The optimizer used to replace the expression referred in the SelectClause</br> e.g. The query
29+ * SELECT abs(name), sum(age)-avg(age) FROM test GROUP BY abs(name).<br>
3130 * will be translated the AST<br>
3231 * Project[abs(age), sub(sum(age), avg(age))<br>
3332 * &ensp Agg(agg=[sum(age), avg(age)], group=[abs(age)]]<br>
@@ -43,8 +42,8 @@ public class ExpressionReferenceOptimizer
4342 private final BuiltinFunctionRepository repository ;
4443
4544 /**
46- * The map of expression and it's reference.
47- * For example, The NamedAggregator should produce the map of Aggregator to Ref(name)
45+ * The map of expression and it's reference. For example, The NamedAggregator should produce the
46+ * map of Aggregator to Ref(name)
4847 */
4948 private final Map <Expression , Expression > expressionMap = new HashMap <>();
5049
@@ -69,17 +68,16 @@ public Expression visitFunction(FunctionExpression node, AnalysisContext context
6968 return expressionMap .get (node );
7069 } else {
7170 final List <Expression > args =
72- node .getArguments ().stream ().map (expr -> expr .accept (this , context ))
71+ node .getArguments ().stream ()
72+ .map (expr -> expr .accept (this , context ))
7373 .collect (Collectors .toList ());
74- Expression optimizedFunctionExpression = (Expression ) repository .compile (
75- context .getFunctionProperties (),
76- node .getFunctionName (),
77- args
78- );
74+ Expression optimizedFunctionExpression =
75+ (Expression )
76+ repository .compile (context .getFunctionProperties (), node .getFunctionName (), args );
7977 // Propagate scoreTracked for OpenSearch functions
8078 if (optimizedFunctionExpression instanceof OpenSearchFunctions .OpenSearchFunction ) {
81- ((OpenSearchFunctions .OpenSearchFunction ) optimizedFunctionExpression ). setScoreTracked (
82- (( OpenSearchFunctions .OpenSearchFunction )node ).isScoreTracked ());
79+ ((OpenSearchFunctions .OpenSearchFunction ) optimizedFunctionExpression )
80+ . setScoreTracked ((( OpenSearchFunctions .OpenSearchFunction ) node ).isScoreTracked ());
8381 }
8482 return optimizedFunctionExpression ;
8583 }
@@ -98,19 +96,17 @@ public Expression visitNamed(NamedExpression node, AnalysisContext context) {
9896 return node .getDelegated ().accept (this , context );
9997 }
10098
101- /**
102- * Implement this because Case/When is not registered in function repository.
103- */
99+ /** Implement this because Case/When is not registered in function repository. */
104100 @ Override
105101 public Expression visitCase (CaseClause node , AnalysisContext context ) {
106102 if (expressionMap .containsKey (node )) {
107103 return expressionMap .get (node );
108104 }
109105
110- List <WhenClause > whenClauses = node . getWhenClauses ()
111- .stream ()
112- .map (expr -> (WhenClause ) expr .accept (this , context ))
113- .collect (Collectors .toList ());
106+ List <WhenClause > whenClauses =
107+ node . getWhenClauses () .stream ()
108+ .map (expr -> (WhenClause ) expr .accept (this , context ))
109+ .collect (Collectors .toList ());
114110 Expression defaultResult = null ;
115111 if (node .getDefaultResult () != null ) {
116112 defaultResult = node .getDefaultResult ().accept (this , context );
@@ -121,14 +117,10 @@ public Expression visitCase(CaseClause node, AnalysisContext context) {
121117 @ Override
122118 public Expression visitWhen (WhenClause node , AnalysisContext context ) {
123119 return new WhenClause (
124- node .getCondition ().accept (this , context ),
125- node .getResult ().accept (this , context ));
120+ node .getCondition ().accept (this , context ), node .getResult ().accept (this , context ));
126121 }
127122
128-
129- /**
130- * Expression Map Builder.
131- */
123+ /** Expression Map Builder. */
132124 class ExpressionMapBuilder extends LogicalPlanNodeVisitor <Void , Void > {
133125
134126 @ Override
@@ -140,20 +132,27 @@ public Void visitNode(LogicalPlan plan, Void context) {
140132 @ Override
141133 public Void visitAggregation (LogicalAggregation plan , Void context ) {
142134 // Create the mapping for all the aggregator.
143- plan .getAggregatorList ().forEach (namedAggregator -> expressionMap
144- .put (namedAggregator .getDelegated (),
145- new ReferenceExpression (namedAggregator .getName (), namedAggregator .type ())));
135+ plan .getAggregatorList ()
136+ .forEach (
137+ namedAggregator ->
138+ expressionMap .put (
139+ namedAggregator .getDelegated (),
140+ new ReferenceExpression (namedAggregator .getName (), namedAggregator .type ())));
146141 // Create the mapping for all the group by.
147- plan .getGroupByList ().forEach (groupBy -> expressionMap
148- .put (groupBy .getDelegated (),
149- new ReferenceExpression (groupBy .getNameOrAlias (), groupBy .type ())));
142+ plan .getGroupByList ()
143+ .forEach (
144+ groupBy ->
145+ expressionMap .put (
146+ groupBy .getDelegated (),
147+ new ReferenceExpression (groupBy .getNameOrAlias (), groupBy .type ())));
150148 return null ;
151149 }
152150
153151 @ Override
154152 public Void visitWindow (LogicalWindow plan , Void context ) {
155153 Expression windowFunc = plan .getWindowFunction ();
156- expressionMap .put (windowFunc ,
154+ expressionMap .put (
155+ windowFunc ,
157156 new ReferenceExpression (((NamedExpression ) windowFunc ).getName (), windowFunc .type ()));
158157 return visitNode (plan , context );
159158 }
0 commit comments