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-35998][SQL] Make from_csv/to_csv to handle year-month intervals properly #33210

Closed
wants to merge 2 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 @@ -22,7 +22,7 @@ import java.io.Writer
import com.univocity.parsers.csv.CsvWriter

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.util.{DateFormatter, TimestampFormatter}
import org.apache.spark.sql.catalyst.util.{DateFormatter, IntervalStringStyles, IntervalUtils, TimestampFormatter}
import org.apache.spark.sql.catalyst.util.LegacyDateFormats.FAST_DATE_FORMAT
import org.apache.spark.sql.types._

Expand Down Expand Up @@ -61,6 +61,11 @@ class UnivocityGenerator(
case TimestampType =>
(row: InternalRow, ordinal: Int) => timestampFormatter.format(row.getLong(ordinal))

case YearMonthIntervalType(start, end) =>
(row: InternalRow, ordinal: Int) =>
IntervalUtils.toYearMonthIntervalString(
row.getInt(ordinal), IntervalStringStyles.ANSI_STYLE, start, end)

case udt: UserDefinedType[_] => makeConverter(udt.sqlType)

case dt: DataType =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.univocity.parsers.csv.CsvParser
import org.apache.spark.SparkUpgradeException
import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.{InternalRow, NoopFilters, OrderedFilters}
import org.apache.spark.sql.catalyst.expressions.{ExprUtils, GenericInternalRow}
import org.apache.spark.sql.catalyst.expressions.{Cast, EmptyRow, ExprUtils, GenericInternalRow, Literal}
import org.apache.spark.sql.catalyst.util._
import org.apache.spark.sql.catalyst.util.LegacyDateFormats.FAST_DATE_FORMAT
import org.apache.spark.sql.errors.QueryExecutionErrors
Expand Down Expand Up @@ -217,6 +217,11 @@ class UnivocityParser(
IntervalUtils.safeStringToInterval(UTF8String.fromString(datum))
}

case ym: YearMonthIntervalType => (d: String) =>
nullSafeDatum(d, name, nullable, options) { datum =>
Cast(Literal(datum), ym).eval(EmptyRow)
}

case udt: UserDefinedType[_] =>
makeConverter(name, udt.sqlType, nullable)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql

import java.text.SimpleDateFormat
import java.time.Period
import java.util.Locale

import scala.collection.JavaConverters._
Expand All @@ -27,6 +28,7 @@ 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._
import org.apache.spark.sql.types.YearMonthIntervalType.{MONTH, YEAR}

class CsvFunctionsSuite extends QueryTest with SharedSparkSession {
import testImplicits._
Expand Down Expand Up @@ -279,4 +281,31 @@ class CsvFunctionsSuite extends QueryTest with SharedSparkSession {
}
}
}

test("SPARK-35998: Make from_csv/to_csv to handle year-month intervals properly") {
val ymDF = Seq(Period.of(1, 2, 0)).toDF
Seq(
(YearMonthIntervalType(), "INTERVAL '1-2' YEAR TO MONTH", Period.of(1, 2, 0)),
(YearMonthIntervalType(YEAR), "INTERVAL '1' YEAR", Period.of(1, 0, 0)),
(YearMonthIntervalType(MONTH), "INTERVAL '14' MONTH", Period.of(1, 2, 0))
).foreach { case (toCsvDtype, toCsvExpected, fromCsvExpected) =>
val toCsvDF = ymDF.select(to_csv(struct($"value" cast toCsvDtype)) as "csv")
checkAnswer(toCsvDF, Row(toCsvExpected))

DataTypeTestUtils.yearMonthIntervalTypes.foreach { fromCsvDtype =>
val fromCsvDF = toCsvDF
.select(
from_csv(
$"csv",
StructType(StructField("a", fromCsvDtype) :: Nil),
Map.empty[String, String]) as "value")
.selectExpr("value.a")
if (toCsvDtype == fromCsvDtype) {
checkAnswer(fromCsvDF, Row(fromCsvExpected))
} else {
checkAnswer(fromCsvDF, Row(null))
}
}
}
}
}