-
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-19851] Add support for EVERY and ANY (SOME) aggregates #22047
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0eb5992
Add new aggregates EVERY and ANY (SOME).
ptkool a8cf7e1
Fix Scala style check errors.
ptkool 519a455
Resolved issue with Any aggregate and added window function test.
ptkool a929914
Added additional pyspark.sql tests.
ptkool d65ef4a
Fix pyspark window function tests.
ptkool 3217636
Resolve several issues with Pyspark tests.
ptkool 309d4b6
Resolve Scala style issues.
ptkool b5e9afb
Fix Python style errors.
ptkool d05ca69
generatedoc fix
dilipbiswal d050193
code review
dilipbiswal 291a13d
python codestyle
dilipbiswal b378fff
Add null filtering logic to the aggregate function along with tests
dilipbiswal e1764df
Fix
dilipbiswal 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
64 changes: 64 additions & 0 deletions
64
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/AnyAgg.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,64 @@ | ||
/* | ||
* 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.spark.sql.catalyst.expressions.aggregate | ||
|
||
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.util.TypeUtils | ||
import org.apache.spark.sql.types._ | ||
|
||
@ExpressionDescription( | ||
usage = "_FUNC_(expr) - Returns true if at least one value of `expr` is true.") | ||
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. BTW, don't forget to add |
||
case class AnyAgg(child: Expression) extends DeclarativeAggregate with ImplicitCastInputTypes { | ||
|
||
override def children: Seq[Expression] = child :: Nil | ||
|
||
override def nullable: Boolean = true | ||
|
||
override def dataType: DataType = BooleanType | ||
|
||
override def inputTypes: Seq[AbstractDataType] = Seq(BooleanType) | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = | ||
TypeUtils.checkForBooleanExpr(child.dataType, "function any") | ||
|
||
private lazy val some = AttributeReference("some", BooleanType)() | ||
|
||
private lazy val valueSet = AttributeReference("valueSet", BooleanType)() | ||
|
||
override lazy val aggBufferAttributes = some :: valueSet :: Nil | ||
|
||
override lazy val initialValues: Seq[Expression] = Seq( | ||
/* some = */ Literal.create(false, BooleanType), | ||
/* valueSet = */ Literal.create(false, BooleanType) | ||
) | ||
|
||
override lazy val updateExpressions: Seq[Expression] = Seq( | ||
/* some = */ Or(some, If (child.isNull, some, child)), | ||
/* valueSet = */ valueSet || child.isNotNull | ||
) | ||
|
||
override lazy val mergeExpressions: Seq[Expression] = Seq( | ||
/* some = */ Or(some.left, some.right), | ||
/* valueSet */ valueSet.right || valueSet.left | ||
) | ||
|
||
override lazy val evaluateExpression: Expression = | ||
If (valueSet, some, Literal.create(null, BooleanType)) | ||
} |
64 changes: 64 additions & 0 deletions
64
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Every.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,64 @@ | ||
/* | ||
* 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.spark.sql.catalyst.expressions.aggregate | ||
|
||
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.util.TypeUtils | ||
import org.apache.spark.sql.types._ | ||
|
||
@ExpressionDescription( | ||
usage = "_FUNC_(expr) - Returns true if all values of `expr` are true.") | ||
case class Every(child: Expression) extends DeclarativeAggregate with ImplicitCastInputTypes { | ||
|
||
override def children: Seq[Expression] = child :: Nil | ||
|
||
override def nullable: Boolean = true | ||
|
||
override def dataType: DataType = BooleanType | ||
|
||
override def inputTypes: Seq[AbstractDataType] = Seq(BooleanType) | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = | ||
TypeUtils.checkForBooleanExpr(child.dataType, "function every") | ||
|
||
private lazy val every = AttributeReference("every", BooleanType)() | ||
|
||
private lazy val valueSet = AttributeReference("valueSet", BooleanType)() | ||
|
||
override lazy val aggBufferAttributes = every :: valueSet :: Nil | ||
|
||
override lazy val initialValues: Seq[Expression] = Seq( | ||
/* every = */ Literal.create(true, BooleanType), | ||
/* valueSet = */ Literal.create(false, BooleanType) | ||
) | ||
|
||
override lazy val updateExpressions: Seq[Expression] = Seq( | ||
/* every = */ And(every, If (child.isNull, every, child)), | ||
/* valueSet = */ valueSet || child.isNotNull | ||
) | ||
|
||
override lazy val mergeExpressions: Seq[Expression] = Seq( | ||
/* every = */ And(every.left, every.right), | ||
/* valueSet */ valueSet.right || valueSet.left | ||
) | ||
|
||
override lazy val evaluateExpression: Expression = | ||
If (valueSet, every, Literal.create(null, BooleanType)) | ||
} |
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
80 changes: 80 additions & 0 deletions
80
...yst/src/test/scala/org/apache/spark/sql/catalyst/expressions/aggregate/AnyTestSuite.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,80 @@ | ||
/* | ||
* 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.spark.sql.catalyst.expressions.aggregate | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Literal} | ||
import org.apache.spark.sql.types.BooleanType | ||
|
||
class AnyTestSuite extends SparkFunSuite { | ||
val input = AttributeReference("input", BooleanType, nullable = true)() | ||
val evaluator = DeclarativeAggregateEvaluator(AnyAgg(input), Seq(input)) | ||
|
||
test("empty buffer") { | ||
assert(evaluator.initialize() === InternalRow(false, false)) | ||
} | ||
|
||
test("update") { | ||
val result = evaluator.update( | ||
InternalRow(true), | ||
InternalRow(false), | ||
InternalRow(true)) | ||
assert(result === InternalRow(true, true)) | ||
} | ||
|
||
test("merge") { | ||
// Empty merge | ||
val p0 = evaluator.initialize() | ||
assert(evaluator.merge(p0) === InternalRow(false, false)) | ||
|
||
// Single merge | ||
val p1 = evaluator.update(InternalRow(true), InternalRow(true)) | ||
assert(evaluator.merge(p1) === InternalRow(true, true)) | ||
|
||
// Multiple merges. | ||
val p2 = evaluator.update(InternalRow(false), InternalRow(null)) | ||
assert(evaluator.merge(p1, p2) === InternalRow(true, true)) | ||
|
||
// Empty partitions (p0 is empty) | ||
assert(evaluator.merge(p0, p2) === InternalRow(false, true)) | ||
assert(evaluator.merge(p2, p1, p0) === InternalRow(true, true)) | ||
} | ||
|
||
test("eval") { | ||
// Null Eval | ||
assert(evaluator.eval(InternalRow(true, false)) === InternalRow(null)) | ||
assert(evaluator.eval(InternalRow(false, false)) === InternalRow(null)) | ||
|
||
// Empty Eval | ||
val p0 = evaluator.initialize() | ||
assert(evaluator.eval(p0) === InternalRow(null)) | ||
|
||
// Update - Eval | ||
val p1 = evaluator.update(InternalRow(true), InternalRow(null)) | ||
assert(evaluator.eval(p1) === InternalRow(true)) | ||
|
||
// Update - Merge - Eval | ||
val p2 = evaluator.update(InternalRow(false), InternalRow(false)) | ||
val m1 = evaluator.merge(p0, p2) | ||
assert(evaluator.eval(m1) === InternalRow(false)) | ||
|
||
// Update - Merge - Eval (empty partition at the end) | ||
val m2 = evaluator.merge(p2, p1, p0) | ||
assert(evaluator.eval(m2) === InternalRow(true)) | ||
} | ||
} |
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.
Please keep the SQL functions and remove the function APIs. Thanks!
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.
@gatorsmile OK.
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.
@gatorsmile Hi Sean, I have prepared two branches. One in which these new aggregate functions are extending from the base Max and Min class basically reusing code. The other in which we replace these aggregate expressions in the optimizer. Below are the links.
branch-extend
branch-rewrite
I would prefer option 1 because of the following reasons.
to block it.
Please let me know how we want to move forward with this. Thanks !!
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.
+1 for option 1
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.
@cloud-fan Thank you very much for your response. I will create a new PR based on option-1 today and close this one.