yaooqinn commented on a change in pull request #26702: [SPARK-30070][SQL]
Support ANSI datetimes predicate - overlaps
URL: https://github.com/apache/spark/pull/26702#discussion_r355343902
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
##########
@@ -2165,3 +2165,75 @@ case class SubtractDates(left: Expression, right:
Expression)
}
}
+/**
+ * The operator `OVERLAPS` determines whether or not two chronological periods
overlap in time. A
+ * chronological period is specified by a pair of datetimes (starting and
ending) or as a
+ * starting datetime and an interval(resolved by analyzer to datetimes pairs
early).
+ *
+ * If the length of the period is greater than 0, then the period consists of
all points of time
+ * greater than or equal to the lower endpoint, and less than the upper
endpoint,
+ * a.k.a [lower, upper).
+ *
+ * If the length of the period is equal to 0, then the period consists of a
single point in time,
+ * the lower endpoint, a.k.a [lower, lower].
+ *
+ * Two periods overlap if they have at least one point in common.
+ *
+ */
+case class DateTimeOverlaps private (
+ leftStart: Expression,
+ leftEnd: Expression,
+ rightStart: Expression,
+ rightEnd: Expression,
+ child: Expression)
+ extends RuntimeReplaceable {
+
+ def this(leftStart: Expression,
+ leftEnd: Expression,
+ rightStart: Expression,
+ rightEnd: Expression) =
+ this(leftStart, leftEnd, rightStart, rightEnd, {
+ val left = Seq(leftStart, leftEnd)
+ val right = Seq(rightStart, rightEnd)
+ If(And(EqualTo(leftStart, leftEnd), EqualTo(rightStart, rightEnd)), /**
both periods are 0 */
+ EqualTo(leftStart, rightStart),
+ If(EqualTo(leftStart, leftEnd), /** left period is 0, ll ∈ [rl, ru) */
+ And(GreaterThanOrEqual(leftStart, Least(right)), LessThan(leftStart,
Greatest(right))),
+ If(EqualTo(rightStart, rightEnd), /** right period is 0, rl ∈ [ll,
lu) */
+ And(GreaterThanOrEqual(rightStart, Least(left)),
LessThan(rightStart, Greatest(left))),
+ {
+ val timePoints = Seq(leftStart, leftEnd, rightStart, rightEnd)
+ val leftPeriod = If(GreaterThan(leftStart, leftEnd),
+ Subtract(leftStart, leftEnd), Subtract(leftEnd, leftStart))
+ val rightPeriod = If(GreaterThan(rightStart, rightEnd),
+ Subtract(rightStart, rightEnd), Subtract(rightEnd, rightStart))
+ LessThan(Subtract(Greatest(timePoints), Least(timePoints)),
+ Add(leftPeriod, rightPeriod))
Review comment:
looks nice
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]