You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "stefankandic (via GitHub)" <gi...@apache.org> on 2026/09/14 15:24:21 UTC

[PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

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

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'common/utils/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
   
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on code in PR #58779:
URL: https://github.com/apache/spark/pull/58779#discussion_r4017896992


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/nullExpressions.scala:
##########
@@ -187,8 +187,18 @@ private case class TypedNullLiteral(child: Expression)
 
   override lazy val replacement: Expression = Literal.create(null, child.dataType)
 
-  override protected def withNewChildInternal(newChild: Expression): TypedNullLiteral =
-    copy(child = newChild)
+  override protected def withNewChildInternal(newChild: Expression): Expression =
+    TypedNullLiteral.create(newChild)
+}
+
+private object TypedNullLiteral {
+  def create(child: Expression): Expression = {
+    if (child.resolved) {
+      Literal.create(null, child.dataType)

Review Comment:
   IfTypeCoercion now synchronizes TypedNullLiteral with the coerced return branch. For With, definitions are transformed first so references have the updated type before coercing the body. Added regression coverage for ANSI and inline settings.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on code in PR #58779:
URL: https://github.com/apache/spark/pull/58779#discussion_r4050303878


##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala:
##########
@@ -363,6 +363,23 @@ class DataFrameFunctionsSuite extends SharedSparkSession {
     }
   }
 
+  test("nullif keeps the coerced type of its first argument") {
+    Seq(true, false).foreach { alwaysInlineCommonExpr =>
+      Seq(true, false).foreach { ansiEnabled =>
+        withSQLConf(
+          SQLConf.ALWAYS_INLINE_COMMON_EXPR.key -> alwaysInlineCommonExpr.toString,
+          SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString,
+          SQLConf.CONCAT_BINARY_AS_STRING.key -> "true") {
+          checkAnswer(sql("SELECT nullif(concat(X'61', X'62'), 'z')"), Row("ab"))

Review Comment:
   Fixed by testing both analyzers independently. I will track the plan discrepancy separately.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on PR #58779:
URL: https://github.com/apache/spark/pull/58779#issuecomment-5763166481

   @uros-b 
   
   1. I don't think the view test is needed. `NullIfResult` creates the null literal after analysis
   from the final `value.dataType`, so the earlier stale-collation state is no longer possible.
   
   2. Good catch. I replaced the obsolete `typednullliteral` explain check with `nullifresult`.
   
   3. I am keeping `branchGroups = Nil`. The value branch is not always reached, and the proposed
   singleton group would fail `EquivalentExpressions`' `exprs.length > 1` assertion.
   
   4. The growth test intentionally checks tree size. Nested `nullif` evaluation is already covered in
   `ColumnExpressionSuite` and `WholeStageCodegenSuite`.


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b commented on PR #58779:
URL: https://github.com/apache/spark/pull/58779#issuecomment-5765262891

   **Merge Summary:**
   - merged into master https://github.com/apache/spark/commit/d8a7143d4fd819eb5b6bfab74bfcbec65d235f5e
   - merged into branch-4.x https://github.com/apache/spark/commit/3cb3da6227dc2c1da77cb61bd97f81a97c828cf7
   - merged into branch-4.3 https://github.com/apache/spark/commit/fba9e4a2554c41a978d1bdfcac61be15908a2901
   
   *Posted by `merge_spark_pr.py`*


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b closed pull request #58779: [SPARK-59496][SQL] Avoid exponential NullIf plan growth
URL: https://github.com/apache/spark/pull/58779


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "stefankandic (via GitHub)" <gi...@apache.org>.
stefankandic commented on code in PR #58779:
URL: https://github.com/apache/spark/pull/58779#discussion_r4048657246


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/nullExpressions.scala:
##########
@@ -187,8 +187,17 @@ private case class TypedNullLiteral(child: Expression)
 
   override lazy val replacement: Expression = Literal.create(null, child.dataType)
 
-  override protected def withNewChildInternal(newChild: Expression): TypedNullLiteral =
-    copy(child = newChild)
+  override protected def withNewChildInternal(newChild: Expression): Expression =
+    TypedNullLiteral.create(newChild)
+}
+
+private[sql] object TypedNullLiteral {
+  /**
+   * Replaces a resolved child with a typed null marker to avoid 3^n growth in nested `NullIf`
+   * plans.
+   */
+  def create(child: Expression): TypedNullLiteral = TypedNullLiteral(
+    if (child.resolved) Literal.create(null, child.dataType) else child)

Review Comment:
   Fixed by construction now: the null literal is created only after analysis, using the final value.dataType. It is never visited independently by collation coercion, so the mismatched-collation state is no longer possible. The structural invariant is covered in NullExpressionsSuite.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on code in PR #58779:
URL: https://github.com/apache/spark/pull/58779#discussion_r4018751683


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/nullExpressions.scala:
##########
@@ -187,8 +187,18 @@ private case class TypedNullLiteral(child: Expression)
 
   override lazy val replacement: Expression = Literal.create(null, child.dataType)
 
-  override protected def withNewChildInternal(newChild: Expression): TypedNullLiteral =
-    copy(child = newChild)
+  override protected def withNewChildInternal(newChild: Expression): Expression =
+    TypedNullLiteral.create(newChild)
+}
+
+private object TypedNullLiteral {
+  def create(child: Expression): Expression = {
+    if (child.resolved) {
+      Literal.create(null, child.dataType)

Review Comment:
   Confirmed fixed at e0e60fd2b38d1c58a3e12eec478c93c7a11d67f6: the typed marker is synchronized with the coerced return branch, and With refreshes its references before body coercion. The new regression covering both ANSI and inlining settings passed in the exact-commit CI reports. Resolving this thread; I reported a separate reproduced single-pass view-collation issue in the new review.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/nullExpressions.scala:
##########
@@ -187,8 +187,17 @@ private case class TypedNullLiteral(child: Expression)
 
   override lazy val replacement: Expression = Literal.create(null, child.dataType)
 
-  override protected def withNewChildInternal(newChild: Expression): TypedNullLiteral =
-    copy(child = newChild)
+  override protected def withNewChildInternal(newChild: Expression): Expression =
+    TypedNullLiteral.create(newChild)
+}
+
+private[sql] object TypedNullLiteral {
+  /**
+   * Replaces a resolved child with a typed null marker to avoid 3^n growth in nested `NullIf`
+   * plans.
+   */
+  def create(child: Expression): TypedNullLiteral = TypedNullLiteral(
+    if (child.resolved) Literal.create(null, child.dataType) else child)

Review Comment:
   [P2] Preserve the generated null's collation during view resolution
   
   This generated `Literal` is revisited by the single-pass resolver and receives the view's default collation, even though its type came from the operand. With the default `spark.sql.alwaysInlineCommonExpr=false`, this reproduces on the current compiled build:
   
   ```sql
   SET spark.sql.analyzer.singlePassResolver.enabled=false;
   CREATE TABLE t (c STRING) USING parquet;
   INSERT INTO t VALUES ('a');
   CREATE VIEW v DEFAULT COLLATION UTF8_LCASE AS
     SELECT lower(nullif(c, c)) AS n FROM t;
   SET spark.sql.analyzer.singlePassResolver.enabled=true;
   SELECT * FROM v;
   ```
   
   The last query fails with `INDETERMINATE_COLLATION_IN_EXPRESSION` for `lower(nullif(c, c))`; the base-code control returns null. `DefaultCollationTypeCoercion` recolors the generated null to UTF8_LCASE while the false branch's `CommonExpressionRef` remains UTF8_BINARY. Collation coercion then wraps the marker in a cast, so the new `IfTypeCoercion` case cannot recognize and repair it. Please keep this generated null's operand-derived collation intact, or synchronize it before collation coercion wraps it, and add a single-pass view regression.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on code in PR #58779:
URL: https://github.com/apache/spark/pull/58779#discussion_r4007571888


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/nullExpressions.scala:
##########
@@ -187,8 +187,18 @@ private case class TypedNullLiteral(child: Expression)
 
   override lazy val replacement: Expression = Literal.create(null, child.dataType)
 
-  override protected def withNewChildInternal(newChild: Expression): TypedNullLiteral =
-    copy(child = newChild)
+  override protected def withNewChildInternal(newChild: Expression): Expression =
+    TypedNullLiteral.create(newChild)
+}
+
+private object TypedNullLiteral {
+  def create(child: Expression): Expression = {
+    if (child.resolved) {
+      Literal.create(null, child.dataType)

Review Comment:
   [P2] Keep the null type synchronized through type coercion
   
   `child.resolved` does not guarantee that its output type is final. With the fixed-point analyzer and `spark.sql.function.concatBinaryAsString=true`, `SELECT nullif(concat(X'61', X'62'), 'z')` constructs `NullIf` while the all-binary `Concat` is already resolved as `BinaryType`. This branch therefore becomes `Literal(null, BinaryType)`. `ConcatTypeCoercion` subsequently changes the operand to `StringType`, but the literal cannot follow that change: ANSI analysis casts the returned value back to binary, while non-ANSI analysis fails with incompatible IF branch types. Previously the placeholder tracked the changed child type. This affects both `spark.sql.alwaysInlineCommonExpr` settings; the `With` path updates its references but cannot update this detached literal. Please preserve type synchronization until coercion is complete and add a regression across both ANSI and inline settings, checking that this query still returns the string `ab`.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on code in PR #58779:
URL: https://github.com/apache/spark/pull/58779#discussion_r4049840611


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/nullExpressions.scala:
##########
@@ -187,8 +187,17 @@ private case class TypedNullLiteral(child: Expression)
 
   override lazy val replacement: Expression = Literal.create(null, child.dataType)
 
-  override protected def withNewChildInternal(newChild: Expression): TypedNullLiteral =
-    copy(child = newChild)
+  override protected def withNewChildInternal(newChild: Expression): Expression =
+    TypedNullLiteral.create(newChild)
+}
+
+private[sql] object TypedNullLiteral {
+  /**
+   * Replaces a resolved child with a typed null marker to avoid 3^n growth in nested `NullIf`
+   * plans.
+   */
+  def create(child: Expression): TypedNullLiteral = TypedNullLiteral(
+    if (child.resolved) Literal.create(null, child.dataType) else child)

Review Comment:
   Confirmed fixed at d1f174c4624df6652fd7063ab414dca6a204ffec. I reran the view reproduction using the revised production classes: it returns the expected null result under legacy, single-pass, and dual analysis with both common-expression settings. NullIfResult keeps the generated null out of analysis, so it no longer receives the view's default collation independently. Resolving this thread.



##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala:
##########
@@ -363,6 +363,23 @@ class DataFrameFunctionsSuite extends SharedSparkSession {
     }
   }
 
+  test("nullif keeps the coerced type of its first argument") {
+    Seq(true, false).foreach { alwaysInlineCommonExpr =>
+      Seq(true, false).foreach { ansiEnabled =>
+        withSQLConf(
+          SQLConf.ALWAYS_INLINE_COMMON_EXPR.key -> alwaysInlineCommonExpr.toString,
+          SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString,
+          SQLConf.CONCAT_BINARY_AS_STRING.key -> "true") {
+          checkAnswer(sql("SELECT nullif(concat(X'61', X'62'), 'z')"), Row("ab"))

Review Comment:
   [P2] Make the added regression pass under the suite's analyzer validation
   
   This new test currently fails in [the exact-head CI run](https://github.com/stefankandic/spark/runs/105709484058) with `HYBRID_ANALYZER_EXCEPTION.LOGICAL_PLAN_COMPARISON_MISMATCH`. With `alwaysInlineCommonExpr=false`, fixed-point analysis retains binary casts in the equality predicate, while single-pass analysis compares strings directly. I reproduced the mismatch with both ANSI settings; each analyzer individually returns the expected string `ab`.
   
   The same mismatch reproduces with the original production classes, so this is an existing analyzer discrepancy exposed by the new test, rather than new result corruption from `NullIfResult`. Nevertheless, the added test leaves this PR's SQL suite failing. Please reconcile the plans, or explicitly run this result-typing regression against each analyzer independently while tracking the pre-existing dual-run discrepancy separately.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-59496][SQL] Avoid exponential NullIf plan growth [spark]

Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b commented on PR #58779:
URL: https://github.com/apache/spark/pull/58779#issuecomment-5765270501

   Also, thank you @HyukjinKwon and @sunchao for review!


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org