Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rebase
  • Loading branch information
yaooqinn committed Nov 6, 2019
commit 998bd9524d18b1d4f7d87f0f77472647cc9169bb
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,6 @@ object TypeCoercion {
SubtractTimestamps(l, Cast(r, TimestampType))
case Subtract(l @ DateType(), r @ TimestampType()) =>
SubtractTimestamps(Cast(l, TimestampType), r)

case Divide(l @ CalendarIntervalType(), r) => IntervalDivide(l, Cast(r, DecimalType(29, 9)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ package object dsl {
def * (other: Expression): Expression = Multiply(expr, other)
def / (other: Expression): Expression = Divide(expr, other)
def div (other: Expression): Expression = IntegralDivide(expr, other)
def intervalDiv (other: Expression): Expression = IntervalDivide(expr, other)
def % (other: Expression): Expression = Remainder(expr, other)
def & (other: Expression): Expression = BitwiseAnd(expr, other)
def | (other: Expression): Expression = BitwiseOr(expr, other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ case class Average(child: Expression) extends DeclarativeAggregate with Implicit
case _: DecimalType =>
DecimalPrecision.decimalAndDecimal(sum / count.cast(DecimalType.LongDecimal)).cast(resultType)
case CalendarIntervalType =>
sum.cast(resultType).intervalDiv(count.cast(DecimalType.LongDecimal))
DivideInterval(sum.cast(resultType), count.cast(DoubleType))
case _ =>
sum.cast(resultType) / count.cast(resultType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2163,33 +2163,3 @@ case class SubtractDates(left: Expression, right: Expression)
})
}
}

// scalastyle:off line.size.limit
@ExpressionDescription(
usage = "expr1 _FUNC_ expr2 - Divide interval value `expr1` by `expr2`. It returns NULL if `expr2` is 0 or NULL.",
examples = """
Examples:
> SELECT interval '1 year 2 month' / 3.0;
interval 4 months 20 days
""",
since = "3.0.0")
// scalastyle:on line.size.limit
case class IntervalDivide(left: Expression, right: Expression)
extends BinaryExpression with ImplicitCastInputTypes {

override def dataType: DataType = CalendarIntervalType

override def inputTypes: Seq[AbstractDataType] = Seq(CalendarIntervalType, DecimalType)

override def nullSafeEval(interval: Any, divisor: Any): Any = {
IntervalUtils.divide(interval.asInstanceOf[CalendarInterval],
divisor.asInstanceOf[Decimal])
}

override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, (interval, divisor) => {
val iu = IntervalUtils.getClass.getName.stripSuffix("$")
s"$iu.divide($interval, $divisor)"
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ object IntervalUtils {
Decimal(result, 18, 6)
}

def divide(interval: CalendarInterval, divisor: Decimal): CalendarInterval = {
if (divisor == Decimal.ZERO || divisor == null) return null
val months = Decimal(interval.months) / divisor
val days = months.remainder(Decimal.ONE) * Decimal(DAYS_PER_MONTH) +
Decimal(interval.days) / divisor
val milliseconds = days.remainder(Decimal.ONE) * Decimal(DateTimeUtils.MICROS_PER_DAY) +
Decimal(interval.microseconds) / divisor
new CalendarInterval(months.toInt, days.toInt, milliseconds.toLong)
}

/**
* Converts a string to [[CalendarInterval]] case-insensitively.
*
Expand Down
84 changes: 83 additions & 1 deletion sql/core/src/test/resources/sql-tests/results/datetime.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 20
-- Number of queries: 30


-- !query 0
Expand Down Expand Up @@ -169,3 +169,85 @@ select (timestamp'2019-10-15' - timestamp'2019-10-14') / 1.5
struct<divide_interval(subtracttimestamps(TIMESTAMP('2019-10-15 00:00:00'), TIMESTAMP('2019-10-14 00:00:00')), CAST(1.5 AS DOUBLE)):interval>
-- !query 19 output
interval 16 hours


-- !query 20
select (timestamp'2019-10-15' - timestamp'2019-10-14') / 0.0
-- !query 20 schema
struct<divide_interval(subtracttimestamps(TIMESTAMP('2019-10-15 00:00:00'), TIMESTAMP('2019-10-14 00:00:00')), CAST(0.0 AS DOUBLE)):interval>
-- !query 20 output
interval 0 microseconds


-- !query 21
select interval '1 year 2 month' / null
-- !query 21 schema
struct<>
-- !query 21 output
org.apache.spark.sql.AnalysisException
cannot resolve '(interval 1 years 2 months / NULL)' due to data type mismatch: differing types in '(interval 1 years 2 months / NULL)' (interval and null).; line 1 pos 7


-- !query 22
select interval '1 year 2 month' / 0
-- !query 22 schema
struct<divide_interval(interval 1 years 2 months, CAST(0 AS DOUBLE)):interval>
-- !query 22 output
NULL


-- !query 23
select interval '1 year 2 month' / 3
-- !query 23 schema
struct<divide_interval(interval 1 years 2 months, CAST(3 AS DOUBLE)):interval>
-- !query 23 output
interval 4 months 20 days


-- !query 24
select interval '1 year 2 month' / 3.0
-- !query 24 schema
struct<divide_interval(interval 1 years 2 months, CAST(3.0 AS DOUBLE)):interval>
-- !query 24 output
interval 4 months 20 days


-- !query 25
SELECT interval '4 months 2 weeks 6 days' * null
-- !query 25 schema
struct<>
-- !query 25 output
org.apache.spark.sql.AnalysisException
cannot resolve '(interval 4 months 20 days * NULL)' due to data type mismatch: differing types in '(interval 4 months 20 days * NULL)' (interval and null).; line 1 pos 7


-- !query 26
SELECT interval '4 months 2 weeks 6 days' * 0
-- !query 26 schema
struct<multiply_interval(interval 4 months 20 days, CAST(0 AS DOUBLE)):interval>
-- !query 26 output
interval 0 microseconds


-- !query 27
SELECT interval '4 months 2 weeks 6 days' * 3
-- !query 27 schema
struct<multiply_interval(interval 4 months 20 days, CAST(3 AS DOUBLE)):interval>
-- !query 27 output
interval 1 years 60 days


-- !query 28
SELECT interval '4 months 2 weeks 6 days' * 3.0
-- !query 28 schema
struct<multiply_interval(interval 4 months 20 days, CAST(3.0 AS DOUBLE)):interval>
-- !query 28 output
interval 1 years 60 days


-- !query 29
SELECT 3.0 * interval '4 months 2 weeks 6 days'
-- !query 29 schema
struct<multiply_interval(interval 4 months 20 days, CAST(3.0 AS DOUBLE)):interval>
-- !query 29 output
interval 1 years 60 days
12 changes: 6 additions & 6 deletions sql/core/src/test/resources/sql-tests/results/group-by.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -667,15 +667,15 @@ select avg(cast(v as interval)) from VALUES ('1 seconds'), ('2 seconds'), (null)
-- !query 65 schema
struct<avg(CAST(v AS INTERVAL)):interval>
-- !query 65 output
NULL
interval 0 microseconds


-- !query 66
select avg(cast(v as interval)) from VALUES ('1 seconds'), ('2 seconds'), (null) t(v) where 1=0
-- !query 66 schema
struct<avg(CAST(v AS INTERVAL)):interval>
-- !query 66 output
NULL
interval 0 microseconds


-- !query 67
Expand Down Expand Up @@ -721,7 +721,7 @@ struct<i:int,avg(CAST(v AS INTERVAL)):interval>
-- !query 71 output
1 interval -1 days
2 interval 2 seconds
3 NULL
3 interval 0 microseconds


-- !query 72
Expand All @@ -745,13 +745,13 @@ struct<i:int,avg(CAST(v AS INTERVAL)) OVER (ORDER BY i ASC NULLS FIRST ROWS BETW
-- !query 73 output
1 interval 1.5 seconds
1 interval 2 seconds
2 NULL
2 NULL
2 interval 0 microseconds
2 interval 0 microseconds


-- !query 74
SELECT interval '1 year 2 month' / 3.0
-- !query 74 schema
struct<intervaldivide(interval 1 years 2 months, CAST(3.0 AS DECIMAL(29,9))):interval>
struct<divide_interval(interval 1 years 2 months, CAST(3.0 AS DOUBLE)):interval>
-- !query 74 output
interval 4 months 20 days