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

SparkSQL: Mark AS as optional keyword for CTE & CTS #4127

Merged
merged 14 commits into from Dec 10, 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
2 changes: 1 addition & 1 deletion src/sqlfluff/dialects/dialect_ansi.py
Expand Up @@ -2378,7 +2378,7 @@ class CTEDefinitionSegment(BaseSegment):
match_grammar: Matchable = Sequence(
Ref("SingleIdentifierGrammar"),
Ref("CTEColumnList", optional=True),
"AS",
Ref.keyword("AS", optional=True),
Bracketed(
# Ephemeral here to subdivide the query.
Ref("SelectableGrammar", ephemeral_name="SelectableGrammar")
Expand Down
2 changes: 1 addition & 1 deletion src/sqlfluff/dialects/dialect_sparksql.py
Expand Up @@ -1107,7 +1107,7 @@ class CreateTableStatementSegment(ansi.CreateTableStatementSegment):
Dedent,
# Create AS syntax:
Sequence(
"AS",
Ref.keyword("AS", optional=True),
OptionallyBracketed(Ref("SelectableGrammar")),
optional=True,
),
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/dialects/ansi/with_insert_statement_a.sql
@@ -1,3 +1,4 @@
-- with insert statement with `AS` keyword
WITH mycte AS (
SELECT
foo,
Expand All @@ -8,3 +9,15 @@ WITH mycte AS (

INSERT INTO table2 (column1, column2, column3)
VALUES ('value1', 'value2', 'value3');

-- with statement without `AS` keyword
WITH mycte (
SELECT
foo,
bar,
baz
FROM mytable1
)

INSERT INTO table2 (column1, column2, column3)
VALUES ('value1', 'value2', 'value3');
65 changes: 62 additions & 3 deletions test/fixtures/dialects/ansi/with_insert_statement_a.yml
Expand Up @@ -3,9 +3,9 @@
# 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: 901525959391d20d72328da170a5095362855cc7191616b96e2282176c97b876
_hash: ef68a2a74be445c8c3d4fac7628a5039e825b39471a35a2190c7cfb2ed58e3d7
file:
statement:
- statement:
with_compound_statement:
keyword: WITH
common_table_expression:
Expand Down Expand Up @@ -64,4 +64,63 @@ file:
- expression:
quoted_literal: "'value3'"
- end_bracket: )
statement_terminator: ;
- statement_terminator: ;
- statement:
with_compound_statement:
keyword: WITH
common_table_expression:
naked_identifier: mycte
bracketed:
start_bracket: (
select_statement:
select_clause:
- keyword: SELECT
- select_clause_element:
column_reference:
naked_identifier: foo
- comma: ','
- select_clause_element:
column_reference:
naked_identifier: bar
- comma: ','
- select_clause_element:
column_reference:
naked_identifier: baz
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: mytable1
end_bracket: )
insert_statement:
- keyword: INSERT
- keyword: INTO
- table_reference:
naked_identifier: table2
- bracketed:
- start_bracket: (
- column_reference:
naked_identifier: column1
- comma: ','
- column_reference:
naked_identifier: column2
- comma: ','
- column_reference:
naked_identifier: column3
- end_bracket: )
- values_clause:
keyword: VALUES
bracketed:
- start_bracket: (
- expression:
quoted_literal: "'value1'"
- comma: ','
- expression:
quoted_literal: "'value2'"
- comma: ','
- expression:
quoted_literal: "'value3'"
- end_bracket: )
- statement_terminator: ;
6 changes: 6 additions & 0 deletions test/fixtures/dialects/sparksql/create_table_select.sql
@@ -0,0 +1,6 @@
-- create table select without `AS` keyword
CREATE TABLE tab1 SELECT * FROM tab2;


-- create table select with `AS` keyword
CREATE TABLE tab1 AS SELECT * FROM tab2;
50 changes: 50 additions & 0 deletions test/fixtures/dialects/sparksql/create_table_select.yml
@@ -0,0 +1,50 @@
# 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: 0c638fbc846086000ecd6a0d36354551fe8ed4377abce53076ab6ab93b98bd53
file:
- statement:
create_table_statement:
- keyword: CREATE
- keyword: TABLE
- table_reference:
naked_identifier: tab1
- select_statement:
select_clause:
keyword: SELECT
select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: tab2
- statement_terminator: ;
- statement:
create_table_statement:
- keyword: CREATE
- keyword: TABLE
- table_reference:
naked_identifier: tab1
- keyword: AS
- select_statement:
select_clause:
keyword: SELECT
select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: tab2
- statement_terminator: ;