From e6a35a5f2a6e5d1a4dacad7f1d1e087ed4f4f97b Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Fri, 11 Aug 2023 11:42:14 -0700 Subject: [PATCH] Spotless apply on PPL Signed-off-by: Mitchell Gale --- build.gradle | 3 +- ppl/build.gradle | 5 + .../org/opensearch/sql/ppl/PPLService.java | 16 +- .../sql/ppl/antlr/PPLSyntaxParser.java | 15 +- .../sql/ppl/domain/PPLQueryRequest.java | 24 +- .../sql/ppl/domain/PPLQueryResponse.java | 4 +- .../opensearch/sql/ppl/parser/AstBuilder.java | 264 +++---- .../sql/ppl/parser/AstExpressionBuilder.java | 203 +++--- .../sql/ppl/parser/AstStatementBuilder.java | 4 +- .../sql/ppl/utils/ArgumentFactory.java | 40 +- .../sql/ppl/utils/PPLQueryDataAnonymizer.java | 91 +-- .../sql/ppl/utils/UnresolvedPlanHelper.java | 9 +- .../opensearch/sql/ppl/PPLServiceTest.java | 122 ++-- .../ppl/antlr/NowLikeFunctionParserTest.java | 32 +- ...ntaxParserMatchBoolPrefixSamplesTests.java | 15 +- ...PPLSyntaxParserMatchPhraseSamplesTest.java | 16 +- .../sql/ppl/antlr/PPLSyntaxParserTest.java | 310 +++++--- .../sql/ppl/domain/PPLQueryRequestTest.java | 20 +- .../sql/ppl/domain/PPLQueryResponseTest.java | 1 - .../sql/ppl/parser/AstBuilderTest.java | 607 +++++++--------- .../ppl/parser/AstExpressionBuilderTest.java | 662 ++++++------------ .../ppl/parser/AstNowLikeFunctionTest.java | 55 +- .../ppl/parser/AstStatementBuilderTest.java | 15 +- .../sql/ppl/utils/ArgumentFactoryTest.java | 46 +- .../ppl/utils/PPLQueryDataAnonymizerTest.java | 99 +-- .../ppl/utils/UnresolvedPlanHelperTest.java | 1 - 26 files changed, 1114 insertions(+), 1565 deletions(-) diff --git a/build.gradle b/build.gradle index 166ced81ba..c0f82c502b 100644 --- a/build.gradle +++ b/build.gradle @@ -86,7 +86,8 @@ spotless { target fileTree('.') { include 'common/**/*.java', 'datasources/**/*.java', - 'core/**/*.java' + 'core/**/*.java', + 'ppl/**/*.java' exclude '**/build/**', '**/build-*/**' } importOrder() diff --git a/ppl/build.gradle b/ppl/build.gradle index 484934ddc3..e16b6decfc 100644 --- a/ppl/build.gradle +++ b/ppl/build.gradle @@ -29,6 +29,11 @@ plugins { id 'antlr' } +// Being ignored as a temporary measure before being removed in favour of +// spotless https://github.com/opensearch-project/sql/issues/1101 +checkstyleTest.ignoreFailures = true +checkstyleMain.ignoreFailures = true + generateGrammarSource { arguments += ['-visitor', '-package', 'org.opensearch.sql.ppl.antlr.parser'] source = sourceSets.main.antlr diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/PPLService.java b/ppl/src/main/java/org/opensearch/sql/ppl/PPLService.java index 40a7a85f78..7769f5dfae 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/PPLService.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/PPLService.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.executor.ExecutionEngine.QueryResponse; @@ -27,9 +26,7 @@ import org.opensearch.sql.ppl.parser.AstStatementBuilder; import org.opensearch.sql.ppl.utils.PPLQueryDataAnonymizer; -/** - * PPLService. - */ +/** PPLService. */ @RequiredArgsConstructor public class PPLService { private final PPLSyntaxParser parser; @@ -45,7 +42,7 @@ public class PPLService { /** * Execute the {@link PPLQueryRequest}, using {@link ResponseListener} to get response. * - * @param request {@link PPLQueryRequest} + * @param request {@link PPLQueryRequest} * @param listener {@link ResponseListener} */ public void execute(PPLQueryRequest request, ResponseListener listener) { @@ -57,10 +54,10 @@ public void execute(PPLQueryRequest request, ResponseListener lis } /** - * Explain the query in {@link PPLQueryRequest} using {@link ResponseListener} to - * get and format explain response. + * Explain the query in {@link PPLQueryRequest} using {@link ResponseListener} to get and format + * explain response. * - * @param request {@link PPLQueryRequest} + * @param request {@link PPLQueryRequest} * @param listener {@link ResponseListener} for explain response */ public void explain(PPLQueryRequest request, ResponseListener listener) { @@ -90,7 +87,6 @@ private AbstractPlan plan( QueryContext.getRequestId(), anonymizer.anonymizeStatement(statement)); - return queryExecutionFactory.create( - statement, queryListener, explainListener); + return queryExecutionFactory.create(statement, queryListener, explainListener); } } diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParser.java b/ppl/src/main/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParser.java index 168ba33a8a..1d4485e749 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParser.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParser.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.antlr; import org.antlr.v4.runtime.CommonTokenStream; @@ -15,13 +14,9 @@ import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLLexer; import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser; -/** - * PPL Syntax Parser. - */ +/** PPL Syntax Parser. */ public class PPLSyntaxParser implements Parser { - /** - * Analyze the query syntax. - */ + /** Analyze the query syntax. */ @Override public ParseTree parse(String query) { OpenSearchPPLParser parser = createParser(createLexer(query)); @@ -30,12 +25,10 @@ public ParseTree parse(String query) { } private OpenSearchPPLParser createParser(Lexer lexer) { - return new OpenSearchPPLParser( - new CommonTokenStream(lexer)); + return new OpenSearchPPLParser(new CommonTokenStream(lexer)); } private OpenSearchPPLLexer createLexer(String query) { - return new OpenSearchPPLLexer( - new CaseInsensitiveCharStream(query)); + return new OpenSearchPPLLexer(new CaseInsensitiveCharStream(query)); } } diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/domain/PPLQueryRequest.java b/ppl/src/main/java/org/opensearch/sql/ppl/domain/PPLQueryRequest.java index 87532e01d0..ca351fcc0a 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/domain/PPLQueryRequest.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/domain/PPLQueryRequest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.domain; import java.util.Locale; @@ -22,12 +21,9 @@ public class PPLQueryRequest { public static final PPLQueryRequest NULL = new PPLQueryRequest("", null, DEFAULT_PPL_PATH, ""); private final String pplQuery; - @Getter - private final JSONObject jsonContent; - @Getter - private final String path; - @Getter - private String format = ""; + @Getter private final JSONObject jsonContent; + @Getter private final String path; + @Getter private String format = ""; @Setter @Getter @@ -43,9 +39,7 @@ public PPLQueryRequest(String pplQuery, JSONObject jsonContent, String path) { this(pplQuery, jsonContent, path, ""); } - /** - * Constructor of PPLQueryRequest. - */ + /** Constructor of PPLQueryRequest. */ public PPLQueryRequest(String pplQuery, JSONObject jsonContent, String path, String format) { this.pplQuery = pplQuery; this.jsonContent = jsonContent; @@ -59,23 +53,21 @@ public String getRequest() { /** * Check if request is to explain rather than execute the query. - * @return true if it is a explain request + * + * @return true if it is a explain request */ public boolean isExplainRequest() { return path.endsWith("/_explain"); } - /** - * Decide on the formatter by the requested format. - */ + /** Decide on the formatter by the requested format. */ public Format format() { Optional optionalFormat = Format.of(format); if (optionalFormat.isPresent()) { return optionalFormat.get(); } else { throw new IllegalArgumentException( - String.format(Locale.ROOT,"response in %s format is not supported.", format)); + String.format(Locale.ROOT, "response in %s format is not supported.", format)); } } - } diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/domain/PPLQueryResponse.java b/ppl/src/main/java/org/opensearch/sql/ppl/domain/PPLQueryResponse.java index 483726702a..5cae8e8f06 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/domain/PPLQueryResponse.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/domain/PPLQueryResponse.java @@ -3,8 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.domain; -public class PPLQueryResponse { -} +public class PPLQueryResponse {} diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java index 323f99a5af..3c693fa0bd 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.parser; import static org.opensearch.sql.ast.dsl.AstDSL.qualifiedName; @@ -74,33 +73,25 @@ import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParserBaseVisitor; import org.opensearch.sql.ppl.utils.ArgumentFactory; -/** - * Class of building the AST. - * Refines the visit path and build the AST nodes - */ +/** Class of building the AST. Refines the visit path and build the AST nodes */ @RequiredArgsConstructor public class AstBuilder extends OpenSearchPPLParserBaseVisitor { private final AstExpressionBuilder expressionBuilder; /** - * PPL query to get original token text. This is necessary because token.getText() returns - * text without whitespaces or other characters discarded by lexer. + * PPL query to get original token text. This is necessary because token.getText() returns text + * without whitespaces or other characters discarded by lexer. */ private final String query; @Override public UnresolvedPlan visitQueryStatement(OpenSearchPPLParser.QueryStatementContext ctx) { UnresolvedPlan pplCommand = visit(ctx.pplCommands()); - return ctx.commands() - .stream() - .map(this::visit) - .reduce(pplCommand, (r, e) -> e.attach(r)); + return ctx.commands().stream().map(this::visit).reduce(pplCommand, (r, e) -> e.attach(r)); } - /** - * Search command. - */ + /** Search command. */ @Override public UnresolvedPlan visitSearchFrom(SearchFromContext ctx) { return visitFromClause(ctx.fromClause()); @@ -108,23 +99,22 @@ public UnresolvedPlan visitSearchFrom(SearchFromContext ctx) { @Override public UnresolvedPlan visitSearchFromFilter(SearchFromFilterContext ctx) { - return new Filter(internalVisitExpression(ctx.logicalExpression())).attach( - visit(ctx.fromClause())); + return new Filter(internalVisitExpression(ctx.logicalExpression())) + .attach(visit(ctx.fromClause())); } @Override public UnresolvedPlan visitSearchFilterFrom(SearchFilterFromContext ctx) { - return new Filter(internalVisitExpression(ctx.logicalExpression())).attach( - visit(ctx.fromClause())); + return new Filter(internalVisitExpression(ctx.logicalExpression())) + .attach(visit(ctx.fromClause())); } /** - * Describe command. - * Current logic separates table and metadata info about table by adding - * MAPPING_ODFE_SYS_TABLE as suffix. - * Even with the introduction of datasource and schema name in fully qualified table name, - * we do the same thing by appending MAPPING_ODFE_SYS_TABLE as syffix to the last part - * of qualified name. + * Describe command.
+ * Current logic separates table and metadata info about table by adding MAPPING_ODFE_SYS_TABLE as + * suffix. Even with the introduction of datasource and schema name in fully qualified table name, + * we do the same thing by appending MAPPING_ODFE_SYS_TABLE as syffix to the last part of + * qualified name. */ @Override public UnresolvedPlan visitDescribeCommand(DescribeCommandContext ctx) { @@ -135,63 +125,52 @@ public UnresolvedPlan visitDescribeCommand(DescribeCommandContext ctx) { return new Relation(new QualifiedName(parts)); } - /** - * Show command. - */ + /** Show command. */ @Override public UnresolvedPlan visitShowDataSourcesCommand( OpenSearchPPLParser.ShowDataSourcesCommandContext ctx) { return new Relation(qualifiedName(DATASOURCES_TABLE_NAME)); } - - /** - * Where command. - */ + /** Where command. */ @Override public UnresolvedPlan visitWhereCommand(WhereCommandContext ctx) { return new Filter(internalVisitExpression(ctx.logicalExpression())); } - /** - * Fields command. - */ + /** Fields command. */ @Override public UnresolvedPlan visitFieldsCommand(FieldsCommandContext ctx) { return new Project( - ctx.fieldList() - .fieldExpression() - .stream() + ctx.fieldList().fieldExpression().stream() .map(this::internalVisitExpression) .collect(Collectors.toList()), - ArgumentFactory.getArgumentList(ctx) - ); + ArgumentFactory.getArgumentList(ctx)); } - /** - * Rename command. - */ + /** Rename command. */ @Override public UnresolvedPlan visitRenameCommand(RenameCommandContext ctx) { return new Rename( - ctx.renameClasue() - .stream() - .map(ct -> new Map(internalVisitExpression(ct.orignalField), - internalVisitExpression(ct.renamedField))) - .collect(Collectors.toList()) - ); + ctx.renameClasue().stream() + .map( + ct -> + new Map( + internalVisitExpression(ct.orignalField), + internalVisitExpression(ct.renamedField))) + .collect(Collectors.toList())); } - /** - * Stats command. - */ + /** Stats command. */ @Override public UnresolvedPlan visitStatsCommand(StatsCommandContext ctx) { ImmutableList.Builder aggListBuilder = new ImmutableList.Builder<>(); for (OpenSearchPPLParser.StatsAggTermContext aggCtx : ctx.statsAggTerm()) { UnresolvedExpression aggExpression = internalVisitExpression(aggCtx.statsFunction()); - String name = aggCtx.alias == null ? getTextInQuery(aggCtx) : StringUtils - .unquoteIdentifier(aggCtx.alias.getText()); + String name = + aggCtx.alias == null + ? getTextInQuery(aggCtx) + : StringUtils.unquoteIdentifier(aggCtx.alias.getText()); Alias alias = new Alias(name, aggExpression); aggListBuilder.add(alias); } @@ -199,12 +178,16 @@ public UnresolvedPlan visitStatsCommand(StatsCommandContext ctx) { List groupList = Optional.ofNullable(ctx.statsByClause()) .map(OpenSearchPPLParser.StatsByClauseContext::fieldList) - .map(expr -> expr.fieldExpression().stream() - .map(groupCtx -> - (UnresolvedExpression) new Alias( - StringUtils.unquoteIdentifier(getTextInQuery(groupCtx)), - internalVisitExpression(groupCtx))) - .collect(Collectors.toList())) + .map( + expr -> + expr.fieldExpression().stream() + .map( + groupCtx -> + (UnresolvedExpression) + new Alias( + StringUtils.unquoteIdentifier(getTextInQuery(groupCtx)), + internalVisitExpression(groupCtx))) + .collect(Collectors.toList())) .orElse(Collections.emptyList()); UnresolvedExpression span = @@ -213,30 +196,23 @@ public UnresolvedPlan visitStatsCommand(StatsCommandContext ctx) { .map(this::internalVisitExpression) .orElse(null); - Aggregation aggregation = new Aggregation( - aggListBuilder.build(), - Collections.emptyList(), - groupList, - span, - ArgumentFactory.getArgumentList(ctx) - ); + Aggregation aggregation = + new Aggregation( + aggListBuilder.build(), + Collections.emptyList(), + groupList, + span, + ArgumentFactory.getArgumentList(ctx)); return aggregation; } - /** - * Dedup command. - */ + /** Dedup command. */ @Override public UnresolvedPlan visitDedupCommand(DedupCommandContext ctx) { - return new Dedupe( - ArgumentFactory.getArgumentList(ctx), - getFieldList(ctx.fieldList()) - ); + return new Dedupe(ArgumentFactory.getArgumentList(ctx), getFieldList(ctx.fieldList())); } - /** - * Head command visitor. - */ + /** Head command visitor. */ @Override public UnresolvedPlan visitHeadCommand(HeadCommandContext ctx) { Integer size = ctx.number != null ? Integer.parseInt(ctx.number.getText()) : 10; @@ -244,58 +220,46 @@ public UnresolvedPlan visitHeadCommand(HeadCommandContext ctx) { return new Head(size, from); } - /** - * Sort command. - */ + /** Sort command. */ @Override public UnresolvedPlan visitSortCommand(SortCommandContext ctx) { return new Sort( - ctx.sortbyClause() - .sortField() - .stream() + ctx.sortbyClause().sortField().stream() .map(sort -> (Field) internalVisitExpression(sort)) - .collect(Collectors.toList()) - ); + .collect(Collectors.toList())); } - /** - * Eval command. - */ + /** Eval command. */ @Override public UnresolvedPlan visitEvalCommand(EvalCommandContext ctx) { return new Eval( - ctx.evalClause() - .stream() + ctx.evalClause().stream() .map(ct -> (Let) internalVisitExpression(ct)) - .collect(Collectors.toList()) - ); + .collect(Collectors.toList())); } private List getGroupByList(ByClauseContext ctx) { - return ctx.fieldList().fieldExpression().stream().map(this::internalVisitExpression) + return ctx.fieldList().fieldExpression().stream() + .map(this::internalVisitExpression) .collect(Collectors.toList()); } private List getFieldList(FieldListContext ctx) { - return ctx.fieldExpression() - .stream() + return ctx.fieldExpression().stream() .map(field -> (Field) internalVisitExpression(field)) .collect(Collectors.toList()); } - /** - * Rare command. - */ + /** Rare command. */ @Override public UnresolvedPlan visitRareCommand(RareCommandContext ctx) { - List groupList = ctx.byClause() == null ? Collections.emptyList() : - getGroupByList(ctx.byClause()); + List groupList = + ctx.byClause() == null ? Collections.emptyList() : getGroupByList(ctx.byClause()); return new RareTopN( CommandType.RARE, ArgumentFactory.getArgumentList(ctx), getFieldList(ctx.fieldList()), - groupList - ); + groupList); } @Override @@ -319,34 +283,31 @@ public UnresolvedPlan visitPatternsCommand(OpenSearchPPLParser.PatternsCommandCo UnresolvedExpression sourceField = internalVisitExpression(ctx.source_field); ImmutableMap.Builder builder = ImmutableMap.builder(); ctx.patternsParameter() - .forEach(x -> { - builder.put(x.children.get(0).toString(), - (Literal) internalVisitExpression(x.children.get(2))); - }); + .forEach( + x -> { + builder.put( + x.children.get(0).toString(), + (Literal) internalVisitExpression(x.children.get(2))); + }); java.util.Map arguments = builder.build(); Literal pattern = arguments.getOrDefault("pattern", AstDSL.stringLiteral("")); return new Parse(ParseMethod.PATTERNS, sourceField, pattern, arguments); } - /** - * Top command. - */ + /** Top command. */ @Override public UnresolvedPlan visitTopCommand(TopCommandContext ctx) { - List groupList = ctx.byClause() == null ? Collections.emptyList() : - getGroupByList(ctx.byClause()); + List groupList = + ctx.byClause() == null ? Collections.emptyList() : getGroupByList(ctx.byClause()); return new RareTopN( CommandType.TOP, ArgumentFactory.getArgumentList(ctx), getFieldList(ctx.fieldList()), - groupList - ); + groupList); } - /** - * From clause. - */ + /** From clause. */ @Override public UnresolvedPlan visitFromClause(FromClauseContext ctx) { if (ctx.tableFunction() != null) { @@ -358,34 +319,31 @@ public UnresolvedPlan visitFromClause(FromClauseContext ctx) { @Override public UnresolvedPlan visitTableSourceClause(TableSourceClauseContext ctx) { - return new Relation(ctx.tableSource() - .stream().map(this::internalVisitExpression) - .collect(Collectors.toList())); + return new Relation( + ctx.tableSource().stream().map(this::internalVisitExpression).collect(Collectors.toList())); } @Override public UnresolvedPlan visitTableFunction(TableFunctionContext ctx) { ImmutableList.Builder builder = ImmutableList.builder(); - ctx.functionArgs().functionArg().forEach(arg - -> { - String argName = (arg.ident() != null) ? arg.ident().getText() : null; - builder.add( - new UnresolvedArgument(argName, - this.internalVisitExpression(arg.valueExpression()))); - }); + ctx.functionArgs() + .functionArg() + .forEach( + arg -> { + String argName = (arg.ident() != null) ? arg.ident().getText() : null; + builder.add( + new UnresolvedArgument( + argName, this.internalVisitExpression(arg.valueExpression()))); + }); return new TableFunction(this.internalVisitExpression(ctx.qualifiedName()), builder.build()); } - /** - * Navigate to & build AST expression. - */ + /** Navigate to & build AST expression. */ private UnresolvedExpression internalVisitExpression(ParseTree tree) { return expressionBuilder.visit(tree); } - /** - * Simply return non-default value for now. - */ + /** Simply return non-default value for now. */ @Override protected UnresolvedPlan aggregateResult(UnresolvedPlan aggregate, UnresolvedPlan nextResult) { if (nextResult != defaultResult()) { @@ -394,52 +352,48 @@ protected UnresolvedPlan aggregateResult(UnresolvedPlan aggregate, UnresolvedPla return aggregate; } - /** - * Kmeans command. - */ + /** Kmeans command. */ @Override public UnresolvedPlan visitKmeansCommand(KmeansCommandContext ctx) { ImmutableMap.Builder builder = ImmutableMap.builder(); ctx.kmeansParameter() - .forEach(x -> { - builder.put(x.children.get(0).toString(), - (Literal) internalVisitExpression(x.children.get(2))); - }); + .forEach( + x -> { + builder.put( + x.children.get(0).toString(), + (Literal) internalVisitExpression(x.children.get(2))); + }); return new Kmeans(builder.build()); } - /** - * AD command. - */ + /** AD command. */ @Override public UnresolvedPlan visitAdCommand(AdCommandContext ctx) { ImmutableMap.Builder builder = ImmutableMap.builder(); ctx.adParameter() - .forEach(x -> { - builder.put(x.children.get(0).toString(), - (Literal) internalVisitExpression(x.children.get(2))); - }); + .forEach( + x -> { + builder.put( + x.children.get(0).toString(), + (Literal) internalVisitExpression(x.children.get(2))); + }); return new AD(builder.build()); } - /** - * ml command. - */ + /** ml command. */ @Override public UnresolvedPlan visitMlCommand(OpenSearchPPLParser.MlCommandContext ctx) { ImmutableMap.Builder builder = ImmutableMap.builder(); ctx.mlArg() - .forEach(x -> { - builder.put(x.argName.getText(), - (Literal) internalVisitExpression(x.argValue)); - }); + .forEach( + x -> { + builder.put(x.argName.getText(), (Literal) internalVisitExpression(x.argValue)); + }); return new ML(builder.build()); } - /** - * Get original text in query. - */ + /** Get original text in query. */ private String getTextInQuery(ParserRuleContext ctx) { Token start = ctx.getStart(); Token stop = ctx.getStop(); diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java index c775747ec4..690e45d67c 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.parser; import static org.opensearch.sql.expression.function.BuiltinFunctionName.IS_NOT_NULL; @@ -83,33 +82,25 @@ import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParserBaseVisitor; import org.opensearch.sql.ppl.utils.ArgumentFactory; -/** - * Class of building AST Expression nodes. - */ +/** Class of building AST Expression nodes. */ public class AstExpressionBuilder extends OpenSearchPPLParserBaseVisitor { private static final int DEFAULT_TAKE_FUNCTION_SIZE_VALUE = 10; - /** - * The function name mapping between fronted and core engine. - */ + /** The function name mapping between fronted and core engine. */ private static Map FUNCTION_NAME_MAPPING = new ImmutableMap.Builder() .put("isnull", IS_NULL.getName().getFunctionName()) .put("isnotnull", IS_NOT_NULL.getName().getFunctionName()) .build(); - /** - * Eval clause. - */ + /** Eval clause. */ @Override public UnresolvedExpression visitEvalClause(EvalClauseContext ctx) { return new Let((Field) visit(ctx.fieldExpression()), visit(ctx.expression())); } - /** - * Logical expression excluding boolean, comparison. - */ + /** Logical expression excluding boolean, comparison. */ @Override public UnresolvedExpression visitLogicalNot(LogicalNotContext ctx) { return new Not(visit(ctx.logicalExpression())); @@ -130,9 +121,7 @@ public UnresolvedExpression visitLogicalXor(LogicalXorContext ctx) { return new Xor(visit(ctx.left), visit(ctx.right)); } - /** - * Comparison expression. - */ + /** Comparison expression. */ @Override public UnresolvedExpression visitCompareExpr(CompareExprContext ctx) { return new Compare(ctx.comparisonOperator().getText(), visit(ctx.left), visit(ctx.right)); @@ -142,22 +131,16 @@ public UnresolvedExpression visitCompareExpr(CompareExprContext ctx) { public UnresolvedExpression visitInExpr(InExprContext ctx) { return new In( visit(ctx.valueExpression()), - ctx.valueList() - .literalValue() - .stream() + ctx.valueList().literalValue().stream() .map(this::visitLiteralValue) .collect(Collectors.toList())); } - /** - * Value Expression. - */ + /** Value Expression. */ @Override public UnresolvedExpression visitBinaryArithmetic(BinaryArithmeticContext ctx) { return new Function( - ctx.binaryOperator.getText(), - Arrays.asList(visit(ctx.left), visit(ctx.right)) - ); + ctx.binaryOperator.getText(), Arrays.asList(visit(ctx.left), visit(ctx.right))); } @Override @@ -165,9 +148,7 @@ public UnresolvedExpression visitParentheticValueExpr(ParentheticValueExprContex return visit(ctx.valueExpression()); // Discard parenthesis around } - /** - * Field expression. - */ + /** Field expression. */ @Override public UnresolvedExpression visitFieldExpression(FieldExpressionContext ctx) { return new Field((QualifiedName) visit(ctx.qualifiedName())); @@ -182,13 +163,10 @@ public UnresolvedExpression visitWcFieldExpression(WcFieldExpressionContext ctx) public UnresolvedExpression visitSortField(SortFieldContext ctx) { return new Field( visit(ctx.sortFieldExpression().fieldExpression().qualifiedName()), - ArgumentFactory.getArgumentList(ctx) - ); + ArgumentFactory.getArgumentList(ctx)); } - /** - * Aggregation function. - */ + /** Aggregation function. */ @Override public UnresolvedExpression visitStatsFunctionCall(StatsFunctionCallContext ctx) { return new AggregateFunction(ctx.statsFunctionName().getText(), visit(ctx.valueExpression())); @@ -206,7 +184,9 @@ public UnresolvedExpression visitDistinctCountFunctionCall(DistinctCountFunction @Override public UnresolvedExpression visitPercentileAggFunction(PercentileAggFunctionContext ctx) { - return new AggregateFunction(ctx.PERCENTILE().getText(), visit(ctx.aggField), + return new AggregateFunction( + ctx.PERCENTILE().getText(), + visit(ctx.aggField), Collections.singletonList(new Argument("rank", (Literal) visit(ctx.value)))); } @@ -214,34 +194,32 @@ public UnresolvedExpression visitPercentileAggFunction(PercentileAggFunctionCont public UnresolvedExpression visitTakeAggFunctionCall( OpenSearchPPLParser.TakeAggFunctionCallContext ctx) { ImmutableList.Builder builder = ImmutableList.builder(); - builder.add(new UnresolvedArgument("size", - ctx.takeAggFunction().size != null ? visit(ctx.takeAggFunction().size) : - AstDSL.intLiteral(DEFAULT_TAKE_FUNCTION_SIZE_VALUE))); - return new AggregateFunction("take", visit(ctx.takeAggFunction().fieldExpression()), - builder.build()); + builder.add( + new UnresolvedArgument( + "size", + ctx.takeAggFunction().size != null + ? visit(ctx.takeAggFunction().size) + : AstDSL.intLiteral(DEFAULT_TAKE_FUNCTION_SIZE_VALUE))); + return new AggregateFunction( + "take", visit(ctx.takeAggFunction().fieldExpression()), builder.build()); } - /** - * Eval function. - */ + /** Eval function. */ @Override public UnresolvedExpression visitBooleanFunctionCall(BooleanFunctionCallContext ctx) { final String functionName = ctx.conditionFunctionBase().getText(); - return buildFunction(FUNCTION_NAME_MAPPING.getOrDefault(functionName, functionName), + return buildFunction( + FUNCTION_NAME_MAPPING.getOrDefault(functionName, functionName), ctx.functionArgs().functionArg()); } - /** - * Eval function. - */ + /** Eval function. */ @Override public UnresolvedExpression visitEvalFunctionCall(EvalFunctionCallContext ctx) { return buildFunction(ctx.evalFunctionName().getText(), ctx.functionArgs().functionArg()); } - /** - * Cast function. - */ + /** Cast function. */ @Override public UnresolvedExpression visitDataTypeFunctionCall(DataTypeFunctionCallContext ctx) { return new Cast(visit(ctx.expression()), visit(ctx.convertedDataType())); @@ -252,15 +230,10 @@ public UnresolvedExpression visitConvertedDataType(ConvertedDataTypeContext ctx) return AstDSL.stringLiteral(ctx.getText()); } - private Function buildFunction(String functionName, - List args) { + private Function buildFunction( + String functionName, List args) { return new Function( - functionName, - args - .stream() - .map(this::visitFunctionArg) - .collect(Collectors.toList()) - ); + functionName, args.stream().map(this::visitFunctionArg).collect(Collectors.toList())); } @Override @@ -290,70 +263,62 @@ public UnresolvedExpression visitTableSource(TableSourceContext ctx) { @Override public UnresolvedExpression visitPositionFunction( - OpenSearchPPLParser.PositionFunctionContext ctx) { + OpenSearchPPLParser.PositionFunctionContext ctx) { return new Function( - POSITION.getName().getFunctionName(), - Arrays.asList(visitFunctionArg(ctx.functionArg(0)), - visitFunctionArg(ctx.functionArg(1)))); + POSITION.getName().getFunctionName(), + Arrays.asList(visitFunctionArg(ctx.functionArg(0)), visitFunctionArg(ctx.functionArg(1)))); } @Override public UnresolvedExpression visitExtractFunctionCall( - OpenSearchPPLParser.ExtractFunctionCallContext ctx) { + OpenSearchPPLParser.ExtractFunctionCallContext ctx) { return new Function( - ctx.extractFunction().EXTRACT().toString(), - getExtractFunctionArguments(ctx)); + ctx.extractFunction().EXTRACT().toString(), getExtractFunctionArguments(ctx)); } private List getExtractFunctionArguments( - OpenSearchPPLParser.ExtractFunctionCallContext ctx) { - List args = Arrays.asList( + OpenSearchPPLParser.ExtractFunctionCallContext ctx) { + List args = + Arrays.asList( new Literal(ctx.extractFunction().datetimePart().getText(), DataType.STRING), - visitFunctionArg(ctx.extractFunction().functionArg()) - ); + visitFunctionArg(ctx.extractFunction().functionArg())); return args; } @Override public UnresolvedExpression visitGetFormatFunctionCall( - OpenSearchPPLParser.GetFormatFunctionCallContext ctx) { + OpenSearchPPLParser.GetFormatFunctionCallContext ctx) { return new Function( - ctx.getFormatFunction().GET_FORMAT().toString(), - getFormatFunctionArguments(ctx)); + ctx.getFormatFunction().GET_FORMAT().toString(), getFormatFunctionArguments(ctx)); } private List getFormatFunctionArguments( - OpenSearchPPLParser.GetFormatFunctionCallContext ctx) { - List args = Arrays.asList( + OpenSearchPPLParser.GetFormatFunctionCallContext ctx) { + List args = + Arrays.asList( new Literal(ctx.getFormatFunction().getFormatType().getText(), DataType.STRING), - visitFunctionArg(ctx.getFormatFunction().functionArg()) - ); + visitFunctionArg(ctx.getFormatFunction().functionArg())); return args; } @Override public UnresolvedExpression visitTimestampFunctionCall( - OpenSearchPPLParser.TimestampFunctionCallContext ctx) { + OpenSearchPPLParser.TimestampFunctionCallContext ctx) { return new Function( - ctx.timestampFunction().timestampFunctionName().getText(), - timestampFunctionArguments(ctx)); + ctx.timestampFunction().timestampFunctionName().getText(), timestampFunctionArguments(ctx)); } private List timestampFunctionArguments( - OpenSearchPPLParser.TimestampFunctionCallContext ctx) { - List args = Arrays.asList( - new Literal( - ctx.timestampFunction().simpleDateTimePart().getText(), - DataType.STRING), + OpenSearchPPLParser.TimestampFunctionCallContext ctx) { + List args = + Arrays.asList( + new Literal(ctx.timestampFunction().simpleDateTimePart().getText(), DataType.STRING), visitFunctionArg(ctx.timestampFunction().firstArg), - visitFunctionArg(ctx.timestampFunction().secondArg) - ); + visitFunctionArg(ctx.timestampFunction().secondArg)); return args; } - /** - * Literal and value. - */ + /** Literal and value. */ @Override public UnresolvedExpression visitIdentsAsQualifiedName(IdentsAsQualifiedNameContext ctx) { return visitIdentifiers(ctx.ident()); @@ -406,8 +371,10 @@ public UnresolvedExpression visitBooleanLiteral(BooleanLiteralContext ctx) { @Override public UnresolvedExpression visitBySpanClause(BySpanClauseContext ctx) { String name = ctx.spanClause().getText(); - return ctx.alias != null ? new Alias(name, visit(ctx.spanClause()), StringUtils - .unquoteIdentifier(ctx.alias.getText())) : new Alias(name, visit(ctx.spanClause())); + return ctx.alias != null + ? new Alias( + name, visit(ctx.spanClause()), StringUtils.unquoteIdentifier(ctx.alias.getText())) + : new Alias(name, visit(ctx.spanClause())); } @Override @@ -421,8 +388,7 @@ private QualifiedName visitIdentifiers(List ctx) { ctx.stream() .map(RuleContext::getText) .map(StringUtils::unquoteIdentifier) - .collect(Collectors.toList()) - ); + .collect(Collectors.toList())); } private List singleFieldRelevanceArguments( @@ -430,13 +396,21 @@ private List singleFieldRelevanceArguments( // all the arguments are defaulted to string values // to skip environment resolving and function signature resolving ImmutableList.Builder builder = ImmutableList.builder(); - builder.add(new UnresolvedArgument("field", - new QualifiedName(StringUtils.unquoteText(ctx.field.getText())))); - builder.add(new UnresolvedArgument("query", - new Literal(StringUtils.unquoteText(ctx.query.getText()), DataType.STRING))); - ctx.relevanceArg().forEach(v -> builder.add(new UnresolvedArgument( - v.relevanceArgName().getText().toLowerCase(), new Literal(StringUtils.unquoteText( - v.relevanceArgValue().getText()), DataType.STRING)))); + builder.add( + new UnresolvedArgument( + "field", new QualifiedName(StringUtils.unquoteText(ctx.field.getText())))); + builder.add( + new UnresolvedArgument( + "query", new Literal(StringUtils.unquoteText(ctx.query.getText()), DataType.STRING))); + ctx.relevanceArg() + .forEach( + v -> + builder.add( + new UnresolvedArgument( + v.relevanceArgName().getText().toLowerCase(), + new Literal( + StringUtils.unquoteText(v.relevanceArgValue().getText()), + DataType.STRING)))); return builder.build(); } @@ -445,19 +419,26 @@ private List multiFieldRelevanceArguments( // all the arguments are defaulted to string values // to skip environment resolving and function signature resolving ImmutableList.Builder builder = ImmutableList.builder(); - var fields = new RelevanceFieldList(ctx - .getRuleContexts(OpenSearchPPLParser.RelevanceFieldAndWeightContext.class) - .stream() - .collect(Collectors.toMap( - f -> StringUtils.unquoteText(f.field.getText()), - f -> (f.weight == null) ? 1F : Float.parseFloat(f.weight.getText())))); + var fields = + new RelevanceFieldList( + ctx.getRuleContexts(OpenSearchPPLParser.RelevanceFieldAndWeightContext.class).stream() + .collect( + Collectors.toMap( + f -> StringUtils.unquoteText(f.field.getText()), + f -> (f.weight == null) ? 1F : Float.parseFloat(f.weight.getText())))); builder.add(new UnresolvedArgument("fields", fields)); - builder.add(new UnresolvedArgument("query", - new Literal(StringUtils.unquoteText(ctx.query.getText()), DataType.STRING))); - ctx.relevanceArg().forEach(v -> builder.add(new UnresolvedArgument( - v.relevanceArgName().getText().toLowerCase(), new Literal(StringUtils.unquoteText( - v.relevanceArgValue().getText()), DataType.STRING)))); + builder.add( + new UnresolvedArgument( + "query", new Literal(StringUtils.unquoteText(ctx.query.getText()), DataType.STRING))); + ctx.relevanceArg() + .forEach( + v -> + builder.add( + new UnresolvedArgument( + v.relevanceArgName().getText().toLowerCase(), + new Literal( + StringUtils.unquoteText(v.relevanceArgValue().getText()), + DataType.STRING)))); return builder.build(); } - } diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstStatementBuilder.java b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstStatementBuilder.java index 3b7e5a78dd..e276e6d523 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstStatementBuilder.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstStatementBuilder.java @@ -21,9 +21,7 @@ import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser; import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParserBaseVisitor; -/** - * Build {@link Statement} from PPL Query. - */ +/** Build {@link Statement} from PPL Query. */ @RequiredArgsConstructor public class AstStatementBuilder extends OpenSearchPPLParserBaseVisitor { diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/utils/ArgumentFactory.java b/ppl/src/main/java/org/opensearch/sql/ppl/utils/ArgumentFactory.java index 941bfe680e..f89ecf9c6e 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/utils/ArgumentFactory.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/utils/ArgumentFactory.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.utils; import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.BooleanLiteralContext; @@ -24,9 +23,7 @@ import org.opensearch.sql.ast.expression.Literal; import org.opensearch.sql.common.utils.StringUtils; -/** - * Util class to get all arguments as a list from the PPL command. - */ +/** Util class to get all arguments as a list from the PPL command. */ public class ArgumentFactory { /** @@ -39,8 +36,7 @@ public static List getArgumentList(FieldsCommandContext ctx) { return Collections.singletonList( ctx.MINUS() != null ? new Argument("exclude", new Literal(true, DataType.BOOLEAN)) - : new Argument("exclude", new Literal(false, DataType.BOOLEAN)) - ); + : new Argument("exclude", new Literal(false, DataType.BOOLEAN))); } /** @@ -62,8 +58,7 @@ public static List getArgumentList(StatsCommandContext ctx) { : new Argument("delim", new Literal(" ", DataType.STRING)), ctx.dedupsplit != null ? new Argument("dedupsplit", getArgumentValue(ctx.dedupsplit)) - : new Argument("dedupsplit", new Literal(false, DataType.BOOLEAN)) - ); + : new Argument("dedupsplit", new Literal(false, DataType.BOOLEAN))); } /** @@ -82,8 +77,7 @@ public static List getArgumentList(DedupCommandContext ctx) { : new Argument("keepempty", new Literal(false, DataType.BOOLEAN)), ctx.consecutive != null ? new Argument("consecutive", getArgumentValue(ctx.consecutive)) - : new Argument("consecutive", new Literal(false, DataType.BOOLEAN)) - ); + : new Argument("consecutive", new Literal(false, DataType.BOOLEAN))); } /** @@ -100,13 +94,12 @@ public static List getArgumentList(SortFieldContext ctx) { ctx.sortFieldExpression().AUTO() != null ? new Argument("type", new Literal("auto", DataType.STRING)) : ctx.sortFieldExpression().IP() != null - ? new Argument("type", new Literal("ip", DataType.STRING)) - : ctx.sortFieldExpression().NUM() != null - ? new Argument("type", new Literal("num", DataType.STRING)) - : ctx.sortFieldExpression().STR() != null - ? new Argument("type", new Literal("str", DataType.STRING)) - : new Argument("type", new Literal(null, DataType.NULL)) - ); + ? new Argument("type", new Literal("ip", DataType.STRING)) + : ctx.sortFieldExpression().NUM() != null + ? new Argument("type", new Literal("num", DataType.STRING)) + : ctx.sortFieldExpression().STR() != null + ? new Argument("type", new Literal("str", DataType.STRING)) + : new Argument("type", new Literal(null, DataType.NULL))); } /** @@ -119,8 +112,7 @@ public static List getArgumentList(TopCommandContext ctx) { return Collections.singletonList( ctx.number != null ? new Argument("noOfResults", getArgumentValue(ctx.number)) - : new Argument("noOfResults", new Literal(10, DataType.INTEGER)) - ); + : new Argument("noOfResults", new Literal(10, DataType.INTEGER))); } /** @@ -130,21 +122,21 @@ public static List getArgumentList(TopCommandContext ctx) { * @return the list of argument with default number of results for the rare command */ public static List getArgumentList(RareCommandContext ctx) { - return Collections - .singletonList(new Argument("noOfResults", new Literal(10, DataType.INTEGER))); + return Collections.singletonList( + new Argument("noOfResults", new Literal(10, DataType.INTEGER))); } /** * parse argument value into Literal. + * * @param ctx ParserRuleContext instance * @return Literal */ private static Literal getArgumentValue(ParserRuleContext ctx) { return ctx instanceof IntegerLiteralContext - ? new Literal(Integer.parseInt(ctx.getText()), DataType.INTEGER) - : ctx instanceof BooleanLiteralContext + ? new Literal(Integer.parseInt(ctx.getText()), DataType.INTEGER) + : ctx instanceof BooleanLiteralContext ? new Literal(Boolean.valueOf(ctx.getText()), DataType.BOOLEAN) : new Literal(StringUtils.unquoteText(ctx.getText()), DataType.STRING); } - } diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizer.java b/ppl/src/main/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizer.java index 1f0e6f0d52..d28e5d122b 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizer.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizer.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.utils; import com.google.common.base.Strings; @@ -54,9 +53,7 @@ import org.opensearch.sql.planner.logical.LogicalRename; import org.opensearch.sql.planner.logical.LogicalSort; -/** - * Utility class to mask sensitive information in incoming PPL queries. - */ +/** Utility class to mask sensitive information in incoming PPL queries. */ public class PPLQueryDataAnonymizer extends AbstractNodeVisitor { private static final String MASK_LITERAL = "***"; @@ -68,8 +65,8 @@ public PPLQueryDataAnonymizer() { } /** - * This method is used to anonymize sensitive data in PPL query. - * Sensitive data includes user data., + * This method is used to anonymize sensitive data in PPL query. Sensitive data includes user + * data. * * @return ppl query string with all user data replace with "***" */ @@ -81,9 +78,7 @@ public String anonymizeStatement(Statement plan) { return plan.accept(this, null); } - /** - * Handle Query Statement. - */ + /** Handle Query Statement. */ @Override public String visitQuery(Query node, String context) { return node.getPlan().accept(this, null); @@ -103,8 +98,9 @@ public String visitRelation(Relation node, String context) { public String visitTableFunction(TableFunction node, String context) { String arguments = node.getArguments().stream() - .map(unresolvedExpression - -> this.expressionAnalyzer.analyze(unresolvedExpression, context)) + .map( + unresolvedExpression -> + this.expressionAnalyzer.analyze(unresolvedExpression, context)) .collect(Collectors.joining(",")); return StringUtils.format("source=%s(%s)", node.getFunctionName().toString(), arguments); } @@ -116,37 +112,34 @@ public String visitFilter(Filter node, String context) { return StringUtils.format("%s | where %s", child, condition); } - /** - * Build {@link LogicalRename}. - */ + /** Build {@link LogicalRename}. */ @Override public String visitRename(Rename node, String context) { String child = node.getChild().get(0).accept(this, context); ImmutableMap.Builder renameMapBuilder = new ImmutableMap.Builder<>(); for (Map renameMap : node.getRenameList()) { - renameMapBuilder.put(visitExpression(renameMap.getOrigin()), + renameMapBuilder.put( + visitExpression(renameMap.getOrigin()), ((Field) renameMap.getTarget()).getField().toString()); } String renames = - renameMapBuilder.build().entrySet().stream().map(entry -> StringUtils.format("%s as %s", - entry.getKey(), entry.getValue())).collect(Collectors.joining(",")); + renameMapBuilder.build().entrySet().stream() + .map(entry -> StringUtils.format("%s as %s", entry.getKey(), entry.getValue())) + .collect(Collectors.joining(",")); return StringUtils.format("%s | rename %s", child, renames); } - /** - * Build {@link LogicalAggregation}. - */ + /** Build {@link LogicalAggregation}. */ @Override public String visitAggregation(Aggregation node, String context) { String child = node.getChild().get(0).accept(this, context); final String group = visitExpressionList(node.getGroupExprList()); - return StringUtils.format("%s | stats %s", child, - String.join(" ", visitExpressionList(node.getAggExprList()), groupBy(group)).trim()); + return StringUtils.format( + "%s | stats %s", + child, String.join(" ", visitExpressionList(node.getAggExprList()), groupBy(group)).trim()); } - /** - * Build {@link LogicalRareTopN}. - */ + /** Build {@link LogicalRareTopN}. */ @Override public String visitRareTopN(RareTopN node, String context) { final String child = node.getChild().get(0).accept(this, context); @@ -154,16 +147,15 @@ public String visitRareTopN(RareTopN node, String context) { Integer noOfResults = (Integer) options.get(0).getValue().getValue(); String fields = visitFieldList(node.getFields()); String group = visitExpressionList(node.getGroupExprList()); - return StringUtils.format("%s | %s %d %s", child, + return StringUtils.format( + "%s | %s %d %s", + child, node.getCommandType().name().toLowerCase(), noOfResults, - String.join(" ", fields, groupBy(group)).trim() - ); + String.join(" ", fields, groupBy(group)).trim()); } - /** - * Build {@link LogicalProject} or {@link LogicalRemove} from {@link Field}. - */ + /** Build {@link LogicalProject} or {@link LogicalRemove} from {@link Field}. */ @Override public String visitProject(Project node, String context) { String child = node.getChild().get(0).accept(this, context); @@ -180,9 +172,7 @@ public String visitProject(Project node, String context) { return StringUtils.format("%s | fields %s %s", child, arg, fields); } - /** - * Build {@link LogicalEval}. - */ + /** Build {@link LogicalEval}. */ @Override public String visitEval(Eval node, String context) { String child = node.getChild().get(0).accept(this, context); @@ -192,14 +182,14 @@ public String visitEval(Eval node, String context) { String target = let.getVar().getField().toString(); expressionsBuilder.add(ImmutablePair.of(target, expression)); } - String expressions = expressionsBuilder.build().stream().map(pair -> StringUtils.format("%s" - + "=%s", pair.getLeft(), pair.getRight())).collect(Collectors.joining(" ")); + String expressions = + expressionsBuilder.build().stream() + .map(pair -> StringUtils.format("%s" + "=%s", pair.getLeft(), pair.getRight())) + .collect(Collectors.joining(" ")); return StringUtils.format("%s | eval %s", child, expressions); } - /** - * Build {@link LogicalSort}. - */ + /** Build {@link LogicalSort}. */ @Override public String visitSort(Sort node, String context) { String child = node.getChild().get(0).accept(this, context); @@ -208,9 +198,7 @@ public String visitSort(Sort node, String context) { return StringUtils.format("%s | sort %s", child, sortList); } - /** - * Build {@link LogicalDedupe}. - */ + /** Build {@link LogicalDedupe}. */ @Override public String visitDedupe(Dedupe node, String context) { String child = node.getChild().get(0).accept(this, context); @@ -220,10 +208,9 @@ public String visitDedupe(Dedupe node, String context) { Boolean keepEmpty = (Boolean) options.get(1).getValue().getValue(); Boolean consecutive = (Boolean) options.get(2).getValue().getValue(); - return StringUtils - .format("%s | dedup %s %d keepempty=%b consecutive=%b", child, fields, allowedDuplication, - keepEmpty, - consecutive); + return StringUtils.format( + "%s | dedup %s %d keepempty=%b consecutive=%b", + child, fields, allowedDuplication, keepEmpty, consecutive); } @Override @@ -238,8 +225,9 @@ private String visitFieldList(List fieldList) { } private String visitExpressionList(List expressionList) { - return expressionList.isEmpty() ? "" : - expressionList.stream().map(this::visitExpression).collect(Collectors.joining(",")); + return expressionList.isEmpty() + ? "" + : expressionList.stream().map(this::visitExpression).collect(Collectors.joining(",")); } private String visitExpression(UnresolvedExpression expression) { @@ -250,11 +238,8 @@ private String groupBy(String groupBy) { return Strings.isNullOrEmpty(groupBy) ? "" : StringUtils.format("by %s", groupBy); } - /** - * Expression Anonymizer. - */ - private static class AnonymizerExpressionAnalyzer extends AbstractNodeVisitor { + /** Expression Anonymizer. */ + private static class AnonymizerExpressionAnalyzer extends AbstractNodeVisitor { public String analyze(UnresolvedExpression unresolved, String context) { return unresolved.accept(this, context); diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/utils/UnresolvedPlanHelper.java b/ppl/src/main/java/org/opensearch/sql/ppl/utils/UnresolvedPlanHelper.java index 4fb9eee6a0..a502f2d769 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/utils/UnresolvedPlanHelper.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/utils/UnresolvedPlanHelper.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.utils; import com.google.common.collect.ImmutableList; @@ -12,15 +11,11 @@ import org.opensearch.sql.ast.tree.Project; import org.opensearch.sql.ast.tree.UnresolvedPlan; -/** - * The helper to add select to {@link UnresolvedPlan} if needed. - */ +/** The helper to add select to {@link UnresolvedPlan} if needed. */ @UtilityClass public class UnresolvedPlanHelper { - /** - * Attach Select All to PPL commands if required. - */ + /** Attach Select All to PPL commands if required. */ public UnresolvedPlan addSelectAll(UnresolvedPlan plan) { if ((plan instanceof Project) && !((Project) plan).isExcluded()) { return plan; diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/PPLServiceTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/PPLServiceTest.java index c14eb3dba1..598f6691cb 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/PPLServiceTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/PPLServiceTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.mockito.ArgumentMatchers.any; @@ -41,21 +40,17 @@ public class PPLServiceTest { private DefaultQueryManager queryManager; - @Mock - private QueryService queryService; + @Mock private QueryService queryService; - @Mock - private ExecutionEngine.Schema schema; + @Mock private ExecutionEngine.Schema schema; - /** - * Setup the test context. - */ + /** Setup the test context. */ @Before public void setUp() { queryManager = DefaultQueryManager.defaultQueryManager(); - pplService = new PPLService(new PPLSyntaxParser(), queryManager, - new QueryPlanFactory(queryService)); + pplService = + new PPLService(new PPLSyntaxParser(), queryManager, new QueryPlanFactory(queryService)); } @After @@ -65,18 +60,20 @@ public void cleanup() throws InterruptedException { @Test public void testExecuteShouldPass() { - doAnswer(invocation -> { - ResponseListener listener = invocation.getArgument(1); - listener.onResponse(new QueryResponse(schema, Collections.emptyList(), Cursor.None)); - return null; - }).when(queryService).execute(any(), any()); - - pplService.execute(new PPLQueryRequest("search source=t a=1", null, QUERY), + doAnswer( + invocation -> { + ResponseListener listener = invocation.getArgument(1); + listener.onResponse(new QueryResponse(schema, Collections.emptyList(), Cursor.None)); + return null; + }) + .when(queryService) + .execute(any(), any()); + + pplService.execute( + new PPLQueryRequest("search source=t a=1", null, QUERY), new ResponseListener() { @Override - public void onResponse(QueryResponse pplQueryResponse) { - - } + public void onResponse(QueryResponse pplQueryResponse) {} @Override public void onFailure(Exception e) { @@ -87,17 +84,20 @@ public void onFailure(Exception e) { @Test public void testExecuteCsvFormatShouldPass() { - doAnswer(invocation -> { - ResponseListener listener = invocation.getArgument(1); - listener.onResponse(new QueryResponse(schema, Collections.emptyList(), Cursor.None)); - return null; - }).when(queryService).execute(any(), any()); - - pplService.execute(new PPLQueryRequest("search source=t a=1", null, QUERY, "csv"), + doAnswer( + invocation -> { + ResponseListener listener = invocation.getArgument(1); + listener.onResponse(new QueryResponse(schema, Collections.emptyList(), Cursor.None)); + return null; + }) + .when(queryService) + .execute(any(), any()); + + pplService.execute( + new PPLQueryRequest("search source=t a=1", null, QUERY, "csv"), new ResponseListener() { @Override - public void onResponse(QueryResponse pplQueryResponse) { - } + public void onResponse(QueryResponse pplQueryResponse) {} @Override public void onFailure(Exception e) { @@ -108,17 +108,20 @@ public void onFailure(Exception e) { @Test public void testExplainShouldPass() { - doAnswer(invocation -> { - ResponseListener listener = invocation.getArgument(1); - listener.onResponse(new ExplainResponse(new ExplainResponseNode("test"))); - return null; - }).when(queryService).explain(any(), any()); - - pplService.explain(new PPLQueryRequest("search source=t a=1", null, EXPLAIN), + doAnswer( + invocation -> { + ResponseListener listener = invocation.getArgument(1); + listener.onResponse(new ExplainResponse(new ExplainResponseNode("test"))); + return null; + }) + .when(queryService) + .explain(any(), any()); + + pplService.explain( + new PPLQueryRequest("search source=t a=1", null, EXPLAIN), new ResponseListener() { @Override - public void onResponse(ExplainResponse pplQueryResponse) { - } + public void onResponse(ExplainResponse pplQueryResponse) {} @Override public void onFailure(Exception e) { @@ -129,7 +132,8 @@ public void onFailure(Exception e) { @Test public void testExecuteWithIllegalQueryShouldBeCaughtByHandler() { - pplService.execute(new PPLQueryRequest("search", null, QUERY), + pplService.execute( + new PPLQueryRequest("search", null, QUERY), new ResponseListener() { @Override public void onResponse(QueryResponse pplQueryResponse) { @@ -137,15 +141,14 @@ public void onResponse(QueryResponse pplQueryResponse) { } @Override - public void onFailure(Exception e) { - - } + public void onFailure(Exception e) {} }); } @Test public void testExplainWithIllegalQueryShouldBeCaughtByHandler() { - pplService.explain(new PPLQueryRequest("search", null, QUERY), + pplService.explain( + new PPLQueryRequest("search", null, QUERY), new ResponseListener<>() { @Override public void onResponse(ExplainResponse pplQueryResponse) { @@ -153,26 +156,26 @@ public void onResponse(ExplainResponse pplQueryResponse) { } @Override - public void onFailure(Exception e) { - - } + public void onFailure(Exception e) {} }); } @Test public void testPrometheusQuery() { - doAnswer(invocation -> { - ResponseListener listener = invocation.getArgument(1); - listener.onResponse(new QueryResponse(schema, Collections.emptyList(), Cursor.None)); - return null; - }).when(queryService).execute(any(), any()); - - pplService.execute(new PPLQueryRequest("source = prometheus.http_requests_total", null, QUERY), + doAnswer( + invocation -> { + ResponseListener listener = invocation.getArgument(1); + listener.onResponse(new QueryResponse(schema, Collections.emptyList(), Cursor.None)); + return null; + }) + .when(queryService) + .execute(any(), any()); + + pplService.execute( + new PPLQueryRequest("source = prometheus.http_requests_total", null, QUERY), new ResponseListener<>() { @Override - public void onResponse(QueryResponse pplQueryResponse) { - - } + public void onResponse(QueryResponse pplQueryResponse) {} @Override public void onFailure(Exception e) { @@ -183,7 +186,8 @@ public void onFailure(Exception e) { @Test public void testInvalidPPLQuery() { - pplService.execute(new PPLQueryRequest("search", null, QUERY), + pplService.execute( + new PPLQueryRequest("search", null, QUERY), new ResponseListener() { @Override public void onResponse(QueryResponse pplQueryResponse) { @@ -191,9 +195,7 @@ public void onResponse(QueryResponse pplQueryResponse) { } @Override - public void onFailure(Exception e) { - - } + public void onFailure(Exception e) {} }); } } diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/NowLikeFunctionParserTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/NowLikeFunctionParserTest.java index 9f635fdd81..f6a04983e2 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/NowLikeFunctionParserTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/NowLikeFunctionParserTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.antlr; import static org.junit.Assert.assertNotEquals; @@ -21,6 +20,7 @@ public class NowLikeFunctionParserTest { /** * Set parameterized values used in test. + * * @param name Function name * @param hasFsp Whether function has fsp argument * @param hasShortcut Whether function has shortcut (call without `()`) @@ -33,24 +33,26 @@ public NowLikeFunctionParserTest(String name, Boolean hasFsp, Boolean hasShortcu /** * Returns function data to test. + * * @return An iterable. */ @Parameterized.Parameters(name = "{0}") public static Iterable functionNames() { - return List.of(new Object[][]{ - {"now", true, false}, - {"current_timestamp", true, true}, - {"localtimestamp", true, true}, - {"localtime", true, true}, - {"sysdate", true, false}, - {"curtime", true, false}, - {"current_time", true, true}, - {"curdate", false, false}, - {"current_date", false, true}, - {"utc_date", false, false}, - {"utc_time", false, false}, - {"utc_timestamp", false, false} - }); + return List.of( + new Object[][] { + {"now", true, false}, + {"current_timestamp", true, true}, + {"localtimestamp", true, true}, + {"localtime", true, true}, + {"sysdate", true, false}, + {"curtime", true, false}, + {"current_time", true, true}, + {"curdate", false, false}, + {"current_date", false, true}, + {"utc_date", false, false}, + {"utc_time", false, false}, + {"utc_timestamp", false, false} + }); } private final String name; diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserMatchBoolPrefixSamplesTests.java b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserMatchBoolPrefixSamplesTests.java index dd146ea2cf..7de197028e 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserMatchBoolPrefixSamplesTests.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserMatchBoolPrefixSamplesTests.java @@ -13,26 +13,24 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; - @RunWith(Parameterized.class) public class PPLSyntaxParserMatchBoolPrefixSamplesTests { - - /** Returns sample queries that the PPLSyntaxParser is expected to parse successfully. + /** + * Returns sample queries that the PPLSyntaxParser is expected to parse successfully. + * * @return an Iterable of sample queries. */ @Parameterized.Parameters(name = "{0}") public static Iterable sampleQueries() { return List.of( "source=t a= 1 | where match_bool_prefix(a, 'hello world')", - "source=t a = 1 | where match_bool_prefix(a, 'hello world'," - + " minimum_should_match = 3)", + "source=t a = 1 | where match_bool_prefix(a, 'hello world'," + " minimum_should_match = 3)", "source=t a = 1 | where match_bool_prefix(a, 'hello world', fuzziness='AUTO')", "source=t a = 1 | where match_bool_prefix(a, 'hello world', fuzziness='AUTO:4,6')", "source=t a= 1 | where match_bool_prefix(a, 'hello world', prefix_length=0)", "source=t a= 1 | where match_bool_prefix(a, 'hello world', max_expansions=1)", - "source=t a= 1 | where match_bool_prefix(a, 'hello world'," - + " fuzzy_transpositions=true)", + "source=t a= 1 | where match_bool_prefix(a, 'hello world'," + " fuzzy_transpositions=true)", "source=t a= 1 | where match_bool_prefix(a, 'hello world'," + " fuzzy_rewrite=constant_score)", "source=t a= 1 | where match_bool_prefix(a, 'hello world'," @@ -43,8 +41,7 @@ public static Iterable sampleQueries() { + " fuzzy_rewrite=top_terms_blended_freqs_1)", "source=t a= 1 | where match_bool_prefix(a, 'hello world'," + " fuzzy_rewrite=top_terms_boost_1)", - "source=t a= 1 | where match_bool_prefix(a, 'hello world'," - + " fuzzy_rewrite=top_terms_1)", + "source=t a= 1 | where match_bool_prefix(a, 'hello world'," + " fuzzy_rewrite=top_terms_1)", "source=t a= 1 | where match_bool_prefix(a, 'hello world', boost=1)", "source=t a = 1 | where match_bool_prefix(a, 'hello world', analyzer = 'standard'," + "prefix_length = '0', boost = 1)"); diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserMatchPhraseSamplesTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserMatchPhraseSamplesTest.java index aef6d1d69e..94222ec103 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserMatchPhraseSamplesTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserMatchPhraseSamplesTest.java @@ -13,22 +13,22 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; - @RunWith(Parameterized.class) public class PPLSyntaxParserMatchPhraseSamplesTest { - - /** Returns sample queries that the PPLSyntaxParser is expected to parse successfully. + /** + * Returns sample queries that the PPLSyntaxParser is expected to parse successfully. + * * @return an Iterable of sample queries. */ @Parameterized.Parameters(name = "{0}") public static Iterable sampleQueries() { return List.of( - "source=t a= 1 | where match_phrase(a, 'hello world')", - "source=t a = 1 | where match_phrase(a, 'hello world', slop = 3)", - "source=t a = 1 | where match_phrase(a, 'hello world', analyzer = 'standard'," - + "zero_terms_query = 'none', slop = 3)", - "source=t a = 1 | where match_phrase(a, 'hello world', zero_terms_query = all)"); + "source=t a= 1 | where match_phrase(a, 'hello world')", + "source=t a = 1 | where match_phrase(a, 'hello world', slop = 3)", + "source=t a = 1 | where match_phrase(a, 'hello world', analyzer = 'standard'," + + "zero_terms_query = 'none', slop = 3)", + "source=t a = 1 | where match_phrase(a, 'hello world', zero_terms_query = all)"); } private final String query; diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserTest.java index 57cee7fa1d..943953d416 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.antlr; import static org.junit.Assert.assertNotEquals; @@ -19,8 +18,7 @@ public class PPLSyntaxParserTest { - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); + @Rule public ExpectedException exceptionRule = ExpectedException.none(); @Test public void testSearchCommandShouldPass() { @@ -140,99 +138,170 @@ public void testTopCommandWithoutNAndGroupByShouldPass() { @Test public void testCanParseMultiMatchRelevanceFunction() { - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match(['address'], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match(['address', 'notes'], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match([\"*\"], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match([\"address\"], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match([`address`], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match([address], 'query')")); - - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match(['address' ^ 1.0, 'notes' ^ 2.2], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match(['address' ^ 1.1, 'notes'], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match(['address', 'notes' ^ 1.5], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match(['address', 'notes' 3], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match(['address' ^ .3, 'notes' 3], 'query')")); - - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE multi_match([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query'," - + "analyzer=keyword, quote_field_suffix=\".exact\", fuzzy_prefix_length = 4)")); + assertNotEquals( + null, new PPLSyntaxParser().parse("SOURCE=test | WHERE multi_match(['address'], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE multi_match(['address', 'notes'], 'query')")); + assertNotEquals( + null, new PPLSyntaxParser().parse("SOURCE=test | WHERE multi_match([\"*\"], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser().parse("SOURCE=test | WHERE multi_match([\"address\"], 'query')")); + assertNotEquals( + null, new PPLSyntaxParser().parse("SOURCE=test | WHERE multi_match([`address`], 'query')")); + assertNotEquals( + null, new PPLSyntaxParser().parse("SOURCE=test | WHERE multi_match([address], 'query')")); + + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE multi_match(['address' ^ 1.0, 'notes' ^ 2.2], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE multi_match(['address' ^ 1.1, 'notes'], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE multi_match(['address', 'notes' ^ 1.5], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE multi_match(['address', 'notes' 3], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE multi_match(['address' ^ .3, 'notes' 3], 'query')")); + + assertNotEquals( + null, + new PPLSyntaxParser() + .parse( + "SOURCE=test | WHERE multi_match([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse( + "SOURCE=test | WHERE multi_match([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query'," + + "analyzer=keyword, quote_field_suffix=\".exact\", fuzzy_prefix_length = 4)")); } @Test public void testCanParseSimpleQueryStringRelevanceFunction() { - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string(['address'], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string(['address', 'notes'], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string([\"*\"], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string([\"address\"], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string([`address`], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string([address], 'query')")); - - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string(['address' ^ 1.0, 'notes' ^ 2.2], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string(['address' ^ 1.1, 'notes'], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string(['address', 'notes' ^ 1.5], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string(['address', 'notes' 3], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string(['address' ^ .3, 'notes' 3], 'query')")); - - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE simple_query_string([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query'," - + "analyzer=keyword, quote_field_suffix=\".exact\", fuzzy_prefix_length = 4)")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE simple_query_string(['address'], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE simple_query_string(['address', 'notes'], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser().parse("SOURCE=test | WHERE simple_query_string([\"*\"], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE simple_query_string([\"address\"], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE simple_query_string([`address`], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser().parse("SOURCE=test | WHERE simple_query_string([address], 'query')")); + + assertNotEquals( + null, + new PPLSyntaxParser() + .parse( + "SOURCE=test | WHERE simple_query_string(['address' ^ 1.0, 'notes' ^ 2.2]," + + " 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE simple_query_string(['address' ^ 1.1, 'notes'], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE simple_query_string(['address', 'notes' ^ 1.5], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE simple_query_string(['address', 'notes' 3], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse( + "SOURCE=test | WHERE simple_query_string(['address' ^ .3, 'notes' 3], 'query')")); + + assertNotEquals( + null, + new PPLSyntaxParser() + .parse( + "SOURCE=test | WHERE simple_query_string([\"Tags\" ^ 1.5, Title, `Body` 4.2]," + + " 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse( + "SOURCE=test | WHERE simple_query_string([\"Tags\" ^ 1.5, Title, `Body` 4.2]," + + " 'query',analyzer=keyword, quote_field_suffix=\".exact\"," + + " fuzzy_prefix_length = 4)")); } @Test public void testCanParseQueryStringRelevanceFunction() { - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string(['address'], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string(['address', 'notes'], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string([\"*\"], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string([\"address\"], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string([`address`], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string([address], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string(['address' ^ 1.0, 'notes' ^ 2.2], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string(['address' ^ 1.1, 'notes'], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string(['address', 'notes' ^ 1.5], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string(['address', 'notes' 3], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string(['address' ^ .3, 'notes' 3], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query')")); - assertNotEquals(null, new PPLSyntaxParser().parse( - "SOURCE=test | WHERE query_string([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query'," - + "analyzer=keyword, quote_field_suffix=\".exact\", fuzzy_prefix_length = 4)")); + assertNotEquals( + null, + new PPLSyntaxParser().parse("SOURCE=test | WHERE query_string(['address'], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE query_string(['address', 'notes'], 'query')")); + assertNotEquals( + null, new PPLSyntaxParser().parse("SOURCE=test | WHERE query_string([\"*\"], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser().parse("SOURCE=test | WHERE query_string([\"address\"], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser().parse("SOURCE=test | WHERE query_string([`address`], 'query')")); + assertNotEquals( + null, new PPLSyntaxParser().parse("SOURCE=test | WHERE query_string([address], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE query_string(['address' ^ 1.0, 'notes' ^ 2.2], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE query_string(['address' ^ 1.1, 'notes'], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE query_string(['address', 'notes' ^ 1.5], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE query_string(['address', 'notes' 3], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse("SOURCE=test | WHERE query_string(['address' ^ .3, 'notes' 3], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse( + "SOURCE=test | WHERE query_string([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query')")); + assertNotEquals( + null, + new PPLSyntaxParser() + .parse( + "SOURCE=test | WHERE query_string([\"Tags\" ^ 1.5, Title, `Body` 4.2], 'query'," + + "analyzer=keyword, quote_field_suffix=\".exact\", fuzzy_prefix_length = 4)")); } @Test @@ -275,15 +344,35 @@ public void testDescribeCommandWithSourceShouldFail() { @Test public void testCanParseExtractFunction() { - String[] parts = List.of("MICROSECOND", "SECOND", "MINUTE", "HOUR", "DAY", - "WEEK", "MONTH", "QUARTER", "YEAR", "SECOND_MICROSECOND", - "MINUTE_MICROSECOND", "MINUTE_SECOND", "HOUR_MICROSECOND", - "HOUR_SECOND", "HOUR_MINUTE", "DAY_MICROSECOND", - "DAY_SECOND", "DAY_MINUTE", "DAY_HOUR", "YEAR_MONTH").toArray(new String[0]); + String[] parts = + List.of( + "MICROSECOND", + "SECOND", + "MINUTE", + "HOUR", + "DAY", + "WEEK", + "MONTH", + "QUARTER", + "YEAR", + "SECOND_MICROSECOND", + "MINUTE_MICROSECOND", + "MINUTE_SECOND", + "HOUR_MICROSECOND", + "HOUR_SECOND", + "HOUR_MINUTE", + "DAY_MICROSECOND", + "DAY_SECOND", + "DAY_MINUTE", + "DAY_HOUR", + "YEAR_MONTH") + .toArray(new String[0]); for (String part : parts) { - assertNotNull(new PPLSyntaxParser().parse( - String.format("SOURCE=test | eval k = extract(%s FROM \"2023-02-06\")", part))); + assertNotNull( + new PPLSyntaxParser() + .parse( + String.format("SOURCE=test | eval k = extract(%s FROM \"2023-02-06\")", part))); } } @@ -294,8 +383,9 @@ public void testCanParseGetFormatFunction() { for (String type : types) { for (String format : formats) { - assertNotNull(new PPLSyntaxParser().parse( - String.format("SOURCE=test | eval k = get_format(%s, %s)", type, format))); + assertNotNull( + new PPLSyntaxParser() + .parse(String.format("SOURCE=test | eval k = get_format(%s, %s)", type, format))); } } } @@ -303,24 +393,28 @@ public void testCanParseGetFormatFunction() { @Test public void testCannotParseGetFormatFunctionWithBadArg() { assertThrows( - SyntaxCheckException.class, - () -> new PPLSyntaxParser().parse( - "SOURCE=test | eval k = GET_FORMAT(NONSENSE_ARG,'INTERNAL')")); + SyntaxCheckException.class, + () -> + new PPLSyntaxParser() + .parse("SOURCE=test | eval k = GET_FORMAT(NONSENSE_ARG,'INTERNAL')")); } @Test public void testCanParseTimestampaddFunction() { - assertNotNull(new PPLSyntaxParser().parse( - "SOURCE=test | eval k = TIMESTAMPADD(MINUTE, 1, '2003-01-02')")); - assertNotNull(new PPLSyntaxParser().parse( - "SOURCE=test | eval k = TIMESTAMPADD(WEEK,1,'2003-01-02')")); + assertNotNull( + new PPLSyntaxParser() + .parse("SOURCE=test | eval k = TIMESTAMPADD(MINUTE, 1, '2003-01-02')")); + assertNotNull( + new PPLSyntaxParser().parse("SOURCE=test | eval k = TIMESTAMPADD(WEEK,1,'2003-01-02')")); } @Test public void testCanParseTimestampdiffFunction() { - assertNotNull(new PPLSyntaxParser().parse( - "SOURCE=test | eval k = TIMESTAMPDIFF(MINUTE, '2003-01-02', '2003-01-02')")); - assertNotNull(new PPLSyntaxParser().parse( - "SOURCE=test | eval k = TIMESTAMPDIFF(WEEK,'2003-01-02','2003-01-02')")); + assertNotNull( + new PPLSyntaxParser() + .parse("SOURCE=test | eval k = TIMESTAMPDIFF(MINUTE, '2003-01-02', '2003-01-02')")); + assertNotNull( + new PPLSyntaxParser() + .parse("SOURCE=test | eval k = TIMESTAMPDIFF(WEEK,'2003-01-02','2003-01-02')")); } } diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/domain/PPLQueryRequestTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/domain/PPLQueryRequestTest.java index b53656e252..29e6ff3298 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/domain/PPLQueryRequestTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/domain/PPLQueryRequestTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.domain; import static org.junit.Assert.assertEquals; @@ -16,8 +15,7 @@ public class PPLQueryRequestTest { - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); + @Rule public ExpectedException exceptionRule = ExpectedException.none(); @Test public void getRequestShouldPass() { @@ -27,40 +25,34 @@ public void getRequestShouldPass() { @Test public void testExplainRequest() { - PPLQueryRequest request = new PPLQueryRequest( - "source=t a=1", null, "/_plugins/_ppl/_explain"); + PPLQueryRequest request = new PPLQueryRequest("source=t a=1", null, "/_plugins/_ppl/_explain"); assertTrue(request.isExplainRequest()); } @Test public void testDefaultFormat() { - PPLQueryRequest request = new PPLQueryRequest( - "source=test", null, "/_plugins/_ppl"); + PPLQueryRequest request = new PPLQueryRequest("source=test", null, "/_plugins/_ppl"); assertEquals(request.format(), Format.JDBC); } @Test public void testJDBCFormat() { - PPLQueryRequest request = new PPLQueryRequest( - "source=test", null, "/_plugins/_ppl", "jdbc"); + PPLQueryRequest request = new PPLQueryRequest("source=test", null, "/_plugins/_ppl", "jdbc"); assertEquals(request.format(), Format.JDBC); } @Test public void testCSVFormat() { - PPLQueryRequest request = new PPLQueryRequest( - "source=test", null, "/_plugins/_ppl", "csv"); + PPLQueryRequest request = new PPLQueryRequest("source=test", null, "/_plugins/_ppl", "csv"); assertEquals(request.format(), Format.CSV); } @Test public void testUnsupportedFormat() { String format = "notsupport"; - PPLQueryRequest request = new PPLQueryRequest( - "source=test", null, "/_plugins/_ppl", format); + PPLQueryRequest request = new PPLQueryRequest("source=test", null, "/_plugins/_ppl", format); exceptionRule.expect(IllegalArgumentException.class); exceptionRule.expectMessage("response in " + format + " format is not supported."); request.format(); } - } diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/domain/PPLQueryResponseTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/domain/PPLQueryResponseTest.java index 03eaaf22f4..50be4efa2e 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/domain/PPLQueryResponseTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/domain/PPLQueryResponseTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.domain; import org.junit.Test; diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java index 599f6bdd75..c9989a49c4 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.parser; import static java.util.Collections.emptyList; @@ -62,353 +61,264 @@ public class AstBuilderTest { - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); + @Rule public ExpectedException exceptionRule = ExpectedException.none(); private PPLSyntaxParser parser = new PPLSyntaxParser(); @Test public void testSearchCommand() { - assertEqual("search source=t a=1", - filter( - relation("t"), - compare("=", field("a"), intLiteral(1)) - ) - ); + assertEqual( + "search source=t a=1", filter(relation("t"), compare("=", field("a"), intLiteral(1)))); } @Test public void testSearchCrossClusterCommand() { - assertEqual("search source=c:t", - relation(qualifiedName("c:t")) - ); + assertEqual("search source=c:t", relation(qualifiedName("c:t"))); } @Test public void testSearchMatchAllCrossClusterCommand() { - assertEqual("search source=*:t", - relation(qualifiedName("*:t")) - ); + assertEqual("search source=*:t", relation(qualifiedName("*:t"))); } @Test public void testPrometheusSearchCommand() { - assertEqual("search source = prometheus.http_requests_total", - relation(qualifiedName("prometheus", "http_requests_total")) - ); + assertEqual( + "search source = prometheus.http_requests_total", + relation(qualifiedName("prometheus", "http_requests_total"))); } @Test public void testSearchCommandWithDataSourceEscape() { - assertEqual("search source = `prometheus.http_requests_total`", - relation("prometheus.http_requests_total") - ); + assertEqual( + "search source = `prometheus.http_requests_total`", + relation("prometheus.http_requests_total")); } @Test public void testSearchCommandWithDotInIndexName() { - assertEqual("search source = http_requests_total.test", - relation(qualifiedName("http_requests_total","test")) - ); + assertEqual( + "search source = http_requests_total.test", + relation(qualifiedName("http_requests_total", "test"))); } @Test public void testSearchWithPrometheusQueryRangeWithPositionedArguments() { - assertEqual("search source = prometheus.query_range(\"test{code='200'}\",1234, 12345, 3)", - tableFunction(Arrays.asList("prometheus", "query_range"), + assertEqual( + "search source = prometheus.query_range(\"test{code='200'}\",1234, 12345, 3)", + tableFunction( + Arrays.asList("prometheus", "query_range"), unresolvedArg(null, stringLiteral("test{code='200'}")), unresolvedArg(null, intLiteral(1234)), unresolvedArg(null, intLiteral(12345)), - unresolvedArg(null, intLiteral(3)) - )); + unresolvedArg(null, intLiteral(3)))); } @Test public void testSearchWithPrometheusQueryRangeWithNamedArguments() { - assertEqual("search source = prometheus.query_range(query = \"test{code='200'}\", " + assertEqual( + "search source = prometheus.query_range(query = \"test{code='200'}\", " + "starttime = 1234, step=3, endtime=12345)", - tableFunction(Arrays.asList("prometheus", "query_range"), + tableFunction( + Arrays.asList("prometheus", "query_range"), unresolvedArg("query", stringLiteral("test{code='200'}")), unresolvedArg("starttime", intLiteral(1234)), unresolvedArg("step", intLiteral(3)), - unresolvedArg("endtime", intLiteral(12345)) - )); + unresolvedArg("endtime", intLiteral(12345)))); } @Test public void testSearchCommandString() { - assertEqual("search source=t a=\"a\"", - filter( - relation("t"), - compare("=", field("a"), stringLiteral("a")) - ) - ); + assertEqual( + "search source=t a=\"a\"", + filter(relation("t"), compare("=", field("a"), stringLiteral("a")))); } @Test public void testSearchCommandWithoutSearch() { - assertEqual("source=t a=1", - filter( - relation("t"), - compare("=", field("a"), intLiteral(1)) - ) - ); + assertEqual("source=t a=1", filter(relation("t"), compare("=", field("a"), intLiteral(1)))); } @Test public void testSearchCommandWithFilterBeforeSource() { - assertEqual("search a=1 source=t", - filter( - relation("t"), - compare("=", field("a"), intLiteral(1)) - )); + assertEqual( + "search a=1 source=t", filter(relation("t"), compare("=", field("a"), intLiteral(1)))); } @Test public void testWhereCommand() { - assertEqual("search source=t | where a=1", - filter( - relation("t"), - compare("=", field("a"), intLiteral(1)) - ) - ); + assertEqual( + "search source=t | where a=1", + filter(relation("t"), compare("=", field("a"), intLiteral(1)))); } @Test public void testWhereCommandWithQualifiedName() { - assertEqual("search source=t | where a.v=1", - filter( - relation("t"), - compare("=", field(qualifiedName("a", "v")), intLiteral(1)) - ) - ); + assertEqual( + "search source=t | where a.v=1", + filter(relation("t"), compare("=", field(qualifiedName("a", "v")), intLiteral(1)))); } @Test public void testFieldsCommandWithoutArguments() { - assertEqual("source=t | fields f, g", - projectWithArg( - relation("t"), - defaultFieldsArgs(), - field("f"), field("g") - )); + assertEqual( + "source=t | fields f, g", + projectWithArg(relation("t"), defaultFieldsArgs(), field("f"), field("g"))); } @Test public void testFieldsCommandWithIncludeArguments() { - assertEqual("source=t | fields + f, g", - projectWithArg( - relation("t"), - defaultFieldsArgs(), - field("f"), field("g") - )); + assertEqual( + "source=t | fields + f, g", + projectWithArg(relation("t"), defaultFieldsArgs(), field("f"), field("g"))); } @Test public void testFieldsCommandWithExcludeArguments() { - assertEqual("source=t | fields - f, g", + assertEqual( + "source=t | fields - f, g", projectWithArg( relation("t"), exprList(argument("exclude", booleanLiteral(true))), - field("f"), field("g") - )); + field("f"), + field("g"))); } @Test public void testSearchCommandWithQualifiedName() { - assertEqual("source=t | fields f.v, g.v", + assertEqual( + "source=t | fields f.v, g.v", projectWithArg( relation("t"), defaultFieldsArgs(), - field(qualifiedName("f", "v")), field(qualifiedName("g", "v")) - )); + field(qualifiedName("f", "v")), + field(qualifiedName("g", "v")))); } @Test public void testRenameCommand() { - assertEqual("source=t | rename f as g", - rename( - relation("t"), - map("f", "g") - )); + assertEqual("source=t | rename f as g", rename(relation("t"), map("f", "g"))); } @Test public void testRenameCommandWithMultiFields() { - assertEqual("source=t | rename f as g, h as i, j as k", - rename( - relation("t"), - map("f", "g"), - map("h", "i"), - map("j", "k") - )); + assertEqual( + "source=t | rename f as g, h as i, j as k", + rename(relation("t"), map("f", "g"), map("h", "i"), map("j", "k"))); } @Test public void testStatsCommand() { - assertEqual("source=t | stats count(a)", + assertEqual( + "source=t | stats count(a)", agg( relation("t"), - exprList( - alias( - "count(a)", - aggregate("count", field("a")) - ) - ), + exprList(alias("count(a)", aggregate("count", field("a")))), emptyList(), emptyList(), - defaultStatsArgs() - )); + defaultStatsArgs())); } @Test public void testStatsCommandWithByClause() { - assertEqual("source=t | stats count(a) by b DEDUP_SPLITVALUES=false", + assertEqual( + "source=t | stats count(a) by b DEDUP_SPLITVALUES=false", agg( relation("t"), - exprList( - alias( - "count(a)", - aggregate("count", field("a")) - ) - ), + exprList(alias("count(a)", aggregate("count", field("a")))), emptyList(), - exprList( - alias( - "b", - field("b") - )), - defaultStatsArgs() - )); + exprList(alias("b", field("b"))), + defaultStatsArgs())); } @Test public void testStatsCommandWithByClauseInBackticks() { - assertEqual("source=t | stats count(a) by `b` DEDUP_SPLITVALUES=false", + assertEqual( + "source=t | stats count(a) by `b` DEDUP_SPLITVALUES=false", agg( relation("t"), - exprList( - alias( - "count(a)", - aggregate("count", field("a")) - ) - ), + exprList(alias("count(a)", aggregate("count", field("a")))), emptyList(), - exprList( - alias( - "b", - field("b") - )), - defaultStatsArgs() - )); + exprList(alias("b", field("b"))), + defaultStatsArgs())); } @Test public void testStatsCommandWithAlias() { - assertEqual("source=t | stats count(a) as alias", + assertEqual( + "source=t | stats count(a) as alias", agg( relation("t"), - exprList( - alias( - "alias", - aggregate("count", field("a")) - ) - ), + exprList(alias("alias", aggregate("count", field("a")))), emptyList(), emptyList(), - defaultStatsArgs() - ) - ); + defaultStatsArgs())); } @Test public void testStatsCommandWithNestedFunctions() { - assertEqual("source=t | stats sum(a+b)", + assertEqual( + "source=t | stats sum(a+b)", agg( relation("t"), - exprList( - alias( - "sum(a+b)", - aggregate( - "sum", - function("+", field("a"), field("b")) - )) - ), + exprList(alias("sum(a+b)", aggregate("sum", function("+", field("a"), field("b"))))), emptyList(), emptyList(), - defaultStatsArgs() - )); - assertEqual("source=t | stats sum(abs(a)/2)", + defaultStatsArgs())); + assertEqual( + "source=t | stats sum(abs(a)/2)", agg( relation("t"), exprList( alias( "sum(abs(a)/2)", - aggregate( - "sum", - function( - "/", - function("abs", field("a")), - intLiteral(2) - ) - ) - ) - ), + aggregate("sum", function("/", function("abs", field("a")), intLiteral(2))))), emptyList(), emptyList(), - defaultStatsArgs() - )); + defaultStatsArgs())); } @Test public void testStatsCommandWithSpan() { - assertEqual("source=t | stats avg(price) by span(timestamp, 1h)", + assertEqual( + "source=t | stats avg(price) by span(timestamp, 1h)", agg( relation("t"), - exprList( - alias("avg(price)", aggregate("avg", field("price"))) - ), + exprList(alias("avg(price)", aggregate("avg", field("price")))), emptyList(), emptyList(), alias("span(timestamp,1h)", span(field("timestamp"), intLiteral(1), SpanUnit.H)), - defaultStatsArgs() - )); + defaultStatsArgs())); - assertEqual("source=t | stats count(a) by span(age, 10)", + assertEqual( + "source=t | stats count(a) by span(age, 10)", agg( relation("t"), - exprList( - alias("count(a)", aggregate("count", field("a"))) - ), + exprList(alias("count(a)", aggregate("count", field("a")))), emptyList(), emptyList(), alias("span(age,10)", span(field("age"), intLiteral(10), SpanUnit.NONE)), - defaultStatsArgs() - )); + defaultStatsArgs())); - assertEqual("source=t | stats avg(price) by span(timestamp, 1h), b", + assertEqual( + "source=t | stats avg(price) by span(timestamp, 1h), b", agg( relation("t"), - exprList( - alias("avg(price)", aggregate("avg", field("price"))) - ), + exprList(alias("avg(price)", aggregate("avg", field("price")))), emptyList(), exprList(alias("b", field("b"))), alias("span(timestamp,1h)", span(field("timestamp"), intLiteral(1), SpanUnit.H)), - defaultStatsArgs() - )); + defaultStatsArgs())); - assertEqual("source=t | stats avg(price) by span(timestamp, 1h), f1, f2", + assertEqual( + "source=t | stats avg(price) by span(timestamp, 1h), f1, f2", agg( relation("t"), - exprList( - alias("avg(price)", aggregate("avg", field("price"))) - ), + exprList(alias("avg(price)", aggregate("avg", field("price")))), emptyList(), exprList(alias("f1", field("f1")), alias("f2", field("f2"))), alias("span(timestamp,1h)", span(field("timestamp"), intLiteral(1), SpanUnit.H)), - defaultStatsArgs() - )); + defaultStatsArgs())); } @Test(expected = org.opensearch.sql.common.antlr.SyntaxCheckException.class) @@ -423,152 +333,128 @@ public void throwExceptionWithEmptyGroupByList() { @Test public void testStatsSpanWithAlias() { - assertEqual("source=t | stats avg(price) by span(timestamp, 1h) as time_span", + assertEqual( + "source=t | stats avg(price) by span(timestamp, 1h) as time_span", agg( relation("t"), - exprList( - alias("avg(price)", aggregate("avg", field("price"))) - ), + exprList(alias("avg(price)", aggregate("avg", field("price")))), emptyList(), emptyList(), - alias("span(timestamp,1h)", span( - field("timestamp"), intLiteral(1), SpanUnit.H), "time_span"), - defaultStatsArgs() - )); + alias( + "span(timestamp,1h)", + span(field("timestamp"), intLiteral(1), SpanUnit.H), + "time_span"), + defaultStatsArgs())); - assertEqual("source=t | stats count(a) by span(age, 10) as numeric_span", + assertEqual( + "source=t | stats count(a) by span(age, 10) as numeric_span", agg( relation("t"), - exprList( - alias("count(a)", aggregate("count", field("a"))) - ), + exprList(alias("count(a)", aggregate("count", field("a")))), emptyList(), emptyList(), - alias("span(age,10)", span( - field("age"), intLiteral(10), SpanUnit.NONE), "numeric_span"), - defaultStatsArgs() - )); + alias( + "span(age,10)", span(field("age"), intLiteral(10), SpanUnit.NONE), "numeric_span"), + defaultStatsArgs())); } @Test public void testDedupCommand() { - assertEqual("source=t | dedup f1, f2", - dedupe( - relation("t"), - defaultDedupArgs(), - field("f1"), field("f2") - )); + assertEqual( + "source=t | dedup f1, f2", + dedupe(relation("t"), defaultDedupArgs(), field("f1"), field("f2"))); } - /** - * disable sortby from the dedup command syntax. - */ + /** disable sortby from the dedup command syntax. */ @Ignore(value = "disable sortby from the dedup command syntax") public void testDedupCommandWithSortby() { - assertEqual("source=t | dedup f1, f2 sortby f3", + assertEqual( + "source=t | dedup f1, f2 sortby f3", agg( relation("t"), exprList(field("f1"), field("f2")), exprList(field("f3", defaultSortFieldArgs())), null, - defaultDedupArgs() - )); + defaultDedupArgs())); } @Test public void testHeadCommand() { - assertEqual("source=t | head", - head(relation("t"), 10, 0)); + assertEqual("source=t | head", head(relation("t"), 10, 0)); } @Test public void testHeadCommandWithNumber() { - assertEqual("source=t | head 3", - head(relation("t"), 3, 0)); + assertEqual("source=t | head 3", head(relation("t"), 3, 0)); } @Test public void testHeadCommandWithNumberAndOffset() { - assertEqual("source=t | head 3 from 4", - head(relation("t"), 3, 4)); + assertEqual("source=t | head 3 from 4", head(relation("t"), 3, 4)); } @Test public void testSortCommand() { - assertEqual("source=t | sort f1, f2", + assertEqual( + "source=t | sort f1, f2", sort( relation("t"), field("f1", defaultSortFieldArgs()), - field("f2", defaultSortFieldArgs()) - )); + field("f2", defaultSortFieldArgs()))); } @Test public void testSortCommandWithOptions() { - assertEqual("source=t | sort - f1, + f2", + assertEqual( + "source=t | sort - f1, + f2", sort( relation("t"), - field("f1", exprList(argument("asc", booleanLiteral(false)), - argument("type", nullLiteral()))), - field("f2", defaultSortFieldArgs()) - )); + field( + "f1", + exprList(argument("asc", booleanLiteral(false)), argument("type", nullLiteral()))), + field("f2", defaultSortFieldArgs()))); } @Test public void testEvalCommand() { - assertEqual("source=t | eval r=abs(f)", - eval( - relation("t"), - let( - field("r"), - function("abs", field("f")) - ) - )); + assertEqual( + "source=t | eval r=abs(f)", + eval(relation("t"), let(field("r"), function("abs", field("f"))))); } @Test public void testIndexName() { - assertEqual("source=`log.2020.04.20.` a=1", - filter( - relation("log.2020.04.20."), - compare("=", field("a"), intLiteral(1)) - )); - assertEqual("describe `log.2020.04.20.`", - relation(mappingTable("log.2020.04.20."))); + assertEqual( + "source=`log.2020.04.20.` a=1", + filter(relation("log.2020.04.20."), compare("=", field("a"), intLiteral(1)))); + assertEqual("describe `log.2020.04.20.`", relation(mappingTable("log.2020.04.20."))); } @Test public void testIdentifierAsIndexNameStartWithDot() { - assertEqual("source=.opensearch_dashboards", - relation(".opensearch_dashboards")); - assertEqual("describe .opensearch_dashboards", - relation(mappingTable(".opensearch_dashboards"))); + assertEqual("source=.opensearch_dashboards", relation(".opensearch_dashboards")); + assertEqual( + "describe .opensearch_dashboards", relation(mappingTable(".opensearch_dashboards"))); } @Test public void testIdentifierAsIndexNameWithDotInTheMiddle() { assertEqual("source=log.2020.10.10", relation("log.2020.10.10")); assertEqual("source=log-7.10-2020.10.10", relation("log-7.10-2020.10.10")); - assertEqual("describe log.2020.10.10", - relation(mappingTable("log.2020.10.10"))); - assertEqual("describe log-7.10-2020.10.10", - relation(mappingTable("log-7.10-2020.10.10"))); + assertEqual("describe log.2020.10.10", relation(mappingTable("log.2020.10.10"))); + assertEqual("describe log-7.10-2020.10.10", relation(mappingTable("log-7.10-2020.10.10"))); } @Test public void testIdentifierAsIndexNameWithSlashInTheMiddle() { - assertEqual("source=log-2020", - relation("log-2020")); - assertEqual("describe log-2020", - relation(mappingTable("log-2020"))); + assertEqual("source=log-2020", relation("log-2020")); + assertEqual("describe log-2020", relation(mappingTable("log-2020"))); } @Test public void testIdentifierAsIndexNameContainStar() { - assertEqual("source=log-2020-10-*", - relation("log-2020-10-*")); - assertEqual("describe log-2020-10-*", - relation(mappingTable("log-2020-10-*"))); + assertEqual("source=log-2020-10-*", relation("log-2020-10-*")); + assertEqual("describe log-2020-10-*", relation(mappingTable("log-2020-10-*"))); } @Test @@ -576,138 +462,132 @@ public void testIdentifierAsIndexNameContainStarAndDots() { assertEqual("source=log-2020.10.*", relation("log-2020.10.*")); assertEqual("source=log-2020.*.01", relation("log-2020.*.01")); assertEqual("source=log-2020.*.*", relation("log-2020.*.*")); - assertEqual("describe log-2020.10.*", - relation(mappingTable("log-2020.10.*"))); - assertEqual("describe log-2020.*.01", - relation(mappingTable("log-2020.*.01"))); - assertEqual("describe log-2020.*.*", - relation(mappingTable("log-2020.*.*"))); + assertEqual("describe log-2020.10.*", relation(mappingTable("log-2020.10.*"))); + assertEqual("describe log-2020.*.01", relation(mappingTable("log-2020.*.01"))); + assertEqual("describe log-2020.*.*", relation(mappingTable("log-2020.*.*"))); } @Test public void testIdentifierAsFieldNameStartWithAt() { - assertEqual("source=log-2020 | fields @timestamp", - projectWithArg( - relation("log-2020"), - defaultFieldsArgs(), - field("@timestamp") - )); + assertEqual( + "source=log-2020 | fields @timestamp", + projectWithArg(relation("log-2020"), defaultFieldsArgs(), field("@timestamp"))); } @Test public void testRareCommand() { - assertEqual("source=t | rare a", + assertEqual( + "source=t | rare a", rareTopN( relation("t"), CommandType.RARE, exprList(argument("noOfResults", intLiteral(10))), emptyList(), - field("a") - )); + field("a"))); } @Test public void testRareCommandWithGroupBy() { - assertEqual("source=t | rare a by b", + assertEqual( + "source=t | rare a by b", rareTopN( relation("t"), CommandType.RARE, exprList(argument("noOfResults", intLiteral(10))), exprList(field("b")), - field("a") - )); + field("a"))); } @Test public void testRareCommandWithMultipleFields() { - assertEqual("source=t | rare `a`, `b` by `c`", + assertEqual( + "source=t | rare `a`, `b` by `c`", rareTopN( relation("t"), CommandType.RARE, exprList(argument("noOfResults", intLiteral(10))), exprList(field("c")), field("a"), - field("b") - )); + field("b"))); } @Test public void testTopCommandWithN() { - assertEqual("source=t | top 1 a", + assertEqual( + "source=t | top 1 a", rareTopN( relation("t"), CommandType.TOP, exprList(argument("noOfResults", intLiteral(1))), emptyList(), - field("a") - )); + field("a"))); } @Test public void testTopCommandWithoutNAndGroupBy() { - assertEqual("source=t | top a", + assertEqual( + "source=t | top a", rareTopN( relation("t"), CommandType.TOP, exprList(argument("noOfResults", intLiteral(10))), emptyList(), - field("a") - )); + field("a"))); } @Test public void testTopCommandWithNAndGroupBy() { - assertEqual("source=t | top 1 a by b", + assertEqual( + "source=t | top 1 a by b", rareTopN( relation("t"), CommandType.TOP, exprList(argument("noOfResults", intLiteral(1))), exprList(field("b")), - field("a") - )); + field("a"))); } @Test public void testTopCommandWithMultipleFields() { - assertEqual("source=t | top 1 `a`, `b` by `c`", + assertEqual( + "source=t | top 1 `a`, `b` by `c`", rareTopN( relation("t"), CommandType.TOP, exprList(argument("noOfResults", intLiteral(1))), exprList(field("c")), field("a"), - field("b") - )); + field("b"))); } @Test public void testGrokCommand() { - assertEqual("source=t | grok raw \"pattern\"", + assertEqual( + "source=t | grok raw \"pattern\"", parse( relation("t"), ParseMethod.GROK, field("raw"), stringLiteral("pattern"), - ImmutableMap.of() - )); + ImmutableMap.of())); } @Test public void testParseCommand() { - assertEqual("source=t | parse raw \"pattern\"", + assertEqual( + "source=t | parse raw \"pattern\"", parse( relation("t"), ParseMethod.REGEX, field("raw"), stringLiteral("pattern"), - ImmutableMap.of() - )); + ImmutableMap.of())); } @Test public void testPatternsCommand() { - assertEqual("source=t | patterns new_field=\"custom_field\" " - + "pattern=\"custom_pattern\" raw", + assertEqual( + "source=t | patterns new_field=\"custom_field\" " + "pattern=\"custom_pattern\" raw", parse( relation("t"), ParseMethod.PATTERNS, @@ -716,8 +596,7 @@ public void testPatternsCommand() { ImmutableMap.builder() .put("new_field", stringLiteral("custom_field")) .put("pattern", stringLiteral("custom_pattern")) - .build() - )); + .build())); } @Test @@ -734,114 +613,118 @@ public void testPatternsCommandWithoutArguments() { @Test public void testKmeansCommand() { - assertEqual("source=t | kmeans centroids=3 iterations=2 distance_type='l1'", - new Kmeans(relation("t"), ImmutableMap.builder() - .put("centroids", new Literal(3, DataType.INTEGER)) - .put("iterations", new Literal(2, DataType.INTEGER)) - .put("distance_type", new Literal("l1", DataType.STRING)) - .build() - )); + assertEqual( + "source=t | kmeans centroids=3 iterations=2 distance_type='l1'", + new Kmeans( + relation("t"), + ImmutableMap.builder() + .put("centroids", new Literal(3, DataType.INTEGER)) + .put("iterations", new Literal(2, DataType.INTEGER)) + .put("distance_type", new Literal("l1", DataType.STRING)) + .build())); } @Test public void testKmeansCommandWithoutParameter() { - assertEqual("source=t | kmeans", - new Kmeans(relation("t"), ImmutableMap.of())); + assertEqual("source=t | kmeans", new Kmeans(relation("t"), ImmutableMap.of())); } @Test public void testMLCommand() { - assertEqual("source=t | ml action='trainandpredict' " - + "algorithm='kmeans' centroid=3 iteration=2 dist_type='l1'", - new ML(relation("t"), ImmutableMap.builder() - .put("action", new Literal("trainandpredict", DataType.STRING)) - .put("algorithm", new Literal("kmeans", DataType.STRING)) - .put("centroid", new Literal(3, DataType.INTEGER)) - .put("iteration", new Literal(2, DataType.INTEGER)) - .put("dist_type", new Literal("l1", DataType.STRING)) - .build() - )); + assertEqual( + "source=t | ml action='trainandpredict' " + + "algorithm='kmeans' centroid=3 iteration=2 dist_type='l1'", + new ML( + relation("t"), + ImmutableMap.builder() + .put("action", new Literal("trainandpredict", DataType.STRING)) + .put("algorithm", new Literal("kmeans", DataType.STRING)) + .put("centroid", new Literal(3, DataType.INTEGER)) + .put("iteration", new Literal(2, DataType.INTEGER)) + .put("dist_type", new Literal("l1", DataType.STRING)) + .build())); } @Test public void testDescribeCommand() { - assertEqual("describe t", - relation(mappingTable("t"))); + assertEqual("describe t", relation(mappingTable("t"))); } @Test public void testDescribeMatchAllCrossClusterSearchCommand() { - assertEqual("describe *:t", - relation(mappingTable("*:t"))); + assertEqual("describe *:t", relation(mappingTable("*:t"))); } @Test public void testDescribeCommandWithMultipleIndices() { - assertEqual("describe t,u", - relation(mappingTable("t,u"))); + assertEqual("describe t,u", relation(mappingTable("t,u"))); } @Test public void testDescribeCommandWithFullyQualifiedTableName() { - assertEqual("describe prometheus.http_metric", + assertEqual( + "describe prometheus.http_metric", relation(qualifiedName("prometheus", mappingTable("http_metric")))); - assertEqual("describe prometheus.schema.http_metric", + assertEqual( + "describe prometheus.schema.http_metric", relation(qualifiedName("prometheus", "schema", mappingTable("http_metric")))); } @Test public void test_fitRCFADCommand_withoutDataFormat() { - assertEqual("source=t | AD shingle_size=10 time_decay=0.0001 time_field='timestamp' " + assertEqual( + "source=t | AD shingle_size=10 time_decay=0.0001 time_field='timestamp' " + "anomaly_rate=0.1 anomaly_score_threshold=0.1 sample_size=256 " + "number_of_trees=256 time_zone='PST' output_after=256 " + "training_data_size=256", - new AD(relation("t"), ImmutableMap.builder() - .put("anomaly_rate", new Literal(0.1, DataType.DOUBLE)) - .put("anomaly_score_threshold", new Literal(0.1, DataType.DOUBLE)) - .put("sample_size", new Literal(256, DataType.INTEGER)) - .put("number_of_trees", new Literal(256, DataType.INTEGER)) - .put("time_zone", new Literal("PST", DataType.STRING)) - .put("output_after", new Literal(256, DataType.INTEGER)) - .put("shingle_size", new Literal(10, DataType.INTEGER)) - .put("time_decay", new Literal(0.0001, DataType.DOUBLE)) - .put("time_field", new Literal("timestamp", DataType.STRING)) - .put("training_data_size", new Literal(256, DataType.INTEGER)) - .build() - )); + new AD( + relation("t"), + ImmutableMap.builder() + .put("anomaly_rate", new Literal(0.1, DataType.DOUBLE)) + .put("anomaly_score_threshold", new Literal(0.1, DataType.DOUBLE)) + .put("sample_size", new Literal(256, DataType.INTEGER)) + .put("number_of_trees", new Literal(256, DataType.INTEGER)) + .put("time_zone", new Literal("PST", DataType.STRING)) + .put("output_after", new Literal(256, DataType.INTEGER)) + .put("shingle_size", new Literal(10, DataType.INTEGER)) + .put("time_decay", new Literal(0.0001, DataType.DOUBLE)) + .put("time_field", new Literal("timestamp", DataType.STRING)) + .put("training_data_size", new Literal(256, DataType.INTEGER)) + .build())); } @Test public void test_fitRCFADCommand_withDataFormat() { - assertEqual("source=t | AD shingle_size=10 time_decay=0.0001 time_field='timestamp' " + assertEqual( + "source=t | AD shingle_size=10 time_decay=0.0001 time_field='timestamp' " + "anomaly_rate=0.1 anomaly_score_threshold=0.1 sample_size=256 " + "number_of_trees=256 time_zone='PST' output_after=256 " + "training_data_size=256 date_format='HH:mm:ss yyyy-MM-dd'", - new AD(relation("t"), ImmutableMap.builder() - .put("anomaly_rate", new Literal(0.1, DataType.DOUBLE)) - .put("anomaly_score_threshold", new Literal(0.1, DataType.DOUBLE)) - .put("sample_size", new Literal(256, DataType.INTEGER)) - .put("number_of_trees", new Literal(256, DataType.INTEGER)) - .put("date_format", new Literal("HH:mm:ss yyyy-MM-dd", DataType.STRING)) - .put("time_zone", new Literal("PST", DataType.STRING)) - .put("output_after", new Literal(256, DataType.INTEGER)) - .put("shingle_size", new Literal(10, DataType.INTEGER)) - .put("time_decay", new Literal(0.0001, DataType.DOUBLE)) - .put("time_field", new Literal("timestamp", DataType.STRING)) - .put("training_data_size", new Literal(256, DataType.INTEGER)) - .build() - )); + new AD( + relation("t"), + ImmutableMap.builder() + .put("anomaly_rate", new Literal(0.1, DataType.DOUBLE)) + .put("anomaly_score_threshold", new Literal(0.1, DataType.DOUBLE)) + .put("sample_size", new Literal(256, DataType.INTEGER)) + .put("number_of_trees", new Literal(256, DataType.INTEGER)) + .put("date_format", new Literal("HH:mm:ss yyyy-MM-dd", DataType.STRING)) + .put("time_zone", new Literal("PST", DataType.STRING)) + .put("output_after", new Literal(256, DataType.INTEGER)) + .put("shingle_size", new Literal(10, DataType.INTEGER)) + .put("time_decay", new Literal(0.0001, DataType.DOUBLE)) + .put("time_field", new Literal("timestamp", DataType.STRING)) + .put("training_data_size", new Literal(256, DataType.INTEGER)) + .build())); } @Test public void test_batchRCFADCommand() { - assertEqual("source=t | AD", - new AD(relation("t"), ImmutableMap.of())); + assertEqual("source=t | AD", new AD(relation("t"), ImmutableMap.of())); } @Test public void testShowDataSourcesCommand() { - assertEqual("show datasources", - relation(DATASOURCES_TABLE_NAME)); + assertEqual("show datasources", relation(DATASOURCES_TABLE_NAME)); } protected void assertEqual(String query, Node expectedPlan) { diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java index 8472e61361..c549a20f3e 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.parser; import static java.util.Collections.emptyList; @@ -58,464 +57,313 @@ public class AstExpressionBuilderTest extends AstBuilderTest { @Test public void testLogicalNotExpr() { - assertEqual("source=t not a=1", - filter( - relation("t"), - not( - compare("=", field("a"), intLiteral(1)) - ) - )); + assertEqual( + "source=t not a=1", filter(relation("t"), not(compare("=", field("a"), intLiteral(1))))); } @Test public void testLogicalOrExpr() { - assertEqual("source=t a=1 or b=2", + assertEqual( + "source=t a=1 or b=2", filter( relation("t"), - or( - compare("=", field("a"), intLiteral(1)), - compare("=", field("b"), intLiteral(2)) - ) - )); + or(compare("=", field("a"), intLiteral(1)), compare("=", field("b"), intLiteral(2))))); } @Test public void testLogicalAndExpr() { - assertEqual("source=t a=1 and b=2", + assertEqual( + "source=t a=1 and b=2", filter( relation("t"), - and( - compare("=", field("a"), intLiteral(1)), - compare("=", field("b"), intLiteral(2)) - ) - )); + and(compare("=", field("a"), intLiteral(1)), compare("=", field("b"), intLiteral(2))))); } @Test public void testLogicalAndExprWithoutKeywordAnd() { - assertEqual("source=t a=1 b=2", + assertEqual( + "source=t a=1 b=2", filter( relation("t"), - and( - compare("=", field("a"), intLiteral(1)), - compare("=", field("b"), intLiteral(2)) - ) - )); + and(compare("=", field("a"), intLiteral(1)), compare("=", field("b"), intLiteral(2))))); } @Test public void testLogicalXorExpr() { - assertEqual("source=t a=1 xor b=2", + assertEqual( + "source=t a=1 xor b=2", filter( relation("t"), - xor( - compare("=", field("a"), intLiteral(1)), - compare("=", field("b"), intLiteral(2)) - ) - )); + xor(compare("=", field("a"), intLiteral(1)), compare("=", field("b"), intLiteral(2))))); } @Test public void testLogicalLikeExpr() { - assertEqual("source=t like(a, '_a%b%c_d_')", - filter( - relation("t"), - function("like", field("a"), stringLiteral("_a%b%c_d_")) - )); + assertEqual( + "source=t like(a, '_a%b%c_d_')", + filter(relation("t"), function("like", field("a"), stringLiteral("_a%b%c_d_")))); } @Test public void testBooleanIsNullFunction() { - assertEqual("source=t isnull(a)", - filter( - relation("t"), - function("is null", field("a")) - )); + assertEqual("source=t isnull(a)", filter(relation("t"), function("is null", field("a")))); } @Test public void testBooleanIsNotNullFunction() { - assertEqual("source=t isnotnull(a)", - filter( - relation("t"), - function("is not null", field("a")) - )); + assertEqual( + "source=t isnotnull(a)", filter(relation("t"), function("is not null", field("a")))); } - /** - * Todo. search operator should not include functionCall, need to change antlr. - */ + /** Todo. search operator should not include functionCall, need to change antlr. */ @Ignore("search operator should not include functionCall, need to change antlr") public void testEvalExpr() { - assertEqual("source=t f=abs(a)", - filter( - relation("t"), - equalTo( - field("f"), - function("abs", field("a")) - ) - )); + assertEqual( + "source=t f=abs(a)", + filter(relation("t"), equalTo(field("f"), function("abs", field("a"))))); } @Test public void testEvalFunctionExpr() { - assertEqual("source=t | eval f=abs(a)", - eval( - relation("t"), - let( - field("f"), - function("abs", field("a")) - ) - )); + assertEqual( + "source=t | eval f=abs(a)", + eval(relation("t"), let(field("f"), function("abs", field("a"))))); } @Test public void testEvalFunctionExprNoArgs() { - assertEqual("source=t | eval f=PI()", - eval( - relation("t"), - let( - field("f"), - function("PI") - ) - )); + assertEqual("source=t | eval f=PI()", eval(relation("t"), let(field("f"), function("PI")))); } @Test public void testPositionFunctionExpr() { - assertEqual("source=t | eval f=position('substr' IN 'str')", + assertEqual( + "source=t | eval f=position('substr' IN 'str')", eval( relation("t"), - let( - field("f"), - function("position", - stringLiteral("substr"), stringLiteral("str")) - ) - )); + let(field("f"), function("position", stringLiteral("substr"), stringLiteral("str"))))); } @Test public void testEvalBinaryOperationExpr() { - assertEqual("source=t | eval f=a+b", - eval( - relation("t"), - let( - field("f"), - function("+", field("a"), field("b")) - ) - )); - assertEqual("source=t | eval f=(a+b)", - eval( - relation("t"), - let( - field("f"), - function("+", field("a"), field("b")) - ) - )); + assertEqual( + "source=t | eval f=a+b", + eval(relation("t"), let(field("f"), function("+", field("a"), field("b"))))); + assertEqual( + "source=t | eval f=(a+b)", + eval(relation("t"), let(field("f"), function("+", field("a"), field("b"))))); } @Test public void testLiteralValueBinaryOperationExpr() { - assertEqual("source=t | eval f=3+2", - eval( - relation("t"), - let( - field("f"), - function("+", intLiteral(3), intLiteral(2)) - ) - )); + assertEqual( + "source=t | eval f=3+2", + eval(relation("t"), let(field("f"), function("+", intLiteral(3), intLiteral(2))))); } @Test public void testBinaryOperationExprWithParentheses() { - assertEqual("source = t | where a = (1 + 2) * 3", + assertEqual( + "source = t | where a = (1 + 2) * 3", filter( relation("t"), - compare("=", + compare( + "=", field("a"), - function("*", - function("+", intLiteral(1), intLiteral(2)), - intLiteral(3))))); + function("*", function("+", intLiteral(1), intLiteral(2)), intLiteral(3))))); } @Test public void testBinaryOperationExprPrecedence() { - assertEqual("source = t | where a = 1 + 2 * 3", + assertEqual( + "source = t | where a = 1 + 2 * 3", filter( relation("t"), - compare("=", + compare( + "=", field("a"), - function("+", - intLiteral(1), - function("*", intLiteral(2), intLiteral(3)))))); + function("+", intLiteral(1), function("*", intLiteral(2), intLiteral(3)))))); } @Test public void testCompareExpr() { - assertEqual("source=t a='b'", - filter( - relation("t"), - compare("=", field("a"), stringLiteral("b")) - )); + assertEqual( + "source=t a='b'", filter(relation("t"), compare("=", field("a"), stringLiteral("b")))); } @Test public void testCompareFieldsExpr() { - assertEqual("source=t a>b", - filter( - relation("t"), - compare(">", field("a"), field("b")) - )); + assertEqual("source=t a>b", filter(relation("t"), compare(">", field("a"), field("b")))); } @Test public void testInExpr() { - assertEqual("source=t f in (1, 2, 3)", - filter( - relation("t"), - in( - field("f"), - intLiteral(1), intLiteral(2), intLiteral(3)) - )); + assertEqual( + "source=t f in (1, 2, 3)", + filter(relation("t"), in(field("f"), intLiteral(1), intLiteral(2), intLiteral(3)))); } @Test public void testFieldExpr() { - assertEqual("source=t | sort + f", - sort( - relation("t"), - field("f", defaultSortFieldArgs()) - )); + assertEqual("source=t | sort + f", sort(relation("t"), field("f", defaultSortFieldArgs()))); } @Test public void testSortFieldWithMinusKeyword() { - assertEqual("source=t | sort - f", + assertEqual( + "source=t | sort - f", sort( relation("t"), - field( - "f", - argument("asc", booleanLiteral(false)), - argument("type", nullLiteral()) - ) - )); + field("f", argument("asc", booleanLiteral(false)), argument("type", nullLiteral())))); } @Test public void testSortFieldWithBackticks() { - assertEqual("source=t | sort `f`", - sort( - relation("t"), - field("f", defaultSortFieldArgs()) - )); + assertEqual("source=t | sort `f`", sort(relation("t"), field("f", defaultSortFieldArgs()))); } @Test public void testSortFieldWithAutoKeyword() { - assertEqual("source=t | sort auto(f)", + assertEqual( + "source=t | sort auto(f)", sort( relation("t"), field( "f", argument("asc", booleanLiteral(true)), - argument("type", stringLiteral("auto")) - ) - )); + argument("type", stringLiteral("auto"))))); } @Test public void testSortFieldWithIpKeyword() { - assertEqual("source=t | sort ip(f)", + assertEqual( + "source=t | sort ip(f)", sort( relation("t"), field( "f", argument("asc", booleanLiteral(true)), - argument("type", stringLiteral("ip")) - ) - )); + argument("type", stringLiteral("ip"))))); } @Test public void testSortFieldWithNumKeyword() { - assertEqual("source=t | sort num(f)", + assertEqual( + "source=t | sort num(f)", sort( relation("t"), field( "f", argument("asc", booleanLiteral(true)), - argument("type", stringLiteral("num")) - ) - )); + argument("type", stringLiteral("num"))))); } @Test public void testSortFieldWithStrKeyword() { - assertEqual("source=t | sort str(f)", + assertEqual( + "source=t | sort str(f)", sort( relation("t"), field( "f", argument("asc", booleanLiteral(true)), - argument("type", stringLiteral("str")) - ) - )); + argument("type", stringLiteral("str"))))); } @Test public void testAggFuncCallExpr() { - assertEqual("source=t | stats avg(a) by b", + assertEqual( + "source=t | stats avg(a) by b", agg( relation("t"), - exprList( - alias( - "avg(a)", - aggregate("avg", field("a")) - ) - ), + exprList(alias("avg(a)", aggregate("avg", field("a")))), emptyList(), - exprList( - alias( - "b", - field("b") - )), - defaultStatsArgs() - )); + exprList(alias("b", field("b"))), + defaultStatsArgs())); } @Test public void testVarAggregationShouldPass() { - assertEqual("source=t | stats var_samp(a) by b", + assertEqual( + "source=t | stats var_samp(a) by b", agg( relation("t"), - exprList( - alias( - "var_samp(a)", - aggregate("var_samp", field("a")) - ) - ), + exprList(alias("var_samp(a)", aggregate("var_samp", field("a")))), emptyList(), - exprList( - alias( - "b", - field("b") - )), - defaultStatsArgs() - )); + exprList(alias("b", field("b"))), + defaultStatsArgs())); } @Test public void testVarpAggregationShouldPass() { - assertEqual("source=t | stats var_pop(a) by b", + assertEqual( + "source=t | stats var_pop(a) by b", agg( relation("t"), - exprList( - alias( - "var_pop(a)", - aggregate("var_pop", field("a")) - ) - ), + exprList(alias("var_pop(a)", aggregate("var_pop", field("a")))), emptyList(), - exprList( - alias( - "b", - field("b") - )), - defaultStatsArgs() - )); + exprList(alias("b", field("b"))), + defaultStatsArgs())); } @Test public void testStdDevAggregationShouldPass() { - assertEqual("source=t | stats stddev_samp(a) by b", + assertEqual( + "source=t | stats stddev_samp(a) by b", agg( relation("t"), - exprList( - alias( - "stddev_samp(a)", - aggregate("stddev_samp", field("a")) - ) - ), + exprList(alias("stddev_samp(a)", aggregate("stddev_samp", field("a")))), emptyList(), - exprList( - alias( - "b", - field("b") - )), - defaultStatsArgs() - )); + exprList(alias("b", field("b"))), + defaultStatsArgs())); } @Test public void testStdDevPAggregationShouldPass() { - assertEqual("source=t | stats stddev_pop(a) by b", + assertEqual( + "source=t | stats stddev_pop(a) by b", agg( relation("t"), - exprList( - alias( - "stddev_pop(a)", - aggregate("stddev_pop", field("a")) - ) - ), + exprList(alias("stddev_pop(a)", aggregate("stddev_pop", field("a")))), emptyList(), - exprList( - alias( - "b", - field("b") - )), - defaultStatsArgs() - )); + exprList(alias("b", field("b"))), + defaultStatsArgs())); } @Test public void testPercentileAggFuncExpr() { - assertEqual("source=t | stats percentile<1>(a)", + assertEqual( + "source=t | stats percentile<1>(a)", agg( relation("t"), exprList( - alias("percentile<1>(a)", - aggregate( - "percentile", - field("a"), - argument("rank", intLiteral(1)) - ) - ) - ), + alias( + "percentile<1>(a)", + aggregate("percentile", field("a"), argument("rank", intLiteral(1))))), emptyList(), emptyList(), - defaultStatsArgs() - )); + defaultStatsArgs())); } @Test public void testCountFuncCallExpr() { - assertEqual("source=t | stats count() by b", + assertEqual( + "source=t | stats count() by b", agg( relation("t"), - exprList( - alias( - "count()", - aggregate("count", AllFields.of()) - ) - ), + exprList(alias("count()", aggregate("count", AllFields.of()))), emptyList(), - exprList( - alias( - "b", - field("b") - )), - defaultStatsArgs() - )); + exprList(alias("b", field("b"))), + defaultStatsArgs())); } @Test public void testDistinctCount() { - assertEqual("source=t | stats distinct_count(a)", + assertEqual( + "source=t | stats distinct_count(a)", agg( relation("t"), - exprList( - alias("distinct_count(a)", - distinctAggregate("count", field("a")))), + exprList(alias("distinct_count(a)", distinctAggregate("count", field("a")))), emptyList(), emptyList(), defaultStatsArgs())); @@ -523,168 +371,114 @@ public void testDistinctCount() { @Test public void testTakeAggregationNoArgsShouldPass() { - assertEqual("source=t | stats take(a)", + assertEqual( + "source=t | stats take(a)", agg( relation("t"), - exprList(alias("take(a)", - aggregate("take", field("a"), unresolvedArg("size", intLiteral(10))))), + exprList( + alias( + "take(a)", + aggregate("take", field("a"), unresolvedArg("size", intLiteral(10))))), emptyList(), emptyList(), - defaultStatsArgs() - )); + defaultStatsArgs())); } @Test public void testTakeAggregationWithArgsShouldPass() { - assertEqual("source=t | stats take(a, 5)", + assertEqual( + "source=t | stats take(a, 5)", agg( relation("t"), - exprList(alias("take(a, 5)", - aggregate("take", field("a"), unresolvedArg("size", intLiteral(5))))), + exprList( + alias( + "take(a, 5)", + aggregate("take", field("a"), unresolvedArg("size", intLiteral(5))))), emptyList(), emptyList(), - defaultStatsArgs() - )); + defaultStatsArgs())); } - @Test public void testEvalFuncCallExpr() { - assertEqual("source=t | eval f=abs(a)", - eval( - relation("t"), - let( - field("f"), - function("abs", field("a")) - ) - )); + assertEqual( + "source=t | eval f=abs(a)", + eval(relation("t"), let(field("f"), function("abs", field("a"))))); } @Test public void testDataTypeFuncCall() { - assertEqual("source=t | eval f=cast(1 as string)", - eval( - relation("t"), - let( - field("f"), - cast(intLiteral(1), stringLiteral("string")) - ) - )); + assertEqual( + "source=t | eval f=cast(1 as string)", + eval(relation("t"), let(field("f"), cast(intLiteral(1), stringLiteral("string"))))); } @Test public void testNestedFieldName() { - assertEqual("source=t | fields field0.field1.field2", + assertEqual( + "source=t | fields field0.field1.field2", projectWithArg( relation("t"), defaultFieldsArgs(), - field( - qualifiedName("field0", "field1", "field2") - ) - )); + field(qualifiedName("field0", "field1", "field2")))); } @Test public void testFieldNameWithSpecialChars() { - assertEqual("source=t | fields `field-0`", - projectWithArg( - relation("t"), - defaultFieldsArgs(), - field( - qualifiedName("field-0") - ) - )); + assertEqual( + "source=t | fields `field-0`", + projectWithArg(relation("t"), defaultFieldsArgs(), field(qualifiedName("field-0")))); } @Test public void testNestedFieldNameWithSpecialChars() { - assertEqual("source=t | fields `field-0`.`field#1`.`field*2`", + assertEqual( + "source=t | fields `field-0`.`field#1`.`field*2`", projectWithArg( relation("t"), defaultFieldsArgs(), - field( - qualifiedName("field-0", "field#1", "field*2") - ) - )); + field(qualifiedName("field-0", "field#1", "field*2")))); } @Test public void testStringLiteralExpr() { - assertEqual("source=t a=\"string\"", - filter( - relation("t"), - compare( - "=", - field("a"), - stringLiteral("string") - ) - )); + assertEqual( + "source=t a=\"string\"", + filter(relation("t"), compare("=", field("a"), stringLiteral("string")))); } @Test public void testIntegerLiteralExpr() { - assertEqual("source=t a=1 b=-1", + assertEqual( + "source=t a=1 b=-1", filter( relation("t"), and( - compare( - "=", - field("a"), - intLiteral(1) - ), - compare( - "=", - field("b"), - intLiteral(-1) - ) - ) - )); + compare("=", field("a"), intLiteral(1)), + compare("=", field("b"), intLiteral(-1))))); } @Test public void testLongLiteralExpr() { - assertEqual("source=t a=1234567890123 b=-1234567890123", + assertEqual( + "source=t a=1234567890123 b=-1234567890123", filter( relation("t"), and( - compare( - "=", - field("a"), - longLiteral(1234567890123L) - ), - compare( - "=", - field("b"), - longLiteral(-1234567890123L) - ) - ) - )); + compare("=", field("a"), longLiteral(1234567890123L)), + compare("=", field("b"), longLiteral(-1234567890123L))))); } @Test public void testDoubleLiteralExpr() { - assertEqual("source=t b=0.1", - filter( - relation("t"), - compare( - "=", - field("b"), - doubleLiteral(0.1) - ) - )); + assertEqual( + "source=t b=0.1", filter(relation("t"), compare("=", field("b"), doubleLiteral(0.1)))); } @Test public void testBooleanLiteralExpr() { - assertEqual("source=t a=true", - filter( - relation("t"), - compare( - "=", - field("a"), - booleanLiteral(true) - ) - )); + assertEqual( + "source=t a=true", filter(relation("t"), compare("=", field("a"), booleanLiteral(true)))); } @Test @@ -692,42 +486,23 @@ public void testIntervalLiteralExpr() { assertEqual( "source=t a = interval 1 day", filter( - relation("t"), - compare( - "=", - field("a"), - intervalLiteral(1, DataType.INTEGER, "day") - ) - )); + relation("t"), compare("=", field("a"), intervalLiteral(1, DataType.INTEGER, "day")))); } @Test public void testKeywordsAsIdentifiers() { - assertEqual( - "source=timestamp", - relation("timestamp") - ); + assertEqual("source=timestamp", relation("timestamp")); assertEqual( "source=t | fields timestamp", - projectWithArg( - relation("t"), - defaultFieldsArgs(), - field("timestamp") - ) - ); + projectWithArg(relation("t"), defaultFieldsArgs(), field("timestamp"))); } @Test public void canBuildKeywordsAsIdentInQualifiedName() { assertEqual( "source=test | fields timestamp", - projectWithArg( - relation("test"), - defaultFieldsArgs(), - field("timestamp") - ) - ); + projectWithArg(relation("test"), defaultFieldsArgs(), field("timestamp"))); } @Test @@ -740,10 +515,7 @@ public void canBuildMatchRelevanceFunctionWithArguments() { "match", unresolvedArg("field", qualifiedName("message")), unresolvedArg("query", stringLiteral("test query")), - unresolvedArg("analyzer", stringLiteral("keyword")) - ) - ) - ); + unresolvedArg("analyzer", stringLiteral("keyword"))))); } @Test @@ -755,13 +527,11 @@ public void canBuildMulti_matchRelevanceFunctionWithArguments() { relation("test"), function( "multi_match", - unresolvedArg("fields", new RelevanceFieldList(ImmutableMap.of( - "field1", 1.F, "field2", 3.2F))), + unresolvedArg( + "fields", + new RelevanceFieldList(ImmutableMap.of("field1", 1.F, "field2", 3.2F))), unresolvedArg("query", stringLiteral("test query")), - unresolvedArg("analyzer", stringLiteral("keyword")) - ) - ) - ); + unresolvedArg("analyzer", stringLiteral("keyword"))))); } @Test @@ -773,13 +543,11 @@ public void canBuildSimple_query_stringRelevanceFunctionWithArguments() { relation("test"), function( "simple_query_string", - unresolvedArg("fields", new RelevanceFieldList(ImmutableMap.of( - "field1", 1.F, "field2", 3.2F))), + unresolvedArg( + "fields", + new RelevanceFieldList(ImmutableMap.of("field1", 1.F, "field2", 3.2F))), unresolvedArg("query", stringLiteral("test query")), - unresolvedArg("analyzer", stringLiteral("keyword")) - ) - ) - ); + unresolvedArg("analyzer", stringLiteral("keyword"))))); } @Test @@ -791,13 +559,11 @@ public void canBuildQuery_stringRelevanceFunctionWithArguments() { relation("test"), function( "query_string", - unresolvedArg("fields", new RelevanceFieldList(ImmutableMap.of( - "field1", 1.F, "field2", 3.2F))), + unresolvedArg( + "fields", + new RelevanceFieldList(ImmutableMap.of("field1", 1.F, "field2", 3.2F))), unresolvedArg("query", stringLiteral("test query")), - unresolvedArg("analyzer", stringLiteral("keyword")) - ) - ) - ); + unresolvedArg("analyzer", stringLiteral("keyword"))))); } @Test @@ -816,11 +582,10 @@ public void functionNameCanBeUsedAsIdentifier() { + "| TIME_TO_SEC | TIMESTAMP | TO_DAYS | UNIX_TIMESTAMP | WEEK | YEAR"); assertFunctionNameCouldBeId( "SUBSTR | SUBSTRING | TRIM | LTRIM | RTRIM | LOWER | UPPER | CONCAT | CONCAT_WS | LENGTH " - + "| STRCMP | RIGHT | LEFT | ASCII | LOCATE | REPLACE" - ); + + "| STRCMP | RIGHT | LEFT | ASCII | LOCATE | REPLACE"); assertFunctionNameCouldBeId( "ABS | CEIL | CEILING | CONV | CRC32 | E | EXP | FLOOR | LN | LOG" - + " | LOG10 | LOG2 | MOD | PI |POW | POWER | RAND | ROUND | SIGN | SQRT | TRUNCATE " + + " | LOG10 | LOG2 | MOD | PI |POW | POWER | RAND | ROUND | SIGN | SQRT | TRUNCATE " + "| ACOS | ASIN | ATAN | ATAN2 | COS | COT | DEGREES | RADIANS | SIN | TAN"); assertFunctionNameCouldBeId( "SEARCH | DESCRIBE | SHOW | FROM | WHERE | FIELDS | RENAME | STATS " @@ -831,100 +596,79 @@ public void functionNameCanBeUsedAsIdentifier() { void assertFunctionNameCouldBeId(String antlrFunctionName) { List functionList = - Arrays.stream(antlrFunctionName.split("\\|")).map(String::stripLeading) - .map(String::stripTrailing).collect( - Collectors.toList()); + Arrays.stream(antlrFunctionName.split("\\|")) + .map(String::stripLeading) + .map(String::stripTrailing) + .collect(Collectors.toList()); assertFalse(functionList.isEmpty()); for (String functionName : functionList) { - assertEqual(String.format(Locale.ROOT, "source=t | fields %s", functionName), - projectWithArg( - relation("t"), - defaultFieldsArgs(), - field( - qualifiedName(functionName) - ) - )); + assertEqual( + String.format(Locale.ROOT, "source=t | fields %s", functionName), + projectWithArg(relation("t"), defaultFieldsArgs(), field(qualifiedName(functionName)))); } } // https://github.com/opensearch-project/sql/issues/1318 @Test public void indexCanBeId() { - assertEqual("source = index | stats count() by index", + assertEqual( + "source = index | stats count() by index", agg( relation("index"), - exprList( - alias( - "count()", - aggregate("count", AllFields.of()) - ) - ), + exprList(alias("count()", aggregate("count", AllFields.of()))), emptyList(), - exprList( - alias( - "index", - field("index") - )), - defaultStatsArgs() - )); + exprList(alias("index", field("index"))), + defaultStatsArgs())); } @Test public void testExtractFunctionExpr() { - assertEqual("source=t | eval f=extract(day from '2001-05-07 10:11:12')", + assertEqual( + "source=t | eval f=extract(day from '2001-05-07 10:11:12')", eval( relation("t"), let( field("f"), - function("extract", - stringLiteral("day"), stringLiteral("2001-05-07 10:11:12")) - ) - )); + function("extract", stringLiteral("day"), stringLiteral("2001-05-07 10:11:12"))))); } - @Test public void testGet_FormatFunctionExpr() { - assertEqual("source=t | eval f=get_format(DATE,'USA')", + assertEqual( + "source=t | eval f=get_format(DATE,'USA')", eval( relation("t"), - let( - field("f"), - function("get_format", - stringLiteral("DATE"), stringLiteral("USA")) - ) - )); + let(field("f"), function("get_format", stringLiteral("DATE"), stringLiteral("USA"))))); } @Test public void testTimeStampAddFunctionExpr() { - assertEqual("source=t | eval f=timestampadd(YEAR, 15, '2001-03-06 00:00:00')", + assertEqual( + "source=t | eval f=timestampadd(YEAR, 15, '2001-03-06 00:00:00')", eval( relation("t"), let( field("f"), - function("timestampadd", + function( + "timestampadd", stringLiteral("YEAR"), intLiteral(15), - stringLiteral("2001-03-06 00:00:00")) - ) - )); + stringLiteral("2001-03-06 00:00:00"))))); } @Test public void testTimeStampDiffFunctionExpr() { - assertEqual("source=t | eval f=timestampdiff(" - + "YEAR, '1997-01-01 00:00:00', '2001-03-06 00:00:00')", + assertEqual( + "source=t | eval f=timestampdiff(" + "YEAR, '1997-01-01 00:00:00', '2001-03-06 00:00:00')", eval( relation("t"), let( field("f"), - function("timestampdiff", + function( + "timestampdiff", stringLiteral("YEAR"), stringLiteral("1997-01-01 00:00:00"), - stringLiteral("2001-03-06 00:00:00")) - ) - )); + stringLiteral("2001-03-06 00:00:00"))))); } } diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java index ddcde513dd..16aa0752e6 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.parser; import static org.junit.Assert.assertEquals; @@ -31,6 +30,7 @@ public class AstNowLikeFunctionTest { /** * Set parameterized values used in test. + * * @param name Function name * @param hasFsp Whether function has fsp argument * @param hasShortcut Whether function has shortcut (call without `()`) @@ -43,24 +43,26 @@ public AstNowLikeFunctionTest(String name, Boolean hasFsp, Boolean hasShortcut) /** * Returns function data to test. + * * @return An iterable. */ @Parameterized.Parameters(name = "{0}") public static Iterable functionNames() { - return List.of(new Object[][]{ - {"now", false, false }, - {"current_timestamp", false, false}, - {"localtimestamp", false, false}, - {"localtime", false, false}, - {"sysdate", true, false}, - {"curtime", false, false}, - {"current_time", false, false}, - {"curdate", false, false}, - {"current_date", false, false}, - {"utc_date", false, false}, - {"utc_time", false, false}, - {"utc_timestamp", false, false} - }); + return List.of( + new Object[][] { + {"now", false, false}, + {"current_timestamp", false, false}, + {"localtimestamp", false, false}, + {"localtime", false, false}, + {"sysdate", true, false}, + {"curtime", false, false}, + {"current_time", false, false}, + {"curdate", false, false}, + {"current_date", false, false}, + {"utc_date", false, false}, + {"utc_time", false, false}, + {"utc_timestamp", false, false} + }); } private final String name; @@ -70,26 +72,20 @@ public static Iterable functionNames() { @Test public void test_function_call_eval() { assertEqual( - eval(relation("t"), let(field("r"), function(name))), - "source=t | eval r=" + name + "()" - ); + eval(relation("t"), let(field("r"), function(name))), "source=t | eval r=" + name + "()"); } @Test public void test_shortcut_eval() { Assume.assumeTrue(hasShortcut); - assertEqual( - eval(relation("t"), let(field("r"), function(name))), - "source=t | eval r=" + name - ); + assertEqual(eval(relation("t"), let(field("r"), function(name))), "source=t | eval r=" + name); } @Test public void test_function_call_where() { assertEqual( filter(relation("t"), compare("=", field("a"), function(name))), - "search source=t | where a=" + name + "()" - ); + "search source=t | where a=" + name + "()"); } @Test @@ -97,18 +93,15 @@ public void test_shortcut_where() { Assume.assumeTrue(hasShortcut); assertEqual( filter(relation("t"), compare("=", field("a"), function(name))), - "search source=t | where a=" + name - ); + "search source=t | where a=" + name); } @Test public void test_function_call_fsp() { Assume.assumeTrue(hasFsp); - assertEqual(filter( - relation("t"), - compare("=", field("a"), function(name, intLiteral(0))) - ), "search source=t | where a=" + name + "(0)" - ); + assertEqual( + filter(relation("t"), compare("=", field("a"), function(name, intLiteral(0)))), + "search source=t | where a=" + name + "(0)"); } protected void assertEqual(Node expectedPlan, String query) { diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstStatementBuilderTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstStatementBuilderTest.java index de74e4932f..7d7b31e822 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstStatementBuilderTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstStatementBuilderTest.java @@ -28,8 +28,7 @@ public class AstStatementBuilderTest { - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); + @Rule public ExpectedException exceptionRule = ExpectedException.none(); private PPLSyntaxParser parser = new PPLSyntaxParser(); @@ -38,9 +37,8 @@ public void buildQueryStatement() { assertEqual( "search source=t a=1", new Query( - project( - filter(relation("t"), compare("=", field("a"), - intLiteral(1))), AllFields.of()), 0)); + project(filter(relation("t"), compare("=", field("a"), intLiteral(1))), AllFields.of()), + 0)); } @Test @@ -50,8 +48,8 @@ public void buildExplainStatement() { new Explain( new Query( project( - filter(relation("t"), compare("=", field("a"), intLiteral(1))), - AllFields.of()), 0))); + filter(relation("t"), compare("=", field("a"), intLiteral(1))), AllFields.of()), + 0))); } private void assertEqual(String query, Statement expectedStatement) { @@ -66,7 +64,8 @@ private void assertExplainEqual(String query, Statement expectedStatement) { private Node plan(String query, boolean isExplain) { final AstStatementBuilder builder = - new AstStatementBuilder(new AstBuilder(new AstExpressionBuilder(), query), + new AstStatementBuilder( + new AstBuilder(new AstExpressionBuilder(), query), AstStatementBuilder.StatementBuilderContext.builder().isExplain(isExplain).build()); return builder.visit(parser.parse(query)); } diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/utils/ArgumentFactoryTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/utils/ArgumentFactoryTest.java index e18dfbd65c..761dbe2997 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/utils/ArgumentFactoryTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/utils/ArgumentFactoryTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.utils; import static java.util.Collections.emptyList; @@ -28,12 +27,10 @@ public class ArgumentFactoryTest extends AstBuilderTest { @Test public void testFieldsCommandArgument() { - assertEqual("source=t | fields - a", + assertEqual( + "source=t | fields - a", projectWithArg( - relation("t"), - exprList(argument("exclude", booleanLiteral(true))), - field("a") - )); + relation("t"), exprList(argument("exclude", booleanLiteral(true))), field("a"))); } @Test @@ -47,20 +44,14 @@ public void testStatsCommandArgument() { "source=t | stats partitions=1 allnum=false delim=',' avg(a) dedup_splitvalues=true", agg( relation("t"), - exprList( - alias( - "avg(a)", - aggregate("avg", field("a"))) - ), + exprList(alias("avg(a)", aggregate("avg", field("a")))), emptyList(), emptyList(), exprList( argument("partitions", intLiteral(1)), argument("allnum", booleanLiteral(false)), argument("delim", stringLiteral(",")), - argument("dedupsplit", booleanLiteral(true)) - ) - )); + argument("dedupsplit", booleanLiteral(true))))); } @Test @@ -72,52 +63,43 @@ public void testStatsCommandDefaultArgument() { @Test public void testDedupCommandArgument() { - assertEqual("source=t | dedup 3 field0 keepempty=false consecutive=true", + assertEqual( + "source=t | dedup 3 field0 keepempty=false consecutive=true", dedupe( relation("t"), exprList( argument("number", intLiteral(3)), argument("keepempty", booleanLiteral(false)), - argument("consecutive", booleanLiteral(true)) - ), - field("field0") - )); + argument("consecutive", booleanLiteral(true))), + field("field0"))); } @Test public void testDedupCommandDefaultArgument() { assertEqual( - "source=t | dedup 1 field0 keepempty=false consecutive=false", - "source=t | dedup field0" - ); + "source=t | dedup 1 field0 keepempty=false consecutive=false", "source=t | dedup field0"); } @Test public void testSortCommandDefaultArgument() { - assertEqual( - "source=t | sort field0", - "source=t | sort field0" - ); + assertEqual("source=t | sort field0", "source=t | sort field0"); } @Test public void testSortFieldArgument() { - assertEqual("source=t | sort - auto(field0)", + assertEqual( + "source=t | sort - auto(field0)", sort( relation("t"), field( "field0", exprList( argument("asc", booleanLiteral(false)), - argument("type", stringLiteral("auto")) - ) - ) - )); + argument("type", stringLiteral("auto")))))); } @Test public void testNoArgConstructorForArgumentFactoryShouldPass() { new ArgumentFactory(); } - } diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizerTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizerTest.java index 1998647dba..cd51ea07df 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizerTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizerTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.utils; import static org.junit.Assert.assertEquals; @@ -29,166 +28,140 @@ public class PPLQueryDataAnonymizerTest { @Test public void testSearchCommand() { - assertEquals("source=t | where a = ***", - anonymize("search source=t a=1") - ); + assertEquals("source=t | where a = ***", anonymize("search source=t a=1")); } @Test public void testTableFunctionCommand() { - assertEquals("source=prometheus.query_range(***,***,***,***)", - anonymize("source=prometheus.query_range('afsd',123,123,3)") - ); + assertEquals( + "source=prometheus.query_range(***,***,***,***)", + anonymize("source=prometheus.query_range('afsd',123,123,3)")); } @Test public void testPrometheusPPLCommand() { - assertEquals("source=prometheus.http_requests_process", - anonymize("source=prometheus.http_requests_process") - ); + assertEquals( + "source=prometheus.http_requests_process", + anonymize("source=prometheus.http_requests_process")); } @Test public void testWhereCommand() { - assertEquals("source=t | where a = ***", - anonymize("search source=t | where a=1") - ); + assertEquals("source=t | where a = ***", anonymize("search source=t | where a=1")); } @Test public void testFieldsCommandWithoutArguments() { - assertEquals("source=t | fields + f,g", - anonymize("source=t | fields f,g")); + assertEquals("source=t | fields + f,g", anonymize("source=t | fields f,g")); } @Test public void testFieldsCommandWithIncludeArguments() { - assertEquals("source=t | fields + f,g", - anonymize("source=t | fields + f,g")); + assertEquals("source=t | fields + f,g", anonymize("source=t | fields + f,g")); } @Test public void testFieldsCommandWithExcludeArguments() { - assertEquals("source=t | fields - f,g", - anonymize("source=t | fields - f,g")); + assertEquals("source=t | fields - f,g", anonymize("source=t | fields - f,g")); } @Test public void testRenameCommandWithMultiFields() { - assertEquals("source=t | rename f as g,h as i,j as k", + assertEquals( + "source=t | rename f as g,h as i,j as k", anonymize("source=t | rename f as g,h as i,j as k")); } @Test public void testStatsCommandWithByClause() { - assertEquals("source=t | stats count(a) by b", - anonymize("source=t | stats count(a) by b")); + assertEquals("source=t | stats count(a) by b", anonymize("source=t | stats count(a) by b")); } @Test public void testStatsCommandWithNestedFunctions() { - assertEquals("source=t | stats sum(+(a,b))", - anonymize("source=t | stats sum(a+b)")); + assertEquals("source=t | stats sum(+(a,b))", anonymize("source=t | stats sum(a+b)")); } @Test public void testDedupCommand() { - assertEquals("source=t | dedup f1,f2 1 keepempty=false consecutive=false", + assertEquals( + "source=t | dedup f1,f2 1 keepempty=false consecutive=false", anonymize("source=t | dedup f1, f2")); } @Test public void testHeadCommandWithNumber() { - assertEquals("source=t | head 3", - anonymize("source=t | head 3")); + assertEquals("source=t | head 3", anonymize("source=t | head 3")); } - //todo, sort order is ignored, it doesn't impact the log analysis. + // todo, sort order is ignored, it doesn't impact the log analysis. @Test public void testSortCommandWithOptions() { - assertEquals("source=t | sort f1,f2", - anonymize("source=t | sort - f1, + f2")); + assertEquals("source=t | sort f1,f2", anonymize("source=t | sort - f1, + f2")); } @Test public void testEvalCommand() { - assertEquals("source=t | eval r=abs(f)", - anonymize("source=t | eval r=abs(f)")); + assertEquals("source=t | eval r=abs(f)", anonymize("source=t | eval r=abs(f)")); } @Test public void testRareCommandWithGroupBy() { - assertEquals("source=t | rare 10 a by b", - anonymize("source=t | rare a by b")); + assertEquals("source=t | rare 10 a by b", anonymize("source=t | rare a by b")); } @Test public void testTopCommandWithNAndGroupBy() { - assertEquals("source=t | top 1 a by b", - anonymize("source=t | top 1 a by b")); + assertEquals("source=t | top 1 a by b", anonymize("source=t | top 1 a by b")); } @Test public void testAndExpression() { - assertEquals("source=t | where a = *** and b = ***", - anonymize("source=t | where a=1 and b=2") - ); + assertEquals("source=t | where a = *** and b = ***", anonymize("source=t | where a=1 and b=2")); } @Test public void testOrExpression() { - assertEquals("source=t | where a = *** or b = ***", - anonymize("source=t | where a=1 or b=2") - ); + assertEquals("source=t | where a = *** or b = ***", anonymize("source=t | where a=1 or b=2")); } @Test public void testXorExpression() { - assertEquals("source=t | where a = *** xor b = ***", - anonymize("source=t | where a=1 xor b=2") - ); + assertEquals("source=t | where a = *** xor b = ***", anonymize("source=t | where a=1 xor b=2")); } @Test public void testNotExpression() { - assertEquals("source=t | where not a = ***", - anonymize("source=t | where not a=1 ") - ); + assertEquals("source=t | where not a = ***", anonymize("source=t | where not a=1 ")); } @Test public void testQualifiedName() { - assertEquals("source=t | fields + field0", - anonymize("source=t | fields field0") - ); + assertEquals("source=t | fields + field0", anonymize("source=t | fields field0")); } @Test public void testDateFunction() { - assertEquals("source=t | eval date=DATE_ADD(DATE(***),INTERVAL *** HOUR)", - anonymize("source=t | eval date=DATE_ADD(DATE('2020-08-26'),INTERVAL 1 HOUR)") - ); + assertEquals( + "source=t | eval date=DATE_ADD(DATE(***),INTERVAL *** HOUR)", + anonymize("source=t | eval date=DATE_ADD(DATE('2020-08-26'),INTERVAL 1 HOUR)")); } @Test public void testExplain() { - assertEquals("source=t | fields + a", - anonymizeStatement("source=t | fields a", true) - ); + assertEquals("source=t | fields + a", anonymizeStatement("source=t | fields a", true)); } @Test public void testQuery() { - assertEquals("source=t | fields + a", - anonymizeStatement("source=t | fields a", false) - ); + assertEquals("source=t | fields + a", anonymizeStatement("source=t | fields a", false)); } @Test public void anonymizeFieldsNoArg() { - assertEquals("source=t | fields + f", - anonymize(projectWithArg(relation("t"), Collections.emptyList(), field("f"))) - ); + assertEquals( + "source=t | fields + f", + anonymize(projectWithArg(relation("t"), Collections.emptyList(), field("f")))); } private String anonymize(String query) { diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/utils/UnresolvedPlanHelperTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/utils/UnresolvedPlanHelperTest.java index d64c8d5db4..7c1264e0b6 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/utils/UnresolvedPlanHelperTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/utils/UnresolvedPlanHelperTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl.utils; import static org.hamcrest.MatcherAssert.assertThat;