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

T-SQL: ALTER FUNCTION/PROCEDURE/VIEW parsing #3867

Merged
merged 2 commits into from Sep 24, 2022
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
14 changes: 7 additions & 7 deletions src/sqlfluff/dialects/dialect_tsql.py
Expand Up @@ -1560,13 +1560,13 @@ class CreateFunctionStatementSegment(BaseSegment):
https://docs.snowflake.com/en/sql-reference/sql/create-function.html
https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver15
https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-function-transact-sql?view=sql-server-ver15
"""

type = "create_function_statement"

match_grammar = Sequence(
"CREATE",
Sequence("OR", "ALTER", optional=True),
OneOf("CREATE", "ALTER", Sequence("CREATE", "OR", "ALTER")),
"FUNCTION",
Ref("ObjectReferenceSegment"),
Ref("FunctionParameterListGrammar"),
Expand Down Expand Up @@ -1830,13 +1830,13 @@ class CreateProcedureStatementSegment(BaseSegment):
"""A `CREATE OR ALTER PROCEDURE` statement.

https://docs.microsoft.com/en-us/sql/t-sql/statements/create-procedure-transact-sql?view=sql-server-ver15
https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-procedure-transact-sql?view=sql-server-ver15
"""

type = "create_procedure_statement"

match_grammar = Sequence(
"CREATE",
Sequence("OR", "ALTER", optional=True),
OneOf("CREATE", "ALTER", Sequence("CREATE", "OR", "ALTER")),
OneOf("PROCEDURE", "PROC"),
Ref("ObjectReferenceSegment"),
Indent,
Expand Down Expand Up @@ -1880,13 +1880,13 @@ class CreateViewStatementSegment(BaseSegment):
"""A `CREATE VIEW` statement.

Adjusted to allow CREATE OR ALTER instead of CREATE OR REPLACE.
# https://docs.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql?view=sql-server-ver15#examples
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql?view=sql-server-ver15#examples
https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-view-transact-sql?view=sql-server-ver15#examples
"""

type = "create_view_statement"
match_grammar = Sequence(
"CREATE",
Sequence("OR", "ALTER", optional=True),
OneOf("CREATE", "ALTER", Sequence("CREATE", "OR", "ALTER")),
"VIEW",
Ref("ObjectReferenceSegment"),
Bracketed(
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/dialects/tsql/create_function.sql
Expand Up @@ -27,5 +27,8 @@ BEGIN
END;
GO

CREATE FUNCTION F (@DATE as datetime) RETURNS INT AS BEGIN RETURN 0 END;
CREATE OR ALTER FUNCTION F (@DATE as datetime) RETURNS INT AS BEGIN RETURN 1 END;
GO

ALTER FUNCTION F (@DATE as datetime) RETURNS INT AS BEGIN RETURN 0 END;
GO
36 changes: 35 additions & 1 deletion 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: e8d6e7c005fdc570dc9a08ccd787476aa3adff9797cdb618e173430f4e60b207
_hash: f5e00002a7c59c889b4a283b8c4cec06a04eb2247fa14abd2f6cada29edcbb12
file:
- batch:
statement:
Expand Down Expand Up @@ -324,6 +324,40 @@ file:
statement:
create_function_statement:
- keyword: CREATE
- keyword: OR
- keyword: ALTER
- keyword: FUNCTION
- object_reference:
naked_identifier: F
- function_parameter_list:
bracketed:
start_bracket: (
parameter: '@DATE'
keyword: as
data_type:
data_type_identifier: datetime
end_bracket: )
- keyword: RETURNS
- data_type:
data_type_identifier: INT
- keyword: AS
- procedure_statement:
statement:
begin_end_block:
- keyword: BEGIN
- statement:
return_segment:
keyword: RETURN
expression:
numeric_literal: '1'
- keyword: END
statement_terminator: ;
- go_statement:
keyword: GO
- batch:
statement:
create_function_statement:
- keyword: ALTER
- keyword: FUNCTION
- object_reference:
naked_identifier: F
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/dialects/tsql/create_view_with_set_statements.sql
Expand Up @@ -4,9 +4,23 @@ GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [DEST].[V_DIFFERENCE_NURSING_HOME__INFECTED_LOCATIONS_TOTAL_PER_REGION] AS

SELECT TOP 1 DATE_OF_REPORT
FROM BASE_CTE;

GO

CREATE OR ALTER VIEW [DEST].[V_DIFFERENCE_NURSING_HOME__INFECTED_LOCATIONS_TOTAL_PER_REGION] AS

SELECT TOP 1 DATE_OF_REPORT
FROM BASE_CTE
ORDER BY DATE_OF_REPORT;

GO


ALTER VIEW [DEST].[V_DIFFERENCE_NURSING_HOME__INFECTED_LOCATIONS_TOTAL_PER_REGION] AS

SELECT DATE_OF_REPORT
,NEW_DATE_OF_REPORT_UNIX AS NEW_DATE_UNIX
Expand Down
67 changes: 66 additions & 1 deletion test/fixtures/dialects/tsql/create_view_with_set_statements.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: 656520aa2e7522804e41ecca3c15d63d0b2b577a15e343308a7f904d97128eaf
_hash: 3c4376ca88135168a4d7c082dc67d118bb7bf79ea8f244951072a2c4c99409e7
file:
- batch:
statement:
Expand All @@ -21,6 +21,36 @@ file:
- keyword: 'ON'
- go_statement:
keyword: GO
- batch:
statement:
create_view_statement:
- keyword: CREATE
- keyword: VIEW
- object_reference:
- quoted_identifier: '[DEST]'
- dot: .
- quoted_identifier: '[V_DIFFERENCE_NURSING_HOME__INFECTED_LOCATIONS_TOTAL_PER_REGION]'
- keyword: AS
- select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
keyword: TOP
expression:
numeric_literal: '1'
select_clause_element:
column_reference:
naked_identifier: DATE_OF_REPORT
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: BASE_CTE
statement_terminator: ;
- go_statement:
keyword: GO
- batch:
statement:
create_view_statement:
Expand All @@ -33,6 +63,41 @@ file:
- dot: .
- quoted_identifier: '[V_DIFFERENCE_NURSING_HOME__INFECTED_LOCATIONS_TOTAL_PER_REGION]'
- keyword: AS
- select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
keyword: TOP
expression:
numeric_literal: '1'
select_clause_element:
column_reference:
naked_identifier: DATE_OF_REPORT
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: BASE_CTE
orderby_clause:
- keyword: ORDER
- keyword: BY
- column_reference:
naked_identifier: DATE_OF_REPORT
statement_terminator: ;
- go_statement:
keyword: GO
- batch:
statement:
create_view_statement:
- keyword: ALTER
- keyword: VIEW
- object_reference:
- quoted_identifier: '[DEST]'
- dot: .
- quoted_identifier: '[V_DIFFERENCE_NURSING_HOME__INFECTED_LOCATIONS_TOTAL_PER_REGION]'
- keyword: AS
- select_statement:
select_clause:
- keyword: SELECT
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/dialects/tsql/stored_procedure_begin_end.sql
Expand Up @@ -21,7 +21,7 @@ SELECT '8'
END;
GO

CREATE PROCEDURE [dbo].[usp_process_tran_log]
CREATE OR ALTER PROCEDURE [dbo].[usp_process_tran_log]
@out_vchCode uddt_output_code OUTPUT
, @out_vchMsg uddt_output_msg OUT
, @in_debug INT = 1 READONLY
Expand All @@ -33,7 +33,7 @@ SELECT '8'
END;
GO

CREATE PROCEDURE [dbo].[usp_process_tran_log]
ALTER PROCEDURE [dbo].[usp_process_tran_log]
@out_vchCode uddt_output_code OUTPUT
, @out_vchMsg uddt_output_msg OUTPUT
, @in_debug INT = 1
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/dialects/tsql/stored_procedure_begin_end.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: bda275337a514353248e0a73540dcd239b19ae80144f7563bb385e709b301be6
_hash: f9e1fac5b61620be692a6788ab340558c42aea8eaf0f8cb0122995c92557adf7
file:
- batch:
create_procedure_statement:
Expand Down Expand Up @@ -109,6 +109,8 @@ file:
- batch:
create_procedure_statement:
- keyword: CREATE
- keyword: OR
- keyword: ALTER
- keyword: PROCEDURE
- object_reference:
- quoted_identifier: '[dbo]'
Expand Down Expand Up @@ -156,7 +158,7 @@ file:
keyword: GO
- batch:
create_procedure_statement:
- keyword: CREATE
- keyword: ALTER
- keyword: PROCEDURE
- object_reference:
- quoted_identifier: '[dbo]'
Expand Down