HeartSaVioR opened a new pull request, #60707:
URL: https://github.com/apache/spark/pull/60707

   ### What changes were proposed in this pull request?
   
   In a streaming query, a source without new data in a micro-batch is planned 
as an empty
   `LocalRelation`. `PropagateEmptyRelation` removes such empty children from a 
`Union`, so the plan
   of a query can change from one micro-batch to the next. When another rule 
then pushes a stateful
   operator below the `Union`, the number of stateful operators changes too, 
and a new operator
   looks for state that no earlier batch wrote.
   
   This PR keeps the empty children of a streaming `Union`, so the plan has the 
same shape in every
   micro-batch. `PropagateEmptyRelation` already keeps empty streaming 
relations for joins,
   aggregations and limits for the same kind of reason. Batch queries are not 
affected: the change
   applies only when the `Union` is streaming.
   
   A new internal conf, 
`spark.sql.optimizer.propagateEmptyRelationCanPruneStreamingUnionChildren`
   (default `false`), restores the old behavior. It is pinned in the offset 
log, and checkpoints
   written before this change read it as `true`, so their plans and stateful 
operators stay as
   they were. This follows SPARK-49699 
(`spark.sql.optimizer.pruneFiltersCanPruneStreamingSubplan`).
   
   ### Why are the changes needed?
   
   A stream-stream left semi join over a union fails when a union input gets 
its first data after
   the first micro-batch. `PushDownLeftSemiAntiJoin` pushes the join below the 
union, one join per
   union input, but in the first batch the empty input had been removed. For 
example, with
   `spark.sql.streaming.noDataMicroBatches.enabled=false`:
   
   ```scala
   def withTime(input: MemoryStream[(Int, Int)]) = input.toDF().toDF("lk", "t")
     .select($"lk", timestamp_seconds($"t").as("lt"))
   val left = withTime(input1).withWatermark("lt", "10 seconds")
     .union(withTime(input2))          // no watermark on this input
   val right = rightInput.toDF().toDF("rk", "s")
     .select($"rk", timestamp_seconds($"s").as("rt"))
   left.join(right, expr("lk = rk AND lt = rt"), "left_semi")
     .select($"lk", $"lt".cast("long"))
   // batch 0: input1 (1, 5) and rightInput (1, 5)
   // batch 1: input2 (2, 6) and rightInput (2, 6)
   ```
   
   - Before: batch 0 outputs `(1, 5)` with one stream-stream join. In batch 1 
the plan has a second
     join, and the query fails with `CANNOT_LOAD_STATE_STORE` 
(`state/1/<partition>/1.changelog does
     not exist`). This happens on every state format. The query also fails 
later whenever input2
     stops getting data for a batch and then gets data again.
   - After: `(1, 5)`, then `(2, 6)`. The plan has two joins in every 
micro-batch.
   
   Only an input whose path to the union is stateless and has no watermark is 
removed: an
   `EventTimeWatermark` or a streaming stateful operator stops the empty 
relation from spreading up.
   So stateful operators inside a union input, and aggregations or 
deduplications above the union,
   are not affected. A left anti join with a streaming right side is not 
supported, so left anti
   joins are not affected either.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. New streaming queries keep empty union inputs in their plans, so a 
stateful operator that
   the optimizer pushes below a union is planned the same way in every 
micro-batch, and such queries
   no longer fail as above. Existing checkpoints keep the old behavior through 
the offset log.
   Batch queries do not change.
   
   ### How was this patch tested?
   
   - `PropagateEmptyRelationSuite`: a streaming union keeps its empty child, 
and drops it with the
     conf set to `true`.
   - `OffsetSeqLogSuite`: an old offset log reads the conf as `true`, a new one 
records `false`.
   - `StreamingJoinSuite` (`StreamingLeftSemiJoinBase`, state formats V2, V3 
and V4): the example
     above, with the second input starting late, then pausing and coming back; 
and a restart from
     a checkpoint without the conf, which keeps the single-join plan and finds 
its state.
   
   Without the change to `PropagateEmptyRelation`, the 
`PropagateEmptyRelationSuite` test and the
   late-input tests fail. Without the value for old checkpoints, the restart 
test fails, because
   the query then plans two joins where the checkpoint has one.
   
   Possible follow-up, not checked here: `PushDownJoinThroughUnion` (inner and 
left outer joins, off
   by default through `spark.sql.optimizer.pushDownJoinThroughUnion.enabled`) 
has no streaming guard
   either. It pushes a join below a union only when the right side is 
broadcast, which could depend
   on a streaming input being empty in a micro-batch.
   
   ### Note for reviewers, including AI review
   
   Please review only this PR's own commit(s). For each finding, give a 
runnable reproducer and
   say whether master has the same behavior.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to