-
-
Notifications
You must be signed in to change notification settings - Fork 755
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
Simplify rule creation by adding a functional API to RuleContext #2126
Simplify rule creation by adding a functional API to RuleContext #2126
Conversation
Codecov Report
@@ Coverage Diff @@
## main #2126 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 150 155 +5
Lines 10886 10988 +102
==========================================
+ Hits 10886 10988 +102
Continue to review full report at Codecov.
|
@jpy-git, @tunetheweb: Please take a preliminary look at this. I'm not looking for a typical "pre-merge" review. This is more of a design review:
Note that some of the functions on the new classes are just stubs (they raise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@barrywhart Like it a lot! 😎 Added my initial thoughts/questions. See if there's anything you think we should change off the back of that and then I can maybe try applying this to L009 to get a better idea of the user experience 👍
https://github.com/barrywhart/sqlfluff into bhart-issue_1808_surrogates_to_simplify_rule_creation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple more random thoughts to add, shaping up nicely!
Will have a more in depth look at the edited rules tomorrow 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks great - docs aren't building properly at the moment but otherwise love it 👍
docs/source/rules.rst
Outdated
* `L001` | ||
* `L008` | ||
* `L009` | ||
* `L013` | ||
* `L015` | ||
* `L017` | ||
* `L021` | ||
* `L025` | ||
* `L031` | ||
* `L035` | ||
* `L036` | ||
* `L038` | ||
* `L042` | ||
* `L043` | ||
* `L047` | ||
* `L050` | ||
* `L052` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it makes sense to keep this list given how many you've converted. It will just end up out of date. If we want to still give some measure of scale, but without having to maintain the list, it might be better to say something like: ...at 2021-12-30, 17 rules use this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.
docs/source/rules.rst
Outdated
.. autoclass:: sqlfluff.core.rules.functional.segments | ||
:members: | ||
|
||
.. autoclass:: sqlfluff.core.rules.functional.segment_predicates | ||
:members: | ||
|
||
The `_eval` function of each rule should take enough arguments that it can | ||
evaluate the position of the given segment in relation to its neighbors, | ||
and that the segment which finally "triggers" the error, should be the one | ||
that would be corrected OR if the rule relates to something that is missing, | ||
then it should flag on the segment FOLLOWING, the place that the desired | ||
element is missing. | ||
.. autoclass:: sqlfluff.core.rules.functional.raw_file_slices | ||
:members: | ||
|
||
.. autoclass:: sqlfluff.core.rules.functional.raw_file_slice_predicates | ||
:members: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes incoming...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@barrywhart - can you push your most recent changes? I can't see them on the branch. I'll give them a spin when they're up.
@alanmcruickshank: Thanks for checking the docs! I hadn't built and previewed those yet. Should be good now. |
@alanmcruickshank: Okay, the fixes are up now. (Earlier, I had accidentally pushed them to the wrong branch.) |
https://github.com/barrywhart/sqlfluff into bhart-issue_1808_surrogates_to_simplify_rule_creation
@barrywhart I'm conscious that things are still getting added to this PR and we still haven't reviewed the awesome-pattern-matching dependency. Looking at the repo it seems like quite a small project? This issue here mentions there may be a lot of overlap with the matching introduced in Python 3.10 I feel like maybe it's best to introduce the functional API and it's rule changes/documentation in this PR, as there are no concerns there and it's been pretty thoroughly reviewed, and separate the pattern matching stuff into it's own PR afterwards. They're not really related, other than both touching rules, and would be good to have any discussion on apm separated for future reference. I can see that the functional API has been linked to several issues so we should get that merged to unblock them. |
@jpy-git: That's definitely valid feedback. I'm happy to remove the pattern matching stuff for now. I did take a look at the new built-in pattern matching in Python 3.10, and there is a backport (although the backport looks new and kind of unofficial). From my evaluation, the built-in pattern matching is less powerful, e.g. it doesn't support patterns with both leading and trailing "don't care" sections. It's also unclear if it supports matching across multiple levels of a tree structure. OTOH:
TL;DR I'll remove it from this PR. I may keep tinkering with it, possibly contributing to it. Among the packages out there, it comes closest to my dream of "regex-like power applied to arbitrary objects, plus support for patterns that span levels of a tree". (AFAICT, most of them aren't even trying to do one or both of these.) Part of the appeal is that the patterns closely resemble SQLFluff dialect definitions, so could be very approachable and intuitive for contributors. |
Makes sense, I def like the concept so it's worth investigating further afterwards 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small mypy cleanup otherwise good 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! 👍 Think we can get this merged if no one else has any objections? 🚀
Docs issue has been fixed :)
I went ahead and merged. I hope that was okay? 😬 @alanmcruickshank |
Brief summary of the change made
Makes progress on #1808
Are there any other side effects of this change that we should be aware of?
rules.rst
for the current list).L041
to useawesome-pattern-matching
packageThese changes generally make the rule code:
This style of code should be easier for heavy SQL users and mathematicians to author. 👍🏽💪🏽
Reviewers may also wish to look at this branch, which implements a new rule (suggested in #1799) using the new API. I thought it'd be a good test case to create a new rule, not just rewrite existing ones. Note that it's a potentially pretty tricky rule, but it lacks some of the typical "code smells" of past tricky rules, e.g. complex loops, lots of
if
statements, heavy use of segment indices.Pull Request checklist
Please confirm you have completed any of the necessary steps below.
Included test cases to demonstrate any code changes, which may be one or more of the following:
.yml
rule test cases intest/fixtures/rules/std_rule_cases
..sql
/.yml
parser test cases intest/fixtures/dialects
(note YML files can be auto generated withpython test/generate_parse_fixture_yml.py
or by runningtox
locally).test/fixtures/linter/autofix
.Added appropriate documentation for the change.
Created GitHub issues for any relevant followup/future enhancements if appropriate.