LuciferYang commented on code in PR #60025:
URL: https://github.com/apache/spark/pull/60025#discussion_r4521870405
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/FilterEstimation.scala:
##########
@@ -366,7 +373,8 @@ case class FilterEstimation(
if (!valueInRange) return Some(0.0)
- val percent = if (colStat.histogram.isDefined) {
+ val useHistogram = colStat.histogram.isDefined &&
canEstimateWithHistogram(attr.dataType)
Review Comment:
I narrowed the description to STRING and BINARY columns. Numeric columns
keep their current estimate in this PR.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/EstimationUtils.scala:
##########
@@ -128,6 +149,16 @@ object EstimationUtils {
if (outputRowCount > 0) outputRowCount * getSizePerRow(attributes,
attrStats) else 1
}
+ /**
+ * Whether the histogram estimation paths apply to a column of this type.
String and binary
+ * columns have no numeric bounds, which those paths need. ANALYZE TABLE
builds no histogram for
+ * them, but a DSv2 connector can report one for any type.
+ */
+ def canEstimateWithHistogram(dataType: DataType): Boolean = dataType match {
Review Comment:
Done in 78f799492c4: an allowlist of the types that `toDouble` converts.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/EstimationUtils.scala:
##########
@@ -87,6 +88,26 @@ object EstimationUtils {
expressions.collect {
case alias @ Alias(attr: Attribute, _) if attributeStats.contains(attr)
=>
alias.toAttribute -> attributeStats(attr)
+ // Read-side CHAR padding wraps each CHAR column of a scan. With padding
on, ANALYZE TABLE
+ // reads the table through it as well, so the statistics already
describe padded values.
+ // Statistics collected otherwise (with padding off, or reported by a
connector) can
+ // describe shorter values, and every non-null padded value has at least
N characters.
+ // A distinct count of 0 or a null count covering every row means no
non-null values, so
+ // there is nothing to pad (ANALYZE then stores the type's default size
as the lengths).
+ // The distinct count is left as is.
+ // It is too high only when stored values differ just in trailing
spaces, which the
+ // padding makes equal.
+ case alias @ Alias(CharVarcharUtils.ReadSidePadding(attr: Attribute,
Literal(n: Int, _)), _)
+ if attributeStats.contains(attr) =>
+ val stat = attributeStats(attr)
+ val noValues =
+ stat.distinctCount.contains(BigInt(0)) || stat.nullCount.exists(_ >=
rowCount)
Review Comment:
Agreed, done in 78f799492c4: the floor is skipped only for a distinct count
of 0. A column reported with only a null count covering every row gets the
floor again, which overestimates its size. `ProjectEstimationSuite` covers your
example: a null count scaled to cover the single remaining row still gets the
floor.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/JoinEstimation.scala:
##########
@@ -201,7 +201,8 @@ case class JoinEstimation(join: Join) extends Logging {
if (ValueInterval.isIntersected(lInterval, rInterval)) {
val (newMin, newMax) = ValueInterval.intersect(lInterval, rInterval,
leftKey.dataType)
val (card, joinStat) = (leftKeyStat.histogram, rightKeyStat.histogram)
match {
- case (Some(l: Histogram), Some(r: Histogram)) =>
+ case (Some(l: Histogram), Some(r: Histogram))
+ if canEstimateWithHistogram(leftKey.dataType) =>
Review Comment:
I'd like to keep the checks in the two estimators for this PR and move the
histogram handling to the entry points in a follow-up. With the allowlist from
your comment on `canEstimateWithHistogram`, dropping histograms in
`transformV2Stats` and `CatalogColumnStat.toPlanStat` would also drop the ones
`ANALYZE` builds for TimestampNTZ columns. From reading the code those columns
already look like they fail in `ValueInterval.apply` with CBO on, so I'd rather
look at that separately.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/EstimationUtils.scala:
##########
@@ -87,6 +88,26 @@ object EstimationUtils {
expressions.collect {
case alias @ Alias(attr: Attribute, _) if attributeStats.contains(attr)
=>
alias.toAttribute -> attributeStats(attr)
+ // Read-side CHAR padding wraps each CHAR column of a scan. With padding
on, ANALYZE TABLE
+ // reads the table through it as well, so the statistics already
describe padded values.
+ // Statistics collected otherwise (with padding off, or reported by a
connector) can
+ // describe shorter values, and every non-null padded value has at least
N characters.
+ // A distinct count of 0 or a null count covering every row means no
non-null values, so
+ // there is nothing to pad (ANALYZE then stores the type's default size
as the lengths).
+ // The distinct count is left as is.
+ // It is too high only when stored values differ just in trailing
spaces, which the
+ // padding makes equal.
+ case alias @ Alias(CharVarcharUtils.ReadSidePadding(attr: Attribute,
Literal(n: Int, _)), _)
Review Comment:
Agreed. I'll look at a dedicated expression or a marker trait in SPARK-60830
instead of adding more name-based matches.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala:
##########
@@ -391,6 +391,22 @@ object CharVarcharUtils extends Logging with
SparkCharVarcharUtils {
}.getOrElse(attr)
}
+ /**
+ * Matches the read-side CHAR padding that [[addPaddingForScan]] wraps a
column in (when
+ * standard semantics are off) and extracts the padded expression and the
length argument.
+ */
+ private[sql] object ReadSidePadding {
+ def unapply(e: Expression): Option[(Expression, Expression)] = e match {
Review Comment:
Done in 78f799492c4: `ReadSidePadding` returns the `Int` length and matches
only an `Int` literal.
--
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]