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-4985][SQL Parquet] Parquet date support #3855

Closed
wants to merge 3 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.
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 @@ -372,7 +372,7 @@ case object TimestampType extends NativeType {
* @group dataType
*/
@DeveloperApi
case object DateType extends NativeType {
case object DateType extends NativeType with PrimitiveType {
private[sql] type JvmType = Date

@transient private[sql] lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package org.apache.spark.sql.parquet

import java.sql.Date

import org.apache.spark.sql.types.decimal.Decimal

import scala.collection.mutable.{Buffer, ArrayBuffer, HashMap}

import parquet.io.api.{PrimitiveConverter, GroupConverter, Binary, Converter}
Expand Down Expand Up @@ -180,6 +184,9 @@ private[parquet] abstract class CatalystConverter extends GroupConverter {
protected[parquet] def updateInt(fieldIndex: Int, value: Int): Unit =
updateField(fieldIndex, value)

protected[parquet] def updateDate(fieldIndex: Int, value: Int): Unit =
updateField(fieldIndex, new Date(value))

protected[parquet] def updateLong(fieldIndex: Int, value: Long): Unit =
updateField(fieldIndex, value)

Expand Down Expand Up @@ -367,6 +374,9 @@ private[parquet] class CatalystPrimitiveRowConverter(
override protected[parquet] def updateInt(fieldIndex: Int, value: Int): Unit =
current.setInt(fieldIndex, value)

override protected[parquet] def updateDate(fieldIndex: Int, value: Int): Unit =
current.update(fieldIndex, new Date(value))

override protected[parquet] def updateLong(fieldIndex: Int, value: Long): Unit =
current.setLong(fieldIndex, value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private[parquet] class RowWriteSupport extends WriteSupport[Row] with Logging {
}
}

private[parquet] def writePrimitive(schema: PrimitiveType, value: Any): Unit = {
private[parquet] def writePrimitive(schema: DataType, value: Any): Unit = {
if (value != null) {
schema match {
case StringType => writer.addBinary(
Expand All @@ -207,6 +207,7 @@ private[parquet] class RowWriteSupport extends WriteSupport[Row] with Logging {
case DoubleType => writer.addDouble(value.asInstanceOf[Double])
case FloatType => writer.addFloat(value.asInstanceOf[Float])
case BooleanType => writer.addBoolean(value.asInstanceOf[Boolean])
case DateType => writer.addInteger(value.asInstanceOf[java.sql.Date].getTime.toInt)
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't conform to the Parquet specification for date and produces invalid data.

When using the DATE annotation, the value must be the number of days from the Unix epoch, 1 January 1970. java.sql.Date and java.util.Date are backed by a long timestamp, the number of milliseconds from the Unix epoch (which is a Parquet TIMESTAMP_MILLIS) and casting that value to an integer makes it impossible to recover the real date.

I recommend using TIMESTAMP_MILLIS instead of date here (you won't need the toInt part). That seems to be what you want, if you're interested in using java.sql.Date. The reason why there is a name mismatch is that the Parquet types mirror SQL types more closely than Java objects.

case d: DecimalType =>
if (d.precisionInfo == None || d.precisionInfo.get.precision > 18) {
sys.error(s"Unsupported datatype $d, cannot write to consumer")
Expand Down Expand Up @@ -352,6 +353,7 @@ private[parquet] class MutableRowWriteSupport extends RowWriteSupport {
case DoubleType => writer.addDouble(record.getDouble(index))
case FloatType => writer.addFloat(record.getFloat(index))
case BooleanType => writer.addBoolean(record.getBoolean(index))
case DateType => writer.addInteger(record.getAs[java.sql.Date](index).getTime.toInt)
case d: DecimalType =>
if (d.precisionInfo == None || d.precisionInfo.get.precision > 18) {
sys.error(s"Unsupported datatype $d, cannot write to consumer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ private[parquet] object ParquetTypesConverter extends Logging {
case ParquetPrimitiveTypeName.BOOLEAN => BooleanType
case ParquetPrimitiveTypeName.DOUBLE => DoubleType
case ParquetPrimitiveTypeName.FLOAT => FloatType
case ParquetPrimitiveTypeName.INT32
if originalType == ParquetOriginalType.DATE => DateType
case ParquetPrimitiveTypeName.INT32 => IntegerType
case ParquetPrimitiveTypeName.INT64 => LongType
case ParquetPrimitiveTypeName.INT96 =>
Expand Down Expand Up @@ -209,6 +211,8 @@ private[parquet] object ParquetTypesConverter extends Logging {
// There is no type for Byte or Short so we promote them to INT32.
case ShortType => Some(ParquetTypeInfo(ParquetPrimitiveTypeName.INT32))
case ByteType => Some(ParquetTypeInfo(ParquetPrimitiveTypeName.INT32))
case DateType => Some(ParquetTypeInfo(
ParquetPrimitiveTypeName.INT32, Some(ParquetOriginalType.DATE)))
case LongType => Some(ParquetTypeInfo(ParquetPrimitiveTypeName.INT64))
case DecimalType.Fixed(precision, scale) if precision <= 18 =>
// TODO: for now, our writer only supports decimals that fit in a Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ class ParquetSchemaSuite extends FunSuite with ParquetTest {
|}
""".stripMargin)

testSchema[(Byte, Short, Int, Long)](
testSchema[(Byte, Short, Int, Long, java.sql.Date)](
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Usually I'd prefer import java.sql.Date and just use Date here.

"logical integral types",
"""
|message root {
| required int32 _1 (INT_8);
| required int32 _2 (INT_16);
| required int32 _3 (INT_32);
| required int64 _4 (INT_64);
| optional int32 _5 (DATE);
|}
""".stripMargin)

Expand Down