From 58ac1f7ce7725937e941a5defb9314bd449bc31e Mon Sep 17 00:00:00 2001 From: Lantao Jin Date: Fri, 11 Oct 2024 21:43:19 +0800 Subject: [PATCH] AND should have higher precedence than OR in predicate expression Signed-off-by: Lantao Jin --- .../src/main/antlr4/OpenSearchPPLParser.g4 | 2 +- ...ogicalPlanFiltersTranslatorTestSuite.scala | 34 +++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/ppl-spark-integration/src/main/antlr4/OpenSearchPPLParser.g4 b/ppl-spark-integration/src/main/antlr4/OpenSearchPPLParser.g4 index 7521faefe..af7e0ec14 100644 --- a/ppl-spark-integration/src/main/antlr4/OpenSearchPPLParser.g4 +++ b/ppl-spark-integration/src/main/antlr4/OpenSearchPPLParser.g4 @@ -358,8 +358,8 @@ expression logicalExpression : comparisonExpression # comparsion | NOT logicalExpression # logicalNot - | left = logicalExpression OR right = logicalExpression # logicalOr | left = logicalExpression (AND)? right = logicalExpression # logicalAnd + | left = logicalExpression OR right = logicalExpression # logicalOr | left = logicalExpression XOR right = logicalExpression # logicalXor | booleanExpression # booleanExpr | isEmptyExpression # isEmptyExpr diff --git a/ppl-spark-integration/src/test/scala/org/opensearch/flint/spark/ppl/PPLLogicalPlanFiltersTranslatorTestSuite.scala b/ppl-spark-integration/src/test/scala/org/opensearch/flint/spark/ppl/PPLLogicalPlanFiltersTranslatorTestSuite.scala index 407b3df84..20809db95 100644 --- a/ppl-spark-integration/src/test/scala/org/opensearch/flint/spark/ppl/PPLLogicalPlanFiltersTranslatorTestSuite.scala +++ b/ppl-spark-integration/src/test/scala/org/opensearch/flint/spark/ppl/PPLLogicalPlanFiltersTranslatorTestSuite.scala @@ -5,23 +5,15 @@ package org.opensearch.flint.spark.ppl -import org.junit.Assert.assertEquals -import org.mockito.Mockito.when import org.opensearch.flint.spark.ppl.PlaneUtils.plan import org.opensearch.sql.ppl.{CatalystPlanContext, CatalystQueryPlanVisitor} import org.scalatest.matchers.should.Matchers -import org.scalatestplus.mockito.MockitoSugar.mock import org.apache.spark.SparkFunSuite -import org.apache.spark.sql.catalyst.TableIdentifier -import org.apache.spark.sql.catalyst.analysis.{Analyzer, FunctionRegistry, TableFunctionRegistry, UnresolvedAttribute, UnresolvedRelation, UnresolvedStar} -import org.apache.spark.sql.catalyst.catalog._ -import org.apache.spark.sql.catalyst.expressions.{Alias, And, Ascending, Descending, Divide, EqualTo, Floor, GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual, Like, Literal, NamedExpression, Not, Or, SortOrder, UnixTimestamp} -import org.apache.spark.sql.catalyst.expressions.aggregate._ -import org.apache.spark.sql.catalyst.parser.ParserInterface +import org.apache.spark.sql.catalyst.analysis.{UnresolvedAttribute, UnresolvedRelation, UnresolvedStar} +import org.apache.spark.sql.catalyst.expressions.{And, Ascending, EqualTo, GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual, Literal, Not, Or, SortOrder} import org.apache.spark.sql.catalyst.plans.PlanTest import org.apache.spark.sql.catalyst.plans.logical._ -import org.apache.spark.sql.types.{IntegerType, StructField, StructType} class PPLLogicalPlanFiltersTranslatorTestSuite extends SparkFunSuite @@ -219,4 +211,26 @@ class PPLLogicalPlanFiltersTranslatorTestSuite comparePlans(expectedPlan, logPlan, false) } + + test("test order of evaluation of predicate expression") { + val context = new CatalystPlanContext + val logPlan = planTransformer.visit( + plan( + pplParser, + "source=employees | where department = 'HR' OR job_title = 'Manager' AND salary > 50000"), + context) + + val table = UnresolvedRelation(Seq("employees")) + val filter = + Filter( + Or( + EqualTo(UnresolvedAttribute("department"), Literal("HR")), + And( + EqualTo(UnresolvedAttribute("job_title"), Literal("Manager")), + GreaterThan(UnresolvedAttribute("salary"), Literal(50000)))), + table) + + val expectedPlan = Project(Seq(UnresolvedStar(None)), filter) + comparePlans(expectedPlan, logPlan, false) + } }