# Presto 常用函数介绍

Presto 是由 Facebook 开发并开源的分布式 SQL 交互式查询引擎，很多公司都是用它实现 OLAP 业务分析。本文列出了 Presto 常用的函数列表。

## 数学函数

### abs(x)

```presto:default> select abs(1.23) as absolute;
absolute
----------
1.23
```

### cbrt(x)

```presto:default> select cbrt(4) as cubic_root;
cubic_root
--------------------
1.5874010519681996
```

### ceiling(x)

```presto:default> select ceiling(4.7) as ceiling;
ceiling
---------
5.0
```

### degrees(x)

```presto:default> select degrees(2) as degree;
degree
--------------------
114.59155902616465
```

### e(x)\exp(x)

Returns the double value for Euler’s number，使用如下：

```presto:default> select e() as exponent;
Result
exponent
-------------------
2.718281828459045
```

### floor(x)

```presto:default> select floor(4.8) as floor;
floor
-------
4.0
```

```presto:default> select from_base('100', 16);
_col0
-------
256
```

### log2(x)

```presto:default> select log2(5) as log_value;
log_value
-------------------
2.321928094887362
```

### mod(n,m)

```presto:default> select mod(2,4) as mod_value;
mod_value
-----------
2
```

### power(x,p)/pow(x,p)

```presto:default> select power(2,3) as power;
power
-------
8.0
```

```presto:default> select radians(4) as radian_value;
---------------------
0.06981317007977318
```

### round(x)

```presto:default> select round(5.9) as round_value;
round_value
-------------
6.0
```

### sign(x)

• 返回0，如果参数为 0
• 返回1，如果参数大于 0
• 返回-1，如果参数小于 0

• 返回 NaN，如果参数是 NaN
• 返回 1，如果参数是 +Infinity
• 返回-1，如果参数是 -Infinity

### sqrt(x)

```presto:default> select sqrt(3) as squareroot;
squareroot
--------------------
1.7320508075688772
```

```presto:default> select to_base(3,4) as base;
base
------
3
```

### truncate(x)

```presto:default> select truncate(5.9) as truncate_value;
truncate_value
----------------
5.0
```

### width_bucket(x, bound1, bound2, n)

```presto:default> select width_bucket(5,3,4,5) as width;
width
-------
6
```

### width_bucket(x, bins)

Returns the bin number of x according to the bins specified by the array bins，使用如下：

```presto:default> select width_bucket(6,array[1,2,3]) as width;
width
-------
3
```

## 三角函数

### acos(x)

```presto:default> select acos(0.5) as inversecosine;
inversecosine
--------------------
1.0471975511965979
```

### atan2(y,x)

```presto:default> select atan2(2,3) as inverse_tangent;
inverse_tangent
--------------------
0.5880026035475675
```

### cosh(x)

```presto:default> select cosh(1) as hyperbolic_cosine;
hyperbolic_cosine
-------------------
1.543080634815244
```

### sin(x)

```presto:default> select sin(90) as sine;
sine
--------------------
0.8939966636005579
```

## 位函数

### bit_count(x, bits)

Count the number of bits，使用如下：

```presto:default> select bit_count(1,2) as bitcounts;
bitcounts
-----------
1
```

### bitwise_and(x,y)

```presto:default> select bitwise_and(2,3) as bit_and;
bit_and
---------
2
```

### bitwise_or(x,y)

```presto:default> select bitwise_or(2,3) as bit_or;
bit_or
--------
3
```

### bitwise_not(x)

```presto:default> select bitwise_not(3) as bit_not;
bit_not
---------
-4
```

### bitwise_xor(x,y)

```presto:default> select bitwise_xor(2,3) as bit_xor;
bit_xor
---------
1
```

## 字符串函数

### concat(string1, ..., stringN)

```presto:default> select concat('it’,'e’,'blog’) as string_concat;
string_concat
----------------
iteblog
```

### length(string)

```presto:default> select length('iteblog’) as string_length;
string_length
---------------
7
```

### lower(string)

```presto:default> select lower('IteBlog’) as string_lower;
string_lower
----------
iteblog
```

### upper(string)

```presto:default> select lower('IteBlog’) as string_upper;
string_upper
----------
ITEBLOG
```

```presto:default> select lpad('Presto',5,'o') as string_padding;
----------------
Prest
```

### ltrim(string)

```presto:default> select ltrim('Apache Presto') as string_ltrim;
string_ltrim
---------------
Apache Presto
```

### replace(string, search, replace)

```presto:default> select replace('cat','c','r') as string_replace;
string_replace
----------------
rat
```

### reverse(string)

```presto:default> select reverse('Presto') as string_reverse;
string_reverse
----------------
otserP
```

```presto:default> select rpad('presto',2,'o') as string_rpad;
-------------
pr
```

### rtrim(string)

```presto:default> select rtrim('Apache Presto    ‘) as string_ltrim;
string_ltrim
---------------
Apache Presto
```

### split(string, delimiter)

```presto:default> select split('apache presto','e') as string_split;
string_split
-------------------
[apach,  pr, sto]
```

### split_part(string, delimiter, index)

```presto:default> select split_part('apache presto’,'p',2);
_col0
-------
ache
```

### strpos(string, substring)

```presto:default> select strpos('apache','ap') as string_position;
string_position
-----------------
1
```

### substr(string, start)

```presto:default> select substr('tutorialspoint',10) as substring;
substring
-----------
point
```

### substr(string, start, length)

```presto:default> select substr('tutorialspoint',10,2) as substring;
substring
-----------
po
```

### trim(string)

```presto:default> select trim('   Presto   ') as string_trim;
string_trim
-------------
Presto
```

## Date 和 Time 函数

### current_date

```presto:default> select current_date() as date;
Date
------------
2021-11-06
```

### current_time

```presto:default> select current_time() as time;
time
---------------------------
18:44:16.345 Asia/Kolkata
```

### current_timestamp

```presto:default> select current_timestamp() as timestamp;
timestamp
--------------------------------------
2016-07-06 13:14:51.911 Asia/Kolkata
```

### current_timezone()

```presto:default> select current_timezone() as timezone;
timezone
--------------
Asia/Kolkata
```

### now()

```presto:default> select now() as today;
today
--------------------------------------
2016-07-06 13:15:49.704 Asia/Kolkata
```

### localtime

```presto:default> select localtime() as local_time;
local_time
--------------
18:47:32.425
```

### localtimestamp

```presto:default> select localtimestamp() as local_timestamp;
local_timestamp
-------------------------
2016-07-06 13:17:59.347
```