Skip to content

Commit af97f10

Browse files
authored
Merge pull request #10 from mindsdb/lateral-join
Support lateral join
2 parents 3b151d1 + b2be4e9 commit af97f10

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

mindsdb_sql_parser/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__title__ = 'mindsdb_sql_parser'
22
__package_name__ = 'mindsdb_sql_parser'
3-
__version__ = '0.4.1'
3+
__version__ = '0.5.0'
44
__description__ = "Mindsdb SQL parser"
55
__email__ = "[email protected]"
66
__author__ = 'MindsDB Inc'

mindsdb_sql_parser/lexer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class MindsDBLexer(Lexer):
5353
GROUP_BY, HAVING, ORDER_BY,
5454
STAR, FOR, UPDATE,
5555

56-
JOIN, INNER, OUTER, CROSS, LEFT, RIGHT, ON, ASOF,
56+
JOIN, INNER, OUTER, CROSS, LEFT, RIGHT, ON, ASOF, LATERAL,
5757

5858
UNION, ALL, INTERSECT, EXCEPT,
5959

@@ -236,6 +236,7 @@ class MindsDBLexer(Lexer):
236236
LEFT = r'\bLEFT\b'
237237
RIGHT = r'\bRIGHT\b'
238238
ASOF = r'\bASOF\b'
239+
LATERAL = r'\bLATERAL\b'
239240

240241
# UNION
241242

mindsdb_sql_parser/parser.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,9 +1283,14 @@ def from_table(self, p):
12831283
'OUTER JOIN',
12841284
'LEFT OUTER JOIN',
12851285
'FULL OUTER JOIN',
1286-
'ASOF JOIN',
1287-
'ASOF LEFT JOIN',
1286+
)
1287+
def join_type(self, p):
1288+
return ' '.join([x for x in p])
1289+
1290+
@_('join_type',
1291+
'ASOF join_type',
12881292
'LEFT ASOF JOIN',
1293+
'join_type LATERAL',
12891294
)
12901295
def join_clause(self, p):
12911296
return ' '.join([x for x in p])

tests/test_base_sql/test_select_structure.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,3 +1293,34 @@ def test_asof_join(self):
12931293
ast = parse_sql(query)
12941294
assert str(ast) == str(expected_ast)
12951295
assert ast.to_tree() == expected_ast.to_tree()
1296+
1297+
def test_lateral_join(self):
1298+
query = f'''
1299+
select * from table1 a
1300+
INNER JOIN LATERAL (SELECT *
1301+
FROM table2 b
1302+
WHERE a.x=b.x
1303+
LIMIT 1) b ON true
1304+
'''
1305+
1306+
expected_ast = Select(
1307+
targets=[Star()],
1308+
from_table=Join(
1309+
condition=Constant(True),
1310+
join_type='INNER JOIN LATERAL',
1311+
left=Identifier('table1', alias=Identifier('a')),
1312+
right=Select(
1313+
targets=[Star()],
1314+
from_table=Identifier('table2', alias=Identifier('b')),
1315+
where=BinaryOperation(op='=', args=[
1316+
Identifier('a.x'), Identifier('b.x')
1317+
]),
1318+
limit=Constant(1),
1319+
alias=Identifier('b'),
1320+
parentheses=True
1321+
),
1322+
)
1323+
)
1324+
ast = parse_sql(query)
1325+
assert str(ast) == str(expected_ast)
1326+
assert ast.to_tree() == expected_ast.to_tree()

0 commit comments

Comments
 (0)