Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/5526#discussion_r29814531
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/sources/commands.scala ---
@@ -41,3 +55,359 @@ private[sql] case class InsertIntoDataSource(
Seq.empty[Row]
}
}
+
+private[sql] case class InsertIntoFSBasedRelation(
+ @transient relation: FSBasedRelation,
+ @transient query: LogicalPlan,
+ partitionColumns: Array[String],
+ mode: SaveMode)
+ extends RunnableCommand {
+
+ override def run(sqlContext: SQLContext): Seq[Row] = {
+ require(
+ relation.paths.length == 1,
+ s"Cannot write to multiple destinations:
${relation.paths.mkString(",")}")
+
+ val hadoopConf = sqlContext.sparkContext.hadoopConfiguration
+ val outputPath = new Path(relation.paths.head)
+ val fs = outputPath.getFileSystem(hadoopConf)
+ val qualifiedOutputPath = fs.makeQualified(outputPath)
+
+ val doInsertion = (mode, fs.exists(qualifiedOutputPath)) match {
+ case (SaveMode.ErrorIfExists, true) =>
+ sys.error(s"path $qualifiedOutputPath already exists.")
+ case (SaveMode.Overwrite, true) =>
+ fs.delete(qualifiedOutputPath, true)
+ true
+ case (SaveMode.Append, _) | (SaveMode.Overwrite, _) |
(SaveMode.ErrorIfExists, false) =>
+ true
+ case (SaveMode.Ignore, exists) =>
+ !exists
+ }
+
+ if (doInsertion) {
+ val job = Job.getInstance(hadoopConf)
+ job.setOutputKeyClass(classOf[Void])
+ job.setOutputValueClass(classOf[Row])
+ FileOutputFormat.setOutputPath(job, qualifiedOutputPath)
+
+ val df = sqlContext.createDataFrame(
+ DataFrame(sqlContext, query).queryExecution.toRdd,
+ relation.schema,
+ needsConversion = false)
+
+ if (partitionColumns.isEmpty) {
+ insert(new DefaultWriterContainer(relation, job), df)
+ } else {
+ val writerContainer = new DynamicPartitionWriterContainer(
+ relation, job, partitionColumns, "__HIVE_DEFAULT_PARTITION__")
+ insertWithDynamicPartitions(writerContainer, df, partitionColumns)
+ }
+ }
+
+ Seq.empty[Row]
+ }
+
+ private def insert(writerContainer: BaseWriterContainer, df: DataFrame):
Unit = {
+ try {
+ writerContainer.driverSideSetup()
+
df.sqlContext.sparkContext.runJob(df.queryExecution.executedPlan.execute(),
writeRows _)
+ writerContainer.commitJob()
+ relation.refresh()
+ } catch { case cause: Throwable =>
+ writerContainer.abortJob()
+ throw new SparkException("Job aborted.", cause)
+ }
+
+ def writeRows(taskContext: TaskContext, iterator: Iterator[Row]): Unit
= {
+ writerContainer.executorSideSetup(taskContext)
+
+ try {
+ while (iterator.hasNext) {
+ val row = iterator.next()
+ writerContainer.outputWriterForRow(row).write(row)
+ }
+ writerContainer.commitTask()
+ } catch { case cause: Throwable =>
+ writerContainer.abortTask()
+ throw new SparkException("Task failed while writing rows.", cause)
+ }
+ }
+ }
+
+ private def insertWithDynamicPartitions(
+ writerContainer: BaseWriterContainer,
+ df: DataFrame,
+ partitionColumns: Array[String]): Unit = {
+
+ require(
+ df.schema == relation.schema,
+ s"""DataFrame must have the same schema as the relation to which is
inserted.
+ |DataFrame schema: ${df.schema}
+ |Relation schema: ${relation.schema}
+ """.stripMargin)
+
+ val sqlContext = df.sqlContext
+
+ val (partitionRDD, dataRDD) = {
+ val fieldNames = relation.schema.fieldNames
+ val dataCols = fieldNames.filterNot(partitionColumns.contains)
+ val df = sqlContext.createDataFrame(
+ DataFrame(sqlContext, query).queryExecution.toRdd,
+ relation.schema,
+ needsConversion = false)
+
+ val partitionColumnsInSpec =
relation.partitionSpec.partitionColumns.map(_.name)
+ require(
+ partitionColumnsInSpec.sameElements(partitionColumns),
+ s"""Partition columns mismatch.
+ |Expected: ${partitionColumnsInSpec.mkString(", ")}
+ |Actual: ${partitionColumns.mkString(", ")}
+ """.stripMargin)
+
+ val partitionDF = df.select(partitionColumns.head,
partitionColumns.tail: _*)
+ val dataDF = df.select(dataCols.head, dataCols.tail: _*)
+
+ partitionDF.queryExecution.executedPlan.execute() ->
+ dataDF.queryExecution.executedPlan.execute()
+ }
+
+ try {
+ writerContainer.driverSideSetup()
+ sqlContext.sparkContext.runJob(partitionRDD.zip(dataRDD), writeRows
_)
--- End diff --
Seems we are evaluating `df` twice?
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]