Skip to content

[SPARK-4336] [SQL] support type auto detection from json string #3202

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 1 commit 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 @@ -180,7 +180,10 @@ private[sql] object JsonRDD extends Logging {
}

private def typeOfPrimitiveValue: PartialFunction[Any, DataType] = {
ScalaReflection.typeOfObject orElse {
def autoDetect: PartialFunction[Any, DataType] = {
case value: String => DataTypeConversions.guessTypeFromString(value)
}
autoDetect orElse ScalaReflection.typeOfObject orElse {
// Since we do not have a data type backed by BigInteger,
// when we see a Java BigInteger, we use DecimalType.
case value: java.math.BigInteger => DecimalType.Unlimited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ protected[sql] object DataTypeConversions {
StructType(structType.getFields.map(asScalaStructField))
}

def guessTypeFromString(s:String): DataType = {
try {
val res = stringToTime(s)
Copy link
Contributor

Choose a reason for hiding this comment

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

This function will be called against every string in json, so it should be very fast. It should fail fast in most cases if the s is not a time, the path in catch is extremely slow, see [1].

[1] http://stackoverflow.com/questions/299068/how-slow-are-java-exceptions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I will use regex to judge then.

Copy link
Contributor

Choose a reason for hiding this comment

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

regex maybe is not fast enough, we still need some quick test to fail fast, such as check it contains some special characters?

Could you post some micro benchmark of it?

if (res.isInstanceOf[java.sql.Date]) {
DateType
} else {
TimestampType
}
} catch {
case _: Throwable => StringType
}
}

def stringToTime(s: String): java.util.Date = {
if (!s.contains('T')) {
// JDBC escape string
Expand Down
24 changes: 16 additions & 8 deletions sql/core/src/test/scala/org/apache/spark/sql/json/JsonSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ class JsonSuite extends QueryTest {
StructField("integer", IntegerType, true) ::
StructField("long", LongType, true) ::
StructField("null", StringType, true) ::
StructField("string", StringType, true) :: Nil)
StructField("string", StringType, true) ::
StructField("time", TimestampType, true) :: Nil)

assert(expectedSchema === jsonSchemaRDD.schema)

Expand All @@ -216,7 +217,8 @@ class JsonSuite extends QueryTest {
10,
21474836470L,
null,
"this is a simple string.") :: Nil
"this is a simple string.",
java.sql.Timestamp.valueOf("2014-11-11 12:00:00.0")) :: Nil
)
}

Expand Down Expand Up @@ -540,7 +542,8 @@ class JsonSuite extends QueryTest {
StructField("integer", IntegerType, true) ::
StructField("long", LongType, true) ::
StructField("null", StringType, true) ::
StructField("string", StringType, true) :: Nil)
StructField("string", StringType, true) ::
StructField("time", TimestampType, true) :: Nil)

assert(expectedSchema === jsonSchemaRDD.schema)

Expand All @@ -554,7 +557,8 @@ class JsonSuite extends QueryTest {
10,
21474836470L,
null,
"this is a simple string.") :: Nil
"this is a simple string.",
java.sql.Timestamp.valueOf("2014-11-11 12:00:00.0")) :: Nil
)
}

Expand All @@ -580,7 +584,8 @@ class JsonSuite extends QueryTest {
10,
21474836470L,
null,
"this is a simple string.") :: Nil
"this is a simple string.",
java.sql.Timestamp.valueOf("2014-11-11 12:00:00.0")) :: Nil
)
}

Expand All @@ -596,7 +601,8 @@ class JsonSuite extends QueryTest {
StructField("integer", IntegerType, true) ::
StructField("long", LongType, true) ::
StructField("null", StringType, true) ::
StructField("string", StringType, true) :: Nil)
StructField("string", StringType, true) ::
StructField("time", TimestampType, true) :: Nil)

val jsonSchemaRDD1 = jsonFile(path, schema)

Expand All @@ -612,7 +618,8 @@ class JsonSuite extends QueryTest {
10,
21474836470L,
null,
"this is a simple string.") :: Nil
"this is a simple string.",
java.sql.Timestamp.valueOf("2014-11-11 12:00:00.0")) :: Nil
)

val jsonSchemaRDD2 = jsonRDD(primitiveFieldAndType, schema)
Expand All @@ -629,7 +636,8 @@ class JsonSuite extends QueryTest {
10,
21474836470L,
null,
"this is a simple string.") :: Nil
"this is a simple string.",
java.sql.Timestamp.valueOf("2014-11-11 12:00:00.0")) :: Nil
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ object TestJsonData {
"bigInteger":92233720368547758070,
"double":1.7976931348623157E308,
"boolean":true,
"null":null
"null":null,
"time":"2014-11-11 12:00:00.0"
}""" :: Nil)

val primitiveFieldValueTypeConflict =
Expand Down