-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kacper Trochimiak <[email protected]>
- Loading branch information
1 parent
8a010b7
commit a539744
Showing
2 changed files
with
162 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...c/test/scala/org/opensearch/flint/spark/ppl/PPLLogicalPlanRenameTranslatorTestSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.spark.ppl | ||
|
||
import org.opensearch.flint.spark.ppl.PlaneUtils.plan | ||
import org.opensearch.sql.ppl.{CatalystPlanContext, CatalystQueryPlanVisitor} | ||
import org.opensearch.sql.ppl.utils.DataTypeTransformer.seq | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.sql.catalyst.analysis.{UnresolvedAttribute, UnresolvedFunction, UnresolvedRelation, UnresolvedStar} | ||
import org.apache.spark.sql.catalyst.expressions.{Alias, Descending, ExprId, NamedExpression, SortOrder} | ||
import org.apache.spark.sql.catalyst.plans.PlanTest | ||
import org.apache.spark.sql.catalyst.plans.logical.{Project, Sort} | ||
|
||
class PPLLogicalPlanRenameTranslatorTestSuite | ||
extends SparkFunSuite | ||
with PlanTest | ||
with LogicalPlanTestUtils | ||
with Matchers { | ||
|
||
private val planTransformer = new CatalystQueryPlanVisitor() | ||
private val pplParser = new PPLSyntaxParser() | ||
|
||
test("test renamed fields not included in fields expressions") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan(pplParser, "source=t | rename a as r_a, b as r_b | fields c", false), | ||
context) | ||
val renameProjectList: Seq[NamedExpression] = | ||
Seq( | ||
UnresolvedStar(None), | ||
Alias(UnresolvedAttribute("a"), "r_a")(), | ||
Alias(UnresolvedAttribute("b"), "r_b")()) | ||
val expectedPlan = Project( | ||
seq(UnresolvedAttribute("c")), | ||
Project(renameProjectList, UnresolvedRelation(Seq("t")))) | ||
comparePlans(expectedPlan, logPlan, checkAnalysis = false) | ||
} | ||
|
||
test("test renamed fields included in fields expression") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan(pplParser, "source=t | rename a as r_a, b as r_b | fields r_a, r_b, c", false), | ||
context) | ||
|
||
val renameProjectList: Seq[NamedExpression] = | ||
Seq( | ||
UnresolvedStar(None), | ||
Alias(UnresolvedAttribute("a"), "r_a")(), | ||
Alias(UnresolvedAttribute("b"), "r_b")()) | ||
val expectedPlan = Project( | ||
seq(UnresolvedAttribute("r_a"), UnresolvedAttribute("r_b"), UnresolvedAttribute("c")), | ||
Project(renameProjectList, UnresolvedRelation(Seq("t")))) | ||
comparePlans(expectedPlan, logPlan, checkAnalysis = false) | ||
} | ||
|
||
test("test renamed fields without fields command") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan(pplParser, "source=t | rename a as r_a, b as r_b", false), | ||
context) | ||
|
||
val renameProjectList: Seq[NamedExpression] = | ||
Seq( | ||
UnresolvedStar(None), | ||
Alias(UnresolvedAttribute("a"), "r_a")(), | ||
Alias(UnresolvedAttribute("b"), "r_b")()) | ||
val expectedPlan = | ||
Project(seq(UnresolvedStar(None)), Project(renameProjectList, UnresolvedRelation(Seq("t")))) | ||
comparePlans(expectedPlan, logPlan, checkAnalysis = false) | ||
} | ||
|
||
test("test renamed fields with sort") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan(pplParser, "source=t | rename a as r_a, b as r_b | sort - r_a | fields r_b", false), | ||
context) | ||
|
||
val renameProjectList: Seq[NamedExpression] = | ||
Seq( | ||
UnresolvedStar(None), | ||
Alias(UnresolvedAttribute("a"), "r_a")(), | ||
Alias(UnresolvedAttribute("b"), "r_b")()) | ||
val renameProject = Project(renameProjectList, UnresolvedRelation(Seq("t"))) | ||
val sortOrder = SortOrder(UnresolvedAttribute("r_a"), Descending, Seq.empty) | ||
val sort = Sort(seq(sortOrder), global = true, renameProject) | ||
val expectedPlan = Project(seq(UnresolvedAttribute("r_b")), sort) | ||
comparePlans(expectedPlan, logPlan, checkAnalysis = false) | ||
} | ||
|
||
test("test rename eval expression output") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan( | ||
pplParser, | ||
"source=t | eval a = RAND() | rename a as eval_rand | fields eval_rand", | ||
false), | ||
context) | ||
|
||
val evalProjectList: Seq[NamedExpression] = Seq( | ||
UnresolvedStar(None), | ||
Alias(UnresolvedFunction("rand", Seq.empty, isDistinct = false), "a")( | ||
exprId = ExprId(0), | ||
qualifier = Seq.empty)) | ||
val evalProject = Project(evalProjectList, UnresolvedRelation(Seq("t"))) | ||
val renameProjectList: Seq[NamedExpression] = | ||
Seq(UnresolvedStar(None), Alias(UnresolvedAttribute("a"), "eval_rand")()) | ||
val expectedPlan = | ||
Project(seq(UnresolvedAttribute("eval_rand")), Project(renameProjectList, evalProject)) | ||
comparePlans(expectedPlan, logPlan, checkAnalysis = false) | ||
} | ||
} |