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

L029: Optionally check quoted identifiers in addition to naked identifiers #1775

Merged
merged 11 commits into from
Oct 31, 2021

Conversation

jpers36
Copy link
Contributor

@jpers36 jpers36 commented Oct 28, 2021

Fixes #1747

This PR adjusts L029 to optionally check quoted identifiers in addition to naked identifiers.
In order to do this, we are adding a new configuration keyword, quoted_identifiers_policy, which represents the same thing as unquoted_identifiers_policy but for quoted identifiers.
The options for quoted_identifiers_policy are the same as unquoted_identifiers_policy with the addition of none, representing no checks for quoted identifiers.

We check quoted identifiers by stripping off the first and last character. If we have a case where this shortcut would fail, we'll need to tweak the code before merging.

Changes:
+quoted_identifiers_policy default of none to default_config.cfg
+quoted_identifiers_policy description in config_info.py
L014.py: Rename unquoted_ids_policy_applicable function to identifiers_policy_applicable in order to reflect change in functionality
L029.py: Reflect renaming of unquoted_ids_policy_applicable function; add quoted_identifiers_policy as a configuration keyword; adjust rule to check quoted identifiers based on the config setting.
L029.yml: Rename test cases to be more descriptive. Add quoted identifier test cases.

@tunetheweb
Copy link
Member

Not sure I agree with this change. The whole point of reserved words are that they can’t be used unless quoted. While I agree that might be best practice I don’t think this should be added to rule L029 by default. It should either be a configurable extra, or a whole new rule IMHO so it can be enabled/disabled independently.

@jpers36
Copy link
Contributor Author

jpers36 commented Oct 28, 2021

@tunetheweb I can set it up as configurable. Something like quoted_identifiers_policy to match the existing unquoted_identifiers_policy?

@tunetheweb
Copy link
Member

Maybe. Will it offer the same options? Or would should it just be on/off or true/false? In which case something like include_quoted_identiifers might be a better name?

But can you explain a little more the rational why you think this shouldn’t be used? Is it invalid in T-SQL? Or is there some other reason?

@jpers36
Copy link
Contributor Author

jpers36 commented Oct 28, 2021

I think it's a good general rule to avoid using reserved or unreserved keywords for other things, even when bracketed/quoted. It reduces ambiguity. The specific case that I really wish to avoid is creating a view or table that uses a keyword as a column. That means mandated bracketing/quoting whenever we reference that column if it's a reserved keyword, and lack of clarity at the least when it's an unreserved keyword.

@codecov
Copy link

codecov bot commented Oct 29, 2021

Codecov Report

Merging #1775 (9631365) into main (69b5c1f) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #1775   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          138       138           
  Lines         9792      9794    +2     
=========================================
+ Hits          9792      9794    +2     
Impacted Files Coverage Δ
src/sqlfluff/core/rules/config_info.py 100.00% <ø> (ø)
src/sqlfluff/rules/L014.py 100.00% <100.00%> (ø)
src/sqlfluff/rules/L029.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 69b5c1f...9631365. Read the comment docs.

@jpers36 jpers36 marked this pull request as ready for review October 29, 2021 16:15
@jpers36 jpers36 requested a review from barrywhart October 29, 2021 16:15
Copy link
Member

@barrywhart barrywhart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small suggestion, but looks pretty good to me.

@tunetheweb: Would you like to review as well? Holding off approving until I hear from you.

src/sqlfluff/rules/L014.py Show resolved Hide resolved
@tunetheweb
Copy link
Member

@tunetheweb: Would you like to review as well? Holding off approving until I hear from you.

Won’t be near a computer for next few days so happy to go with your approval.

One slight nit from a quick look on phone is all test cases are t-sql, which uses different quotes than most. Might be good to have one ANSI I’ve too. But other than that great set of test cases.

@barrywhart
Copy link
Member

@jpers36: The other reviewer suggested adding at least one test case that doesn't use TSQL. Could you add an ANSI test case to address that?

This raises another question -- are there any databases where quotes are more than one character long? If so, code like this wouldn't work correctly:

context.segment.raw.upper()[1:-1]

Copy link
Member

@barrywhart barrywhart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request for one new test case. Also a potential question about potentially other dialects using oddball quotes.

@jpers36
Copy link
Contributor Author

jpers36 commented Oct 29, 2021

@barrywhart regarding the single character questions, I just investigated all currently-supported databases and confirmed they all use single characters for quote/delimited identifiers. Working on the rest of the recommendations.

@jpers36 jpers36 requested a review from barrywhart October 29, 2021 20:13
Copy link
Member

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this is good to merge. @barrywhart anything holding it up from your end?

@tunetheweb tunetheweb merged commit 6ae2181 into sqlfluff:main Oct 31, 2021
@tunetheweb tunetheweb changed the title L029: Check quoted identifiers in addition to naked identifiers L029: Optionally check quoted identifiers in addition to naked identifiers Nov 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New Rule Suggestion: Identify and flag object names that are reserved words
3 participants