Skip to content

Commit

Permalink
#902 Support USE statement in ANSI (#950)
Browse files Browse the repository at this point in the history
* Move USE statement from Snowflake to ANSI ⏪

* Add parser tests for USE statements in MySQL and Snowflake ✅

* Update CHANGELOG ✏️

* Fix due to decorator removed when merging
  • Loading branch information
Daniel Mateus Pires committed Apr 12, 2021
1 parent f32e63f commit cf15298
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix bug in L048 which flagged adjoining commas as failures.
- Fix bug in L019 with inline comments.
- Fix bug in L036 with multiple newlines.
- Support "USE" statement in ANSI ([#902](https://github.com/sqlfluff/sqlfluff/issues/902)).
- Parse explain statement ([#893](https://github.com/sqlfluff/sqlfluff/issues/893)).

## [0.5.1] - 2021-04-09
Expand Down
24 changes: 24 additions & 0 deletions src/sqlfluff/dialects/dialect_ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,7 @@ class StatementSegment(BaseSegment):
Ref("CreateModelStatementSegment"),
Ref("DropModelStatementSegment"),
Ref("DescribeStatementSegment"),
Ref("UseStatementSegment"),
Ref("ExplainStatementSegment"),
)

Expand Down Expand Up @@ -2600,6 +2601,29 @@ class DescribeStatementSegment(BaseSegment):
)


@ansi_dialect.segment()
class UseStatementSegment(BaseSegment):
"""A `USE` statement.
USE [ ROLE ] <name>
USE [ WAREHOUSE ] <name>
USE [ DATABASE ] <name>
USE [ SCHEMA ] [<db_name>.]<name>
"""

type = "use_statement"
match_grammar = StartsWith("USE")

parse_grammar = Sequence(
"USE",
OneOf("ROLE", "WAREHOUSE", "DATABASE", "SCHEMA", optional=True),
Ref("ObjectReferenceSegment"),
)


@ansi_dialect.segment()
class ExplainStatementSegment(BaseSegment):
"""An `Explain` statement.
Expand Down
17 changes: 0 additions & 17 deletions src/sqlfluff/dialects/dialect_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,23 +413,6 @@ class SelectStatementSegment(ansi_dialect.get_segment("SelectStatementSegment"))
)


@snowflake_dialect.segment()
class UseStatementSegment(BaseSegment):
"""A snowflake `USE` statement.
https://docs.snowflake.com/en/sql-reference/sql/use.html
"""

type = "use_statement"
match_grammar = StartsWith("USE")

parse_grammar = Sequence(
"USE",
OneOf("ROLE", "WAREHOUSE", "DATABASE", "SCHEMA", optional=True),
Ref("ObjectReferenceSegment"),
)


@snowflake_dialect.segment()
class CreateCloneStatementSegment(BaseSegment):
"""A snowflake `CREATE ... CLONE` statement.
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/parser/mysql/use_database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use my_db;
7 changes: 7 additions & 0 deletions test/fixtures/parser/mysql/use_database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file:
statement:
use_statement:
keyword: use
object_reference:
identifier: my_db
statement_terminator: ;
7 changes: 7 additions & 0 deletions test/fixtures/parser/snowflake/snowflake_use.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use role my_role;

use warehouse my_warehouse;

use database my_database;

use schema my_schema;
29 changes: 29 additions & 0 deletions test/fixtures/parser/snowflake/snowflake_use.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
file:
- statement:
use_statement:
- keyword: use
- keyword: role
- object_reference:
identifier: my_role
- statement_terminator: ;
- statement:
use_statement:
- keyword: use
- keyword: warehouse
- object_reference:
identifier: my_warehouse
- statement_terminator: ;
- statement:
use_statement:
- keyword: use
- keyword: database
- object_reference:
identifier: my_database
- statement_terminator: ;
- statement:
use_statement:
- keyword: use
- keyword: schema
- object_reference:
identifier: my_schema
- statement_terminator: ;

0 comments on commit cf15298

Please sign in to comment.