Skip to content

Commit

Permalink
Better date function for tsql (#1636)
Browse files Browse the repository at this point in the history
* Better date function parsing for T-SQL

* Add rule test case

* Add additional case

* Update function name

* Wording
  • Loading branch information
tunetheweb committed Oct 14, 2021
1 parent 8619f79 commit 6794b96
Show file tree
Hide file tree
Showing 8 changed files with 429 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/sqlfluff/dialects/dialect_ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ class FunctionSegment(BaseSegment):
match_grammar = OneOf(
Sequence(
Sequence(
Ref("DateAddFunctionNameSegment"),
Ref("DatePartFunctionNameSegment"),
Bracketed(
Delimited(
Ref("DatePartClause"),
Expand All @@ -987,7 +987,7 @@ class FunctionSegment(BaseSegment):
Ref("FunctionNameSegment"),
max_times=1,
min_times=1,
exclude=Ref("DateAddFunctionNameSegment"),
exclude=Ref("DatePartFunctionNameSegment"),
),
Bracketed(
Ref(
Expand Down Expand Up @@ -3142,7 +3142,7 @@ class DropSequenceStatementSegment(BaseSegment):


@ansi_dialect.segment()
class DateAddFunctionNameSegment(BaseSegment):
class DatePartFunctionNameSegment(BaseSegment):
"""DATEADD function name segment.
Need to be able to specify this as type function_name
Expand Down
6 changes: 3 additions & 3 deletions src/sqlfluff/dialects/dialect_exasol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3412,7 +3412,7 @@ class FunctionSegment(BaseSegment):
match_grammar = OneOf(
Sequence(
Sequence(
Ref("DateAddFunctionNameSegment"),
Ref("DatePartFunctionNameSegment"),
Bracketed(
Ref(
"FunctionContentsGrammar",
Expand All @@ -3430,7 +3430,7 @@ class FunctionSegment(BaseSegment):
Ref("FunctionNameSegment"),
max_times=1,
min_times=1,
exclude=Ref("DateAddFunctionNameSegment"),
exclude=Ref("DatePartFunctionNameSegment"),
),
Bracketed(
Ref(
Expand All @@ -3447,7 +3447,7 @@ class FunctionSegment(BaseSegment):


@exasol_dialect.segment(replace=True)
class DateAddFunctionNameSegment(BaseSegment):
class DatePartFunctionNameSegment(BaseSegment):
"""DATEADD function name segment.
Need to be able to specify this as type function_name
Expand Down
2 changes: 1 addition & 1 deletion src/sqlfluff/dialects/dialect_redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@


@redshift_dialect.segment(replace=True)
class DateAddFunctionNameSegment(BaseSegment):
class DatePartFunctionNameSegment(BaseSegment):
"""DATEADD function name segment.
Override to support DATEDIFF as well
Expand Down
15 changes: 13 additions & 2 deletions src/sqlfluff/dialects/dialect_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ class FunctionSegment(BaseSegment):
match_grammar = OneOf(
Sequence(
Sequence(
Ref("DateAddFunctionNameSegment"),
Ref("DatePartFunctionNameSegment"),
Bracketed(
Delimited(
Ref("DatePartClause"),
Expand Down Expand Up @@ -992,7 +992,7 @@ class FunctionSegment(BaseSegment):
# List of special functions handled differently
Ref("CastFunctionNameSegment"),
Ref("ConvertFunctionNameSegment"),
Ref("DateAddFunctionNameSegment"),
Ref("DatePartFunctionNameSegment"),
Ref("WithinGroupFunctionNameSegment"),
),
),
Expand Down Expand Up @@ -1465,3 +1465,14 @@ class DropStatementSegment(BaseSegment):
Ref("DelimiterSegment", optional=True),
],
)


@tsql_dialect.segment(replace=True)
class DatePartFunctionNameSegment(BaseSegment):
"""DATEADD function name segment.
Override to support DATEDIFF as well
"""

type = "function_name"
match_grammar = OneOf("DATEADD", "DATEDIFF", "DATEDIFF_BIG", "DATENAME")
3 changes: 3 additions & 0 deletions src/sqlfluff/dialects/dialect_tsql_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@
"COLUMNSTORE",
"D",
"DATEADD",
"DATEDIFF",
"DATEDIFF_BIG",
"DATENAME",
"DAY",
"DAYOFYEAR",
"DD",
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/dialects/tsql/select_date_functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SELECT
[hello],
DATEDIFF(day, [mydate], GETDATE()) AS [test],
DATEPART(day, [mydate], GETDATE()) AS [test2],
DATEDIFF(year, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(quarter, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(month, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(dayofyear, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(day, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(week, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(hour, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(minute, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(second, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(millisecond, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF(microsecond, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEDIFF_BIG(microsecond, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'),
DATEADD(year,2147483647, '20060731'),
DATEADD(year,-2147483647, '20060731'),
DATENAME(year, '12:10:30.123'),
DATENAME(month, '12:10:30.123'),
DATENAME(day, '12:10:30.123'),
DATENAME(dayofyear, '12:10:30.123'),
DATENAME(weekday, '12:10:30.123')
FROM
mytable;

0 comments on commit 6794b96

Please sign in to comment.