1.問題
在開發過程中,向hive分區表新增字段,發現查詢新增字段的值為NULL
2.問題復現
1.創建分區表,並插入數據
create table student(id int,name string) partitioned by (dt string);
insert into table student partition(dt = '2019-11-13') select 1,'zhangsan';
insert into table student partition(dt = '2019-11-14') select 2,'lisi';
select * from student where dt = '2019-11-13';

select * from student where dt = '2019-11-14';

2.增加字段,插入數據
alter table student add columns(sex string);
insert into table student partition(dt = '2019-11-13') select 1,'zhangsan','male';
insert into table student partition(dt = '2019-11-14') select 2,'lisi','female';
insert into table student partition(dt = '2019-11-15') select 3,'wangwu','female';
3.驗證
select * from student where dt = '2019-11-13';

select * from student where dt = '2019-11-14';

但是 impala查詢正常

select * from student where dt = '2019-11-15';

4.結論
- 分區在增加字段前存在,會出現查詢新增字段值為NULL的情況
- 分區在增加字段前不存在,正常
3.解決方法
對於在增加字段前已經存在的分區,必須再執行
alter table student paritition(dt = '2019-11-14') add columns(sex string);
select * from student where dt = '2019-11-14';

