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

groupby and having getfield index fixes #2281

Merged
merged 27 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
12 changes: 6 additions & 6 deletions enginetest/queries/integration_plans.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3496,9 +3496,19 @@ Select * from (
},
},
{
Query: `SELECT SUBSTRING_INDEX(mytable.s, "d", 1) AS s FROM mytable INNER JOIN othertable ON (SUBSTRING_INDEX(mytable.s, "d", 1) = SUBSTRING_INDEX(othertable.s2, "d", 1)) GROUP BY 1 HAVING s = 'secon'`,
Query: `SELECT SUBSTRING_INDEX(mytable.s, "d", 1) AS s FROM mytable INNER JOIN othertable ON (SUBSTRING_INDEX(mytable.s, "d", 1) = SUBSTRING_INDEX(othertable.s2, "d", 1)) GROUP BY 1 HAVING s = 'secon';`,
Expected: []sql.Row{{"secon"}},
},
{
Query: `SELECT SUBSTRING_INDEX(mytable.s, "d", 1) AS ss FROM mytable INNER JOIN othertable ON (SUBSTRING_INDEX(mytable.s, "d", 1) = SUBSTRING_INDEX(othertable.s2, "d", 1)) GROUP BY s HAVING s = 'secon';`,
Expected: []sql.Row{},
},
{
Query: `SELECT SUBSTRING_INDEX(mytable.s, "d", 1) AS ss FROM mytable INNER JOIN othertable ON (SUBSTRING_INDEX(mytable.s, "d", 1) = SUBSTRING_INDEX(othertable.s2, "d", 1)) GROUP BY ss HAVING ss = 'secon';`,
Expected: []sql.Row{
{"secon"},
},
},
{
Query: `SELECT TRIM(mytable.s) AS s FROM mytable`,
Expected: []sql.Row{{"first row"}, {"second row"}, {"third row"}},
Expand Down
48 changes: 46 additions & 2 deletions sql/planbuilder/aggregates.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,12 +775,56 @@ func (b *Builder) buildHaving(fromScope, projScope, outScope *scope, having *ast
if fromScope.groupBy == nil {
fromScope.initGroupBy()
}
havingScope := fromScope.push()
// Having specifies conditions on groups. If not group by is present, all rows implicitly form a single aggregate group.
havingScope := fromScope.push() // TODO: we should not be including the entire fromScope

for _, c := range projScope.cols {
if c.tableId.IsEmpty() {
// If there are conflicting aliases in the projScope, we prioritize the fromScope
found := false
for _, cc := range fromScope.cols {
if c.col == cc.col {
havingScope.addColumn(cc)
found = true
break
}
}
if found {
continue
}

alias, isAlias := c.scalar.(*expression.Alias)
if !isAlias {
// TODO: error?
continue
}

// Aliased GetFields are allowed in having clauses regardless of weather they are in the group by
_, isGetField := alias.Child.(*expression.GetField)
if isGetField {
havingScope.newColumn(c)
continue
}

// Aliased expression is allowed if there is no group by (it is implicitly a single group)
if len(fromScope.groupBy.inCols) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is inCols == 0 equivalent to hasAggs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think hasAggs() returns whether or not there are aggregations used in the groupBy, while inCols are just the columns in the groupBy (can be outside of aggregation function)

havingScope.newColumn(c)
continue
}

// Aliased expressions are allowed in having clauses if they are in the group by
for _, cc := range fromScope.groupBy.inCols {
if c.col == cc.col {
havingScope.newColumn(c)
found = true
break
}
}

if found {
continue
}
}

havingScope.groupBy = fromScope.groupBy
h := b.buildScalar(havingScope, having.Expr)
outScope.node = plan.NewHaving(h, outScope.node)
Expand Down
Loading
Loading