DDL¶
CREATE DATABASE¶
Create catalog with specified name.
CREATE DATABASE [ IF NOT EXISTS ] catalog
-- create catalog cat
CREATE DATABASE cat;
CREATE SCHEMA¶
Create schema under specified catalog, or the default DataFusion catalog if not specified.
CREATE SCHEMA [ IF NOT EXISTS ] [ catalog. ] schema_name
-- create schema emu under catalog cat
CREATE SCHEMA cat.emu;
CREATE EXTERNAL TABLE¶
CREATE EXTERNAL TABLE
SQL statement registers a location on a local
file system or remote object store as a named table which can be queried.
The supported syntax is:
CREATE EXTERNAL TABLE
[ IF NOT EXISTS ]
<TABLE_NAME>[ (<column_definition>) ]
STORED AS <file_type>
[ WITH HEADER ROW ]
[ DELIMITER <char> ]
[ COMPRESSION TYPE <GZIP | BZIP2 | XZ | ZSTD> ]
[ PARTITIONED BY (<column list>) ]
[ WITH ORDER (<ordered column list>)
[ OPTIONS (<key_value_list>) ]
LOCATION <literal>
<column_definition> := (<column_name> <data_type>, ...)
<column_list> := (<column_name>, ...)
<ordered_column_list> := (<column_name> <sort_clause>, ...)
<key_value_list> := (<literal> <literal, <literal> <literal>, ...)
file_type
is one of CSV
, PARQUET
, AVRO
or JSON
LOCATION <literal>
specfies the location to find the data. It can be
a path to a file or directory of partitioned files locally or on an
object store.
Parquet data sources can be registered by executing a CREATE EXTERNAL TABLE
SQL statement such as the following. It is not necessary to
provide schema information for Parquet files.
CREATE EXTERNAL TABLE taxi
STORED AS PARQUET
LOCATION '/mnt/nyctaxi/tripdata.parquet';
CSV data sources can also be registered by executing a CREATE EXTERNAL TABLE
SQL statement. The schema will be inferred based on
scanning a subset of the file.
CREATE EXTERNAL TABLE test
STORED AS CSV
WITH HEADER ROW
LOCATION '/path/to/aggregate_simple.csv';
It is also possible to specify the schema manually.
CREATE EXTERNAL TABLE test (
c1 VARCHAR NOT NULL,
c2 INT NOT NULL,
c3 SMALLINT NOT NULL,
c4 SMALLINT NOT NULL,
c5 INT NOT NULL,
c6 BIGINT NOT NULL,
c7 SMALLINT NOT NULL,
c8 INT NOT NULL,
c9 BIGINT NOT NULL,
c10 VARCHAR NOT NULL,
c11 FLOAT NOT NULL,
c12 DOUBLE NOT NULL,
c13 VARCHAR NOT NULL
)
STORED AS CSV
WITH HEADER ROW
LOCATION '/path/to/aggregate_test_100.csv';
It is also possible to specify a directory that contains a partitioned table (multiple files with the same schema)
CREATE EXTERNAL TABLE test
STORED AS CSV
WITH HEADER ROW
LOCATION '/path/to/directory/of/files';
When creating an output from a data source that is already ordered by
an expression, you can pre-specify the order of the data using the
WITH ORDER
clause. This applies even if the expression used for
sorting is complex, allowing for greater flexibility.
Here’s an example of how to use WITH ORDER
clause.
CREATE EXTERNAL TABLE test (
c1 VARCHAR NOT NULL,
c2 INT NOT NULL,
c3 SMALLINT NOT NULL,
c4 SMALLINT NOT NULL,
c5 INT NOT NULL,
c6 BIGINT NOT NULL,
c7 SMALLINT NOT NULL,
c8 INT NOT NULL,
c9 BIGINT NOT NULL,
c10 VARCHAR NOT NULL,
c11 FLOAT NOT NULL,
c12 DOUBLE NOT NULL,
c13 VARCHAR NOT NULL
)
STORED AS CSV
WITH HEADER ROW
WITH ORDER (c2 ASC, c5 + c8 DESC NULL FIRST)
LOCATION '/path/to/aggregate_test_100.csv';
Where WITH ORDER
clause specifies the sort order:
WITH ORDER (sort_expression1 [ASC | DESC] [NULLS { FIRST | LAST }]
[, sort_expression2 [ASC | DESC] [NULLS { FIRST | LAST }] ...])
Cautions when using the WITH ORDER Clause¶
It’s important to understand that using the
WITH ORDER
clause in theCREATE EXTERNAL TABLE
statement only specifies the order in which the data should be read from the external file. If the data in the file is not already sorted according to the specified order, then the results may not be correct.It’s also important to note that the
WITH ORDER
clause does not affect the ordering of the data in the original external file.
If data sources are already partitioned in Hive style, PARTITIONED BY
can be used for partition pruning.
/mnt/nyctaxi/year=2022/month=01/tripdata.parquet
/mnt/nyctaxi/year=2021/month=12/tripdata.parquet
/mnt/nyctaxi/year=2021/month=11/tripdata.parquet
CREATE EXTERNAL TABLE taxi
STORED AS PARQUET
PARTITIONED BY (year, month)
LOCATION '/mnt/nyctaxi';
CREATE TABLE¶
An in-memory table can be created with a query or values list.
CREATE [OR REPLACE] TABLE [IF NOT EXISTS] table_name AS [SELECT | VALUES LIST];
CREATE TABLE IF NOT EXISTS valuetable AS VALUES(1,'HELLO'),(12,'DATAFUSION');
CREATE TABLE IF NOT EXISTS valuetable(c1 INT, c2 VARCHAR) AS VALUES(1,'HELLO'),(12,'DATAFUSION');
CREATE TABLE memtable as select * from valuetable;
DROP TABLE¶
Removes the table from DataFusion’s catalog.
DROP TABLE [ IF EXISTS ] table_name;
CREATE TABLE users AS VALUES(1,2),(2,3);
DROP TABLE users;
-- or use 'if exists' to silently ignore if the table doesn't exist
DROP TABLE IF EXISTS nonexistent_table;
CREATE VIEW¶
View is a virtual table based on the result of a SQL query. It can be created from an existing table or values list.
CREATE [ OR REPLACE ] VIEW view_name AS statement;
CREATE TABLE users AS VALUES(1,2),(2,3),(3,4),(4,5);
CREATE VIEW test AS SELECT column1 FROM users;
SELECT * FROM test;
+---------+
| column1 |
+---------+
| 1 |
| 2 |
| 3 |
| 4 |
+---------+
CREATE VIEW test AS VALUES(1,2),(5,6);
SELECT * FROM test;
+---------+---------+
| column1 | column2 |
+---------+---------+
| 1 | 2 |
| 5 | 6 |
+---------+---------+
DROP VIEW¶
Removes the view from DataFusion’s catalog.
DROP VIEW [ IF EXISTS ] view_name;
-- drop users_v view from the customer_a schema
DROP VIEW IF EXISTS customer_a.users_v;