Skip to content

Commit 181d5a5

Browse files
committed
add test
1 parent 5cbc653 commit 181d5a5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

enginetest/queries/order_by_group_by_queries.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,50 @@ var OrderByGroupByScriptTests = []ScriptTest{
346346
},
347347
},
348348
},
349+
{
350+
// https://github.com/dolthub/dolt/issues/9605
351+
Name: "Order by CTE column wrapped by parentheses",
352+
SetUpScript: []string{
353+
"create table tree_data (id int not null, parent_id int, primary key (id), constraint foreign key (parent_id) references tree_data (id));",
354+
"insert into tree_data values (1,null),(2,null),(3,null),(4,1),(5,4),(6,3)",
355+
},
356+
Assertions: []ScriptTestAssertion{
357+
{
358+
Query: `WITH RECURSIVE __rank_table(id, parent_id, rank_order) AS (SELECT tree_data.id,
359+
tree_data.parent_id,
360+
row_number() OVER (
361+
ORDER BY tree_data.id) AS rank_order
362+
FROM tree_data),
363+
__tree(tree_depth, tree_path, tree_ordering, tree_pk) AS
364+
(SELECT 0,
365+
cast(concat("", id, "") AS char(1000)),
366+
cast(concat("", lpad(concat(T.rank_order, ""), 20, "0")) AS char(1000)),
367+
T.id
368+
FROM __rank_table T
369+
WHERE T.parent_id IS NULL
370+
UNION ALL SELECT __tree.tree_depth + 1,
371+
concat(__tree.tree_path, T2.id, ""),
372+
concat(__tree.tree_ordering, lpad(concat(T2.rank_order, ""), 20, "0")),
373+
T2.id
374+
FROM __tree,
375+
__rank_table T2
376+
WHERE __tree.tree_pk = T2.parent_id)
377+
SELECT __tree.tree_depth AS tree_depth,
378+
tree_data.id,
379+
tree_data.parent_id
380+
FROM __tree,
381+
tree_data
382+
WHERE __tree.tree_pk = tree_data.id
383+
ORDER BY (__tree.tree_depth) DESC;`,
384+
Expected: []sql.Row{
385+
{2, 5, 4},
386+
{1, 4, 1},
387+
{1, 6, 3},
388+
{0, 1, nil},
389+
{0, 2, nil},
390+
{0, 3, nil},
391+
},
392+
},
393+
},
394+
},
349395
}

sql/planbuilder/orderby.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (b *Builder) analyzeOrderBy(fromScope, projScope *scope, order ast.OrderBy)
4747
descending = true
4848
}
4949
expr := o.Expr
50+
// unwrap expressions wrapped by parentheses
5051
if parensExpr, ok := expr.(*ast.ParenExpr); ok {
5152
expr = parensExpr.Expr
5253
}

0 commit comments

Comments
 (0)