Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ppl help command #685

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c9f39dc
add `-help` command to describe available command grammar as part of …
YANG-DB Sep 20, 2024
d764268
update scala format
YANG-DB Sep 20, 2024
df13de7
Merge branch 'main' into ppl-help-command
YANG-DB Sep 20, 2024
8574140
Merge branch 'main' into ppl-help-command
YANG-DB Sep 27, 2024
4427b8f
Merge remote-tracking branch 'origin/ppl-help-command' into ppl-help-…
YANG-DB Sep 27, 2024
2bb31e9
update scala format
YANG-DB Sep 28, 2024
498f6e7
update help command & source linces header
YANG-DB Oct 1, 2024
c5e0ec7
Merge branch 'main' into ppl-help-command
YANG-DB Oct 1, 2024
b1c8fcf
update help command & source linces header
YANG-DB Oct 1, 2024
ddacb88
update help command for explain
YANG-DB Oct 1, 2024
b52b0d7
update help method name for fields clause
YANG-DB Oct 1, 2024
b8dce6d
update help methods support in antlr
YANG-DB Oct 1, 2024
bc78c55
Merge branch 'main' into ppl-help-command
YANG-DB Oct 3, 2024
0f9e857
Merge branch 'main' into ppl-help-command
YANG-DB Oct 4, 2024
9ce860d
Merge branch 'main' into ppl-help-command
YANG-DB Oct 7, 2024
5f2a1e8
update help methods support in antlr
YANG-DB Oct 9, 2024
969b19e
update help methods support in antlr
YANG-DB Oct 9, 2024
4b03ceb
Merge remote-tracking branch 'origin/ppl-help-command' into ppl-help-…
YANG-DB Oct 9, 2024
d946d9d
Merge branch 'main' into ppl-help-command
YANG-DB Oct 9, 2024
92b1659
update help methods support in antlr
YANG-DB Oct 9, 2024
af72ad8
Merge branch 'main' into ppl-help-command
YANG-DB Oct 11, 2024
3c0a949
Merge branch 'main' into ppl-help-command
YANG-DB Oct 12, 2024
1dae1d5
Merge branch 'main' into ppl-help-command
YANG-DB Oct 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.flint.spark.ppl

import org.opensearch.flint.spark.PrintLiteralCommandDescriptionLogicalPlan
import org.opensearch.sql.ppl.parser.AstCommandDescriptionVisitor

import org.apache.spark.sql.{QueryTest, Row}
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.analysis.{UnresolvedAttribute, UnresolvedRelation, UnresolvedStar}
import org.apache.spark.sql.catalyst.expressions.{Ascending, Literal, SortOrder}
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.execution.command.DescribeTableCommand
import org.apache.spark.sql.streaming.StreamTest

class FlintSparkPPLHelpCommandITSuite
extends QueryTest
with LogicalPlanTestUtils
with FlintPPLSuite
with StreamTest {

override def beforeAll(): Unit = {
super.beforeAll()
}

protected override def afterEach(): Unit = {
super.afterEach()
// Stop all streaming jobs if any
spark.streams.active.foreach { job =>
job.stop()
job.awaitTermination()
}
}

ingore("search -help command") {
val helpText =
"""
|SEARCH Command:
|
|Syntax:
| (SEARCH)? fromClause
| | (SEARCH)? fromClause logicalExpression
| | (SEARCH)? logicalExpression fromClause
|
|Description:
|The SEARCH command is used to retrieve data from a specified source. It can be used with or without additional filters.
|- You can specify the data source using the FROM clause.
|- You can add filters using logical expressions.
|- The order of FROM clause and logical expression can be interchanged.
""".stripMargin.trim

val pplParser = new PPLSyntaxParser()
val frame = sql(s"""
search -help
""".stripMargin)

// Retrieve the results
val results: Array[Row] = frame.collect()
// Define the expected results
val expectedResults: Array[Row] = Array(
Row(AstCommandDescriptionVisitor.describeCommand(helpText, pplParser.getParserVersion())))

// Extract values from rows, assuming single column
val actualValues = results.map(_.getAs[String](0))
val expectedValues = expectedResults.map(_.getAs[String](0))

assert(
actualValues.sameElements(expectedValues),
s"""
|Expected: ${expectedValues.mkString(", ")}
|Actual: ${actualValues.mkString(", ")}
|""".stripMargin)

// Retrieve the logical plan
val logicalPlan: LogicalPlan = frame.queryExecution.logical
// Expected plan should match the produced custom logical plan
val expectedPlan: LogicalPlan = PrintLiteralCommandDescriptionLogicalPlan(
AstCommandDescriptionVisitor.describeCommand(helpText, pplParser.getParserVersion()))

// Compare the plans
comparePlans(expectedPlan, logicalPlan, false)
}

}
17 changes: 17 additions & 0 deletions ppl-spark-integration/src/main/antlr4/OpenSearchPPLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@

lexer grammar OpenSearchPPLLexer;

@lexer::members {
public static final String GRAMMAR_VERSION = "1.0.0";
public static final String LAST_UPDATED = "2024-09-18";

public static String getGrammarVersion() {
return GRAMMAR_VERSION;
}

public static String getLastUpdated() {
return LAST_UPDATED;
}
}


channels { WHITESPACE, ERRORCHANNEL }


Expand Down Expand Up @@ -80,6 +94,9 @@ WITH: 'WITH';
// CLAUSE KEYWORDS
SORTBY: 'SORTBY';

// HELP COMMAND
HELP: 'HELP';

// FIELD KEYWORDS
AUTO: 'AUTO';
STR: 'STR';
Expand Down
85 changes: 62 additions & 23 deletions ppl-spark-integration/src/main/antlr4/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@

parser grammar OpenSearchPPLParser;

@parser::members {
public static final String GRAMMAR_VERSION = "1.0.0";
public static final String LAST_UPDATED = "2024-09-18";

public static String getGrammarVersion() {
return GRAMMAR_VERSION;
}

public static String getLastUpdated() {
return LAST_UPDATED;
}
}

options { tokenVocab = OpenSearchPPLLexer; }
root
Expand Down Expand Up @@ -32,6 +44,7 @@ subSearch
pplCommands
: searchCommand
| describeCommand
| helpCommand
;

commands
Expand All @@ -58,10 +71,16 @@ searchCommand
: (SEARCH)? fromClause # searchFrom
| (SEARCH)? fromClause logicalExpression # searchFromFilter
| (SEARCH)? logicalExpression fromClause # searchFilterFrom
| (SEARCH)? HELP # searchHelp
;

describeCommand
: DESCRIBE tableSourceClause
: DESCRIBE tableSourceClause # describeClause
| DESCRIBE HELP # describeHelp
;

helpCommand
: HELP commandNames # helpCommandName
;

explainCommand
Expand All @@ -81,11 +100,13 @@ showDataSourcesCommand
;

whereCommand
: WHERE logicalExpression
: WHERE logicalExpression # whereClause
| WHERE HELP # whereHelp
;

correlateCommand
: CORRELATE correlationType FIELDS LT_PRTHS fieldList RT_PRTHS (scopeClause)? mappingList
: CORRELATE correlationType FIELDS LT_PRTHS fieldList RT_PRTHS (scopeClause)? mappingList # correlateClause
| CORRELATE HELP # correlateHelp
;

correlationType
Expand All @@ -107,51 +128,63 @@ mappingClause
;

fieldsCommand
: FIELDS (PLUS | MINUS)? fieldList
: FIELDS (PLUS | MINUS)? fieldList # fieldsClause
| FIELDS HELP # fieldsHelp
;

renameCommand
: RENAME renameClasue (COMMA renameClasue)*
: 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 (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 (number = integerLiteral)? fieldList (KEEPEMPTY EQUAL keepempty = booleanLiteral)? (CONSECUTIVE EQUAL consecutive = booleanLiteral)? # dedupClause
| DEDUP HELP # dedupHelp
;

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

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

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

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

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

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

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

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

patternsParameter
Expand All @@ -166,7 +199,8 @@ patternsMethod

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

lookupMappingList
Expand Down Expand Up @@ -257,7 +291,8 @@ tableSourceClause

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

joinType
Expand Down Expand Up @@ -306,7 +341,7 @@ bySpanClause
;

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

sortbyClause
Expand Down Expand Up @@ -342,6 +377,7 @@ statsFunctionName

takeAggFunction
: TAKE LT_PRTHS fieldExpression (COMMA size = integerLiteral)? RT_PRTHS
| TAKE HELP
;

percentileAggFunction
Expand Down Expand Up @@ -401,7 +437,7 @@ booleanExpression
;

isEmptyExpression
: (ISEMPTY | ISBLANK) LT_PRTHS functionArg RT_PRTHS
: (ISEMPTY | ISBLANK) LT_PRTHS functionArg RT_PRTHS
;

caseFunction
Expand Down Expand Up @@ -943,8 +979,11 @@ keywordsCanBeId
| textFunctionName
| mathematicalFunctionName
| positionFunctionName
// commands
| SEARCH
| commandNames
;

commandNames
: SEARCH // commands
| DESCRIBE
| SHOW
| FROM
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.ast.tree;

import java.util.Collections;

/**
* Extend Projection to describe the command itself
*/
public class DescribeCommand extends Project{
private String commandDescription;

public DescribeCommand(String commandDescription) {
super(Collections.emptyList());
this.commandDescription = commandDescription;
}

public String getDescription() {
return commandDescription;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.sql.ast.tree;

public interface Help {
/**
* command description (help)
* @return
*/
String describe();

/**
* command samples (help)
* @return
*/
String sample();
}
Loading
Loading