Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support expressions in order_by #400

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions mindsdb_sql/parser/dialects/mindsdb/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ def drop_chat_bot(self, p):

# -- triggers --
@_('CREATE TRIGGER identifier ON identifier LPAREN raw_query RPAREN')
@_('CREATE TRIGGER identifier ON identifier COLUMNS ordering_terms LPAREN raw_query RPAREN')
@_('CREATE TRIGGER identifier ON identifier COLUMNS column_list LPAREN raw_query RPAREN')
def create_trigger(self, p):
query_str = tokens_to_string(p.raw_query)

columns = None
if hasattr(p, 'ordering_terms'):
columns = [i.field for i in p.ordering_terms]
if hasattr(p, 'column_list'):
columns = [Identifier(i) for i in p.column_list]

return CreateTrigger(
name=p.identifier0,
Expand Down Expand Up @@ -1116,17 +1116,21 @@ def ordering_term(self, p):
p.ordering_term.nulls = p.NULLS_LAST
return p.ordering_term

@_('identifier DESC')
@_('ordering_term DESC')
def ordering_term(self, p):
return OrderBy(field=p.identifier, direction='DESC')
item = p.ordering_term
item.direction = 'DESC'
return item

@_('identifier ASC')
@_('ordering_term ASC')
def ordering_term(self, p):
return OrderBy(field=p.identifier, direction='ASC')
item = p.ordering_term
item.direction = 'ASC'
return item

@_('identifier')
@_('expr')
def ordering_term(self, p):
return OrderBy(field=p.identifier, direction='default')
return OrderBy(field=p.expr, direction='default')

@_('select USING kw_parameter_list')
def select(self, p):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_parser/test_base_sql/test_select_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def test_exits(self):
select * from db.item
where l_orderkey = o_orderkey
)
group by orderpriority
group by max(orderpriority)
'''
ast = parse_sql(sql)

Expand All @@ -631,7 +631,7 @@ def test_exits(self):
])
))
]),
group_by=[Identifier('orderpriority')],
group_by=[Function(op='max', args=[Identifier('orderpriority')])],
)

assert str(ast) == str(expected_ast)
Loading