Skip to content

Commit

Permalink
Merge pull request #402 from mindsdb/staging
Browse files Browse the repository at this point in the history
Release 0.19.0
  • Loading branch information
ea-rus authored Sep 19, 2024
2 parents 66aae44 + 18fbd6b commit 565ee04
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion mindsdb_sql/__about__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__title__ = 'mindsdb_sql'
__package_name__ = 'mindsdb_sql'
__version__ = '0.18.1'
__version__ = '0.19.0'
__description__ = "Pure python SQL parser"
__email__ = "[email protected]"
__author__ = 'MindsDB Inc'
Expand Down
4 changes: 2 additions & 2 deletions mindsdb_sql/parser/dialects/mindsdb/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ def FLOAT(self, t):
def INTEGER(self, t):
return t

@_(r"'(?:\\.|[^'])*'")
@_(r"'(?:\\.|[^'])*(?:''(?:\\.|[^'])*)*'")
def QUOTE_STRING(self, t):
t.value = t.value.replace('\\"', '"').replace("\\'", "'")
t.value = t.value.replace('\\"', '"').replace("\\'", "'").replace("''", "'")
return t

@_(r'"(?:\\.|[^"])*"')
Expand Down
25 changes: 16 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 Expand Up @@ -1672,13 +1676,16 @@ def json_value(self, p):

@_('identifier DOT identifier',
'identifier DOT integer',
'identifier DOT dquote_string',
'identifier DOT star')
def identifier(self, p):
node = p[0]
if isinstance(p[2], Star):
node.parts.append(p[2])
elif isinstance(p[2], int):
node.parts.append(str(p[2]))
elif isinstance(p[2], str):
node.parts.append(p[2])
else:
node.parts += p[2].parts
return node
Expand Down
2 changes: 1 addition & 1 deletion mindsdb_sql/planner/query_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def get_query_info(self, query):

def find_objects(node, is_table, **kwargs):
if isinstance(node, Function):
if node.namespace is not None:
if node.namespace is not None or node.op.lower() in ('llm',):
user_functions.append(node)

if is_table:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_parser/test_base_sql/test_base_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,29 @@ def test_escaping(self):
assert str(ast).lower() == str(expected_ast).lower()
assert ast.to_tree() == expected_ast.to_tree()

def test_quotes_escaping(self):
sql = "select 'women''s soccer'"

expected_ast = Select(
targets=[
Constant(value="women's soccer")
]
)
ast = parse_sql(sql)

assert str(ast).lower() == str(expected_ast).lower()
assert ast.to_tree() == expected_ast.to_tree()

def test_quotes_identifier(self):
sql = 'select t2."var (k)" from t2'

expected_ast = Select(
targets=[
Identifier(parts=['t2', 'var (k)'])
],
from_table=Identifier('t2')
)
ast = parse_sql(sql)

assert str(ast).lower() == str(expected_ast).lower()
assert ast.to_tree() == expected_ast.to_tree()
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)

0 comments on commit 565ee04

Please sign in to comment.