Vijiy
Vijiy

Reputation: 1197

Adding Column at partition level in hive

Am new to hive, We had requirement to add columns to existing hive table. I did that with the help of below command. alter table tableName add columns (colName datatype) cascade;

But in hive documentation, we have alter command to add columns at partition level. I tried below commands.

hive> SET hive.exec.dynamic.partition = true;
hive> alter table test_alter_col partition(c=1) add columns (d1 int);
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Duplicate column name: d1
hive> select d1 from test_alter_col where c=1;
FAILED: SemanticException [Error 10004]: Line 1:7 Invalid table alias or column reference 'd1': (possible column names are: a1, b1, d, c)
hive> alter table test_alter_col partition(c=1) add columns (d2 int);
OK
Time taken: 0.178 seconds
hive> select d2 from test_alter_col where c=1;
FAILED: SemanticException [Error 10004]: Line 1:7 Invalid table alias or column reference 'd2': (possible column names are: a1, b1, d, c)
hive>

What exactly does the above command do and is there any use case for using alter command at partition level.

Edit 1 -

Have tried below commands as well, but still neither i am able to query newly added column nor able to insert the data.

create table test_partn (a int, b int, c int) partitioned by (d int) row format delimited fields terminated by '\t' stored as textfile;

insert into table test_partn partition(d) values (1, 11, 111, 1111), (2, 22, 222, 2222), (3, 33, 333, 3333);

SET hive.exec.dynamic.partition = true;

alter table test_partn partition(d=1111) add columns (e int);
insert into test_partn partition(d=1111) values (1, 12, 13, 14);
FAILED: SemanticException [Error 10044]: Line 1:12 Cannot insert into target table because column number/types are different '1111': Table insclause-0 has 3 columns, but query has 4 columns.

alter table test_partn partition(d=3333) add columns (e int) restrict;
insert into test_partn partition(d=3333) values (1, 12, 13, 14);

Thank You, Vijay

Upvotes: 0

Views: 2393

Answers (2)

Vijiy
Vijiy

Reputation: 1197

I think it adding columns at partition level will only add the col names at metadata level.

i tried with query - describe extended tablename partition (keycol=value)

Below are the results.

hive> describe extended test_partn partition(d=1111);
OK
a                       int
b                       int
c                       int
e                       int
d                       int

# Partition Information
# col_name              data_type               comment

d                       int

Detailed Partition Information  Partition(values:[1111], dbName:test, tableName:test_partn, createTime:1539261860, lastAccessTime:0, sd:StorageDescriptor(cols:[FieldSchema(name:a, type:int, comment:null), FieldSchema(name:b, type:int, comment:null), FieldSchema(name:c, type:int, comment:null), FieldSchema(name:e, type:int, comment:null), FieldSchema(name:d, type:int, comment:null)], location:maprfs:/user/hive/warehouse/test.db/test_partn/d=1111, inputFormat:org.apache.hadoop.mapred.TextInputFormat, outputFormat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat, compressed:false, numBuckets:-1, serdeInfo:SerDeInfo(name:null, serializationLib:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, parameters:{serialization.format=     , field.delim=
Time taken: 0.139 seconds, Fetched: 12 row(s)
hive>

I can see the newly added columns only for this partition

Upvotes: 0

Rishu S
Rishu S

Reputation: 3968

hive> alter table test_alter_col partition(c=1) add columns (d1 int); FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Duplicate column name: d1 h

Regarding your first command, it seems that there is already a duplicate column name in your hive table. You need to use a different column.

Alternatively, if you want to add a column to an already partitioned hive table, you can use the below command:

ALTER TABLE <table name> ADD columns (column1 string) CASCADE;

The above should do your work of adding a column to an already partitioned table. Catch here is the CASCADE keyword which will cascade the changes to all of the partitions in hive.

Hope this helps :)

Upvotes: 1

Related Questions