Skip to content

fix: cap projection input beneath local and global limits - #7195

Open
rich7420 wants to merge 15 commits into
apache:mainfrom
rich7420:fix/7147-limit-projection
Open

rich7420 wants to merge 15 commits into
apache:mainfrom
rich7420:fix/7147-limit-projection

Conversation

@rich7420

@rich7420 rich7420 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Part of #7147.

Rationale for this change

A Comet projection below LocalLimit or GlobalLimit can evaluate a failing ANSI cast on rows beyond the limit, including when the LIMIT is inside a subquery.

What changes are included in this PR?

Extend the existing CollectLimit projection input cap to adjacent LocalLimit and GlobalLimit using the same conversion helper. Include the OFFSET prefix and retain the outer limit, configuration gating, column-only pruning, AQE links and repeated planning.

Preserve the guarded make_date conversion under CollectLimit. Move behavioral checks into SQL fixtures and retain Scala plan assertions. Update compatibility documentation. Spark 4.0 added a LocalLimit below the shuffle, so the OFFSET cases need Spark 4.0 or later and are in limit_projection_offset.sql.

Throwing filters, GlobalLimit codegen OFFSET-prefix differences and already-ordered TakeOrderedAndProject inputs remain outside this PR.

How are these changes tested?

  • On the merge with main at f67d51d, CometExecRuleSuite, the LIMIT tests in CometNativeCastSuite and both SQL fixtures pass on Spark 4.1.3 and 4.0.4 (148 tests per version). On Spark 3.5.9 with strict warnings, 143 pass and 2 are canceled by version gates.
  • With the LocalLimit and GlobalLimit cases removed, all eight fixture runs fail with CAST_INVALID_INPUT on Spark 4.1.3. They pass with the fix restored.
  • The exec and expressions suite groups from pr_build_linux.yml pass on Spark 4.1.3.
  • Spark SQL sql_core passes on Spark 4.1.3 with dev/local-ci.sh. All four shards ran a total of 16,762 tests, matching the counts from the merge-queue run on f67d51d.
  • Spotless and Scalastyle are clean on the default profile.
  • Not run: Spark 4.2, the Spark SQL catalyst and sql_hive shards, the Iceberg suites and the rest of the PR tier. CI (fork) has no run for this branch, so the runs above are local.

@github-actions github-actions Bot added the bug Something isn't working label Oct 24, 2026
@rich7420
rich7420 marked this pull request as ready for review October 27, 2026 14:10

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capping the projection input is the batch-engine counterpart of Spark's lazy codegen, which stops at the limit, and it is the follow-up #7147 scoped. The offset handling (rows already includes the OFFSET prefix) matches SparkStrategies and limit.scala. Spark's PushProjectionThroughLimit does not make this unnecessary, since it is what creates the Limit -> Project shape. Two comments.

}
}

test("non-collect LIMIT skips a failing cast beyond its projection input cap") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior checks in this test are a query that must match Spark, an error for limit + 1, an AQE on and off matrix and a few spark.comet.batchSize values, which CometSqlFileTestSuite supports. array_append_eager_eval.sql is a precedent with the same shape (-- Config: spark.sql.ansi.enabled=true, /*+ COALESCE(1) */ for a single file and an expect_error_class), and array_null_short_circuit.sql already uses expect_error(CAST_INVALID_INPUT). The plan-shape assertion on CometLocalLimitExec.limit == limit + offset is covered by the CometExecRuleSuite loops over collect, local and global limits, so this test could shrink to the parts a fixture cannot express. The #6035 sibling test lives in this suite in Scala, so keeping the pattern is defensible. The dispatch assertion needs a -- Config: line for the codegen setting. I have not run such a fixture.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I moved the behavior checks into SQL fixtures, limit_projection.sql plus limit_projection_offset.sql for the OFFSET cases, which need Spark 4.0 or later: ANSI on, COALESCE(1), expect_error_class(CAST_INVALID_INPUT) for limit + 1, AQE on/off and batch sizes 2 and 8192. The fixtures use a -- Config: line for the codegen dispatch. The two non-collect Scala tests now only check what a fixture can't: that the cap is limit + offset with AQE on and off, and that it applies in each partition.

@sunchao sunchao added the run-spark-4.1-tests Run the Spark 4.1 SQL tests on this pull request instead of waiting for the merge queue label Oct 27, 2026
@rich7420
rich7420 marked this pull request as draft November 10, 2026 03:07
# Conflicts:
#	spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala
Spark 3.5 plans LIMIT with OFFSET with no LocalLimit below the shuffle,
so Spark itself evaluates the projection on every row and raises
CAST_INVALID_INPUT where these cases expect a result. Move the OFFSET
queries to limit_projection_offset.sql with MinSparkVersion 4.0 and skip
the AQE OFFSET test before Spark 4.0. The other cases still run on 3.5.
Both partitions return the same value, so the query result alone would
not notice a partition that lost its row.
… OFFSET codegen

An OFFSET-only GlobalLimit has no limit to cap at. On Spark 3.5 the
generated code for a GlobalLimit with an OFFSET can evaluate the
projection past the limit, so Spark can raise an error Comet does not.
The compatibility page said "native local limits", which the documentation
style guide's rule for "native" does not allow in the user guide. Name
spark.comet.exec.localLimit.enabled instead, and say "a Comet limit" in the
rule's doc comment.
@rich7420
rich7420 marked this pull request as ready for review November 29, 2026 20:03
…ctions

On Spark 3.5.9, with GlobalLimit and the projection in one codegen stage, Spark did not evaluate the projection past the limit: a LIMIT 2 OFFSET 1 over range(0, 5, 1, 1) with an invalid value at id 3 or 4 returned rows 1 and 2. Remove the sentence that said it could.

A projection below another projection still evaluates its whole input batch, so list it with the other open cases.
With one partition there is no shuffle between the limit and the projection. On Spark 3.5 a LIMIT with OFFSET then plans as GlobalLimit over the projection, which no SQL fixture reached.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working run-spark-4.1-tests Run the Spark 4.1 SQL tests on this pull request instead of waiting for the merge queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants