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

    https://github.com/apache/spark/pull/10498#discussion_r48818640
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/WriterContainer.scala
 ---
    @@ -312,14 +318,35 @@ private[sql] class DynamicPartitionWriterContainer(
         isAppend: Boolean)
       extends BaseWriterContainer(relation, job, isAppend) {
     
    +  private def bucketColumns = 
bucketSpec.get.resolvedBucketingColumns(inputSchema)
    +  private def sortColumns = 
bucketSpec.get.resolvedSortingColumns(inputSchema)
    +  private def numBuckets = bucketSpec.get.numBuckets
    +  private def bucketIdExpr = Pmod(new Murmur3Hash(bucketColumns), 
Literal(numBuckets))
    +
       def writeRows(taskContext: TaskContext, iterator: 
Iterator[InternalRow]): Unit = {
         val outputWriters = new java.util.HashMap[InternalRow, OutputWriter]
         executorSideSetup(taskContext)
     
         var outputWritersCleared = false
     
    -    // Returns the partition key given an input row
    -    val getPartitionKey = UnsafeProjection.create(partitionColumns, 
inputSchema)
    +    val getKey = if (bucketSpec.isEmpty) {
    +      UnsafeProjection.create(partitionColumns, inputSchema)
    +    } else { // If it's bucketed, we should also consider bucket id as 
part of the key.
    +      UnsafeProjection.create(partitionColumns :+ bucketIdExpr, 
inputSchema)
    +    }
    +
    +    val keySchema = if (bucketSpec.isEmpty) {
    +      StructType.fromAttributes(partitionColumns)
    +    } else { // If it's bucketed, we should also consider bucket id as 
part of the key.
    +      StructType.fromAttributes(partitionColumns).add("bucketId", 
IntegerType, nullable = false)
    +    }
    +
    +    def getBucketId(key: InternalRow): Option[Int] = if 
(bucketSpec.isDefined) {
    --- End diff --
    
    put the entire statement in one line, eg
    ```
    def getBucketId(key: InternalRow): Option[Int] = 
      if (bucketSpec.isDefined) Some(key.getInt(partitionColumns.length)) else 
None
    ```



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