Skip to content
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

[GLUTEN-7096] [CH] fix exception when same names in group by #7101

Merged
merged 4 commits into from
Sep 24, 2024
Merged
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 @@ -26,7 +26,7 @@ import org.apache.gluten.extension.columnar.AddFallbackTagRule
import org.apache.gluten.extension.columnar.MiscColumnarRules.TransformPreOverrides
import org.apache.gluten.sql.shims.SparkShimLoader
import org.apache.gluten.substrait.expression.{ExpressionBuilder, ExpressionNode, WindowFunctionNode}
import org.apache.gluten.utils.{CHJoinValidateUtil, UnknownJoinStrategy}
import org.apache.gluten.utils.{CHAggUtil, CHJoinValidateUtil, UnknownJoinStrategy}
import org.apache.gluten.vectorized.CHColumnarBatchSerializer

import org.apache.spark.ShuffleDependency
Expand Down Expand Up @@ -160,7 +160,7 @@ class CHSparkPlanExecApi extends SparkPlanExecApi with Logging {
child: SparkPlan): HashAggregateExecBaseTransformer =
CHHashAggregateExecTransformer(
requiredChildDistributionExpressions,
groupingExpressions.distinct,
CHAggUtil.distinctIgnoreQualifier(groupingExpressions),
aggregateExpressions,
aggregateAttributes,
initialInputBufferOffset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.gluten.substrait.{AggregationParams, SubstraitContext}
import org.apache.gluten.substrait.expression.{AggregateFunctionNode, ExpressionBuilder, ExpressionNode}
import org.apache.gluten.substrait.extensions.{AdvancedExtensionNode, ExtensionBuilder}
import org.apache.gluten.substrait.rel.{RelBuilder, RelNode}
import org.apache.gluten.utils.CHAggUtil

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.aggregate._
Expand Down Expand Up @@ -429,6 +430,15 @@ case class CHHashAggregateExecPullOutHelper(
aggregateAttr.toList
}

override def allAggregateResultAttributes(
groupingExpressions: Seq[NamedExpression]): List[Attribute] = {
if (aggregateExpressions.nonEmpty) {
super.allAggregateResultAttributes(groupingExpressions)
} else {
super.allAggregateResultAttributes(CHAggUtil.distinctIgnoreQualifier(groupingExpressions))
}
}

protected def getAttrForAggregateExpr(
exp: AggregateExpression,
aggregateAttributeList: Seq[Attribute],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.gluten.utils

import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.expressions.NamedExpression

import scala.util.control.Breaks.{break, breakable}

object CHAggUtil extends Logging {
def distinctIgnoreQualifier(expressions: Seq[NamedExpression]): Seq[NamedExpression] = {
var dist = List[NamedExpression]()
for (i <- expressions.indices) {
var k = -1
breakable {
for (j <- 0 to i - i)
if (
j != i &&
expressions(i).name == expressions(j).name &&
expressions(i).exprId == expressions(j).exprId &&
expressions(i).dataType == expressions(j).dataType &&
expressions(i).nullable == expressions(j).nullable
) {
k = j
break
}
}
if (k < 0) dist = dist :+ expressions(i)
}
dist
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,50 @@ class GlutenHiveSQLQueryCHSuite extends GlutenHiveSQLQuerySuiteBase {
ignoreIfNotExists = true,
purge = false)
}

testGluten("GLUTEN-7096: Same names in group by may cause exception") {
sql("create table if not exists test_7096 (day string, rtime int, uid string, owner string)")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-02', 567, 'user2', 'owner2')")
val query =
"""
|select days, rtime, uid, owner, day1
|from (
| select day1 as days, rtime, uid, owner, day1
| from (
| select distinct coalesce(day, "today") as day1, rtime, uid, owner
| from test_7096 where day = '2024-09-01'
| )) group by days, rtime, uid, owner, day1
|""".stripMargin
val df = sql(query)
checkAnswer(df, Seq(Row("2024-09-01", 123, "user1", "owner1", "2024-09-01")))
spark.sessionState.catalog.dropTable(
TableIdentifier("test_7096"),
ignoreIfNotExists = true,
purge = false)
}

testGluten("GLUTEN-7096: Same names with different qualifier in group by may cause exception") {
sql("create table if not exists test_7096 (day string, rtime int, uid string, owner string)")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-02', 567, 'user2', 'owner2')")
val query =
"""
|select days, rtime, uid, owner, day1
|from (
| select day1 as days, rtime, uid, owner, day1
| from (
| select distinct coalesce(day, "today") as day1, rtime, uid, owner
| from test_7096 where day = '2024-09-01'
| ) t1 ) t2 group by days, rtime, uid, owner, day1
|""".stripMargin
val df = sql(query)
checkAnswer(df, Seq(Row("2024-09-01", 123, "user1", "owner1", "2024-09-01")))
spark.sessionState.catalog.dropTable(
TableIdentifier("test_7096"),
ignoreIfNotExists = true,
purge = false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,50 @@ class GlutenHiveSQLQueryCHSuite extends GlutenHiveSQLQuerySuiteBase {
ignoreIfNotExists = true,
purge = false)
}

testGluten("GLUTEN-7096: Same names in group by may cause exception") {
sql("create table if not exists test_7096 (day string, rtime int, uid string, owner string)")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-02', 567, 'user2', 'owner2')")
val query =
"""
|select days, rtime, uid, owner, day1
|from (
| select day1 as days, rtime, uid, owner, day1
| from (
| select distinct coalesce(day, "today") as day1, rtime, uid, owner
| from test_7096 where day = '2024-09-01'
| )) group by days, rtime, uid, owner, day1
|""".stripMargin
val df = sql(query)
checkAnswer(df, Seq(Row("2024-09-01", 123, "user1", "owner1", "2024-09-01")))
spark.sessionState.catalog.dropTable(
TableIdentifier("test_7096"),
ignoreIfNotExists = true,
purge = false)
}

testGluten("GLUTEN-7096: Same names with different qualifier in group by may cause exception") {
sql("create table if not exists test_7096 (day string, rtime int, uid string, owner string)")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-02', 567, 'user2', 'owner2')")
val query =
"""
|select days, rtime, uid, owner, day1
|from (
| select day1 as days, rtime, uid, owner, day1
| from (
| select distinct coalesce(day, "today") as day1, rtime, uid, owner
| from test_7096 where day = '2024-09-01'
| ) t1 ) t2 group by days, rtime, uid, owner, day1
|""".stripMargin
val df = sql(query)
checkAnswer(df, Seq(Row("2024-09-01", 123, "user1", "owner1", "2024-09-01")))
spark.sessionState.catalog.dropTable(
TableIdentifier("test_7096"),
ignoreIfNotExists = true,
purge = false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,50 @@ class GlutenHiveSQLQueryCHSuite extends GlutenHiveSQLQuerySuiteBase {
ignoreIfNotExists = true,
purge = false)
}

testGluten("GLUTEN-7096: Same names in group by may cause exception") {
sql("create table if not exists test_7096 (day string, rtime int, uid string, owner string)")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-02', 567, 'user2', 'owner2')")
val query =
"""
|select days, rtime, uid, owner, day1
|from (
| select day1 as days, rtime, uid, owner, day1
| from (
| select distinct coalesce(day, "today") as day1, rtime, uid, owner
| from test_7096 where day = '2024-09-01'
| )) group by days, rtime, uid, owner, day1
|""".stripMargin
val df = sql(query)
checkAnswer(df, Seq(Row("2024-09-01", 123, "user1", "owner1", "2024-09-01")))
spark.sessionState.catalog.dropTable(
TableIdentifier("test_7096"),
ignoreIfNotExists = true,
purge = false)
}

testGluten("GLUTEN-7096: Same names with different qualifier in group by may cause exception") {
sql("create table if not exists test_7096 (day string, rtime int, uid string, owner string)")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-02', 567, 'user2', 'owner2')")
val query =
"""
|select days, rtime, uid, owner, day1
|from (
| select day1 as days, rtime, uid, owner, day1
| from (
| select distinct coalesce(day, "today") as day1, rtime, uid, owner
| from test_7096 where day = '2024-09-01'
| ) t1 ) t2 group by days, rtime, uid, owner, day1
|""".stripMargin
val df = sql(query)
checkAnswer(df, Seq(Row("2024-09-01", 123, "user1", "owner1", "2024-09-01")))
spark.sessionState.catalog.dropTable(
TableIdentifier("test_7096"),
ignoreIfNotExists = true,
purge = false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,50 @@ class GlutenHiveSQLQueryCHSuite extends GlutenHiveSQLQuerySuiteBase {
ignoreIfNotExists = true,
purge = false)
}

testGluten("GLUTEN-7096: Same names in group by may cause exception") {
sql("create table if not exists test_7096 (day string, rtime int, uid string, owner string)")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-02', 567, 'user2', 'owner2')")
val query =
"""
|select days, rtime, uid, owner, day1
|from (
| select day1 as days, rtime, uid, owner, day1
| from (
| select distinct coalesce(day, "today") as day1, rtime, uid, owner
| from test_7096 where day = '2024-09-01'
| )) group by days, rtime, uid, owner, day1
|""".stripMargin
val df = sql(query)
checkAnswer(df, Seq(Row("2024-09-01", 123, "user1", "owner1", "2024-09-01")))
spark.sessionState.catalog.dropTable(
TableIdentifier("test_7096"),
ignoreIfNotExists = true,
purge = false)
}

testGluten("GLUTEN-7096: Same names with different qualifier in group by may cause exception") {
sql("create table if not exists test_7096 (day string, rtime int, uid string, owner string)")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-01', 123, 'user1', 'owner1')")
sql("insert into test_7096 values ('2024-09-02', 567, 'user2', 'owner2')")
val query =
"""
|select days, rtime, uid, owner, day1
|from (
| select day1 as days, rtime, uid, owner, day1
| from (
| select distinct coalesce(day, "today") as day1, rtime, uid, owner
| from test_7096 where day = '2024-09-01'
| ) t1 ) t2 group by days, rtime, uid, owner, day1
|""".stripMargin
val df = sql(query)
checkAnswer(df, Seq(Row("2024-09-01", 123, "user1", "owner1", "2024-09-01")))
spark.sessionState.catalog.dropTable(
TableIdentifier("test_7096"),
ignoreIfNotExists = true,
purge = false)
}
}
Loading