Skip to content

Fix schema/type inference issue #261

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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
15 changes: 8 additions & 7 deletions src/main/scala/com/databricks/spark/csv/util/InferSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ private[csv] object InferSchema {
mergeRowTypes)

val structFields = header.zip(rootTypes).map { case (thisHeader, rootType) =>
StructField(thisHeader, rootType, nullable = true)
val dType = rootType match {
Copy link
Member Author

Choose a reason for hiding this comment

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

BTW, does this not produce Nulltype after merging Nulltypes?
It looks the test Merging Nulltypes should yeild Nulltype is not covering this.

Copy link
Contributor

Choose a reason for hiding this comment

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

@HyukjinKwon Please elaborate. Referring to doc comments in InferSchema.scala

/**

  • Similar to the JSON schema inference.
  • [[org.apache.spark.sql.execution.datasources.json.InferSchema]]
  • 1. Infer type of each row
    
  • 2. Merge row types to find common type
    
  • 3. Replace any null types with string type
    
    */

All null types should be replaced with String types at the end. This is what is happening right now?
If I understand you correctly, you feel there is a need for some more tests to be added. Could you please mention what that test is suppose to test?

As far as testing merging two nulltypes returning null type, is already covered.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, sorry. Please ignore my comment.

case z: NullType => StringType
case other => other
}
StructField(thisHeader, dType, nullable = true)
}

StructType(structFields)
Expand All @@ -62,11 +66,7 @@ private[csv] object InferSchema {
first: Array[DataType],
second: Array[DataType]): Array[DataType] = {
first.zipAll(second, NullType, NullType).map { case ((a, b)) =>
val tpe = findTightestCommonType(a, b).getOrElse(StringType)
tpe match {
case _: NullType => StringType
case other => other
}
findTightestCommonType(a, b).getOrElse(NullType)
}
}

Expand All @@ -93,7 +93,6 @@ private[csv] object InferSchema {
}
}


private def tryParseInteger(field: String): DataType = if ((allCatch opt field.toInt).isDefined) {
IntegerType
} else {
Expand Down Expand Up @@ -152,6 +151,8 @@ private[csv] object InferSchema {
case (t1, t2) if t1 == t2 => Some(t1)
case (NullType, t1) => Some(t1)
case (t1, NullType) => Some(t1)
case (StringType, t2) => Some(StringType)
case (t1, StringType) => Some(StringType)

// Promote numeric types to the highest of the two and all numeric types to unlimited decimal
case (t1, t2) if Seq(t1, t2).forall(numericPrecedence.contains) =>
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/simple.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
A,B,C,D
1,,,
,1,,
,,1,
,,,1
13 changes: 12 additions & 1 deletion src/test/scala/com/databricks/spark/csv/CsvSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ abstract class AbstractCsvSuite extends FunSuite with BeforeAndAfterAll {
val tempEmptyDir = "target/test/empty/"
val commentsFile = "src/test/resources/comments.csv"
val disableCommentsFile = "src/test/resources/disable_comments.csv"
private val simpleDatasetFile = "src/test/resources/simple.csv"

val numCars = 3

Expand Down Expand Up @@ -658,7 +659,6 @@ abstract class AbstractCsvSuite extends FunSuite with BeforeAndAfterAll {
assert(results.toSeq.map(_.toSeq) === expected)
}


test("Setting comment to null disables comment support") {
val results: Array[Row] = new CsvParser()
.withDelimiter(',')
Expand Down Expand Up @@ -717,6 +717,17 @@ abstract class AbstractCsvSuite extends FunSuite with BeforeAndAfterAll {

assert(results.size === numCars)
}

test("Type/Schema inference works as expected for the simple sparse dataset.") {
val df = new CsvParser()
.withUseHeader(true)
.withInferSchema(true)
.csvFile(sqlContext, simpleDatasetFile)

assert(
df.schema.fields.map(_.dataType).deep ==
Array(IntegerType, IntegerType, IntegerType, IntegerType).deep)
}
}

class CsvSuite extends AbstractCsvSuite {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class InferSchemaSuite extends FunSuite {
assert(InferSchema.inferField(LongType, "2015-08 14:49:00") == StringType)
}

test("Merging Nulltypes should yeild Nulltype.") {
assert(
InferSchema.mergeRowTypes(Array(NullType),
Array(NullType)).deep == Array(NullType).deep)
}

test("Type arrays are merged to highest common type") {
assert(
InferSchema.mergeRowTypes(Array(StringType),
Expand Down