File tree Expand file tree Collapse file tree 2 files changed +16
-0
lines changed
Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ class Parser(parser.Parser):
5252 "INTERSECT" : lambda self , query : query .intersect (
5353 _select_all (self ._parse_table ()), distinct = False , copy = False
5454 ),
55+ "SORT" : lambda self , query : self ._parse_order_by (query ),
5556 }
5657
5758 def _parse_statement (self ) -> t .Optional [exp .Expression ]:
@@ -103,6 +104,13 @@ def _parse_take(self, query: exp.Query) -> t.Optional[exp.Query]:
103104 num = self ._parse_number () # TODO: TAKE for ranges a..b
104105 return query .limit (num ) if num else None
105106
107+ def _parse_order_by (self , query : exp .Select ) -> t .Optional [exp .Query ]:
108+ l_brace = self ._match (TokenType .L_BRACE )
109+ expressions = self ._parse_csv (self ._parse_ordered )
110+ if l_brace and not self ._match (TokenType .R_BRACE ):
111+ self .raise_error ("Expecting }" )
112+ return query .order_by (self .expression (exp .Order , expressions = expressions ), copy = False )
113+
106114 def _parse_expression (self ) -> t .Optional [exp .Expression ]:
107115 if self ._next and self ._next .token_type == TokenType .ALIAS :
108116 alias = self ._parse_id_var (True )
Original file line number Diff line number Diff line change @@ -37,6 +37,14 @@ def test_prql(self):
3737 "FROM x FILTER (age > 25 || age < 22) FILTER age > 26 FILTER age < 27" ,
3838 "SELECT * FROM x WHERE ((age > 25 OR age < 22) AND age > 26) AND age < 27" ,
3939 )
40+ self .validate_identity (
41+ "FROM x SORT age" ,
42+ "SELECT * FROM x ORDER BY age" ,
43+ )
44+ self .validate_identity (
45+ "FROM x SORT {age, name}" ,
46+ "SELECT * FROM x ORDER BY age, name" ,
47+ )
4048 self .validate_identity ("FROM x APPEND y" , "SELECT * FROM x UNION ALL SELECT * FROM y" )
4149 self .validate_identity ("FROM x REMOVE y" , "SELECT * FROM x EXCEPT ALL SELECT * FROM y" )
4250 self .validate_identity (
You can’t perform that action at this time.
0 commit comments