Skip to content

Commit

Permalink
Migrate TimeWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGekk committed Feb 9, 2024
1 parent 51cfec7 commit a463178
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions common/utils/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -7670,6 +7670,11 @@
"Field not found: <name>"
]
},
"_LEGACY_ERROR_TEMP_3231" : {
"message" : [
"Intervals greater than a month is not supported (<interval>)."
]
},
"_LEGACY_ERROR_USER_RAISED_EXCEPTION" : {
"message" : [
"<errorMessage>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.spark.sql.catalyst.expressions

import org.apache.spark.SparkIllegalArgumentException
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodeGenerator, ExprCode}
Expand Down Expand Up @@ -199,8 +200,9 @@ object TimeWindow {
def getIntervalInMicroSeconds(interval: String): Long = {
val cal = IntervalUtils.fromIntervalString(interval)
if (cal.months != 0) {
throw new IllegalArgumentException(
s"Intervals greater than a month is not supported ($interval).")
throw new SparkIllegalArgumentException(
errorClass = "_LEGACY_ERROR_TEMP_3231",
messageParameters = Map("interval" -> interval))
}
Math.addExact(Math.multiplyExact(cal.days, MICROS_PER_DAY), cal.microseconds)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

package org.apache.spark.sql.catalyst.expressions

import scala.jdk.CollectionConverters._
import scala.reflect.ClassTag

import org.scalatest.PrivateMethodTester

import org.apache.spark.{SparkException, SparkFunSuite}
import org.apache.spark.{SparkException, SparkFunSuite, SparkIllegalArgumentException, SparkThrowable}
import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.util.DateTimeConstants._
import org.apache.spark.sql.internal.SQLConf
Expand All @@ -35,7 +36,8 @@ class TimeWindowSuite extends SparkFunSuite with ExpressionEvalHelper with Priva
}
}

private def checkErrorMessage[E <: Exception : ClassTag](msg: String, value: String): Unit = {
private def checkErrorMessage[E <: SparkThrowable : ClassTag](
parameters: Map[String, String], value: String): Unit = {
val validDuration = "10 second"
val validTime = "5 second"
val e1 = intercept[E] {
Expand All @@ -48,25 +50,25 @@ class TimeWindowSuite extends SparkFunSuite with ExpressionEvalHelper with Priva
TimeWindow(Literal(10L), validDuration, validDuration, value).startTime
}
Seq(e1, e2, e3).foreach { e =>
e.getMessage.contains(msg)
assert(e.getMessageParameters().asScala == parameters)
}
}

test("blank intervals throw exception") {
for (blank <- Seq(null, " ", "\n", "\t")) {
for ((blank, i) <- Seq((null, "''"), (" ", "' '"), ("\n", "'\n'"), ("\t", "'\t'"))) {
checkErrorMessage[AnalysisException](
"The window duration, slide duration and start time cannot be null or blank.", blank)
Map("intervalString" -> i), blank)
}
}

test("invalid intervals throw exception") {
checkErrorMessage[AnalysisException](
"did not correspond to a valid interval string.", "2 apples")
Map("intervalString" -> "'2 apples'"), "2 apples")
}

test("intervals greater than a month throws exception") {
checkErrorMessage[IllegalArgumentException](
"Intervals greater than or equal to a month is not supported (1 month).", "1 month")
checkErrorMessage[SparkIllegalArgumentException](
Map("interval" -> "1 month"), "1 month")
}

test("interval strings work with and without 'interval' prefix and return microseconds") {
Expand Down

0 comments on commit a463178

Please sign in to comment.