[SPARK-51975][SQL] Add variant_from_arrays and variant_from_entries#57497
Open
SreeramaYeshwanthGowd wants to merge 2 commits into
Open
[SPARK-51975][SQL] Add variant_from_arrays and variant_from_entries#57497SreeramaYeshwanthGowd 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 two built in scalar SQL functions that construct a Variant object directly from collections:
variant_from_arrays(keys, values)builds a Variant object from an array of string keys and an array of values.variant_from_entries(entries)builds a Variant object from an array of key/value struct entries (a two field struct per entry).API surface added:
variant_from_arrays,variant_from_entriesfunctions.variant_from_arrays(keys, values),functions.variant_from_entries(entries)pyspark.sql.functions.variant_from_arrays/variant_from_entriesImplementation notes:
variantExpressions.scalanext toto_variant_object, delegating (eval and codegen) to two newVariantExpressionEvalUtilshelpers that build the object in a single pass withVariantBuilder(addKey/finishWritingObject) and reuse the existingbuildVariantfor each value. This avoids materializing an intermediateMapType, which is the point of the ticket.to_variant_objectandmap_from_*precedents:to_variant_objectdoes for non-string-keyed maps, it is not implicitly cast); a null key raisesNULL_MAP_KEY.VARIANT_DUPLICATE_KEY, matchingto_variant_object(Variant objects cannot hold duplicate keys). This is stricter thanmap_from_*under the non defaultspark.sql.mapKeyDedupPolicy=LAST_WIN.NULL, and forvariant_from_entriesa null entry yieldsNULL(asmap_from_entriesdoes).variant_from_arrays, a keys/values length mismatch reuses the same errormap_from_arraysraises.Why are the changes needed?
Today, creating a Variant object from arrays or entries requires a two step expression:
to_variant_object(map_from_arrays(...))orto_variant_object(map_from_entries(...)). That materializes an intermediateMapTypeonly to immediately convert it.variant_from_arrays/variant_from_entriesbuild the Variant object directly, reducing allocation and CPU, which matters for large maps and high volume workloads. They also mirror Spark's ownmap_from_arrays/map_from_entriesnaming, so the Variant surface stays consistent.Does this PR introduce any user-facing change?
Yes. It adds two new built in SQL functions and their Scala and PySpark DataFrame API entries. No existing behavior changes.
How was this patch tested?
VariantExpressionSuitecovering object construction, key sorting, empty input, null values, nested values, null array / null entry inputs returning null, and the null key / duplicate key / length mismatch errors.VariantEndToEndSuiteexercising the DataFrame API under bothCODEGEN_ONLYandNO_CODEGENover a non foldable relation.variant/variant-from-arrays-entries.sql, covering each JSON type, null handling, and the null key / duplicate key / length mismatch / non-string key / unsupported value type errors.test_functions.pytest, and regeneratedsql-expression-schema.md.Was this patch authored or co-authored using generative AI tooling? No