Disregard CV12 when UNNEST is detected in from_expression_element (#7211)

This commit is contained in:
Annebelle Olminkhof
2025-10-23 23:49:10 +02:00
committed by GitHub
parent 367611d707
commit b1a9d8a436
2 changed files with 57 additions and 0 deletions

View File

@@ -108,9 +108,17 @@ class Rule_CV12(BaseRule):
no_recursive_seg_type=["select_statement"],
)
)
encountered_function = self._get_from_expression_element_function(
join_table_reference
)
if encountered_function in ("UNNEST",):
# If UNNEST function is used, disregard lack of condition
continue
encountered_references.add(
self._get_from_expression_element_alias(join_table_reference)
)
join_clause_keywords = [
seg for seg in join_clause.segments if seg.type == "keyword"
]
@@ -262,6 +270,32 @@ class Rule_CV12(BaseRule):
return alias_str
@staticmethod
def _get_from_expression_element_function(from_expr_element: BaseSegment) -> str:
"""Extract the function name from a from_expression_element if present."""
def get_segment_or_default(
segment: BaseSegment, child_type: str, default: BaseSegment
) -> BaseSegment:
"""Helper to safely get a child segment or return a default."""
child = segment.get_child(child_type)
return child if child else default
table_expr = get_segment_or_default(
from_expr_element, "table_expression", from_expr_element
)
if "function" not in table_expr.direct_descendant_type_set:
return ""
function = get_segment_or_default(table_expr, "function", table_expr)
function_name = get_segment_or_default(function, "function_name", table_expr)
function_id = get_segment_or_default(
function_name, "function_name_identifier", table_expr
)
return function_id.raw_upper
@staticmethod
def _is_where_clause_simplifable(where_clause: BaseSegment) -> bool:
assert where_clause.is_type("where_clause")

View File

@@ -109,3 +109,26 @@ test_fail_join_with_bracketed_join:
)
WHERE
bar.id = foo1.id;
test_pass_left_join_unnest_where_aliased:
pass_str: |
SELECT
t.user_id,
unnested_value AS purchased_product
FROM
t
LEFT JOIN
UNNEST(t.purchases) AS unnested_value
WHERE
unnested_value = 'laptop'
test_pass_left_join_unnest_where_unaliased:
pass_str: |
SELECT
t.user_id
FROM
t
LEFT JOIN
UNNEST(t.purchases)
WHERE
t.user_id != 1