-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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
[SPARK-12818][SQL] Specializes integral and string types for Count-min Sketch #10968
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -24,7 +24,7 @@ import scala.collection.JavaConverters._ | |
import org.apache.spark.annotation.Experimental | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.execution.stat._ | ||
import org.apache.spark.sql.types.{IntegralType, StringType} | ||
import org.apache.spark.sql.types._ | ||
import org.apache.spark.util.sketch.{BloomFilter, CountMinSketch} | ||
|
||
/** | ||
|
@@ -38,6 +38,7 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
|
||
/** | ||
* Calculate the sample covariance of two numerical columns of a DataFrame. | ||
* | ||
* @param col1 the name of the first column | ||
* @param col2 the name of the second column | ||
* @return the covariance of the two columns. | ||
|
@@ -48,7 +49,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* df.stat.cov("rand1", "rand2") | ||
* res1: Double = 0.065... | ||
* }}} | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
def cov(col1: String, col2: String): Double = { | ||
|
@@ -70,7 +70,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* df.stat.corr("rand1", "rand2") | ||
* res1: Double = 0.613... | ||
* }}} | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
def corr(col1: String, col2: String, method: String): Double = { | ||
|
@@ -92,7 +91,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* df.stat.corr("rand1", "rand2", "pearson") | ||
* res1: Double = 0.613... | ||
* }}} | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
def corr(col1: String, col2: String): Double = { | ||
|
@@ -109,7 +107,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* Null elements will be replaced by "null", and back ticks will be dropped from elements if they | ||
* exist. | ||
* | ||
* | ||
* @param col1 The name of the first column. Distinct items will make the first item of | ||
* each row. | ||
* @param col2 The name of the second column. Distinct items will make the column names | ||
|
@@ -129,7 +126,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* | 3| 0| 1| 1| | ||
* +---------+---+---+---+ | ||
* }}} | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
def crosstab(col1: String, col2: String): DataFrame = { | ||
|
@@ -175,7 +171,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* | ... | | ||
* +----------+ | ||
* }}} | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
def freqItems(cols: Array[String], support: Double): DataFrame = { | ||
|
@@ -193,7 +188,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* | ||
* @param cols the names of the columns to search frequent items in. | ||
* @return A Local DataFrame with the Array of frequent items for each column. | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
def freqItems(cols: Array[String]): DataFrame = { | ||
|
@@ -236,7 +230,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* | ... | | ||
* +----------+ | ||
* }}} | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
def freqItems(cols: Seq[String], support: Double): DataFrame = { | ||
|
@@ -254,7 +247,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* | ||
* @param cols the names of the columns to search frequent items in. | ||
* @return A Local DataFrame with the Array of frequent items for each column. | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
def freqItems(cols: Seq[String]): DataFrame = { | ||
|
@@ -263,6 +255,7 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
|
||
/** | ||
* Returns a stratified sample without replacement based on the fraction given on each stratum. | ||
* | ||
* @param col column that defines strata | ||
* @param fractions sampling fraction for each stratum. If a stratum is not specified, we treat | ||
* its fraction as zero. | ||
|
@@ -283,7 +276,6 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
* | 3| 2| | ||
* +---+-----+ | ||
* }}} | ||
* | ||
* @since 1.5.0 | ||
*/ | ||
def sampleBy[T](col: String, fractions: Map[T, Double], seed: Long): DataFrame = { | ||
|
@@ -300,13 +292,13 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
|
||
/** | ||
* Returns a stratified sample without replacement based on the fraction given on each stratum. | ||
* | ||
* @param col column that defines strata | ||
* @param fractions sampling fraction for each stratum. If a stratum is not specified, we treat | ||
* its fraction as zero. | ||
* @param seed random seed | ||
* @tparam T stratum type | ||
* @return a new [[DataFrame]] that represents the stratified sample | ||
* | ||
* @since 1.5.0 | ||
*/ | ||
def sampleBy[T](col: String, fractions: ju.Map[T, jl.Double], seed: Long): DataFrame = { | ||
|
@@ -374,21 +366,27 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
val singleCol = df.select(col) | ||
val colType = singleCol.schema.head.dataType | ||
|
||
require( | ||
colType == StringType || colType.isInstanceOf[IntegralType], | ||
s"Count-min Sketch only supports string type and integral types, " + | ||
s"and does not support type $colType." | ||
) | ||
val updater: (CountMinSketch, InternalRow) => Unit = colType match { | ||
// For string type, we can get bytes of our `UTF8String` directly, and call the `addBinary` | ||
// instead of `addString` to avoid unnecessary conversion. | ||
case StringType => (sketch, row) => sketch.addBinary(row.getUTF8String(0).getBytes) | ||
case ByteType => (sketch, row) => sketch.addLong(row.getByte(0)) | ||
case ShortType => (sketch, row) => sketch.addLong(row.getShort(0)) | ||
case IntegerType => (sketch, row) => sketch.addLong(row.getInt(0)) | ||
case LongType => (sketch, row) => sketch.addLong(row.getLong(0)) | ||
case _ => | ||
throw new IllegalArgumentException( | ||
s"Count-min Sketch only supports string type and integral types, " + | ||
s"and does not support type $colType." | ||
) | ||
} | ||
|
||
singleCol.rdd.aggregate(zero)( | ||
(sketch: CountMinSketch, row: Row) => { | ||
sketch.add(row.get(0)) | ||
singleCol.queryExecution.toRdd.aggregate(zero)( | ||
(sketch: CountMinSketch, row: InternalRow) => { | ||
updater(sketch, row) | ||
sketch | ||
}, | ||
|
||
(sketch1: CountMinSketch, sketch2: CountMinSketch) => { | ||
sketch1.mergeInPlace(sketch2) | ||
} | ||
_ mergeInPlace _ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no infix notation |
||
) | ||
} | ||
|
||
|
@@ -447,19 +445,27 @@ final class DataFrameStatFunctions private[sql](df: DataFrame) { | |
require(colType == StringType || colType.isInstanceOf[IntegralType], | ||
s"Bloom filter only supports string type and integral types, but got $colType.") | ||
|
||
val seqOp: (BloomFilter, InternalRow) => BloomFilter = if (colType == StringType) { | ||
(filter, row) => | ||
// For string type, we can get bytes of our `UTF8String` directly, and call the `putBinary` | ||
// instead of `putString` to avoid unnecessary conversion. | ||
filter.putBinary(row.getUTF8String(0).getBytes) | ||
filter | ||
} else { | ||
(filter, row) => | ||
// TODO: specialize it. | ||
filter.putLong(row.get(0, colType).asInstanceOf[Number].longValue()) | ||
filter | ||
val updater: (BloomFilter, InternalRow) => Unit = colType match { | ||
// For string type, we can get bytes of our `UTF8String` directly, and call the `putBinary` | ||
// instead of `putString` to avoid unnecessary conversion. | ||
case StringType => (filter, row) => filter.putBinary(row.getUTF8String(0).getBytes) | ||
case ByteType => (filter, row) => filter.putLong(row.getByte(0)) | ||
case ShortType => (filter, row) => filter.putLong(row.getShort(0)) | ||
case IntegerType => (filter, row) => filter.putLong(row.getInt(0)) | ||
case LongType => (filter, row) => filter.putLong(row.getLong(0)) | ||
case _ => | ||
throw new IllegalArgumentException( | ||
s"Bloom filter only supports string type and integral types, " + | ||
s"and does not support type $colType." | ||
) | ||
} | ||
|
||
singleCol.queryExecution.toRdd.aggregate(zero)(seqOp, _ mergeInPlace _) | ||
singleCol.queryExecution.toRdd.aggregate(zero)( | ||
(filter: BloomFilter, row: InternalRow) => { | ||
updater(filter, row) | ||
filter | ||
}, | ||
_ mergeInPlace _ | ||
) | ||
} | ||
} |
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.
need to add java doc.
also update the other java doc to say "Increment item's count by one." or "Increment item's count by count"