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

[PR] [SPARK-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

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

   ### What changes were proposed in this pull request?
   
   `StringSubstitutor` (added in SPARK-52990) is the hand-rolled `${name}` / `${name:-default}` interpolation parser behind every error-message format, but it has no direct test coverage. This PR adds `StringSubstitutorSuite` to exercise it on its own.
   
   The suite covers basic substitution, no-placeholder/empty/null input, non-string values rendered via `toString`, undefined variables (both the throwing default and the leave-as-is mode), default values (resolved, defaulted, and empty), substitution that grows the buffer, escaped placeholders under `preserveEscapes` true/false, nested expansion off/on, cycle detection, an unclosed placeholder, and an empty default.
   
   While adding the suite I also tidied a small inconsistency in the default-value branch of `substitute`: it computed `newLength` (the scan length adjusted for the replacement) but then recursed with the original, unadjusted `length`, leaving `newLength` dead. The resolved-value branch right above it already recurses with `newLength`. This makes the two branches symmetric. The change is behavior-preserving: a default value is always shorter than the `${name:-default}` placeholder it replaces, so the adjusted length is only ever smaller, and since `length` serves solely as the upper bound of the scan (`endPos = offset + length`) both values leave the resumed scan covering the same region. The suite passes unchanged against the code before and after this one-line edit.
   
   ### Why are the changes needed?
   
   `StringSubstitutor` is on the path of all error-message formatting yet was only tested indirectly. A dedicated suite documents and locks in its behavior, including the less-obvious cases (escapes, defaults, nesting, cycles), and the bookkeeping cleanup removes a confusing dead local.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The production change is behavior-preserving; the rest is test-only.
   
   ### How was this patch tested?
   
   New unit tests. `build/sbt 'common-utils/testOnly *StringSubstitutorSuite'` passes (13 cases).
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 4.8)
   


-- 
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-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

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

   Thanks @MaxGekk, good catches. Addressed them in the latest commit:
   
   - **Custom syntax**: added a test using `prefix = "%{"`, `escape = '%'`, `valueDelimiter = "|"`, including the escaped `%%{x}`.
   - **Mixed unresolved/resolved**: with exceptions disabled, pinned that `"${undefined} ${x}"` -> `"${undefined} X"`, so the scan keeps going past an unresolved placeholder.
   - **Null value**: the only caller, `ErrorClassesJSONReader`, already maps null params to the string `"null"` before building the substitutor, so I defined the same behavior here -- `value.toString` -> `String.valueOf(value)`, which renders null as `"null"` instead of NPEing. I avoided "treat as undefined" since the key is actually present, and that would throw `Undefined variable` by default. Added a test for it.
   - **Placeholders inside a default value**: pinned that they are not expanded -- the first `}` closes the placeholder, so `"${undefined:-${x}}"` -> `"${x}"`.
   
   Also covered `${}` (empty name, no default) and strengthened the cycle-detection check to assert the `Infinite loop in property interpolation of` prefix. The suite is 18 tests now.
   


-- 
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-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

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

   cc @dongjoon-hyun FYI


-- 
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-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

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

   Thanks @MaxGekk! Addressed both points in `33fc38af962`:
   
   1. Reworded the test comment to describe what the test actually verifies (the scan continues past the default-value replacement and still expands the later `${b}`), rather than implying the length adjustment is required — you're right that it passes either way, so the old wording was misleading.
   2. Updated the PR description to document the second production change (`value.toString` -> `String.valueOf(value)`, so a null resolver value renders as `"null"` instead of NPEing) and noted why it isn't user-facing: the sole caller, `ErrorClassesJSONReader`, already maps null message parameters to `"null"` before constructing the substitutor.
   


-- 
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-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang closed pull request #56648: [SPARK-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping
URL: https://github.com/apache/spark/pull/56648


-- 
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-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

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

   Merged into master and branch-4.x. Thanks @dongjoon-hyun @MaxGekk @uros-b 


-- 
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-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

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

   re-trigger the failed tests


-- 
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-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

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


##########
common/utils/src/test/scala/org/apache/spark/StringSubstitutorSuite.scala:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark
+
+import org.scalatest.funsuite.AnyFunSuite // scalastyle:ignore funsuite
+
+class StringSubstitutorSuite extends AnyFunSuite { // scalastyle:ignore funsuite
+
+  test("basic substitution") {
+    val sub = new StringSubstitutor(Map("a" -> "X", "b" -> "Y"))
+    assert(sub.replace("${a}") === "X")
+    assert(sub.replace("pre${a}post") === "preXpost")
+    assert(sub.replace("${a}${b}") === "XY")
+    assert(sub.replace("${a}-${b}") === "X-Y")
+  }
+
+  test("no placeholders, empty and null input") {
+    val sub = new StringSubstitutor(Map("a" -> "X"))
+    assert(sub.replace("hello") === "hello")
+    assert(sub.replace("") === "")
+    assert(sub.replace(null) === null)
+  }
+
+  test("non-string values are rendered with toString") {
+    val sub = new StringSubstitutor(Map("n" -> 42, "b" -> true))
+    assert(sub.replace("${n}/${b}") === "42/true")
+  }
+
+  test("undefined variable throws by default") {
+    val sub = new StringSubstitutor(Map.empty[String, Any])
+    val e = intercept[IllegalArgumentException](sub.replace("${x}"))
+    assert(e.getMessage.contains("x"))
+  }
+
+  test("undefined variable is left as-is when exceptions are disabled") {
+    val sub =
+      new StringSubstitutor(Map.empty[String, Any], enableUndefinedVariableException = false)
+    assert(sub.replace("a${x}b") === "a${x}b")
+  }
+
+  test("default value is used only when the variable is undefined") {
+    assert(new StringSubstitutor(Map.empty[String, Any]).replace("${x:-def}") === "def")
+    assert(new StringSubstitutor(Map("x" -> "X")).replace("${x:-def}") === "X")
+    // Empty variable name: the delimiter is at index 0, so the text after it is the default.
+    assert(new StringSubstitutor(Map.empty[String, Any]).replace("${:-fallback}") === "fallback")
+  }
+
+  test("a default-value substitution is followed by a further substitution") {
+    // After a default-value replacement the buffer shifts, so the scan length must be adjusted
+    // for the later ${b} to still be expanded.

Review Comment:
   This comment overstates the mechanism: the length adjustment isn't required for `${b}` to expand. Tracing `${a:-LONG_DEFAULT}${b}` under the pre-fix code (unadjusted `length`) still yields `LONG_DEFAULTY` — as the PR description itself notes ("the suite passes unchanged against the code before and after this one-line edit"). So this test passes either way and doesn't actually guard the `newLength` change; as written it could mislead a maintainer into thinking removing the adjustment would break it. Suggest describing what the test verifies instead:
   ```suggestion
       // After a default-value replacement the buffer shifts; this checks the scan continues
       // past it and still expands the later ${b}.
   ```



-- 
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-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

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

   Nice addition - the new `StringSubstitutorSuite` significantly improves direct coverage for a parser that sits on the error-message path.
   
   I have a few test-gap suggestions that would make the suite more complete:
   
   1. **Custom syntax knobs are not covered yet**
      `StringSubstitutor` exposes configurable `prefix`, `suffix`, `escape`, and `valueDelimiter`, but all tests use defaults. Could we add at least one test with non-default values (e.g. `%{x}` and a custom delimiter)? This protects constructor-level behavior.
   
   2. **Mixed unresolved/resolved placeholders in one string**
      With `enableUndefinedVariableException = false`, it would be good to pin that scanning continues after an unresolved placeholder:
      - input: `" "`
      - expected: `" X"`  
      This specifically guards scan progression.
   
   3. **Resolver value = `null` behavior**
      Current implementation does `value.toString`; if resolver has `("x" -> null)`, that likely NPEs. A test here would document intended behavior (throw vs treat as undefined), rather than leaving it accidental.
   
   4. **Placeholders inside default values**
      Consider pinning `` semantics. It’s a subtle parser behavior whether `` is expanded in that flow; a dedicated test would avoid ambiguity/regressions.
   
   Optional nice-to-have:
   - `` (empty var name without default),
   - extra escape edge cases (`24578x`, etc.),
   - stronger assertion on cycle-detection message prefix.
   
   Overall direction looks good; these are mainly to lock in less-obvious corners.


-- 
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-57532][CORE][TESTS] Add a test suite for StringSubstitutor and tidy its default-value length bookkeeping [spark]

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

   Any other new suggestions? @dongjoon-hyun @MaxGekk 


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