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: function DATEPART: date type is parsed as column identifier #3630

Closed
2 of 3 tasks
tkachenkomaria244 opened this issue Jul 18, 2022 · 1 comment · Fixed by #3681
Closed
2 of 3 tasks

TSQL: function DATEPART: date type is parsed as column identifier #3630

tkachenkomaria244 opened this issue Jul 18, 2022 · 1 comment · Fixed by #3681
Labels
bug Something isn't working good first issue Good for newcomers t-sql Issues related to the T-SQL/TSQL/Transact SQL dialect

Comments

@tkachenkomaria244
Copy link
Contributor

Search before asking

  • I searched the issues and found no similar issues.

What Happened

With TSQL dialect first parameter in function DATEPART is parsed as "identifier" segment instead of "date_part" segment.
This causes violation of L028 rules during linting.
datepart-transact-sql

Expected Behaviour

No L028 rule violations.

In parsing structure this should be similar to DATEADD:

[L:  2, P:  5]      |                        function:
[L:  2, P:  5]      |                            function_name:
[L:  2, P:  5]      |                                function_name_identifier:     'dateadd'
[L:  2, P: 12]      |                            bracketed:
[L:  2, P: 12]      |                                start_bracket:                '('
[L:  2, P: 13]      |                                [META] indent:
[L:  2, P: 13]      |                                date_part:                    'dw'
[L:  2, P: 15]      |                                comma:                        ','
[L:  2, P: 16]      |                                whitespace:                   ' '
[L:  2, P: 17]      |                                expression:
[L:  2, P: 17]      |                                    column_reference:
[L:  2, P: 17]      |                                        identifier:           'p'
[L:  2, P: 18]      |                                        dot:                  '.'
[L:  2, P: 19]      |                                        identifier:           'start_date'
[L:  2, P: 29]      |                                [META] dedent:
[L:  2, P: 29]      |                                end_bracket:                  ')'
[L:  2, P: 30]      |                        whitespace:                           ' '

Observed Behaviour

== [D:\py_code\sqlfluff\real_tests\v_v.sql] FAIL
L:   2 | P:  14 | L028 | Unqualified reference 'dw' found in single table select.

parsing result for function:

[L:  2, P:  5]      |                        function:
[L:  2, P:  5]      |                            function_name:
[L:  2, P:  5]      |                                function_name_identifier:     'datepart'
[L:  2, P: 13]      |                            bracketed:
[L:  2, P: 13]      |                                start_bracket:                '('
[L:  2, P: 14]      |                                [META] indent:
[L:  2, P: 14]      |                                expression:
[L:  2, P: 14]      |                                    column_reference:
[L:  2, P: 14]      |                                        identifier:           'dw'
[L:  2, P: 16]      |                                comma:                        ','
[L:  2, P: 17]      |                                whitespace:                   ' '
[L:  2, P: 18]      |                                expression:
[L:  2, P: 18]      |                                    column_reference:
[L:  2, P: 18]      |                                        identifier:           'p'
[L:  2, P: 19]      |                                        dot:                  '.'
[L:  2, P: 20]      |                                        identifier:           'start_date'
[L:  2, P: 30]      |                                [META] dedent:
[L:  2, P: 30]      |                                end_bracket:                  ')'
[L:  2, P: 31]      |                        whitespace:                           ' '

How to reproduce

Run linting on SQL code:
sqlfluff lint D:\py_code\sqlfluff\real_tests\v_v.sql --rules L028 --config config\sqlfluff\.sqlfluff

select
    datepart(dw, p.start_date) as dayofweek
from
    p as p

Dialect

tsql

Version

sqlfluff, version 0.13.1
Python 3.9.6

Configuration

[sqlfluff]
sql_file_exts = .sql
dialect = tsql
templater = placeholder

[sqlfluff:indentation]
indented_joins = False
template_blocks_indent = True

[sqlfluff:rules]
tab_space_size = 4
max_line_length = 250
indent_unit = tab
comma_style = leading
single_table_references = qualified
allow_scalar = False

Are you willing to work on and submit a PR to address the issue?

  • Yes I am willing to submit a PR!

Code of Conduct

@tkachenkomaria244 tkachenkomaria244 added the bug Something isn't working label Jul 18, 2022
@tunetheweb tunetheweb added the t-sql Issues related to the T-SQL/TSQL/Transact SQL dialect label Jul 19, 2022
@tunetheweb
Copy link
Member

Should be as easy as adding DATEPART to date_part_function_name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers t-sql Issues related to the T-SQL/TSQL/Transact SQL dialect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants