Github user viirya commented on a diff in the pull request: https://github.com/apache/spark/pull/18542#discussion_r125802474 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileFormatWriter.scala --- @@ -116,13 +116,26 @@ object FileFormatWriter extends Logging { val partitionSet = AttributeSet(partitionColumns) val dataColumns = allColumns.filterNot(partitionSet.contains) - val bucketIdExpression = bucketSpec.map { spec => + val bucketPartitioning = bucketSpec.map { spec => val bucketColumns = spec.bucketColumnNames.map(c => dataColumns.find(_.name == c).get) + HashPartitioning(bucketColumns, spec.numBuckets) + } + + val bucketIdExpression = bucketPartitioning.map { partitioning => // Use `HashPartitioning.partitionIdExpression` as our bucket id expression, so that we can // guarantee the data distribution is same between shuffle and bucketed data source, which // enables us to only shuffle one side when join a bucketed table and a normal one. - HashPartitioning(bucketColumns, spec.numBuckets).partitionIdExpression + partitioning.partitionIdExpression } + + // If the plan's outputPartitioning is the same as the the bucket spec, then each row will have + // a constant bucket id. We possibly can avoid the sort altogether. + val bucketSortExpression = plan.outputPartitioning match { + case output: HashPartitioning => + bucketPartitioning.filterNot(_.semanticEquals(output)).map(_.partitionIdExpression) --- End diff -- The output partitioning doesn't guarantee that there's only one value for the partitioning columns in the partition. For example, assume the output partitioning is [a], it only guarantees that the rows with the same values of [a] will be in the same partition. So a partition can contains all the rows with `a = 1`, `a = 2`..., rather than all rows in the partition have `a = 1`.
--- 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