Skip to content

Commit

Permalink
fix: left join with extra spaces in the middle confuses parser (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
reata committed Apr 8, 2020
1 parent d80f93b commit c16243c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions sqllineage/core.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import argparse
import re
import sys
from typing import List, Set

import sqlparse
from sqlparse.sql import Comment, Function, Identifier, Parenthesis, Statement, TokenList
from sqlparse.tokens import Keyword, Token

SOURCE_TABLE_TOKENS = ('FROM', 'JOIN', 'INNER JOIN', 'LEFT JOIN', 'RIGHT JOIN', 'LEFT OUTER JOIN', 'RIGHT OUTER JOIN',
'FULL OUTER JOIN', 'CROSS JOIN')
SOURCE_TABLE_TOKENS = (r'FROM',
# inspired by https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/keywords.py
r'((LEFT\s+|RIGHT\s+|FULL\s+)?(INNER\s+|OUTER\s+|STRAIGHT\s+)?|(CROSS\s+|NATURAL\s+)?)?JOIN')
TARGET_TABLE_TOKENS = ('INTO', 'OVERWRITE', 'TABLE')
TEMP_TABLE_TOKENS = ('WITH',)

Expand Down Expand Up @@ -59,7 +61,7 @@ def _extract_from_token(self, token: Token) -> None:
if isinstance(sub_token, TokenList):
self._extract_from_token(sub_token)
if sub_token.ttype in Keyword:
if sub_token.normalized in SOURCE_TABLE_TOKENS:
if any(re.match(regex, sub_token.normalized) for regex in SOURCE_TABLE_TOKENS):
source_table_token_flag = True
elif sub_token.normalized in TARGET_TABLE_TOKENS:
target_table_token_flag = True
Expand Down
4 changes: 4 additions & 0 deletions tests/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def test_select_left_join():
helper("SELECT * FROM tab1 LEFT JOIN tab2", {"tab1", "tab2"})


def test_select_left_join_with_extra_space_in_middle():
helper("SELECT * FROM tab1 LEFT JOIN tab2", {"tab1", "tab2"})


def test_select_left_semi_join():
helper("SELECT * FROM tab1 LEFT SEMI JOIN tab2", {"tab1", "tab2"})

Expand Down

0 comments on commit c16243c

Please sign in to comment.