Skip to content

[SPARK-58307][SQL][PYTHON] Add json_typeof function#57479

Open
SreeramaYeshwanthGowd wants to merge 2 commits into
apache:masterfrom
SreeramaYeshwanthGowd:add-json-typeof-function
Open

[SPARK-58307][SQL][PYTHON] Add json_typeof function#57479
SreeramaYeshwanthGowd wants to merge 2 commits into
apache:masterfrom
SreeramaYeshwanthGowd:add-json-typeof-function

Conversation

@SreeramaYeshwanthGowd

Copy link
Copy Markdown

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 of object, array, string, number, boolean, or null. It returns SQL NULL when the input is not a valid JSON string or is an empty string.

API surface added:

  • SQL: json_typeof(json)
  • Scala DataFrame: functions.json_typeof(col)
  • PySpark, classic and Spark Connect: pyspark.sql.functions.json_typeof(col)

Implementation notes:

  • Modeled on the existing json_object_keys function: a RuntimeReplaceable expression whose replacement is a StaticInvoke into JsonExpressionUtils, using the same Jackson parser (CreateJacksonParser / SharedFactory) that already backs json_array_length and json_object_keys. No new dependency.
  • Like those functions, malformed input returns NULL: json_typeof reads the outermost value and consumes it, so a structurally invalid JSON value returns NULL (consistent with json_object_keys).
  • Both integer and floating point JSON numbers map to 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_typeof completes that family. It is a lightweight top level type probe: unlike schema_of_json, which does full recursive schema inference and returns a DDL string such as ARRAY<BIGINT>, json_typeof returns a single simple type token. It is provided by PostgreSQL (json_typeof and jsonb_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_typeof and the corresponding Scala and PySpark DataFrame API entries. No existing behavior changes.

Example:

spark-sql> SELECT json_typeof('{"a": 1}');
object
spark-sql> SELECT json_typeof('[1, 2, 3]');
array
spark-sql> SELECT json_typeof('123');
number
spark-sql> SELECT json_typeof('not json');
NULL

How was this patch tested?

Added catalyst unit tests in JsonExpressionsSuite covering each JSON type (object, array, string, number, boolean, null), invalid and empty inputs returning null, and null propagation, and a DataFrame API test in JsonFunctionsSuite. Added a PySpark doctest and regenerated sql-expression-schema.md.

Was this patch authored or co-authored using generative AI tooling? No

Sign up for free to 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.

1 participant