[SPARK-58307][SQL][PYTHON] Add json_typeof function#57479
Open
SreeramaYeshwanthGowd wants to merge 2 commits into
Open
[SPARK-58307][SQL][PYTHON] Add json_typeof function#57479SreeramaYeshwanthGowd wants to merge 2 commits into
SreeramaYeshwanthGowd wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Add a built in
json_typeof(json)scalar function that returns the type of the outermost value in a JSON string, as one ofobject,array,string,number,boolean, ornull. It returns SQLNULLwhen the input is not a valid JSON string or is an empty string.API surface added:
json_typeof(json)functions.json_typeof(col)pyspark.sql.functions.json_typeof(col)Implementation notes:
json_object_keysfunction: aRuntimeReplaceableexpression whose replacement is aStaticInvokeintoJsonExpressionUtils, using the same Jackson parser (CreateJacksonParser/SharedFactory) that already backsjson_array_lengthandjson_object_keys. No new dependency.NULL:json_typeofreads the outermost value and consumes it, so a structurally invalid JSON value returnsNULL(consistent withjson_object_keys).number, matching PostgreSQL and BigQuery.Why are the changes needed?
Spark can already ask a JSON string for its array length (
json_array_length) and its object keys (json_object_keys), but not for the type of its outermost value.json_typeofcompletes that family. It is a lightweight top level type probe: unlikeschema_of_json, which does full recursive schema inference and returns a DDL string such asARRAY<BIGINT>,json_typeofreturns a single simple type token. It is provided by PostgreSQL (json_typeofandjsonb_typeof) and BigQuery (JSON_TYPE), so it also improves parity with the engines Spark users migrate from.Does this PR introduce any user-facing change?
Yes. It adds a new built in SQL function
json_typeofand the corresponding Scala and PySpark DataFrame API entries. No existing behavior changes.Example:
How was this patch tested?
Added catalyst unit tests in
JsonExpressionsSuitecovering each JSON type (object, array, string, number, boolean, null), invalid and empty inputs returning null, and null propagation, and a DataFrame API test inJsonFunctionsSuite. Added a PySpark doctest and regeneratedsql-expression-schema.md.Was this patch authored or co-authored using generative AI tooling? No