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


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/ProjectEstimationSuite.scala:
##########
@@ -163,6 +164,54 @@ class ProjectEstimationSuite extends 
StatsEstimationTestBase {
     assert(proj2.stats == expectedStats2)
   }
 
+  test("SPARK-60810: column stats of a CHAR column survive read-side padding") 
{
+    val c = 
DataTypeUtils.toAttribute(CharVarcharUtils.replaceCharVarcharWithStringInSchema(
+      StructType(Seq(StructField("c", CharType(5))))).head)
+    val colStat = ColumnStat(distinctCount = Some(10), nullCount = Some(0),
+      avgLen = Some(5), maxLen = Some(5))
+    val child = StatsTestPlan(
+      outputList = Seq(c),
+      rowCount = 1000,
+      attributeStats = AttributeMap(Seq(c -> colStat)))
+
+    val padded = CharVarcharUtils.addPaddingForScan(c)
+    assert(padded != c, "expected a read-side padding expression for a CHAR 
column")
+    val proj = Project(Seq(Alias(padded, "c")()), child)
+    assert(proj.stats.attributeStats.get(proj.output.head).contains(colStat))
+
+    // Statistics of values shorter than N, e.g. collected with read-side 
padding off: every
+    // non-null padded value has at least N characters.
+    val shortStat = colStat.copy(avgLen = Some(3), maxLen = Some(4))
+    val shortChild = child.copy(attributeStats = AttributeMap(Seq(c -> 
shortStat)))
+    val shortProj = Project(Seq(Alias(padded, "c")()), shortChild)
+    
assert(shortProj.stats.attributeStats.get(shortProj.output.head).contains(colStat))
+    // The same without a distinct count, e.g. from a connector that reports 
only lengths.
+    val noNdvChild = child.copy(
+      attributeStats = AttributeMap(Seq(c -> shortStat.copy(distinctCount = 
None))))
+    val noNdvProj = Project(Seq(Alias(padded, "c")()), noNdvChild)
+    assert(noNdvProj.stats.attributeStats.get(noNdvProj.output.head)
+      .contains(colStat.copy(distinctCount = None)))
+
+    // An all-null column has no values to pad; ANALYZE stores the default 
size of the string
+    // type, 20, as its lengths, which a CHAR(30) floor must not raise.
+    val wide = 
DataTypeUtils.toAttribute(CharVarcharUtils.replaceCharVarcharWithStringInSchema(
+      StructType(Seq(StructField("w", CharType(30))))).head)
+    val nullStat = ColumnStat(distinctCount = Some(0), nullCount = Some(1000),

Review Comment:
   **Nit (P3):** This is the only case with a distinct count of 0, and its null 
count also covers every row, so it passes whether or not `getAliasStats` checks 
`stat.distinctCount.contains(BigInt(0))`: without that check, 
`stat.nullCount.exists(_ >= rowCount)` still exempts it, and every new test 
stays green. The distinct-count check is the only one that applies to an `IS 
NULL` filter pushed below the padding Project. `evaluateNullCheck` sets the 
distinct count to 0 but keeps the null count, which `updateStat` then scales 
with the row count: 200 nulls in 1000 rows come out as 200 rows with a null 
count of 40. If that check regressed, the padding alias over such a filter 
would get its lengths raised to N for a column with no non-null values, and no 
test would notice. A case with a distinct count of 0 and a null count below the 
row count would pin it.
   
   **Verification:**
   - **Regression:** A padding alias over a non-null CHAR(5) column whose 
statistics report avgLen 7 and maxLen 9 keeps 7 and 9.
   - **Regression:** A padding alias over a column whose statistics have a 
distinct count of 0 and a null count below the row count, as an IS NULL filter 
below the padding Project produces, keeps its reported lengths instead of 
raising them to N.



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