Skip to content

[SPARK-19406] [SQL] Fix function to_json to respect user-provided options - #16745

Closed
gatorsmile wants to merge 1 commit into
apache:masterfrom
gatorsmile:toJson
Closed

[SPARK-19406] [SQL] Fix function to_json to respect user-provided options#16745
gatorsmile wants to merge 1 commit into
apache:masterfrom
gatorsmile:toJson

Conversation

@gatorsmile

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

Currently, the function to_json allows users to provide options for generating JSON. However, it does not pass it to JacksonGenerator. Thus, it ignores the user-provided options. This PR is to fix it. Below is an example.

valdf=Seq(Tuple1(Tuple1(java.sql.Timestamp.valueOf("2015-08-26 18:00:00.0")))).toDF("a")
valoptions=Map("timestampFormat"->"dd/MM/yyyy HH:mm")
df.select(to_json($"a", options)).show(false)

The current output is like

+--------------------------------------+
|structtojson(a) |
+--------------------------------------+
|{"_1":"2015-08-26T18:00:00.000-07:00"}|
+--------------------------------------+

After the fix, the output is like

+-------------------------+
|structtojson(a) |
+-------------------------+
|{"_1":"26/08/2015 18:00"}|
+-------------------------+

How was this patch tested?

Added test cases for both from_json and to_json

@gatorsmile

Copy link
Copy Markdown
MemberAuthor

@marmbrus

Copy link
Copy Markdown
Contributor

LGTM

@HyukjinKwon

Copy link
Copy Markdown
Member

LGTM too.

@SparkQA

Copy link
Copy Markdown

Test build #72174 has finished for PR 16745 at commit 99756eb.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

asfgit pushed a commit that referenced this pull request Jan 31, 2017
### What changes were proposed in this pull request?
Currently, the function `to_json` allows users to provide options for generating JSON. However, it does not pass it to `JacksonGenerator`. Thus, it ignores the user-provided options. This PR is to fix it. Below is an example.
```Scala
val df = Seq(Tuple1(Tuple1(java.sql.Timestamp.valueOf("2015-08-26 18:00:00.0")))).toDF("a")
val options = Map("timestampFormat" -> "dd/MM/yyyy HH:mm")
df.select(to_json($"a", options)).show(false)
```
The current output is like
```
+--------------------------------------+
|structtojson(a) |
+--------------------------------------+
|{"_1":"2015-08-26T18:00:00.000-07:00"}|
+--------------------------------------+
```
After the fix, the output is like
```
+-------------------------+
|structtojson(a) |
+-------------------------+
|{"_1":"26/08/2015 18:00"}|
+-------------------------+
```
### How was this patch tested?
Added test cases for both `from_json` and `to_json`
Author: gatorsmile <gatorsmile@gmail.com>
Closes#16745 from gatorsmile/toJson.
(cherry picked from commit f9156d2)
Signed-off-by: gatorsmile <gatorsmile@gmail.com>
@gatorsmile

Copy link
Copy Markdown
MemberAuthor

Thanks! Merging to master/2.1

cmonkey pushed a commit to cmonkey/spark that referenced this pull request Feb 15, 2017
### What changes were proposed in this pull request?
Currently, the function `to_json` allows users to provide options for generating JSON. However, it does not pass it to `JacksonGenerator`. Thus, it ignores the user-provided options. This PR is to fix it. Below is an example.
```Scala
val df = Seq(Tuple1(Tuple1(java.sql.Timestamp.valueOf("2015-08-26 18:00:00.0")))).toDF("a")
val options = Map("timestampFormat" -> "dd/MM/yyyy HH:mm")
df.select(to_json($"a", options)).show(false)
```
The current output is like
```
+--------------------------------------+
|structtojson(a) |
+--------------------------------------+
|{"_1":"2015-08-26T18:00:00.000-07:00"}|
+--------------------------------------+
```
After the fix, the output is like
```
+-------------------------+
|structtojson(a) |
+-------------------------+
|{"_1":"26/08/2015 18:00"}|
+-------------------------+
```
### How was this patch tested?
Added test cases for both `from_json` and `to_json`
Author: gatorsmile <gatorsmile@gmail.com>
Closesapache#16745 from gatorsmile/toJson.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@gatorsmile@marmbrus@HyukjinKwon@SparkQA