-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GLUTEN-7028][CH] Support write parquet files with bucket
- Loading branch information
Showing
11 changed files
with
165 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...nds-clickhouse/src/main/scala/org/apache/gluten/extension/WriteFilesWithBucketValue.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.gluten.extension | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Alias, BitwiseAnd, HiveHash, Literal, Pmod} | ||
import org.apache.spark.sql.catalyst.plans.physical.HashPartitioning | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.execution.{ProjectExec, SparkPlan} | ||
import org.apache.spark.sql.execution.datasources.{BucketingUtils, WriteFilesExec, WriterBucketSpec} | ||
|
||
/** | ||
* Wrap with bucket value to specify the bucket file name in native write. Native writer will remove | ||
* this value in the final output. | ||
*/ | ||
object WriteFilesWithBucketValue extends Rule[SparkPlan] { | ||
override def apply(plan: SparkPlan): SparkPlan = { | ||
plan.transformDown { | ||
case writeFiles: WriteFilesExec if writeFiles.bucketSpec.isDefined => | ||
val spec = getWriterBucketSpec(writeFiles) | ||
val wrapBucketValue = ProjectExec( | ||
writeFiles.child.output :+ Alias(spec.bucketIdExpression, "__bucket_value__")(), | ||
writeFiles.child) | ||
writeFiles.copy(child = wrapBucketValue) | ||
} | ||
} | ||
|
||
private def getWriterBucketSpec(writeFilesExec: WriteFilesExec): WriterBucketSpec = { | ||
val partitionColumns = writeFilesExec.partitionColumns | ||
val outputColumns = writeFilesExec.child.output | ||
val dataColumns = outputColumns.filterNot(partitionColumns.contains) | ||
val bucketSpec = writeFilesExec.bucketSpec.get | ||
val bucketColumns = bucketSpec.bucketColumnNames.map(c => dataColumns.find(_.name == c).get) | ||
if ( | ||
writeFilesExec.options.getOrElse( | ||
BucketingUtils.optionForHiveCompatibleBucketWrite, | ||
"false") == "true" | ||
) { | ||
val hashId = BitwiseAnd(HiveHash(bucketColumns), Literal(Int.MaxValue)) | ||
val bucketIdExpression = Pmod(hashId, Literal(bucketSpec.numBuckets)) | ||
// The bucket file name prefix is following Hive, Presto and Trino conversion, so this | ||
// makes sure Hive bucketed table written by Spark, can be read by other SQL engines. | ||
// | ||
// Hive: `org.apache.hadoop.hive.ql.exec.Utilities#getBucketIdFromFile()`. | ||
// Trino: `io.trino.plugin.hive.BackgroundHiveSplitLoader#BUCKET_PATTERNS`. | ||
val fileNamePrefix = (bucketId: Int) => f"$bucketId%05d_0_" | ||
WriterBucketSpec(bucketIdExpression, fileNamePrefix) | ||
} else { | ||
// Spark bucketed table: use `HashPartitioning.partitionIdExpression` as 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. | ||
val bucketIdExpression = | ||
HashPartitioning(bucketColumns, bucketSpec.numBuckets).partitionIdExpression | ||
WriterBucketSpec(bucketIdExpression, (_: Int) => "") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters