{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/180050283","pull_request_review_id":110401438,"id":180050283,"diff_hunk":"@@ -92,26 +93,30 @@ object TextInputJsonDataSource extends JsonDataSource {\n       sparkSession: SparkSession,\n       inputPaths: Seq[FileStatus],\n       parsedOptions: JSONOptions): StructType = {\n-    val json: Dataset[String] = createBaseDataset(\n-      sparkSession, inputPaths, parsedOptions.lineSeparator)\n+    val json: Dataset[String] = createBaseDataset(sparkSession, inputPaths, parsedOptions)\n+\n     inferFromDataset(json, parsedOptions)\n   }\n \n   def inferFromDataset(json: Dataset[String], parsedOptions: JSONOptions): StructType = {\n     val sampled: Dataset[String] = JsonUtils.sample(json, parsedOptions)\n-    val rdd: RDD[UTF8String] = sampled.queryExecution.toRdd.map(_.getUTF8String(0))\n-    JsonInferSchema.infer(rdd, parsedOptions, CreateJacksonParser.utf8String)\n+    val rdd: RDD[InternalRow] = sampled.queryExecution.toRdd\n+    val rowParser = parsedOptions.encoding.map { enc =>\n+      CreateJacksonParser.internalRow(enc, _: JsonFactory, _: InternalRow, 0)","path":"sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/json/JsonDataSource.scala","position":25,"original_position":25,"commit_id":"b817184d35d0e2589682f1dcd88b9f29b2063f5b","original_commit_id":"fcd0a214448c74ebb60a0fb92a92a7c3b21166c4","user":{"login":"MaxGekk","id":1580697,"avatar_url":"https://avatars1.githubusercontent.com/u/1580697?v=4","gravatar_id":"","url":"https://api.github.com/users/MaxGekk","html_url":"https://github.com/MaxGekk","followers_url":"https://api.github.com/users/MaxGekk/followers","following_url":"https://api.github.com/users/MaxGekk/following{/other_user}","gists_url":"https://api.github.com/users/MaxGekk/gists{/gist_id}","starred_url":"https://api.github.com/users/MaxGekk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MaxGekk/subscriptions","organizations_url":"https://api.github.com/users/MaxGekk/orgs","repos_url":"https://api.github.com/users/MaxGekk/repos","events_url":"https://api.github.com/users/MaxGekk/events{/privacy}","received_events_url":"https://api.github.com/users/MaxGekk/received_events","type":"User","site_admin":false},"body":"I didn't it originally but rejected the solution because overhead of wrapping the array by `ByteArrayInputStream` per-each row is very high. It increases execution time up to 20% in some cases.","created_at":"2018-04-09T10:25:05Z","updated_at":"2018-04-09T10:25:05Z","html_url":"https://github.com/apache/spark/pull/20937#discussion_r180050283","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/20937","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/180050283"},"html":{"href":"https://github.com/apache/spark/pull/20937#discussion_r180050283"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/20937"}},"in_reply_to_id":179952415},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/20937","id":178353446,"html_url":"https://github.com/apache/spark/pull/20937","diff_url":"https://github.com/apache/spark/pull/20937.diff","patch_url":"https://github.com/apache/spark/pull/20937.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/20937","number":20937,"state":"open","locked":false,"title":"[SPARK-23094][SPARK-23723][SPARK-23724][SQL] Support custom encoding for json files","user":{"login":"MaxGekk","id":1580697,"avatar_url":"https://avatars1.githubusercontent.com/u/1580697?v=4","gravatar_id":"","url":"https://api.github.com/users/MaxGekk","html_url":"https://github.com/MaxGekk","followers_url":"https://api.github.com/users/MaxGekk/followers","following_url":"https://api.github.com/users/MaxGekk/following{/other_user}","gists_url":"https://api.github.com/users/MaxGekk/gists{/gist_id}","starred_url":"https://api.github.com/users/MaxGekk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MaxGekk/subscriptions","organizations_url":"https://api.github.com/users/MaxGekk/orgs","repos_url":"https://api.github.com/users/MaxGekk/repos","events_url":"https://api.github.com/users/MaxGekk/events{/privacy}","received_events_url":"https://api.github.com/users/MaxGekk/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nI propose new option for JSON datasource which allows to specify encoding (charset) of input and output files. Here is an example of using of the option:\r\n\r\n```\r\nspark.read.schema(schema)\r\n  .option(\"multiline\", \"true\")\r\n  .option(\"encoding\", \"UTF-16LE\")\r\n  .json(fileName)\r\n```\r\n\r\nIf the option is not specified, charset auto-detection mechanism is used by default.\r\n\r\nThe option can be used for saving datasets to jsons. Currently Spark is able to save datasets into json files in `UTF-8` charset only. The changes allow to save data in any supported charset. Here is the approximate list of supported charsets by Oracle Java SE: https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html . An user can specify the charset of output jsons via the charset option like `.option(\"charset\", \"UTF-16BE\")`. By default the output charset is still `UTF-8` to keep backward compatibility.\r\n\r\nThe solution has the following restrictions for per-line mode (`multiline = false`):\r\n\r\n- If charset is different from UTF-8, the lineSep option must be specified. The option required because Hadoop LineReader cannot detect the line separator correctly. Here is the ticket for solving the issue: https://issues.apache.org/jira/browse/SPARK-23725\r\n\r\n- Encoding with [BOM](https://en.wikipedia.org/wiki/Byte_order_mark) are not supported. For example, the `UTF-16` and `UTF-32` encodings are blacklisted. The problem can be solved by https://github.com/MaxGekk/spark-1/pull/2\r\n\r\n## How was this patch tested?\r\n\r\nI added the following tests:\r\n- reads an json file in `UTF-16LE` encoding with BOM in `multiline` mode\r\n- read json file by using charset auto detection (`UTF-32BE` with BOM) \r\n- read json file using of user's charset (`UTF-16LE`)\r\n- saving in `UTF-32BE` and read the result by standard library (not by Spark)\r\n- checking that default charset is `UTF-8`\r\n- handling wrong (unsupported) charset\r\n","created_at":"2018-03-29T14:54:59Z","updated_at":"2018-04-09T10:25:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8225b68574006bc9e96285a43fda47210a1bb19e","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/20937/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/20937/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/20937/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/b817184d35d0e2589682f1dcd88b9f29b2063f5b","head":{"label":"MaxGekk:json-encoding-line-sep","ref":"json-encoding-line-sep","sha":"b817184d35d0e2589682f1dcd88b9f29b2063f5b","user":{"login":"MaxGekk","id":1580697,"avatar_url":"https://avatars1.githubusercontent.com/u/1580697?v=4","gravatar_id":"","url":"https://api.github.com/users/MaxGekk","html_url":"https://github.com/MaxGekk","followers_url":"https://api.github.com/users/MaxGekk/followers","following_url":"https://api.github.com/users/MaxGekk/following{/other_user}","gists_url":"https://api.github.com/users/MaxGekk/gists{/gist_id}","starred_url":"https://api.github.com/users/MaxGekk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MaxGekk/subscriptions","organizations_url":"https://api.github.com/users/MaxGekk/orgs","repos_url":"https://api.github.com/users/MaxGekk/repos","events_url":"https://api.github.com/users/MaxGekk/events{/privacy}","received_events_url":"https://api.github.com/users/MaxGekk/received_events","type":"User","site_admin":false},"repo":{"id":123690489,"name":"spark-1","full_name":"MaxGekk/spark-1","owner":{"login":"MaxGekk","id":1580697,"avatar_url":"https://avatars1.githubusercontent.com/u/1580697?v=4","gravatar_id":"","url":"https://api.github.com/users/MaxGekk","html_url":"https://github.com/MaxGekk","followers_url":"https://api.github.com/users/MaxGekk/followers","following_url":"https://api.github.com/users/MaxGekk/following{/other_user}","gists_url":"https://api.github.com/users/MaxGekk/gists{/gist_id}","starred_url":"https://api.github.com/users/MaxGekk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MaxGekk/subscriptions","organizations_url":"https://api.github.com/users/MaxGekk/orgs","repos_url":"https://api.github.com/users/MaxGekk/repos","events_url":"https://api.github.com/users/MaxGekk/events{/privacy}","received_events_url":"https://api.github.com/users/MaxGekk/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/MaxGekk/spark-1","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/MaxGekk/spark-1","forks_url":"https://api.github.com/repos/MaxGekk/spark-1/forks","keys_url":"https://api.github.com/repos/MaxGekk/spark-1/keys{/key_id}","collaborators_url":"https://api.github.com/repos/MaxGekk/spark-1/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/MaxGekk/spark-1/teams","hooks_url":"https://api.github.com/repos/MaxGekk/spark-1/hooks","issue_events_url":"https://api.github.com/repos/MaxGekk/spark-1/issues/events{/number}","events_url":"https://api.github.com/repos/MaxGekk/spark-1/events","assignees_url":"https://api.github.com/repos/MaxGekk/spark-1/assignees{/user}","branches_url":"https://api.github.com/repos/MaxGekk/spark-1/branches{/branch}","tags_url":"https://api.github.com/repos/MaxGekk/spark-1/tags","blobs_url":"https://api.github.com/repos/MaxGekk/spark-1/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/MaxGekk/spark-1/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/MaxGekk/spark-1/git/refs{/sha}","trees_url":"https://api.github.com/repos/MaxGekk/spark-1/git/trees{/sha}","statuses_url":"https://api.github.com/repos/MaxGekk/spark-1/statuses/{sha}","languages_url":"https://api.github.com/repos/MaxGekk/spark-1/languages","stargazers_url":"https://api.github.com/repos/MaxGekk/spark-1/stargazers","contributors_url":"https://api.github.com/repos/MaxGekk/spark-1/contributors","subscribers_url":"https://api.github.com/repos/MaxGekk/spark-1/subscribers","subscription_url":"https://api.github.com/repos/MaxGekk/spark-1/subscription","commits_url":"https://api.github.com/repos/MaxGekk/spark-1/commits{/sha}","git_commits_url":"https://api.github.com/repos/MaxGekk/spark-1/git/commits{/sha}","comments_url":"https://api.github.com/repos/MaxGekk/spark-1/comments{/number}","issue_comment_url":"https://api.github.com/repos/MaxGekk/spark-1/issues/comments{/number}","contents_url":"https://api.github.com/repos/MaxGekk/spark-1/contents/{+path}","compare_url":"https://api.github.com/repos/MaxGekk/spark-1/compare/{base}...{head}","merges_url":"https://api.github.com/repos/MaxGekk/spark-1/merges","archive_url":"https://api.github.com/repos/MaxGekk/spark-1/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/MaxGekk/spark-1/downloads","issues_url":"https://api.github.com/repos/MaxGekk/spark-1/issues{/number}","pulls_url":"https://api.github.com/repos/MaxGekk/spark-1/pulls{/number}","milestones_url":"https://api.github.com/repos/MaxGekk/spark-1/milestones{/number}","notifications_url":"https://api.github.com/repos/MaxGekk/spark-1/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/MaxGekk/spark-1/labels{/name}","releases_url":"https://api.github.com/repos/MaxGekk/spark-1/releases{/id}","deployments_url":"https://api.github.com/repos/MaxGekk/spark-1/deployments","created_at":"2018-03-03T12:37:00Z","updated_at":"2018-04-02T11:46:19Z","pushed_at":"2018-04-08T19:57:16Z","git_url":"git://github.com/MaxGekk/spark-1.git","ssh_url":"git@github.com:MaxGekk/spark-1.git","clone_url":"https://github.com/MaxGekk/spark-1.git","svn_url":"https://github.com/MaxGekk/spark-1","homepage":null,"size":270346,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":2,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"8d40a79a077a30024a8ef921781b68f6f7e542d1","user":{"login":"apache","id":47359,"avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","deployments_url":"https://api.github.com/repos/apache/spark/deployments","created_at":"2014-02-25T08:00:08Z","updated_at":"2018-04-09T10:17:58Z","pushed_at":"2018-04-09T10:05:07Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":270220,"stargazers_count":16841,"watchers_count":16841,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":15444,"mirror_url":null,"archived":false,"open_issues_count":501,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":15444,"open_issues":501,"watchers":16841,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/20937"},"html":{"href":"https://github.com/apache/spark/pull/20937"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/20937"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/20937/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/20937/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/20937/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/b817184d35d0e2589682f1dcd88b9f29b2063f5b"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"MaxGekk"}}