sarutak opened a new pull request, #60337: URL: https://github.com/apache/spark/pull/60337
### What changes were proposed in this pull request? `ConstantPropagation` moves the equalities of an `AND`, such as `a = 1`, in front of its other conjuncts when a conjunct that can not be evaluated unconditionally (`ExprUtils.canEvaluateUnconditionally`), e.g. one that can fail or is non-deterministic, keeps a propagated attribute (SPARK-60983), or comes after a conjunct that is rewritten (SPARK-61033). This guards the conjunct that can fail: e.g. in `d <> 0 AND n / d > 1 AND d = 5`, the guard `d <> 0` becomes `TRUE`, and `n / d > 1` only runs on rows where `d = 5` is not `FALSE`. This PR keeps the conjuncts in their written order instead, and replaces an attribute in a conjunct only: - with the constant of an equality before the conjunct, which holds on the rows that reach the conjunct where the equality is not `NULL`; - with the constant of an equality after the conjunct, only if every conjunct in between, as rewritten, can be evaluated unconditionally. On a row where the attribute of an `EqualTo` is `NULL`, the equality is `NULL`, so the row also reaches the conjuncts after it. There, a conjunct rewritten with the equality's constant, before or after it, can still decide whether a later one runs, as on master and in released versions: with ANSI mode on, `(a = 1 AND coalesce(a, 0) > 0 AND 10 / c > 0) OR b > 0` over the rows `(a, b, c) = (NULL, 1, 0), (1, 0, 1)` fails with `DIVIDE_BY_ZERO` on master, with this PR and on 4.1.4, and returns both rows without the rule; so does `(coalesce(a, 0) > 0 AND a = 1 AND 10 / c > 0) OR b > 0` on master and with this PR. This PR does not change that; SPARK-61438 is to fix it. So, with ANSI mode on, `d <> 0 AND n / d > 1 AND d = 5` is left as it is, as are `y > 0 AND 10 / x > 0 AND y = 1` and `y > 0 AND rand() < 0.5 AND y = 1`, while `y = 1 AND 10 / x > 0 AND y > 0` still becomes `y = 1 AND 10 / x > 0`, and `y > 0 AND x > y AND y = 1` still becomes `x > 1 AND y = 1`. The rules within a conjunct (SPARK-60983, SPARK-61033) are unchanged. A condition with a conjunct that is `FALSE` with the constants of all the equalities, such as `a = 1 AND 10 / c > 0 AND a = 2`, still becomes `FALSE`, also when a conjunct that can fail separates them, as no row passes it. An `EqualTo` with a `NULL` literal, which is `NULL` on every row, is no longer propagated, so the equalities under a `NOT`, whose attributes are not nullable, are never `NULL`. Propagating it turns e.g. `NOT (d = NULL AND 10 / c > 0 AND (d + 0) <=> 5)` into `TRUE` together with `ConstantFolding` and `BooleanSimplification`, although the condition is `NULL` where `d` is 5 and `c` is positive. With the default rules, `NullPropagation` removes such an equality first. With it excluded, `spark.range(4, 7, 1, 1).select($"id", lit(1) as "c").where(!($"id" === typedLit[java.lang.Long](null) && lit(10) / $"c" > 0 && ($"id" + 0 <=> 5)))` returns all three rows, including the one with `id` 5, on master, 4.1.4 and 4.0.5, and only the rows with `id` 4 and 6 with this PR. ### Why are the changes needed? Moving an equality ahead of the conditions written before it changes what those conditions see. A non-deterministic condition before it then only runs on the rows that pass the equality, so the query returns other rows than without `ConstantPropagation`. Replacing an attribute with the constant of a later equality in a condition before a non-deterministic one does the same, as the condition can become `TRUE` and stop dropping rows, which released versions do. Over a single file: ```scala spark.range(0, 100, 1, 1).select($"id", $"id" % 3 as "a").write.parquet(path) val df = spark.read.parquet(path) // With ANSI mode off or on. df.where($"a" > 0 && rand(10) < 0.5 && $"a" === 1).count() // With ANSI mode on, where the multiplication can overflow, so it keeps `a`. df.where(rand(10) < 0.5 && $"id" * $"a" > 0 && $"a" === 1).count() ``` On master with `spark.sql.optimizer.excludedRules` set to `ConstantPropagation`, or with this PR, they return 14 and 16, and the filters keep the written order. On master, they return 13 and 13, where the optimized filters are `isnotnull(a) AND ((a = 1) AND (rand(10) < 0.5))` and `(a = 1) AND isnotnull(a) AND isnotnull(id) AND (rand(10) < 0.5) AND ((id * a) > 0)`. On 4.1.4 and 4.3.0 with ANSI mode off, the first one returns 16, where the filter is `isnotnull(a) AND ((rand(10) < 0.5) AND (a = 1))`. Also, a later rule can turn the moved equality into a condition that can fail. #59940 (SPARK-60730) keeps the conditions after one over an expensive alias, such as a UDF, above the projection, in their written order. There, with `i` the alias `cast(s as int)`, `f` a UDF and ANSI mode on, `ConstantPropagation` moves `i = 1` in front of `f > 0 && i * id > 0 && i = 1`, so the cast is pushed below the projection ahead of `f > 0`, and fails with `CAST_INVALID_INPUT` on rows `f > 0` drops. With this PR on top of #59940, the query returns its row. This PR is to land before #59940. Whether a conjunct can fail is judged on it as it is when the rule runs, and an equality in between is not checked, so a conjunct over such an alias that is kept above the projection, such as `i = 1` or `i > 0`, counts as one that can not fail, and a condition before it can still take the constant of an equality after it; SPARK-61437 is to fix that. SPARK-60983 and SPARK-61033 are in master and branch-4.x only, so no released version moves the equalities. Released versions replace the attributes in every conjunct, and since SPARK-42500 (4.0.0) in every comparison in it; the first query above also returns 16 on 4.0.5 and 4.2.1 with ANSI mode off. This targets master and branch-4.x. This PR is based on SPARK-60983 and SPARK-61033, so bringing it to branch-4.3 and older would need them there as well. ### Does this PR introduce _any_ user-facing change? Compared with released versions, a condition no longer takes the constant of a later equality when a condition that `ExprUtils.canEvaluateUnconditionally` rejects, e.g. one that can fail, is non-deterministic or has a subquery, is between them. So, but for the rows where an equality is `NULL` described above, the non-deterministic condition runs on the same rows as without the rule, e.g. the first query above returns 14 instead of 16 with ANSI mode off, and so does the condition that can fail: with ANSI mode on, `y > 0 AND 10 / x > 0 AND y = 1` over the rows `(y, x) = (0, 0), (1, 1)` returns `(1, 1)`, while 4.1.4 rewrites `y > 0` to `TRUE` and fails with `DIVIDE_BY_ZERO`. Such a condition is no longer simplified, e.g. `p > y` in `p > y AND amount + 1 > 0 AND y = 5` with ANSI mode on stays, where 4.1.4 and master have `p > 5`. With `p` a partition column, the partition filter `p > 5` is still inferred from the constraints, but with `spark.sql.constraintPropagation.enabled` set to ` false`, 4.1.4 and master prune the partitions with `p > 5` and this PR does not. Compared with master, the conjuncts of a filter keep their written order in `ConstantPropagation`, so a non-deterministic condition before an equality runs on the same rows as without the rule, but for the rows where an equality is `NULL`. A condition that can fail also runs on the rows that an equality after it drops, as written, as in released versions and as without the rule, so a query that succeeds on master can fail: e.g. with ANSI mode on, `a > 0 AND 10 / c > 0 AND a = 1` over the rows `(a, c) = (1, 1), (2, 0)` in one file returns `(1, 1)` on master and fails with `DIVIDE_BY_ZERO` with this PR and without the rule. A condition separated from a later equality by a condition that `ExprUtils.canEvaluateUnconditionally` rejects, e.g. one that can fail, is non-deterministic or has a subquery, such as `d <> 0` in `d <> 0 AND n / d > 1 AND d = 5`, is no longer simplified with the equality's constant. A costly condition written before an equality, such as one with a UDF, again runs on all the rows that reach it, as in released versions. Compared with both, an `EqualTo` with a `NULL` literal is no longer propagated, which changes the result of the query with `NullPropagation` excluded described above; with the default rules, the results are the same. ### How was this patch tested? New tests in `ConstantPropagationSuite`, which also check the order of the conjuncts, since `comparePlans` sorts them: a conjunct before a conjunct that can fail, is non-deterministic or has a subquery and an equality is not rewritten, also in a nested `AND`; equalities before a conjunct are propagated into it across such a conjunct; equalities after it are propagated when the conjuncts in between can not fail, also when one can fail only before rewriting, in one pass of the rule; the written order is kept in an `AND` under an `OR`; a conjunct that is `FALSE` with the constants, also not the first one, still makes the condition `FALSE` across a conjunct that can fail, also in an `OR` and under a `NOT`, but not one that is `NULL` with them; an equality identical to the propagated one counts as it, and one in another form does not; an `EqualNullSafe` before a conjunct gives its constant across a conjunct that can fail; an `EqualTo` with a `NULL` literal on either side is not propaga ted, but an `EqualNullSafe` with one is; the written order is also kept for an `EqualNullSafe`, a literal on the left, under a `NOT`, and for the first of two equalities over the same attribute; the shape of a nested `AND` is kept. The existing tests that expected an equality in front now expect the written order. A new test in `DataFrameSuite` with the queries above, which fails on master. Also ran `catalyst/testOnly org.apache.spark.sql.catalyst.optimizer.*`, and `DataFrameSuite`, `SubquerySuite`, `SQLQuerySuite`, `SQLQueryTestSuite`, `InjectRuntimeFilterSuite` and the TPC-DS / TPC-H plan stability suites in `sql`, without changes to golden files or approved plans. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Kiro CLI 3.1.0 (Claude Opus 5.5) -- 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]
