Skip to content

Commit

Permalink
explain command supports explainMode argument
Browse files Browse the repository at this point in the history
Signed-off-by: Kacper Trochimiak <[email protected]>
  • Loading branch information
kt-eliatra committed Sep 26, 2024
1 parent 1236265 commit a2ab018
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package org.opensearch.flint.spark.ppl
import org.apache.spark.sql.{QueryTest, Row}
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.analysis.{UnresolvedAttribute, UnresolvedFunction, UnresolvedRelation, UnresolvedStar}
import org.apache.spark.sql.catalyst.expressions.{Alias, Ascending, Descending, EqualTo, Literal, Not, SortOrder}
import org.apache.spark.sql.catalyst.expressions.{Alias, Ascending, Descending, EqualTo, IsNotNull, Literal, Not, SortOrder}
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.execution.ExplainMode
import org.apache.spark.sql.execution.command.{DescribeTableCommand, ExplainCommand}
Expand Down Expand Up @@ -39,9 +39,9 @@ class FlintSparkPPLBasicITSuite
}
}

test("explain test") {
test("explain simple mode test") {
val frame = sql(s"""
| explain | source = $testTable | where state != 'California' | fields name
| explain simple | source = $testTable | where state != 'California' | fields name
| """.stripMargin)

// Retrieve the logical plan
Expand All @@ -55,7 +55,87 @@ class FlintSparkPPLBasicITSuite
Project(Seq(UnresolvedAttribute("name")), filter),
ExplainMode.fromString("simple"))
// Compare the two plans
assert(expectedPlan === logicalPlan)
comparePlans(logicalPlan, expectedPlan, checkAnalysis = false)
}

test("explain extended mode test") {
val frame = sql(s"""
| explain extended | source = $testTable
| """.stripMargin)

// Retrieve the logical plan
val logicalPlan: LogicalPlan = frame.queryExecution.logical
// Define the expected logical plan
val relation = UnresolvedRelation(Seq("spark_catalog", "default", "flint_ppl_test"))
val expectedPlan: LogicalPlan =
ExplainCommand(
Project(Seq(UnresolvedStar(None)), relation),
ExplainMode.fromString("extended"))
// Compare the two plans
comparePlans(logicalPlan, expectedPlan, checkAnalysis = false)
}

test("explain codegen mode test") {
val frame = sql(s"""
| explain codegen | source = $testTable | dedup name | fields name, state
| """.stripMargin)

// Retrieve the logical plan
val logicalPlan: LogicalPlan = frame.queryExecution.logical
// Define the expected logical plan
val relation = UnresolvedRelation(Seq("spark_catalog", "default", "flint_ppl_test"))
val nameAttribute = UnresolvedAttribute("name")
val dedup =
Deduplicate(Seq(nameAttribute), Filter(IsNotNull(nameAttribute), relation))
val expectedPlan: LogicalPlan =
ExplainCommand(
Project(Seq(UnresolvedAttribute("name"), UnresolvedAttribute("state")), dedup),
ExplainMode.fromString("codegen"))
// Compare the two plans
comparePlans(logicalPlan, expectedPlan, checkAnalysis = false)
}

test("explain cost mode test") {
val frame = sql(s"""
| explain cost | source = $testTable | sort name | fields name, age
| """.stripMargin)

// Retrieve the logical plan
val logicalPlan: LogicalPlan = frame.queryExecution.logical
// Define the expected logical plan
val relation = UnresolvedRelation(Seq("spark_catalog", "default", "flint_ppl_test"))
val sort: LogicalPlan =
Sort(
Seq(SortOrder(UnresolvedAttribute("name"), Ascending)),
global = true,
relation)
val expectedPlan: LogicalPlan =
ExplainCommand(
Project(Seq(UnresolvedAttribute("name"), UnresolvedAttribute("age")), sort),
ExplainMode.fromString("cost"))
// Compare the two plans
comparePlans(logicalPlan, expectedPlan, checkAnalysis = false)
}

test("explain formatted mode test") {
val frame = sql(s"""
| explain formatted | source = $testTable | fields - name
| """.stripMargin)

// Retrieve the logical plan
val logicalPlan: LogicalPlan = frame.queryExecution.logical
// Define the expected logical plan
val relation = UnresolvedRelation(Seq("spark_catalog", "default", "flint_ppl_test"))
val dropColumns = DataFrameDropColumns(
Seq(UnresolvedAttribute("name")), relation
)
val expectedPlan: LogicalPlan =
ExplainCommand(
Project(Seq(UnresolvedStar(Option.empty)), dropColumns),
ExplainMode.fromString("formatted"))

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

test("describe (extended) table query test") {
Expand Down
8 changes: 6 additions & 2 deletions ppl-spark-integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,12 @@ See the next samples of PPL queries :
- `describe table` This command is equal to the `DESCRIBE EXTENDED table` SQL command

**Explain**
- `explain | source = table | where a = 1 | fields a,b,c`
- `explain | describe table`
- `explain simple | source = table | where a = 1 | fields a,b,c`
- `explain extended | source = table`
- `explain codegen | source = table | dedup a | fields a,b,c`
- `explain cost | source = table | sort a | fields a,b,c`
- `explain formatted | source = table | fields - a`
- `explain simple | describe table`

**Fields**
- `source = table`
Expand Down
9 changes: 8 additions & 1 deletion ppl-spark-integration/src/main/antlr4/OpenSearchPPLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ NEW_FIELD: 'NEW_FIELD';
KMEANS: 'KMEANS';
AD: 'AD';
ML: 'ML';
EXPLAIN: 'EXPLAIN';

//Native JOIN KEYWORDS
JOIN: 'JOIN';
Expand All @@ -57,6 +56,14 @@ APPROXIMATE: 'APPROXIMATE';
SCOPE: 'SCOPE';
MAPPING: 'MAPPING';

//EXPLAIN KEYWORDS
EXPLAIN: 'EXPLAIN';
FORMATTED: 'FORMATTED';
COST: 'COST';
CODEGEN: 'CODEGEN';
EXTENDED: 'EXTENDED';
SIMPLE: 'SIMPLE';

// COMMAND ASSIST KEYWORDS
AS: 'AS';
BY: 'BY';
Expand Down
10 changes: 9 additions & 1 deletion ppl-spark-integration/src/main/antlr4/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ describeCommand
;

explainCommand
: EXPLAIN
: EXPLAIN explainMode
;

explainMode
: FORMATTED
| COST
| CODEGEN
| EXTENDED
| SIMPLE
;

showDataSourcesCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,31 @@
public class Explain extends Statement {

private Statement statement;
private ExplainMode explainMode;

public Explain(Query statement) {
public Explain(Query statement, String explainMode) {
this.statement = statement;
this.explainMode = ExplainMode.valueOf(explainMode);
}

public Statement getStatement() {
return statement;
}

public ExplainMode getExplainMode() {
return explainMode;
}

@Override
public <R, C> R accept(AbstractNodeVisitor<R, C> visitor, C context) {
return visitor.visitExplain(this, context);
}

public enum ExplainMode {
formatted,
cost,
codegen,
extended,
simple
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public LogicalPlan visitQuery(Query node, CatalystPlanContext context) {
@Override
public LogicalPlan visitExplain(Explain node, CatalystPlanContext context) {
node.getStatement().accept(this, context);
return context.apply(p -> new ExplainCommand(p, ExplainMode.fromString("simple")));
return context.apply(p -> new ExplainCommand(p, ExplainMode.fromString(node.getExplainMode().name())));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public AstStatementBuilder(AstBuilder astBuilder, StatementBuilderContext contex

@Override
public Statement visitDmlStatement(OpenSearchPPLParser.DmlStatementContext ctx) {
boolean explain = ctx.explainCommand() != null;
Query query = new Query(addSelectAll(astBuilder.visit(ctx)), context.getFetchSize());
return explain ? new Explain(query) : query;
OpenSearchPPLParser.ExplainCommandContext explainContext = ctx.explainCommand();
if (explainContext != null) {
return new Explain(query, explainContext.explainMode().getText());
}
return query;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class PPLLogicalPlanBasicQueriesTranslatorTestSuite
test("test fields + field list") {
val context = new CatalystPlanContext
val logPlan = planTransformer.visit(
plan(pplParser, "source=t | sort - A | fields + A, B | head 5", false),
plan(pplParser, "source=t | sort - A | fields + A, B | head 5"),
context)

val table = UnresolvedRelation(Seq("t"))
Expand All @@ -298,7 +298,7 @@ class PPLLogicalPlanBasicQueriesTranslatorTestSuite
test("test fields - field list") {
val context = new CatalystPlanContext
val logPlan = planTransformer.visit(
plan(pplParser, "source=t | sort - A | fields - A, B | head 5", false),
plan(pplParser, "source=t | sort - A | fields - A, B | head 5"),
context)

val table = UnresolvedRelation(Seq("t"))
Expand Down

0 comments on commit a2ab018

Please sign in to comment.