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 @@ -2923,7 +2923,22 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
val extraAggExprs = new LinkedHashMap[Expression, NamedExpression]
val transformed = exprs.map { e =>
if (!e.resolved) {
e
val aggregatedCondition =
Aggregate(
agg.groupingExpressions,
Alias(e, "havingCondition")() :: Nil,
agg.child)
val resolvedOperator = executeSameContext(aggregatedCondition)
def resolvedAggregateFilter =
resolvedOperator
.asInstanceOf[Aggregate]
.aggregateExpressions.head

if (resolvedOperator.resolved) {
buildAggExprList(resolvedAggregateFilter, agg, extraAggExprs)
} else {
e
}
} else {
buildAggExprList(e, agg, extraAggExprs)
}
Expand Down
22 changes: 22 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 @@ -5059,6 +5059,28 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
}
}
}

test("SPARK-53094: Fix cube-related data quality problem") {
withTable("table1") {
withSQLConf() {
sql(
"""CREATE TABLE table1(product string, amount bigint,
|region string) using csv""".stripMargin)

sql("INSERT INTO table1 " + "VALUES('a', 100, 'east')")
sql("INSERT INTO table1 " + "VALUES('b', 200, 'east')")
sql("INSERT INTO table1 " + "VALUES('a', 150, 'west')")
sql("INSERT INTO table1 " + "VALUES('b', 250, 'west')")
sql("INSERT INTO table1 " + "VALUES('a', 120, 'east')")

checkAnswer(
sql("select product, region, sum(amount) as s " +
"from table1 group by product, region with cube having count(product) > 2 " +
"order by s desc"),
Seq(Row(null, null, 820), Row(null, "east", 420), Row("a", null, 370)))
}
}
}
}

case class Foo(bar: Option[String])