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

Fix sqllineage to read nested sqlfluff config files. #637

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion sqllineage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sqllineage.drawing import draw_lineage_graph
from sqllineage.runner import LineageRunner
from sqllineage.utils.constant import LineageLevel
from sqllineage.utils.helpers import extract_sql_from_args
from sqllineage.utils.helpers import extract_file_path_from_args, extract_sql_from_args

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -108,8 +108,10 @@ def main(args=None) -> None:
warnings.warn("Both -e and -f options are specified. -e option will be ignored")
if args.f or args.e:
sql = extract_sql_from_args(args)
file_path = extract_file_path_from_args(args)
runner = LineageRunner(
sql,
file_path=file_path,
dialect=args.dialect,
metadata_provider=metadata_provider,
verbose=args.verbose,
Expand Down
9 changes: 5 additions & 4 deletions sqllineage/core/parser/sqlfluff/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ class SqlFluffLineageAnalyzer(LineageAnalyzer):
PARSER_NAME = "sqlfluff"
SUPPORTED_DIALECTS = list(dialect.label for dialect in dialect_readout())

def __init__(self, dialect: str, silent_mode: bool = False):
def __init__(self, file_path: str, dialect: str, silent_mode: bool = False):
self._dialect = dialect
self._silent_mode = silent_mode
self.tsql_split_cache: Dict[str, BaseSegment] = {}
self.sqlfluff_config = FluffConfig.from_path(
path=file_path, overrides={"dialect": self._dialect}
)

def split_tsql(self, sql: str) -> List[str]:
"""
Expand Down Expand Up @@ -79,9 +82,7 @@ def analyze(
)

def _list_specific_statement_segment(self, sql: str):
parsed = Linter(
config=FluffConfig.from_root(overrides={"dialect": self._dialect})
).parse_string(sql)
parsed = Linter(config=self.sqlfluff_config).parse_string(sql)
violations = [
str(e)
for e in parsed.violations
Expand Down
7 changes: 6 additions & 1 deletion sqllineage/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(
verbose: bool = False,
silent_mode: bool = False,
draw_options: Optional[Dict[str, Any]] = None,
file_path: str = ".",
):
"""
The entry point of SQLLineage after command line options are parsed.
Expand All @@ -51,6 +52,7 @@ def __init__(
:param metadata_provider: metadata service object providing table schema
:param verbose: verbose flag indicating whether statement-wise lineage result will be shown
:param silent_mode: boolean flag indicating whether to skip lineage analysis for unknown statement types
:param file_path: path of the SQL file.
"""
if dialect == SQLPARSE_DIALECT:
warnings.warn(
Expand All @@ -60,6 +62,7 @@ def __init__(
stacklevel=2,
)
self._sql = sql
self._file_path = file_path
self._verbose = verbose
self._draw_options = draw_options if draw_options else {}
self._evaluated = False
Expand Down Expand Up @@ -183,7 +186,9 @@ def _eval(self):
analyzer = (
SqlParseLineageAnalyzer()
if self._dialect == SQLPARSE_DIALECT
else SqlFluffLineageAnalyzer(self._dialect, self._silent_mode)
else SqlFluffLineageAnalyzer(
self._file_path, self._dialect, self._silent_mode
)
)
if SQLLineageConfig.TSQL_NO_SEMICOLON and self._dialect == "tsql":
self._stmt = analyzer.split_tsql(self._sql.strip())
Expand Down
7 changes: 7 additions & 0 deletions sqllineage/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def extract_sql_from_args(args: Namespace) -> str:
return sql


def extract_file_path_from_args(args: Namespace) -> str:
file_path = "."
if getattr(args, "f", None):
file_path = args.f
return file_path


def split(sql: str) -> List[str]:
# TODO: we need a parser independent split function
import sqlparse
Expand Down