ALTER TABLE Statement

The ALTER TABLE statement changes the structure or properties of an existing Impala table.

In Impala, this is primarily a logical operation that updates the table metadata in the metastore database that Impala shares with Hive. Most ALTER TABLE operations do not actually rewrite, move, and so on the actual data files. (The RENAME TO clause is the one exception; it can cause HDFS files to be moved to different paths.) When you do an ALTER TABLE operation, you typically need to perform corresponding physical filesystem operations, such as rewriting the data files to include extra fields, or converting them to a different file format.

Syntax:

  1. ALTER TABLE [old_db_name.]old_table_name RENAME TO [new_db_name.]new_table_name
  2. ALTER TABLE name ADD COLUMNS (col_spec[, col_spec ...])
  3. ALTER TABLE name DROP [COLUMN] column_name
  4. ALTER TABLE name CHANGE column_name new_name new_type
  5. ALTER TABLE name REPLACE COLUMNS (col_spec[, col_spec ...])
  6. -- Kudu tables only.
  7. ALTER TABLE name ALTER [COLUMN] column_name
  8. { SET kudu_storage_attr attr_value
  9. | DROP DEFAULT }
  10. kudu_storage_attr ::= { DEFAULT | BLOCK_SIZE | ENCODING | COMPRESSION }
  11. -- Non-Kudu tables only.
  12. ALTER TABLE name ALTER [COLUMN] column_name
  13. SET COMMENT 'comment_text'
  14. ALTER TABLE name ADD [IF NOT EXISTS] PARTITION (partition_spec)
  15. [location_spec]
  16. [cache_spec]
  17. ALTER TABLE name ADD [IF NOT EXISTS] RANGE PARTITION kudu_partition_spec
  18. ALTER TABLE name DROP [IF EXISTS] PARTITION (partition_spec)
  19. [PURGE]
  20. ALTER TABLE name DROP [IF EXISTS] RANGE PARTITION kudu_partition_spec
  21. ALTER TABLE name RECOVER PARTITIONS
  22. ALTER TABLE name [PARTITION (partition_spec)]
  23. SET { FILEFORMAT file_format
  24. | LOCATION 'hdfs_path_of_directory'
  25. | TBLPROPERTIES (table_properties)
  26. | SERDEPROPERTIES (serde_properties) }
  27. ALTER TABLE name colname
  28. ('statsKey'='val, ...)
  29. statsKey ::= numDVs | numNulls | avgSize | maxSize
  30. ALTER TABLE name [PARTITION (partition_spec)] SET { CACHED IN 'pool_name' [WITH REPLICATION = integer] | UNCACHED }
  31. new_name ::= [new_database.]new_table_name
  32. col_spec ::= col_name type_name [kudu_attributes]
  33. kudu_attributes ::= { [NOT] NULL | ENCODING codec | COMPRESSION algorithm |
  34. DEFAULT constant | BLOCK_SIZE number }
  35. partition_spec ::= simple_partition_spec | complex_partition_spec
  36. simple_partition_spec ::= partition_col=constant_value
  37. complex_partition_spec ::= comparison_expression_on_partition_col
  38. kudu_partition_spec ::= constant range_operator VALUES range_operator constant | VALUE = constant
  39. cache_spec ::= CACHED IN 'pool_name' [WITH REPLICATION = integer] | UNCACHED
  40. location_spec ::= LOCATION 'hdfs_path_of_directory'
  41. table_properties ::= 'name'='value'[, 'name'='value' ...]
  42. serde_properties ::= 'name'='value'[, 'name'='value' ...]
  43. file_format ::= { PARQUET | TEXTFILE | RCFILE | SEQUENCEFILE | AVRO }

Statement type: DDL

Complex type considerations:

In Impala 2.3 and higher, the ALTER TABLE statement can change the metadata for tables containing complex types (ARRAY, STRUCT, and MAP). For example, you can use an ADD COLUMNS, DROP COLUMN, or CHANGE clause to modify the table layout for complex type columns. Although Impala queries only work for complex type columns in Parquet tables, the complex type support in the ALTER TABLE statement applies to all file formats. For example, you can use Impala to update metadata for a staging table in a non-Parquet file format where the data is populated by Hive. Or you can use ALTER TABLE SET FILEFORMAT to change the format of an existing table to Parquet so that Impala can query it. Remember that changing the file format for a table does not convert the data files within the table; you must prepare any Parquet data files containing complex types outside Impala, and bring them into the table using LOAD DATA or updating the table’s LOCATION property. See Complex Types (Impala 2.3 or higher only) for details about using complex types.

Usage notes:

Whenever you specify partitions in an ALTER TABLE statement, through the PARTITION (partition_spec) clause, you must include all the partitioning columns in the specification.

Most of the ALTER TABLE operations work the same for internal tables (managed by Impala) as for external tables (with data files located in arbitrary locations). The exception is renaming a table; for an external table, the underlying data directory is not renamed or moved.

Dropping or altering multiple partitions:

In Impala 2.8 and higher, the expression for the partition clause with a DROP or SET operation can include comparison operators such as <, IN, or BETWEEN, and Boolean operators such as AND and OR.

For example, you might drop a group of partitions corresponding to a particular date range after the data “ages out”:

  1. alter table historical_data drop partition (year < 1995);
  2. alter table historical_data drop partition (year = 1996 and month between 1 and 6);

For tables with multiple partition keys columns, you can specify multiple conditions separated by commas, and the operation only applies to the partitions that match all the conditions (similar to using an AND clause):

  1. alter table historical_data drop partition (year < 1995, last_name like 'A%');

This technique can also be used to change the file format of groups of partitions, as part of an ETL pipeline that periodically consolidates and rewrites the underlying data files in a different file format:

  1. alter table fast_growing_data partition (year = 2016, month in (10,11,12)) set fileformat parquet;

Note:

The extended syntax involving comparison operators and multiple partitions applies to the SET FILEFORMAT, SET TBLPROPERTIES, SET SERDEPROPERTIES, and SET [UN]CACHED clauses. You can also use this syntax with the PARTITION clause in the COMPUTE INCREMENTAL STATS statement, and with the PARTITION clause of the SHOW FILES statement. Some forms of ALTER TABLE still only apply to one partition at a time: the SET LOCATION and ADD PARTITION clauses. The PARTITION clauses in the LOAD DATA and INSERT statements also only apply to one partition at a time.

A DDL statement that applies to multiple partitions is considered successful (resulting in no changes) even if no partitions match the conditions. The results are the same as if the IF EXISTS clause was specified.

The performance and scalability of this technique is similar to issuing a sequence of single-partition ALTER TABLE statements in quick succession. To minimize bottlenecks due to communication with the metastore database, or causing other DDL operations on the same table to wait, test the effects of performing ALTER TABLE statements that affect large numbers of partitions.

Amazon S3 considerations:

You can specify an s3a:// prefix on the LOCATION attribute of a table or partition to make Impala query data from the Amazon S3 filesystem. In Impala 2.6 and higher, Impala automatically handles creating or removing the associated folders when you issue ALTER TABLE statements with the ADD PARTITION or DROP PARTITION clauses.

In Impala 2.6 and higher, Impala DDL statements such as CREATE DATABASE, CREATE TABLE, DROP DATABASE CASCADE, DROP TABLE, and ALTER TABLE [ADD|DROP] PARTITION can create or remove folders as needed in the Amazon S3 system. Prior to Impala 2.6, you had to create folders yourself and point Impala database, tables, or partitions at them, and manually remove folders when no longer needed. See Using Impala with the Amazon S3 Filesystem for details about reading and writing S3 data with Impala.

HDFS caching (CACHED IN clause):

If you specify the CACHED IN clause, any existing or future data files in the table directory or the partition subdirectories are designated to be loaded into memory with the HDFS caching mechanism. See Using HDFS Caching with Impala (Impala 2.1 or higher only) for details about using the HDFS caching feature.

In Impala 2.2 and higher, the optional WITH REPLICATION clause for CREATE TABLE and ALTER TABLE lets you specify a replication factor, the number of hosts on which to cache the same data blocks. When Impala processes a cached data block, where the cache replication factor is greater than 1, Impala randomly selects a host that has a cached copy of that data block. This optimization avoids excessive CPU usage on a single host when the same cached data block is processed multiple times. Where practical, specify a value greater than or equal to the HDFS block replication factor.

If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed metadata has been received by all the Impala nodes. See SYNC_DDL Query Option for details.

The following sections show examples of the use cases for various ALTER TABLE clauses.

To rename a table (RENAME TO clause):

The RENAME TO clause lets you change the name of an existing table, and optionally which database it is located in.

For internal tables, this operation physically renames the directory within HDFS that contains the data files; the original directory name no longer exists. By qualifying the table names with database names, you can use this technique to move an internal table (and its associated data directory) from one database to another. For example:

  1. create database d1;
  2. create database d2;
  3. create database d3;
  4. use d1;
  5. create table mobile (x int);
  6. use d2;
  7. -- Move table from another database to the current one.
  8. alter table d1.mobile rename to mobile;
  9. use d1;
  10. -- Move table from one database to another.
  11. alter table d2.mobile rename to d3.mobile;

For external tables,

To change the physical location where Impala looks for data files associated with a table or partition:

  1. ALTER TABLE table_name [PARTITION (partition_spec)] SET LOCATION 'hdfs_path_of_directory';

The path you specify is the full HDFS path where the data files reside, or will be created. Impala does not create any additional subdirectory named after the table. Impala does not move any data files to this new location or change any data files that might already exist in that directory.

To set the location for a single partition, include the PARTITION clause. Specify all the same partitioning columns for the table, with a constant value for each, to precisely identify the single partition affected by the statement:

  1. create table p1 (s string) partitioned by (month int, day int);
  2. -- Each ADD PARTITION clause creates a subdirectory in HDFS.
  3. alter table p1 add partition (month=1, day=1);
  4. alter table p1 add partition (month=1, day=2);
  5. alter table p1 add partition (month=2, day=1);
  6. alter table p1 add partition (month=2, day=2);
  7. -- Redirect queries, INSERT, and LOAD DATA for one partition
  8. -- to a specific different directory.
  9. alter table p1 partition (month=1, day=1) set location '/usr/external_data/new_years_day';

Note: If you are creating a partition for the first time and specifying its location, for maximum efficiency, use a single ALTER TABLE statement including both the ADD PARTITION and LOCATION clauses, rather than separate statements with ADD PARTITION and SET LOCATION clauses.

To automatically detect new partition directories added through Hive or HDFS operations:

In Impala 2.3 and higher, the RECOVER PARTITIONS clause scans a partitioned table to detect if any new partition directories were added outside of Impala, such as by Hive ALTER TABLE statements or by hdfs dfs or hadoop fs commands. The RECOVER PARTITIONS clause automatically recognizes any data files present in these new directories, the same as the REFRESH statement does.

For example, here is a sequence of examples showing how you might create a partitioned table in Impala, create new partitions through Hive, copy data files into the new partitions with the hdfs command, and have Impala recognize the new partitions and new data:

In Impala, create the table, and a single partition for demonstration purposes:

  1. create database recover_partitions;
  2. use recover_partitions;
  3. create table t1 (s string) partitioned by (yy int, mm int);
  4. insert into t1 partition (yy = 2016, mm = 1) values ('Partition exists');
  5. show files in t1;
  6. +---------------------------------------------------------------------+------+--------------+
  7. | Path | Size | Partition |
  8. +---------------------------------------------------------------------+------+--------------+
  9. | /user/hive/warehouse/recover_partitions.db/t1/yy=2016/mm=1/data.txt | 17B | yy=2016/mm=1 |
  10. +---------------------------------------------------------------------+------+--------------+
  11. quit;

In Hive, create some new partitions. In a real use case, you might create the partitions and populate them with data as the final stages of an ETL pipeline.

  1. hive> use recover_partitions;
  2. OK
  3. hive> alter table t1 add partition (yy = 2016, mm = 2);
  4. OK
  5. hive> alter table t1 add partition (yy = 2016, mm = 3);
  6. OK
  7. hive> quit;

For demonstration purposes, manually copy data (a single row) into these new partitions, using manual HDFS operations:

  1. $ hdfs dfs -ls /user/hive/warehouse/recover_partitions.db/t1/yy=2016/
  2. Found 3 items
  3. drwxr-xr-x - impala hive 0 2016-05-09 16:06 /user/hive/warehouse/recover_partitions.db/t1/yy=2016/mm=1
  4. drwxr-xr-x - jrussell hive 0 2016-05-09 16:14 /user/hive/warehouse/recover_partitions.db/t1/yy=2016/mm=2
  5. drwxr-xr-x - jrussell hive 0 2016-05-09 16:13 /user/hive/warehouse/recover_partitions.db/t1/yy=2016/mm=3
  6. $ hdfs dfs -cp /user/hive/warehouse/recover_partitions.db/t1/yy=2016/mm=1/data.txt \
  7. /user/hive/warehouse/recover_partitions.db/t1/yy=2016/mm=2/data.txt
  8. $ hdfs dfs -cp /user/hive/warehouse/recover_partitions.db/t1/yy=2016/mm=1/data.txt \
  9. /user/hive/warehouse/recover_partitions.db/t1/yy=2016/mm=3/data.txt
  1. hive> select * from t1;
  2. OK
  3. Partition exists 2016 1
  4. Partition exists 2016 2
  5. Partition exists 2016 3
  6. hive> quit;

In Impala, initially the partitions and data are not visible. Running ALTER TABLE with the RECOVER PARTITIONS clause scans the table data directory to find any new partition directories, and the data files inside them:

  1. select * from t1;
  2. +------------------+------+----+
  3. | s | yy | mm |
  4. +------------------+------+----+
  5. | Partition exists | 2016 | 1 |
  6. +------------------+------+----+
  7. alter table t1 recover partitions;
  8. select * from t1;
  9. +------------------+------+----+
  10. | s | yy | mm |
  11. +------------------+------+----+
  12. | Partition exists | 2016 | 1 |
  13. | Partition exists | 2016 | 3 |
  14. | Partition exists | 2016 | 2 |
  15. +------------------+------+----+

To change the key-value pairs of the TBLPROPERTIES and SERDEPROPERTIES fields:

  1. ALTER TABLE table_name SET TBLPROPERTIES ('key1'='value1', 'key2'='value2'[, ...]);
  2. ALTER TABLE table_name SET SERDEPROPERTIES ('key1'='value1', 'key2'='value2'[, ...]);

The TBLPROPERTIES clause is primarily a way to associate arbitrary user-specified data items with a particular table.

The SERDEPROPERTIES clause sets up metadata defining how tables are read or written, needed in some cases by Hive but not used extensively by Impala. You would use this clause primarily to change the delimiter in an existing text table or partition, by setting the 'serialization.format' and 'field.delim' property values to the new delimiter character:

  1. -- This table begins life as pipe-separated text format.
  2. create table change_to_csv (s1 string, s2 string) row format delimited fields terminated by '|';
  3. -- Then we change it to a CSV table.
  4. alter table change_to_csv set SERDEPROPERTIES ('serialization.format'=',', 'field.delim'=',');
  5. insert overwrite change_to_csv values ('stop','go'), ('yes','no');
  6. !hdfs dfs -cat 'hdfs://hostname:8020/data_directory/dbname.db/change_to_csv/data_file';
  7. stop,go
  8. yes,no

Use the DESCRIBE FORMATTED statement to see the current values of these properties for an existing table. See CREATE TABLE Statement for more details about these clauses. See impala_perf_stats.html#perf_table_stats_manual for an example of using table properties to fine-tune the performance-related table statistics.

To manually set or update table or column statistics:

Although for most tables the COMPUTE STATS or COMPUTE INCREMENTAL STATS statement is all you need to keep table and column statistics up to date for a table, sometimes for a very large table or one that is updated frequently, the length of time to recompute all the statistics might make it impractical to run those statements as often as needed. As a workaround, you can use the ALTER TABLE statement to set table statistics at the level of the entire table or a single partition, or column statistics at the level of the entire table.

You can set the numrows value for table statistics by changing the TBLPROPERTIES setting for a table or partition. For example:

  1. create table analysis_data stored as parquet as select * from raw_data;
  2. Inserted 1000000000 rows in 181.98s
  3. compute stats analysis_data;
  4. insert into analysis_data select * from smaller_table_we_forgot_before;
  5. Inserted 1000000 rows in 15.32s
  6. -- Now there are 1001000000 rows. We can update this single data point in the stats.
  7. alter table analysis_data set tblproperties('numRows'='1001000000', 'STATS_GENERATED_VIA_STATS_TASK'='true');
  1. -- If the table originally contained 1 million rows, and we add another partition with 30 thousand rows,
  2. -- change the numRows property for the partition and the overall table.
  3. alter table partitioned_data partition(year=2009, month=4) set tblproperties ('numRows'='30000', 'STATS_GENERATED_VIA_STATS_TASK'='true');
  4. alter table partitioned_data set tblproperties ('numRows'='1030000', 'STATS_GENERATED_VIA_STATS_TASK'='true');

See impala_perf_stats.html#perf_table_stats_manual for details.

In Impala 2.6 and higher, you can use the SET COLUMN STATS clause to set a specific stats value for a particular column.

You specify a case-insensitive symbolic name for the kind of statistics: numDVs, numNulls, avgSize, maxSize. The key names and values are both quoted. This operation applies to an entire table, not a specific partition. For example:

  1. create table t1 (x int, s string);
  2. insert into t1 values (1, 'one'), (2, 'two'), (2, 'deux');
  3. show column stats t1;
  4. +--------+--------+------------------+--------+----------+----------+
  5. | Column | Type | #Distinct Values | #Nulls | Max Size | Avg Size |
  6. +--------+--------+------------------+--------+----------+----------+
  7. | x | INT | -1 | -1 | 4 | 4 |
  8. | s | STRING | -1 | -1 | -1 | -1 |
  9. +--------+--------+------------------+--------+----------+----------+
  10. alter table t1 set column stats x ('numDVs'='2','numNulls'='0');
  11. alter table t1 set column stats s ('numdvs'='3','maxsize'='4');
  12. show column stats t1;
  13. +--------+--------+------------------+--------+----------+----------+
  14. | Column | Type | #Distinct Values | #Nulls | Max Size | Avg Size |
  15. +--------+--------+------------------+--------+----------+----------+
  16. | x | INT | 2 | 0 | 4 | 4 |
  17. | s | STRING | 3 | -1 | 4 | -1 |
  18. +--------+--------+------------------+--------+----------+----------+

To reorganize columns for a table:

  1. ALTER TABLE table_name ADD COLUMNS (column_defs);
  2. ALTER TABLE table_name REPLACE COLUMNS (column_defs);
  3. ALTER TABLE table_name CHANGE column_name new_name new_type;
  4. ALTER TABLE table_name DROP column_name;

The column_spec is the same as in the CREATE TABLE statement: the column name, then its data type, then an optional comment. You can add multiple columns at a time. The parentheses are required whether you add a single column or multiple columns. When you replace columns, all the original column definitions are discarded. You might use this technique if you receive a new set of data files with different data types or columns in a different order. (The data files are retained, so if the new columns are incompatible with the old ones, use INSERT OVERWRITE or LOAD DATA OVERWRITE to replace all the data before issuing any further queries.)

For example, here is how you might add columns to an existing table. The first ALTER TABLE adds two new columns, and the second ALTER TABLE adds one new column. A single Impala query reads both the old and new data files, containing different numbers of columns. For any columns not present in a particular data file, all the column values are considered to be NULL.

  1. create table t1 (x int);
  2. insert into t1 values (1), (2);
  3. alter table t1 add columns (s string, t timestamp);
  4. insert into t1 values (3, 'three', now());
  5. alter table t1 add columns (b boolean);
  6. insert into t1 values (4, 'four', now(), true);
  7. select * from t1 order by x;
  8. +---+-------+-------------------------------+------+
  9. | x | s | t | b |
  10. +---+-------+-------------------------------+------+
  11. | 1 | NULL | NULL | NULL |
  12. | 2 | NULL | NULL | NULL |
  13. | 3 | three | 2016-05-11 11:19:45.054457000 | NULL |
  14. | 4 | four | 2016-05-11 11:20:20.260733000 | true |
  15. +---+-------+-------------------------------+------+

You might use the CHANGE clause to rename a single column, or to treat an existing column as a different type than before, such as to switch between treating a column as STRING and TIMESTAMP, or between INT and BIGINT. You can only drop a single column at a time; to drop multiple columns, issue multiple ALTER TABLE statements, or define the new set of columns with a single ALTER TABLE ... REPLACE COLUMNS statement.

The following examples show some safe operations to drop or change columns. Dropping the final column in a table lets Impala ignore the data causing any disruption to existing data files. Changing the type of a column works if existing data values can be safely converted to the new type. The type conversion rules depend on the file format of the underlying table. For example, in a text table, the same value can be interpreted as a STRING or a numeric value, while in a binary format such as Parquet, the rules are stricter and type conversions only work between certain sizes of integers.

  1. create table optional_columns (x int, y int, z int, a1 int, a2 int);
  2. insert into optional_columns values (1,2,3,0,0), (2,3,4,100,100);
  3. -- When the last column in the table is dropped, Impala ignores the
  4. -- values that are no longer needed. (Dropping A1 but leaving A2
  5. -- would cause problems, as we will see in a subsequent example.)
  6. alter table optional_columns drop column a2;
  7. alter table optional_columns drop column a1;
  8. select * from optional_columns;
  9. +---+---+---+
  10. | x | y | z |
  11. +---+---+---+
  12. | 1 | 2 | 3 |
  13. | 2 | 3 | 4 |
  14. +---+---+---+
  1. create table int_to_string (s string, x int);
  2. insert into int_to_string values ('one', 1), ('two', 2);
  3. -- What was an INT column will now be interpreted as STRING.
  4. -- This technique works for text tables but not other file formats.
  5. -- The second X represents the new name of the column, which we keep the same.
  6. alter table int_to_string change x x string;
  7. -- Once the type is changed, we can insert non-integer values into the X column
  8. -- and treat that column as a string, for example by uppercasing or concatenating.
  9. insert into int_to_string values ('three', 'trois');
  10. select s, upper(x) from int_to_string;
  11. +-------+----------+
  12. | s | upper(x) |
  13. +-------+----------+
  14. | one | 1 |
  15. | two | 2 |
  16. | three | TROIS |
  17. +-------+----------+

Remember that Impala does not actually do any conversion for the underlying data files as a result of ALTER TABLE statements. If you use ALTER TABLE to create a table layout that does not agree with the contents of the underlying files, you must replace the files yourself, such as using LOAD DATA to load a new set of data files, or INSERT OVERWRITE to copy from another table and replace the original data.

The following example shows what happens if you delete the middle column from a Parquet table containing three columns. The underlying data files still contain three columns of data. Because the columns are interpreted based on their positions in the data file instead of the specific column names, a SELECT * query now reads the first and second columns from the data file, potentially leading to unexpected results or conversion errors. For this reason, if you expect to someday drop a column, declare it as the last column in the table, where its data can be ignored by queries after the column is dropped. Or, re-run your ETL process and create new data files if you drop or change the type of a column in a way that causes problems with existing data files.

  1. -- Parquet table showing how dropping a column can produce unexpected results.
  2. create table p1 (s1 string, s2 string, s3 string) stored as parquet;
  3. insert into p1 values ('one', 'un', 'uno'), ('two', 'deux', 'dos'),
  4. ('three', 'trois', 'tres');
  5. select * from p1;
  6. +-------+-------+------+
  7. | s1 | s2 | s3 |
  8. +-------+-------+------+
  9. | one | un | uno |
  10. | two | deux | dos |
  11. | three | trois | tres |
  12. +-------+-------+------+
  13. alter table p1 drop column s2;
  14. -- The S3 column contains unexpected results.
  15. -- Because S2 and S3 have compatible types, the query reads
  16. -- values from the dropped S2, because the existing data files
  17. -- still contain those values as the second column.
  18. select * from p1;
  19. +-------+-------+
  20. | s1 | s3 |
  21. +-------+-------+
  22. | one | un |
  23. | two | deux |
  24. | three | trois |
  25. +-------+-------+
  1. -- Parquet table showing how dropping a column can produce conversion errors.
  2. create table p2 (s1 string, x int, s3 string) stored as parquet;
  3. insert into p2 values ('one', 1, 'uno'), ('two', 2, 'dos'), ('three', 3, 'tres');
  4. select * from p2;
  5. +-------+---+------+
  6. | s1 | x | s3 |
  7. +-------+---+------+
  8. | one | 1 | uno |
  9. | two | 2 | dos |
  10. | three | 3 | tres |
  11. +-------+---+------+
  12. alter table p2 drop column x;
  13. select * from p2;
  14. WARNINGS:
  15. File 'hdfs_filename' has an incompatible Parquet schema for column 'add_columns.p2.s3'.
  16. Column type: STRING, Parquet schema:
  17. optional int32 x [i:1 d:1 r:0]
  18. File 'hdfs_filename' has an incompatible Parquet schema for column 'add_columns.p2.s3'.
  19. Column type: STRING, Parquet schema:
  20. optional int32 x [i:1 d:1 r:0]

In Impala 2.6 and higher, if an Avro table is created without column definitions in the CREATE TABLE statement, and columns are later added through ALTER TABLE, the resulting table is now queryable. Missing values from the newly added columns now default to NULL.

To change the file format that Impala expects data to be in, for a table or partition:

Use an ALTER TABLE ... SET FILEFORMAT clause. You can include an optional PARTITION (col1=val1, col2=val2, ... clause so that the file format is changed for a specific partition rather than the entire table.

Because this operation only changes the table metadata, you must do any conversion of existing data using regular Hadoop techniques outside of Impala. Any new data created by the Impala INSERT statement will be in the new format. You cannot specify the delimiter for Text files; the data files must be comma-delimited.

To set the file format for a single partition, include the PARTITION clause. Specify all the same partitioning columns for the table, with a constant value for each, to precisely identify the single partition affected by the statement:

  1. create table p1 (s string) partitioned by (month int, day int);
  2. -- Each ADD PARTITION clause creates a subdirectory in HDFS.
  3. alter table p1 add partition (month=1, day=1);
  4. alter table p1 add partition (month=1, day=2);
  5. alter table p1 add partition (month=2, day=1);
  6. alter table p1 add partition (month=2, day=2);
  7. -- Queries and INSERT statements will read and write files
  8. -- in this format for this specific partition.
  9. alter table p1 partition (month=2, day=2) set fileformat parquet;

To add or drop partitions for a table, the table must already be partitioned (that is, created with a PARTITIONED BY clause). The partition is a physical directory in HDFS, with a name that encodes a particular column value (the partition key). The Impala INSERT statement already creates the partition if necessary, so the ALTER TABLE ... ADD PARTITION is primarily useful for importing data by moving or copying existing data files into the HDFS directory corresponding to a partition. (You can use the LOAD DATA statement to move files into the partition directory, or ALTER TABLE ... PARTITION (...) SET LOCATION to point a partition at a directory that already contains data files.

The DROP PARTITION clause is used to remove the HDFS directory and associated data files for a particular set of partition key values; for example, if you always analyze the last 3 months worth of data, at the beginning of each month you might drop the oldest partition that is no longer needed. Removing partitions reduces the amount of metadata associated with the table and the complexity of calculating the optimal query plan, which can simplify and speed up queries on partitioned tables, particularly join queries. Here is an example showing the ADD PARTITION and DROP PARTITION clauses.

To avoid errors while adding or dropping partitions whose existence is not certain, add the optional IF [NOT] EXISTS clause between the ADD or DROP keyword and the PARTITION keyword. That is, the entire clause becomes ADD IF NOT EXISTS PARTITION or DROP IF EXISTS PARTITION. The following example shows how partitions can be created automatically through INSERT statements, or manually through ALTER TABLE statements. The IF [NOT] EXISTS clauses let the ALTER TABLE statements succeed even if a new requested partition already exists, or a partition to be dropped does not exist.

Inserting 2 year values creates 2 partitions:

  1. create table partition_t (s string) partitioned by (y int);
  2. insert into partition_t (s,y) values ('two thousand',2000), ('nineteen ninety',1990);
  3. show partitions partition_t;
  4. +-------+-------+--------+------+--------------+-------------------+--------+-------------------+
  5. | y | #Rows | #Files | Size | Bytes Cached | Cache Replication | Format | Incremental stats |
  6. +-------+-------+--------+------+--------------+-------------------+--------+-------+
  7. | 1990 | -1 | 1 | 16B | NOT CACHED | NOT CACHED | TEXT | false |
  8. | 2000 | -1 | 1 | 13B | NOT CACHED | NOT CACHED | TEXT | false |
  9. | Total | -1 | 2 | 29B | 0B | | | |
  10. +-------+-------+--------+------+--------------+-------------------+--------+-------+

Without the IF NOT EXISTS clause, an attempt to add a new partition might fail:

  1. alter table partition_t add partition (y=2000);
  2. ERROR: AnalysisException: Partition spec already exists: (y=2000).

The IF NOT EXISTS clause makes the statement succeed whether or not there was already a partition with the specified key value:

  1. alter table partition_t add if not exists partition (y=2000);
  2. alter table partition_t add if not exists partition (y=2010);
  3. show partitions partition_t;
  4. +-------+-------+--------+------+--------------+-------------------+--------+-------------------+
  5. | y | #Rows | #Files | Size | Bytes Cached | Cache Replication | Format | Incremental stats |
  6. +-------+-------+--------+------+--------------+-------------------+--------+-------+
  7. | 1990 | -1 | 1 | 16B | NOT CACHED | NOT CACHED | TEXT | false |
  8. | 2000 | -1 | 1 | 13B | NOT CACHED | NOT CACHED | TEXT | false |
  9. | 2010 | -1 | 0 | 0B | NOT CACHED | NOT CACHED | TEXT | false |
  10. | Total | -1 | 2 | 29B | 0B | | | |
  11. +-------+-------+--------+------+--------------+-------------------+--------+-------+

Likewise, the IF EXISTS clause lets DROP PARTITION succeed whether or not the partition is already in the table:

  1. alter table partition_t drop if exists partition (y=2000);
  2. alter table partition_t drop if exists partition (y=1950);
  3. show partitions partition_t;
  4. +-------+-------+--------+------+--------------+-------------------+--------+-------------------+
  5. | y | #Rows | #Files | Size | Bytes Cached | Cache Replication | Format | Incremental stats |
  6. +-------+-------+--------+------+--------------+-------------------+--------+-------+
  7. | 1990 | -1 | 1 | 16B | NOT CACHED | NOT CACHED | TEXT | false |
  8. | 2010 | -1 | 0 | 0B | NOT CACHED | NOT CACHED | TEXT | false |
  9. | Total | -1 | 1 | 16B | 0B | | | |
  10. +-------+-------+--------+------+--------------+-------------------+--------+-------+

The optional PURGE keyword, available in Impala 2.3 and higher, is used with the DROP PARTITION clause to remove associated HDFS data files immediately rather than going through the HDFS trashcan mechanism. Use this keyword when dropping a partition if it is crucial to remove the data as quickly as possible to free up space, or if there is a problem with the trashcan, such as the trash cannot being configured or being in a different HDFS encryption zone than the data files.

  1. -- Create an empty table and define the partitioning scheme.
  2. create table part_t (x int) partitioned by (month int);
  3. -- Create an empty partition into which you could copy data files from some other source.
  4. alter table part_t add partition (month=1);
  5. -- After changing the underlying data, issue a REFRESH statement to make the data visible in Impala.
  6. refresh part_t;
  7. -- Later, do the same for the next month.
  8. alter table part_t add partition (month=2);
  9. -- Now you no longer need the older data.
  10. alter table part_t drop partition (month=1);
  11. -- If the table was partitioned by month and year, you would issue a statement like:
  12. -- alter table part_t drop partition (year=2003,month=1);
  13. -- which would require 12 ALTER TABLE statements to remove a year's worth of data.
  14. -- If the data files for subsequent months were in a different file format,
  15. -- you could set a different file format for the new partition as you create it.
  16. alter table part_t add partition (month=3) set fileformat=parquet;

The value specified for a partition key can be an arbitrary constant expression, without any references to columns. For example:

  1. alter table time_data add partition (month=concat('Decem','ber'));
  2. alter table sales_data add partition (zipcode = cast(9021 * 10 as string));

Note:

An alternative way to reorganize a table and its associated data files is to use CREATE TABLE to create a variation of the original table, then use INSERT to copy the transformed or reordered data to the new table. The advantage of ALTER TABLE is that it avoids making a duplicate copy of the data files, allowing you to reorganize huge volumes of data in a space-efficient way using familiar Hadoop techniques.

To switch a table between internal and external:

You can switch a table from internal to external, or from external to internal, by using the ALTER TABLE statement:

  1. -- Switch a table from internal to external.
  2. ALTER TABLE table_name SET TBLPROPERTIES('EXTERNAL'='TRUE');
  3. -- Switch a table from external to internal.
  4. ALTER TABLE table_name SET TBLPROPERTIES('EXTERNAL'='FALSE');

Cancellation: Cannot be cancelled.

HDFS permissions:

Most ALTER TABLE clauses do not actually read or write any HDFS files, and so do not depend on specific HDFS permissions. For example, the SET FILEFORMAT clause does not actually check the file format existing data files or convert them to the new format, and the SET LOCATION clause does not require any special permissions on the new location. (Any permission-related failures would come later, when you actually query or insert into the table.)

In general, ALTER TABLE clauses that do touch HDFS files and directories require the same HDFS permissions as corresponding CREATE, INSERT, or SELECT statements. The permissions allow the user ID that the impalad daemon runs under, typically the impala user, to read or write files or directories, or (in the case of the execute bit) descend into a directory. The RENAME TO clause requires read, write, and execute permission in the source and destination database directories and in the table data directory, and read and write permission for the data files within the table. The ADD PARTITION and DROP PARTITION clauses require write and execute permissions for the associated partition directory.

Kudu considerations:

Because of the extra constraints and features of Kudu tables, such as the NOT NULL and DEFAULT attributes for columns, ALTER TABLE has specific requirements related to Kudu tables:

  • In an ADD COLUMNS operation, you can specify the NULL, NOT NULL, and DEFAULT default_value column attributes.

  • In Impala 2.9 and higher, you can also specify the ENCODING, COMPRESSION, and BLOCK_SIZE attributes when adding a column.

  • If you add a column with a NOT NULL attribute, it must also have a DEFAULT attribute, so the default value can be assigned to that column for all existing rows.

  • The DROP COLUMN clause works the same for a Kudu table as for other kinds of tables.

  • Although you can change the name of a column with the CHANGE clause, you cannot change the type of a column in a Kudu table.

  • You cannot change the nullability of existing columns in a Kudu table.

  • In Impala 2.10, you can change the default value, encoding, compression, or block size of existing columns in a Kudu table by using the SET clause.

  • You cannot use the REPLACE COLUMNS clause with a Kudu table.

  • The RENAME TO clause for a Kudu table only affects the name stored in the metastore database that Impala uses to refer to the table. To change which underlying Kudu table is associated with an Impala table name, you must change the TBLPROPERTIES property of the table: SET TBLPROPERTIES('kudu.table_name'='kudu_tbl_name). Doing so causes Kudu to change the name of the underlying Kudu table.

The following are some examples of using the ADD COLUMNS clause for a Kudu table:

  1. CREATE TABLE t1 ( x INT, PRIMARY KEY (x) )
  2. PARTITION BY HASH (x) PARTITIONS 16
  3. STORED AS KUDU
  4. ALTER TABLE t1 ADD COLUMNS (y STRING ENCODING prefix_encoding);
  5. ALTER TABLE t1 ADD COLUMNS (z INT DEFAULT 10);
  6. ALTER TABLE t1 ADD COLUMNS (a STRING NOT NULL DEFAULT '', t TIMESTAMP COMPRESSION default_compression);

The following are some examples of modifying column defaults and storage attributes for a Kudu table:

  1. create table kt (x bigint primary key, s string default 'yes', t timestamp)
  2. stored as kudu;
  3. -- You can change the default value for a column, which affects any rows
  4. -- inserted after this change is made.
  5. alter table kt alter column s set default 'no';
  6. -- You can remove the default value for a column, which affects any rows
  7. -- inserted after this change is made. If the column is nullable, any
  8. -- future inserts default to NULL for this column. If the column is marked
  9. -- NOT NULL, any future inserts must specify a value for the column.
  10. alter table kt alter column s drop default;
  11. insert into kt values (1, 'foo', now());
  12. -- Because of the DROP DEFAULT above, omitting S from the insert
  13. -- gives it a value of NULL.
  14. insert into kt (x, t) values (2, now());
  15. select * from kt;
  16. +---+------+-------------------------------+
  17. | x | s | t |
  18. +---+------+-------------------------------+
  19. | 2 | NULL | 2017-10-02 00:03:40.652156000 |
  20. | 1 | foo | 2017-10-02 00:03:04.346185000 |
  21. +---+------+-------------------------------+
  22. -- Other storage-related attributes can also be changed for columns.
  23. -- These changes take effect for any newly inserted rows, or rows
  24. -- rearranged due to compaction after deletes or updates.
  25. alter table kt alter column s set encoding prefix_encoding;
  26. -- The COLUMN keyword is optional in the syntax.
  27. alter table kt alter x set block_size 2048;
  28. alter table kt alter column t set compression zlib;
  29. desc kt;
  30. +------+-----------+---------+-------------+----------+---------------+-----------------+---------------------+------------+
  31. | name | type | comment | primary_key | nullable | default_value | encoding | compression | block_size |
  32. +------+-----------+---------+-------------+----------+---------------+-----------------+---------------------+------------+
  33. | x | bigint | | true | false | | AUTO_ENCODING | DEFAULT_COMPRESSION | 2048 |
  34. | s | string | | false | true | | PREFIX_ENCODING | DEFAULT_COMPRESSION | 0 |
  35. | t | timestamp | | false | true | | AUTO_ENCODING | ZLIB | 0 |
  36. +------+-----------+---------+-------------+----------+---------------+-----------------+---------------------+------------+

Kudu tables all use an underlying partitioning mechanism. The partition syntax is different than for non-Kudu tables. You can use the ALTER TABLE statement to add and drop range partitions from a Kudu table. Any new range must not overlap with any existing ranges. Dropping a range removes all the associated rows from the table. See Partitioning for Kudu Tables for details.

Related information:

Overview of Impala Tables, CREATE TABLE Statement, DROP TABLE Statement, Partitioning for Impala Tables, Internal Tables, External Tables

Parent topic: Impala SQL Statements