Skip to content

tdfs4ds.lineage.lineage

tdfs4ds.lineage.lineage

_COMMENT_SINGLE module-attribute

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

_COMMENT_MULTI module-attribute

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

_RE_CREATE_TABLE_AS module-attribute

_RE_CREATE_TABLE_AS = re.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    ', re.IGNORECASE | re.VERBOSE)

_RE_SINGLE_QUOTED_STRING module-attribute

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

_RE_REPLACE_VIEW_AS module-attribute

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

_RE_CREATE_OR_REPLACE_VIEW_AS module-attribute

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

_RE_MERGE_INTO module-attribute

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

_RE_UPDATE module-attribute

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

_RE_USING module-attribute

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

_RE_DELETE_FROM module-attribute

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

_RE_TD_ON_CLAUSE module-attribute

_RE_TD_ON_CLAUSE = re.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    ', re.IGNORECASE | re.VERBOSE | re.DOTALL)

_RE_INSERT_INTO module-attribute

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

_RE_CREATE_VIEW_AS module-attribute

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

_RE_FROM_JOIN module-attribute

_RE_FROM_JOIN = re.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    ', re.IGNORECASE | re.VERBOSE)

_RE_CTE_NAMES module-attribute

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

_RE_FROM_FUNCTION module-attribute

_RE_FROM_FUNCTION = re.compile('\\b(?:EXTRACT|TRIM|SUBSTRING|OVERLAY)\\s*\\(', re.IGNORECASE)

_RE_FROM_WORD module-attribute

_RE_FROM_WORD = re.compile('\\bFROM\\b', re.IGNORECASE)

_RE_USING_FUNCTION module-attribute

_RE_USING_FUNCTION = re.compile('\\b(?:TRANSLATE_CHK|TRANSLATE|CHAR2HEXINT)\\s*\\(', re.IGNORECASE)

_RE_USING_WORD module-attribute

_RE_USING_WORD = re.compile('\\bUSING\\b', re.IGNORECASE)

_NOT_AN_OBJECT module-attribute

_NOT_AN_OBJECT = frozenset({'SELECT', 'SEL', 'FROM', 'WHERE', 'TABLE', 'VALUES', 'CASE', 'NULL', 'AS', 'ON', 'AND', 'OR', 'NOT', 'CAST', 'POSITION', 'SUBSTRING', 'SUBSTR', 'TRIM', 'EXTRACT', 'COALESCE', 'NULLIF', 'INTERVAL', 'DATE', 'TIME', 'TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'USER'})

_RE_NUMBER module-attribute

_RE_NUMBER = re.compile('^\\d[\\d.]*$')

_RE_USING_TABLE module-attribute

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

_RE_SPEC_TABLE_NAME module-attribute

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

_RE_WITH_CLAUSE module-attribute

_RE_WITH_CLAUSE = re.compile('\\bWITH\\b(?:\\s+RECURSIVE\\b)?\\s*', re.IGNORECASE)

_RE_CTE_HEAD module-attribute

_RE_CTE_HEAD = re.compile('\\s*(?P<name>"[^"]+"|\\w+)\\s*', re.IGNORECASE)

_RE_CTE_AS module-attribute

_RE_CTE_AS = re.compile('\\s*AS\\s*\\(', re.IGNORECASE)

_DEFAULT_DB module-attribute

_DEFAULT_DB = '<DEFAULT_DATABASE>'

_RE_TD_FUNC_CALL module-attribute

_RE_TD_FUNC_CALL = re.compile('\\bTD_[A-Z_0-9]+\\s*\\(', re.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.

_not_an_object

_not_an_object(name: str) -> bool

A bare number or reserved word found where a table name is expected. A quoted or database-qualified name is left alone: that spelling names a real object.

_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_function_from

_mask_function_from(sql: str, func_re=None, word_re=None) -> str

Blank the FROM of EXTRACT(.. FROM ..)-like calls, at their own level only, so the next word is not read as a table (CAST, POSITION, 1, a column), however deep the arguments. A sub-query inside the call keeps its FROM. Offsets do not move.

_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

_bare_cte_name

_bare_cte_name(name: str) -> str

_extract_cte_names

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

Names defined by WITH [RECURSIVE] name [(col, ...)] AS (...) [, name2 ... AS (...)], wherever the WITH clause sits (top level, view body, nested sub-query).

_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.