Github user cloud-fan commented on a diff in the pull request: https://github.com/apache/spark/pull/16593#discussion_r97017743 --- Diff: sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala --- @@ -45,6 +46,18 @@ case class CreateHiveTableAsSelectCommand( override def innerChildren: Seq[LogicalPlan] = Seq(query) override def run(sparkSession: SparkSession): Seq[Row] = { + // when create a partitioned table, we should reorder the columns + // to put the partition columns at the end + val partitionAttrs = tableDesc.partitionColumnNames.map { p => + query.output.find(_.name == p).getOrElse( + new AnalysisException(s"Partition column[$p] does not exist " + + s"in query output partition").asInstanceOf[NamedExpression] + ) + } + val partitionSet = AttributeSet(partitionAttrs) + val dataAttrs = query.output.filterNot(partitionSet.contains) + val reorderedOutputQuery = Project(dataAttrs ++ partitionAttrs, query) --- End diff -- we can revert this after https://github.com/apache/spark/pull/16655
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org