宏函数
示例
#macro(func_name $amt) (
case
when $amt <= 100 then 100
when $amt <= 1000 then 1000
else 10000
end
) #end;
select
amt,
#func_name('amt')
from (
select 0 as amt
union all
select 100 as amt
union all
select 9999 as amt
) t;
内置操作符Built-in Operators
逻辑运算符Operators Precedences
Example |
Operators |
Description |
---|---|---|
A[B] , A.identifier | bracket_op([]), dot(.) | element selector, dot |
-A |
unary(+), unary(-), unary(~) |
unary prefix operators |
A IS [NOT] (NULL|TRUE|FALSE) | IS NULL,IS NOT NULL, ... | unary suffix |
A ^ B |
bitwise xor(^) |
bitwise xor |
A * B | star(*), divide(/), mod(%), div(DIV) | multiplicative operators |
A + B | plus(+), minus(-) | additive operators |
A || B | string concatenate(||) | string concatenate |
A & B | bitwise and(&) | bitwise and |
A | B | bitwise or(|) | bitwise or |
Complex Type Constructors
map(key1, value1, key2, value2, ...)
Creates a map with the given key/value pairs.
named_struct (name1, val1, name2, val2, ...) Creates a struct with the given field names and values.
map取数 M[key]
M is a Map<K, V> and key has type K
Returns the value corresponding to the key in the map. For example, if M is a map comprising of {'f' -> 'foo', 'b' -> 'bar', 'all' -> 'foobar'} then M['all'] returns 'foobar'.
示例:
select map("aa",1,1,"bb") > {"aa":"1","1":"bb"}
select map("aa",1,1,"bb")["cc"] > NULL
select map("aa",1,1,"bb")["aa"]-2 > -1.0, 因为select '1' -2就是-1.0
select map("aa",1,1,"bb")["1"] > 'bb'
named_struct用法
SELECT named_struct("name", "John", "age", 30, "gender", "male") as person;
可以使用字段名称访问命名结构体的字段:
SELECT person.name, person.age FROM (
SELECT named_struct("name", "John", "age", 30, "gender", "male") as person) t;
输出结果:
name age
John 30
map和named_struct的区别及该如何选择?
区别:
map和struct中都保存的k-v对,但是map中k-v对数量和类型是不确定的,而struct中k-v对数量和类型都是确定的。
选择:
如果某列的k-v对数量和类型都是确定且一样的,使用struct。例如某列保存的是用户的个人信息,包含user_id,age、gender。(named_stuct(“user_id”,“age”,“gender”)),相当于定义了一个模板,一个类。数据:named_stuct(“user_id”:“001”,“age”:18,“gender”:“M”),是这个类的对象,查询user_id属性,使用.user_id。
如果某列的k-v对数量和类型不完全一样,使用map。例如只能确定每列是key:String,value:Int类型的数据。该列某行数据是{“name”:“jack”,“age”:18},另一行数据是{“name”:“mart”,“age”:28,“gender”:“M”},则该列只能使用map不能用struct。
内置udf函数
Mathematical Functions
。。。
Collection Functions内置收集函数
int size(Map<K.V>) 返回 Map 类型中的元素数。
int size(Array<T>) 返回数组类型中的元素数。select size(null); -- 结果-1
boolean array_contains(Array<T>, value) 如果数组包含值,则返回 TRUE。
array<t> sort_array(Array<T>) Sorts the input array in ascending order according to the natural ordering of the array elements and returns it.
类型转换功能
-- cast(coalesce(id,-1) as BIGINT) as id
-- decode(gender, 'M', 1, 'F', 0, -1) as gender
Date Functions
时间转换
将timestamp转换成s为单位
timestamp = timestamp > 1000000000000L ? timestamp / 1000 : timestamp;
from_unixtime(bigint unixtime[, string format]) Converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string repres