Skip to content

Commit 919d551

Browse files
committed
Revert "[SPARK-29390][SQL] Add the justify_days(), justify_hours() and justif_interval() functions"
This reverts commit f926809. Closes apache#27032 from gatorsmile/revertSPARK-29390. Authored-by: Xiao Li <[email protected]> Signed-off-by: Xiao Li <[email protected]>
1 parent 724dcf0 commit 919d551

File tree

9 files changed

+524
-884
lines changed

9 files changed

+524
-884
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,6 @@ object FunctionRegistry {
422422
expression[MakeDate]("make_date"),
423423
expression[MakeTimestamp]("make_timestamp"),
424424
expression[MakeInterval]("make_interval"),
425-
expression[JustifyDays]("justify_days"),
426-
expression[JustifyHours]("justify_hours"),
427-
expression[JustifyInterval]("justify_interval"),
428425
expression[DatePart]("date_part"),
429426

430427
// collection functions

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package org.apache.spark.sql.catalyst.expressions
1919

2020
import java.util.Locale
2121

22-
import scala.util.control.NonFatal
23-
2422
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode}
2523
import org.apache.spark.sql.catalyst.util.IntervalUtils
2624
import org.apache.spark.sql.catalyst.util.IntervalUtils._
@@ -259,69 +257,3 @@ case class MakeInterval(
259257

260258
override def prettyName: String = "make_interval"
261259
}
262-
263-
abstract class IntervalJustifyLike(
264-
child: Expression,
265-
justify: CalendarInterval => CalendarInterval,
266-
justifyFuncName: String) extends UnaryExpression with ExpectsInputTypes {
267-
override def inputTypes: Seq[AbstractDataType] = Seq(CalendarIntervalType)
268-
269-
override def dataType: DataType = CalendarIntervalType
270-
271-
override def nullSafeEval(input: Any): Any = {
272-
try {
273-
justify(input.asInstanceOf[CalendarInterval])
274-
} catch {
275-
case NonFatal(_) => null
276-
}
277-
}
278-
279-
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
280-
nullSafeCodeGen(ctx, ev, child => {
281-
val iu = IntervalUtils.getClass.getCanonicalName.stripSuffix("$")
282-
s"""
283-
|try {
284-
| ${ev.value} = $iu.$justifyFuncName($child);
285-
|} catch (java.lang.ArithmeticException e) {
286-
| ${ev.isNull} = true;
287-
|}
288-
|""".stripMargin
289-
})
290-
}
291-
292-
override def prettyName: String = justifyFuncName
293-
}
294-
295-
@ExpressionDescription(
296-
usage = "_FUNC_(expr) - Adjust interval so 30-day time periods are represented as months",
297-
examples = """
298-
Examples:
299-
> SELECT _FUNC_(interval '1 month -59 day 25 hour');
300-
-29 days 25 hours
301-
""",
302-
since = "3.0.0")
303-
case class JustifyDays(child: Expression)
304-
extends IntervalJustifyLike(child, justifyDays, "justifyDays")
305-
306-
@ExpressionDescription(
307-
usage = "_FUNC_(expr) - Adjust interval so 24-hour time periods are represented as days",
308-
examples = """
309-
Examples:
310-
> SELECT _FUNC_(interval '1 month -59 day 25 hour');
311-
1 months -57 days -23 hours
312-
""",
313-
since = "3.0.0")
314-
case class JustifyHours(child: Expression)
315-
extends IntervalJustifyLike(child, justifyHours, "justifyHours")
316-
317-
@ExpressionDescription(
318-
usage = "_FUNC_(expr) - Adjust interval using justifyHours and justifyDays, with additional" +
319-
" sign adjustments",
320-
examples = """
321-
Examples:
322-
> SELECT _FUNC_(interval '1 month -59 day 25 hour');
323-
-27 days -23 hours
324-
""",
325-
since = "3.0.0")
326-
case class JustifyInterval(child: Expression)
327-
extends IntervalJustifyLike(child, justifyInterval, "justifyInterval")

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -790,40 +790,4 @@ object IntervalUtils {
790790

791791
new CalendarInterval(totalMonths, totalDays, micros)
792792
}
793-
794-
/**
795-
* Adjust interval so 30-day time periods are represented as months.
796-
*/
797-
def justifyDays(interval: CalendarInterval): CalendarInterval = {
798-
val monthToDays = interval.months * DAYS_PER_MONTH
799-
val totalDays = monthToDays + interval.days
800-
val months = Math.toIntExact(totalDays / DAYS_PER_MONTH)
801-
val days = totalDays % DAYS_PER_MONTH
802-
new CalendarInterval(months, days.toInt, interval.microseconds)
803-
}
804-
805-
/**
806-
* Adjust interval so 24-hour time periods are represented as days.
807-
*/
808-
def justifyHours(interval: CalendarInterval): CalendarInterval = {
809-
val dayToUs = MICROS_PER_DAY * interval.days
810-
val totalUs = Math.addExact(interval.microseconds, dayToUs)
811-
val days = totalUs / MICROS_PER_DAY
812-
val microseconds = totalUs % MICROS_PER_DAY
813-
new CalendarInterval(interval.months, days.toInt, microseconds)
814-
}
815-
816-
/**
817-
* Adjust interval using justifyHours and justifyDays, with additional sign adjustments.
818-
*/
819-
def justifyInterval(interval: CalendarInterval): CalendarInterval = {
820-
val monthToDays = DAYS_PER_MONTH * interval.months
821-
val dayToUs = Math.multiplyExact(monthToDays + interval.days, MICROS_PER_DAY)
822-
val totalUs = Math.addExact(interval.microseconds, dayToUs)
823-
val microseconds = totalUs % MICROS_PER_DAY
824-
val totalDays = totalUs / MICROS_PER_DAY
825-
val days = totalDays % DAYS_PER_MONTH
826-
val months = totalDays / DAYS_PER_MONTH
827-
new CalendarInterval(months.toInt, days.toInt, microseconds)
828-
}
829793
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/IntervalUtilsSuite.scala

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -300,31 +300,6 @@ class IntervalUtilsSuite extends SparkFunSuite with SQLHelper {
300300
}
301301
}
302302

303-
test("justify days") {
304-
checkFromStringWithFunc("1 month 35 day", 2, 5, 0, justifyDays)
305-
checkFromStringWithFunc("-1 month 35 day", 0, 5, 0, justifyDays)
306-
checkFromStringWithFunc("1 month -35 day", 0, -5, 0, justifyDays)
307-
checkFromStringWithFunc("-1 month -35 day", -2, -5, 0, justifyDays)
308-
checkFromStringWithFunc("-1 month 2 day", 0, -28, 0, justifyDays)
309-
}
310-
311-
test("justify hours") {
312-
checkFromStringWithFunc("29 day 25 hour", 0, 30, 1 * MICROS_PER_HOUR, justifyHours)
313-
checkFromStringWithFunc("29 day -25 hour", 0, 27, 23 * MICROS_PER_HOUR, justifyHours)
314-
checkFromStringWithFunc("-29 day 25 hour", 0, -27, -23 * MICROS_PER_HOUR, justifyHours)
315-
checkFromStringWithFunc("-29 day -25 hour", 0, -30, -1 * MICROS_PER_HOUR, justifyHours)
316-
}
317-
318-
test("justify interval") {
319-
checkFromStringWithFunc("1 month 29 day 25 hour", 2, 0, 1 * MICROS_PER_HOUR, justifyInterval)
320-
checkFromStringWithFunc("-1 month 29 day -25 hour", 0, -2, -1 * MICROS_PER_HOUR,
321-
justifyInterval)
322-
checkFromStringWithFunc("1 month -29 day -25 hour", 0, 0, -1 * MICROS_PER_HOUR, justifyInterval)
323-
checkFromStringWithFunc("-1 month -29 day -25 hour", -2, 0, -1 * MICROS_PER_HOUR,
324-
justifyInterval)
325-
intercept[ArithmeticException](justifyInterval(new CalendarInterval(2, 0, Long.MaxValue)))
326-
}
327-
328303
test("to ansi sql standard string") {
329304
val i1 = new CalendarInterval(0, 0, 0)
330305
assert(IntervalUtils.toSqlStandardString(i1) === "0")

sql/core/src/test/resources/sql-tests/inputs/interval.sql

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,6 @@ select cast('- 1 second' as interval);
7777
select cast('- -1 second' as interval);
7878
select cast('- +1 second' as interval);
7979

80-
-- justify intervals
81-
select justify_days(cast(null as interval));
82-
select justify_hours(cast(null as interval));
83-
select justify_interval(cast(null as interval));
84-
select justify_days(interval '1 month 59 day 25 hour');
85-
select justify_hours(interval '1 month 59 day 25 hour');
86-
select justify_interval(interval '1 month 59 day 25 hour');
87-
select justify_days(interval '1 month -59 day 25 hour');
88-
select justify_hours(interval '1 month -59 day 25 hour');
89-
select justify_interval(interval '1 month -59 day 25 hour');
90-
select justify_days(interval '1 month 59 day -25 hour');
91-
select justify_hours(interval '1 month 59 day -25 hour');
92-
select justify_interval(interval '1 month 59 day -25 hour');
93-
9480
-- interval literal
9581
select interval 13.123456789 seconds, interval -13.123456789 second;
9682
select interval 1 year 2 month 3 week 4 day 5 hour 6 minute 7 seconds 8 millisecond 9 microsecond;

sql/core/src/test/resources/sql-tests/inputs/postgreSQL/interval.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,13 @@
157157
-- select '100000000y 10mon -1000000000d -100000h -10min -10.000001s ago'::interval;
158158

159159
-- test justify_hours() and justify_days()
160-
SELECT justify_hours(interval '6 months 3 days 52 hours 3 minutes 2 seconds') as `6 mons 5 days 4 hours 3 mins 2 seconds`;
161-
SELECT justify_days(interval '6 months 36 days 5 hours 4 minutes 3 seconds') as `7 mons 6 days 5 hours 4 mins 3 seconds`;
160+
-- [SPARK-29390] Add the justify_days(), justify_hours() and justify_interval() functions
161+
-- SELECT justify_hours(interval '6 months 3 days 52 hours 3 minutes 2 seconds') as `6 mons 5 days 4 hours 3 mins 2 seconds`;
162+
-- SELECT justify_days(interval '6 months 36 days 5 hours 4 minutes 3 seconds') as `7 mons 6 days 5 hours 4 mins 3 seconds`;
162163

163164
-- test justify_interval()
164-
SELECT justify_interval(interval '1 month -1 hour') as `1 month -1 hour`;
165+
166+
-- SELECT justify_interval(interval '1 month -1 hour') as `1 month -1 hour`;
165167

166168
-- test fractional second input, and detection of duplicate units
167169
-- [SPARK-28259] Date/Time Output Styles and Date Order Conventions

0 commit comments

Comments
 (0)