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 SQL masking issue where only one statement is processed with sqlparse #19105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 17 additions & 13 deletions ingestion/src/metadata/ingestion/lineage/masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def mask_literals_with_sqlparse(query: str):
logger = get_logger()

try:
parsed = sqlparse.parse(query) # Parse the query
# Parse all statements in the query
parsed_statements = sqlparse.parse(query)

if not parsed:
if not parsed_statements:
return query
parsed = parsed[0]

def mask_token(token):
# Mask all literals: strings, numbers, or other literal values
Expand All @@ -61,17 +61,21 @@ def mask_token(token):
for t in token.tokens:
mask_token(t)

# Process all tokens
for token in parsed.tokens:
if isinstance(token, Comparison):
# In comparisons, mask both sides if literals
for t in token.tokens:
mask_token(t)
else:
mask_token(token)
# Process each statement
masked_statements = []
for statement in parsed_statements:
for token in statement.tokens:
if isinstance(token, Comparison):
# In comparisons, mask both sides if literals
for t in token.tokens:
mask_token(t)
else:
mask_token(token)
masked_statements.append(str(statement))

# Reconstruct the query with masked literals
return "".join(masked_statements)

# Return the formatted masked query
return str(parsed)
except Exception as exc:
logger.debug(f"Failed to mask query with sqlparse: {exc}")
logger.debug(traceback.format_exc())
Expand Down
Loading