Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,10 @@ case class Aggregate(
override def output: Seq[Attribute] = aggregateExpressions.map(_.toAttribute)
override def maxRows: Option[Long] = child.maxRows

override def validConstraints: Set[Expression] =
child.constraints.union(getAliasedConstraints(aggregateExpressions))
override def validConstraints: Set[Expression] = {
val nonAgg = aggregateExpressions.filter(_.find(_.isInstanceOf[AggregateExpression]).isEmpty)
child.constraints.union(getAliasedConstraints(nonAgg))
}

override lazy val statistics: Statistics = {
if (groupingExpressions.isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ class ConstraintPropagationSuite extends SparkFunSuite {
assert(tr.analyze.constraints.isEmpty)

val aliasedRelation = tr.where('c.attr > 10 && 'a.attr < 5)
.groupBy('a, 'c, 'b)('a, 'c.as("c1"), count('a).as("a3")).select('c1, 'a).analyze
.groupBy('a, 'c, 'b)('a, 'c.as("c1"), count('a).as("a3")).select('c1, 'a, 'a3).analyze
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sameeragarwal How's this one?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good


// SPARK-16644: aggregate expression count(a) should not appear in the constraints.
verifyConstraints(aliasedRelation.analyze.constraints,
ExpressionSet(Seq(resolveColumn(aliasedRelation.analyze, "c1") > 10,
IsNotNull(resolveColumn(aliasedRelation.analyze, "c1")),
resolveColumn(aliasedRelation.analyze, "a") < 5,
IsNotNull(resolveColumn(aliasedRelation.analyze, "a")))))
IsNotNull(resolveColumn(aliasedRelation.analyze, "a")),
IsNotNull(resolveColumn(aliasedRelation.analyze, "a3")))))
}

test("propagating constraints in expand") {
Expand Down
17 changes: 17 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2965,4 +2965,21 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
}
}
}

test("SPARK-16644: Aggregate should not put aggregate expressions to constraints") {
withTable("tbl") {
sql("CREATE TABLE tbl(a INT, b INT) USING parquet")
checkAnswer(sql(
"""
|SELECT
| a,
| MAX(b) AS c1,
| b AS c2
|FROM tbl
|WHERE a = b
|GROUP BY a, b
|HAVING c1 = 1
""".stripMargin), Nil)
}
}
}