Apache Presto – SQL Functions
As of now we were discussing running some simple basic queries on Presto. This chapter will discuss the important SQL functions.
Math Functions
Math functions operate on mathematical formulas. Following table describes the list of functions in detail.
S.No. | Function & Description |
---|---|
1. |
Returns the absolute value of x |
2. |
Returns the cube root of x |
3. |
Returns the x value rounded up to the nearest integer |
4. |
ceil(x) Alias for ceiling(x) |
5. |
Returns the degree value for x |
6. |
Returns the double value for Euler’s number |
7. |
exp(x) Returns the exponent value for Euler’s number |
8. |
Returns x rounded down to the nearest integer |
9. |
from_base(string,radix) Returns the value of string interpreted as a base-radix number |
10. |
ln(x) Returns the natural logarithm of x |
11. |
Returns the base 2 logarithm of x |
12. |
log10(x) Returns the base 10 logarithm of x |
13. |
log(x,y) Returns the base y logarithm of x |
14. |
Returns the modulus (remainder) of n divided by m |
15. |
pi() Returns pi value. The result will be returned as a double value |
16. |
Returns power of value ‘p’ to the x value |
17. |
pow(x,p) Alias for power(x,p) |
18. |
converts the angle x in degree radians |
19. |
rand() Alias for radians() |
20. |
Returns the pseudo-random value |
21. |
rand(n) Alias for random() |
22. |
Returns the rounded value for x |
23. |
round(x,d) x value rounded for the ‘d’ decimal places |
24. |
sign(x) Returns the signum function of x, i.e., 0 if the argument is 0 1 if the argument is greater than 0 -1 if the argument is less than 0 For double arguments, the function additionally returns − NaN if the argument is NaN 1 if the argument is +Infinity -1 if the argument is -Infinity |
25. |
Returns the square root of x |
26. |
Return type is archer. The result is returned as the base radix for x |
27. |
Truncates the value for x |
28. |
Returns the bin number of x specified bound1 and bound2 bounds and n number of buckets |
29. |
Returns the bin number of x according to the bins specified by the array bins |
Trigonometric Functions
Trigonometric functions arguments are represented as radians(). Following table lists out the functions.
S.No | Functions & Description |
---|---|
1. |
Returns the inverse cosine value(x) |
2. |
asin(x) Returns the inverse sine value(x) |
3. |
atan(x) Returns the inverse tangent value(x) |
4. |
Returns the inverse tangent value(y/x) |
5. |
cos(x) Returns the cosine value(x) |
6. |
Returns the hyperbolic cosine value(x) |
7. |
Returns the sine value(x) |
8. |
tan(x) Returns the tangent value(x) |
9. |
tanh(x) Returns the hyperbolic tangent value(x) |
Bitwise Functions
The following table lists out the Bitwise functions.
S.No | Functions & Description |
---|---|
1. |
Count the number of bits |
2. |
Perform bitwise AND operation for two bits, x and y |
3. |
Bitwise OR operation between two bits x, y |
4. |
Bitwise Not operation for bit x |
5. |
XOR operation for bits x, y |
String Functions
Following table lists out the String functions.
S.No | Functions & Description |
---|---|
1. |
Concatenate the given strings |
2. |
Returns the length of the given string |
3. |
Returns the lowercase format for the string |
4. |
Returns the uppercase format for the given string |
5. |
Left padding for the given string |
6. |
Removes the leading whitespace from the string |
7. |
Replaces the string value |
8. |
Reverses the operation performed for the string |
9. |
Right padding for the given string |
10. |
Removes the trailing whitespace from the string |
11. |
Splits the string on delimiter and returns an array of size at the most limit |
12. |
Splits the string on delimiter and returns the field index |
13. |
Returns the starting position of the substring in the string |
14. |
Returns the substring for the given string |
15. |
Returns the substring for the given string with the specific length |
16. |
Removes the leading and trailing whitespace from the string |
Date and Time Functions
Following table lists out the Date and Time functions.
S.No | Functions & Description |
---|---|
1. |
Returns the current date |
2. |
Returns the current time |
3. |
Returns the current timestamp |
4. |
Returns the current timezone |
5. |
Returns the current date,timestamp with the timezone |
6. |
Returns the local time |
7. |
Returns the local timestamp |
Regular Expression Functions
The following table lists out the Regular Expression functions.
S.No | Functions & Description |
---|---|
1. |
Returns the string matched by the regular expression for the pattern |
2. |
Returns the string matched by the regular expression for the pattern and the group |
3. |
Returns the first substring matched by the regular expression for the pattern |
4. |
Returns the first substring matched by the regular expression for the pattern and the group |
5. |
Returns the string matches for the pattern. If the string is returned, the value will be true otherwise false |
6. |
Replaces the instance of the string matched for the expression with the pattern |
7. |
Replace the instance of the string matched for the expression with the pattern and replacement |
8. |
Splits the regular expression for the given pattern |
JSON Functions
The following table lists out JSON functions.
S.No | Functions & Description |
---|---|
1. |
Check the value exists in a json array. If the value exists it will return true, otherwise false |
2. |
Get the element for index in json array |
3. |
Returns the length in json array |
4. |
Returns the json structure format |
5. |
Parses the string as a json |
6. |
Returns the size of the value |
URL Functions
The following table lists out the URL functions.
S.No | Functions & Description |
---|---|
1. |
Returns the URL’s host |
2. |
Returns the URL’s path |
3. |
Returns the URL’s port |
4. |
Returns the URL’s protocol |
5. |
Returns the URL’s query string |
Aggregate Functions
The following table lists out the Aggregate functions.
S.No | Functions & Description |
---|---|
1. |
avg(x) Returns average for the given value |
2. |
Returns the minimum value from two values |
3. |
Returns the maximum value from two values |
4. |
Returns the sum of value |
5. |
Returns the number of input rows |
6. |
Returns the count of input values |
7. |
Returns the checksum for x |
8. |
Returns the arbitrary value for x |
Color Functions
Following table lists out the Color functions.
S.No | Functions & Description |
---|---|
1. |
Renders a single bar using rgb low_color and high_color |
2. |
Renders a single bar for the specified width |
3. |
Returns the color value for the entered string |
4. |
Renders value x using the specific color using ANSI color codes |
5. |
Accepts boolean value b and renders a green true or a red false using ANSI color codes |
6. |
rgb(red, green, blue) Returns a color value capturing the RGB value of three component color values supplied as int parameters ranging from 0 to 255 |
Array Functions
The following table lists out the Array functions.
S.No | Functions & Description |
---|---|
1. |
Finds the max element in an array |
2. |
Finds the min element in an array |
3. |
Sorts the elements in an array |
4. |
Removes the specific element from an array |
5. |
Concatenates two arrays |
6. |
Finds the given elements in an array. True will be returned if it is present, otherwise false |
7. |
Find the position of the given element in an array |
8. |
Performs an intersection between two arrays |
9. |
Returns the array element position |
10. |
Slices the array elements with the specific length |
Teradata Functions
The following table lists out Teradata functions.
S.No | Functions & Description |
---|---|
1. |
Returns the index of the string with the given substring |
2. |
Returns the substring of the given string. You can specify the start index here |
3. |
Returns the substring of the given string for the specific start index and length of the string |