Skip to content

Commit

Permalink
T-SQL: Parse DATEPART date type as date type instead of column name (
Browse files Browse the repository at this point in the history
…#3681)

Co-authored-by: Greg Finley <gregory.finley@gmail.com>
Co-authored-by: Barry Pollard <barry_pollard@hotmail.com>
  • Loading branch information
3 people committed Jul 27, 2022
1 parent 7a40bf6 commit 3030af7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/sqlfluff/dialects/dialect_tsql.py
Expand Up @@ -86,7 +86,7 @@

tsql_dialect.sets("date_part_function_name").clear()
tsql_dialect.sets("date_part_function_name").update(
["DATEADD", "DATEDIFF", "DATEDIFF_BIG", "DATENAME"]
["DATEADD", "DATEDIFF", "DATEDIFF_BIG", "DATENAME", "DATEPART"]
)

tsql_dialect.insert_lexer_matchers(
Expand Down
98 changes: 41 additions & 57 deletions test/fixtures/dialects/tsql/create_function.yml
Expand Up @@ -3,7 +3,7 @@
# 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: f78dbf5872c29abdcfe06775bc5c22755fad8596b185c6de7b73b032c9a3a5e2
_hash: fb5668d4692a023cbc141d281e70e350027273f62325d15d8d755a7f1c10321b
file:
- batch:
statement:
Expand Down Expand Up @@ -54,27 +54,23 @@ file:
function_name:
function_name_identifier: DATEPART
bracketed:
- start_bracket: (
- expression:
column_reference:
identifier: wk
- comma: ','
- expression:
start_bracket: (
date_part: wk
comma: ','
expression:
parameter: '@DATE'
- end_bracket: )
end_bracket: )
- binary_operator: +
- literal: '1'
- binary_operator: '-'
- function:
function_name:
function_name_identifier: DATEPART
bracketed:
- start_bracket: (
- expression:
column_reference:
identifier: wk
- comma: ','
- expression:
start_bracket: (
date_part: wk
comma: ','
expression:
function:
function_name:
keyword: CAST
Expand All @@ -85,14 +81,12 @@ file:
function_name:
function_name_identifier: DATEPART
bracketed:
- start_bracket: (
- expression:
column_reference:
identifier: yy
- comma: ','
- expression:
start_bracket: (
date_part: yy
comma: ','
expression:
parameter: '@DATE'
- end_bracket: )
end_bracket: )
keyword: as
data_type:
data_type_identifier: CHAR
Expand All @@ -104,7 +98,7 @@ file:
end_bracket: )
binary_operator: +
literal: "'0104'"
- end_bracket: )
end_bracket: )
statement_terminator: ;
- statement:
if_then_statement:
Expand Down Expand Up @@ -145,14 +139,12 @@ file:
function_name:
function_name_identifier: DATEPART
bracketed:
- start_bracket: (
- expression:
column_reference:
identifier: yy
- comma: ','
- expression:
start_bracket: (
date_part: yy
comma: ','
expression:
parameter: '@DATE'
- end_bracket: )
end_bracket: )
binary_operator: '-'
literal: '1'
keyword: AS
Expand All @@ -179,14 +171,12 @@ file:
function_name:
function_name_identifier: DATEPART
bracketed:
- start_bracket: (
- expression:
column_reference:
identifier: DAY
- comma: ','
- expression:
start_bracket: (
date_part: DAY
comma: ','
expression:
parameter: '@DATE'
- end_bracket: )
end_bracket: )
keyword: AS
data_type:
data_type_identifier: CHAR
Expand Down Expand Up @@ -215,14 +205,12 @@ file:
function_name:
function_name_identifier: DATEPART
bracketed:
- start_bracket: (
- expression:
column_reference:
identifier: mm
- comma: ','
- expression:
start_bracket: (
date_part: mm
comma: ','
expression:
parameter: '@DATE'
- end_bracket: )
end_bracket: )
comparison_operator:
raw_comparison_operator: '='
literal: '12'
Expand All @@ -238,27 +226,23 @@ file:
function_name:
function_name_identifier: DATEPART
bracketed:
- start_bracket: (
- expression:
column_reference:
identifier: dd
- comma: ','
- expression:
start_bracket: (
date_part: dd
comma: ','
expression:
parameter: '@DATE'
- end_bracket: )
end_bracket: )
- binary_operator: '-'
- function:
function_name:
function_name_identifier: DATEPART
bracketed:
- start_bracket: (
- expression:
column_reference:
identifier: dw
- comma: ','
- expression:
start_bracket: (
date_part: dw
comma: ','
expression:
parameter: '@DATE'
- end_bracket: )
end_bracket: )
end_bracket: )
comparison_operator:
- raw_comparison_operator: '>'
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/dialects/tsql/datepart.sql
@@ -0,0 +1,2 @@
SELECT DATEPART(DW, my_table.date) AS dayofweek
FROM my_table;
37 changes: 37 additions & 0 deletions test/fixtures/dialects/tsql/datepart.yml
@@ -0,0 +1,37 @@
# 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: 6178c02c375d73445a741139a586236270723a4390dbdd51c1f7b58d1a9b9f6c
file:
batch:
statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: DATEPART
bracketed:
start_bracket: (
date_part: DW
comma: ','
expression:
column_reference:
- identifier: my_table
- dot: .
- identifier: date
end_bracket: )
alias_expression:
keyword: AS
identifier: dayofweek
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
statement_terminator: ;
6 changes: 2 additions & 4 deletions test/fixtures/dialects/tsql/select_date_functions.yml
Expand Up @@ -3,7 +3,7 @@
# 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: 301d40a2421d58284616bf1d1ce005637ac62374ecedb67f8c5fe08547a0eff8
_hash: 6a04c3b0a3cf43d6c427104da2692dfd66c82ddd26bffb08a25c4b22a546ebbe
file:
batch:
statement:
Expand Down Expand Up @@ -44,9 +44,7 @@ file:
function_name_identifier: DATEPART
bracketed:
- start_bracket: (
- expression:
column_reference:
identifier: day
- date_part: day
- comma: ','
- expression:
column_reference:
Expand Down

0 comments on commit 3030af7

Please sign in to comment.