GideonPotok commented on code in PR #47154:
URL: https://github.com/apache/spark/pull/47154#discussion_r1709917290


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Mode.scala:
##########
@@ -86,6 +88,57 @@ case class Mode(
     buffer
   }
 
+  private def getCollationAwareBuffer(
+      childDataType: DataType,
+      buffer: OpenHashMap[AnyRef, Long]): Iterable[(AnyRef, Long)] = {
+    def getBuffer(groupingFunction: AnyRef => _): Iterable[(AnyRef, Long)] = {
+      buffer.groupMapReduce(t =>
+        groupingFunction(t._1))(x => x)((x, y) => (x._1, x._2 + y._2)).values
+    }
+    def getMyBuffer(
+        childDataType: DataType): Option[AnyRef => _] = {
+      childDataType match {
+        case _ if UnsafeRowUtils.isBinaryStable(child.dataType) => None
+        case _ => Some(f(_, childDataType))
+      }
+    }
+    getMyBuffer(childDataType).map(getBuffer).getOrElse(buffer)
+  }
+
+  private def f(myData: AnyRef, myElementType: DataType): AnyRef = {
+    if (UnsafeRowUtils.isBinaryStable(myElementType)) {
+      // Short-circuit if there is no collation.
+      myData
+    } else if (myElementType.isInstanceOf[StructType]) {
+      recursivelyGetBufferForStructType(
+        myData.asInstanceOf[InternalRow].toSeq(
+          myElementType.asInstanceOf[StructType]).zip(
+          myElementType.asInstanceOf[StructType].fields))
+    } else if (myElementType.isInstanceOf[ArrayType]) {
+      recursivelyGetBufferForArrayType(
+        myElementType.asInstanceOf[ArrayType],
+        myData.asInstanceOf[ArrayData])
+    } else if (myElementType.isInstanceOf[StringType]) {
+      CollationFactory.getCollationKey(
+        myData.asInstanceOf[UTF8String],
+        myElementType.asInstanceOf[StringType].collationId)
+    } else {
+      throw new UnsupportedOperationException(
+        s"Unsupported data type for collation-aware mode: $myElementType")
+    }
+  }
+
+  private def recursivelyGetBufferForStructType(
+      tuples: Seq[(Any, StructField)]): Seq[Any] = {
+   tuples.map(t => f(t._1.asInstanceOf[AnyRef], t._2.dataType))
+  }
+
+  private def recursivelyGetBufferForArrayType(
+      a: ArrayType,
+      data: ArrayData): Seq[Any] = {
+    (0 until data.numElements()).map(i => f(data.get(i, a.elementType), 
a.elementType))
+  }
+

Review Comment:
   As a follow up is best. 



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