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
workaround for avg
  • Loading branch information
yaooqinn committed Dec 24, 2019
commit ba44c5a7a5cee3e46464103fb31e36aaa2a2c12d
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 =>
DivideInterval(sum.cast(resultType), count.cast(DoubleType))
DivideInterval(sum.cast(resultType), count.cast(DoubleType), false)
case _ =>
sum.cast(resultType) / count.cast(resultType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ abstract class IntervalNumOperation(interval: Expression, num: Expression)
try {
operation(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
} catch {
case _: ArithmeticException if (!checkOverflow) => null
case _: ArithmeticException if !checkOverflow => null
}
}

Expand Down Expand Up @@ -168,7 +168,10 @@ case class MultiplyInterval(interval: Expression, num: Expression)
override protected val operationName: String = if (checkOverflow) "multiply" else "safeMultiply"
}

case class DivideInterval(interval: Expression, num: Expression)
case class DivideInterval(
interval: Expression,
num: Expression,
override val checkOverflow: Boolean = SQLConf.get.ansiEnabled)
extends IntervalNumOperation(interval, num) {

override protected def operation(interval: CalendarInterval, num: Double): CalendarInterval = {
Expand Down
25 changes: 13 additions & 12 deletions sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -995,19 +995,17 @@ struct<i:int,sum(CAST(v AS INTERVAL)) OVER (ORDER BY i ASC NULLS FIRST ROWS BETW
-- !query 105
select avg(cast(v as interval)) from VALUES (null) t(v)
-- !query 105 schema
struct<>
struct<avg(CAST(v AS INTERVAL)):interval>
-- !query 105 output
java.lang.ArithmeticException
divide by zero
NULL


-- !query 106
select avg(cast(v as interval)) from VALUES ('1 seconds'), ('2 seconds'), (null) t(v) where 1=0
-- !query 106 schema
struct<>
struct<avg(CAST(v AS INTERVAL)):interval>
-- !query 106 output
java.lang.ArithmeticException
divide by zero
NULL


-- !query 107
Expand Down Expand Up @@ -1049,10 +1047,11 @@ select
from VALUES (1, '-1 weeks'), (2, '2 seconds'), (3, null), (1, '5 days') t(i, v)
group by i
-- !query 111 schema
struct<>
struct<i:int,avg(CAST(v AS INTERVAL)):interval>
-- !query 111 output
java.lang.ArithmeticException
divide by zero
1 -1 days
2 2 seconds
3 NULL


-- !query 112
Expand All @@ -1072,10 +1071,12 @@ SELECT
avg(cast(v as interval)) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
FROM VALUES (1,'1 seconds'), (1,'2 seconds'), (2,NULL), (2,NULL) t(i,v)
-- !query 113 schema
struct<>
struct<i:int,avg(CAST(v AS INTERVAL)) OVER (ORDER BY i ASC NULLS FIRST ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING):interval>
-- !query 113 output
java.lang.ArithmeticException
divide by zero
1 1.5 seconds
1 2 seconds
2 NULL
2 NULL


-- !query 114
Expand Down
18 changes: 9 additions & 9 deletions sql/core/src/test/resources/sql-tests/results/interval.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -183,55 +183,55 @@ struct<min(CAST(v AS INTERVAL)):interval>
-- !query 22
select 3 * (timestamp'2019-10-15 10:11:12.001002' - date'2019-10-15')
-- !query 22 schema
struct<Multiply_interval(subtracttimestamps(TIMESTAMP '2019-10-15 10:11:12.001002', CAST(DATE '2019-10-15' AS TIMESTAMP)), CAST(3 AS DOUBLE)):interval>
struct<multiply_interval(subtracttimestamps(TIMESTAMP '2019-10-15 10:11:12.001002', CAST(DATE '2019-10-15' AS TIMESTAMP)), CAST(3 AS DOUBLE)):interval>
-- !query 22 output
30 hours 33 minutes 36.003006 seconds


-- !query 23
select interval 4 month 2 weeks 3 microseconds * 1.5
-- !query 23 schema
struct<Multiply_interval(INTERVAL '4 months 14 days 0.000003 seconds', CAST(1.5 AS DOUBLE)):interval>
struct<multiply_interval(INTERVAL '4 months 14 days 0.000003 seconds', CAST(1.5 AS DOUBLE)):interval>
-- !query 23 output
6 months 21 days 0.000005 seconds


-- !query 24
select (timestamp'2019-10-15' - timestamp'2019-10-14') / 1.5
-- !query 24 schema
struct<Divide_interval(subtracttimestamps(TIMESTAMP '2019-10-15 00:00:00', TIMESTAMP '2019-10-14 00:00:00'), CAST(1.5 AS DOUBLE)):interval>
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 24 output
16 hours


-- !query 25
select interval '2 seconds' / 0
-- !query 25 schema
struct<Divide_interval(INTERVAL '2 seconds', CAST(0 AS DOUBLE)):interval>
struct<divide_interval(INTERVAL '2 seconds', CAST(0 AS DOUBLE)):interval>
-- !query 25 output
NULL


-- !query 26
select interval '2 seconds' / null
-- !query 26 schema
struct<Divide_interval(INTERVAL '2 seconds', CAST(NULL AS DOUBLE)):interval>
struct<divide_interval(INTERVAL '2 seconds', CAST(NULL AS DOUBLE)):interval>
-- !query 26 output
NULL


-- !query 27
select interval '2 seconds' * null
-- !query 27 schema
struct<Multiply_interval(INTERVAL '2 seconds', CAST(NULL AS DOUBLE)):interval>
struct<multiply_interval(INTERVAL '2 seconds', CAST(NULL AS DOUBLE)):interval>
-- !query 27 output
NULL


-- !query 28
select null * interval '2 seconds'
-- !query 28 schema
struct<Multiply_interval(INTERVAL '2 seconds', CAST(NULL AS DOUBLE)):interval>
struct<multiply_interval(INTERVAL '2 seconds', CAST(NULL AS DOUBLE)):interval>
-- !query 28 output
NULL

Expand Down Expand Up @@ -1226,14 +1226,14 @@ struct<(b + INTERVAL '1 months'):interval>
-- !query 129
select a * 1.1 from values (interval '-2147483648 months', interval '2147483647 months') t(a, b)
-- !query 129 schema
struct<Multiply_interval(a, CAST(1.1 AS DOUBLE)):interval>
struct<multiply_interval(a, CAST(1.1 AS DOUBLE)):interval>
-- !query 129 output
-178956970 years -8 months -2147483648 days -2562047788 hours -54.775808 seconds


-- !query 130
select a / 0.5 from values (interval '-2147483648 months', interval '2147483647 months') t(a, b)
-- !query 130 schema
struct<Divide_interval(a, CAST(0.5 AS DOUBLE)):interval>
struct<divide_interval(a, CAST(0.5 AS DOUBLE)):interval>
-- !query 130 output
-178956970 years -8 months -2147483648 days -2562047788 hours -54.775808 seconds