-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-9241][SQL] Supporting multiple DISTINCT columns - follow-up (3) #9566
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,36 @@ class ScalaAggregateFunction(schema: StructType) extends UserDefinedAggregateFun | |
| } | ||
| } | ||
|
|
||
| class LongProductSum extends UserDefinedAggregateFunction { | ||
| def inputSchema: StructType = new StructType() | ||
| .add("a", LongType) | ||
| .add("b", LongType) | ||
|
|
||
| def bufferSchema: StructType = new StructType() | ||
| .add("product", LongType) | ||
|
|
||
| def dataType: DataType = LongType | ||
|
|
||
| def deterministic: Boolean = true | ||
|
|
||
| def initialize(buffer: MutableAggregationBuffer): Unit = { | ||
| buffer(0) = 0L | ||
| } | ||
|
|
||
| def update(buffer: MutableAggregationBuffer, input: Row): Unit = { | ||
| if (!(input.isNullAt(0) || input.isNullAt(1))) { | ||
| buffer(0) = buffer.getLong(0) + input.getLong(0) * input.getLong(1) | ||
| } | ||
| } | ||
|
|
||
| def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = { | ||
| buffer1(0) = buffer1.getLong(0) + buffer2.getLong(0) | ||
| } | ||
|
|
||
| def evaluate(buffer: Row): Any = | ||
| buffer.getLong(0) | ||
| } | ||
|
|
||
| abstract class AggregationQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton { | ||
| import testImplicits._ | ||
|
|
||
|
|
@@ -110,6 +140,7 @@ abstract class AggregationQuerySuite extends QueryTest with SQLTestUtils with Te | |
| // Register UDAFs | ||
| sqlContext.udf.register("mydoublesum", new MyDoubleSum) | ||
| sqlContext.udf.register("mydoubleavg", new MyDoubleAvg) | ||
| sqlContext.udf.register("longProductSum", new LongProductSum) | ||
| } | ||
|
|
||
| override def afterAll(): Unit = { | ||
|
|
@@ -545,19 +576,21 @@ abstract class AggregationQuerySuite extends QueryTest with SQLTestUtils with Te | |
| | count(distinct value2), | ||
| | sum(distinct value2), | ||
| | count(distinct value1, value2), | ||
| | longProductSum(distinct value1, value2), | ||
|
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. Actually, what is the semantic of this? If we have
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. To be more specific, my question is not for the semantic of
Contributor
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. Yes and No. The input we care about only consists of these tuples: However in the current implementation a distinct aggregate will see more input than those. It will also see records from other groups. However, the values in these records are nulled out. The assumption here is that an AggregateFunction is not changed by an all NULL update. The only case I can think of that would be problematic is a We could solve this by wrapping AggregateFunctions with an operator which will only update if the group id is correct.
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. yeah, right. I was thinking about just the inputs of this particular distinct agg function (records with other gid values are ignored). |
||
| | count(value1), | ||
| | sum(value1), | ||
| | count(value2), | ||
| | sum(value2), | ||
| | longProductSum(value1, value2), | ||
| | count(*), | ||
| | count(1) | ||
| |FROM agg2 | ||
| |GROUP BY key | ||
| """.stripMargin), | ||
| Row(null, 3, 30, 3, 60, 3, 3, 30, 3, 60, 4, 4) :: | ||
| Row(1, 2, 40, 3, -10, 3, 3, 70, 3, -10, 3, 3) :: | ||
| Row(2, 2, 0, 1, 1, 1, 3, 1, 3, 3, 4, 4) :: | ||
| Row(3, 0, null, 1, 3, 0, 0, null, 1, 3, 2, 2) :: Nil) | ||
| Row(null, 3, 30, 3, 60, 3, -4700, 3, 30, 3, 60, -4700, 4, 4) :: | ||
| Row(1, 2, 40, 3, -10, 3, -100, 3, 70, 3, -10, -100, 3, 3) :: | ||
| Row(2, 2, 0, 1, 1, 1, 1, 3, 1, 3, 3, 2, 4, 4) :: | ||
| Row(3, 0, null, 1, 3, 0, 0, 0, null, 1, 3, 0, 2, 2) :: Nil) | ||
| } | ||
|
|
||
| test("test count") { | ||
|
|
||
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.
By calling
toMapI am potentially breaking the alignment betweendistinctAggChildrenanddistinctAggChildAttrs.