Skip to content

Commit

Permalink
Added IT tests
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrik Saly <[email protected]>
  • Loading branch information
salyh committed Oct 29, 2024
1 parent f8be3a7 commit a0f6af8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.opensearch.flint.spark.ppl

import java.sql.Timestamp

import org.apache.spark.sql.QueryTest
import org.apache.spark.sql.streaming.StreamTest

Expand Down Expand Up @@ -56,12 +58,12 @@ class FlintSparkPPLBetweenITSuite
| """.stripMargin)

val results = frame.collect()
assert(results.length == 3)
assert(results.length == 1)
assert(frame.columns.length == 6)

results.foreach(row => {
val age = row.getAs[Int]("age")
assert(age >= 20 && age <= 30, s"Age $age is not between 20 and 30")
assert(age >= 21 && age <= 29, s"Age $age is not between 21 and 29")
})
}

Expand All @@ -80,36 +82,53 @@ class FlintSparkPPLBetweenITSuite
})
}

test("test between should return records where NOT between two integer values") {
val frame = sql(s"""
| source = $testTable | where NOT age between 20 and 30
| """.stripMargin)

val results = frame.collect()
assert(results.length == 1)
assert(frame.columns.length == 6)

results.foreach(row => {
val age = row.getAs[Int]("age")
assert(age < 20 || age > 30, s"Age $age is not between 20 and 30")
})
}

test("test between should return records between two date values") {
val frame = sql(s"""
| source = $timeSeriesTestTable | where timestamp between '2023-10-01 00:01:00' and '2023-10-01 00:10:00'
| source = $timeSeriesTestTable | where time between '2023-10-01 00:01:00' and '2023-10-01 00:10:00'
| """.stripMargin)

val results = frame.collect()
assert(results.length == 2)
assert(frame.columns.length == 4)

results.foreach(row => {
val ts = row.getAs[String]("timestamp")
val ts = row.getAs[Timestamp]("time")
assert(
ts >= "2023-10-01 00:01:00" && ts <= "2023-10-01 00:01:00",
!ts.before(Timestamp.valueOf("2023-10-01 00:01:00")) || !ts.after(
Timestamp.valueOf("2023-10-01 00:01:00")),
s"Timestamp $ts is not between '2023-10-01 00:01:00' and '2023-10-01 00:10:00'")
})
}

test("test between should return records NOT between two date values") {
val frame = sql(s"""
| source = $timeSeriesTestTable | where timestamp NOT between '2023-10-01 00:01:00' and '2023-10-01 00:10:00'
| source = $timeSeriesTestTable | where time NOT between '2023-10-01 00:01:00' and '2023-10-01 00:10:00'
| """.stripMargin)

val results = frame.collect()
assert(results.length == 4)
assert(results.length == 3)
assert(frame.columns.length == 4)

results.foreach(row => {
val ts = row.getAs[String]("timestamp")
val ts = row.getAs[Timestamp]("time")
assert(
ts < "2023-10-01 00:01:00" || ts > "2023-10-01 00:01:00",
ts.before(Timestamp.valueOf("2023-10-01 00:01:00")) || ts.after(
Timestamp.valueOf("2023-10-01 00:01:00")),
s"Timestamp $ts is not between '2023-10-01 00:01:00' and '2023-10-01 00:10:00'")
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ logicalExpression
comparisonExpression
: left = valueExpression comparisonOperator right = valueExpression # compareExpr
| valueExpression NOT? IN valueList # inExpr
| expr1 = functionArg BETWEEN expr2 = functionArg AND expr3 = functionArg # between
| expr1 = functionArg NOT? BETWEEN expr2 = functionArg AND expr3 = functionArg # between
;

valueExpressionList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public UnresolvedExpression visitConvertedDataType(OpenSearchPPLParser.Converted
@Override
public UnresolvedExpression visitBetween(OpenSearchPPLParser.BetweenContext ctx) {
UnresolvedExpression betweenExpr = new Between(visit(ctx.expr1),visit(ctx.expr2),visit(ctx.expr3));
return betweenExpr;
return ctx.NOT() != null ? new Not(betweenExpr) : betweenExpr;
}

private Function buildFunction(
Expand Down

0 comments on commit a0f6af8

Please sign in to comment.