You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "vladanvasi-db (via GitHub)" <gi...@apache.org> on 2024/12/11 16:44:28 UTC
[PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
vladanvasi-db opened a new pull request, #49148:
URL: https://github.com/apache/spark/pull/49148
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
In this PR, I propose extending the current `DataSet` API and adding another `toJSON` method which will take a jsonOptions map parameter.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
These changes are needed because in some cases, users need to specify some options and more robustly control how the JSON strings will be formed from Dataset. One example is then the output string has a timestamp:
Currently, the timestamp returned by `toJSON` method will have millisecond precision, but sometimes, users need better precision like micro/nano. With appropriate option, users will be able to format the timestamp correctly.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No.
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
N/A.
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
No.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on code in PR #49148:
URL: https://github.com/apache/spark/pull/49148#discussion_r1883521354
##########
sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala:
##########
@@ -1672,13 +1672,18 @@ class Dataset[T] private[sql](
/** @inheritdoc */
override def toJSON: Dataset[String] = {
+ toJSON(Map.empty[String, String])
+ }
+
+ /** @inheritdoc */
+ override def toJSON(jsonOptions: Map[String, String]): Dataset[String] = {
Review Comment:
Yes, actually we can bypass it using this one properly, however, do you still think it is worth to extend the `toJSON` API? In my opinion, a lot of users will not figure out this workaround when trying to specify options for `toJSON` in a `DataSet`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548][SQL] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db closed pull request #49148: [SPARK-50548][SQL] Extended DataSet API to support specifying JSON options in toJSON method
URL: https://github.com/apache/spark/pull/49148
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #49148:
URL: https://github.com/apache/spark/pull/49148#discussion_r1881206531
##########
sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala:
##########
@@ -1672,13 +1672,18 @@ class Dataset[T] private[sql](
/** @inheritdoc */
override def toJSON: Dataset[String] = {
+ toJSON(Map.empty[String, String])
+ }
+
+ /** @inheritdoc */
+ override def toJSON(jsonOptions: Map[String, String]): Dataset[String] = {
Review Comment:
and .. actually we can easily work around via:
```scala
df.select(to_json(struct(col("*")), options)).as(StringEncoder)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #49148:
URL: https://github.com/apache/spark/pull/49148#issuecomment-2543187903
> Currently, the timestamp returned by toJSON method will have millisecond precision, but sometimes, users need better precision like micro.
I would consider to change the default timestamp pattern, and output timestamp with microseconds precision by default in builtin text datasources. I believe it is worth to do in the release 4.0.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #49148:
URL: https://github.com/apache/spark/pull/49148#discussion_r1881210709
##########
sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala:
##########
@@ -1672,13 +1672,18 @@ class Dataset[T] private[sql](
/** @inheritdoc */
override def toJSON: Dataset[String] = {
+ toJSON(Map.empty[String, String])
+ }
+
+ /** @inheritdoc */
+ override def toJSON(jsonOptions: Map[String, String]): Dataset[String] = {
Review Comment:
ah, we probably shouldn't add `Dataset.toJSON` at the first place...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548][SQL] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on PR #49148:
URL: https://github.com/apache/spark/pull/49148#issuecomment-2548531330
Thank you for comments. I will close this PR for now, as changing the default timestamp pattern seems as better option.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #49148:
URL: https://github.com/apache/spark/pull/49148#discussion_r1881205793
##########
sql/api/src/main/scala/org/apache/spark/sql/api/Dataset.scala:
##########
@@ -3169,6 +3169,14 @@ abstract class Dataset[T] extends Serializable {
*/
def toJSON: Dataset[String]
+ /**
+ * Returns the content of the Dataset as a Dataset of JSON strings respecting specified
+ * `jsonOptions`.
+ *
+ * @since 4.0.0
+ */
+ def toJSON(jsonOptions: Map[String, String]): Dataset[String]
Review Comment:
could need an implementation for spark connect as well
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #49148:
URL: https://github.com/apache/spark/pull/49148#issuecomment-2536983871
Please update the PR description as it's a user-facing change. Let's also add tests for it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "vladanvasi-db (via GitHub)" <gi...@apache.org>.
vladanvasi-db commented on PR #49148:
URL: https://github.com/apache/spark/pull/49148#issuecomment-2543171258
From my understanding, `toJSON` is a proper API and it was added in Spark 2.0.0.
It is documented [here](https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/Dataset.html#toJSON--), at the official `DataSet` Spark documentation. On the internet, I see a lot of code examples using this `toJSON` API to construct series of JSON strings from DataSet/DataFrame, and I have not heard one place which says it is not a proper API or it is getting deprecated.
The workaround that @HyukjinKwon proposed works in my case, so I want to hear your thoughts about whether it is worth extending `DataSet` API with `toJSON(jsonOptions: Map[String, String]` method. These JSON options are also well documented [here](https://spark.apache.org/docs/3.5.1/sql-data-sources-json.html), and I think it would be better for users to have a method `toJSON(options)` for their purpose instead of using the workaround that was proposed above.
Please share your thoughts @cloud-fan @MaxGekk @HyukjinKwon because this one is critical for timestamp precision in `JSON` converted strings from the `DataSet`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #49148:
URL: https://github.com/apache/spark/pull/49148#discussion_r1881206136
##########
sql/api/src/main/scala/org/apache/spark/sql/api/Dataset.scala:
##########
@@ -3169,6 +3169,14 @@ abstract class Dataset[T] extends Serializable {
*/
def toJSON: Dataset[String]
+ /**
+ * Returns the content of the Dataset as a Dataset of JSON strings respecting specified
+ * `jsonOptions`.
+ *
+ * @since 4.0.0
+ */
+ def toJSON(jsonOptions: Map[String, String]): Dataset[String]
Review Comment:
would need an implementation for spark connect as well
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #49148:
URL: https://github.com/apache/spark/pull/49148#discussion_r1881205793
##########
sql/api/src/main/scala/org/apache/spark/sql/api/Dataset.scala:
##########
@@ -3169,6 +3169,14 @@ abstract class Dataset[T] extends Serializable {
*/
def toJSON: Dataset[String]
+ /**
+ * Returns the content of the Dataset as a Dataset of JSON strings respecting specified
+ * `jsonOptions`.
+ *
+ * @since 4.0.0
+ */
+ def toJSON(jsonOptions: Map[String, String]): Dataset[String]
Review Comment:
could need an implementation for spark connect as well
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50548] Extended DataSet API to support specifying JSON options in toJSON method [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #49148:
URL: https://github.com/apache/spark/pull/49148#issuecomment-2544574997
my 2 cents, @vladanvasi-db we didn't have expressions like `to_json` at that time we added `Dataset.toJson`. Given that many people use it, I wouldn't deprecate it for now but I would also avoid extending this feature for the matter of consistency. Another example could be `Dataset.toCsv` which I don't think we should add it as an API
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org