diff --git a/src/sqlfluff/dialects/dialect_exasol.py b/src/sqlfluff/dialects/dialect_exasol.py index 7fd99f56082..2c6bd5f3e65 100644 --- a/src/sqlfluff/dialects/dialect_exasol.py +++ b/src/sqlfluff/dialects/dialect_exasol.py @@ -390,9 +390,9 @@ class WithInvalidForeignKeySegment(BaseSegment): Ref("BracketedColumnReferenceListGrammar"), Dedent, # dedent for the indent in the select clause "FROM", - Ref("ObjectReferenceSegment"), + Ref("TableReferenceSegment"), "REFERENCING", - Ref("ObjectReferenceSegment"), + Ref("TableReferenceSegment"), Ref("BracketedColumnReferenceListGrammar", optional=True), ) @@ -403,7 +403,7 @@ class IntoTableSegment(BaseSegment): type = "into_table_clause" match_grammar = StartsWith(Sequence("INTO", "TABLE"), terminator="FROM") - parse_grammar = Sequence("INTO", "TABLE", Ref("ObjectReferenceSegment")) + parse_grammar = Sequence("INTO", "TABLE", Ref("TableReferenceSegment")) @exasol_dialect.segment(replace=True) @@ -3078,7 +3078,7 @@ class OpenSchemaSegment(BaseSegment): """`OPEN SCHEMA` statement.""" type = "open_schema_statement" - match_grammar = Sequence("OPEN", "SCHEMA", Ref("ObjectReferenceSegment")) + match_grammar = Sequence("OPEN", "SCHEMA", Ref("SchemaReferenceSegment")) @exasol_dialect.segment() @@ -3107,12 +3107,12 @@ class RecompressReorganizeSegment(BaseSegment): OneOf( Sequence( "TABLE", - Ref("ObjectReferenceSegment"), + Ref("TableReferenceSegment"), Ref("BracketedColumnReferenceListGrammar"), ), - Sequence("TABLES", Delimited(Ref("ObjectReferenceSegment"))), - Sequence("SCHEMA", Ref("ObjectReferenceSegment")), - Sequence("SCHEMAS", Delimited(Ref("ObjectReferenceSegment"))), + Sequence("TABLES", Delimited(Ref("TableReferenceSegment"))), + Sequence("SCHEMA", Ref("SchemaReferenceSegment")), + Sequence("SCHEMAS", Delimited(Ref("SchemaReferenceSegment"))), "DATABASE", ), Ref.keyword("ENFORCE", optional=True), @@ -3129,12 +3129,12 @@ class PreloadSegment(BaseSegment): OneOf( Sequence( "TABLE", - Ref("ObjectReferenceSegment"), + Ref("TableReferenceSegment"), Ref("BracketedColumnReferenceListGrammar"), ), - Sequence("TABLES", Delimited(Ref("ObjectReferenceSegment"))), - Sequence("SCHEMA", Ref("ObjectReferenceSegment")), - Sequence("SCHEMAS", Delimited(Ref("ObjectReferenceSegment"))), + Sequence("TABLES", Delimited(Ref("TableReferenceSegment"))), + Sequence("SCHEMA", Ref("SchemaReferenceSegment")), + Sequence("SCHEMAS", Delimited(Ref("SchemaReferenceSegment"))), "DATABASE", ), ) @@ -3214,7 +3214,7 @@ class ExecuteScriptSegment(BaseSegment): match_grammar = Sequence( "EXECUTE", "SCRIPT", - Ref("ObjectReferenceSegment"), + Ref("ScriptReferenceSegment"), Bracketed( Delimited(Ref.keyword("ARRAY", optional=True), Ref("ExpressionSegment")), optional=True, diff --git a/src/sqlfluff/dialects/dialect_hive.py b/src/sqlfluff/dialects/dialect_hive.py index 8338fe8c106..12fa345c690 100644 --- a/src/sqlfluff/dialects/dialect_hive.py +++ b/src/sqlfluff/dialects/dialect_hive.py @@ -507,3 +507,50 @@ class StatementSegment(ansi_dialect.get_segment("StatementSegment")): # type: i Ref("DropModelStatementSegment"), ], ) + + +@hive_dialect.segment(replace=True) +class InsertStatementSegment(BaseSegment): + """An `INSERT` statement. + + Full Apache Hive `INSERT` reference here: + https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML + """ + + type = "insert_statement" + match_grammar = StartsWith("INSERT") + parse_grammar = Sequence( + "INSERT", + OneOf( + Sequence( + "OVERWRITE", + OneOf( + Sequence( + "TABLE", + Ref("TableReferenceSegment"), + Ref("PartitionSpecGrammar", optional=True), + Ref("IfNotExistsGrammar", optional=True), + Ref("SelectableGrammar"), + ), + Sequence( + Sequence("LOCAL", optional=True), + "DIRECTORY", + Ref("SingleOrDoubleQuotedLiteralGrammar"), + Ref("RowFormatClauseSegment", optional=True), + Ref("StoredAsGrammar", optional=True), + Ref("SelectableGrammar"), + ), + ), + ), + Sequence( + "INTO", + "TABLE", + Ref("TableReferenceSegment"), + Ref("PartitionSpecGrammar", optional=True), + OneOf( + Ref("SelectableGrammar"), + Ref("ValuesClauseSegment"), + ), + ), + ), + ) diff --git a/test/fixtures/dialects/exasol/ExecuteScript.yml b/test/fixtures/dialects/exasol/ExecuteScript.yml index 989a2bc8483..e9cbeb814ed 100644 --- a/test/fixtures/dialects/exasol/ExecuteScript.yml +++ b/test/fixtures/dialects/exasol/ExecuteScript.yml @@ -3,20 +3,20 @@ # 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: 6681b20a2a0f43830205d4e154910b53cfb41982f4e1a437cdce9ef6ad8c9f09 +_hash: 996e085117024ed5ade66d3fb0c8e56e06a45e98e894f5bf8f38a64eeaa971bf file: - statement: execute_script_statement: - keyword: EXECUTE - keyword: SCRIPT - - object_reference: + - script_reference: identifier: script_1 - statement_terminator: ; - statement: execute_script_statement: - keyword: EXECUTE - keyword: SCRIPT - - object_reference: + - script_reference: identifier: script_1 - keyword: WITH - keyword: OUTPUT @@ -25,7 +25,7 @@ file: execute_script_statement: - keyword: EXECUTE - keyword: SCRIPT - - object_reference: + - script_reference: identifier: script_2 - bracketed: - start_bracket: ( @@ -45,7 +45,7 @@ file: execute_script_statement: - keyword: EXECUTE - keyword: SCRIPT - - object_reference: + - script_reference: identifier: script_3 - bracketed: start_bracket: ( diff --git a/test/fixtures/dialects/exasol/OpenCloseSchema.yml b/test/fixtures/dialects/exasol/OpenCloseSchema.yml index 19b70ba98a0..44bf0337672 100644 --- a/test/fixtures/dialects/exasol/OpenCloseSchema.yml +++ b/test/fixtures/dialects/exasol/OpenCloseSchema.yml @@ -3,20 +3,20 @@ # 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: 0a2e62784075b09001ce7e5f5664ff92edf451c5681f2238d7a40ce254d997ea +_hash: 451857e48ad7d4bea308e653fcb46058a99cec1bc416eeab1da865ef5ba1d6a6 file: - statement: open_schema_statement: - keyword: OPEN - keyword: SCHEMA - - object_reference: + - schema_reference: identifier: test - statement_terminator: ; - statement: open_schema_statement: - keyword: OPEN - keyword: SCHEMA - - object_reference: + - schema_reference: identifier: '"test"' - statement_terminator: ; - statement: diff --git a/test/fixtures/dialects/exasol/PreloadStatement.yml b/test/fixtures/dialects/exasol/PreloadStatement.yml index 4eb774e2c3e..a34ea4ef8a6 100644 --- a/test/fixtures/dialects/exasol/PreloadStatement.yml +++ b/test/fixtures/dialects/exasol/PreloadStatement.yml @@ -3,13 +3,13 @@ # 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: fc832df27cdfa5ec9952229ae0d8730944f1a63d0b6f92f57cd1649af6cd98f3 +_hash: 8ccdfc4bd57a103aa969a16ee2fe5629a5aafc35c388b9dd597eb941e28a7066 file: - statement: preload_statement: - keyword: PRELOAD - keyword: TABLE - - object_reference: + - table_reference: identifier: t - bracketed: start_bracket: ( @@ -26,27 +26,27 @@ file: preload_statement: - keyword: PRELOAD - keyword: TABLES - - object_reference: + - table_reference: identifier: t1 - comma: ',' - - object_reference: + - table_reference: identifier: t2 - statement_terminator: ; - statement: preload_statement: - keyword: PRELOAD - keyword: SCHEMAS - - object_reference: + - schema_reference: identifier: s1 - comma: ',' - - object_reference: + - schema_reference: identifier: s2 - statement_terminator: ; - statement: preload_statement: - keyword: PRELOAD - keyword: SCHEMA - - object_reference: + - schema_reference: identifier: s1 - statement_terminator: ; - statement: diff --git a/test/fixtures/dialects/exasol/RecompressStatement.yml b/test/fixtures/dialects/exasol/RecompressStatement.yml index c949de48f27..eb0a81ef901 100644 --- a/test/fixtures/dialects/exasol/RecompressStatement.yml +++ b/test/fixtures/dialects/exasol/RecompressStatement.yml @@ -3,13 +3,13 @@ # 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: 12717d423891499caf01b58d7ee219869c78cf89ef38ce5ca72305e1121c2a5f +_hash: 53a6f747c711b3806acd9c82f64dfd18084a610b473ce52227459a2817d741ce file: - statement: recompress_reorganize_statement: - keyword: RECOMPRESS - keyword: TABLE - - object_reference: + - table_reference: identifier: t1 - bracketed: start_bracket: ( @@ -21,10 +21,10 @@ file: recompress_reorganize_statement: - keyword: RECOMPRESS - keyword: TABLES - - object_reference: + - table_reference: identifier: t2 - comma: ',' - - object_reference: + - table_reference: identifier: t3 - keyword: ENFORCE - statement_terminator: ; diff --git a/test/fixtures/dialects/exasol/SelectStatement.yml b/test/fixtures/dialects/exasol/SelectStatement.yml index 4a9640d13ad..51addeea81a 100644 --- a/test/fixtures/dialects/exasol/SelectStatement.yml +++ b/test/fixtures/dialects/exasol/SelectStatement.yml @@ -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: c18d191e0be145ed6b398d233eaca31d5890e8a3bce7a725c72cdca21126215b +_hash: c779ff30a065b616a67966cb4168c174892973c083415a6b0168ace96a4dbdc0 file: - statement: select_statement: @@ -807,10 +807,10 @@ file: identifier: nr end_bracket: ) - keyword: from - - object_reference: + - table_reference: identifier: T1 - keyword: REFERENCING - - object_reference: + - table_reference: identifier: T2 - bracketed: start_bracket: ( @@ -839,10 +839,10 @@ file: identifier: name - end_bracket: ) - keyword: from - - object_reference: + - table_reference: identifier: T1 - keyword: REFERENCING - - object_reference: + - table_reference: identifier: T2 - statement_terminator: ; - statement: @@ -864,10 +864,10 @@ file: identifier: name - end_bracket: ) - keyword: from - - object_reference: + - table_reference: identifier: T1 - keyword: REFERENCING - - object_reference: + - table_reference: identifier: T2 - bracketed: - start_bracket: ( @@ -892,7 +892,7 @@ file: into_table_clause: - keyword: INTO - keyword: TABLE - - object_reference: + - table_reference: identifier: t2 from_clause: keyword: FROM diff --git a/test/fixtures/dialects/hive/insert_into_table.sql b/test/fixtures/dialects/hive/insert_into_table.sql new file mode 100644 index 00000000000..3dd4af1b2e3 --- /dev/null +++ b/test/fixtures/dialects/hive/insert_into_table.sql @@ -0,0 +1,2 @@ +INSERT INTO TABLE foo +SELECT a, b FROM bar; diff --git a/test/fixtures/dialects/hive/insert_into_table.yml b/test/fixtures/dialects/hive/insert_into_table.yml new file mode 100644 index 00000000000..700d7679359 --- /dev/null +++ b/test/fixtures/dialects/hive/insert_into_table.yml @@ -0,0 +1,32 @@ +# 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: d4bcf9077cf6a10011ca75dfeb29a04b441b9b34575c5c548ea7243571b7af32 +file: + statement: + insert_statement: + - keyword: INSERT + - keyword: INTO + - keyword: TABLE + - table_reference: + identifier: foo + - select_statement: + select_clause: + - keyword: SELECT + - select_clause_element: + column_reference: + identifier: a + - comma: ',' + - select_clause_element: + column_reference: + identifier: b + from_clause: + keyword: FROM + from_expression: + from_expression_element: + table_expression: + table_reference: + identifier: bar + statement_terminator: ; diff --git a/test/fixtures/dialects/hive/insert_into_table_partition.sql b/test/fixtures/dialects/hive/insert_into_table_partition.sql new file mode 100644 index 00000000000..1f90d2ba7ac --- /dev/null +++ b/test/fixtures/dialects/hive/insert_into_table_partition.sql @@ -0,0 +1,3 @@ +INSERT INTO TABLE foo +PARTITION (a='test_foo', b='test_bar') +SELECT a, b, c, d FROM bar; diff --git a/test/fixtures/dialects/hive/insert_into_table_partition.yml b/test/fixtures/dialects/hive/insert_into_table_partition.yml new file mode 100644 index 00000000000..01b3575c701 --- /dev/null +++ b/test/fixtures/dialects/hive/insert_into_table_partition.yml @@ -0,0 +1,53 @@ +# 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: 2b11170f6ae125eefc32024fbc81eb41ea5583cb28a75b91e9d69b4b7bd8e311 +file: + statement: + insert_statement: + - keyword: INSERT + - keyword: INTO + - keyword: TABLE + - table_reference: + identifier: foo + - keyword: PARTITION + - bracketed: + - start_bracket: ( + - column_reference: + identifier: a + - comparison_operator: '=' + - literal: "'test_foo'" + - comma: ',' + - column_reference: + identifier: b + - comparison_operator: '=' + - literal: "'test_bar'" + - end_bracket: ) + - select_statement: + select_clause: + - keyword: SELECT + - select_clause_element: + column_reference: + identifier: a + - comma: ',' + - select_clause_element: + column_reference: + identifier: b + - comma: ',' + - select_clause_element: + column_reference: + identifier: c + - comma: ',' + - select_clause_element: + column_reference: + identifier: d + from_clause: + keyword: FROM + from_expression: + from_expression_element: + table_expression: + table_reference: + identifier: bar + statement_terminator: ; diff --git a/test/fixtures/dialects/hive/insert_into_table_values.sql b/test/fixtures/dialects/hive/insert_into_table_values.sql new file mode 100644 index 00000000000..0c00752ef53 --- /dev/null +++ b/test/fixtures/dialects/hive/insert_into_table_values.sql @@ -0,0 +1,2 @@ +INSERT INTO TABLE foo +VALUES ('foo', 'bar'), ('rab', 'oof'); diff --git a/test/fixtures/dialects/hive/insert_into_table_values.yml b/test/fixtures/dialects/hive/insert_into_table_values.yml new file mode 100644 index 00000000000..2c262989eff --- /dev/null +++ b/test/fixtures/dialects/hive/insert_into_table_values.yml @@ -0,0 +1,30 @@ +# 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: bb6dbd2b9e8addb80366e239a45859ac8344fd7d076f44f5871aa6571eb94737 +file: + statement: + insert_statement: + - keyword: INSERT + - keyword: INTO + - keyword: TABLE + - table_reference: + identifier: foo + - values_clause: + - keyword: VALUES + - bracketed: + - start_bracket: ( + - literal: "'foo'" + - comma: ',' + - literal: "'bar'" + - end_bracket: ) + - comma: ',' + - bracketed: + - start_bracket: ( + - literal: "'rab'" + - comma: ',' + - literal: "'oof'" + - end_bracket: ) + statement_terminator: ; diff --git a/test/fixtures/dialects/hive/insert_overwrite_directory.sql b/test/fixtures/dialects/hive/insert_overwrite_directory.sql new file mode 100644 index 00000000000..5110df97226 --- /dev/null +++ b/test/fixtures/dialects/hive/insert_overwrite_directory.sql @@ -0,0 +1,2 @@ +INSERT OVERWRITE LOCAL DIRECTORY '/path' +SELECT a, b FROM foo; diff --git a/test/fixtures/dialects/hive/insert_overwrite_directory.yml b/test/fixtures/dialects/hive/insert_overwrite_directory.yml new file mode 100644 index 00000000000..a7bd3edc35e --- /dev/null +++ b/test/fixtures/dialects/hive/insert_overwrite_directory.yml @@ -0,0 +1,32 @@ +# 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: f668a6aa92b5afb493a944e446f3787c2c67fd95d730eff5bf345fcf590210c5 +file: + statement: + insert_statement: + - keyword: INSERT + - keyword: OVERWRITE + - keyword: LOCAL + - keyword: DIRECTORY + - literal: "'/path'" + - select_statement: + select_clause: + - keyword: SELECT + - select_clause_element: + column_reference: + identifier: a + - comma: ',' + - select_clause_element: + column_reference: + identifier: b + from_clause: + keyword: FROM + from_expression: + from_expression_element: + table_expression: + table_reference: + identifier: foo + statement_terminator: ; diff --git a/test/fixtures/dialects/hive/insert_overwrite_table.sql b/test/fixtures/dialects/hive/insert_overwrite_table.sql new file mode 100644 index 00000000000..94898999a76 --- /dev/null +++ b/test/fixtures/dialects/hive/insert_overwrite_table.sql @@ -0,0 +1,2 @@ +INSERT OVERWRITE TABLE foo +SELECT a, b FROM bar; diff --git a/test/fixtures/dialects/hive/insert_overwrite_table.yml b/test/fixtures/dialects/hive/insert_overwrite_table.yml new file mode 100644 index 00000000000..a2e48b0584e --- /dev/null +++ b/test/fixtures/dialects/hive/insert_overwrite_table.yml @@ -0,0 +1,32 @@ +# 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: f17c7a773f061fc3d63e162469053524753a20423ea6b6cb86ae6308a11408a2 +file: + statement: + insert_statement: + - keyword: INSERT + - keyword: OVERWRITE + - keyword: TABLE + - table_reference: + identifier: foo + - select_statement: + select_clause: + - keyword: SELECT + - select_clause_element: + column_reference: + identifier: a + - comma: ',' + - select_clause_element: + column_reference: + identifier: b + from_clause: + keyword: FROM + from_expression: + from_expression_element: + table_expression: + table_reference: + identifier: bar + statement_terminator: ; diff --git a/test/fixtures/dialects/hive/insert_overwrite_table_partition.sql b/test/fixtures/dialects/hive/insert_overwrite_table_partition.sql new file mode 100644 index 00000000000..472e33d22b9 --- /dev/null +++ b/test/fixtures/dialects/hive/insert_overwrite_table_partition.sql @@ -0,0 +1,3 @@ +INSERT OVERWRITE TABLE foo +PARTITION (a='test_foo', b='test_bar') IF NOT EXISTS +SELECT a, b, c, d FROM bar; diff --git a/test/fixtures/dialects/hive/insert_overwrite_table_partition.yml b/test/fixtures/dialects/hive/insert_overwrite_table_partition.yml new file mode 100644 index 00000000000..511f2cfc1ed --- /dev/null +++ b/test/fixtures/dialects/hive/insert_overwrite_table_partition.yml @@ -0,0 +1,56 @@ +# 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: e6a682c094d53ff0e0db4c2997dbd6a000d661fbff503448cb2275bd903e4875 +file: + statement: + insert_statement: + - keyword: INSERT + - keyword: OVERWRITE + - keyword: TABLE + - table_reference: + identifier: foo + - keyword: PARTITION + - bracketed: + - start_bracket: ( + - column_reference: + identifier: a + - comparison_operator: '=' + - literal: "'test_foo'" + - comma: ',' + - column_reference: + identifier: b + - comparison_operator: '=' + - literal: "'test_bar'" + - end_bracket: ) + - keyword: IF + - keyword: NOT + - keyword: EXISTS + - select_statement: + select_clause: + - keyword: SELECT + - select_clause_element: + column_reference: + identifier: a + - comma: ',' + - select_clause_element: + column_reference: + identifier: b + - comma: ',' + - select_clause_element: + column_reference: + identifier: c + - comma: ',' + - select_clause_element: + column_reference: + identifier: d + from_clause: + keyword: FROM + from_expression: + from_expression_element: + table_expression: + table_reference: + identifier: bar + statement_terminator: ;