Skip to content

[SPARK-23094] Fix invalid character handling in JsonDataSource #20302

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 @@ -40,10 +40,11 @@ private[sql] object CreateJacksonParser extends Serializable {
}

def text(jsonFactory: JsonFactory, record: Text): JsonParser = {
jsonFactory.createParser(record.getBytes, 0, record.getLength)
val bain = new ByteArrayInputStream(record.getBytes, 0, record.getLength)
jsonFactory.createParser(new InputStreamReader(bain, "UTF-8"))
}

def inputStream(jsonFactory: JsonFactory, record: InputStream): JsonParser = {
jsonFactory.createParser(record)
jsonFactory.createParser(new InputStreamReader(record, "UTF-8"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import org.apache.spark.sql.types._
class JsonHadoopFsRelationSuite extends HadoopFsRelationTest {
override val dataSourceName: String = "json"

private val badJson = "\u0000\u0000\u0000A\u0001AAA"

// JSON does not write data of NullType and does not play well with BinaryType.
override protected def supportsDataType(dataType: DataType): Boolean = dataType match {
case _: NullType => false
Expand Down Expand Up @@ -105,4 +107,36 @@ class JsonHadoopFsRelationSuite extends HadoopFsRelationTest {
)
}
}

test("invalid json with leading nulls - from file (multiLine=true)") {
import testImplicits._
withTempDir { tempDir =>
val path = tempDir.getAbsolutePath
Seq(badJson, """{"a":1}""").toDS().write.mode("overwrite").text(path)
val expected = s"""$badJson\n{"a":1}\n"""
val schema = new StructType().add("a", IntegerType).add("_corrupt_record", StringType)
val df =
spark.read.format(dataSourceName).option("multiLine", true).schema(schema).load(path)
checkAnswer(df, Row(null, expected))
}
}

test("invalid json with leading nulls - from file (multiLine=false)") {
import testImplicits._
withTempDir { tempDir =>
val path = tempDir.getAbsolutePath
Seq(badJson, """{"a":1}""").toDS().write.mode("overwrite").text(path)
val schema = new StructType().add("a", IntegerType).add("_corrupt_record", StringType)
val df =
spark.read.format(dataSourceName).option("multiLine", false).schema(schema).load(path)
checkAnswer(df, Seq(Row(1, null), Row(null, badJson)))
}
}

test("invalid json with leading nulls - from dataset") {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: this case looks already passing tho, and I thought we should put this in JsonSuite.

Copy link
Member

Choose a reason for hiding this comment

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

The whole suite should be moved. Not related to this PR. I will take this.

Copy link
Member

Choose a reason for hiding this comment

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

I think usually we encourage to put the test cases in a better suite, in-place.

Copy link
Member

Choose a reason for hiding this comment

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

This test suite is still in org.apache.spark.sql.sources. We should move these test suite to /sql/core

Copy link
Member

Choose a reason for hiding this comment

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

See the PR #20331

import testImplicits._
checkAnswer(
spark.read.json(Seq(badJson).toDS()),
Row(badJson))
}
}