LuciferYang opened a new pull request, #60616:
URL: https://github.com/apache/spark/pull/60616
### What changes were proposed in this pull request?
This is the follow-up that SPARK-60810 left open. Under standard
CHAR/VARCHAR semantics, scans wrap CHAR and VARCHAR columns in
`charTypeReadSideCheck` / `varcharTypeReadSideCheck` instead of
`readSidePadding`. When CHAR and VARCHAR are first-class types (standard
semantics or `spark.sql.preserveCharVarcharTypeInfo`), a comparison with a
STRING also casts the column to STRING. The estimators matched neither, so
these columns lost their statistics.
- A new `CharVarcharUtils.ReadSideCheck` extractor matches the two checks.
`ReadSidePadding` is unchanged, so `V2ExpressionBuilder` still never compiles a
check as `CHAR_READ_SIDE_PADDING`.
- `getAliasStats` passes the column's stats through a check, with
SPARK-60810's length floor for CHAR only, since the VARCHAR check never makes a
value longer.
- `EstimationUtils.stripCastToPlainString` strips a cast to a STRING of the
same collation, which leaves the value unchanged. `FilterEstimation` applies it
to predicates and `JoinEstimation` to join keys, also when checking a side for
uniqueness.
### Why are the changes needed?
With CBO on, after `ANALYZE` on a `CHAR(5)` or `VARCHAR(5)` column with 10
distinct values in 1000 rows, under standard semantics:
| query | before | after | same data as `STRING` |
|---|---|---|---|
| `GROUP BY c` | no row count | 10 | 10 |
| `WHERE c = '3'` | 1000 | 100 | 100 |
| `WHERE c IN ('3', '4')` | 1000 | 200 | 200 |
| `WHERE c IS NULL` | 1000 | 0 | 0 |
| join with a `STRING` column of the same data | 1000000 | 100000 | 100000 |
Under `preserveCharVarcharTypeInfo` alone, `=` and `IN` were also estimated
as 1000 rows and the join as 1000000. `ANALYZE` reads through the same checks
(it fails on an over-long value under standard semantics), so the stats
describe the values the predicates compare.
### Does this PR introduce _any_ user-facing change?
Query results are unchanged. With CBO on and either conf enabled, plans over
analyzed CHAR and VARCHAR columns can change, and the 4.4 migration guide entry
from SPARK-60810 now covers these modes. Equi-joins between CHAR or VARCHAR
keys of different types, such as `VARCHAR(5)` and `VARCHAR(10)`, keep their
current estimate, as the entry says.
### How was this patch tested?
New tests, each of which fails without the change:
- `ProjectEstimationSuite`, `FilterEstimationSuite`, `JoinEstimationSuite`:
stats through both checks (length floor for CHAR only, none for an all-null
column), each predicate form under the cast, casts that are not stripped
(another collation, a length-checked type, a non-string column), and join keys
cast to STRING, including a side unique on its key.
- `StatisticsCollectionSuite`, end to end under both confs for CHAR(5) and
VARCHAR(5): `GROUP BY`, `=`, `IN`, `IS NULL`, a join with a STRING column, and
partition pruning.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
--
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]