Skip to content

tdfs4ds.lineage.lineage

tdfs4ds.lineage.lineage

_COMMENT_SINGLE module-attribute

_COMMENT_SINGLE = compile('--[^\\n]*')

_COMMENT_MULTI module-attribute

_COMMENT_MULTI = compile('/\\*.*?\\*/', DOTALL | MULTILINE)

_RE_CREATE_TABLE_AS module-attribute

_RE_CREATE_TABLE_AS = compile('\n    \\bCREATE\\s+\n    (?:MULTISET\\s+|SET\\s+|VOLATILE\\s+|TEMP(?:ORARY)?\\s+)?   # qualifiers\n    TABLE\\s+\n    (?P<name>[\\w".@$#]+)\n    \\s+AS\\s*\\(                                              # <-- require AS (\n    ', IGNORECASE | VERBOSE)

_RE_SINGLE_QUOTED_STRING module-attribute

_RE_SINGLE_QUOTED_STRING = compile("\n    '\n    (?:''|[^'])*   # escaped quote or any non-quote char\n    '\n    ", VERBOSE | DOTALL)

_RE_REPLACE_VIEW_AS module-attribute

_RE_REPLACE_VIEW_AS = compile('\n    \\bREPLACE\\s+VIEW\\s+\n    (?P<name>[\\w".@$#]+)\n    \\s+AS\\b\n    ', IGNORECASE | VERBOSE)

_RE_CREATE_OR_REPLACE_VIEW_AS module-attribute

_RE_CREATE_OR_REPLACE_VIEW_AS = compile('\n    \\b(?:CREATE|REPLACE)\\s+VIEW\\s+\n    (?P<name>[\\w".@$#]+)\n    \\s+AS\\b\n    ', IGNORECASE | VERBOSE)

_RE_MERGE_INTO module-attribute

_RE_MERGE_INTO = compile('\n    \\bMERGE\\s+INTO\\s+\n    (?P<name>[\\w".@$#]+)\n    \\b\n    ', IGNORECASE | VERBOSE)

_RE_UPDATE module-attribute

_RE_UPDATE = compile('\n    \\bUPDATE\\s+\n    (?!SET\\b)                 # <-- prevents UPDATE SET from matching\n    (?P<name>[\\w".@$#]+)\n    \\b\n    ', IGNORECASE | VERBOSE)

_RE_USING module-attribute

_RE_USING = compile('\n    \\bUSING\\s+(?P<name>[\\w".@$#]+)\\b\n    ', IGNORECASE | VERBOSE)

_RE_DELETE_FROM module-attribute

_RE_DELETE_FROM = compile('\n    \\bDELETE\\s+FROM\\s+\n    (?P<name>[\\w".@$#]+)\n    \\b\n    ', IGNORECASE | VERBOSE)

_RE_TD_ON_CLAUSE module-attribute

_RE_TD_ON_CLAUSE = compile('\n    \\bON\\b\\s+\n    (?P<arg>\n        \\([^\\)]*\\)          # naive: one-level parens (good enough for your test cases)\n        |\n        [\\w".@$#]+             # identifier\n    )\n    ', IGNORECASE | VERBOSE | DOTALL)

_RE_INSERT_INTO module-attribute

_RE_INSERT_INTO = compile('\n    \\bINSERT\\s+INTO\\s+\n    (?P<name>[\\w".@$#]+)\n    \\b\n    ', IGNORECASE | VERBOSE)

_RE_CREATE_VIEW_AS module-attribute

_RE_CREATE_VIEW_AS = compile('\n    \\b(?:CREATE|REPLACE)\\s+VIEW\\s+\n    (?P<name>[\\w".@$#]+)\n    \\s+AS\\b\n    ', IGNORECASE | VERBOSE)

_RE_FROM_JOIN module-attribute

_RE_FROM_JOIN = compile('\n    \\bFROM\\b\n    \\s+(?P<name>[\\w".@$#]+)\n    |\n    \\b(?:INNER\\s+JOIN|LEFT\\s+JOIN|RIGHT\\s+JOIN|FULL\\s+OUTER\\s+JOIN|CROSS\\s+JOIN|JOIN)\\b\n    \\s+(?P<join_name>[\\w".@$#]+)\n    ', IGNORECASE | VERBOSE)

_RE_CTE_NAMES module-attribute

_RE_CTE_NAMES = compile('\n    \\bWITH\\b\\s*(?P<first>\\w+)\\s+AS\\s*\\(\n    |,\\s*(?P<next>\\w+)\\s+AS\\s*\\(\n    ', IGNORECASE | VERBOSE)

_RE_EXTRACT_FROM module-attribute

_RE_EXTRACT_FROM = compile('\\bEXTRACT\\s*\\(\\s*[^()]*?\\bFROM\\b[^()]*?\\)', IGNORECASE | DOTALL)

_RE_USING_TABLE module-attribute

_RE_USING_TABLE = compile('\n    \\bUSING\\s+(?P<name>[\\w".@$#]+)\\b\n    ', IGNORECASE | VERBOSE)

_RE_SPEC_TABLE_NAME module-attribute

_RE_SPEC_TABLE_NAME = compile('\\b(?:SERIES_SPEC|ART_SPEC)\\s*\\(.*?\\bTABLE_NAME\\s*\\(\\s*([^)\\s,]+)\\s*\\)', IGNORECASE | DOTALL)

_DEFAULT_DB module-attribute

_DEFAULT_DB = '<DEFAULT_DATABASE>'

_RE_TD_FUNC_CALL module-attribute

_RE_TD_FUNC_CALL = compile('\\bTD_[A-Z_0-9]+\\s*\\(', IGNORECASE)

logger_safe

logger_safe(level, message, *args, **kwargs)

Wrapper around the global logger that only emits logs when tdfs4ds.DISPLAY_LOGS is True. level is a string like "info", "error", etc.

_mask_single_quoted_strings

_mask_single_quoted_strings(sql: str) -> str

Replace all single-quoted SQL string literals with empty string ''. Preserves SQL structure while removing misleading keywords.

_extract_using_tables

_extract_using_tables(sql: str) -> List[str]

_extract_series_art_table_names

_extract_series_art_table_names(sql: str) -> List[str]

Extract the table/view names referenced inside SERIES_SPEC(TABLE_NAME(...)) / ART_SPEC(TABLE_NAME(...)) clauses of a UAF EXECUTE FUNCTION statement.

Inert on ordinary SQL (SERIES_SPEC/ART_SPEC do not appear in normal views), so wiring it into analyze_sql_query adds no spurious sources for non-UAF views.

_mask_extract_from

_mask_extract_from(sql: str) -> str

_is_placeholder

_is_placeholder(part: str) -> bool

_extract_balanced_parens

_extract_balanced_parens(sql: str, open_paren_index: int) -> str

Return the substring starting at open_paren_index (which must point to '(') up to and including the matching closing ')'. Falls back to end-of-string if unbalanced.

_qualify_default_db

_qualify_default_db(name: str, default_db: str) -> str

_remove_sql_comments

_remove_sql_comments(sql: str) -> str

_extract_cte_names

_extract_cte_names(sql: str) -> Set[str]

_strip_alias

_strip_alias(name: str) -> str

_quote_identifier

_quote_identifier(name: str) -> str

_dedupe_preserve_order

_dedupe_preserve_order(items: Iterable[str]) -> List[str]

_extract_td_function_inputs

_extract_td_function_inputs(sql: str) -> List[str]

analyze_sql_query

analyze_sql_query(sql_query: str, *, debug: bool = False) -> Dict[str, List[str]]

Extracts source and target tables/views from a SQL query.