数学函数

round(double a) 		返回对a四舍五入的bigint值
round(double a,int d) 	返回四舍五入值,保留d位小数
bround(double a)		银行家舍入法(1-4舍,6-9进,5:前位数是偶,舍,前位数是奇,进)
bround(double a,int d)  银行家舍入法,保留d位小数

floor(double a) 		向下取整 (取左值)
ceil(double a),ceiling(double a)向上取整(取右值)

rand()/rand(int seed)	返回一个(0-1)之间的随机数,若给定种子,这返回一个稳定的随机数序列(伪随机)
exp(double a)/exp(decimal a) 返回e的a次幂
pow(double a, double p), power(double a, double p)) 计算a的p次幂

ln(double a)/ln(decimal a) 			以自然数为底a的对数
log10(double a)/log10(decimal a) 	以10为底a的对数
log2(double a)/log2(decimal a) 		以2为底a的对数
log(double base, double a)log(DECIMAL base, DECIMAL a) 以base为底a的对数

sqrt(DOUBLE a),sqrt(DECIMAL a) 	计算a的平方根

bin(bigint a)二进制a的string类型
hex(bigint a)十六进制a的string类型,若a为string类型,转换为字符对应的十六进制
unhex(string a)逆十六进制
conv(bigint/string num,int from_base,int to_base) 将num从from_base进制转换为to_base进制

abs(double a)	计算a的绝对值
pmod(int/double a,int/double b) a%b

sin(double/decimal a) 	sin a
asin(double/decimal a) 	arcsin a
cos(double/decimal a) 	cos a 
acos(double/decimal a) 	arccos a
tan(double/decimal a) 	tan a
atan(double/decimal a) 	arctan a

degrees(double/decimal a) 弧度-->角度
radians(double/decimal a) 角度-->弧度

positive(int/double a)	返回a
negative(int/double a)	返回-a

sign(double/decimal a)	a为正:1.0,负:-1.0,否则0.0

e() 	自然数e
pi()	数学中的pi

factorial(int a) 	a的阶乘
cbrt(double a)		a的立方根

shiftleft(TINYINT|SMALLINT|INT|bigint a,int b) a按位左移b
shiftright(TINYINT|SMALLINT|INT|bigint a,int b) a按位右移b
shiftrightunsigned(TINYINT|SMALLINT|INT|bigint a,int b) a无符号按位右移b

greatest(T v1,T v2...)	求最大值
least(T v1,T v2...)		求最小值

集合函数

size(Map<K.V>) 		求map的长度
size(Array)			求数组的长度
map_keys(Map<K.V>)	返回map中的所有key(Array数组)
map_values(Map<K.V>)返回map中的所有value(Array数组)
array_contains(Array, value)	判断数组中是否包含value返回true/false
sort_array(Array)	按自然顺序对数组进行排序并返回

类型转换函数

binary(string a) 将输入的值转换成二进制
cast(expr as <type>)将expr转换成type类型

日期函数

from_unixtime() 将时间的秒值转换成format格式

unix_timestamp() 获取本地时区下的时间戳
unix_timestamp('2001-1-1 3:4:4') 将时间字符串转换成时间戳
unix_timestamp(string date,string pattern) 将时间字符串转换成指定格式时间戳,格式不对返回0

to_date(string timestamp) 返回时间串的日期部分 to_date('2001-1-1 3:4:4')="2001-1-1"

year(string date) 	返回年份 int
month(string date) 	返回月份 int
day(string date) 	返回字符串的天 int
hour(string date) 	返回小时 int
minute(string date)	返回分钟	int
second(string date)	返回秒	int

weekofyear(string date)						返回时间字符串位于一年的第几周
datediff(string enddate,string startdate)	计算开始时间和结束时间间相差的天数
quarter(date/timestamp/string) 返回时间所属季度(value:1,2,3,4) quarter('2001-1-1 3:4:4')=1

date_add(string startdate,int days)			从开始时间startdate加上days
date_sub(string startdate,int days)			从开始时间startdate减去days

add_months(string start_date,int num_months)返回当前时间下再增加num_months个月的日期

from_utc_timestamp(String date,string timezone)
	from_utc_timestamp('1970-01-01 00:00:00','PRC')
	>> 1970-01-01 08:00:00
	将UTC时间转化为指定时区下时间戳
to_utc_timestamp(string date,string timezone)
	如果给定指定时区下时间戳,将其转化为UTC下时间戳

current_date			返回当前时间日期
current_timestamp		返回当前时间戳
last_day(string date)	返回这个月最后一天日期
next_day(string start_date,string day_of_week)
	next_day('2015-01-14','TU')=2015-01-20
	返回下一个周几对应的日期
trunc(string date,string format)
	trunc("2016-06-26","MM")=2016-06-01
	trunc("2016-06-26","YY")=2016-01-01
	返回时间最开始的年份或月份
months_between(string date1,string date2)
	返回相差的月份	date1>date2:正
date_format(date/timestamp/string ts, string fmt)
	按指定格式返回date

条件函数

if(boolean testCondition,T valueTrue,T valueFalseOrNull)
	testCondition=true,则返回valueTrue,否则valueFalseOrNull

nvl(T value,T default)	value=null,则返回default,否则返回value

coalesce(T v1,T v2,...)	返回第一个不为null的值,全为null,返回null

case a when b then c [when d then e]...[else f] end
	a=b 返回c,a=d 返回e,否则f

case when a then b [when c then d]...[else e] end
	a=true b,c=true d,否则e

isnull(a)		a=null返回true,否则false
isnotnull(a)	同上,逆

字符函数

ascii(string str)	返回str中首个ASCII字符串的整数值
base64(binary bin)	2进制-->64位字符串
unbase64(string str)64位字符串-->二进制值
concat(string a,string b,...)	字符串拼接
concat_ws(string separator,string b,string b)	字符串拼接,用separator分隔
concat_ws(string separator,array<string>)		拼接Array中元素,用指定分隔符分隔

context_ngrams(array<string<string>>,array<string>,int K,int pf)
	与ngrams类似
ngrams(array<array<string>>, int N, int K, int pf)
	返回出现次数TOP K的的子序列,n表示子序列的长度

decode(binary bin,string charset)使用指定字符集解码二进制值成字符串
encode(string src,string charset)使用指定字符集将字符串编码为二进制值
find_in_set(strign str,string strList)返回str出现的位置,str=','或找不到,返回0,任意参数为null返回null
format_number(number x,int d) 将数值转化为字符串	92310.213='92,310.21'

get_json_object(string json_string,string path)
	从指定路径的json字符串上抽取json对象

in_file(string str,string filename) 文件filename中存在str,返回true
instr(string str,string substr) 返回str中子字符串substr的位置,失败返回0,参数为null返回null(位置从1开始)
length(string A) 字符串长度
locate(string substr,string str[,int pos]) 返回str字符串中pos位置后substr第一次出现的位置
lower(string A)/lcase(string A) 大写转小写
upper(string A)/ucase(string A)	小写转大写
initcap(string A)				首字母大写,其余字母小写

lpad(string str,int len,string pad) 从左侧开始用pad填充字符串,达到length长度,比length大则去掉多余部分
rpad(string str,int len,string pad) 从右侧

ltrim(string a)	去掉字符串a前面的空格
rtrim(string a) 去掉字符串a后面的空格
trim(string a)	去掉字符串a前后出现的空格

parse_url(string urlString, string partToExtract [, string keyToExtract])
	parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY', 'k1') =‘v1’
printf(String format, Obj... args)
	按照printf风格格式输出字符串
regexp_replace(string inital_string, string pattern, string replacement)
	使用正则,匹配上的inital_string用replacement代替
regexp_extract(string subject, string pattern, int index)
	使用正则,抽取匹配的第index个子字符串
	select regexp_extract('hello,world','(\\w+),(\\w+)',1);		world
	select regexp_extract('hello,world','(\\w+),(\\w+)',2);		hello
repeat(string str,int n) 输出n次str
reverse(string A)	反转字符串

sentences(string str, string lang, string locale)
	sentences('Hello there! How are you?') =( ("Hello", "there"), ("How", "are", "you") )

space(int n)		返回n个空格
split(string str,string pat)	按正则表达式pat来分隔字符串,返回字符串数组
str_to_map(text[, delimiter1, delimiter2])
			字符串-->Map,
			第一个参数:字符串,
			第二个参数:键值对分隔符,默认逗号
			第三个参数:键值见分隔符,默认=
substr(string|binary A, int start[,int len])/substring(string|binary A, int start[,int len])
			从start开始截取字符串A[,长度为len]
substring_index(string A, string delim, int count)
			截取第count分隔符之前的字段,delim:分隔符
			count为正,从左边开始截取
			count为负,从右边开始截取
translate(string|char|varchar input, string|char|varchar from, string|char|varchar to)
		select translate("hello,world","world","cc")
			-->hec,cc
			将字符串input中出现的from替换为to

levenshtein(string A, string B)
			返回两个字符串之间的编辑距离(levenshtein)

soundex(string A)	普通字符串-->soundex字符串

聚合函数

count(*)		统计总行数,包括含有null的行
count(expr)		统计提供非NULL的expr表达式值的行数
count(distinct expr[,expr])
				统计提供非NULL且去重后的expr表达式值的行数

sum(col),sum(distinct col)
				求指定列的和,distinct去重后列的和
avg(col),avg(distinct col)
				求指定列的平均值,distinct去重后列的平均值
min(col)		求指定列的最小值
max(col)		求指定列的最大值
variance(col),var_pop(col)
				求指定列数值的方差
var_samp(col)	求指定列数值的样本方差
stddev_pop(col)	求指定列数值的标准偏差
stddev_samp(col)求指定列数值的样本标准偏差
covar_pop(col)	求指定列数值的协方差
covar_samp(col1,col2)
				求指定列数值的样本协方差
corr(col1,col2)	求指定列数值的相关系数
percentile(bigint col,p)
				返回col的p%分位数

表生成函数

explode(array<type> a)	对于a中的每个元素,将生成一行且包含该元素
explode(array)			每行对应数组中的一个元素
explode(Map)			每行对应每个map键-值
posexplode(array)		还返回各元素在数组中所在的位置
stack(INT n, v_1, v_2, ..., v_k)
						将k列转换为n行,每行k/n个字段,n为常数
json_tuple(jsonStr,k1,k2,...)
						从一个json字符串中获取多件键,以tuple返回
pase_url_tuple(url,p1,p2,...)
						类似于pase_url,可以一次提取多个
inline(ARRAY<STRUCT[,STRUCT]>)
						将结构体数组提取出来并插入到表中