diff --git a/docs/sql-ref-syntax-dml-insert-into.md b/docs/sql-ref-syntax-dml-insert-into.md new file mode 100644 index 000000000000..1bb043db59e5 --- /dev/null +++ b/docs/sql-ref-syntax-dml-insert-into.md @@ -0,0 +1,209 @@ +--- +layout: global +title: INSERT INTO +displayTitle: INSERT INTO +license: | + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--- + +### Description + +The `INSERT INTO` statement inserts new rows into a table. The inserted rows can be specified by value expressions or result from a query. + +### Syntax +{% highlight sql %} +INSERT INTO [ TABLE ] table_name + [ PARTITION ( partition_col_name [ = partition_col_val ] [ , ... ] ) ] + { { VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ] } | query } +{% endhighlight %} + +### Parameters +
+
table_name
+
The name of an existing table.
+
+ +
+
PARTITION ( partition_col_name [ = partition_col_val ] [ , ... ] )
+
Specifies one or more partition column and value pairs. The partition value is optional.
+
+ +
+
VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ]
+
Specifies the values to be inserted. Either an explicitly specified value or a NULL can be inserted. A comma must be used to seperate each value in the clause. More than one set of values can be specified to insert multiple rows.
+
+ +
+
query
+
A query that produces the rows to be inserted. It can be in one of following formats: + +
+
+ +### Examples +#### Single Row Insert Using a VALUES Clause +{% highlight sql %} + CREATE TABLE students (name VARCHAR(64), address VARCHAR(64), student_id INT) + USING PARQUET PARTITIONED BY (student_id); + + INSERT INTO students + VALUES ('Amy Smith', '123 Park Ave, San Jose', 111111); + + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Amy Smith | 123 Park Ave, San Jose | 111111 | + + -------------- + ------------------------------ + -------------- + +{% endhighlight %} + +#### Multi-Row Insert Using a VALUES Clause +{% highlight sql %} + INSERT INTO students + VALUES ('Bob Brown', '456 Taylor St, Cupertino', 222222), + ('Cathy Johnson', '789 Race Ave, Palo Alto', 333333); + + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Amy Smith | 123 Park Ave, San Jose | 111111 | + + -------------- + ------------------------------ + -------------- + + | Bob Brown | 456 Taylor St, Cupertino | 222222 | + + -------------- + ------------------------------ + -------------- + + | Cathy Johnson | 789 Race Ave, Palo Alto | 333333 | + + -------------- + ------------------------------ + -------------- + +{% endhighlight %} + +#### Insert Using a SELECT Statement +Assuming the `persons` table has already been created and populated. + +{% highlight sql %} + SELECT * FROM persons; + + + -------------- + ------------------------------ + -------------- + + | name | address | ssn | + + -------------- + ------------------------------ + -------------- + + | Dora Williams | 134 Forest Ave, Melo Park | 123456789 | + + -------------- + ------------------------------ + -------------- + + | Eddie Davis | 245 Market St, Milpitas | 345678901 | + + -------------- + ------------------------------ + ---------------+ + + INSERT INTO students PARTITION (student_id = 444444) + SELECT name, address FROM persons WHERE name = "Dora Williams"; + + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Amy Smith | 123 Park Ave, San Jose | 111111 | + + -------------- + ------------------------------ + -------------- + + | Bob Brown | 456 Taylor St, Cupertino | 222222 | + + -------------- + ------------------------------ + -------------- + + | Cathy Johnson | 789 Race Ave, Palo Alto | 333333 | + + -------------- + ------------------------------ + -------------- + + | Dora Williams | 134 Forest Ave, Melo Park | 444444 | + + -------------- + ------------------------------ + -------------- + +{% endhighlight %} + +#### Insert Using a TABLE Statement +Assuming the `visiting_students` table has already been created and populated. + +{% highlight sql %} + SELECT * FROM visiting_students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Fleur Laurent | 345 Copper St, London | 777777 | + + -------------- + ------------------------------ + -------------- + + | Gordon Martin | 779 Lake Ave, Oxford | 888888 | + + -------------- + ------------------------------ + -------------- + + + INSERT INTO students TABLE visiting_students; + + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Amy Smith | 123 Park Ave, San Jose | 111111 | + + -------------- + ------------------------------ + -------------- + + | Bob Brown | 456 Taylor St, Cupertino | 222222 | + + -------------- + ------------------------------ + -------------- + + | Cathy Johnson | 789 Race Ave, Palo Alto | 333333 | + + -------------- + ------------------------------ + -------------- + + | Dora Williams | 134 Forest Ave, Melo Park | 444444 | + + -------------- + ------------------------------ + -------------- + + | Fleur Laurent | 345 Copper St, London | 777777 | + + -------------- + ------------------------------ + -------------- + + | Gordon Martin | 779 Lake Ave, Oxford | 888888 | + + -------------- + ------------------------------ + -------------- + +{% endhighlight %} + +#### Insert Using a FROM Statement +Assuming the `applicants` table has already been created and populated. + +{% highlight sql %} + SELECT * FROM applicants; + + + -------------- + ------------------------------ + -------------- + -------------- + + | name | address | student_id | qualified | + + -------------- + ------------------------------ + -------------- + -------------- + + | Helen Davis | 469 Mission St, San Diego | 999999 | true | + + -------------- + ------------------------------ + -------------- + -------------- + + | Ivy King | 367 Leigh Ave, Santa Clara | 101010 | false | + + -------------- + ------------------------------ + -------------- + -------------- + + | Jason Wang | 908 Bird St, Saratoga | 121212 | true | + + -------------- + ------------------------------ + -------------- + -------------- + + + INSERT INTO students + FROM applicants SELECT name, address, id applicants WHERE qualified = true; + + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Amy Smith | 123 Park Ave, San Jose | 111111 | + + -------------- + ------------------------------ + -------------- + + | Bob Brown | 456 Taylor St, Cupertino | 222222 | + + -------------- + ------------------------------ + -------------- + + | Cathy Johnson | 789 Race Ave, Palo Alto | 333333 | + + -------------- + ------------------------------ + -------------- + + | Dora Williams | 134 Forest Ave, Melo Park | 444444 | + + -------------- + ------------------------------ + -------------- + + | Fleur Laurent | 345 Copper St, London | 777777 | + + -------------- + ------------------------------ + -------------- + + | Gordon Martin | 779 Lake Ave, Oxford | 888888 | + + -------------- + ------------------------------ + -------------- + + | Helen Davis | 469 Mission St, San Diego | 999999 | + + -------------- + ------------------------------ + -------------- + + | Jason Wang | 908 Bird St, Saratoga | 121212 | + + -------------- + ------------------------------ + -------------- + +{% endhighlight %} + +Related Statements: + * [INSERT OVERWRITE statement](sql-ref-syntax-dml-insert-overwrite-table.html) + * [INSERT OVERWRITE DIRECTORY statement](sql-ref-syntax-dml-insert-overwrite-directory.html) + * [INSERT OVERWRITE DIRECTORY with Hive format statement](sql-ref-syntax-dml-insert-overwrite-directory-hive.html) \ No newline at end of file diff --git a/docs/sql-ref-syntax-dml-insert-overwrite-directory-hive.md b/docs/sql-ref-syntax-dml-insert-overwrite-directory-hive.md new file mode 100644 index 000000000000..f09da800b592 --- /dev/null +++ b/docs/sql-ref-syntax-dml-insert-overwrite-directory-hive.md @@ -0,0 +1,87 @@ +--- +layout: global +title: INSERT OVERWRITE DIRECTORY with Hive format +displayTitle: INSERT OVERWRITE DIRECTORY with Hive format +license: | + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--- + +### Description +The `INSERT OVERWRITE DIRECTORY` with Hive format overwrites the existing data in the directory with the new values using Hive `SerDe`. +Hive support must be enabled to use this command. The inserted rows can be specified by value expressions or result from a query. + +### Syntax +{% highlight sql %} +INSERT OVERWRITE [ LOCAL ] DIRECTORY directory_path + [ ROW FORMAT row_format ] [ STORED AS file_format ] + { { VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ] } | query } +{% endhighlight %} + +### Parameters +
+
directory_path
+
+ Specifies the destination directory. The LOCAL keyword is used to specify that the directory is on the local file system. +
+
+ +
+
row_format
+
+ Specifies the row format for this insert. Valid options are SERDE clause and DELIMITED clause. SERDE clause can be used to specify a custom SerDe for this insert. Alternatively, DELIMITED clause can be used to specify the native SerDe and state the delimiter, escape character, null character, and so on. +
+
+ +
+
file_format
+
+ Specifies the file format for this insert. Valid options are TEXTFILE, SEQUENCEFILE, RCFILE, ORC, PARQUET, and AVRO. You can also specify your own input and output format using INPUTFORMAT and OUTPUTFORMAT. ROW FORMAT SERDE can only be used with TEXTFILE, SEQUENCEFILE, or RCFILE, while ROW FORMAT DELIMITED can only be used with TEXTFILE. +
+
+ +
+
VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ]
+
+ Specifies the values to be inserted. Either an explicitly specified value or a NULL can be inserted. A comma must be used to seperate each value in the clause. More than one set of values can be specified to insert multiple rows. +
+
+ +
+
query
+
A query that produces the rows to be inserted. It can be in one of following formats: + +
+
+ +### Examples +{% highlight sql %} + INSERT OVERWRITE LOCAL DIRECTORY '/tmp/destination' + STORED AS orc + SELECT * FROM test_table; + + INSERT OVERWRITE LOCAL DIRECTORY '/tmp/destination' + ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' + SELECT * FROM test_table; +{% endhighlight %} + +Related Statements: + * [INSERT INTO statement](sql-ref-syntax-dml-insert-into.html) + * [INSERT OVERWRITE statement](sql-ref-syntax-dml-insert-overwrite-table.html) + * [INSERT OVERWRITE DIRECTORY statement](sql-ref-syntax-dml-insert-overwrite-directory.html) \ No newline at end of file diff --git a/docs/sql-ref-syntax-dml-insert-overwrite-directory.md b/docs/sql-ref-syntax-dml-insert-overwrite-directory.md new file mode 100644 index 000000000000..8262d9e83c0b --- /dev/null +++ b/docs/sql-ref-syntax-dml-insert-overwrite-directory.md @@ -0,0 +1,85 @@ +--- +layout: global +title: INSERT OVERWRITE DIRECTORY +displayTitle: INSERT OVERWRITE DIRECTORY +license: | + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--- +### Description +The `INSERT OVERWRITE DIRECTORY` statement overwrites the existing data in the directory with the new values using Spark native format. The inserted rows can be specified by value expressions or result from a query. + +### Syntax +{% highlight sql %} +INSERT OVERWRITE [ LOCAL ] DIRECTORY [ directory_path ] + USING file_format [ OPTIONS ( key = val [ , ... ] ) ] + { { VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ] } | query } +{% endhighlight %} + +### Parameters +
+
directory_path
+
+ Specifies the destination directory. It can also be specified in OPTIONS using path. The LOCAL keyword is used to specify that the directory is on the local file system. +
+
+ +
+
file_format
+
+ Specifies the file format to use for the insert. Valid options are TEXT, CSV, JSON, JDBC, PARQUET, ORC, HIVE, DELTA, LIBSVM, or a fully qualified class name of a custom implementation of org.apache.spark.sql.sources.DataSourceRegister. +
+
+ +
+
OPTIONS ( key = val [ , ... ] )
+
Specifies one or more table property key and value pairs.
+
+ +
+
VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ]
+
+ Specifies the values to be inserted. Either an explicitly specified value or a NULL can be inserted. A comma must be used to seperate each value in the clause. More than one set of values can be specified to insert multiple rows. +
+
+ +
+
query
+
A query that produces the rows to be inserted. It can be in one of following formats: + +
+
+ +### Examples +{% highlight sql %} +INSERT OVERWRITE DIRECTORY '/tmp/destination' + USING parquet + OPTIONS (col1 1, col2 2, col3 'test') + SELECT * FROM test_table; + +INSERT OVERWRITE DIRECTORY + USING parquet + OPTIONS ('path' '/tmp/destination', col1 1, col2 2, col3 'test') + SELECT * FROM test_table; +{% endhighlight %} + +Related Statements: + * [INSERT INTO statement](sql-ref-syntax-dml-insert-into.html) + * [INSERT OVERWRITE statement](sql-ref-syntax-dml-insert-overwrite-table.html) + * [INSERT OVERWRITE DIRECTORY with Hive format statement](sql-ref-syntax-dml-insert-overwrite-directory-hive.html) \ No newline at end of file diff --git a/docs/sql-ref-syntax-dml-insert-overwrite-table.md b/docs/sql-ref-syntax-dml-insert-overwrite-table.md new file mode 100644 index 000000000000..6ad03473039f --- /dev/null +++ b/docs/sql-ref-syntax-dml-insert-overwrite-table.md @@ -0,0 +1,191 @@ +--- +layout: global +title: INSERT OVERWRITE +displayTitle: INSERT OVERWRITE +license: | + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--- + +### Description + +The `INSERT OVERWRITE` statement overwrites the existing data in the table using the new values. The inserted rows can be specified by value expressions or result from a query. + +### Syntax +{% highlight sql %} +INSERT OVERWRITE [TABLE] table_name + [ PARTITION ( partition_col_name [ = partition_col_val ] [ , ... ] ) [ IF NOT EXISTS ] ] + { { VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ] } | query } +{% endhighlight %} + +### Parameters +
+
table_name
+
The name of an existing table.
+
+ +
+
PARTITION ( partition_col_name [ = partition_col_val ] [ , ... ] )
+
Specifies one or more partition column and value pairs. The partition value is optional.
+
+ +
+
VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ]
+
Specifies the values to be inserted. Either an explicitly specified value or a NULL can be inserted. A comma must be used to seperate each value in the clause. More than one set of values can be specified to insert multiple rows.
+
+ +
+
query
+
A query that produces the rows to be inserted. It can be in one of following formats: + +
+
+ +### Examples +#### Insert Using a VALUES Clause + +Assuming the `students` table has already been created and populated. + +{% highlight sql %} + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Amy Smith | 123 Park Ave, San Jose | 111111 | + + -------------- + ------------------------------ + -------------- + + | Bob Brown | 456 Taylor St, Cupertino | 222222 | + + -------------- + ------------------------------ + -------------- + + | Cathy Johnson | 789 Race Ave, Palo Alto | 333333 | + + -------------- + ------------------------------ + -------------- + + | Dora Williams | 134 Forest Ave, Melo Park | 444444 | + + -------------- + ------------------------------ + -------------- + + | Fleur Laurent | 345 Copper St, London | 777777 | + + -------------- + ------------------------------ + -------------- + + | Gordon Martin | 779 Lake Ave, Oxford | 888888 | + + -------------- + ------------------------------ + -------------- + + | Helen Davis | 469 Mission St, San Diego | 999999 | + + -------------- + ------------------------------ + -------------- + + | Jason Wang | 908 Bird St, Saratoga | 121212 | + + -------------- + ------------------------------ + -------------- + + + INSERT OVERWRITE students + VALUES ('Ashua Hill', '456 Erica Ct, Cupertino', 111111), + ('Brian Reed', '723 Kern Ave, Palo Alto', 222222); + + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Ashua Hill | 456 Erica Ct, Cupertino | 111111 | + + -------------- + ------------------------------ + -------------- + + | Brian Reed | 723 Kern Ave, Palo Alto | 222222 | + + -------------- + ------------------------------ + -------------- + +{% endhighlight %} + +#### Insert Using a SELECT Statement +Assuming the `persons` table has already been created and populated. + +{% highlight sql %} + SELECT * FROM persons; + + + -------------- + ------------------------------ + -------------- + + | name | address | ssn | + + -------------- + ------------------------------ + -------------- + + | Dora Williams | 134 Forest Ave, Melo Park | 123456789 | + + -------------- + ------------------------------ + -------------- + + | Eddie Davis | 245 Market St, Milpitas | 345678901 | + + -------------- + ------------------------------ + ---------------+ + + INSERT OVERWRITE students PARTITION (student_id = 222222) + SELECT name, address FROM persons WHERE name = "Dora Williams"; + + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Ashua Hill | 456 Erica Ct, Cupertino | 111111 | + + -------------- + ------------------------------ + -------------- + + | Dora Williams | 134 Forest Ave, Melo Park | 222222 | + + -------------- + ------------------------------ + -------------- + +{% endhighlight %} + +#### Insert Using a TABLE Statement +Assuming the `visiting_students` table has already been created and populated. + +{% highlight sql %} + SELECT * FROM visiting_students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Fleur Laurent | 345 Copper St, London | 777777 | + + -------------- + ------------------------------ + -------------- + + | Gordon Martin | 779 Lake Ave, Oxford | 888888 | + + -------------- + ------------------------------ + -------------- + + + INSERT OVERWRITE students TABLE visiting_students; + + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Fleur Laurent | 345 Copper St, London | 777777 | + + -------------- + ------------------------------ + -------------- + + | Gordon Martin | 779 Lake Ave, Oxford | 888888 | + + -------------- + ------------------------------ + -------------- + +{% endhighlight %} + +#### Insert Using a FROM Statement +Assuming the `applicants` table has already been created and populated. + +{% highlight sql %} + SELECT * FROM applicants; + + + -------------- + ------------------------------ + -------------- + -------------- + + | name | address | student_id | qualified | + + -------------- + ------------------------------ + -------------- + -------------- + + | Helen Davis | 469 Mission St, San Diego | 999999 | true | + + -------------- + ------------------------------ + -------------- + -------------- + + | Ivy King | 367 Leigh Ave, Santa Clara | 101010 | false | + + -------------- + ------------------------------ + -------------- + -------------- + + | Jason Wang | 908 Bird St, Saratoga | 121212 | true | + + -------------- + ------------------------------ + -------------- + -------------- + + + INSERT OVERWRITE students; + FROM applicants SELECT name, address, id applicants WHERE qualified = true; + + SELECT * FROM students; + + + -------------- + ------------------------------ + -------------- + + | name | address | student_id | + + -------------- + ------------------------------ + -------------- + + | Helen Davis | 469 Mission St, San Diego | 999999 | + + -------------- + ------------------------------ + -------------- + + | Jason Wang | 908 Bird St, Saratoga | 121212 | + + -------------- + ------------------------------ + -------------- + +{% endhighlight %} + +Related Statements: + * [INSERT INTO statement](sql-ref-syntax-dml-insert-into.html) + * [INSERT OVERWRITE DIRECTORY statement](sql-ref-syntax-dml-insert-overwrite-directory.html) + * [INSERT OVERWRITE DIRECTORY with Hive format statement](sql-ref-syntax-dml-insert-overwrite-directory-hive.html) diff --git a/docs/sql-ref-syntax-dml-insert.md b/docs/sql-ref-syntax-dml-insert.md index 200be07a3309..15a2e2889694 100644 --- a/docs/sql-ref-syntax-dml-insert.md +++ b/docs/sql-ref-syntax-dml-insert.md @@ -1,6 +1,6 @@ --- layout: global -title: INSERT +title: INSERT displayTitle: INSERT license: | Licensed to the Apache Software Foundation (ASF) under one or more @@ -9,9 +9,9 @@ license: | The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,4 +19,8 @@ license: | limitations under the License. --- -**This page is under construction** +The INSERT statements: +* [INSERT INTO statement](sql-ref-syntax-dml-insert-into.html) +* [INSERT OVERWRITE statement](sql-ref-syntax-dml-insert-overwrite-table.html) +* [INSERT OVERWRITE DIRECTORY statement](sql-ref-syntax-dml-insert-overwrite-directory.html) +* [INSERT OVERWRITE DIRECTORY with Hive format statement](sql-ref-syntax-dml-insert-overwrite-directory-hive.html)