Skip to content

Commit

Permalink
L025: Derived query requires alias -- also handle UNION, etc. (#3548)
Browse files Browse the repository at this point in the history
* L025: Derived query requires alias -- also handle UNION, etc.

* Handle WITH statements

Co-authored-by: Barry Hart <barry.hart@mailchimp.com>
  • Loading branch information
barrywhart and Barry Hart committed Jul 3, 2022
1 parent fe8b93e commit b66071c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/sqlfluff/rules/L025.py
Expand Up @@ -121,7 +121,9 @@ def is_alias_required(
dialect_name in cls._dialects_requiring_alias_for_values_clause
)
elif any(
seg.is_type("select_statement")
seg.is_type(
"select_statement", "set_expression", "with_compound_statement"
)
for seg in segment.iter_segments(expanding=("bracketed",))
):
# The FROM expression is a derived table, i.e. a nested
Expand Down
22 changes: 21 additions & 1 deletion test/fixtures/rules/std_rule_cases/L025.yml
Expand Up @@ -312,8 +312,28 @@ test_fail_snowflake_flatten_function:
core:
dialect: snowflake

test_pass_derived_query_requires_alias:
test_pass_derived_query_requires_alias_1:
# Case 1: Simple derived query
pass_str: |
SELECT * FROM (
SELECT 1
) as a
test_pass_derived_query_requires_alias_2:
# Case 2: Derived query uses set operation (UNION)
pass_str: |
SELECT * FROM (
SELECT col FROM dbo.tab
UNION
SELECT -1 AS col
) AS a
test_pass_derived_query_requires_alias_3:
# Case 3: Derived query includes a WITH statement
pass_str: |
SELECT * FROM (
WITH foo AS (
SELECT col FROM dbo.tab
)
SELECT * FROM foo
) AS a

0 comments on commit b66071c

Please sign in to comment.