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

Postgres & T-SQL: Drop Function #1924

Merged
merged 7 commits into from Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
24 changes: 24 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Expand Up @@ -474,6 +474,29 @@ class CreateFunctionStatementSegment(BaseSegment):
)


@postgres_dialect.segment()
class DropFunctionStatementSegment(BaseSegment):
"""A `DROP FUNCTION` statement.

As per the specification: https://www.postgresql.org/docs/14/sql-dropfunction.html
"""

type = "drop_function_statement"

match_grammar = Sequence(
"DROP",
"FUNCTION",
Ref("IfExistsGrammar", optional=True),
Delimited(
Sequence(
Ref("FunctionNameSegment"),
Ref("FunctionParameterListGrammar", optional=True),
)
),
OneOf("CASCADE", "RESTRICT", optional=True),
)


@postgres_dialect.segment()
class WellKnownTextGeometrySegment(BaseSegment):
"""A Data Type Segment to identify Well Known Text Geometric Data Types.
Expand Down Expand Up @@ -2141,6 +2164,7 @@ class StatementSegment(BaseSegment):
Ref("CreateTableAsStatementSegment"),
Ref("AlterTriggerStatementSegment"),
Ref("SetStatementSegment"),
Ref("DropFunctionStatementSegment"),
],
)

Expand Down
19 changes: 19 additions & 0 deletions src/sqlfluff/dialects/dialect_tsql.py
Expand Up @@ -248,6 +248,7 @@ class StatementSegment(ansi_dialect.get_segment("StatementSegment")): # type: i
Ref("DropStatisticsStatementSegment"),
Ref("DropProcedureStatementSegment"),
Ref("UpdateStatisticsStatementSegment"),
Ref("DropFunctionStatementSegment"),
],
)

Expand Down Expand Up @@ -875,6 +876,24 @@ class CreateFunctionStatementSegment(BaseSegment):
)


@tsql_dialect.segment()
class DropFunctionStatementSegment(BaseSegment):
"""A `DROP FUNCTION` statement.

As per specification https://docs.microsoft.com/en-us/sql/t-sql/statements/drop-function-transact-sql?view=sql-server-ver15
"""

type = "drop_function_statement"

match_grammar = Sequence(
"DROP",
"FUNCTION",
Ref("IfExistsGrammar", optional=True),
Delimited(Ref("FunctionNameSegment")),
Ref("DelimiterSegment", optional=True),
)


@tsql_dialect.segment()
class SetStatementSegment(BaseSegment):
"""A Set statement.
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/dialects/postgres/postgres_drop_function.sql
@@ -0,0 +1,5 @@
DROP FUNCTION sqrt(integer);
DROP FUNCTION sqrt(integer), sqrt(bigint);
DROP FUNCTION update_employee_salaries;
DROP FUNCTION update_employee_salaries();
DROP FUNCTION IF EXISTS foo (IN my_var integer, VARIADIC my_var_2 text);
82 changes: 82 additions & 0 deletions test/fixtures/dialects/postgres/postgres_drop_function.yml
@@ -0,0 +1,82 @@
# 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: c03cd688fad76ef981e529184fbc95c3da919d6e393d39d53e1c9a8ba6b8bb23
file:
- statement:
drop_function_statement:
- keyword: DROP
- keyword: FUNCTION
- function_name:
function_name_identifier: sqrt
- base:
bracketed:
start_bracket: (
data_type:
data_type_identifier: integer
end_bracket: )
- statement_terminator: ;
- statement:
drop_function_statement:
- keyword: DROP
- keyword: FUNCTION
- function_name:
function_name_identifier: sqrt
- base:
bracketed:
start_bracket: (
data_type:
data_type_identifier: integer
end_bracket: )
- comma: ','
- function_name:
function_name_identifier: sqrt
- base:
bracketed:
start_bracket: (
data_type:
data_type_identifier: bigint
end_bracket: )
- statement_terminator: ;
- statement:
drop_function_statement:
- keyword: DROP
- keyword: FUNCTION
- function_name:
function_name_identifier: update_employee_salaries
- statement_terminator: ;
- statement:
drop_function_statement:
- keyword: DROP
- keyword: FUNCTION
- function_name:
function_name_identifier: update_employee_salaries
- base:
bracketed:
start_bracket: (
end_bracket: )
- statement_terminator: ;
- statement:
drop_function_statement:
- keyword: DROP
- keyword: FUNCTION
- keyword: IF
- keyword: EXISTS
- function_name:
function_name_identifier: foo
- base:
bracketed:
- start_bracket: (
- keyword: IN
- parameter: my_var
- data_type:
data_type_identifier: integer
- comma: ','
- keyword: VARIADIC
- parameter: my_var_2
- data_type:
data_type_identifier: text
- end_bracket: )
- statement_terminator: ;
2 changes: 2 additions & 0 deletions test/fixtures/dialects/tsql/drop_function.sql
@@ -0,0 +1,2 @@
DROP FUNCTION Sales.fn_SalesByStore;
DROP FUNCTION IF EXISTS sales, sales2;
29 changes: 29 additions & 0 deletions test/fixtures/dialects/tsql/drop_function.yml
@@ -0,0 +1,29 @@
# 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: d14f2b6b16bb2743a5c4f8439be4a40e30bf5a63afca8622686674ef4445013c
file:
batch:
- statement:
drop_function_statement:
- keyword: DROP
- keyword: FUNCTION
- function_name:
identifier: Sales
dot: .
function_name_identifier: fn_SalesByStore
- statement_terminator: ;
- statement:
drop_function_statement:
- keyword: DROP
- keyword: FUNCTION
- keyword: IF
- keyword: EXISTS
- function_name:
function_name_identifier: sales
- comma: ','
- function_name:
function_name_identifier: sales2
- statement_terminator: ;