@@ -346,4 +346,50 @@ var OrderByGroupByScriptTests = []ScriptTest{
346
346
},
347
347
},
348
348
},
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
+ },
349
395
}
0 commit comments