Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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 @@ -102,7 +102,7 @@ class CSVInferSchema(val options: CSVOptions) extends Serializable {
if (field == null || field.isEmpty || field == options.nullValue) {
typeSoFar
} else {
typeSoFar match {
val typeElemInfer = typeSoFar match {
case NullType => tryParseInteger(field)
case IntegerType => tryParseInteger(field)
case LongType => tryParseLong(field)
Comment thread
planga82 marked this conversation as resolved.
Expand All @@ -116,6 +116,7 @@ class CSVInferSchema(val options: CSVOptions) extends Serializable {
case other: DataType =>
throw new UnsupportedOperationException(s"Unexpected data type $other")
}
compatibleType(typeSoFar, typeElemInfer).getOrElse(StringType)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class CSVInferSchemaSuite extends SparkFunSuite with SQLHelper {
assert(inferSchema.inferField(IntegerType, "1.0") == DoubleType)
assert(inferSchema.inferField(DoubleType, null) == DoubleType)
assert(inferSchema.inferField(DoubleType, "test") == StringType)
assert(inferSchema.inferField(LongType, "2015-08-20 14:57:00") == TimestampType)
assert(inferSchema.inferField(DoubleType, "2015-08-20 15:57:00") == TimestampType)
assert(inferSchema.inferField(LongType, "True") == BooleanType)
assert(inferSchema.inferField(IntegerType, "FALSE") == BooleanType)
assert(inferSchema.inferField(TimestampType, "FALSE") == BooleanType)
assert(inferSchema.inferField(LongType, "2015-08-20 14:57:00") == StringType)
assert(inferSchema.inferField(DoubleType, "2015-08-20 15:57:00") == StringType)
assert(inferSchema.inferField(LongType, "True") == StringType)
assert(inferSchema.inferField(IntegerType, "FALSE") == StringType)
assert(inferSchema.inferField(TimestampType, "FALSE") == StringType)

val textValueOne = Long.MaxValue.toString + "0"
val decimalValueOne = new java.math.BigDecimal(textValueOne)
Expand Down
4 changes: 4 additions & 0 deletions sql/core/src/test/resources/test-data/mixed-types1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
col_mixed_types
2012
1997
True
Comment thread
planga82 marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ abstract class CSVSuite extends QueryTest with SharedSparkSession with TestCsvDa
private val valueMalformedFile = "test-data/value-malformed.csv"
private val badAfterGoodFile = "test-data/bad_after_good.csv"
private val malformedRowFile = "test-data/malformedRow.csv"
private val mixedTypes = "test-data/mixed-types1.csv"

/** Verifies data and schema. */
private def verifyCars(
Expand Down Expand Up @@ -2341,6 +2342,17 @@ abstract class CSVSuite extends QueryTest with SharedSparkSession with TestCsvDa
checkAnswer(csv, Row(null))
}
}

test("SPARK-32025: infer the schema from mixed-type values") {
val data = spark
.read
.format("csv")
.option("header", "true")
.option("inferSchema", "true")
.load(testFile(mixedTypes))

assert(data.schema.fields.last == StructField("col_mixed_types", StringType, true))
}
}

class CSVv1Suite extends CSVSuite {
Expand Down