Github user yhuai commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5526#discussion_r29814337
  
    --- 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)
    --- End diff --
    
    I guess we also need to do type conversion based on the value of 
`needConversion` of the `BaseRelation` before we pass it to the writer? Like 
what we do in `createPhysicalRDD` (inside `DataSourceStrategy`).


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

Reply via email to