shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4650953742


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala:
##########
@@ -504,20 +535,61 @@ object Union {
     Union (left :: right :: Nil)
   }
 
-  // updating nullability to make all the children consistent
-  def mergeChildOutputs(childOutputs: Seq[Seq[Attribute]]): Seq[Attribute] = {
-    childOutputs.transpose.map { attrs =>
+  // updating nullability to make all the children consistent, and the CHAR 
lengths of the raw
+  // types if the children have CHAR columns of different lengths, see
+  // CharVarcharUtils.widerCharRawTypes. ApplyCharTypePadding pads the values 
of the shorter ones.
+  def mergeChildOutputs(childOutputs: Seq[Seq[Attribute]]): Seq[Attribute] =
+    mergeChildOutputs(childOutputs, widenCharLengths = true)
+
+  def mergeChildOutputs(
+      childOutputs: Seq[Seq[Attribute]],
+      widenCharLengths: Boolean): Seq[Attribute] = {
+    val rawTypes = widerCharRawTypes(childOutputs, widenCharLengths)
+    childOutputs.transpose.zipWithIndex.map { case (attrs, i) =>
       val firstAttr = attrs.head
       val nullable = attrs.exists(_.nullable)
       val newDt = attrs.map(_.dataType).reduce(StructType.unionLikeMerge)
-      if (firstAttr.dataType == newDt) {
+      val metadata = 
rawTypes(i).map(CharVarcharUtils.withRawType(firstAttr.metadata, _))
+        .getOrElse(firstAttr.metadata)
+      if (firstAttr.dataType == newDt && metadata == firstAttr.metadata) {
         firstAttr.withNullability(nullable)
       } else {
-        AttributeReference(firstAttr.name, newDt, nullable, 
firstAttr.metadata)(
+        AttributeReference(firstAttr.name, newDt, nullable, metadata)(
           firstAttr.exprId, firstAttr.qualifier)
       }
     }
   }
+
+  private[sql] def widerCharRawTypes(
+      childOutputs: Seq[Seq[Attribute]],
+      widenCharLengths: Boolean): Seq[Option[DataType]] = {
+    if (widenCharLengths) {
+      CharVarcharUtils.widerCharRawTypes(childOutputs)
+    } else {
+      Seq.fill(childOutputs.head.length)(None)
+    }
+  }
+
+  private[logical] def withWiderCharRawType(
+      attr: Attribute,
+      rawType: Option[DataType]): Attribute = {
+    rawType.map(CharVarcharUtils.withRawType(attr.metadata, _))
+      .filter(_ != attr.metadata)
+      .map(attr.withMetadata)
+      .getOrElse(attr)
+  }
+
+  /**
+   * Whether the plan reads the recursive reference of a recursive CTE that it 
is not part of, as a
+   * set operation in the recursive term can. The CHAR lengths of such a set 
operation are not
+   * widened, as those of the [[UnionLoop]] are not: they must stay the 
lengths of the reference.
+   */
+  def readsRecursiveReference(plan: LogicalPlan): Boolean = plan match {
+    case _: UnionLoop => false
+    case r: CTERelationRef => r.recursive

Review Comment:
   Thanks, confirmed: your query returned no rows. Rather than following every 
path to the recursive reference, `ApplyCharTypePadding` now narrows the values 
of the recursive term of a `UnionLoop` to the anchor's CHAR lengths (the same 
`charTypeNarrowing` the views use). The CTE's columns have those lengths, so 
the values from the recursion now match them, whichever set operation widened 
them. `readsRecursiveReference` is now only needed so that the union of the 
anchor and the recursive term isn't widened before it becomes the `UnionLoop`. 
Your query, with `x` reading `r`, is in the set-operations test and returns n = 
0, 1, 2.



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