spark-test commented on code in PR #60734:
URL: https://github.com/apache/spark/pull/60734#discussion_r4659621564


##########
docs/sql-migration-guide.md:
##########
@@ -33,6 +33,7 @@ license: |
 
 ## Upgrading from Spark SQL 4.3 to 4.4
 
+- Since Spark 4.4, `to_char` and `to_varchar` keep the zero digit when they 
format a zero value with a format string that has no digits after the decimal 
point. For example, `to_char(0, '999')` returns `'  0'`. Previously, the result 
was all spaces, for example `'   '`. Data derived from affected query results 
may need to be recomputed. To restore the previous behavior, set 
`spark.sql.legacy.toChar.formatZeroAsBlank` to `true`.

Review Comment:
   **Nit (P3):** This entry, the `spark.sql.legacy.toChar.formatZeroAsBlank` 
doc (SQLConf.scala:9571-9574) and the new `ToNumberParser` comments 
(ToNumberParser.scala:120-122 and 771-773) all say that, before this change, 
every zero formatted with no digits after the decimal point lost its digit and 
came out as spaces. Reading `ToNumberParser.format`, that holds only for some 
formats:
   
   - For `'999.'` the decimal point already turns the last blank into `0`, and 
for `'099'` a digit run that starts with `0` already pads with zeros, so 
`to_char(0, '999.')` is `'  0 '` and `to_char(0, '099')` is `'000'` with the 
legacy setting on or off.
   - Among the formats that do change, the old result was often not all spaces: 
`'$999'` gave `'$   '`, `'S999'` gave `'   +'` and `'999S'` gave `'+   '`.
   
   A zero result changes exactly when its old form left out the ones digit. The 
query results are right; only these descriptions are off, so a user looking for 
affected stored values, or a maintainer reading the comments, gets the wrong 
picture. One possible rewording of this entry:
   
   ```suggestion
   - Since Spark 4.4, `to_char` and `to_varchar` keep the ones digit when they 
format a zero value with a format string that has no digits after the decimal 
point. For example, `to_char(0, '999')` returns `'  0'` and `to_char(0, 
'999S')` returns `'  0+'`, where previously they returned `'   '` and `'+   '`. 
Formats that already rendered the zero, such as `'999.'` and `'099'`, are 
unchanged. Data derived from affected query results may need to be recomputed. 
To restore the previous behavior, set 
`spark.sql.legacy.toChar.formatZeroAsBlank` to `true`.
   ```
   
   The config doc and the `@param` text could say that `true` restores the 
earlier result, which leaves out the ones digit of a zero for the affected 
formats, and the branch comment could note that for some formats the decimal 
point or the zero padding already renders the zero, so the branch changes 
nothing for them.
   
   **Verification:**
   - **Inspection:** Read the reworded migration-guide entry, config doc, 
parameter doc and branch comment and confirm that each limits the change to 
zero values whose earlier result left out the ones digit, that none of them 
says every format without fractional digits was affected or that the earlier 
result was always all spaces, and that any rule a text gives for which formats 
are affected (the last integer digit run starts with 9, and either no decimal 
point follows the digits or a leading S comes right before integer digits whose 
runs all start with 9) also leaves out formats such as 'S0,999.', whose earlier 
result already showed the zero.
   - **Inspection:** Confirm that every example result the reworded texts quote 
matches what to_char returns for that input with the rollback setting off and 
on: for example, '999' gives two spaces followed by 0 with the setting off and 
three spaces with it on, '$999' gives $, two spaces and 0, or $ and three 
spaces, and '999.' gives two spaces, 0 and a trailing space either way.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala:
##########
@@ -2327,6 +2327,41 @@ class StringExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper {
     }
   }
 
+  test("ToCharacter: zero value with no digits after the decimal point") {
+    def toChar(decimal: Decimal, format: String): Expression = {
+      val expr = ToCharacter(Literal(decimal), Literal(format))
+      assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+      expr
+    }
+    Seq(
+      (Decimal(0), "9") -> "0",
+      (Decimal(0), "999") -> "  0",
+      (Decimal("0.00"), "999") -> "  0",
+      (Decimal(0), "9G999") -> "    0",
+      (Decimal(0), "$999") -> "$  0",
+      (Decimal(0), "999S") -> "  0+",
+      (Decimal(0), "S999") -> "  +0",
+      (Decimal(0), "999MI") -> "  0 ",
+      (Decimal(0), "999PR") -> "  0  ",
+      (Decimal(0), "099") -> "000",
+      (Decimal(0), "999.9") -> "  0.0",
+      (Decimal(0), "999.") -> "  0 "
+    ).foreach { case ((decimal, format), expected) =>
+      checkEvaluation(toChar(decimal, format), expected)
+    }
+    val legacyExpr = 
withSQLConf(SQLConf.LEGACY_TO_CHAR_FORMAT_ZERO_AS_BLANK.key -> "true") {
+      checkEvaluation(toChar(Decimal(0), "9"), " ")
+      checkEvaluation(toChar(Decimal(0), "999"), "   ")
+      toChar(Decimal(0), "999")
+    }
+    // The config value is captured when the expression is built, not when it 
is evaluated.
+    checkEvaluation(legacyExpr, "   ")
+    checkEvaluation(legacyExpr.withNewChildren(legacyExpr.children), "   ")

Review Comment:
   **Nit (P3):** `withNewChildren` returns the same instance when every new 
child equals the current one (TreeNode.scala:425-431), so this call hands back 
`legacyExpr` itself and the line repeats the check above it. It can fail only 
when that check fails, and it never rebuilds the node through 
`withNewChildrenInternal`. Today copies are covered only indirectly: 
`ToCharacter` is stateful, so the projections inside `checkEvaluation` take 
fresh copies. If that ever changed, a `withNewChildrenInternal` that re-read 
the session value would go unnoticed. Passing a child that differs, such as the 
same zero as a wider decimal literal, makes the call rebuild the node:
   
   ```suggestion
       val rebuilt = legacyExpr.withNewChildren(
         Seq(Literal(Decimal(0), DecimalType(10, 0)), legacyExpr.children(1)))
       assert(rebuilt ne legacyExpr)
       checkEvaluation(rebuilt, "   ")
   ```
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/60734#discussion_r4637129278).
   
   **Verification:**
   - **Behavior:** Run the edited test and confirm that the rebuilt expression 
is a new instance and still formats zero with the format '999' as three spaces 
after the legacy setting has returned to its default.
   - **Regression:** Temporarily make ToCharacter.withNewChildrenInternal build 
the node with the two-argument constructor, which re-reads the session value, 
and remove the stateful override so that evaluation no longer rebuilds the 
expression through withNewChildrenInternal (the serialization round trip in 
checkEvaluation still copies it and keeps the captured value); confirm that the 
edited check fails while the check at StringExpressionsSuite.scala:2358 still 
passes, then restore both.



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

Reply via email to