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

Spark3: Support for EXPLAIN statement #2767

Merged
merged 3 commits into from Mar 2, 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
4 changes: 1 addition & 3 deletions src/sqlfluff/dialects/dialect_ansi.py
Expand Up @@ -3374,9 +3374,7 @@ class ExplainStatementSegment(BaseSegment):
Ref("DeleteStatementSegment"),
)

match_grammar = StartsWith("EXPLAIN")

parse_grammar = Sequence(
match_grammar = Sequence(
"EXPLAIN",
explainable_stmt,
)
Expand Down
8 changes: 4 additions & 4 deletions src/sqlfluff/dialects/dialect_postgres.py
Expand Up @@ -1097,9 +1097,7 @@ class ExplainStatementSegment(BaseSegment):

type = "explain_statement"

match_grammar = ansi_dialect.get_segment("ExplainStatementSegment").match_grammar

parse_grammar = Sequence(
match_grammar = Sequence(
"EXPLAIN",
OneOf(
Sequence(
Expand All @@ -1113,7 +1111,9 @@ class ExplainStatementSegment(BaseSegment):
Bracketed(Delimited(Ref("ExplainOptionSegment"))),
optional=True,
),
ansi_dialect.get_segment("ExplainStatementSegment").explainable_stmt,
ansi_dialect.get_segment(
"ExplainStatementSegment",
).explainable_stmt,
)


Expand Down
2 changes: 1 addition & 1 deletion src/sqlfluff/dialects/dialect_snowflake.py
Expand Up @@ -2938,7 +2938,7 @@ class ExplainStatementSegment(
https://docs.snowflake.com/en/sql-reference/sql/explain.html
"""

parse_grammar = Sequence(
match_grammar = Sequence(
"EXPLAIN",
Sequence(
"USING",
Expand Down
28 changes: 28 additions & 0 deletions src/sqlfluff/dialects/dialect_spark3.py
Expand Up @@ -1679,6 +1679,34 @@ class TransformClauseSegment(BaseSegment):
)


@spark3_dialect.segment(replace=True)
class ExplainStatementSegment(BaseSegment):
"""An `Explain` statement.

Enhanced from ANSI dialect to allow for additonal parameters.

EXPLAIN [ EXTENDED | CODEGEN | COST | FORMATTED ] explainable_stmt

https://spark.apache.org/docs/latest/sql-ref-syntax-qry-explain.html
"""

type = "explain_statement"

explainable_stmt = Ref("StatementSegment")

match_grammar = Sequence(
"EXPLAIN",
OneOf(
"EXTENDED",
"CODEGEN",
"COST",
"FORMATTED",
optional=True,
),
explainable_stmt,
)


# Auxiliary Statements
@spark3_dialect.segment()
class AddExecutablePackage(BaseSegment):
Expand Down
63 changes: 63 additions & 0 deletions test/fixtures/dialects/spark3/explain.sql
@@ -0,0 +1,63 @@
EXPLAIN SELECT
a,
b
FROM person;

EXPLAIN SELECT TRANSFORM (zip_code, name, age)
USING 'cat' AS (a, b, c)
FROM person
WHERE zip_code > 94511;

EXPLAIN ALTER DATABASE inventory SET DBPROPERTIES (
'Edited-by' = 'John'
);

EXPLAIN ALTER TABLE student RENAME TO studentinfo;

EXPLAIN ALTER VIEW view_identifier RENAME TO view_identifier;

EXPLAIN CREATE DATABASE IF NOT EXISTS database_name
COMMENT "database_comment"
LOCATION "root/database_directory"
WITH DBPROPERTIES ( "property_name" = "property_value");

EXPLAIN CREATE OR REPLACE TEMPORARY FUNCTION IF NOT EXISTS
function_name AS "class_name" USING FILE "resource_locations";

EXPLAIN CREATE TABLE student (id INT, student_name STRING, age INT) USING CSV;

EXPLAIN
CREATE TABLE student (id INT, student_name STRING, age INT)
STORED AS ORC;

EXPLAIN CREATE TABLE student_dupli LIKE student
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS TEXTFILE
TBLPROPERTIES ('owner' = 'xxxx');

EXPLAIN
CREATE VIEW experienced_employee_extended
AS SELECT a
FROM experienced_employee;

EXPLAIN DROP DATABASE IF EXISTS dbname;

EXPLAIN DROP FUNCTION test_avg;

EXPLAIN USE database_name;

EXPLAIN TRUNCATE TABLE student PARTITION(age = 10);

EXPLAIN MSCK REPAIR TABLE table_identifier ADD PARTITIONS;

EXPLAIN REFRESH TABLE tbl1;

EXPLAIN REFRESH FUNCTION func1;

EXPLAIN LOAD DATA LOCAL INPATH '/user/hive/warehouse/students'
OVERWRITE INTO TABLE test_load;

EXPLAIN INSERT INTO TABLE students VALUES
('Amy Smith', '123 Park Ave, San Jose', 111111);

EXPLAIN DROP VIEW IF EXISTS view_identifier;