Skip to content

feat: add Spark equal_null, encode, to_binary, try_to_binary and stack - #1870

Open
slachiewicz wants to merge 1 commit into
apache:mainfrom
slachiewicz:spark-functions-1867
Open

slachiewicz wants to merge 1 commit into
apache:mainfrom
slachiewicz:spark-functions-1867

Conversation

@slachiewicz

@slachiewicz slachiewicz commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

Closes #1867.

Rationale for this change

datafusion-spark 56 registers equal_null, to_binary, try_to_binary, encode and stack, but datafusion.functions.spark had no wrappers for them, so code ported from PySpark could not use them from the DataFrame API.

What changes are included in this PR?

Adds five functions to datafusion.functions.spark, with pyspark's parameter names:

Function What it does
equal_null(col1, col2) Equality that treats two NULLs as equal
encode(col, charset) Encodes a string as binary in the given character set
to_binary(col, format=None) Converts a string to binary; format is "hex" (default), "utf-8", "utf8" or "base64"
try_to_binary(col, format=None) Like to_binary, but returns NULL instead of raising an error
stack(n, *cols) Separates the values into n rows
from datafusion import col, lit
from datafusion.functions import spark

df.select(spark.equal_null(col("a"), col("b")))
df.select(spark.encode(col("s"), "UTF-16BE"))
df.select(spark.to_binary(lit("YWJj"), format="base64"))  # b"abc"
df.select(spark.try_to_binary(lit("xyz")))                # NULL, not an error
df.select(spark.stack(2, lit(1), lit(2), lit(3)).alias("row")).unnest_columns("row")

Two points differ from pyspark:

  • In to_binary and try_to_binary, a bare str for format is the format itself, not a column name. The format must be a constant, so a column name could never work.
  • stack returns its rows as one list of structs with the fields col0, col1 and so on, the same as upstream, so the caller unnests it. n must be an integer literal.

Each function has a docstring example and a test.

Open questions for review

  • format as a literal. The module's rule for pyspark column-or-name parameters is that a bare str names a column. I deviated because the format must be a constant. The alternative is to accept only an Expr and require lit("base64").
  • try_to_binary with an unknown format. It returns NULL, which is upstream's behavior and what the test asserts. I have not checked whether Spark itself returns NULL or raises in that case.
  • stack signature and result. pyspark's signature is stack(*cols); I named the first parameter n for readability, and positional calls behave the same. The result is a list of structs that the caller unnests; the wrapper could unnest for the caller instead, but then it would not be a plain expression.

Are there any user-facing changes?

Yes, five new functions in datafusion.functions.spark. Nothing existing changes.

@slachiewicz
slachiewicz marked this pull request as ready for review December 6, 2026 11:12
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.

Expose the Spark functions equal_null, to_binary, try_to_binary, encode and stack

1 participant