@@ -249,28 +249,13 @@ func validateGroupBy(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scop
249249 }
250250
251251 var err error
252- var parent sql.Node
253- checkParent := false
254252 var project * plan.Project
255253 var having * plan.Having
256254 transform .Inspect (n , func (n sql.Node ) bool {
257- defer func () {
258- parent = n
259- }()
260-
261255 // Allow the parser use the GroupBy node to eval the aggregation functions
262256 // for sql statements that don't make use of the GROUP BY expression.
263257 switch n := n .(type ) {
264258 case * plan.GroupBy :
265- if checkParent {
266- switch parent .(type ) {
267- case * plan.Having , * plan.Project , * plan.Sort :
268- // TODO: these shouldn't be skipped but we currently aren't able to validate GroupBys with selected aliased
269- // expressions and a lot of our tests group by aliases
270- // https://github.com/dolthub/dolt/issues/4998
271- return true
272- }
273- }
274259
275260 if len (n .GroupByExprs ) == 0 {
276261 return true
@@ -316,9 +301,10 @@ func validateGroupBy(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scop
316301 })
317302 }
318303
319- // TODO: also allow grouping by unique non-nullable columns
304+ // TODO: also allow grouping by unique non-nullable columns https://github.com/dolthub/dolt/issues/9700
320305 // TODO: There's currently no way to tell whether or not a primary key column is part of a multi-column
321- // primary key.
306+ // primary key. When there is a join, we only check if one primary key column is referenced, meaning we
307+ // sometimes incorrectly validate group bys when a joined table has a multi-column primary key.
322308 if len (primaryKeys ) != 0 && (groupByPrimaryKeys == len (primaryKeys ) || (isJoin && groupByPrimaryKeys > 0 )) {
323309 return true
324310 }
@@ -327,8 +313,6 @@ func validateGroupBy(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scop
327313
328314 for _ , expr := range selectExprs {
329315 if ! expressionReferencesOnlyGroupBys (groupBys , expr ) {
330- // TODO: this is currently too restrictive. Dependent columns are fine to reference
331- // https://dev.mysql.com/doc/refman/8.4/en/group-by-functional-dependence.html
332316 err = analyzererrors .ErrValidationGroupBy .New (expr .String ())
333317 return false
334318 }
0 commit comments