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 @@ -186,13 +186,18 @@ class CSVFileFormat extends TextBasedFileFormat with DataSourceRegister {
}

private def verifySchema(schema: StructType): Unit = {
schema.foreach { field =>
field.dataType match {
case _: ArrayType | _: MapType | _: StructType =>
throw new UnsupportedOperationException(
s"CSV data source does not support ${field.dataType.simpleString} data type.")
def verifyType(dataType: DataType): Unit = dataType match {
case ByteType | ShortType | IntegerType | LongType | FloatType |
DoubleType | BooleanType | _: DecimalType | TimestampType |
DateType | StringType =>

case udt: UserDefinedType[_] => verifyType(udt.sqlType)
Copy link
Member Author

Choose a reason for hiding this comment

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

Meaning adding cases here for unsupported types.


case _ =>
}
throw new UnsupportedOperationException(
s"CSV data source does not support ${dataType.simpleString} data type.")
}

schema.foreach(field => verifyType(field.dataType))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ private[csv] object CSVTypeCast {
DateTimeUtils.millisToDays(DateTimeUtils.stringToTime(datum).getTime)
}
case _: StringType => UTF8String.fromString(datum)
case udt: UserDefinedType[_] => castTo(datum, udt.sqlType, nullable, options)
case _ => throw new RuntimeException(s"Unsupported type: ${castType.typeName}")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import org.apache.hadoop.io.SequenceFile.CompressionType
import org.apache.hadoop.io.compress.GzipCodec

import org.apache.spark.SparkException
import org.apache.spark.sql.{DataFrame, QueryTest, Row}
import org.apache.spark.sql.catalyst.util.DateTimeUtils
import org.apache.spark.sql.{DataFrame, QueryTest, Row, UDT}
import org.apache.spark.sql.test.{SharedSQLContext, SQLTestUtils}
import org.apache.spark.sql.types._

Expand Down Expand Up @@ -681,6 +680,19 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils {
Seq((1, Array("Tesla", "Chevy", "Ford"))).toDF("id", "brands").write.csv(csvDir)
}.getMessage
assert(msg.contains("CSV data source does not support array<string> data type"))

msg = intercept[UnsupportedOperationException] {
Seq((1, new UDT.MyDenseVector(Array(0.25, 2.25, 4.25)))).toDF("id", "vectors")
.write.csv(csvDir)
}.getMessage
assert(msg.contains("CSV data source does not support array<double> data type"))

msg = intercept[SparkException] {
val schema = StructType(StructField("a", new UDT.MyDenseVectorUDT(), true) :: Nil)
spark.range(1).write.csv(csvDir)
spark.read.schema(schema).csv(csvDir).collect()
}.getCause.getMessage
assert(msg.contains("Unsupported type: array"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class JsonHadoopFsRelationSuite extends HadoopFsRelationTest {
override protected def supportsDataType(dataType: DataType): Boolean = dataType match {
case _: NullType => false
case _: BinaryType => false
// `TimestampType` is disabled because `DatatypeConverter.parseDateTime()`
// in `DateTimeUtils` parses the formatted string wrongly when the date is
// too early. (e.g. "1600-07-13T08:36:32.847").
case _: TimestampType => false
case _: CalendarIntervalType => false
case _ => true
}
Expand Down