Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-10520][SQL] Allow average out of DateType #28754

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql.catalyst.expressions

import java.math.{BigDecimal => JavaBigDecimal}
import java.sql.Date
import java.time.ZoneId
import java.util.Locale
import java.util.concurrent.TimeUnit._
Expand Down Expand Up @@ -64,6 +65,7 @@ object Cast {

case (StringType, DateType) => true
case (TimestampType, DateType) => true
case (DoubleType, DateType) => true

case (StringType, CalendarIntervalType) => true

Expand Down Expand Up @@ -492,6 +494,8 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
private[this] def castToDate(from: DataType): Any => Any = from match {
case StringType =>
buildCast[UTF8String](_, s => DateTimeUtils.stringToDate(s, zoneId).orNull)
case DoubleType =>
buildCast[Double](_, daysSinceEpoch => daysSinceEpoch.toInt)
case TimestampType =>
// throw valid precision more than seconds, according to Hive.
// Timestamp.nanos is in 0 to 999,999,999, no more than a second.
Expand Down Expand Up @@ -718,8 +722,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
})
case BooleanType =>
buildCast[Boolean](_, b => if (b) 1d else 0d)
case DateType =>
buildCast[Int](_, d => null)
case DateType => _.asInstanceOf[Int].toDouble
case TimestampType =>
buildCast[Long](_, t => timestampToDouble(t))
case x: NumericType =>
Expand Down Expand Up @@ -1143,6 +1146,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
$evNull = true;
}
"""
case DoubleType => (c, evPrim, evNull) => code"$evPrim = (int) $c;"
case TimestampType =>
val zid = getZoneId()
(c, evPrim, evNull) =>
Expand Down Expand Up @@ -1605,7 +1609,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
case BooleanType =>
(c, evPrim, evNull) => code"$evPrim = $c ? 1.0d : 0.0d;"
case DateType =>
(c, evPrim, evNull) => code"$evNull = true;"
(c, evPrim, evNull) => code"$evPrim = (double) $c;"
case TimestampType =>
(c, evPrim, evNull) => code"$evPrim = ${timestampToDoubleCode(c)};"
case DecimalType() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ case class Average(child: Expression) extends DeclarativeAggregate with Implicit

override def children: Seq[Expression] = child :: Nil

override def inputTypes: Seq[AbstractDataType] = Seq(NumericType)
override def inputTypes: Seq[AbstractDataType] = Seq(NumericType, DateType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fokko, before you go further, can we check other DBMSes as references? I would like to avoid having a variant behaviour in Spark alone compared to other DBMSes ...

Copy link
Contributor Author

@Fokko Fokko Jun 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that makes sense. See the details below, let me know if I'm missing something, but I don't think there is a real consensus on the subject.

Postgres

For postgres, it is just unsupported

postgres@366ecc8a0fb9:/$ psql
psql (12.3 (Debian 12.3-1.pgdg100+1))
Type "help" for help.

postgres=# SELECT CAST(CAST('2020-01-01' AS DATE) AS decimal);
ERROR:  cannot cast type date to numeric
LINE 1: SELECT CAST(CAST('2020-01-01' AS DATE) AS decimal);
               ^

postgres=# SELECT CAST(CAST('2020-01-01' AS DATE) AS integer);
ERROR:  cannot cast type date to integer
LINE 1: SELECT CAST(CAST('2020-01-01' AS DATE) AS integer);
               ^

The way to get the epoch in days is:

postgres=# SELECT EXTRACT(DAYS FROM (now() - '1970-01-01'));
date_part 
-----------
    18422
(1 row)

MySQL

For MySQL it will convert it automatically to a YYYYMMDD format:

mysql> SELECT CAST(CAST('2020-01-01' AS DATE) AS decimal);
+---------------------------------------------+
| CAST(CAST('2020-01-01' AS DATE) AS decimal) |
+---------------------------------------------+
|                                    20200101 |
+---------------------------------------------+
1 row in set (0.00 sec)

Converting to an int is not allowed:

mysql> SELECT CAST(CAST('2020-01-01' AS DATE) AS int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int)' at line 1

mysql> SELECT CAST(CAST('2020-01-01' AS DATE) AS bigint);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bigint)' at line 1

BigQuery

Unsupported

image

https://cloud.google.com/bigquery/docs/reference/standard-sql/conversion_rules

Excel

The greatest DBMS of them all:

image

Which is the epoch since 01-01-1900 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HyukjinKwon what are your thoughts on this? Can we move this forward?


override def checkInputDataTypes(): TypeCheckResult =
TypeUtils.checkForNumericExpr(child.dataType, "function average")
override def checkInputDataTypes(): TypeCheckResult = {
val isNumeric = TypeUtils.checkForNumericExpr(child.dataType, "function average")

if(isNumeric.isFailure && child.dataType == DateType) {
TypeCheckResult.TypeCheckSuccess
} else {
isNumeric
}
}

override def nullable: Boolean = true

Expand All @@ -53,6 +60,7 @@ case class Average(child: Expression) extends DeclarativeAggregate with Implicit
private lazy val resultType = child.dataType match {
case DecimalType.Fixed(p, s) =>
DecimalType.bounded(p + 4, s + 4)
case DateType => DateType
case _ => DoubleType
}

Expand All @@ -77,9 +85,11 @@ case class Average(child: Expression) extends DeclarativeAggregate with Implicit
)

// If all input are nulls, count will be 0 and we will get null after the division.
override lazy val evaluateExpression = child.dataType match {
override lazy val evaluateExpression: Expression = child.dataType match {
case _: DecimalType =>
DecimalPrecision.decimalAndDecimal(sum / count.cast(DecimalType.LongDecimal)).cast(resultType)
case _: DateType =>
(sum / count).cast(resultType)
case _ =>
sum.cast(resultType) / count.cast(resultType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ abstract class CastSuiteBase extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(cast(d, IntegerType), null)
checkEvaluation(cast(d, LongType), null)
checkEvaluation(cast(d, FloatType), null)
checkEvaluation(cast(d, DoubleType), null)
checkEvaluation(cast(d, DoubleType), 0.0d)
checkEvaluation(cast(d, DecimalType.SYSTEM_DEFAULT), null)
checkEvaluation(cast(d, DecimalType(10, 2)), null)
checkEvaluation(cast(d, StringType), "1970-01-01")
Expand Down Expand Up @@ -644,7 +644,6 @@ abstract class CastSuiteBase extends SparkFunSuite with ExpressionEvalHelper {
assert(cast(1, DateType).checkInputDataTypes().isFailure)
assert(cast(1L, DateType).checkInputDataTypes().isFailure)
assert(cast(1.0.toFloat, DateType).checkInputDataTypes().isFailure)
assert(cast(1.0, DateType).checkInputDataTypes().isFailure)
}

test("SPARK-20302 cast with same structure") {
Expand Down Expand Up @@ -1319,6 +1318,21 @@ class CastSuite extends CastSuiteBase {
}
}

private val dateDaysSinceEpoch = 18389.0 // Days since epoch (1970-01-01)
private val date = Date.valueOf("2020-05-07")

test("SPARK-10520: Cast a Date to Double") {
withDefaultTimeZone(UTC) {
checkEvaluation(cast(Literal(date), DoubleType), dateDaysSinceEpoch)
}
}

test("SPARK-10520: Cast a Double to Date") {
withDefaultTimeZone(UTC) {
checkEvaluation(cast(Literal(dateDaysSinceEpoch), DateType), date)
}
}

test("cast a timestamp before the epoch 1970-01-01 00:00:00Z") {
withDefaultTimeZone(UTC) {
val negativeTs = Timestamp.valueOf("1900-05-05 18:34:56.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package org.apache.spark.sql

import java.sql.Date

import scala.util.Random

import org.scalatest.matchers.must.Matchers.the

import org.apache.spark.sql.catalyst.util.DateTimeTestUtils.{withDefaultTimeZone, UTC}
import org.apache.spark.sql.execution.WholeStageCodegenExec
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec, SortAggregateExec}
Expand Down Expand Up @@ -318,6 +321,14 @@ class DataFrameAggregateSuite extends QueryTest
Row(new java.math.BigDecimal(2), new java.math.BigDecimal(6)) :: Nil)
}

test("SPARK-10520: date average") {
withDefaultTimeZone(UTC) {
checkAnswer(
testDataDates.agg(avg($"a")),
Row(new Date(2011, 4, 3)))
}
}

test("null average") {
checkAnswer(
testData3.agg(avg($"b")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql.test

import java.nio.charset.StandardCharsets
import java.sql.Date

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, SparkSession, SQLContext, SQLImplicits}
Expand Down Expand Up @@ -73,6 +74,16 @@ private[sql] trait SQLTestData { self =>
df
}

protected lazy val testDataDates: DataFrame = {
val df = spark.sparkContext.parallelize(
TestDataDate(new Date(2000, 1, 1)) ::
TestDataDate(new Date(2010, 1, 1)) ::
TestDataDate(new Date(2015, 1, 1)) ::
TestDataDate(new Date(2020, 1, 1)) :: Nil, 2).toDF()
df.createOrReplaceTempView("testDates")
df
}

protected lazy val negativeData: DataFrame = {
val df = spark.sparkContext.parallelize(
(1 to 100).map(i => TestData(-i, (-i).toString))).toDF()
Expand Down Expand Up @@ -326,6 +337,7 @@ private[sql] object SQLTestData {
case class TestData(key: Int, value: String)
case class TestData2(a: Int, b: Int)
case class TestData3(a: Int, b: Option[Int])
case class TestDataDate(a: Date)
case class LargeAndSmallInts(a: Int, b: Int)
case class DecimalData(a: BigDecimal, b: BigDecimal)
case class BinaryData(a: Array[Byte], b: Int)
Expand Down