Hive 严格模式设置

Hive 在早期使用参数 hive.mapred.mode 来决定是否执行严格模式, 其值为 strict 或者 nostrict. 当其值为 strict 时,执行严格模式,如从分区表查询时,过滤条件必须有分区字段。

在 Hive 3.1.3 中,因为 hive.mapred.mode 比较粗暴,为了能更好的在不同地方是否严格模式,在不同的地方用各自的参数。

为了使用不同的参数,hive.mapred.mode 已经被废弃,不能设置,如果设置了,则以 hive.mapred.mode 值为准,新的参数不生效。

新的参数值为 boolean 型, 值可以为 true 和 false。

严格模式的参数如下:

1. hive.strict.checks.orderby.no.limit

是否禁止 order by 后没有 limit 的操作。

注意:当前检查不考虑数据量,仅从 query 的语法上检查。

示例:

当 hive.strict.checks.orderby.no.limit 为 true 时,以下查询不能执行。

select c1, c2 from t order by c1;

必须有 limit 部分,如以下 query 可以执行。

select c1, c2 from t order by c1 limit 100;

2. hive.strict.checks.no.partition.filter

是否禁止查询分区表没有分区字段的过滤操作。

注意:当前检查不考虑数据量,仅从 query 的语法上检查。

如表 t 有分区字段 pc1, pc2。

当 hive.strict.checks.no.partition.filter=true 时,以下查询不能执行。

select c1, sum(c2) 
from t 
where c3 > 100 
group by c1 ;

必须加上至少一个分区字段, 如以下 query 可以执行。

select c1, sum(c2) 
from t 
where c3 > 100 
    and pt1='aaa' 
group by c1 ;

3. hive.strict.checks.type.safety

执行严格的类型检查,当开启时,禁止以下操作:

bigint 和 string 类型的比较。

bigint 和 double 类型的比较。

开启时,执行以下 Query 会报错。

create table t1(c_bigint bigint, c_string string, c_double double);

开启时,执行以下查询会有警告WARNING: Comparing a bigint and a double may result in a loss of precision.:

select * from t1 where c_bigint > c_double;

4. hive.strict.checks.cartesian.product

开启时,禁止迪卡尔积关联。

select * from t1 join t2;

需要加上关联条件,可以用 on 或者 where 部分都可以。

如以下 Query 都可以

select * from t1 join t2 
on t1.c1=t2.c1;
select * from t1 join t2 
where t1.c1=t2.c1;

5. hive.strict.checks.bucketing

开启时,禁用对 bucketing 表执行 load into 操作。