Skip to content

TSQL: L025: Code corruption -- alias removed by accident #3966

@fmms

Description

@fmms

Search before asking

  • I searched the issues and found no similar issues.

What Happened

I have a query similar to the following one:

SELECT ROW_NUMBER() OVER(PARTITION BY a.object_id ORDER BY a.object_id)
FROM sys.objects a
CROSS JOIN sys.objects b
CROSS JOIN sys.objects c

Expected Behaviour

Query is fixed properly and can still be executed.

Observed Behaviour

sqlfluff thinks the aliases can be removed:

PS > sqlfluff fix --config .\.sqlfluff .\query.sql
==== finding fixable violations ====
== [query.sql] FAIL
L:   3 | P:  24 | L025 | Alias 'b' is never used in SELECT statement.
L:   4 | P:  24 | L025 | Alias 'c' is never used in SELECT statement.

The aliases b and c are removed and thus the query is not valid anymore.

Msg 1013, Level 16, State 1, Line 1
The objects "sys.objects" and "sys.objects" in the FROM clause have the same exposed names. Use correlation names to distinguish them.

How to reproduce

Try to fix the following query with tsql dialect:

SELECT ROW_NUMBER() OVER(PARTITION BY a.object_id ORDER BY a.object_id)
FROM sys.objects a
CROSS JOIN sys.objects b
CROSS JOIN sys.objects c

Dialect

tsql

Version

1.3.2

Configuration

nothing special

Are you willing to work on and submit a PR to address the issue?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingrule bugA rule is not working as intended, either missing errors or incorrectly highlighting non-errorst-sqlIssues related to the T-SQL/TSQL/Transact SQL dialect

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions