-
Notifications
You must be signed in to change notification settings - Fork 37
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
cidrmatch
ppl command add logical tests and docs
#865
Merged
YANG-DB
merged 3 commits into
opensearch-project:main
from
YANG-DB:fix-cidrmatch-missing-logical-tests
Nov 4, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
155 changes: 155 additions & 0 deletions
155
...src/test/scala/org/opensearch/flint/spark/ppl/PPLLogicalPlanParseCidrmatchTestSuite.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,155 @@ | ||
/* | ||
* 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.expression.function.SerializableUdf | ||
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, And, Ascending, CaseWhen, Descending, EqualTo, GreaterThan, Literal, NullsFirst, NullsLast, RegExpExtract, ScalaUDF, SortOrder} | ||
import org.apache.spark.sql.catalyst.plans.PlanTest | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.types.DataTypes | ||
|
||
class PPLLogicalPlanParseCidrmatchTestSuite | ||
extends SparkFunSuite | ||
with PlanTest | ||
with LogicalPlanTestUtils | ||
with Matchers { | ||
|
||
private val planTransformer = new CatalystQueryPlanVisitor() | ||
private val pplParser = new PPLSyntaxParser() | ||
|
||
test("test cidrmatch for ipv4 for 192.168.1.0/24") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan( | ||
pplParser, | ||
"source=t | where isV6 = false and isValid = true and cidrmatch(ipAddress, '192.168.1.0/24')"), | ||
context) | ||
|
||
val ipAddress = UnresolvedAttribute("ipAddress") | ||
val cidrExpression = Literal("192.168.1.0/24") | ||
|
||
val filterIpv6 = EqualTo(UnresolvedAttribute("isV6"), Literal(false)) | ||
val filterIsValid = EqualTo(UnresolvedAttribute("isValid"), Literal(true)) | ||
val cidr = ScalaUDF( | ||
SerializableUdf.cidrFunction, | ||
DataTypes.BooleanType, | ||
seq(ipAddress, cidrExpression), | ||
seq(), | ||
Option.empty, | ||
Option.apply("cidr"), | ||
false, | ||
true) | ||
|
||
val expectedPlan = Project( | ||
Seq(UnresolvedStar(None)), | ||
Filter(And(And(filterIpv6, filterIsValid), cidr), UnresolvedRelation(Seq("t")))) | ||
assert(compareByString(expectedPlan) === compareByString(logPlan)) | ||
} | ||
|
||
test("test cidrmatch for ipv6 for 2003:db8::/32") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan( | ||
pplParser, | ||
"source=t | where isV6 = true and isValid = false and cidrmatch(ipAddress, '2003:db8::/32')"), | ||
context) | ||
|
||
val ipAddress = UnresolvedAttribute("ipAddress") | ||
val cidrExpression = Literal("2003:db8::/32") | ||
|
||
val filterIpv6 = EqualTo(UnresolvedAttribute("isV6"), Literal(true)) | ||
val filterIsValid = EqualTo(UnresolvedAttribute("isValid"), Literal(false)) | ||
val cidr = ScalaUDF( | ||
SerializableUdf.cidrFunction, | ||
DataTypes.BooleanType, | ||
seq(ipAddress, cidrExpression), | ||
seq(), | ||
Option.empty, | ||
Option.apply("cidr"), | ||
false, | ||
true) | ||
|
||
val expectedPlan = Project( | ||
Seq(UnresolvedStar(None)), | ||
Filter(And(And(filterIpv6, filterIsValid), cidr), UnresolvedRelation(Seq("t")))) | ||
assert(compareByString(expectedPlan) === compareByString(logPlan)) | ||
} | ||
|
||
test("test cidrmatch for ipv6 for 2003:db8::/32 with ip field projected") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan( | ||
pplParser, | ||
"source=t | where isV6 = true and cidrmatch(ipAddress, '2003:db8::/32') | fields ip"), | ||
context) | ||
|
||
val ipAddress = UnresolvedAttribute("ipAddress") | ||
val cidrExpression = Literal("2003:db8::/32") | ||
|
||
val filterIpv6 = EqualTo(UnresolvedAttribute("isV6"), Literal(true)) | ||
val cidr = ScalaUDF( | ||
SerializableUdf.cidrFunction, | ||
DataTypes.BooleanType, | ||
seq(ipAddress, cidrExpression), | ||
seq(), | ||
Option.empty, | ||
Option.apply("cidr"), | ||
false, | ||
true) | ||
|
||
val expectedPlan = Project( | ||
Seq(UnresolvedAttribute("ip")), | ||
Filter(And(filterIpv6, cidr), UnresolvedRelation(Seq("t")))) | ||
assert(compareByString(expectedPlan) === compareByString(logPlan)) | ||
} | ||
|
||
test("test cidrmatch for ipv6 for 2003:db8::/32 with ip field bool respond for each ip") { | ||
val context = new CatalystPlanContext | ||
val logPlan = | ||
planTransformer.visit( | ||
plan( | ||
pplParser, | ||
"source=t | where isV6 = true | eval inRange = case(cidrmatch(ipAddress, '2003:db8::/32'), 'in' else 'out') | fields ip, inRange"), | ||
context) | ||
|
||
val ipAddress = UnresolvedAttribute("ipAddress") | ||
val cidrExpression = Literal("2003:db8::/32") | ||
|
||
val filterIpv6 = EqualTo(UnresolvedAttribute("isV6"), Literal(true)) | ||
val filterClause = Filter(filterIpv6, UnresolvedRelation(Seq("t"))) | ||
val cidr = ScalaUDF( | ||
SerializableUdf.cidrFunction, | ||
DataTypes.BooleanType, | ||
seq(ipAddress, cidrExpression), | ||
seq(), | ||
Option.empty, | ||
Option.apply("cidr"), | ||
false, | ||
true) | ||
|
||
val equalTo = EqualTo(Literal(true), cidr) | ||
val caseFunction = CaseWhen(Seq((equalTo, Literal("in"))), Literal("out")) | ||
val aliasStatusCategory = Alias(caseFunction, "inRange")() | ||
val evalProjectList = Seq(UnresolvedStar(None), aliasStatusCategory) | ||
val evalProject = Project(evalProjectList, filterClause) | ||
|
||
val expectedPlan = | ||
Project(Seq(UnresolvedAttribute("ip"), UnresolvedAttribute("inRange")), evalProject) | ||
|
||
assert(compareByString(expectedPlan) === compareByString(logPlan)) | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
)
is missing