RF01: false positive when an alias/table reference differs in letter case from its declaration
Search before asking
- I searched the issues and found no similar issues.
What Happened
RF01 (references.from) reports a false-positive "reference not found" when
a table alias or table name is referenced using a different letter case than
it was declared with, even though the two spellings denote the same
identifier under the dialect's own identifier-casing rules.
SELECT CI.id
FROM source_table AS ci
JOIN second_table AS b ON ci.id = b.id;On dialect = ansi this raises:
Reference 'CI.id' refers to table/view not found in the FROM clause or found
in ancestor statement.even though ci/CI are the same identifier under ansi's own upper-folding
rule for unquoted identifiers, and the query is valid, executable SQL. The
same false positive reproduces for an unaliased table reference (e.g.
SELECT SOURCE_TABLE.id FROM source_table ...), and for other dialects
regardless of which way they fold (verified upper-folding ansi/trino/
tsql and lower-folding postgres).
Expected Behaviour
An unquoted alias/table reference should resolve case-insensitively,
consistent with the dialect's own identifier-casefold rules -- the same way
sibling rules RF02/RF07/ST07 already compare identifiers (via
raw_normalized()), and the same way real databases treat unquoted
identifiers as case-insensitive. A quoted identifier should keep matching
case-sensitively, since most dialects do not casefold those.
Observed Behaviour
RF01 flags the reference as undefined instead of resolving it, in every
case where the reference's case differs from the alias/table's declared
case. Reversing which side is upper/lower-case still fails, confirming this
isn't about one direction (declared-vs-used) but that identifier comparisons
inside RF01 never case-fold at all.
How to reproduce
$ sqlfluff lint --dialect ansi -
SELECT CI.id
FROM source_table AS ci
JOIN second_table AS b ON ci.id = b.id;
^DAlso reproduces with the alias/reference case direction reversed, with an
unaliased table (SELECT SOURCE_TABLE.id FROM source_table;), and on
postgres/trino/tsql.
Dialect
ansi (reproduces on every dialect RF01 runs against by default; verified
ansi, postgres, trino, tsql).
Version
sqlfluff, version 4.3.0 (current main, commit e2d032239). Python 3.12.3.
Configuration
Default configuration, RF01 enabled (its default state).
Impact and validation
Impact: contract-violation — RF01's job is to distinguish valid FROM-clause
references from undefined ones, and it gets this wrong for identifiers the
dialect itself treats as equivalent.
Entry point / who reaches it: any user running sqlfluff lint/fix with
RF01 enabled (its default state) on SQL where an unquoted alias or table
name is referenced with different letter case than it was declared with.
This is triggered via the plain CLI/API lint path, no special configuration
needed.
Negative control: reverting the fix and re-running
pytest test/rules/yaml_test_cases_test.py -k RF01 -q gives 3 failed, 116 passed; with the fix restored, 119 passed.
This issue was drafted with the help of an AI coding assistant (Claude Code).
AI-assisted: locating the root cause and drafting this report. Manually
validated: I reproduced the false positive myself by directly running
sqlfluff lint against the queries above on four dialects with opposite
identifier-folding directions, and I manually validated which identifier
types each dialect casefolds by reading the dialect definitions myself.
I will open a PR fixing this shortly.
Signed-off-by: Udaya Tejas [email protected]
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
- I agree to follow this project's Code of Conduct
Source: sqlfluff/sqlfluff