Hive 如何跨字符串列求和?
How Hive sums across string column?
我正在使用 apache-hive-1.2.1
并创建了一个 table :
test_table : key -> integer and name -> string
key name
--------------
1 name1
2 name2
3 3
我很惊讶以下查询如何给出相应的输出,因为
该列的类型为字符串:
select sum(name) from test_table;
Output : 3.0
和
select avg(name) from test_table;
Output : 3.0
即使列类型是字符串,hive 是否也执行聚合? Hive 是怎么做到的?
请参考 hive builtin UDAF-sum() 的代码,它采用下面的签名 accepts "Only numeric or string type arguments(line-71)
name = "sum", value = "_FUNC_(x) - Returns the sum of a set of numbers"
在 GenericUDAFEvaluator() 中,您可以在第 66 和 67 行找到 case STRING: return new GenericUDAFSumDouble(); ,这意味着取决于基元 type/data 传递的参数类型,正在完成相应的聚合。即,对于 name1,name2(strings),聚合的对应值来自 new DoubleWritable(0);
所以 => 0.0+0.0+3 = 3.0
一个聚合函数,returns 一组数字的总和。它的单个参数可以是数字列,也可以是应用于列值的函数或表达式的数字结果。指定列具有 NULL 值的行将被忽略。如果 table 为空,或者提供给 MIN 的所有值均为 NULL,则 SUM returns NULL.
当查询包含 GROUP BY 子句时,returns每个分组值组合一个值。
Return 类型:整数参数为 BIGINT,浮点参数为 DOUBLE
我正在使用 apache-hive-1.2.1
并创建了一个 table :
test_table : key -> integer and name -> string
key name
--------------
1 name1
2 name2
3 3
我很惊讶以下查询如何给出相应的输出,因为
该列的类型为字符串:
select sum(name) from test_table;
Output : 3.0
和
select avg(name) from test_table;
Output : 3.0
即使列类型是字符串,hive 是否也执行聚合? Hive 是怎么做到的?
请参考 hive builtin UDAF-sum() 的代码,它采用下面的签名 accepts "Only numeric or string type arguments(line-71)
name = "sum", value = "_FUNC_(x) - Returns the sum of a set of numbers"
在 GenericUDAFEvaluator() 中,您可以在第 66 和 67 行找到 case STRING: return new GenericUDAFSumDouble(); ,这意味着取决于基元 type/data 传递的参数类型,正在完成相应的聚合。即,对于 name1,name2(strings),聚合的对应值来自 new DoubleWritable(0);
所以 => 0.0+0.0+3 = 3.0
一个聚合函数,returns 一组数字的总和。它的单个参数可以是数字列,也可以是应用于列值的函数或表达式的数字结果。指定列具有 NULL 值的行将被忽略。如果 table 为空,或者提供给 MIN 的所有值均为 NULL,则 SUM returns NULL.
当查询包含 GROUP BY 子句时,returns每个分组值组合一个值。
Return 类型:整数参数为 BIGINT,浮点参数为 DOUBLE