Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #793 from erizocosmico/fix/having-orderby
Browse files Browse the repository at this point in the history
sql/analyzer: fix order by resolution for all nodes
  • Loading branch information
ajnavarro committed Jul 30, 2019
2 parents b7f3982 + a427ee2 commit 550cc54
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
21 changes: 21 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,27 @@ var queries = []struct {
`SELECT CASE WHEN NULL THEN "yes" ELSE "no" END AS test`,
[]sql.Row{{"no"}},
},
{
`SELECT
table_schema,
table_name,
CASE
WHEN table_type = 'BASE TABLE' THEN
CASE
WHEN table_schema = 'mysql'
OR table_schema = 'performance_schema' THEN 'SYSTEM TABLE'
ELSE 'TABLE'
END
WHEN table_type = 'TEMPORARY' THEN 'LOCAL_TEMPORARY'
ELSE table_type
END AS TABLE_TYPE
FROM information_schema.tables
WHERE table_schema = 'mydb'
AND table_name = 'mytable'
HAVING table_type IN ('TABLE', 'VIEW')
ORDER BY table_type, table_schema, table_name`,
[]sql.Row{{"mydb", "mytable", "TABLE"}},
},
}

func TestQueries(t *testing.T) {
Expand Down
14 changes: 12 additions & 2 deletions sql/analyzer/resolve_orderby.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,18 @@ func pushSortDown(sort *plan.Sort) (sql.Node, error) {
case *plan.ResolvedTable:
return child, nil
default:
// Can't do anything here, there should be either a project or a groupby
// below an order by.
children := child.Children()
if len(children) == 1 {
newChild, err := pushSortDown(plan.NewSort(sort.SortFields, children[0]))
if err != nil {
return nil, err
}

return child.WithChildren(newChild)
}

// If the child has more than one children we don't know to which side
// the sort must be pushed down.
return nil, errSortPushdown.New(child)
}
}
Expand Down

0 comments on commit 550cc54

Please sign in to comment.