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 @@ -85,7 +85,9 @@ private[csv] object CSVInferSchema {
case NullType => tryParseInteger(field, options)
case IntegerType => tryParseInteger(field, options)
case LongType => tryParseLong(field, options)
case _: DecimalType => tryParseDecimal(field, options)
case _: DecimalType =>
// DecimalTypes have different precisions and scales, so we try to find the common type.
findTightestCommonType(typeSoFar, tryParseDecimal(field, options)).getOrElse(StringType)
case DoubleType => tryParseDouble(field, options)
case TimestampType => tryParseTimestamp(field, options)
case BooleanType => tryParseBoolean(field, options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,21 @@ class CSVInferSchemaSuite extends SparkFunSuite {
val options = new CSVOptions(Map("TiMeStampFormat" -> "yyyy-mm"))
assert(CSVInferSchema.inferField(TimestampType, "2015-08", options) == TimestampType)
}

test("SPARK-18877: `inferField` on DecimalType should find a common type with `typeSoFar`") {
val options = new CSVOptions(Map.empty[String, String])

// 9.03E+12 is Decimal(3, -10) and 1.19E+11 is Decimal(3, -9).
assert(CSVInferSchema.inferField(DecimalType(3, -10), "1.19E+11", options) ==
DecimalType(4, -9))

// BigDecimal("12345678901234567890.01234567890123456789") is precision 40 and scale 20.
val value = "12345678901234567890.01234567890123456789"
assert(CSVInferSchema.inferField(DecimalType(3, -10), value, options) == DoubleType)

// Seq(s"${Long.MaxValue}1", "2015-12-01 00:00:00") should be StringType
assert(CSVInferSchema.inferField(NullType, s"${Long.MaxValue}1", options) == DecimalType(20, 0))
assert(CSVInferSchema.inferField(DecimalType(20, 0), "2015-12-01 00:00:00", options)
== StringType)
}
}