Impala add column with default value

Viewed 2465

I want to add a column to an existing impala table(and view) with a default value (so that the existing rows also have a value). The column should not allow null values.

ALTER TABLE dbName.tblName ADD COLUMNS (id STRING NOT NULL '-1')

I went through the docs but could not find an example that specifically does this. How do I do this in Impala? Hue underlines/does not recognize the NOT NULL command

2 Answers

Are you using Kudu as a storage layer for your table? Because if not, then according to Impala docs,

Note: Impala only allows PRIMARY KEY clauses and NOT NULL constraints on columns for Kudu tables. These constraints are enforced on the Kudu side.

...

For non-Kudu tables, Impala allows any column to contain NULL values, because it is not practical to enforce a "not null" constraint on HDFS data files that could be prepared using external tools and ETL processes.

Impala's ALTER TABLE syntax also does not support specifying default column values (in general, non-Kudu).

With Impala you could try as follow

add the column


ALTER TABLE dbName.tblName ADD COLUMNS(id STRING);

once you've added the column you can fill that column as below using the same table

INSERT OVERWRITE dbName.tblName SELECT col1,...,coln, '-1' FROM dbName.tblName;

where col1,...,coln are the previous columns before the add columns command and '-1' is to fill the new column.

Related