Skip to content

Commit

Permalink
update help methods support in antlr
Browse files Browse the repository at this point in the history
Signed-off-by: YANGDB <[email protected]>
  • Loading branch information
YANG-DB committed Oct 1, 2024
1 parent b52b0d7 commit b8dce6d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 47 deletions.
59 changes: 29 additions & 30 deletions ppl-spark-integration/src/main/antlr4/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -131,58 +131,58 @@ fieldsCommand
;

renameCommand
: RENAME renameClasue (COMMA renameClasue)*
| RENAME HELP
: RENAME renameClasue (COMMA renameClasue)* # renameClause
| RENAME HELP # renameHelp
;

statsCommand
: STATS (PARTITIONS EQUAL partitions = integerLiteral)? (ALLNUM EQUAL allnum = booleanLiteral)? (DELIM EQUAL delim = stringLiteral)? statsAggTerm (COMMA statsAggTerm)* (statsByClause)? (DEDUP_SPLITVALUES EQUAL dedupsplit = booleanLiteral)?
| STATS HELP
: STATS (PARTITIONS EQUAL partitions = integerLiteral)? (ALLNUM EQUAL allnum = booleanLiteral)? (DELIM EQUAL delim = stringLiteral)? statsAggTerm (COMMA statsAggTerm)* (statsByClause)? (DEDUP_SPLITVALUES EQUAL dedupsplit = booleanLiteral)? # statsClause
| STATS HELP # statsHelp
;

dedupCommand
: DEDUP (number = integerLiteral)? fieldList (KEEPEMPTY EQUAL keepempty = booleanLiteral)? (CONSECUTIVE EQUAL consecutive = booleanLiteral)?
| DEDUP HELP
: DEDUP (number = integerLiteral)? fieldList (KEEPEMPTY EQUAL keepempty = booleanLiteral)? (CONSECUTIVE EQUAL consecutive = booleanLiteral)? # dedupClause
| DEDUP HELP # dedupHelp
;

sortCommand
: SORT sortbyClause
| SORT HELP
: SORT sortbyClause # sortClause
| SORT HELP # sortHelp
;

evalCommand
: EVAL evalClause (COMMA evalClause)*
| EVAL HELP
: EVAL evalClause (COMMA evalClause)* # evalCommandClause
| EVAL HELP # evalHelp
;

headCommand
: HEAD (number = integerLiteral)? (FROM from = integerLiteral)?
| HEAD HELP
: HEAD (number = integerLiteral)? (FROM from = integerLiteral)? # headClause
| HEAD HELP # headHelp
;

topCommand
: TOP (number = integerLiteral)? fieldList (byClause)?
| TOP HELP
topCommand
: TOP (number = integerLiteral)? fieldList (byClause)? # topClause
| TOP HELP # topHelp
;

rareCommand
: RARE fieldList (byClause)?
| RARE HELP
rareCommand
: RARE fieldList (byClause)? # rareClause
| RARE HELP # rareHelp
;

grokCommand
: GROK (source_field = expression) (pattern = stringLiteral)
| GROK HELP
: GROK (source_field = expression) (pattern = stringLiteral) # grokClause
| GROK HELP # grokHelp
;

parseCommand
: PARSE (source_field = expression) (pattern = stringLiteral)
| PARSE HELP
: PARSE (source_field = expression) (pattern = stringLiteral) # parseClause
| PARSE HELP # parseHelp
;

patternsCommand
: PATTERNS (patternsParameter)* (source_field = expression)
| PATTERNS HELP
: PATTERNS (patternsParameter)* (source_field = expression) # patternsClause
| PATTERNS HELP # patternsHelp
;

patternsParameter
Expand All @@ -197,8 +197,8 @@ patternsMethod

// lookup
lookupCommand
: LOOKUP tableSource lookupMappingList ((APPEND | REPLACE) outputCandidateList)?
| LOOKUP HELP
: LOOKUP tableSource lookupMappingList ((APPEND | REPLACE) outputCandidateList)? # lookupClause
| LOOKUP HELP # lookupHelp
;

lookupMappingList
Expand Down Expand Up @@ -266,8 +266,8 @@ tableSourceClause

// join
joinCommand
: (joinType) JOIN sideAlias joinHintList? joinCriteria? right = tableSource
| JOIN HELP
: (joinType) JOIN sideAlias joinHintList? joinCriteria? right = tableSource # joinClause
| JOIN HELP # joinHelp
;

joinType
Expand Down Expand Up @@ -316,8 +316,7 @@ bySpanClause
;

spanClause
: SPAN LT_PRTHS fieldExpression COMMA value = literalValue (unit = timespanUnit)? RT_PRTHS
| SPAN HELP
: SPAN LT_PRTHS fieldExpression COMMA value = literalValue (unit = timespanUnit)? RT_PRTHS
;

sortbyClause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public UnresolvedPlan visitCorrelateClause(OpenSearchPPLParser.CorrelateClauseCo
}

@Override
public UnresolvedPlan visitJoinCommand(OpenSearchPPLParser.JoinCommandContext ctx) {
public UnresolvedPlan visitJoinClause(OpenSearchPPLParser.JoinClauseContext ctx) {
Join.JoinType joinType = getJoinType(ctx.joinType());
if (ctx.joinCriteria() == null) {
joinType = Join.JoinType.CROSS;
Expand Down Expand Up @@ -218,7 +218,7 @@ public UnresolvedPlan visitFieldsClause(OpenSearchPPLParser.FieldsClauseContext

/** Rename command. */
@Override
public UnresolvedPlan visitRenameCommand(OpenSearchPPLParser.RenameCommandContext ctx) {
public UnresolvedPlan visitRenameClause(OpenSearchPPLParser.RenameClauseContext ctx) {
return new Rename(
ctx.renameClasue().stream()
.map(
Expand All @@ -231,7 +231,7 @@ public UnresolvedPlan visitRenameCommand(OpenSearchPPLParser.RenameCommandContex

/** Stats command. */
@Override
public UnresolvedPlan visitStatsCommand(OpenSearchPPLParser.StatsCommandContext ctx) {
public UnresolvedPlan visitStatsClause(OpenSearchPPLParser.StatsClauseContext ctx) {
ImmutableList.Builder<UnresolvedExpression> aggListBuilder = new ImmutableList.Builder<>();
for (OpenSearchPPLParser.StatsAggTermContext aggCtx : ctx.statsAggTerm()) {
UnresolvedExpression aggExpression = internalVisitExpression(aggCtx.statsFunction());
Expand Down Expand Up @@ -276,21 +276,21 @@ public UnresolvedPlan visitStatsCommand(OpenSearchPPLParser.StatsCommandContext

/** Dedup command. */
@Override
public UnresolvedPlan visitDedupCommand(OpenSearchPPLParser.DedupCommandContext ctx) {
public UnresolvedPlan visitDedupClause(OpenSearchPPLParser.DedupClauseContext ctx) {
return new Dedupe(ArgumentFactory.getArgumentList(ctx), getFieldList(ctx.fieldList()));
}

/** Head command visitor. */
@Override
public UnresolvedPlan visitHeadCommand(OpenSearchPPLParser.HeadCommandContext ctx) {
public UnresolvedPlan visitHeadClause(OpenSearchPPLParser.HeadClauseContext ctx) {
Integer size = ctx.number != null ? Integer.parseInt(ctx.number.getText()) : 10;
Integer from = ctx.from != null ? Integer.parseInt(ctx.from.getText()) : 0;
return new Head(size, from);
}

/** Sort command. */
@Override
public UnresolvedPlan visitSortCommand(OpenSearchPPLParser.SortCommandContext ctx) {
public UnresolvedPlan visitSortClause(OpenSearchPPLParser.SortClauseContext ctx) {
return new Sort(
ctx.sortbyClause().sortField().stream()
.map(sort -> (Field) internalVisitExpression(sort))
Expand All @@ -299,7 +299,7 @@ public UnresolvedPlan visitSortCommand(OpenSearchPPLParser.SortCommandContext ct

/** Eval command. */
@Override
public UnresolvedPlan visitEvalCommand(OpenSearchPPLParser.EvalCommandContext ctx) {
public UnresolvedPlan visitEvalCommandClause(OpenSearchPPLParser.EvalCommandClauseContext ctx) {
return new Eval(
ctx.evalClause().stream()
.map(ct -> (Let) internalVisitExpression(ct))
Expand All @@ -319,23 +319,23 @@ private List<Field> getFieldList(OpenSearchPPLParser.FieldListContext ctx) {
}

@Override
public UnresolvedPlan visitGrokCommand(OpenSearchPPLParser.GrokCommandContext ctx) {
public UnresolvedPlan visitGrokClause(OpenSearchPPLParser.GrokClauseContext ctx) {
UnresolvedExpression sourceField = internalVisitExpression(ctx.source_field);
Literal pattern = (Literal) internalVisitExpression(ctx.pattern);

return new Parse(ParseMethod.GROK, sourceField, pattern, ImmutableMap.of());
}

@Override
public UnresolvedPlan visitParseCommand(OpenSearchPPLParser.ParseCommandContext ctx) {
public UnresolvedPlan visitParseClause(OpenSearchPPLParser.ParseClauseContext ctx) {
UnresolvedExpression sourceField = internalVisitExpression(ctx.source_field);
Literal pattern = (Literal) internalVisitExpression(ctx.pattern);

return new Parse(ParseMethod.REGEX, sourceField, pattern, ImmutableMap.of());
}

@Override
public UnresolvedPlan visitPatternsCommand(OpenSearchPPLParser.PatternsCommandContext ctx) {
public UnresolvedPlan visitPatternsClause(OpenSearchPPLParser.PatternsClauseContext ctx) {
UnresolvedExpression sourceField = internalVisitExpression(ctx.source_field);
ImmutableMap.Builder<String, Literal> builder = ImmutableMap.builder();
ctx.patternsParameter()
Expand All @@ -353,7 +353,7 @@ public UnresolvedPlan visitPatternsCommand(OpenSearchPPLParser.PatternsCommandCo

/** Lookup command */
@Override
public UnresolvedPlan visitLookupCommand(OpenSearchPPLParser.LookupCommandContext ctx) {
public UnresolvedPlan visitLookupClause(OpenSearchPPLParser.LookupClauseContext ctx) {
Relation lookupRelation = new Relation(this.internalVisitExpression(ctx.tableSource()));
Lookup.OutputStrategy strategy =
ctx.APPEND() != null ? Lookup.OutputStrategy.APPEND : Lookup.OutputStrategy.REPLACE;
Expand All @@ -372,7 +372,7 @@ private java.util.Map<Alias, Field> buildLookupPair(List<OpenSearchPPLParser.Loo

/** Top command. */
@Override
public UnresolvedPlan visitTopCommand(OpenSearchPPLParser.TopCommandContext ctx) {
public UnresolvedPlan visitTopClause(OpenSearchPPLParser.TopClauseContext ctx) {
ImmutableList.Builder<UnresolvedExpression> aggListBuilder = new ImmutableList.Builder<>();
ImmutableList.Builder<UnresolvedExpression> groupListBuilder = new ImmutableList.Builder<>();
ctx.fieldList().fieldExpression().forEach(field -> {
Expand Down Expand Up @@ -413,7 +413,7 @@ public UnresolvedPlan visitTopCommand(OpenSearchPPLParser.TopCommandContext ctx)

/** Rare command. */
@Override
public UnresolvedPlan visitRareCommand(OpenSearchPPLParser.RareCommandContext ctx) {
public UnresolvedPlan visitRareClause(OpenSearchPPLParser.RareClauseContext ctx) {
ImmutableList.Builder<UnresolvedExpression> aggListBuilder = new ImmutableList.Builder<>();
ImmutableList.Builder<UnresolvedExpression> groupListBuilder = new ImmutableList.Builder<>();
ctx.fieldList().fieldExpression().forEach(field -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public AstCommandDescriptionVisitor(AstExpressionBuilder expressionBuilder, Stri
this.version = version;
}

@Override
public UnresolvedPlan visitRenameHelp(OpenSearchPPLParser.RenameHelpContext ctx) {
return super.visitRenameHelp(ctx);
}

@Override
public UnresolvedPlan visitWhereHelp(OpenSearchPPLParser.WhereHelpContext ctx) {
return super.visitWhereHelp(ctx);
Expand All @@ -35,6 +40,31 @@ public UnresolvedPlan visitCorrelateHelp(OpenSearchPPLParser.CorrelateHelpContex
return super.visitCorrelateHelp(ctx);
}

@Override
public UnresolvedPlan visitTopHelp(OpenSearchPPLParser.TopHelpContext ctx) {
return super.visitTopHelp(ctx);
}

@Override
public UnresolvedPlan visitEvalHelp(OpenSearchPPLParser.EvalHelpContext ctx) {
return super.visitEvalHelp(ctx);
}

@Override
public UnresolvedPlan visitSortHelp(OpenSearchPPLParser.SortHelpContext ctx) {
return super.visitSortHelp(ctx);
}

@Override
public UnresolvedPlan visitDedupHelp(OpenSearchPPLParser.DedupHelpContext ctx) {
return super.visitDedupHelp(ctx);
}

@Override
public UnresolvedPlan visitStatsHelp(OpenSearchPPLParser.StatsHelpContext ctx) {
return super.visitStatsHelp(ctx);
}

@Override
public UnresolvedPlan visitSearchHelp(OpenSearchPPLParser.SearchHelpContext ctx) {
String description = "SEARCH Command:\n" +
Expand All @@ -57,6 +87,11 @@ public UnresolvedPlan visitDescribeHelp(OpenSearchPPLParser.DescribeHelpContext
return super.visitDescribeHelp(ctx);
}

@Override
public UnresolvedPlan visitFieldsHelp(OpenSearchPPLParser.FieldsHelpContext ctx) {
return super.visitFieldsHelp(ctx);
}

@Override
public UnresolvedPlan visitHelpCommandName(OpenSearchPPLParser.HelpCommandNameContext ctx) {
OpenSearchPPLParser.CommandNamesContext commandedName = ctx.commandNames();
Expand All @@ -72,7 +107,42 @@ public UnresolvedPlan visitHelpCommandName(OpenSearchPPLParser.HelpCommandNameCo
return new DescribeCommand(describeCommand("This command has no help description - please check revision for compatability", version));
}

public static String describeCommand( String description, String version) {
@Override
public UnresolvedPlan visitHeadHelp(OpenSearchPPLParser.HeadHelpContext ctx) {
return super.visitHeadHelp(ctx);
}

@Override
public UnresolvedPlan visitJoinHelp(OpenSearchPPLParser.JoinHelpContext ctx) {
return super.visitJoinHelp(ctx);
}

@Override
public UnresolvedPlan visitRareHelp(OpenSearchPPLParser.RareHelpContext ctx) {
return super.visitRareHelp(ctx);
}

@Override
public UnresolvedPlan visitGrokHelp(OpenSearchPPLParser.GrokHelpContext ctx) {
return super.visitGrokHelp(ctx);
}

@Override
public UnresolvedPlan visitParseHelp(OpenSearchPPLParser.ParseHelpContext ctx) {
return super.visitParseHelp(ctx);
}

@Override
public UnresolvedPlan visitPatternsHelp(OpenSearchPPLParser.PatternsHelpContext ctx) {
return super.visitPatternsHelp(ctx);
}

@Override
public UnresolvedPlan visitLookupHelp(OpenSearchPPLParser.LookupHelpContext ctx) {
return super.visitLookupHelp(ctx);
}

public static String describeCommand(String description, String version) {
StringBuilder commandSummary = new StringBuilder();
commandSummary.append("\n\n").append(version).append(" PPL Revision:\n\n");
commandSummary.append("Description:\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static List<Argument> getArgumentList(OpenSearchPPLParser.FieldsClauseCon
* @param ctx StatsCommandContext instance
* @return the list of arguments fetched from the stats command
*/
public static List<Argument> getArgumentList(OpenSearchPPLParser.StatsCommandContext ctx) {
public static List<Argument> getArgumentList(OpenSearchPPLParser.StatsClauseContext ctx) {
return Arrays.asList(
ctx.partitions != null
? new Argument("partitions", getArgumentValue(ctx.partitions))
Expand All @@ -59,7 +59,7 @@ public static List<Argument> getArgumentList(OpenSearchPPLParser.StatsCommandCon
* @param ctx DedupCommandContext instance
* @return the list of arguments fetched from the dedup command
*/
public static List<Argument> getArgumentList(OpenSearchPPLParser.DedupCommandContext ctx) {
public static List<Argument> getArgumentList(OpenSearchPPLParser.DedupClauseContext ctx) {
return Arrays.asList(
ctx.number != null
? new Argument("number", getArgumentValue(ctx.number))
Expand Down Expand Up @@ -100,7 +100,7 @@ public static List<Argument> getArgumentList(OpenSearchPPLParser.SortFieldContex
* @param ctx TopCommandContext instance
* @return the list of arguments fetched from the top command
*/
public static List<Argument> getArgumentList(OpenSearchPPLParser.TopCommandContext ctx) {
public static List<Argument> getArgumentList(OpenSearchPPLParser.TopClauseContext ctx) {
return Collections.singletonList(
ctx.number != null
? new Argument("noOfResults", getArgumentValue(ctx.number))
Expand Down

0 comments on commit b8dce6d

Please sign in to comment.