dongjoon-hyun commented on code in PR #60616:
URL: https://github.com/apache/spark/pull/60616#discussion_r4635821032
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/EstimationUtils.scala:
##########
@@ -86,31 +86,21 @@ object EstimationUtils {
attributeStats: AttributeMap[ColumnStat],
rowCount: BigInt): Seq[(Attribute, ColumnStat)] = {
expressions.collect {
- case alias @ Alias(attr: Attribute, _) if attributeStats.contains(attr)
=>
+ // A cast to a STRING of the same collation keeps the value and the
column's statistics,
+ // e.g. the cast that a union with a STRING adds to a first-class
VARCHAR column.
+ case alias @ Alias(WithoutCastToPlainString(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 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.
- // TODO(SPARK-60830): under
spark.sql.charVarchar.standardSemantics.enabled the scan wraps
- // CHAR and VARCHAR columns in charTypeReadSideCheck and
varcharTypeReadSideCheck instead,
- // so they still lose their statistics here and in
FilterEstimation.calculateSingleCondition.
- // They need an extractor of their own, because V2ExpressionBuilder
relies on
- // ReadSidePadding never matching the checks. Under first-class CHAR
types the filter unwrap
- // also has to see through the cast to STRING.
- case alias @ Alias(CharVarcharUtils.ReadSidePadding(attr: Attribute, n),
_)
+ // A scan wraps each CHAR column in read-side padding, or under standard
CHAR/VARCHAR
+ // semantics each CHAR and VARCHAR column in a read-side check. ANALYZE
TABLE reads the table
+ // through them as well, so the statistics already describe the values
they return.
+ // Statistics collected otherwise (with padding off, or reported by a
connector) can describe
+ // shorter values. The VARCHAR check never makes a value longer.
Review Comment:
Minor: `varcharTypeReadSideCheck` also trims trailing spaces beyond N
(`trimTrailingSpaces` in `CharVarcharCodegenUtils`), so stored values that
differ only in them, e.g. `'abcde'` and `'abcde '` in a `VARCHAR(5)` column,
are read as one value. ANALYZE TABLE reads through the check, so its statistics
are fine, but with statistics collected otherwise (reported by a connector, or
a table written by another engine), the distinct count kept here is too high.
For example, if those are the only two values, `WHERE v = 'abcde'` is estimated
at half of the rows while every row matches.
This is the same trade-off that `padToCharLength` documents for the padding,
but this sentence covers only the lengths. Could the comment also say that the
distinct count is kept as is under the checks?
`FilterEstimation.unwrapCharVarcharRead` (FilterEstimation.scala:263) takes the
same statistics.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DistinctKeyVisitor.scala:
##########
@@ -20,12 +20,21 @@ package org.apache.spark.sql.catalyst.plans.logical
import org.apache.spark.sql.catalyst.expressions.{Alias, Expression,
ExpressionSet, NamedExpression}
import org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys
import org.apache.spark.sql.catalyst.plans.{Inner, LeftOuter, LeftSemiOrAnti,
RightOuter}
+import org.apache.spark.sql.catalyst.util.CharVarcharUtils
/**
* A visitor pattern for traversing a [[LogicalPlan]] tree and propagate the
distinct attributes.
*/
object DistinctKeyVisitor extends LogicalPlanVisitor[Set[ExpressionSet]] {
+ /**
+ * The join keys that a side's distinct keys are compared with to decide
whether the side is
+ * unique on them. A key cast to a STRING of its own collation, as a
comparison of a CHAR or
+ * VARCHAR column with a STRING adds, is unique where the column is.
+ */
+ def joinKeySet(keys: Seq[Expression]): ExpressionSet =
Review Comment:
Uniqueness through the cast is handled here, where the join keys are
compared, but not where the distinct keys go through a projection.
`projectDistinctKeys` maps a key only to an alias of the same expression (L43),
and `SimplifyCasts` keeps the cast, so `Alias(Cast(c, StringType), k)` does not
carry `{c}` over to `{k}`.
For example, `SELECT s FROM u LEFT JOIN (SELECT CAST(c AS STRING) AS k FROM
t GROUP BY c) g ON s = g.k` keeps the join, while the same query with `SELECT
c` in the subquery and `ON s = CAST(g.c AS STRING)` has it removed. The new
migration guide entry says such a side "is also taken as unique on the column
cast to a `STRING`", which reads as covering the first query too.
`RemoveRedundantAggregates` over such a projection misses it the same way.
Could `projectDistinctKeys` also map a key to an alias of its cast to a
plain STRING, e.g. by keying the aliases on
`CharVarcharUtils.stripCastToPlainString(alias.child)`? Or is it left for a
follow-up, with the migration guide narrowed to the cast in a join key?
--
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]