diff --git a/backends-clickhouse/src/test/scala/org/apache/gluten/execution/GlutenClickhouseCountDistinctSuite.scala b/backends-clickhouse/src/test/scala/org/apache/gluten/execution/GlutenClickhouseCountDistinctSuite.scala index 1b954df22eac..5887050d0aaa 100644 --- a/backends-clickhouse/src/test/scala/org/apache/gluten/execution/GlutenClickhouseCountDistinctSuite.scala +++ b/backends-clickhouse/src/test/scala/org/apache/gluten/execution/GlutenClickhouseCountDistinctSuite.scala @@ -213,4 +213,11 @@ class GlutenClickhouseCountDistinctSuite extends GlutenClickHouseWholeStageTrans } ) } + + test("GLUTEN-5852: Fix mismatch result columns size exception related to 5618") { + val sql = + "select distinct * from (select 2 as r3, count(distinct a, b, c), 2 as r1, 2 as r2 from " + + "values (0, null, 1), (1, 1, 1), (2, 2, 1), (1, 2, 1) ,(2, 2, 2) as data(a,b,c) group by c)" + compareResultsAgainstVanillaSpark(sql, true, { _ => }) + } } diff --git a/gluten-core/src/main/scala/org/apache/gluten/utils/PullOutProjectHelper.scala b/gluten-core/src/main/scala/org/apache/gluten/utils/PullOutProjectHelper.scala index 824694837dcb..505f13f263a2 100644 --- a/gluten-core/src/main/scala/org/apache/gluten/utils/PullOutProjectHelper.scala +++ b/gluten-core/src/main/scala/org/apache/gluten/utils/PullOutProjectHelper.scala @@ -61,7 +61,12 @@ trait PullOutProjectHelper { replaceBoundReference: Boolean = false): Expression = expr match { case alias: Alias => - projectExprsMap.getOrElseUpdate(alias.child.canonicalized, alias).toAttribute + alias.child match { + case _: Literal => + projectExprsMap.getOrElseUpdate(alias, alias).toAttribute + case _ => + projectExprsMap.getOrElseUpdate(alias.child.canonicalized, alias).toAttribute + } case attr: Attribute => attr case e: BoundReference if !replaceBoundReference => e case other =>