spark-test commented on code in PR #60117:
URL: https://github.com/apache/spark/pull/60117#discussion_r4477044575
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/streaming/OffsetSeqLogSuite.scala:
##########
@@ -93,6 +93,14 @@ class OffsetSeqLogSuite extends SharedSparkSession {
assert(sessionConf.getConfString(key.key) === "true")
}
+ test("SPARK-60905: Python UDFs are expensive only for checkpoints that
record it") {
+ val entry = SQLConf.AVOID_DOUBLE_FILTER_EVAL_FOR_PYTHON_UDF
+ val (_, offsetSeq) = readFromResource("offset-log-version-2.1.0")
Review Comment:
**Nit (P3):** This reads the pre-4.2 offset log. There
`avoidDoubleFilterEval` already resolves to false, so
`PushPredicateThroughNonJoin` pushes every conjunct without calling
`isExpensive`, and the new setting has no effect. The regime this PR fixes is a
4.2.x or 4.3.x checkpoint, where `avoidDoubleFilterEval` resolves to true.
Neither this test nor the FilterPushdownSuite case, which sets the flag by
hand, gives a failure signal there, and nothing restarts a query without the
key. So a missing-key default that became vintage-dependent, like
`avoidDoubleFilterEval`'s, or a restored value that never reached the optimizer
would keep every new assertion green while bringing back both reported failures.
The sibling settings already have this coverage. The SPARK-60885 case just
above reads `checkpoint-version-4.2.0-deduplication-metadata-boundary/offsets`,
which records the source-evolution setting but not this one.
StreamingQueryManagerSuite has restart tests for both settings: the SPARK-60632
one removes the key with `rewriteOffsetLogConf` and checks where the filter is
planned, and the SPARK-60885 one restarts a stream-stream join query. The same
patterns fit here, with a `TestPythonUDF` under `shouldTestPythonUDFs` for the
restart.
**Verification:**
- **Compatibility:** Reading the offset log of a 4.2-era checkpoint, one
that records the source-evolution setting but not
avoidDoubleFilterEvalForPythonUDF, yields false for the new setting, and
applying that log to a session where the setting is true changes it to false.
- **Regression:** A streaming query whose filter on a Python UDF result sits
above a watermark or a stream-stream join keeps the filter above the projection
on a fresh checkpoint. After the setting is removed from the latest offset-log
entry, its restart plans the filter below the projection. The check is skipped
when Python UDFs cannot run.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -3088,12 +3088,23 @@ object PushPredicateThroughNonJoin extends
Rule[LogicalPlan] with PredicateHelpe
/**
* Whether `e` is expensive. Version 0 of the regex pattern cost estimate
treats every LIKE and
- * RLIKE as expensive. It is checked here rather than in
`StringRegexExpression.expensive`,
- * because `hasExpensiveChild` caches the answer of a parent expression.
+ * RLIKE as expensive, and with `avoidDoubleFilterEvalForPythonUDF` off a
Python UDF is only as
+ * expensive as its arguments. Both are checked here rather than in
`expensive`, because
+ * `hasExpensiveChild` caches the answer of a parent expression.
*/
- private def isExpensive(e: Expression): Boolean = e.expensive ||
- (conf.getConf(SQLConf.REGEX_PATTERN_COST_ESTIMATE_VERSION) == 0 &&
- e.exists(_.isInstanceOf[StringRegexExpression]))
+ private def isExpensive(e: Expression): Boolean = {
+ val classified = if
(conf.getConf(SQLConf.AVOID_DOUBLE_FILTER_EVAL_FOR_PYTHON_UDF)) {
+ e
+ } else {
+ // The stand-in is not foldable either, so a LIKE/RLIKE pattern from a
UDF stays expensive.
+ e.transformUp {
Review Comment:
**Nit (P3):** With `avoidDoubleFilterEvalForPythonUDF` false, this
`transformUp` runs on every `isExpensive` call, before `expensive` is
consulted. It is unpruned, so each call walks the whole alias tree. For an
alias that holds a Python UDF, it also rebuilds every ancestor and recomputes
their lazy `hasExpensiveChild`, only to discard the copy. `isCheap` calls
`isExpensive` once per referenced alias of each conjunct, and
`splitProjectForExpensiveConditions` once per alias, in every optimizer
iteration of every micro-batch. Every streaming query restarted from a 4.2.x or
4.3.x checkpoint runs in this mode for the life of its checkpoint, whether or
not it uses Python UDFs.
Most of that work cannot change the answer. Replacing a Python UDF with
`PrettyPythonUDF` can only lower `expensive`. Each override is constant `true`,
`hasExpensiveChild`, or `hasExpensiveChild || _expensivePattern`, and the
stand-in is non-foldable, like the UDFs it replaces. So an alias whose
`e.expensive` is false stays cheap with or without the rewrite. Rewriting only
when `e.expensive` is true gives the same answers without the walk, for example:
```scala
// The stand-in can only make `e` cheaper, so a cheap `e` needs no
rewrite.
val classified =
if (!e.expensive ||
conf.getConf(SQLConf.AVOID_DOUBLE_FILTER_EVAL_FOR_PYTHON_UDF)) {
e
} else {
...
}
```
The version-0 regex check below stays as it is. The cost is optimizer CPU
only; plans and results are not affected.
**Verification:**
- **Regression:** With the setting false, every alias keeps the
classification it has today. The optimizer filter pushdown tests pass
unchanged, including the case that pushes a filter on a Python UDF alias with
cheap arguments while one over an expensive RLIKE stays above, the cases that
keep scalar Python UDF filters above the projection by default, and the regex
cost-estimate version 0 cases. A query whose aliases hold no expensive part,
such as `a + 1`, still has its filters pushed below the projection.
--
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]