Skip to content

Commit

Permalink
feat: programmatically list supported dialects
Browse files Browse the repository at this point in the history
  • Loading branch information
reata committed Oct 15, 2023
1 parent a3d754b commit fbe8f40
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
26 changes: 4 additions & 22 deletions sqllineage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,10 @@ def main(args=None) -> None:
elif args.graph_visualization:
return draw_lineage_graph(**{"host": args.host, "port": args.port})
elif args.dialects:
print(
"""non-validating
ansi
athena
bigquery
clickhouse
databricks
db2
exasol
hive
materialize
mysql
oracle
postgres
redshift
snowflake
soql
sparksql
sqlite
teradata
tsql"""
)
dialects = []
for _, supported_dialects in LineageRunner.supported_dialects().items():
dialects += supported_dialects
print("\n".join(dialects))
else:
parser.print_help()

Expand Down
4 changes: 4 additions & 0 deletions sqllineage/core/analyzer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import abstractmethod
from typing import List

from sqllineage.core.holders import StatementLineageHolder

Expand All @@ -8,6 +9,9 @@ class LineageAnalyzer:
Parser specific implementation should inherit this class and implement analyze method
"""

PARSER_NAME: str = ""
SUPPORTED_DIALECTS: List[str] = []

@abstractmethod
def analyze(self, sql: str) -> StatementLineageHolder:
"""
Expand Down
5 changes: 4 additions & 1 deletion sqllineage/core/parser/sqlfluff/analyzer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings
from typing import Dict, List

from sqlfluff.core import Linter, SQLLexError, SQLParseError
from sqlfluff.core import Linter, SQLLexError, SQLParseError, dialect_readout
from sqlfluff.core.parser import BaseSegment

from sqllineage.core.analyzer import LineageAnalyzer
Expand All @@ -17,6 +17,9 @@
class SqlFluffLineageAnalyzer(LineageAnalyzer):
"""SQL Statement Level Lineage Analyzer for `sqlfluff`"""

PARSER_NAME = "sqlfluff"
SUPPORTED_DIALECTS = list(dialect.label for dialect in dialect_readout())

def __init__(self, dialect: str):
self._dialect = dialect
self.tsql_split_cache: Dict[str, BaseSegment] = {}
Expand Down
3 changes: 3 additions & 0 deletions sqllineage/core/parser/sqlparse/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
class SqlParseLineageAnalyzer(LineageAnalyzer):
"""SQL Statement Level Lineage Analyzer."""

PARSER_NAME = "sqlparse"
SUPPORTED_DIALECTS = ["non-validating"]

def analyze(self, sql: str) -> StatementLineageHolder:
# get rid of comments, which cause inconsistencies in sqlparse output
stmt = sqlparse.parse(trim_comment(sql))[0]
Expand Down
21 changes: 21 additions & 0 deletions sqllineage/runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import warnings
from collections import OrderedDict
from typing import Dict, List, Optional, Tuple

from sqllineage import DEFAULT_DIALECT, SQLPARSE_DIALECT
Expand Down Expand Up @@ -186,3 +187,23 @@ def _eval(self):
self._stmt_holders = [analyzer.analyze(stmt) for stmt in self._stmt]
self._sql_holder = SQLLineageHolder.of(*self._stmt_holders)
self._evaluated = True

@staticmethod
def supported_dialects() -> Dict[str, List[str]]:
"""
an ordered dict (so we can make sure the default parser implementation comes first)
with kv as parser_name: dialect list
"""
dialects = OrderedDict(
[
(
SqlParseLineageAnalyzer.PARSER_NAME,
SqlParseLineageAnalyzer.SUPPORTED_DIALECTS,
),
(
SqlFluffLineageAnalyzer.PARSER_NAME,
SqlFluffLineageAnalyzer.SUPPORTED_DIALECTS,
),
]
)
return dialects
4 changes: 4 additions & 0 deletions sqllineagejs/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ const dialects = {
"athena",
"bigquery",
"clickhouse",
"databricks",
"db2",
"duckdb",
"exasol",
"greenplum",
"hive",
"materialize",
"mysql",
Expand All @@ -107,6 +110,7 @@ const dialects = {
"sparksql",
"sqlite",
"teradata",
"trino",
"tsql"
]
}
Expand Down

0 comments on commit fbe8f40

Please sign in to comment.