You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "vinodkc (via GitHub)" <gi...@apache.org> on 2026/07/19 01:48:33 UTC

[PR] [SPARK-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

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

   <!--
   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
        'core/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.
   -->
   Adds `override def stateful: Boolean = true` to seven JSON/CSV/XML expressions:
   `JsonToStructs`, `GetJsonObject`, `JsonTuple`, `CsvToStructs`, `StructsToCsv`, XmlToStructs`, and `StructsToXml`.
   
   ### 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.
   -->
   These expressions hold mutable state in `@transient lazy val evaluator` fields but did not override `stateful`, so `freshCopyIfContainsStatefulExpression()` never copied them. When two projections share the same expression node, threads race on the shared evaluator and silently corrupt results
   
   ### 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.
   -->
   Added one unit test per suite (`JsonExpressionsSuite`, `CsvExpressionsSuite`,`XmlExpressionsSuite`) 
   
   ### 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.
   -->
   No


-- 
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-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -585,6 +590,8 @@ case class SchemaOfJson(
     }
   }
 
+  override def stateful: Boolean = true

Review Comment:
    Dropped, same reason as SchemaOfJson.



-- 
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-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/csvExpressions.scala:
##########
@@ -187,6 +188,8 @@ case class SchemaOfCsv(
   override protected def withNewChildInternal(newChild: Expression): SchemaOfCsv =
     copy(child = newChild)
 
+  override def stateful: Boolean = true

Review Comment:
   Dropped, same reason as SchemaOfJson.



-- 
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-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

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

   **Merge Summary:**
   - merged into master https://github.com/apache/spark/commit/c2685ecf98e167be991683db7d6a8b9b33e1297b
   - merged into branch-4.x https://github.com/apache/spark/commit/bfb95152ba996746578177007b654da8879f9888
   - merged into branch-4.3 https://github.com/apache/spark/commit/9251fab63e8bdee34d82853f640fad543b679532
   
   *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-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -585,6 +590,8 @@ case class SchemaOfJson(
     }
   }
 
+  override def stateful: Boolean = true

Review Comment:
   `SchemaOfJson` is `RuntimeReplaceable` and requires a **foldable** child (`NON_FOLDABLE_INPUT` check above), and `SchemaOfJsonEvaluator` holds only config lazy-vals (no per-row `var`). A foldable input is constant-folded via a single eval, so no multi-eval race is possible — this override is unnecessary. It's also inconsistent with `SchemaOfXml` (same `RuntimeReplaceable` + config-only-evaluator shape), which is correctly left unmarked. Suggest dropping it (and the matching one on `SchemaOfCsv`).



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -518,6 +521,8 @@ case class StructsToJson(
   override protected def withNewChildInternal(newChild: Expression): StructsToJson =
     copy(child = newChild)
 
+  override def stateful: Boolean = true

Review Comment:
   `StructsToJson` is `RuntimeReplaceable`, so this override is dead at execution. `ReplaceExpressions` (Finish Analysis batch, `Optimizer.scala:338`) rewrites this node to `Invoke(Literal(evaluator), ...)` before any execution-time `freshCopyIfContainsStatefulExpression`, and `RuntimeReplaceable.eval` asserts `input == null` (`Expression.scala:462`) so it's never row-evaluated in place. After replacement the evaluator is pinned inside a `Literal` (non-stateful `LeafExpression`), and `freshCopy` on a `Literal` returns `this` — both copies keep the same evaluator. So if a shared-node race is real for `StructsToJson` (its evaluator does hold mutable `writer`/`gen` and it accepts a non-foldable child), this override does **not** fix it; the sharing would have to be broken where the evaluator actually lives (inside `replacement`).



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala:
##########
@@ -1041,4 +1042,42 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
       input)
   }
 
+  test("JsonToStructs, GetJsonObject, JsonTuple are stateful and produce fresh copies") {
+    val schema = StructType(StructField("a", IntegerType) :: Nil)
+    val jsonToStructs = JsonToStructs(schema, Map.empty, Literal("{}"), UTC_OPT)
+    assert(jsonToStructs.stateful)
+    assert(jsonToStructs.freshCopyIfContainsStatefulExpression() ne jsonToStructs)
+
+    val getJsonObject = GetJsonObject(Literal("{}"), Literal("$.a"))
+    assert(getJsonObject.stateful)
+    assert(getJsonObject.freshCopyIfContainsStatefulExpression() ne getJsonObject)
+
+    val jsonTuple = JsonTuple(Literal("{}") :: Literal("a") :: Nil)
+    assert(jsonTuple.stateful)
+    assert(jsonTuple.freshCopyIfContainsStatefulExpression() ne jsonTuple)
+  }
+
+  test("StructsToJson and SchemaOfJson are stateful and produce fresh copies with " +

Review Comment:
   This test asserts `copy1.replacement...evaluator ne copy2.replacement...evaluator` on the **pre-replacement** node — each `freshCopy` trivially has its own `@transient lazy val evaluator`, so this passes regardless. But it doesn't reflect the executed plan: `ReplaceExpressions` materializes the `Invoke` once and the evaluator is shared through the resulting `Literal` (see the `StructsToJson` comment). So for the `RuntimeReplaceable` expressions this assertion is green without exercising the runtime sharing it's meant to guard. If the intent is to prove `StructsToJson` no longer races, the test needs to go through replacement (e.g. run `ReplaceExpressions` / an end-to-end duplicated-projection query) rather than inspecting `.replacement` on the un-replaced node.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/csvExpressions.scala:
##########
@@ -187,6 +188,8 @@ case class SchemaOfCsv(
   override protected def withNewChildInternal(newChild: Expression): SchemaOfCsv =
     copy(child = newChild)
 
+  override def stateful: Boolean = true

Review Comment:
   Same as `SchemaOfJson`: `SchemaOfCsv` is `RuntimeReplaceable`, requires a foldable child, and `SchemaOfCsvEvaluator` holds only config lazy-vals — constant-folded, so no race is possible and the override is unnecessary/dead at execution. Inconsistent with the unmarked `SchemaOfXml`. Suggest dropping it.



-- 
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-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

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


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala:
##########
@@ -1041,4 +1042,42 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
       input)
   }
 
+  test("JsonToStructs, GetJsonObject, JsonTuple are stateful and produce fresh copies") {
+    val schema = StructType(StructField("a", IntegerType) :: Nil)
+    val jsonToStructs = JsonToStructs(schema, Map.empty, Literal("{}"), UTC_OPT)
+    assert(jsonToStructs.stateful)
+    assert(jsonToStructs.freshCopyIfContainsStatefulExpression() ne jsonToStructs)
+
+    val getJsonObject = GetJsonObject(Literal("{}"), Literal("$.a"))
+    assert(getJsonObject.stateful)
+    assert(getJsonObject.freshCopyIfContainsStatefulExpression() ne getJsonObject)
+
+    val jsonTuple = JsonTuple(Literal("{}") :: Literal("a") :: Nil)
+    assert(jsonTuple.stateful)
+    assert(jsonTuple.freshCopyIfContainsStatefulExpression() ne jsonTuple)
+  }
+
+  test("StructsToJson and SchemaOfJson are stateful and produce fresh copies with " +

Review Comment:
   Removed the StructsToJson/SchemaOfJson test since those overrides are dropped. Added MultiGetJsonObject assertion to the existing test instead.



-- 
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-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

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

    Added override def stateful: Boolean = true to MultiGetJsonObject.


-- 
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-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -518,6 +521,8 @@ case class StructsToJson(
   override protected def withNewChildInternal(newChild: Expression): StructsToJson =
     copy(child = newChild)
 
+  override def stateful: Boolean = true

Review Comment:
    Dropped. 
   Since it's RuntimeReplaceable, the override is dead at execution,  the sharing via Literal(evaluator) will be addressed by SPARK-58208's deep-copy in QueryExecution.optimizedPlan.



-- 
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-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -585,6 +590,8 @@ case class SchemaOfJson(
     }
   }
 
+  override def stateful: Boolean = true

Review Comment:
   Dropped. 



-- 
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-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races [spark]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon closed pull request #57354: [SPARK-58205][SQL] Mark JSON/CSV/XML expressions stateful to prevent shared-evaluator data races
URL: https://github.com/apache/spark/pull/57354


-- 
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