-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-9298][SQL] Add pearson correlation aggregation function #8587
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
Changes from 1 commit
cb34a95
0dd6320
1505cd2
d3e4414
d10afbe
cc1657b
e1fb438
02562f3
5fbcf91
2f7b864
3b731e2
4f8c381
7dcf689
2de76b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -752,12 +752,12 @@ case class LastFunction( | |
| * Only support AggregateExpression2. | ||
| * | ||
| */ | ||
| case class Corr( | ||
| left: Expression, | ||
| right: Expression) extends BinaryExpression with AggregateExpression { | ||
| case class Corr(left: Expression, right: Expression) | ||
| extends BinaryExpression with AggregateExpression with ImplicitCastInputTypes { | ||
|
Contributor
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. This is just a place holder, right? Can we change it to
Member
Author
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.
Sorry. I think I know what you meant. |
||
| override def nullable: Boolean = false | ||
| override def dataType: DoubleType.type = DoubleType | ||
| override def toString: String = s"CORRELATION($left, $right)" | ||
| override def inputTypes: Seq[AbstractDataType] = Seq(DoubleType, DoubleType) | ||
| } | ||
|
Contributor
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. What will be the error message if we call this function when |
||
|
|
||
| // Compute standard deviation based on online algorithm specified here: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -581,6 +581,20 @@ abstract class AggregationQuerySuite extends QueryTest with SQLTestUtils with Te | |
| val df3 = Seq.tabulate(0)(i => (1.0 * i, 2.0 * i)).toDF("a", "b") | ||
| val corr4 = df3.groupBy().agg(corr("a", "b")).collect()(0).getDouble(0) | ||
| assert(corr4.isNaN) | ||
|
|
||
| val df4 = Seq.tabulate(10)(i => (1 * i, 2 * i, i * -1)).toDF("a", "b", "c") | ||
| val corr5 = df4.repartition(2).groupBy().agg(corr("a", "b")).collect()(0).getDouble(0) | ||
| assert(math.abs(corr5 - 1.0) < 1e-12) | ||
| val corr6 = df4.groupBy().agg(corr("a", "c")).collect()(0).getDouble(0) | ||
| assert(math.abs(corr6 + 1.0) < 1e-12) | ||
|
|
||
| withSQLConf(SQLConf.USE_SQL_AGGREGATE2.key -> "false") { | ||
| val errorMessage = intercept[AnalysisException] { | ||
| val df = Seq.tabulate(10)(i => (1.0 * i, 2.0 * i, i * -1.0)).toDF("a", "b", "c") | ||
| val corr1 = df.repartition(2).groupBy().agg(corr("a", "b")).collect()(0).getDouble(0) | ||
| }.getMessage | ||
| assert(errorMessage.contains("Corr is only implemented based on the new Aggregate Function")) | ||
| } | ||
| } | ||
|
Contributor
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. What will happen if the data type of input parameters are not double?
Member
Author
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. I will add ImplicitCastInputTypes to case class Corr. So the other NumericType can be automatically casting to double. |
||
|
|
||
| test("test Last implemented based on AggregateExpression1") { | ||
|
|
||
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.
Is it safe to assume that the
count2inbuffer1is non zero? There is - currently - no documentation on this.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.
We only need to consider count in buffer2. I will add document for it.
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.
Thanks for the comment. Now it is obvious, I wasn't thinking...
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.
No need to add comment for it?