Skip to content
Open
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 @@ -30,7 +30,7 @@ import org.apache.flink.table.planner.utils.TableConfigUtils.isOperatorDisabled

import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall}
import org.apache.calcite.plan.RelOptRule.{any, operand}
import org.apache.calcite.rel.RelNode
import org.apache.calcite.rel.{RelCollations, RelNode}

import scala.collection.JavaConversions._

Expand Down Expand Up @@ -101,7 +101,9 @@ class BatchPhysicalHashAggRule
// create BatchPhysicalLocalHashAggregate
val localRequiredTraitSet = input.getTraitSet.replace(FlinkConventions.BATCH_PHYSICAL)
val newInput = RelOptRule.convert(input, localRequiredTraitSet)
val providedTraitSet = localRequiredTraitSet
// A hash aggregate does not preserve input ordering; reset the collation to EMPTY
// so the local agg does not advertise a stale collation from its input.
val providedTraitSet = localRequiredTraitSet.replace(RelCollations.EMPTY)
val localHashAgg = createLocalAgg(
agg.getCluster,
providedTraitSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ HashAggregate(isMerge=[true], select=[Final_AVG(sum$0, count$1) AS EXPR$0, Final
+- LocalHashAggregate(select=[Partial_AVG(a) AS (sum$0, count$1), Partial_SUM(b) AS sum$2, Partial_COUNT(c) AS count$3, Partial_SUM($f3) AS sum$4])
+- Calc(select=[CAST(1 AS INTEGER) AS a, b, c, c._1 AS $f3], where=[(a = 1)])
+- TableSourceScan(table=[[default_catalog, default_database, MyTable]], fields=[a, b, c])
]]>
</Resource>
</TestCase>
<TestCase name="testGroupAggregateAfterOrderBy">
<Resource name="optimized exec plan">
<![CDATA[
Calc(select=[EXPR$0 AS cnt])
+- HashAggregate(isMerge=[true], groupBy=[d], select=[d, Final_COUNT(count$0) AS EXPR$0])
+- Exchange(distribution=[hash[d]])
+- LocalHashAggregate(groupBy=[d], select=[d, Partial_COUNT(e) AS count$0])
+- Sort(orderBy=[c ASC])
+- Exchange(distribution=[single])
+- TableSourceScan(table=[[default_catalog, default_database, MyTable]], fields=[a, b, c, d, e])
]]>
</Resource>
</TestCase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ class AggregateTest extends TableTestBase {

util.verifyExecPlan(resultTable)
}

@Test
def testGroupAggregateAfterOrderBy(): Unit = {
// order_by before a group_by that prunes the sort-key column must not
// leave a stale collation on the local hash aggregate.
val util = batchTestUtil()
val sourceTable = util
.addTableSource[(Int, Long, Int, String, Double)]("MyTable", 'a, 'b, 'c, 'd, 'e)

val resultTable = sourceTable
.orderBy('c)
.groupBy('d)
.select('e.count.as('cnt))

util.verifyExecPlan(resultTable)
}
}