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

Fix parsing types and add check to test in future #2652

Merged
merged 8 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/sqlfluff/dialects/dialect_ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3058,6 +3058,7 @@ class SetClauseSegment(BaseSegment):
class FunctionDefinitionGrammar(BaseSegment):
"""This is the body of a `CREATE FUNCTION AS` statement."""

type = "function_definition"
match_grammar = Sequence(
"AS",
Ref("QuotedLiteralSegment"),
Expand Down Expand Up @@ -3113,6 +3114,7 @@ class CreateFunctionStatementSegment(BaseSegment):
class FunctionParameterListGrammar(BaseSegment):
"""The parameters for a function ie. `(string, number)`."""

type = "function_parameter_list"
# Function parameter list
match_grammar = Bracketed(
Delimited(
Expand Down
2 changes: 2 additions & 0 deletions src/sqlfluff/dialects/dialect_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ class FunctionSegment(BaseSegment):
class FunctionDefinitionGrammar(BaseSegment):
"""This is the body of a `CREATE FUNCTION AS` statement."""

type = "function_definition"
match_grammar = Sequence(
AnyNumberOf(
Sequence(
Expand Down Expand Up @@ -599,6 +600,7 @@ class FunctionParameterListGrammar(BaseSegment):

# Function parameter list. Note that the only difference from the ANSI
# grammar is that BigQuery provides overrides bracket_pairs_set.
type = "function_parameter_list"
match_grammar = Bracketed(
Delimited(
Ref("FunctionParameterGrammar"),
Expand Down
2 changes: 2 additions & 0 deletions src/sqlfluff/dialects/dialect_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ class CreateProcedureStatementSegment(BaseSegment):
class FunctionDefinitionGrammar(BaseSegment):
"""This is the body of a `CREATE FUNCTION` statement."""

type = "function_definition"
match_grammar = Ref("TransactionStatementSegment")


Expand Down Expand Up @@ -848,6 +849,7 @@ class AlterTableStatementSegment(BaseSegment):
class ProcedureParameterListGrammar(BaseSegment):
"""The parameters for a procedure ie. `(in/out/inout name datatype)`."""

type = "procedure_parameter_list"
match_grammar = Bracketed(
Delimited(
Ref("ProcedureParameterGrammar"),
Expand Down
3 changes: 3 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ class FunctionDefinitionGrammar(BaseSegment):
https://www.postgresql.org/docs/13/sql-createfunction.html
"""

type = "function_definition"
match_grammar = Sequence(
AnyNumberOf(
Ref("LanguageClauseSegment"),
Expand Down Expand Up @@ -2212,6 +2213,7 @@ class PartitionBoundSpecSegment(BaseSegment):
As per https://www.postgresql.org/docs/13/sql-altertable.html.
"""

type = "partition_bound_segment"
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
match_grammar = OneOf(
Sequence(
"IN",
Expand Down Expand Up @@ -2322,6 +2324,7 @@ class TableConstraintUsingIndexSegment(BaseSegment):
As specified in: https://www.postgresql.org/docs/13/sql-altertable.html.
"""

type = "table_constraint_segment"
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
match_grammar = Sequence(
Sequence( # [ CONSTRAINT <Constraint name> ]
"CONSTRAINT", Ref("ObjectReferenceSegment"), optional=True
Expand Down
3 changes: 3 additions & 0 deletions src/sqlfluff/dialects/dialect_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ class ValuesClauseSegment(BaseSegment):
class FunctionDefinitionGrammar(BaseSegment):
"""This is the body of a `CREATE FUNCTION AS` statement."""

type = "function_definition"
match_grammar = Sequence(
"AS",
OneOf(Ref("QuotedLiteralSegment"), Ref("DollarQuotedLiteralSegment")),
Expand Down Expand Up @@ -3359,6 +3360,7 @@ class DescribeStatementSegment(BaseSegment):
https://docs.snowflake.com/en/sql-reference/sql/desc.html
"""

type = "describe_statement"
match_grammar = Sequence(
OneOf("DESCRIBE", "DESC"),
OneOf(
Expand Down Expand Up @@ -3535,6 +3537,7 @@ class TransactionStatementSegment(BaseSegment):
https://docs.snowflake.com/en/sql-reference/sql/rollback.html
"""

type = "transaction_statement_segment"
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
match_grammar = OneOf(
Sequence(
"BEGIN",
Expand Down
1 change: 1 addition & 0 deletions src/sqlfluff/dialects/dialect_spark3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,7 @@ class RefreshFunctionStatementSegment(BaseSegment):
class StatementSegment(BaseSegment):
"""Overriding StatementSegment to allow for additional segment parsing."""

type = "statement"
match_grammar = ansi_dialect.get_segment("StatementSegment").match_grammar.copy()

parse_grammar = ansi_dialect.get_segment("StatementSegment").parse_grammar.copy(
Expand Down
1 change: 1 addition & 0 deletions src/sqlfluff/dialects/dialect_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2969,6 +2969,7 @@ class PostTableExpressionGrammar(BaseSegment):
https://docs.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table?view=sql-server-ver15
"""

type = "post_table_expression_grammar"
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
match_grammar = Sequence(
Sequence("WITH", optional=True),
Bracketed(
Expand Down
9 changes: 5 additions & 4 deletions src/sqlfluff/rules/L052.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
break
except ValueError: # pragma: no cover
pass
else:
self.logger.error(
"Unable to identify statement terminated by %r", context.segment
)
elif memory["ended_statements"]:
# Unless it's an empty statement we shouldn't reach here
raise ValueError(
f"Unable to identify statement terminated by {context.segment!r}"
) # pragma: no cover

# Locate semicolon and search back over the raw stack
# to find the end of the preceding statement.
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/dialects/ansi/create_function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
# 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: 561d6ce111232861a4bab77ecf23c9cba4b2ed492c6f1538b35f4c59a2e579f4
_hash: 464777673d070691ae11a20dc3510745429b36256e61c9269c0b247409701c7a
file:
statement:
create_function_statement:
- keyword: CREATE
- keyword: FUNCTION
- function_name:
function_name_identifier: add
- base:
- function_parameter_list:
bracketed:
- start_bracket: (
- data_type:
Expand All @@ -23,7 +23,7 @@ file:
- keyword: RETURNS
- data_type:
data_type_identifier: integer
- base:
- function_definition:
- keyword: AS
- literal: "'select $1 + $2;'"
- keyword: LANGUAGE
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/dialects/ansi/create_function_no_args.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
# 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: f23c156e64e82141c241dbef04aba9cc6d118493c80e4f7e266da13145682b96
_hash: 19e4092a664affa9d51bbe9365080cbc88e9003c5b17dc2c9259f549b2bc03e2
file:
statement:
create_function_statement:
- keyword: CREATE
- keyword: FUNCTION
- function_name:
function_name_identifier: add
- base:
- function_parameter_list:
bracketed:
start_bracket: (
end_bracket: )
- keyword: RETURNS
- data_type:
data_type_identifier: integer
- base:
- function_definition:
- keyword: AS
- literal: "'select $1 + $2;'"
- keyword: LANGUAGE
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/dialects/ansi/create_zero_argument_function.yml
Original file line number Diff line number Diff line change
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: 70f3b48d6b9e3569b60dbe7b4860155a93958fb624d2cd0f554c2010a6523abb
_hash: 519f26f25605bc9c2ea3d93c0758bb28d57354f445d527cbb2ffc53a9260f9ed
file:
- statement:
create_function_statement:
Expand All @@ -13,14 +13,14 @@ file:
- keyword: FUNCTION
- function_name:
function_name_identifier: a
- base:
- function_parameter_list:
bracketed:
start_bracket: (
end_bracket: )
- keyword: RETURNS
- data_type:
data_type_identifier: integer
- base:
- function_definition:
- keyword: AS
- literal: "'\n SELECT 1;\n'"
- keyword: LANGUAGE
Expand All @@ -32,14 +32,14 @@ file:
- keyword: FUNCTION
- function_name:
function_name_identifier: a
- base:
- function_parameter_list:
bracketed:
start_bracket: (
end_bracket: )
- keyword: RETURNS
- data_type:
data_type_identifier: integer
- base:
- function_definition:
- keyword: AS
- literal: "'\n SELECT 1;\n'"
- keyword: LANGUAGE
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/dialects/bigquery/create_function_no_args.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
# 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: fa012a4e84dfe072b317e5e5ace48d266ab98c320b14d91491ff85e1f5c33243
_hash: 71492f64ce880f27b02655dd780bd1c63233f5c037c5c3a0fe99aa0611be442e
file:
statement:
create_function_statement:
- keyword: CREATE
- keyword: FUNCTION
- function_name:
function_name_identifier: add
- base:
- function_parameter_list:
bracketed:
start_bracket: (
end_bracket: )
- keyword: RETURNS
- data_type:
data_type_identifier: integer
- base:
- function_definition:
- keyword: AS
- udf_body: "'select $1 + $2;'"
- keyword: LANGUAGE
Expand Down
Original file line number Diff line number Diff line change
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: 4dd3e99e4321a5c62fecad2e1703d9c001452eab142905bbb364c177a361b78b
_hash: 2a9fb25f37a507a705e3e12b3cb8a48cac10c161235b595779629e6d1577cd46
file:
statement:
create_function_statement:
Expand All @@ -12,7 +12,7 @@ file:
- keyword: FUNCTION
- function_name:
function_name_identifier: qs
- base:
- function_parameter_list:
bracketed:
- start_bracket: (
- parameter: foo1
Expand Down Expand Up @@ -87,7 +87,7 @@ file:
data_type_identifier: INT64
end_angle_bracket: '>'
end_angle_bracket: '>'
- base:
- function_definition:
- keyword: LANGUAGE
- parameter: js
- keyword: AS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
# 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: f4691f284b1f46350285c6dc91a444e7e4198352ba0e960b8c0ef8f2d1d9411d
_hash: 7f2513b695990453252859cc64731f0c691a79916219ee57e43a3bdc67f41aa8
file:
statement:
create_function_statement:
- keyword: CREATE
- keyword: FUNCTION
- function_name:
function_name_identifier: qs
- base:
- function_parameter_list:
bracketed:
start_bracket: (
parameter: y
data_type:
data_type_identifier: STRING
end_bracket: )
- base:
- function_definition:
- keyword: DETERMINISTIC
- keyword: LANGUAGE
- parameter: js
Expand Down
Original file line number Diff line number Diff line change
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: 65b77a68deca7c57c1220bb182f25aa394e4959608b3b637bfc4789c19537125
_hash: 74febd125c1eb7fe516c95ef631e854e7c83649e5b3f32a1ec417a20e23d07ce
file:
statement:
create_function_statement:
Expand All @@ -12,7 +12,7 @@ file:
- keyword: FUNCTION
- function_name:
function_name_identifier: parseTopSellers
- base:
- function_parameter_list:
bracketed:
start_bracket: (
parameter: arr_str
Expand All @@ -35,7 +35,7 @@ file:
data_type_identifier: FLOAT64
- end_angle_bracket: '>'
end_angle_bracket: '>'
- base:
- function_definition:
- keyword: LANGUAGE
- parameter: js
- keyword: OPTIONS
Expand Down
Original file line number Diff line number Diff line change
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: fe1d8471b24961aea092895d11ae6a6bfc12fdfe321c7b29caf58bbbd84f8312
_hash: a93c7bb9defb272cbc85dcc80feea185c38a95cdfa7c272f4bdbee43d6ac5f61
file:
statement:
create_function_statement:
Expand All @@ -12,7 +12,7 @@ file:
- keyword: FUNCTION
- function_name:
function_name_identifier: qs
- base:
- function_parameter_list:
bracketed:
start_bracket: (
parameter: y
Expand All @@ -31,7 +31,7 @@ file:
data_type_identifier: INT64
end_angle_bracket: '>'
end_angle_bracket: '>'
- base:
- function_definition:
- keyword: LANGUAGE
- parameter: js
- keyword: AS
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/dialects/bigquery/create_js_function_simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
# 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: 96f88fcc9be589d88561ea790a1deb4ed794c11d9f0279ece2eedcd28cdc7dcd
_hash: 72b8e6ab4471c0c42038231a70bd81ba9ec1773a23875d126a643e655e7c0eec
file:
statement:
create_function_statement:
- keyword: CREATE
- keyword: FUNCTION
- function_name:
function_name_identifier: qs
- base:
- function_parameter_list:
bracketed:
start_bracket: (
parameter: y
data_type:
data_type_identifier: STRING
end_bracket: )
- base:
- function_definition:
- keyword: LANGUAGE
- parameter: js
- keyword: AS
Expand Down
Loading