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

Commit 550cc54

Browse files
authored
Merge pull request #793 from erizocosmico/fix/having-orderby
sql/analyzer: fix order by resolution for all nodes
2 parents b7f3982 + a427ee2 commit 550cc54

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

engine_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,27 @@ var queries = []struct {
14331433
`SELECT CASE WHEN NULL THEN "yes" ELSE "no" END AS test`,
14341434
[]sql.Row{{"no"}},
14351435
},
1436+
{
1437+
`SELECT
1438+
table_schema,
1439+
table_name,
1440+
CASE
1441+
WHEN table_type = 'BASE TABLE' THEN
1442+
CASE
1443+
WHEN table_schema = 'mysql'
1444+
OR table_schema = 'performance_schema' THEN 'SYSTEM TABLE'
1445+
ELSE 'TABLE'
1446+
END
1447+
WHEN table_type = 'TEMPORARY' THEN 'LOCAL_TEMPORARY'
1448+
ELSE table_type
1449+
END AS TABLE_TYPE
1450+
FROM information_schema.tables
1451+
WHERE table_schema = 'mydb'
1452+
AND table_name = 'mytable'
1453+
HAVING table_type IN ('TABLE', 'VIEW')
1454+
ORDER BY table_type, table_schema, table_name`,
1455+
[]sql.Row{{"mydb", "mytable", "TABLE"}},
1456+
},
14361457
}
14371458

14381459
func TestQueries(t *testing.T) {

sql/analyzer/resolve_orderby.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,18 @@ func pushSortDown(sort *plan.Sort) (sql.Node, error) {
166166
case *plan.ResolvedTable:
167167
return child, nil
168168
default:
169-
// Can't do anything here, there should be either a project or a groupby
170-
// below an order by.
169+
children := child.Children()
170+
if len(children) == 1 {
171+
newChild, err := pushSortDown(plan.NewSort(sort.SortFields, children[0]))
172+
if err != nil {
173+
return nil, err
174+
}
175+
176+
return child.WithChildren(newChild)
177+
}
178+
179+
// If the child has more than one children we don't know to which side
180+
// the sort must be pushed down.
171181
return nil, errSortPushdown.New(child)
172182
}
173183
}

0 commit comments

Comments
 (0)