You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/11/02 06:24:50 UTC

[GitHub] [spark] zhengruifeng commented on a diff in pull request #38475: [SPARK-40992][CONNECT] Support toDF(columnNames) in Connect DSL

zhengruifeng commented on code in PR #38475:
URL: https://github.com/apache/spark/pull/38475#discussion_r1011238884


##########
connector/connect/src/main/scala/org/apache/spark/sql/connect/planner/SparkConnectPlanner.scala:
##########
@@ -123,6 +125,24 @@ class SparkConnectPlanner(plan: proto.Relation, session: SparkSession) {
     logical.Range(start, end, step, numPartitions)
   }
 
+  private def transformRenameColumns(rel: proto.RenameColumns): LogicalPlan = {
+    val input = new QueryExecution(session, transformRelation(rel.getInput)).analyzed
+    if (input.schema.size != rel.getColumnNamesCount) {
+      throw InvalidPlanInput(
+        "The number of columns doesn't match.\n" +
+          s"Old column names (${input.schema.size}): "
+          + input.schema.fields.map(_.name).mkString(", ") + "\n" +
+          s"New column names (${rel.getColumnNamesCount}): "
+          + rel.getColumnNamesList.asScala.mkString(", "))
+    }
+
+    val newCols =
+      input.output.zip(rel.getColumnNamesList.asScala).map { case (oldAttribute, newName) =>
+        Alias(oldAttribute, newName)()
+      }
+    logical.Project(newCols, input)

Review Comment:
   i guess this can be simpler:
   ```
   Dataset.ofRows(session, transformRelation(rel.getInput)).toDF(rel.getColumnNamesList.asScala).logicalPlan
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org