-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experimental support for BloomFilterAggregate expression in a reduction context [databricks] #8892
Merged
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
780156e
Support BloomFilterMightContain expression
jlowe e9f4529
Fix null scalar handling, add null tests
jlowe 24a1831
scalastyle fixes
jlowe 0c7d711
Fix overrides
jlowe b7b4140
Update to new spark-rapids-jni BloomFilter API
jlowe 5fd7aff
Merge branch 'branch-23.08' into might-contain
jlowe fc5e391
Support BloomFilterAggregate expression in a reduction context
jlowe 26f3f5a
Merge branch 'branch-23.08' into bloom-filter-agg
jlowe 9202839
Fix tests to skip on Databricks and check for specific classes
jlowe 2e5f43f
Reduce test case combinations, focus most tests on CPU/GPU interop
jlowe 25169d3
Merge branch 'branch-23.08' into bloom-filter-agg
jlowe b6d9b69
Disable Bloom filter join expressions by default
jlowe 707da45
Add literal tags for Spark type
jlowe 5c35556
Fix two-column Bloom filter joins
jlowe b4af8ad
Add batch size limit test
jlowe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
122 changes: 122 additions & 0 deletions
122
sql-plugin/src/main/spark330/scala/com/nvidia/spark/rapids/GpuBloomFilterAggregate.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,122 @@ | ||
/* | ||
* Copyright (c) 2023, NVIDIA CORPORATION. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
/*** spark-rapids-shim-json-lines | ||
{"spark": "330"} | ||
{"spark": "330cdh"} | ||
{"spark": "330db"} | ||
{"spark": "331"} | ||
{"spark": "332"} | ||
{"spark": "332db"} | ||
{"spark": "333"} | ||
{"spark": "340"} | ||
{"spark": "341"} | ||
spark-rapids-shim-json-lines ***/ | ||
package com.nvidia.spark.rapids | ||
|
||
import ai.rapids.cudf.{ColumnVector, GroupByAggregation, Scalar} | ||
import com.nvidia.spark.rapids.Arm.closeOnExcept | ||
import com.nvidia.spark.rapids.GpuBloomFilterAggregate.optimalNumOfHashFunctions | ||
import com.nvidia.spark.rapids.jni.BloomFilter | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Expression} | ||
import org.apache.spark.sql.internal.SQLConf.{RUNTIME_BLOOM_FILTER_MAX_NUM_BITS, RUNTIME_BLOOM_FILTER_MAX_NUM_ITEMS} | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.rapids.{CudfAggregate, GpuAggregateFunction} | ||
import org.apache.spark.sql.types.{BinaryType, DataType} | ||
|
||
case class GpuBloomFilterAggregate( | ||
child: Expression, | ||
estimatedNumItemsRequested: Long, | ||
numBitsRequested: Long) extends GpuAggregateFunction { | ||
|
||
override def nullable: Boolean = true | ||
|
||
override def dataType: DataType = BinaryType | ||
|
||
override def prettyName: String = "bloom_filter_agg" | ||
|
||
private val estimatedNumItems: Long = | ||
Math.min(estimatedNumItemsRequested, SQLConf.get.getConf(RUNTIME_BLOOM_FILTER_MAX_NUM_ITEMS)) | ||
|
||
private val numBits: Long = Math.min(numBitsRequested, | ||
SQLConf.get.getConf(RUNTIME_BLOOM_FILTER_MAX_NUM_BITS)) | ||
|
||
private lazy val numHashes: Int = optimalNumOfHashFunctions(estimatedNumItems, numBits) | ||
|
||
override def children: Seq[Expression] = Seq(child) | ||
|
||
override lazy val initialValues: Seq[Expression] = Seq(GpuLiteral(null, BinaryType)) | ||
|
||
override val inputProjection: Seq[Expression] = Seq(child) | ||
|
||
override val updateAggregates: Seq[CudfAggregate] = Seq(GpuBloomFilterUpdate(numHashes, numBits)) | ||
|
||
override val mergeAggregates: Seq[CudfAggregate] = Seq(GpuBloomFilterMerge) | ||
|
||
private lazy val bloomAttr: AttributeReference = AttributeReference("bloomFilter", dataType)() | ||
|
||
override def aggBufferAttributes: Seq[AttributeReference] = Seq(bloomAttr) | ||
|
||
override val evaluateExpression: Expression = bloomAttr | ||
} | ||
|
||
object GpuBloomFilterAggregate { | ||
/** | ||
* From Spark's BloomFilter.optimalNumOfHashFunctions | ||
* | ||
* Computes the optimal k (number of hashes per item inserted in Bloom filter), given the | ||
* expected insertions and total number of bits in the Bloom filter. | ||
* | ||
* See http://en.wikipedia.org/wiki/File:Bloom_filter_fp_probability.svg for the formula. | ||
* | ||
* @param n expected insertions (must be positive) | ||
* @param m total number of bits in Bloom filter (must be positive) | ||
*/ | ||
private def optimalNumOfHashFunctions(n: Long, m: Long): Int = { | ||
// (m / n) * log(2), but avoid truncation due to division! | ||
Math.max(1, Math.round(m.toDouble / n * Math.log(2)).toInt) | ||
} | ||
} | ||
|
||
case class GpuBloomFilterUpdate(numHashes: Int, numBits: Long) extends CudfAggregate { | ||
override val reductionAggregate: ColumnVector => Scalar = (col: ColumnVector) => { | ||
closeOnExcept(BloomFilter.create(numHashes, numBits)) { bloomFilter => | ||
BloomFilter.put(bloomFilter, col) | ||
bloomFilter | ||
} | ||
} | ||
|
||
override lazy val groupByAggregate: GroupByAggregation = | ||
throw new UnsupportedOperationException("group by aggregations are not supported") | ||
|
||
override def dataType: DataType = BinaryType | ||
|
||
override val name: String = "gpu_bloom_filter_update" | ||
} | ||
|
||
object GpuBloomFilterMerge extends CudfAggregate { | ||
override val reductionAggregate: ColumnVector => Scalar = (col: ColumnVector) => { | ||
BloomFilter.merge(col) | ||
} | ||
|
||
override lazy val groupByAggregate: GroupByAggregation = | ||
throw new UnsupportedOperationException("group by aggregations are not supported") | ||
|
||
override def dataType: DataType = BinaryType | ||
|
||
override val name: String = "gpu_bloom_filter_merge" | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Technically Spark is also checking to be sure that estimatedItems and estimatedBits are literals, actually foldable and > 0 and <= the config. So we could mark those as lit too and then the docs just show that we fully support it instead of adding a comment that we do not.