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

TSQL: Allow for !>, !< operators #1640

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/sqlfluff/dialects/dialect_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
Indent,
AnyNumberOf,
CommentSegment,
StringParser,
SymbolSegment,
)

from sqlfluff.core.dialects import load_raw_dialect
Expand Down Expand Up @@ -74,6 +76,9 @@
CommentSegment,
segment_kwargs={"trim_start": ("--")},
),
# Patching to add !<, !>
RegexLexer("greater_than_or_equal", ">=|!<", CodeSegment),
RegexLexer("less_than_or_equal", "<=|!>", CodeSegment),
]
)

Expand All @@ -88,9 +93,27 @@
QuotedLiteralSegmentWithN=NamedParser(
"single_quote_with_n", CodeSegment, name="quoted_literal", type="literal"
),
NotGreaterThanSegment=StringParser(
"!>", SymbolSegment, name="less_than_equal_to", type="comparison_operator"
),
NotLessThanSegment=StringParser(
"!<", SymbolSegment, name="greater_than_equal_to", type="comparison_operator"
),
)

tsql_dialect.replace(
ComparisonOperatorGrammar=OneOf(
Ref("EqualsSegment"),
Ref("GreaterThanSegment"),
Ref("LessThanSegment"),
Ref("GreaterThanOrEqualToSegment"),
Ref("LessThanOrEqualToSegment"),
Ref("NotEqualToSegment_a"),
Ref("NotEqualToSegment_b"),
Ref("LikeOperatorSegment"),
Ref("NotGreaterThanSegment"),
Ref("NotLessThanSegment"),
),
SingleIdentifierGrammar=OneOf(
Ref("NakedIdentifierSegment"),
Ref("QuotedIdentifierSegment"),
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/dialects/tsql/select.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--For testing valid select clause elements
SELECT
CASE WHEN 1 = 1 THEN 'True'
WHEN 1 > 1 THEN 'False'
WHEN 1 < 1 THEN 'False'
WHEN 1 >= 1 THEN 'True'
WHEN 1 <= 1 THEN 'True'
WHEN 1 <> 1 THEN 'False'
WHEN 1 !< 1 THEN 'Why is this a thing?'
WHEN 1 != 1 THEN 'False'
WHEN 1 !> 1 THEN 'NULL Handling, Probably'
ELSE 'Silly Tests'
END
92 changes: 92 additions & 0 deletions test/fixtures/dialects/tsql/select.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# YML test files are auto-generated from SQL files and should not be edited by
# hand. To help enforce this, the "hash" field in the file must match a hash
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: 7a3d7a550f46510b3607368ba5d59bd5cbbbc183c7e48dcf23d6761ee6e39161
file:
batch:
statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
expression:
case_expression:
- keyword: CASE
- keyword: WHEN
- expression:
- literal: '1'
- comparison_operator: '='
- literal: '1'
- keyword: THEN
- expression:
literal: "'True'"
- keyword: WHEN
- expression:
- literal: '1'
- comparison_operator: '>'
- literal: '1'
- keyword: THEN
- expression:
literal: "'False'"
- keyword: WHEN
- expression:
- literal: '1'
- comparison_operator: <
- literal: '1'
- keyword: THEN
- expression:
literal: "'False'"
- keyword: WHEN
- expression:
- literal: '1'
- comparison_operator: '>='
- literal: '1'
- keyword: THEN
- expression:
literal: "'True'"
- keyword: WHEN
- expression:
- literal: '1'
- comparison_operator: <=
- literal: '1'
- keyword: THEN
- expression:
literal: "'True'"
- keyword: WHEN
- expression:
- literal: '1'
- comparison_operator: <>
- literal: '1'
- keyword: THEN
- expression:
literal: "'False'"
- keyword: WHEN
- expression:
- literal: '1'
- comparison_operator: '!<'
- literal: '1'
- keyword: THEN
- expression:
literal: "'Why is this a thing?'"
- keyword: WHEN
- expression:
- literal: '1'
- comparison_operator: '!='
- literal: '1'
- keyword: THEN
- expression:
literal: "'False'"
- keyword: WHEN
- expression:
- literal: '1'
- comparison_operator: '!>'
- literal: '1'
- keyword: THEN
- expression:
literal: "'NULL Handling, Probably'"
- keyword: ELSE
- expression:
literal: "'Silly Tests'"
- keyword: END