From e08bf4d74369e772f0fd94fbe56e427546072350 Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Fri, 28 Jul 2023 01:05:47 -0700 Subject: [PATCH 1/3] Resolving merge conflicts for pre tag in java docs. Signed-off-by: Mitchell Gale --- .../ExpressionReferenceOptimizer.java | 63 ++++++++------- .../analysis/SelectExpressionAnalyzer.java | 80 +++++++++---------- .../org/opensearch/sql/ast/dsl/AstDSL.java | 77 +++++++++--------- .../sql/data/type/WideningTypeRule.java | 17 ++-- 4 files changed, 116 insertions(+), 121 deletions(-) diff --git a/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java b/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java index eaf5c4abca..dd59a7b79e 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import java.util.HashMap; @@ -26,6 +25,7 @@ import org.opensearch.sql.planner.logical.LogicalWindow; /** + *
  * The optimizer used to replace the expression referred in the SelectClause
  * e.g. The query SELECT abs(name), sum(age)-avg(age) FROM test GROUP BY abs(name).
  * will be translated the AST
@@ -37,14 +37,15 @@
  * LogicalProject[Ref("abs(age)"), sub(Ref("sum(age)"), Ref("avg(age)"))
  *  LogicalAgg(agg=[sum(age), avg(age)], group=[abs(age)]]
  *   LogicalRelation
+ * 
*/ public class ExpressionReferenceOptimizer extends ExpressionNodeVisitor { private final BuiltinFunctionRepository repository; /** - * The map of expression and it's reference. - * For example, The NamedAggregator should produce the map of Aggregator to Ref(name) + * The map of expression and it's reference. For example, The NamedAggregator should produce the + * map of Aggregator to Ref(name) */ private final Map expressionMap = new HashMap<>(); @@ -69,17 +70,16 @@ public Expression visitFunction(FunctionExpression node, AnalysisContext context return expressionMap.get(node); } else { final List args = - node.getArguments().stream().map(expr -> expr.accept(this, context)) + node.getArguments().stream() + .map(expr -> expr.accept(this, context)) .collect(Collectors.toList()); - Expression optimizedFunctionExpression = (Expression) repository.compile( - context.getFunctionProperties(), - node.getFunctionName(), - args - ); + Expression optimizedFunctionExpression = + (Expression) + repository.compile(context.getFunctionProperties(), node.getFunctionName(), args); // Propagate scoreTracked for OpenSearch functions if (optimizedFunctionExpression instanceof OpenSearchFunctions.OpenSearchFunction) { - ((OpenSearchFunctions.OpenSearchFunction) optimizedFunctionExpression).setScoreTracked( - ((OpenSearchFunctions.OpenSearchFunction)node).isScoreTracked()); + ((OpenSearchFunctions.OpenSearchFunction) optimizedFunctionExpression) + .setScoreTracked(((OpenSearchFunctions.OpenSearchFunction) node).isScoreTracked()); } return optimizedFunctionExpression; } @@ -98,19 +98,17 @@ public Expression visitNamed(NamedExpression node, AnalysisContext context) { return node.getDelegated().accept(this, context); } - /** - * Implement this because Case/When is not registered in function repository. - */ + /** Implement this because Case/When is not registered in function repository. */ @Override public Expression visitCase(CaseClause node, AnalysisContext context) { if (expressionMap.containsKey(node)) { return expressionMap.get(node); } - List whenClauses = node.getWhenClauses() - .stream() - .map(expr -> (WhenClause) expr.accept(this, context)) - .collect(Collectors.toList()); + List whenClauses = + node.getWhenClauses().stream() + .map(expr -> (WhenClause) expr.accept(this, context)) + .collect(Collectors.toList()); Expression defaultResult = null; if (node.getDefaultResult() != null) { defaultResult = node.getDefaultResult().accept(this, context); @@ -121,14 +119,10 @@ public Expression visitCase(CaseClause node, AnalysisContext context) { @Override public Expression visitWhen(WhenClause node, AnalysisContext context) { return new WhenClause( - node.getCondition().accept(this, context), - node.getResult().accept(this, context)); + node.getCondition().accept(this, context), node.getResult().accept(this, context)); } - - /** - * Expression Map Builder. - */ + /** Expression Map Builder. */ class ExpressionMapBuilder extends LogicalPlanNodeVisitor { @Override @@ -140,20 +134,27 @@ public Void visitNode(LogicalPlan plan, Void context) { @Override public Void visitAggregation(LogicalAggregation plan, Void context) { // Create the mapping for all the aggregator. - plan.getAggregatorList().forEach(namedAggregator -> expressionMap - .put(namedAggregator.getDelegated(), - new ReferenceExpression(namedAggregator.getName(), namedAggregator.type()))); + plan.getAggregatorList() + .forEach( + namedAggregator -> + expressionMap.put( + namedAggregator.getDelegated(), + new ReferenceExpression(namedAggregator.getName(), namedAggregator.type()))); // Create the mapping for all the group by. - plan.getGroupByList().forEach(groupBy -> expressionMap - .put(groupBy.getDelegated(), - new ReferenceExpression(groupBy.getNameOrAlias(), groupBy.type()))); + plan.getGroupByList() + .forEach( + groupBy -> + expressionMap.put( + groupBy.getDelegated(), + new ReferenceExpression(groupBy.getNameOrAlias(), groupBy.type()))); return null; } @Override public Void visitWindow(LogicalWindow plan, Void context) { Expression windowFunc = plan.getWindowFunction(); - expressionMap.put(windowFunc, + expressionMap.put( + windowFunc, new ReferenceExpression(((NamedExpression) windowFunc).getName(), windowFunc.type())); return visitNode(plan, context); } diff --git a/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java index 734f37378b..e040c882c6 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import com.google.common.collect.ImmutableList; @@ -30,23 +29,21 @@ import org.opensearch.sql.expression.ReferenceExpression; /** - * Analyze the select list in the {@link AnalysisContext} to construct the list of - * {@link NamedExpression}. + * Analyze the select list in the {@link AnalysisContext} to construct the list of {@link + * NamedExpression}. */ @RequiredArgsConstructor public class SelectExpressionAnalyzer - extends - AbstractNodeVisitor, AnalysisContext> { + extends AbstractNodeVisitor, AnalysisContext> { private final ExpressionAnalyzer expressionAnalyzer; private ExpressionReferenceOptimizer optimizer; - /** - * Analyze Select fields. - */ - public List analyze(List selectList, - AnalysisContext analysisContext, - ExpressionReferenceOptimizer optimizer) { + /** Analyze Select fields. */ + public List analyze( + List selectList, + AnalysisContext analysisContext, + ExpressionReferenceOptimizer optimizer) { this.optimizer = optimizer; ImmutableList.Builder builder = new ImmutableList.Builder<>(); for (UnresolvedExpression unresolvedExpression : selectList) { @@ -68,13 +65,12 @@ public List visitAlias(Alias node, AnalysisContext context) { } Expression expr = referenceIfSymbolDefined(node, context); - return Collections.singletonList(DSL.named( - unqualifiedNameIfFieldOnly(node, context), - expr, - node.getAlias())); + return Collections.singletonList( + DSL.named(unqualifiedNameIfFieldOnly(node, context), expr, node.getAlias())); } /** + *
    * The Alias could be
    * 1. SELECT name, AVG(age) FROM s BY name ->
    * Project(Alias("name", expr), Alias("AVG(age)", aggExpr))
@@ -85,33 +81,34 @@ public List visitAlias(Alias node, AnalysisContext context) {
    * 3. SELECT length(name) as l, AVG(age) FROM s BY l
    * Project(Alias("name", expr, l), Alias("AVG(age)", aggExpr))
    * Agg(Alias("AVG(age)", aggExpr), Alias("length(name)", groupExpr))
+   * 
*/ - private Expression referenceIfSymbolDefined(Alias expr, - AnalysisContext context) { + private Expression referenceIfSymbolDefined(Alias expr, AnalysisContext context) { UnresolvedExpression delegatedExpr = expr.getDelegated(); // Pass named expression because expression like window function loses full name // (OVER clause) and thus depends on name in alias to be replaced correctly return optimizer.optimize( DSL.named( - expr.getName(), - delegatedExpr.accept(expressionAnalyzer, context), - expr.getAlias()), + expr.getName(), delegatedExpr.accept(expressionAnalyzer, context), expr.getAlias()), context); } @Override - public List visitAllFields(AllFields node, - AnalysisContext context) { + public List visitAllFields(AllFields node, AnalysisContext context) { TypeEnvironment environment = context.peek(); Map lookupAllFields = environment.lookupAllFields(Namespace.FIELD_NAME); - return lookupAllFields.entrySet().stream().map(entry -> DSL.named(entry.getKey(), - new ReferenceExpression(entry.getKey(), entry.getValue()))).collect(Collectors.toList()); + return lookupAllFields.entrySet().stream() + .map( + entry -> + DSL.named( + entry.getKey(), new ReferenceExpression(entry.getKey(), entry.getValue()))) + .collect(Collectors.toList()); } @Override - public List visitNestedAllTupleFields(NestedAllTupleFields node, - AnalysisContext context) { + public List visitNestedAllTupleFields( + NestedAllTupleFields node, AnalysisContext context) { TypeEnvironment environment = context.peek(); Map lookupAllTupleFields = environment.lookupAllTupleFields(Namespace.FIELD_NAME); @@ -121,25 +118,25 @@ public List visitNestedAllTupleFields(NestedAllTupleFields node Pattern p = Pattern.compile(node.getPath() + "\\.[^\\.]+$"); return lookupAllTupleFields.entrySet().stream() .filter(field -> p.matcher(field.getKey()).find()) - .map(entry -> { - Expression nestedFunc = new Function( - "nested", - List.of( - new QualifiedName(List.of(entry.getKey().split("\\.")))) - ).accept(expressionAnalyzer, context); - return DSL.named("nested(" + entry.getKey() + ")", nestedFunc); - }) + .map( + entry -> { + Expression nestedFunc = + new Function( + "nested", + List.of(new QualifiedName(List.of(entry.getKey().split("\\."))))) + .accept(expressionAnalyzer, context); + return DSL.named("nested(" + entry.getKey() + ")", nestedFunc); + }) .collect(Collectors.toList()); } /** - * Get unqualified name if select item is just a field. For example, suppose an index - * named "accounts", return "age" for "SELECT accounts.age". But do nothing for expression - * in "SELECT ABS(accounts.age)". - * Note that an assumption is made implicitly that original name field in Alias must be - * the same as the values in QualifiedName. This is true because AST builder does this. - * Otherwise, what unqualified() returns will override Alias's name as NamedExpression's name - * even though the QualifiedName doesn't have qualifier. + * Get unqualified name if select item is just a field. For example, suppose an index named + * "accounts", return "age" for "SELECT accounts.age". But do nothing for expression in "SELECT + * ABS(accounts.age)". Note that an assumption is made implicitly that original name field in + * Alias must be the same as the values in QualifiedName. This is true because AST builder does + * this. Otherwise, what unqualified() returns will override Alias's name as NamedExpression's + * name even though the QualifiedName doesn't have qualifier. */ private String unqualifiedNameIfFieldOnly(Alias node, AnalysisContext context) { UnresolvedExpression selectItem = node.getDelegated(); @@ -149,5 +146,4 @@ private String unqualifiedNameIfFieldOnly(Alias node, AnalysisContext context) { } return node.getName(); } - } diff --git a/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java b/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java index d5f10fcfd4..1d6b612887 100644 --- a/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java +++ b/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.dsl; import java.util.Arrays; @@ -63,9 +62,7 @@ import org.opensearch.sql.ast.tree.UnresolvedPlan; import org.opensearch.sql.ast.tree.Values; -/** - * Class of static methods to create specific node instances. - */ +/** Class of static methods to create specific node instances. */ @UtilityClass public class AstDSL { @@ -132,8 +129,9 @@ public static UnresolvedPlan rename(UnresolvedPlan input, Map... maps) { /** * Initialize Values node by rows of literals. - * @param values rows in which each row is a list of literal values - * @return Values node + * + * @param values rows in which each row is a list of literal values + * @return Values node */ @SafeVarargs public UnresolvedPlan values(List... values) { @@ -249,27 +247,24 @@ public static Function function(String funcName, UnresolvedExpression... funcArg } /** + *
    * CASE
    *     WHEN search_condition THEN result_expr
    *     [WHEN search_condition THEN result_expr] ...
    *     [ELSE result_expr]
    * END
+   * 
*/ - public UnresolvedExpression caseWhen(UnresolvedExpression elseClause, - When... whenClauses) { + public UnresolvedExpression caseWhen(UnresolvedExpression elseClause, When... whenClauses) { return caseWhen(null, elseClause, whenClauses); } /** - * CASE case_value_expr - * WHEN compare_expr THEN result_expr - * [WHEN compare_expr THEN result_expr] ... - * [ELSE result_expr] - * END + * CASE case_value_expr WHEN compare_expr THEN result_expr [WHEN compare_expr THEN result_expr] + * ... [ELSE result_expr] END */ - public UnresolvedExpression caseWhen(UnresolvedExpression caseValueExpr, - UnresolvedExpression elseClause, - When... whenClauses) { + public UnresolvedExpression caseWhen( + UnresolvedExpression caseValueExpr, UnresolvedExpression elseClause, When... whenClauses) { return new Case(caseValueExpr, Arrays.asList(whenClauses), elseClause); } @@ -281,19 +276,20 @@ public When when(UnresolvedExpression condition, UnresolvedExpression result) { return new When(condition, result); } - public UnresolvedExpression highlight(UnresolvedExpression fieldName, - java.util.Map arguments) { + public UnresolvedExpression highlight( + UnresolvedExpression fieldName, java.util.Map arguments) { return new HighlightFunction(fieldName, arguments); } - public UnresolvedExpression score(UnresolvedExpression relevanceQuery, - Literal relevanceFieldWeight) { + public UnresolvedExpression score( + UnresolvedExpression relevanceQuery, Literal relevanceFieldWeight) { return new ScoreFunction(relevanceQuery, relevanceFieldWeight); } - public UnresolvedExpression window(UnresolvedExpression function, - List partitionByList, - List> sortList) { + public UnresolvedExpression window( + UnresolvedExpression function, + List partitionByList, + List> sortList) { return new WindowFunction(function, partitionByList, sortList); } @@ -328,9 +324,10 @@ public static UnresolvedExpression compare( return new Compare(operator, left, right); } - public static UnresolvedExpression between(UnresolvedExpression value, - UnresolvedExpression lowerBound, - UnresolvedExpression upperBound) { + public static UnresolvedExpression between( + UnresolvedExpression value, + UnresolvedExpression lowerBound, + UnresolvedExpression upperBound) { return new Between(value, lowerBound, upperBound); } @@ -398,9 +395,7 @@ public static List defaultFieldsArgs() { return exprList(argument("exclude", booleanLiteral(false))); } - /** - * Default Stats Command Args. - */ + /** Default Stats Command Args. */ public static List defaultStatsArgs() { return exprList( argument("partitions", intLiteral(1)), @@ -409,9 +404,7 @@ public static List defaultStatsArgs() { argument("dedupsplit", booleanLiteral(false))); } - /** - * Default Dedup Command Args. - */ + /** Default Dedup Command Args. */ public static List defaultDedupArgs() { return exprList( argument("number", intLiteral(1)), @@ -447,9 +440,12 @@ public static List defaultTopArgs() { return exprList(argument("noOfResults", intLiteral(10))); } - public static RareTopN rareTopN(UnresolvedPlan input, CommandType commandType, - List noOfResults, List groupList, - Field... fields) { + public static RareTopN rareTopN( + UnresolvedPlan input, + CommandType commandType, + List noOfResults, + List groupList, + Field... fields) { return new RareTopN(input, commandType, noOfResults, Arrays.asList(fields), groupList) .attach(input); } @@ -458,11 +454,12 @@ public static Limit limit(UnresolvedPlan input, Integer limit, Integer offset) { return new Limit(limit, offset).attach(input); } - public static Parse parse(UnresolvedPlan input, ParseMethod parseMethod, - UnresolvedExpression sourceField, - Literal pattern, - java.util.Map arguments) { + public static Parse parse( + UnresolvedPlan input, + ParseMethod parseMethod, + UnresolvedExpression sourceField, + Literal pattern, + java.util.Map arguments) { return new Parse(parseMethod, sourceField, pattern, arguments, input); } - } diff --git a/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java b/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java index e1f356782f..7f8936e00c 100644 --- a/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java +++ b/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.type; import static org.opensearch.sql.data.type.ExprCoreType.UNKNOWN; @@ -12,6 +11,7 @@ import org.opensearch.sql.exception.ExpressionEvaluationException; /** + *
  * The definition of widening type rule for expression value.
  * ExprType     Widens to data types
  * INTEGER      LONG, FLOAT, DOUBLE
@@ -22,6 +22,7 @@
  * BOOLEAN      BOOLEAN
  * ARRAY        ARRAY
  * STRUCT       STRUCT
+ * 
*/ @UtilityClass public class WideningTypeRule { @@ -29,8 +30,8 @@ public class WideningTypeRule { public static final int TYPE_EQUAL = 0; /** - * The widening distance is calculated from the leaf to root. - * e.g. distance(INTEGER, FLOAT) = 2, but distance(FLOAT, INTEGER) = IMPOSSIBLE_WIDENING + * The widening distance is calculated from the leaf to root. e.g. distance(INTEGER, FLOAT) = 2, + * but distance(FLOAT, INTEGER) = IMPOSSIBLE_WIDENING * * @param type1 widen from type * @param type2 widen to type @@ -48,15 +49,15 @@ private static int distance(ExprType type1, ExprType type2, int distance) { } else { return type1.getParent().stream() .map(parentOfType1 -> distance(parentOfType1, type2, distance + 1)) - .reduce(Math::min).get(); + .reduce(Math::min) + .get(); } } /** - * The max type among two types. The max is defined as follow - * if type1 could widen to type2, then max is type2, vice versa - * if type1 could't widen to type2 and type2 could't widen to type1, - * then throw {@link ExpressionEvaluationException}. + * The max type among two types. The max is defined as follow if type1 could widen to type2, then + * max is type2, vice versa if type1 could't widen to type2 and type2 could't widen to type1, then + * throw {@link ExpressionEvaluationException}. * * @param type1 type1 * @param type2 type2 From 2558f0639ee9419f936764accdddd08d8485e7cd Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Fri, 28 Jul 2023 01:21:19 -0700 Subject: [PATCH 2/3] running spotless check on newly pre tagged javadocs. Signed-off-by: Mitchell Gale --- .../opensearch/sql/analysis/ExpressionReferenceOptimizer.java | 2 ++ .../org/opensearch/sql/analysis/SelectExpressionAnalyzer.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java b/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java index dd59a7b79e..cb4592a4af 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java @@ -25,6 +25,8 @@ import org.opensearch.sql.planner.logical.LogicalWindow; /** + * + * *
  * The optimizer used to replace the expression referred in the SelectClause
  * e.g. The query SELECT abs(name), sum(age)-avg(age) FROM test GROUP BY abs(name).
diff --git a/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java
index e040c882c6..2f55490a24 100644
--- a/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java
+++ b/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java
@@ -70,6 +70,8 @@ public List visitAlias(Alias node, AnalysisContext context) {
   }
 
   /**
+   *
+   *
    * 
    * The Alias could be
    * 1. SELECT name, AVG(age) FROM s BY name ->

From ae45ff161fc409117f0cb72db3bc6e756d38dc1a Mon Sep 17 00:00:00 2001
From: Mitchell Gale 
Date: Fri, 28 Jul 2023 01:28:59 -0700
Subject: [PATCH 3/3] Converts java doc table to proper java doc table.

Signed-off-by: Mitchell Gale 
---
 .../sql/data/type/WideningTypeRule.java       | 22 +++++++++----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java b/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java
index 7f8936e00c..5587570ad9 100644
--- a/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java
+++ b/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java
@@ -11,18 +11,18 @@
 import org.opensearch.sql.exception.ExpressionEvaluationException;
 
 /**
- * 
  * The definition of widening type rule for expression value.
- * ExprType     Widens to data types
- * INTEGER      LONG, FLOAT, DOUBLE
- * LONG         FLOAT, DOUBLE
- * FLOAT        DOUBLE
- * DOUBLE       DOUBLE
- * STRING       STRING
- * BOOLEAN      BOOLEAN
- * ARRAY        ARRAY
- * STRUCT       STRUCT
- * 
+ * + * + * + * + * + * + * + * + * + * + *
ExprTypeWidens to data types
INTEGERLONG, FLOAT, DOUBLE
LONGFLOAT, DOUBLE
FLOATDOUBLE
DOUBLEDOUBLE
STRINGSTRING
BOOLEANBOOLEAN
ARRAYARRAY
STRUCTSTRUCT
*/ @UtilityClass public class WideningTypeRule {