different action of cast and alter in hive

526 Views Asked by At

Currently, I try to union several tables in hive. After I achieved this, I found that some column type is not proper. I thought some should be float but some kind it becomes string. Then I ran alter command:alter table table_name change column_name column_name float; It returned error message:

FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Unable to alter table. The following columns have types incompatible with the existing columns in their respective positions

I thought this means that hive don't support convert string to float as showed in table. But I found that I can do cast on the same column and get wanted result. This makes me confused why there will be different result between alter and cast. What is the logic behind it. THX.

1

There are 1 best solutions below

9
On

It seems you have missed CHANGE keyword.

alter table table_name CHANGE column_name column_name_new float;

See here: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-ChangeColumnName/Type/Position/Comment

Demo:

hive> create table t(a string);
OK
Time taken: 0.069 seconds
hive> alter table t change a a_new float;
OK
Time taken: 0.158 seconds
hive> describe formatted t;
OK
# col_name              data_type               comment

a_new                   float