Skip to content

[SPARK-57534][PYSPARK] Expose multi-argument Oracle-style decode() in PySpark - #57568

Open
ybapat wants to merge 1 commit into
apache:masterfrom
ybapat:issue-57534-pyspark-decode
Open

[SPARK-57534][PYSPARK] Expose multi-argument Oracle-style decode() in PySpark#57568
ybapat wants to merge 1 commit into
apache:masterfrom
ybapat:issue-57534-pyspark-decode

Conversation

@ybapat

@ybapat ybapat commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

The SQL decode function in Spark supports two forms:

  1. Binary decode (2-arg): decode(binary_col, charset) — decodes binary data to a string.
  2. Oracle-style CASE WHEN (3+ args): decode(expr, search1, result1, ..., [default]) — returns the result for the first matching search value.

The CASE WHEN form is implemented in Scala (object Decode { def createExpr(...) }) and available via spark.sql("SELECT decode(...)"), but PySpark's decode() function only exposed the 2-argument binary form. Issue #57534 tracks this gap.

This PR updates pyspark.sql.functions.decode (both classic and Connect paths) to accept variadic *args and dispatch to the correct form:

  • 1 arg → binary decode with charset (existing behavior)
  • 3+ args → Oracle-style CASE WHEN (new)
  • 0 args → PySparkTypeError with NOT_ENOUGH_ARGS

Changes:

  • python/pyspark/sql/functions/builtin.py: Change signature to decode(col, *args), add 3-branch dispatch, update docstring with examples.
  • python/pyspark/sql/connect/functions/builtin.py: Mirror changes for Spark Connect path.
  • python/pyspark/errors/error-conditions.json: Add NOT_ENOUGH_ARGS error class.

Why are the changes needed?

PySpark users cannot use the Oracle-style decode via the Python API without resorting to spark.sql(). This change closes the gap between the SQL and Python APIs.

Does this PR introduce any user-facing change?

Yes — new functionality. The existing 2-argument binary decode is fully backward compatible. Users can now call:

sf.decode(col("status"), lit(1), lit("active"), lit(0), lit("inactive"), lit("unknown"))

How was this patch tested?

Manual verification of both dispatch paths. The existing doctest for binary decode continues to pass. The CASE WHEN examples are marked # doctest: +SKIP as they require a live Spark session.

Closes #57534

Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

@art000109

art000109 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Spark-57534 is a task for this PR
apache/spark-docker#127

Isn't it?

…EN form

The PySpark decode() function previously only exposed the 2-argument
binary decode form. Spark SQL has supported both the 2-arg binary form
and the Oracle-style CASE WHEN multi-argument form (decode(expr,
search1, result1, ..., default)) since 3.2.0. This change updates
the Python API to pass through variable arguments so both forms work
from PySpark.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@ybapat
ybapat force-pushed the issue-57534-pyspark-decode branch from 3a4567e to 299e8ec Compare July 27, 2026 20:27
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.

[SQL] Add CASE WHEN equivalent for the DECODE function

2 participants