Skip to content

[VL] Support Arrow-optimized Python UDTF offload (continues #12276) - #13731

Draft
dadarshabhat1 wants to merge 14 commits into
apache:mainfrom
dadarshabhat1:udtf-arrow-v2
Draft

dadarshabhat1 wants to merge 14 commits into
apache:mainfrom
dadarshabhat1:udtf-arrow-v2

Conversation

@dadarshabhat1

@dadarshabhat1 dadarshabhat1 commented Nov 23, 2026 •

Copy link
Copy Markdown

What changes are proposed in this pull request?

This continues #12276 by @FelixYBW. It offloads Spark's ArrowEvalPythonUDTFExec (Arrow-optimized Python UDTFs, useArrow=True, Spark 3.5+) to ColumnarArrowEvalPythonUDTFExec in the Velox backend. The original commits are kept unchanged with their authorship. The changes on top are in e9eaebe.

Output goes to Velox as Arrow, not rows

ColumnarArrowEvalPythonUDTFExec now outputs ArrowJavaBatchType instead of VanillaBatchType. With VanillaBatchType, a Velox parent got ColumnarToRow -> RowToVeloxColumnar. Now it gets OffloadArrowData -> ArrowColumnarToVeloxColumnar, which imports the buffers through the Arrow C data interface. This follows the approach described in the #12276 comment.

  • One copy into tracked memory: each Python batch is copied once, with VectorAppender, into Gluten's Arrow allocator before the runner reuses its buffers. The output memory is now tracked by Gluten.
  • Coalescing: the Python worker returns one batch per input row. Those results are coalesced into batches of up to spark.gluten.sql.columnar.maxBatchSize rows.
  • Child columns: each input row's requiredChildOutput values are written once and repeated by index for its output rows.
  • Batch ownership: each output batch owns its buffers and is closed by its consumer, as in ColumnarArrowEvalPythonExec after [GLUTEN-13706][VL] Fix pandas UDFs on partitions with more than one batch #13707.
  • Same behaviour as Spark: output is joined with input rows exactly as in vanilla Spark. terminate() rows join the last input row, or a null row if the partition is empty.

Fixes to #12276

  1. Spark 4.0.0 NoSuchMethodError: the 4.0 shim called DataType.equalsIgnoreCompatibleCollation(Seq[DataType], Seq[DataType]), which only exists since 4.0.1. The output type check now uses a local helper that ignores string collations.
  2. Spark 4.1+ @arrow_udtf: Spark 4.1+ also plans SQL_ARROW_UDTF (302) through ArrowEvalPythonUDTFExec, and its worker doesn't return one batch per input row. The shims now only match SQL_ARROW_TABLE_UDF (301).
  3. Spark 4.2: added the shims/spark42 shim. Without it, Spark 4.2 didn't compile after merging main.

Structure

  • Shims: ArrowPythonUDTFEvaluator is now defined once, in shims/common. Each version's shim only builds the arguments for ArrowPythonUDTFRunner and returns its raw output. The 4.2 shim is the same as 4.1's. The two versions' signatures and the bytecode of ArrowEvalPythonUDTFExec / EvalPythonUDTFExec match.
  • Shared exec: flattening, type checking, coalescing and memory handling are written once in backends-velox.
  • Fallback: the exec falls back to vanilla Spark for spark.sql.execution.arrow.useLargeVarTypes=true and for user-defined types. Other unsupported types are rejected by Gluten's schema validation.
  • Non-Arrow UDTFs (BatchEvalPythonUDTFExec) and scalar Arrow UDFs are not affected.

Still to do

  • More tests: multiple and empty partitions; string, decimal, date/timestamp and nested types; empty results; HybridRowQueue spilling; named arguments and TABLE(...) PARTITION BY on 4.0; a plan check that no ColumnarToRow / RowToVeloxColumnar pair is inserted above the UDTF; a join or aggregation consuming the output.
  • Benchmarks against vanilla Spark and against main (where the UDTF falls back).

How was this patch tested?

  • Compiles: backends-velox (main and test code) compiles with -Pbackends-velox -Pjava-17 for Spark 3.4, 3.5, 4.0, 4.1 and 4.2.
  • Not yet run: ArrowEvalPythonUDTFSuite from [VL] support arrow UDTF #12276, and the tests listed under "Still to do". They need a Python environment with pyspark, pandas and pyarrow, which I'm setting up.
  • Planned: runs on Spark 3.5.4 and 4.0.0.

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

Generated-by: Claude Code (claude-opus-5-5). The original commits from #12276 were also generated with Claude Code, as stated in that PR.

root and others added 10 commits June 11, 2026 05:53
- Move the Spark version specific code (PythonUDTF, ArrowEvalPythonUDTFExec and
  ArrowPythonUDTFRunner only exist since Spark 3.5 and differ in 4.0/4.1) into
  ArrowEvalPythonUDTFShim so the build passes on all Spark versions.
- Join each Python output batch with its input row via HybridRowQueue, the same
  way as Spark does, so requiredChildOutput (e.g. LATERAL joins) and terminate()
  results are correct.
- Use BatchIterator instead of grouped() which buffered mutable projected rows,
  and propagate job artifact UUID and Python metrics.
- Provide a default no-op in SparkPlanExecApi so other backends are unaffected,
  gated by spark.gluten.sql.columnar.arrowUdf.
- Add ArrowEvalPythonUDTFSuite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added CORE works for Gluten Core VELOX labels Nov 23, 2026
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

root added 2 commits November 23, 2026 22:43
Output ArrowJavaBatchType instead of VanillaBatchType, so Velox imports
the UDTF output through OffloadArrowData and the Arrow C data interface
rather than ColumnarToRow -> RowToVeloxColumnar.

- Copy each Python batch once, with VectorAppender, into Gluten's Arrow
  allocator before the runner reuses its buffers, and coalesce results
  into batches of up to spark.gluten.sql.columnar.maxBatchSize rows.
- Write each input row's required child columns once and repeat them
  by index for its output rows.
- Check the Python output types with a local helper instead of Spark's
  equalsIgnoreCompatibleCollation, whose Seq overload is missing in
  Spark 4.0.0.
- Fall back for spark.sql.execution.arrow.useLargeVarTypes and for
  user-defined types.
- Move ArrowPythonUDTFEvaluator to shims/common. The shims now only
  build the runner input and return its raw output.
- Only match SQL_ARROW_TABLE_UDF, since Spark 4.1+ also plans
  SQL_ARROW_UDTF through ArrowEvalPythonUDTFExec.
- Add the Spark 4.2 shim.
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CORE works for Gluten Core VELOX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants