Table SQL Statements

Tables can be created in either the shared $scratch location or in filesystem sources types. In addition, if tables are created in a source, they can also be altered and dropped.

The following SQL commands are documented on this page:

CREATE TABLE AS

The CREATE TABLE AS command is used to create tables. Tables are created in either the shared $scratch location or in filesystem sources types.

Tables are written in Parquet format and can be used with HASH PARTITION and LOCALSORT options.

$scratch Location

By default, the $scratch location points to /scratch directory under Dremio’s configured distributed cache location (paths.dist property in dremio.conf). There are no security protocols or permissions associated with this location – it is readable and writable by all users.

Filesystem Sources

Dremio CTAS supports all filesystem source types (S3, ADLS, NAS, HDFS, and MapR-FS) using the filesystem permissions for the written table using impersonation.

Enabling CTAS and DROP TABLE for Sources
In order to use the CREATE TABLE AS (CTAS) and DROP TABLE (DROP) commands on a source, you must enable exports for the source. If enabled, all users/groups who have access to the source can leverage CTAS and DROP TABLE on sources.

Exports can be enabled:

  • When creating a new source.
  • By editing an existing source.

To enable exports into the source:

  1. Select Advanced Options.
  2. Check the Enable exports into this source (CTAS and DROP) box.

CTAS functionality is enabled on a per source basis.

Impersonation
For sources that support impersonation (HDFS and MapR File System) and if impersonation is enabled, Dremio executes the CTAS and DROP TABLE operations as the user running the command. This requires that the user have the appropriate permissions (read, execute, and/or write) for the operation. If impersonation is not enabled, the operation is executed as the user running the Dremio service user.

For additional security, access can be further controlled by selecting specifically restricted filesystem sub-directories when you initially create your Dremio source.

Limitations include:

  • Virtual datasets can be exported to their respective sources via standard SQL-based commands ONLY.
  • DROP TABLE functionality on S3 or ADLS source datasets is slow when working with very large tables.

Syntax

$scratch location syntax

CREATE TABLE $scratch.<TABLE_NAME>
[HASH PARTITION BY (column, . .) ]
[LOCALSORT BY (column) ]
AS <QUERY>

Filesystem source syntax

CREATE TABLE <source>.<TABLE_NAME>
[HASH PARTITION BY (column, . .) ]
[LOCALSORT BY (column) ]
AS <QUERY>

Example

Creating $scratch table

CREATE TABLE $scratch.my_table
AS select * from TPCH.lineitem

Creating source table

CREATE TABLE s3.upload.lineitem2
AS select * from TPCH.lineitem

Once a source table has been created, the Dremio UI displays the following:

  • Path where the table was created.
  • Number of records in the new table.
  • File size of the table.

Querying Tables

Querying $scratch table

select count(*) from $scratch.my_table

Querying source table

select count(*) from s3.upload.lineitem2

ALTER TABLE

Version Considerations:

For companies using Dremio v16.0+, users must have the ALTER privilege granted for a dataset or folder in order to use this command. For earlier versions of access control on Dremio, users must have the Can Edit privilege to use this command.

Modifies the structure of columns in a table, such as adding, deleting, or modifying columns in an existing table. To create a table, refer to the CREATE TABLE command.

Syntax

ALTER TABLE table_name ADD COLUMN column_name data_type(size);
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name ALTER COLUMN column_name data_type(size);
ALTER TABLE table_name MODIFY COLUMN column_name data_type(size);
ALTER TABLE table_name REFRESH METADATA;
ALTER TABLE table_name REFRESH METADATA FOR PARTITIONS ("level" = 'partition');

The ADD COLUMN command creates a new column with the specified name, data type, and character limit. See the example below for how to employ this.

ALTER TABLE customer 1 ADD COLUMN SSN INT(9);

The DROP COLUMN command deletes the specified column, along with all data stored in it. As this cannot be undone, ensure that you’ve secured a backup or snapshot of your databases. See the example below for how to employ this.

ALTER TABLE customer1 DROP COLUMN SSN;

The ALTER COLUMN and MODIFY COLUMN commands are used to change the data type for a specific column. This effectively alters how queries read the data stored in this column, so the current data type should be similar, if not identical, to the way the new data type is read.

Depending on your source, you will need to use either ALTER COLUMN or MODIFY COLUMN specifically. For example, SQL Server uses ALTER COLUMN whereas Oracle uses MODIFY COLUMN. For additional information, see the `ALTER TABLE - ALTER/MODIFY COLUMN resource.

See the example below for how to employ this.

ALTER TABLE customer1 ALTER COLUMN SSN CHAR(11);

Warning About ALTER/MODIFY Data Types:

We recommend against changing data types for a column if it already has records on it. Unless you’ve gone through the column to ensure no issues are found with the data format, this may create issues regarding how the data is read. The likely end result is myriad NULL values. Where possible, it is safer to add a new column with the desired data type and then drop the column whose data you wish to read differently.

Metadata Refreshes

Version Considerations:

This SQL command may only be used with Dremio 18.0+.

The ALTER TABLE command may be used to manually refresh the metadata associated with a table. When coupled with Near-Real-Time Metadata Refreshes and Near-Real-Time Metadata Refreshes for Reflections to fully benefit from this functionality.

You may perform metadata refreshes one of two ways: full or partial.

See the example below for how to execute this command for a full metadata refresh.

ALTER TABLE customer1 REFRESH METADATA;

See the example below for a partial metadata refresh.

ALTER TABLE customer1 REFRESH METADATA FOR PARTITIONS ("dir0"='a')

This example identifies the level of the partition to use (“dir0”) and the specific partition (‘a’) to use for the refresh.

Parameters

The ALTER TABLE command consists of the following parameters for which values must be included:

table_name

The name of the table being altered must be included in the command. This helps to restrict the scope of the change rather than accidentally apply a DROP COLUMN on every dataset in Dremio. This must match the name of your desired table.

column_name

Dremio cannot perform any alteration to column data without the column itself specified. This helps to refine and restrict the scope of the changes,

data_type(size)

This specifies the type of data to be stored for records if a new column is being added or an axisting column being modified.

The (size) component of this parameter defines the limitation on the total number of allowed characters that may be stored on each row for this column.

For a list of data types and their maximum allowed sizes, SQL Data Types.

DROP TABLE

Tables can be dropped only if they have been created on a filesystem source and if you have permission.

Syntax

DROP TABLE <source>.<TABLE_NAME>

Example

DROP TABLE s3.upload.lineitem2