Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2053,10 +2053,15 @@ case class DatePart(field: Expression, source: Expression, child: Expression)
if (!field.foldable) {
throw new AnalysisException("The field parameter needs to be a foldable string value.")
}
val fieldStr = field.eval().asInstanceOf[UTF8String].toString
DatePart.parseExtractField(fieldStr, source, {
throw new AnalysisException(s"Literals of type '$fieldStr' are currently not supported.")
})
val fieldEval = field.eval()
if (fieldEval == null) {
Literal(null, DoubleType)
Copy link
Contributor

Choose a reason for hiding this comment

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

DoubleType ?

Copy link
Member Author

Choose a reason for hiding this comment

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

See the example in the description. PostgreSQL returns NULL of the double precision type which is equivalent of DOUBLE type in Spark SQL. Which type would you use here?

} else {
val fieldStr = fieldEval.asInstanceOf[UTF8String].toString
DatePart.parseExtractField(fieldStr, source, {
throw new AnalysisException(s"Literals of type '$fieldStr' are currently not supported.")
})
}
})
}

Expand Down
2 changes: 2 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/date_part.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ select date_part('secs', c) from t;
select date_part('not_supported', c) from t;

select date_part(c, c) from t;

select date_part(null, c) from t;
10 changes: 9 additions & 1 deletion sql/core/src/test/resources/sql-tests/results/date_part.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 51
-- Number of queries: 52


-- !query 0
Expand Down Expand Up @@ -410,3 +410,11 @@ struct<>
-- !query 50 output
org.apache.spark.sql.AnalysisException
The field parameter needs to be a foldable string value.;; line 1 pos 7


-- !query 51
select date_part(null, c) from t
-- !query 51 schema
struct<date_part(NULL, t.`c`):double>
-- !query 51 output
NULL
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.apache.spark.sql.catalyst.util.DateTimeUtils
import org.apache.spark.sql.functions._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSparkSession
import org.apache.spark.sql.types.{DoubleType, StructField, StructType}
import org.apache.spark.unsafe.types.CalendarInterval

class DateFunctionsSuite extends QueryTest with SharedSparkSession {
Expand Down Expand Up @@ -796,4 +797,13 @@ class DateFunctionsSuite extends QueryTest with SharedSparkSession {
Seq(Row(Instant.parse(timestamp))))
}
}

test("handling null field by date_part") {
val input = Seq(Date.valueOf("2019-09-20")).toDF("d")
Seq("date_part(null, d)", "date_part(null, date'2019-09-20')").foreach { expr =>
val df = input.selectExpr(expr)
assert(df.schema.headOption.get.dataType == DoubleType)
checkAnswer(df, Row(null))
}
}
}