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

    https://github.com/apache/spark/pull/2226#discussion_r17276837
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/InsertIntoHiveTable.scala
 ---
    @@ -101,62 +103,135 @@ case class InsertIntoHiveTable(
       }
     
       def saveAsHiveFile(
    -      rdd: RDD[Writable],
    +      rdd: RDD[(Writable, String)],
           valueClass: Class[_],
           fileSinkConf: FileSinkDesc,
    -      conf: JobConf,
    -      isCompressed: Boolean) {
    +      conf: SerializableWritable[JobConf],
    +      isCompressed: Boolean,
    +      dynamicPartNum: Int) {
         if (valueClass == null) {
           throw new SparkException("Output value class not set")
         }
    -    conf.setOutputValueClass(valueClass)
    +    conf.value.setOutputValueClass(valueClass)
         if (fileSinkConf.getTableInfo.getOutputFileFormatClassName == null) {
           throw new SparkException("Output format class not set")
         }
         // Doesn't work in Scala 2.9 due to what may be a generics bug
         // TODO: Should we uncomment this for Scala 2.10?
         // conf.setOutputFormat(outputFormatClass)
    -    conf.set("mapred.output.format.class", 
fileSinkConf.getTableInfo.getOutputFileFormatClassName)
    +    conf.value.set("mapred.output.format.class",
    +      fileSinkConf.getTableInfo.getOutputFileFormatClassName)
         if (isCompressed) {
           // Please note that isCompressed, "mapred.output.compress", 
"mapred.output.compression.codec",
           // and "mapred.output.compression.type" have no impact on ORC 
because it uses table properties
           // to store compression information.
    -      conf.set("mapred.output.compress", "true")
    +      conf.value.set("mapred.output.compress", "true")
           fileSinkConf.setCompressed(true)
    -      
fileSinkConf.setCompressCodec(conf.get("mapred.output.compression.codec"))
    -      
fileSinkConf.setCompressType(conf.get("mapred.output.compression.type"))
    +      
fileSinkConf.setCompressCodec(conf.value.get("mapred.output.compression.codec"))
    +      
fileSinkConf.setCompressType(conf.value.get("mapred.output.compression.type"))
         }
    -    conf.setOutputCommitter(classOf[FileOutputCommitter])
    -    FileOutputFormat.setOutputPath(
    -      conf,
    -      SparkHiveHadoopWriter.createPathFromString(fileSinkConf.getDirName, 
conf))
    +    conf.value.setOutputCommitter(classOf[FileOutputCommitter])
     
    +    FileOutputFormat.setOutputPath(
    +      conf.value,
    +      SparkHiveHadoopWriter.createPathFromString(fileSinkConf.getDirName, 
conf.value))
         log.debug("Saving as hadoop file of type " + valueClass.getSimpleName)
    +    var writer: SparkHiveHadoopWriter = null
    +    // Map restore writesr for Dynamic Partition
    +    var writerMap: scala.collection.mutable.HashMap[String, 
SparkHiveHadoopWriter] = null
    +    if (dynamicPartNum == 0) {
    +      writer = new SparkHiveHadoopWriter(conf.value, fileSinkConf)
    +      writer.preSetup()
    +    } else {
    +      writerMap =  new scala.collection.mutable.HashMap[String, 
SparkHiveHadoopWriter]
    +    }
     
    -    val writer = new SparkHiveHadoopWriter(conf, fileSinkConf)
    -    writer.preSetup()
    -
    -    def writeToFile(context: TaskContext, iter: Iterator[Writable]) {
    -      // Hadoop wants a 32-bit task attempt ID, so if ours is bigger than 
Int.MaxValue, roll it
    -      // around by taking a mod. We expect that no task will be attempted 
2 billion times.
    -      val attemptNumber = (context.attemptId % Int.MaxValue).toInt
    -
    +    def writeToFile(context: TaskContext, iter: Iterator[(Writable, 
String)]) {
    +    // Hadoop wants a 32-bit task attempt ID, so if ours is bigger than 
Int.MaxValue, roll it
    +    // around by taking a mod. We expect that no task will be attempted 2 
billion times.
    +    val attemptNumber = (context.attemptId % Int.MaxValue).toInt
    +    // writer for No Dynamic Partition
    +    if (dynamicPartNum == 0) {
           writer.setup(context.stageId, context.partitionId, attemptNumber)
           writer.open()
    +    }
     
    -      var count = 0
    -      while(iter.hasNext) {
    -        val record = iter.next()
    -        count += 1
    -        writer.write(record)
    +    var count = 0
    +    // writer for Dynamic Partition
    +    var writer2: SparkHiveHadoopWriter = null
    +    while(iter.hasNext) {
    +      val record = iter.next()
    +      count += 1
    +      if (record._2 == null) { // without Dynamic Partition
    +        writer.write(record._1)
    +      } else { // for Dynamic Partition
    +      val location = fileSinkConf.getDirName
    +      val partLocation = location + record._2 // this is why the writer 
can write to different file
    +      writer2 = writerMap.get(record._2) match {
    +        case Some(writer)=> writer
    +        case None => {
    +          val tempWriter = new SparkHiveHadoopWriter(conf.value,
    +            new FileSinkDesc(partLocation, fileSinkConf.getTableInfo, 
false))
    +          tempWriter.setup(context.stageId, context.partitionId, 
attemptNumber)
    +          tempWriter.open(record._2)
    +          writerMap += (record._2 -> tempWriter)
    +          tempWriter
    +        }
    +           }
    +            writer2.write(record._1)
    +          }
    +        }
    +      if (dynamicPartNum == 0) {
    +        writer.close()
    +        writer.commit()
    +      } else {
    +        for ((k,v) <- writerMap) {
    +          v.close()
    +          v.commit()
    +        }
    +      }
           }
     
    -      writer.close()
    -      writer.commit()
    +      sc.sparkContext.runJob(rdd, writeToFile _)
    +    if (dynamicPartNum == 0) {
    +      writer.commitJob()
    +    } else {
    +      for ((k,v) <- writerMap) {
    +        v.commitJob()
    +      }
    +      writerMap.clear()
         }
    +  }
    +  /*
    +  * e.g.
    +  * for sql: Insert.....tablename(part1,part2) select ....val1,val2 from 
...
    +  *     return: /part1=val1/part2=val2
    +  * for sql: Insert.....tablename(part1=val1,part2) select ....,val2 from 
...
    +  *     return: /part2=val2
    +  * for sql: Insert.....tablename(part1=val1,part2,part3) select 
....,val2,val3 from ...
    +  *     return: /part2=val2/part3=val3
    +  */
    +  private def getDynamicPartDir(partCols: Array[String],
    +      row: Row,
    +      dynamicPartNum: Int,
    +      defaultPartName: String): String = {
    +    assert(dynamicPartNum > 0)
    +    partCols
    +      .takeRight(dynamicPartNum)
    +      .zip(row.takeRight(dynamicPartNum))
    +      .map { case (c, v) => s"/$c=${handleNull(v, defaultPartName)}" }
    +      .mkString
    +  }
     
    -    sc.sparkContext.runJob(rdd, writeToFile _)
    -    writer.commitJob()
    + /*
    + * if rowVal is null or "",will return 
HiveConf.get(hive.exec.default.partition.name) with default
    + * */
    --- End diff --
    
    Should be:
    ```scala
    /**
     * Returns `rowVal` as a String. If `rowVal` is null or equal to "", 
returns the default partition name.
     */


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