ulysses-you commented on a change in pull request #25464: [SPARK-28746][SQL] 
Add partitionby hint  for sql queries
URL: https://github.com/apache/spark/pull/25464#discussion_r322550420
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveHints.scala
 ##########
 @@ -137,28 +137,83 @@ object ResolveHints {
   }
 
   /**
-   * COALESCE Hint accepts name "COALESCE" and "REPARTITION".
-   * Its parameter includes a partition number.
+   * COALESCE Hint accepts name "COALESCE" and "REPARTITION" and 
"REPARTITIONBYRANGE".
+   * Its parameter includes a partition number and columns.
    */
   object ResolveCoalesceHints extends Rule[LogicalPlan] {
-    private val COALESCE_HINT_NAMES = Set("COALESCE", "REPARTITION")
+    val COALESCE_HINT_NAMES: Set[String] = Set("COALESCE", "REPARTITION", 
"REPARTITIONBYRANGE")
+
+    /**
+     * same with repartition api
+     */
+    private def createRepartition(
+        shuffle: Boolean, h: UnresolvedHint): LogicalPlan = {
+      def createRepartitionByExpression(
+           numPartitions: Int, exprs: Seq[Any], h: UnresolvedHint): 
RepartitionByExpression = {
+        val errExprs = exprs.filter(!_.isInstanceOf[UnresolvedAttribute])
+        if (errExprs.nonEmpty) throw new AnalysisException(
+          s"Repartition hint parameter should be columns but was $errExprs")
+        RepartitionByExpression(
+          exprs.map(_.asInstanceOf[UnresolvedAttribute]), h.child, 
numPartitions)
+      }
+
+      h.parameters match {
+        case Seq(IntegerLiteral(numPartitions)) =>
+          Repartition(numPartitions, shuffle, h.child)
+        case Seq(numPartitions: Int) =>
+          Repartition(numPartitions, shuffle, h.child)
+
+        case param @ Seq(IntegerLiteral(numPartitions), _*) if shuffle =>
+          createRepartitionByExpression(numPartitions, param.tail, h)
+        case param @ Seq(numPartitions: Int, _*) if shuffle =>
+          createRepartitionByExpression(numPartitions, param.tail, h)
+
+        case _ =>
+          throw new AnalysisException(s"${h.name} hint expects a partition 
number " +
+            "and columns")
+      }
+    }
+
+    /**
+     * same with repartitionByRange api
+     */
+    private def createRepartitionByRange(
+        h: UnresolvedHint): RepartitionByExpression = {
+      def createRepartitionByExpression(
+           numPartitions: Int, exprs: Seq[Any], h: UnresolvedHint): 
RepartitionByExpression = {
+        val sortOrder: Seq[SortOrder] = exprs.map {
+          case expr: SortOrder => expr
+          case expr: Expression => SortOrder(expr, Ascending)
+          case _ => throw new AnalysisException(
+            s"RepartitionByRange hint parameter should be columns but was 
$exprs")
+        }
+        RepartitionByExpression(sortOrder, h.child, numPartitions)
+      }
+
+      h.parameters match {
+        case param @ Seq(IntegerLiteral(numPartitions), _*) =>
+          createRepartitionByExpression(numPartitions, param.tail, h)
+        case param @ Seq(numPartitions: Int, _*) =>
+          createRepartitionByExpression(numPartitions, param.tail, h)
+
 
 Review comment:
   Yeah, we can add `SQLConf` to this class. So does 
`repartition(partitionExprs: Column*)`.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to