From b692a88a96b7d1bf7abfa543721def84398d8876 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 6 Nov 2024 16:36:31 -0800 Subject: [PATCH] Refactor code Signed-off-by: Andy Kwok --- .../sql/ppl/utils/TrendlineCatalystUtils.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/ppl-spark-integration/src/main/java/org/opensearch/sql/ppl/utils/TrendlineCatalystUtils.java b/ppl-spark-integration/src/main/java/org/opensearch/sql/ppl/utils/TrendlineCatalystUtils.java index 05ac40988..307041bd4 100644 --- a/ppl-spark-integration/src/main/java/org/opensearch/sql/ppl/utils/TrendlineCatalystUtils.java +++ b/ppl-spark-integration/src/main/java/org/opensearch/sql/ppl/utils/TrendlineCatalystUtils.java @@ -111,35 +111,34 @@ private static NamedExpression getWMAComputationExpression(CatalystExpressionVis Trendline.TrendlineComputation node, Field sortField, CatalystPlanContext context) { - + int dataPoints = node.getNumberOfDataPoints(); //window lower boundary Expression windowLowerBoundary = getIntExpression(visitor, context, - Math.negateExact(node.getNumberOfDataPoints() - 1)); + Math.negateExact(dataPoints - 1)); //window definition visitor.analyze(sortField, context); Expression sortDefinition = context.popNamedParseExpressions().get(); - WindowSpecDefinition windowDefinition = getCommonWindowDefinition( + WindowSpecDefinition windowDefinition = getWmaCommonWindowDefinition( sortDefinition, SortUtils.isSortedAscending(sortField), windowLowerBoundary); // Divisor - Expression divider = getIntExpression(visitor, context, - (node.getNumberOfDataPoints() * (node.getNumberOfDataPoints()+1) / 2)); + Expression divisor = getIntExpression(visitor, context, + (dataPoints * (dataPoints + 1) / 2)); // Aggregation - Expression WMAExpression = getNthValueAggregations(visitor, node, context, windowDefinition, - node.getNumberOfDataPoints()) + Expression WMAExpression = getNthValueAggregations(visitor, node, context, windowDefinition, dataPoints) .stream() .reduce(Add::new) .orElse(null); - return getAlias(node.getAlias(), new Divide(WMAExpression, divider)); + return getAlias(node.getAlias(), new Divide(WMAExpression, divisor)); } /** * Helper method to produce an Alias Expression with provide value and name. * @param name The name for the Alias. * @param expression The expression which will be evaluated. - * @return A Alias instance with logical plan representation of `expression AS name`. + * @return An Alias instance with logical plan representation of `expression AS name`. */ private static NamedExpression getAlias(String name, Expression expression) { return org.apache.spark.sql.catalyst.expressions.Alias$.MODULE$.apply(expression, @@ -151,7 +150,7 @@ private static NamedExpression getAlias(String name, Expression expression) { } /** - * Helper method to retrieve an Int in expression form for logical plan composition purpose. + * Helper method to retrieve an Int expression instance for logical plan composition purpose. * @param expressionVisitor Visitor instance to process the incoming object. * @param context Context instance to retrieve the Expression instance. * @param i Target value for the expression. @@ -167,12 +166,13 @@ static Expression getIntExpression(CatalystExpressionVisitor expressionVisitor, /** * Helper method to retrieve a WindowSpecDefinition with provided sorting condition. * `windowspecdefinition('sortField ascending NULLS FIRST, specifiedwindowframe(RowFrame, windowLowerBoundary, currentrow$())` + * * @param sortField The field being used for the sorting operation. * @param ascending The boolean instance for the sorting order. * @param windowLowerBoundary The Integer expression instance which specify the even lookbehind / lookahead. * @return A WindowSpecDefinition instance which will be used to composite the WMA calculation. */ - static WindowSpecDefinition getCommonWindowDefinition(Expression sortField, boolean ascending, Expression windowLowerBoundary) { + static WindowSpecDefinition getWmaCommonWindowDefinition(Expression sortField, boolean ascending, Expression windowLowerBoundary) { return new WindowSpecDefinition( seq(), seq(SortUtils.sortOrder(sortField, ascending)), @@ -180,7 +180,7 @@ static WindowSpecDefinition getCommonWindowDefinition(Expression sortField, bool } /** - * To produce a list of Expression with responsible to return appropriate lookbehind / lookahead value for WMA calculation, sample logical plan listed below. + * To produce a list of Expressions responsible to return appropriate lookbehind / lookahead value for WMA calculation, sample logical plan listed below. * (((('nth_value('salary, 1) windowspecdefinition(Field(field=age, fieldArgs=[]) ASC NULLS FIRST, specifiedwindowframe(RowFrame, -2, currentrow$())) * 1) + * * @param visitor Visitor instance to resolve Expression. @@ -195,7 +195,6 @@ private static List getNthValueAggregations(CatalystExpressionVisito CatalystPlanContext context, WindowSpecDefinition windowDefinition, int dataPoints) { - List expressions = new ArrayList<>(); for (int i = 1; i <= dataPoints; i++) { // Get the offset parameter