cj-zhukov opened a new pull request, #26187: URL: https://github.com/apache/datafusion/pull/26187
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #https://github.com/apache/datafusion/issues/26185. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. Please explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the implementation. "COUNT(DISTINCT) returns wrong results when the column contains nulls" is the user-visible problem. --> There is no way to serialize a struct, or a set of columns, into a JSON string column without `collect()`-ing the `DataFrame` and rebuilding it. Spark has `to_json(struct)` and Polars has `struct.json_encode()`. This PR adds a Spark-compatible `to_json` to `datafusion-spark` so the conversion stays a lazy projection and runs batch by batch. ```rust use datafusion_spark::expr_fn::to_json; df.with_column("json", to_json(&["foo", "bar"]))?; // {"foo":1,"bar":"bak"} df.with_column("json", to_json(col("event")))?; // existing struct column ``` ```sql SELECT to_json(named_struct('foo', foo, 'bar', bar)) AS json FROM t; ``` ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR. --> - `datafusion/spark/src/function/json/to_json.rs`: `ToJson` scalar UDF (Struct -> Utf8). Serializes each batch with the `arrow-json` row encoder; NULL fields are omitted (Spark `ignoreNullFields` default), a NULL struct yields NULL, nested structs/lists are handled recursively. Non-struct input is rejected at plan time. - `IntoJsonStruct` trait + `expr_fn::to_json(input)`: accepts either a struct `Expr` or a list of column names, which are packed with `named_struct` keyed by their unqualified names. The result is a plain `Expr`, so `with_column` needs no changes. - `datafusion/spark/src/function/json/mod.rs`: registration (functions(), expr_fn). - `datafusion/core/Cargo.toml`: `datafusion-spark` as a `dev-dependency` for the `DataFrame` tests. ## What is the testing strategy for this PR? - Unit tests in `to_json.rs` (array/scalar input, nulls, nested types, escaping, invalid input, name packing incl. qualified names). - `datafusion/core/tests/dataframe/mod.rs`: `with_column_to_json` (add, overwrite, col(..) on a struct column, SQL equivalence, plan-time rejection) and `with_column_to_json_edge_cases` (null handling, constant input, nested struct + list, escaping/UTF-8, qualified names, empty input, empty column list). <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code Briefly describe how this PR is tested, and point to the specific tests you added. For example: 'This new feature is covered by the `sqllogictest` cases added in `foo.slt`'. If this PR does not add tests, explain why. For example, if the change is already covered by existing tests, please mention it. You should also check the `codecov` bot reply on this PR to confirm the changed code is exercised. --> ## Are there any user-facing changes? Yes: new `to_json` function in `datafusion-spark` (SQL via with_spark_features() / register_udf, `DataFrame` via `datafusion_spark::expr_fn::to_json`). No changes to existing APIs. <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please add the `api change` label. --> -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
