← Back to Pull requests

feat: run user-defined generators inside the native generate operator #7253

Closed
unknown closed this pull request without merging on Nov 19, 2026 16:26 UTC
View on GitHub
unknown unknown commented on Oct 26, 2026 23:45 UTC
Author

Which issue does this PR close?

Closes #7252.

Stacked on #7251 (inline / stack), whose commits appear here until it merges. The last three commits are new.

Rationale for this change

A GenerateExec whose generator Comet has no native implementation of falls back to Spark. That costs a columnar-to-row transition before it and a row-to-columnar transition after it, and leaves the rest of the stage in Spark. This affects:

  • Hive GenericUDTFs used in LATERAL VIEW;

  • json_tuple;

  • the ReplicateRows generator that EXCEPT ALL and INTERSECT ALL plan (now lowered to a native expression, see below);

  • custom Catalyst generators.
  • The generator itself still runs on the JVM one row at a time, so the gain is keeping the stage native around it. The JVM generator works a batch at a time: it evaluates every row of a batch, and holds all the rows one input row generates, before passing any on. The user guide says so.

    What changes are included in this PR?

    The issue proposed returning generated columns plus a parent-row index. This uses a simpler shape that fits the existing UDF bridge, which requires one output value per input row:

  • CometGeneratorUDF (a CometUDF): for each input row, it returns the rows the generator emits as one array<struct> element. CometExplodeExec then unnests and flattens that the way #7251 does for inline. An input row that emits nothing gets an empty array, so outer keeps it as one row of nulls, through the existing null handling.

  • Inputs and outputs: it reads the bound generator's columns through CometVector rows, and writes the output with Comet's ArrowWriter into a vector from CometBatchKernelCodegen.allocateOutput, so the list and struct field names match what the native side declares.

  • State: each native plan gets its own generator instance, keyed by digest and plan, and dropped in releasePlan, so the state a Hive UDTF keeps carries across batches. The serde folds a per-occurrence number into the digest, so two identical generators in one plan don't share an instance.

  • terminate(): a new terminate expression on the Explode proto is the same UDF called with a flag set. ExplodeExec evaluates it once the input is exhausted and emits its rows with the last input row's other columns, which it keeps as a one-row copy. That matches Spark's GenerateExec, which pairs them with the last row as Hive does. outer does not apply to these rows. With no input rows, the other columns are null.

  • Serde: CometGeneratorUDF.declineReason and CometGeneratorUDF.calls. The generator falls back when:

  • - it is nondeterministic (the existing gate);
    - its tree holds a subquery, an unevaluable node or a native UDF call;
    - it calls code outside Spark (CometInvokeTargets);
    - its output type cannot be serialized.
  • ReplicateRows: it is lowered to inline(array_repeat(named_struct(...), n)), so EXCEPT ALL and INTERSECT ALL run fully native. A NULL or negative count gives no rows, as in Spark, and a count beyond Int.MaxValue is clamped rather than wrapped. The JVM generator is the fallback if the expression does not convert.

  • Config: spark.comet.exec.jvmGenerator.enabled, on by default.

  • Small refactors: CometScalaUDF.closureSerialize is pulled out for reuse, and ArrowWriter and ArrowFieldWriter are widened from private[arrow] to private[comet].

  • Docs: a Generators section in the Scala/Java UDF page, plus the operator, expression and plan pages.
  • Python UDTFs (@udtf) plan as their own exec nodes and are not covered.

    How are these changes tested?

  • Rust ExplodeExec tests:

  • - terminate rows carry the last input row across several batches;
    - with no input rows the other columns are null;
    - an empty terminate adds no outer row, including over an empty input whose carried column is non-nullable;
    - with terminate set, the planner declares the carried columns nullable.
  • CometGenerateExecSuite:

  • - a test Hive GenericUDTF (registered without a Hive session) that keeps a running count and emits a total from close(). The suite checks it runs natively against Spark: with and without LATERAL VIEW OUTER, with no input rows, and as two identical UDTFs in one plan;
    - a three-partition run with AQE off, checking that each partition ends with its own close() row;
    - json_tuple, and EXCEPT ALL through ReplicateRows, both checked to run natively;
    - the fallback reason when the config is off;
    - the state of the UDTF across batch boundaries, using a batch size of 2 and of 1;
    - a generator that reuses one row and byte array, checking each row is kept apart;
    - an empty terminate over a non-nullable carried column;
    - EXCEPT ALL and INTERSECT ALL with counts of zero, one and many, checked to plan no JvmScalarUdf.
  • SQL file tests: json_tuple.sql, and except_all.sql over DECIMAL, BINARY, FLOAT and DOUBLE (NaN, -0.0, infinity), DATE, TIMESTAMP, TIMESTAMP_NTZ, array and struct columns with NULLs.

  • Local runs: on the default Spark 4.1 profile, CometGenerateExecSuite (66 passed), and the inline, stack, explode, json_tuple and except_all SQL files.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: rich7420 Review duration: 34121m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #7253"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%