-
Notifications
You must be signed in to change notification settings - Fork 33
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
Translate PPL-builtin functions to Spark-builtin functions #448
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.ppl.utils; | ||
|
||
import org.apache.spark.sql.catalyst.analysis.UnresolvedFunction; | ||
import org.apache.spark.sql.catalyst.expressions.Expression; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import static org.opensearch.sql.ppl.utils.DataTypeTransformer.seq; | ||
import static scala.Option.empty; | ||
|
||
public interface BuiltinFunctionTranslator { | ||
|
||
static Expression builtinFunction(org.opensearch.sql.ast.expression.Function function, List<Expression> args) { | ||
if (BuiltinFunctionName.of(function.getFuncName()).isEmpty()) { | ||
// TODO should we support UDF in future? | ||
throw new IllegalStateException("Unknown builtin function: " + function.getFuncName()); | ||
} else { | ||
String name = BuiltinFunctionName.of(function.getFuncName()).get().name().toLowerCase(Locale.ROOT); | ||
return new UnresolvedFunction(seq(name), seq(args), false, empty(),false); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,14 @@ | |
|
||
package org.opensearch.flint.spark.ppl | ||
|
||
import org.apache.spark.sql.catalyst.analysis.AnalysisTest | ||
import org.apache.spark.sql.catalyst.expressions.{Alias, ExprId} | ||
import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan, Project} | ||
|
||
/** | ||
* general utility functions for ppl to spark transformation test | ||
*/ | ||
trait LogicalPlanTestUtils { | ||
trait LogicalPlanTestUtils extends AnalysisTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what / where will this trait be use is ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I tried to |
||
|
||
/** | ||
* utility function to compare two logical plans while ignoring the auto-generated expressionId | ||
|
@@ -52,5 +53,4 @@ trait LogicalPlanTestUtils { | |
// Return the string representation of the transformed plan | ||
transformedPlan.toString | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.spark.ppl | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.opensearch.flint.spark.ppl.PlaneUtils.plan | ||
import org.opensearch.sql.common.antlr.SyntaxCheckException | ||
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.{EqualTo, Literal} | ||
import org.apache.spark.sql.catalyst.plans.logical.{Filter, Project} | ||
|
||
class PPLLogicalPlanMathFunctionsTranslatorTestSuite | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add additional IT tests that use these commands with a more complex context of full fledge query use cases There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
extends SparkFunSuite | ||
with LogicalPlanTestUtils | ||
with Matchers { | ||
|
||
private val planTransformer = new CatalystQueryPlanVisitor() | ||
private val pplParser = new PPLSyntaxParser() | ||
|
||
test("test abs") { | ||
val context = new CatalystPlanContext | ||
val logPlan = planTransformer.visit(plan(pplParser, "source=t a = abs(b)", false), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("abs", seq(UnresolvedAttribute("b")), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
assertEquals(expectedPlan, logPlan) | ||
} | ||
|
||
test("test ceil") { | ||
val context = new CatalystPlanContext | ||
val logPlan = planTransformer.visit(plan(pplParser, "source=t a = ceil(b)", false), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("ceil", seq(UnresolvedAttribute("b")), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
assertEquals(expectedPlan, logPlan) | ||
} | ||
|
||
test("test floor") { | ||
val context = new CatalystPlanContext | ||
val logPlan = planTransformer.visit(plan(pplParser, "source=t a = floor(b)", false), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("floor", seq(UnresolvedAttribute("b")), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
assertEquals(expectedPlan, logPlan) | ||
} | ||
|
||
test("test ln") { | ||
val context = new CatalystPlanContext | ||
val logPlan = planTransformer.visit(plan(pplParser, "source=t a = ln(b)", false), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("ln", seq(UnresolvedAttribute("b")), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
assertEquals(expectedPlan, logPlan) | ||
} | ||
|
||
test("test mod") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit(plan(pplParser, "source=t a = mod(10, 4)", false), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("mod", seq(Literal(10), Literal(4)), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
assertEquals(expectedPlan, logPlan) | ||
} | ||
|
||
test("test pow") { | ||
val context = new CatalystPlanContext | ||
val logPlan = planTransformer.visit(plan(pplParser, "source=t a = pow(2, 3)", false), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("pow", seq(Literal(2), Literal(3)), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
assertEquals(expectedPlan, logPlan) | ||
} | ||
|
||
test("test sqrt") { | ||
val context = new CatalystPlanContext | ||
val logPlan = planTransformer.visit(plan(pplParser, "source=t a = sqrt(b)", false), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("sqrt", seq(UnresolvedAttribute("b")), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
assertEquals(expectedPlan, logPlan) | ||
} | ||
|
||
test("test arithmetic: + - * / %") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a few more complex test for such operators There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan(pplParser, "source=t a = b % 2 + 1 * 5 + 10 / 2", false), | ||
context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction( | ||
"add", | ||
seq( | ||
UnresolvedFunction( | ||
"add", | ||
seq( | ||
UnresolvedFunction( | ||
"modulus", | ||
seq(UnresolvedAttribute("b"), Literal(2)), | ||
isDistinct = false), | ||
UnresolvedFunction("multiply", seq(Literal(1), Literal(5)), isDistinct = false)), | ||
isDistinct = false), | ||
UnresolvedFunction("divide", seq(Literal(10), Literal(2)), isDistinct = false)), | ||
isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
assertEquals(expectedPlan, logPlan) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
inExpr
back since it is required byposition
functions.