shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4652689736
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala:
##########
@@ -409,23 +437,214 @@ object CharVarcharUtils extends Logging with
SparkCharVarcharUtils {
* attributes. When comparing two char type columns/fields, we need to pad
the shorter one to
* the longer length.
*/
- def addPaddingInStringComparison(attrs: Seq[Attribute], alwaysPad: Boolean):
Seq[Expression] = {
- val rawTypes = attrs.map(attr => getRawType(attr.metadata))
+ def addPaddingInStringComparison(attrs: Seq[Attribute], alwaysPad: Boolean):
Seq[Expression] =
+ addPaddingInStringComparison(attrs, attrs.map(attr =>
getRawType(attr.metadata)), alwaysPad)
+
+ /**
+ * Like the method above, for expressions with the given raw types, such as
a scalar subquery or
+ * a column of an IN subquery, whose raw type is the one of their column.
With `truncate = false`,
+ * it pads like read-side padding, which keeps longer values instead of
truncating them.
+ */
+ def addPaddingInStringComparison(
+ exprs: Seq[Expression],
+ rawTypes: Seq[Option[DataType]],
+ alwaysPad: Boolean,
+ truncate: Boolean = true): Seq[Expression] = {
if (rawTypes.exists(_.isEmpty)) {
- attrs
- } else if (attrs.exists(attr => !RowOrdering.isOrderable(attr.dataType))) {
+ exprs
+ } else if (exprs.exists(expr => !RowOrdering.isOrderable(expr.dataType))) {
// Comparing a non-orderable type, such as a struct holding a MAP, is
rejected later by
// CheckAnalysis. Leave it alone so the error names the attribute the
user wrote rather
// than the rewritten struct.
- attrs
+ exprs
} else {
val typeWithTargetCharLength =
rawTypes.map(_.get).reduce(typeWithWiderCharLength)
- attrs.zip(rawTypes.map(_.get)).map { case (attr, rawType) =>
- padCharToTargetLength(attr, rawType, typeWithTargetCharLength,
alwaysPad).getOrElse(attr)
+ exprs.zip(rawTypes.map(_.get)).map { case (expr, rawType) =>
+ padCharToTargetLength(expr, rawType, typeWithTargetCharLength,
alwaysPad, truncate)
+ .getOrElse(expr)
+ }
+ }
+ }
+
+ /**
+ * The raw type of the output of a UNION at one position, given the outputs
of its children
+ * there, if they are CHAR types, or structs and arrays of them, with
different lengths at the
+ * same places: the widest lengths, as the SQL standard gives the union of
CHAR(n) and CHAR(m)
+ * the length max(n, m). Keeps the names of the first attribute's fields.
Returns None if the
+ * raw types are the same, or not all CHAR types of the same shape.
+ */
+ def widerCharRawType(attrs: Seq[Attribute]): Option[DataType] = {
+ val rawTypeStrings = attrs.map(attr => getRawTypeString(attr.metadata))
+ if (rawTypeStrings.exists(_.isEmpty) || rawTypeStrings.distinct.length ==
1) {
Review Comment:
Thanks, confirmed. The widening cast now keeps the CHAR types it doesn't
change: `WidenSetOperationTypes.widenTypes` gives its `Alias(Cast(...))` the
raw type of the cast value (`CharVarcharUtils.metadataOfCast`), and so do its
`UnionLoop` case and the single-pass resolver's `coerceChildOutputs`, so the
two analyzers agree. In your example, the INT-to-BIGINT cast of the union's
struct keeps `c` as CHAR(5), and the outer INTERSECT pads tb's `c` to it. The
set-operations test has your query: INTERSECT returns the row with 'a ', and
EXCEPT is empty. A cast that keeps no CHAR or VARCHAR type gets no metadata, as
before.
--
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]