Repository navigation
Conversation
There was a problem hiding this comment.
The PR backlog is huge and the maintainers don't have the bandwidth to review them all, so here is an automated review. Feel free to ignore it.
I traced the filter ordering in SparkExpressionEvaluation.filterCondition against GeneratePredicateHelper in Spark 3.5.8, 4.0.4 and 4.1.3, and the shared-subexpression selection in sharedGuardedDivisions against EquivalentExpressions and ProjectExec.doConsume. I found no correctness problem. The null-check placement, the trailing IsNotNull handling and the treatment of a NULL conjunct as a rejection all match Spark's generated filter. Both And and Or recurse into both children in EquivalentExpressions, so the intermediate projection matches what Spark evaluates up front. The same-plan-id rollup in operators/projection.rs also covers the extra native projection for metrics. This was all read, not run. I did not build the native library or run logical_ansi_division.sql.
The new SQL file only exercises DIV on the filter path and DIV or / on the projection path, but containsAnsiDivision and hasMatchingOperandGuards also admit ANSI / and % and decimal operands. Could we add a filter query that uses / and one that uses %, mirroring the first two queries? The shared expression of a decimal division is also worth one projection query. rewrittenExprToProto runs DecimalPrecision.promote on the rewritten tree, where the shared part is now a bound column of the pre-promotion type, so a test would show that the CheckOverflow wrapping still lines up.
Could we also add a filter whose zero-divisor row is the first row of a later batch, or otherwise confirm that the new <=> true guards keep the lazy right-operand evaluation when only a few rows in a batch survive the earlier conjuncts? The current table has four rows in one batch, so every query takes the same path through DataFusion's AND evaluation.
On Spark 4.2.0, FilterExec has its own subexpression elimination path (subexpressionEliminationFilterExecEnabled) that emits shared subexpressions just before the first predicate that uses them. The ordering looks equivalent to the one this PR reproduces, but the PR only cites the 4.1.3 source. Could you confirm that against the 4.2 FilterExec code?
andygrove
left a comment
There was a problem hiding this comment.
This is an automated re-review of the changes pushed since my earlier automated review. Feel free to ignore it.
The earlier asks are addressed. The SQL file now has filter queries for / and %, a decimal projection query, a zero divisor at the start of the second batch (spark.comet.batchSize=2), and a separate Spark 4.2 file. The 4.2 handling matches FilterExec.doConsume: the filter now keeps the original condition when a shared term sits under an AND or OR.
I traced the new PARTIAL_READER tag and the tag removal for the capped projection in limitCollectProjectionInput. I also traced materializedInputs binding, which skips serializers that use their own inputs, such as lambdas. I found no correctness problem. The LIMIT 1, LIMIT 2 and LIMIT 3 queries would fail without the tag. This was read, not run.
andygrove
left a comment
There was a problem hiding this comment.
This is an automated re-review of the changes pushed since my earlier automated review. Feel free to ignore it.
The ProjectInputs rewrite binds the same exact matches as the dynamic variable did, and I found no behavior change in it. I traced the other two changes and have two questions.
The root of the plan now gets Some(true) when producesObjects(plan) holds, so a filter or projection under Dataset.rdd keeps its original order. Nothing in the diff would fail if that line were reverted, because the SQL file cannot reach it and CometTypedDatasetSuite only covers typed filters. Could we add a test that runs something like SELECT x FROM t WHERE y IS NOT NULL AND abs(10 DIV x) > 0 through .rdd.take(1), with a passing row first and a row with x = 0 and y = NULL after it in the same batch? Spark stops after the first row and Comet should too.
The new op.conf.hugeMethodLimit < default guard treats any lowered limit as "Spark will leave whole-stage codegen". The limit is a bytecode size per generated method, and Spark's own config doc suggests 8000 for HotSpot. A small filter compiles well under that, so Spark keeps whole-stage evaluation and raises, while Comet now keeps the original order and returns the row. That matches the behavior before this PR, so it is not a regression. Is it the direction you want for limits that do not actually force the fallback? If so, could the doc paragraph say that a lowered limit only matches Spark when the generated method exceeds it, instead of reading as if any value below the default switches Spark to a standalone filter? The test at hugeMethodLimit=1 only shows the case where every method exceeds the limit.
andygrove
left a comment
There was a problem hiding this comment.
This is an automated re-review of the changes pushed since my earlier automated review. Feel free to ignore it.
The Dataset.rdd question is now addressed. I traced filtered.rdd.take(1) with the root reverted to None. The filter is reordered so abs(10 DIV x) > 0 runs before the trailing IsNotNull(p), the same order Spark's GeneratePredicateHelper uses, so the x = 0, p = NULL row in the same batch raises. With the tag it keeps p IS NOT NULL first and returns Row(1L). The question about a hugeMethodLimit below the default is not answered in the delta, and the doc paragraph is unchanged, so I am leaving it as asked.
The new !isSpark40Plus && plan.output.isEmpty condition looks wider than Dataset.isEmpty. On Spark 3.x a write has a DataWritingCommandExec at the root, whose output is empty, so it now gets Some(true). That would tag every filter and projection below the write as a partial reader and turn the reordering off. Spark's generated code would still evaluate the division first, so as far as I can trace, something like INSERT INTO t SELECT x FROM divisors WHERE p IS NOT NULL AND abs(10 DIV x) > 0 with ANSI on would raise DIVIDE_BY_ZERO in Spark 3.5 and return the row in Comet. I have not run this. The previous head did not tag that plan. Could the condition be narrowed so a command root is excluded, for example by matching the zero-column Project that select() adds, and could a test cover a write on 3.x as well as isEmpty?
viirya
left a comment
There was a problem hiding this comment.
The ordering logic matches FilterExec.doConsume in 4.1.3 and 4.2.0 as far as I traced it. My concern is whether Comet should match this behavior at all.
Spark raises here only under generated code. The same filter returns rows with spark.sql.codegen.wholeStage=false, and the rows Comet returns today are valid results for the query. Matching the error ties Comet to Spark's predicate ordering, its EquivalentExpressions traversal, the order generated code evaluates operands in, Add canonicalization and the 4.2 filter CSE, and each of these can change in any Spark release. When one changes toward lazier evaluation, Comet raises on a query that Spark completes. That is worse than the gap this PR closes, and with ANSI on by default in Spark 4 it would hit default configs. The coverage is also narrow. simpleOperand limits the dividend to a literal or a column, so shapes like (a - b) / c still differ.
#7480 accepts documenting the difference. Is there a user report that needs error parity here? If not, I'd lean toward the doc section alone.
| // External actions such as foreachPartition or RDD.take can stop reading without adding a | ||
| // physical-plan node. Require a materializing exchange before enabling batch observation. | ||
| visit(plan, requiresRowConsumption = true) | ||
| val partial = plan match { |
There was a problem hiding this comment.
An unrecognized root gets None, which allows the reorder. The metrics logic above defaults the other way. PySpark's df.rdd builds on queryExecution.toRdd, so its root has no DeserializeToObjectExec, and df.rdd.take(1) isn't recognized as a partial reader. Since a wrong guess here fails the query, could an unknown root count as a partial reader for this check?
| } | ||
| } | ||
|
|
||
| // Spark 4.2 evaluates the shared division before testing p and q. |
There was a problem hiding this comment.
On 4.2 canReorder is false, so Comet keeps the original condition. It raises only because p is NULL and AND goes on past a NULL left side. With p = false, q = false, Spark 4.2 still computes 10 DIV x first and raises, but Comet short-circuits and doesn't raise. Could the test use a non-NULL guard, and could the doc list this case?
| For example, below a `LIMIT`, moving a division ahead of a filter condition could raise an | ||
| error on a row beyond the limit. Comet keeps the original order in that filter. | ||
|
|
||
| When `spark.sql.codegen.hugeMethodLimit` is below its default of 65535 bytes, Comet |
There was a problem hiding this comment.
With spark.comet.parquet.rowFilterPushdown.enabled=true the scan applies the pushed IS NOT NULL, so the row never reaches the reordered filter and Comet returns rows where Spark raises. Could this paragraph mention that?
| } | ||
| } | ||
|
|
||
| test("ANSI division with partial readers and shared expressions") { |
There was a problem hiding this comment.
This test covers four separate mechanisms: partial readers, the write root, 4.2 shared terms and floating-point reassociation. Could it be split into named tests so a failure points at the one that broke?
Which issue does this PR close?
Closes #7480.
Rationale for this change
Both queries in #7480 encounter a Parquet row with
x = 0andy = NULL. Spark raisesDIVIDE_BY_ZEROwith ANSI mode enabled.WHERE y IS NOT NULL AND abs(10 DIV x) > 0, Spark's generated filter checks a column for NULL when a predicate first needs it. The comparison needs onlyx, so Spark divides before checkingy. Comet checksyfirst and discards the row. Spark filter code generationSELECT x <> 0 AND abs(10 DIV x) + abs(10 DIV x) > 1, Spark's generated projection evaluates the repeatedabs(10 DIV x)before testingx <> 0. Comet skips the entire right operand and returnsfalse. Whole-stage projection, standalone generated projectionWhat changes are included in this PR?
Both queries keep their Comet operators. The changes apply during plan serialization to eligible expressions containing ANSI
/,DIVor%.ANDorORin a child DataFusion projection. Bind exact matches against that projection's output columns. This preserves the expression trees used for null checks, the order of operations inside lambdas, and short-circuiting for unshared terms. Both projections use the same Spark operator ID for metrics.(x + a) + bfor(x + b) + a. With floating-point inputs the two sums can round to different values. Keep a division under its guard when computing it early would recompute a shared operand with a different expression tree. Apply the same check before reordering Spark 4.2 filter predicates. Spark addition canonicalizationLIMIT. A projection directly under a collect limit can use the existing cap on its input. On Spark 3.x,Dataset.isEmptycan stop early on a zero-column result, while a write command consumes its input even when the command returns zero columns.spark.sql.codegen.hugeMethodLimitis below its default. Spark compares the compiled method size with the configured limit, so a small stage can still run with whole-stage code generation and raise a division error that Comet skips. Spark’s runtime checkHow are these changes tested?
The SQL fixture compares Spark and Comet over a single Parquet file. Both issue reproductions require a Comet plan and check the exception type, error class and SQLSTATE. Controls cover first-use null checks, a zero divisor starting the second batch, nullable arithmetic, a shared decimal division on either side of a collect limit, shared array arguments, floating-point addition order in a lambda, an unshared failing sibling, and codegen and subexpression elimination settings.
A Dataset test feeds the same filter to three consumers.
isEmptyandrdd.take(1)stop at the first match. An unpartitioned ParquetINSERT … SELECTreaches the second row and must raiseDIVIDE_BY_ZEROwith Comet both enabled and disabled. The test also checks the version-specific behavior of a shared filter predicate. On the same input, it compares native projection and Spark 4.2 filter results with Spark when the shared sum is1.0and recomputing the divisor in its written order yields0.0.Ran targeted Spotless and
git diff --check. Hosted CI is configured for all Spark profiles.