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

[Snowflake] implement GROUP BY ALL #1180

Merged
merged 1 commit into from
Nov 12, 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 @@ -437,12 +437,13 @@ class LogicalPlanGenerator(
val child = generate(aggregate.child)
val expressions = expr.commas(aggregate.grouping_expressions)
aggregate.group_type match {
case ir.GroupByAll => code"$child GROUP BY ALL"
case ir.GroupBy =>
code"$child GROUP BY $expressions"
case ir.Pivot if aggregate.pivot.isDefined =>
val pivot = aggregate.pivot.get
val col = expr.generate(pivot.col)
val values = pivot.values.map(expr.generate(_)).mkCode(" IN(", ", ", ")")
val values = pivot.values.map(expr.generate).mkCode(" IN(", ", ", ")")
code"$child PIVOT($expressions FOR $col$values)"
case a => partialResult(a, ir.UnsupportedGroupType(a.toString))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ case object UnspecifiedGroupType extends GroupType

case object GroupBy extends GroupType

case object GroupByAll extends GroupType

case object Pivot extends GroupType

case object UnspecifiedFormat extends ParseFormat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ class SnowflakeRelationBuilder(override val vc: SnowflakeVisitorCoordinator)
private def buildGroupBy(ctx: GroupByClauseContext, input: ir.LogicalPlan): ir.LogicalPlan = {
Option(ctx).fold(input) { c =>
val groupingExpressions =
c.groupByList()
.groupByElem()
.asScala
Option(c.groupByList()).toSeq
.flatMap(_.groupByElem().asScala)
.map(_.accept(vc.expressionBuilder))
val groupType = if (c.ALL() != null) ir.GroupByAll else ir.GroupBy
val aggregate =
ir.Aggregate(child = input, group_type = ir.GroupBy, grouping_expressions = groupingExpressions, pivot = None)
ir.Aggregate(child = input, group_type = groupType, grouping_expressions = groupingExpressions, pivot = None)
buildHaving(c.havingClause(), aggregate)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ class SnowflakeToDatabricksTranspilerTest extends AnyWordSpec with TranspilerTes
"SELECT ARRAY_SORT([0, 2, 4, NULL, 5, NULL], 1 = 1, TRUE);".failsTranspilation
}

"GROUP BY ALL" in {
"SELECT car_model, COUNT(DISTINCT city) FROM dealer GROUP BY ALL;" transpilesTo
"SELECT car_model, COUNT(DISTINCT city) FROM dealer GROUP BY ALL;"
}

}

"Snowflake transpile function with optional brackets" should {
Expand Down
Loading