# Functions

A collection of built-in SQL functions of Lenses. You can also enrich it with additional user-defined functions and user-defined aggregation functions.

<table><thead><tr><th width="294">Function</th><th width="222">Description</th><th width="126">Streaming</th><th>Snapshot</th></tr></thead><tbody><tr><td><pre><code>AS_NON_NULLABLE (expr)
</code></pre></td><td>Returns the provided value with its type changed from the original type to its non nullable version</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>AS_NULLABLE (expr)
</code></pre></td><td>Returns the provided value with its type changed from the original type to its nullable version</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CAST (dt AS int)
</code></pre></td><td>Enables conversion of values from one data type to another</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>COALESCE (value, prevValue)
</code></pre></td><td>Returns the first non-null expression in the expression list</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DUMP (expr)
</code></pre></td><td>Show the internal representation of a value</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>EXISTS (field)
</code></pre></td><td>Returns true if the given field is present false otherwise</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ISNOTNULL (expr)
</code></pre></td><td>Returns true if the input is not null; false otherwise</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ISNULL (expr)
</code></pre></td><td>Returns true if the input is null; false otherwise</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>SIZEOF (expr)
</code></pre></td><td>Returns the number of elements in an array</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TYPEOF ()
</code></pre></td><td>Returns the object type of a complex expression. This can only be used when the format on the wire includes the details of the object's full name</td><td>no</td><td>✓</td></tr></tbody></table>

### String functions <a href="#string-functions" id="string-functions"></a>

<table><thead><tr><th>Function</th><th>Description</th><th>Streaming</th><th>Snapshot</th></tr></thead><tbody><tr><td><pre><code>ABBREVIATE (expr, lengthExpr)
</code></pre></td><td>Abbreviates the expression to the given length and appends ellipses</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>BASE64 (expr)
</code></pre></td><td>Returns the input string using base64 algorithm</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CAPITALIZE (expr)
</code></pre></td><td>Capitalizes the first letter of the expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CENTER (target,size,padExpr)
</code></pre></td><td>Centers a String in a larger String of size N</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CHOP (expr)
</code></pre></td><td>Returns the last character from an expression of type string</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CONCATENATE (expr1, expr2, expr3)
</code></pre></td><td>Returns the string representation of concatenating each <code>expression</code> in the list. Null fields are left out</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CONCAT (expr1, expr2, expr3)
</code></pre></td><td>Alias for CONCATENATE</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CONTAINS (sourceExpr,targetExpr)
</code></pre></td><td>Returns true if an expression contains the given substring</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DECODE64 (expr)
</code></pre></td><td>Decodes a Base64 encrypted string</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DELETEWHITESPACE (expr)
</code></pre></td><td>Removes all whitespace from an expression of type string</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DIGITS (expr)
</code></pre></td><td>Retains only the digits from a string expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DROPLEFT (expr, lengthExpr)
</code></pre></td><td>Removes the left most ’length’ characters from a string expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DROPRIGHT (expr, lengthExpr)
</code></pre></td><td>Removes the left most ’length’ characters from a string expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ENDSWITH (sourceExpr, targetExpr)
</code></pre></td><td>Returns true if an expression ends with the given substring</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>INDEXOF (expr, substringExpr)
</code></pre></td><td>Returns the index of a substring in an expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LENGTH expr
</code></pre></td><td>Returns the length of a string. Calculates length using characters as defined by UTF-16</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LEN expr
</code></pre></td><td>Alias for LENGTH</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LOWERCASE (strExpr)
</code></pre></td><td>Returns the expression in lowercase</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LOWER (strExpr)
</code></pre></td><td>Alias for LOWERCASE</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LEFTPAD (strExpr, lengthExpr, padExpr)
</code></pre></td><td>Prepends the value of <em>padExpr</em> to the value of <em>strExpr</em> until the total length is <em>lengthExpr</em></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LPAD (strExpr, lengthExpr, padExpr)
</code></pre></td><td>Alias for LEFTPAD</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>MKSTRING (anyType)
</code></pre></td><td>Returns a string representation of any given value</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>REGEXP (strExpr, regexExpr)
</code></pre></td><td>Returns the matched groups otherwise null</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>REGEX (strExpr, regexExpr)
</code></pre></td><td>Alias for REGEXP</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>REGEX_MATCHES (strExpr, regexExpr)
</code></pre></td><td>Returns an array of captured groups.</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>REPLACE (sourceExpr, targetExpr, replaceExpr)
</code></pre></td><td>Returns a new string in which all occurrences of a specified String in the current string are replaced with another specified String</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>REVERSE (expr)
</code></pre></td><td>Reverses the order of the elements in the input</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>RIGHTPAD (strExpr, lengthExpr, padExpr)
</code></pre></td><td>Appends the value of <em>padExpr</em> to the value of <em>strExpr</em> until the total length is <em>lengthExpr</em></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>RPAD (strExpr, lengthExpr, padExpr)
</code></pre></td><td>Alias for RIGHTPAD</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>STARTSWITH (exprSource, exprTarget)
</code></pre></td><td>Returns true if an expression starts with the given substring</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>STRIPACCENTS (expr)
</code></pre></td><td>Removes diacritics (approximately the same as accents) from an expression. The case will not be altered</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>SUBSTRING (expr, startIndexExpr, endIndexExpr)
</code></pre></td><td>Returns a new string that is a substring of this string</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>SUBSTR (expr, startIndexExpr)
</code></pre></td><td>Alias for SUBSTRING</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>SWAPCASE (expr)
</code></pre></td><td>Swaps the case of a string expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TAKELEFT (expr, lengthExpr)
</code></pre></td><td>Returns the left most ’length’ characters from a string expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TAKERIGHT (expr, lengthExpr)
</code></pre></td><td>Returns the right most ’length’ characters from a string expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TRIM (expr)
</code></pre></td><td>Removes leading and trailing spaces from the input expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TRUNCATE (strExpr, nExpr)
</code></pre></td><td>Truncates a string so that it has at most N characters</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>UNCAPITALIZE (expr)
</code></pre></td><td>Changes the first letter of each word in the expression to lowercase</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>UPPER (strExpr)
</code></pre><pre><code>UPPERCASE (strExpr)
</code></pre></td><td>Returns the expression in uppercase</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>UUID ()
</code></pre></td><td>Returns an universally unique identifier</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>JSON_EXTRACT_FIRST (json_string,pattern)
</code></pre></td><td>Interprets ‘pattern’ as a Json path pattern and applies it to ‘json_string’, returning the first match, as a string containing valid json. Examples for the pattern parameter: “$.a”, “$[‘a’]”, “$[0]”, “$.points[?(@[‘id’]==‘i4’)].x”, “$[‘points’][?(@[‘y’] >= 3)].id”, “$.conditions[?(@ == false)]”</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>JSON_EXTRACT_ALL (json_string,pattern)
</code></pre></td><td>Interprets ‘pattern’ as a Json path pattern and applies it to ‘json_string’, returning all matches, as an array of strings containing valid json. Examples for the pattern parameter: “$.a”, “$[‘a’]”, “$[0]”, “$.points[?(@[‘id’]==‘i4’)].x”, “$[‘points’][?(@[‘y’] >= 3)].id”, “$.conditions[?(@ == false)]”</td><td>✓</td><td>✓</td></tr></tbody></table>

### Obfuscation functions  <a href="#obfuscation-functions" id="obfuscation-functions"></a>

They allow us to redact the value to avoid providing the full content details. Useful when querying data containing customers' personally identifiable information like credit cards.

<table><thead><tr><th>Name</th><th>Description</th><th>Streaming</th><th>Snapshot</th></tr></thead><tbody><tr><td><pre><code>ANONYMIZE (strExpr)
</code></pre></td><td>Obfuscates the entire string input</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>MASK (strExpr)
</code></pre></td><td>Alias for ANONYMIZE</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>EMAIL (emailExpr)
</code></pre></td><td>Anonymize the value and obfuscates an email address</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FIRST1 (strExpr)
</code></pre></td><td>Anonymize the value and only keeps the first character</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FIRST2 (strExpr)
</code></pre></td><td>Anonymize the value and only keeps the first two characters</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FIRST3 (strExpr)
</code></pre></td><td>Anonymize the value and only keeps the first three characters</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FIRST4 (strExpr)
</code></pre></td><td>Anonymize the value and only keeps the first four characters</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LAST1 (strExpr)
</code></pre></td><td>Anonymize the value and only keeps the last character</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LAST2 (strExpr)
</code></pre></td><td>Anonymize the value and only keeps the last two characters</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LAST3 (strExpr)
</code></pre></td><td>Anonymize the value and only keeps the last three characters</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>LAST4 (strExpr)
</code></pre></td><td>Anonymize the value and only keeps the last four characters</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>INITIALS (strExpr)
</code></pre></td><td>Anonymize the value and only keeps the initials of all the words in the input</td><td>✓</td><td>✓</td></tr></tbody></table>

### Numeric functions  <a href="#numeric-functions" id="numeric-functions"></a>

<table><thead><tr><th>Name</th><th>Description</th><th>Streaming</th><th>Snapshot</th></tr></thead><tbody><tr><td><code>%</code></td><td>The remainder operator (%) computes the remainder after dividing its first operand by its second i.e. <code>numExpr % numExpr</code></td><td>✓</td><td>✓</td></tr><tr><td><code>/</code></td><td>Divides one number by another (an arithmetic operator) i.e. <code>numExpr / numExpr</code></td><td>✓</td><td>✓</td></tr><tr><td><code>-</code></td><td>Subtracts one number from another (an arithmetic operator) i.e. <code>numExpr - numExpr</code></td><td>✓</td><td>✓</td></tr><tr><td><code>*</code></td><td>Multiplies one number with another (an arithmetic operator) i.e. <code>numExpr * numExpr</code></td><td>✓</td><td>✓</td></tr><tr><td><code>+</code></td><td>Adds one number to another (an arithmetic operator) i.e. <code>numExpr + numExpr</code></td><td>✓</td><td>✓</td></tr><tr><td><code>-</code> (negative)</td><td>Returns the negative of the value of a numeric expression (a unary operator) i.e. <code>-numExpr</code></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ABS (numExpr)
</code></pre></td><td>Returns the absolute value of an <code>expression</code> that evaluates to a number type</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ACOS (numExpr)
</code></pre></td><td>Returns the trigonometric arc cosine of an expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ASIN (numExpr)
</code></pre></td><td>Returns the trigonometric arc sine of an expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ATAN (numExpr)
</code></pre></td><td>Returns the trigonometric arc tangent of an expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CBRT (numExpr)
</code></pre></td><td>Returns the cube root of <em>numExpr</em></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CEIL (numExpr)
</code></pre></td><td>Returns the absolute value of an <code>expression</code></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>COSH (numExpr)
</code></pre></td><td>Returns the hyperbolic cosine of an expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>COS (numExpr)
</code></pre></td><td>Returns the trigonometric cosine of an expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DEGREES (numExpr)
</code></pre></td><td>Converts the input expression to degrees</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DISTANCE (x1,y1,x2,y2)
</code></pre></td><td>Calculates the distance between two points using the haversine method</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FLOOR (numExpr)
</code></pre></td><td>Returns the largest value not greater than the argument</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>MAX (numExpr1,numExpr2,numExpr3)
</code></pre></td><td>Returns the maximum element from an arbitrary number of given elements</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>MIN (numExpr1,numExpr2,numExpr3)
</code></pre></td><td>Returns the minimum element from an arbitrary number of given elements</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>MOD (numExpr, numExpr)
</code></pre></td><td>Alias for <code>%</code></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>NEG (numExpr)
</code></pre></td><td>Returns the negative value of an <code>expression</code> it has to evaluate to a number type</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>POW (numExpr1, numExpr2)
</code></pre></td><td>Returns <em>numExp1</em> raised to the <em>numExp2</em> power</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>RADIANS (numExpr)
</code></pre></td><td>Converts the input expression to radians</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>RANDINT ()
</code></pre></td><td>Returns a random integer</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ROUND (numExpr)
</code></pre></td><td>Returns the closest integer of an <code>expression</code>, with ties rounding towards positive infinity</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>SIGN (numExpr)
</code></pre></td><td>Returns +1 if a value is positive or -1 if a value is negative</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>SINH (numExpr)
</code></pre></td><td>Returns the hyperbolic sine of an expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>SIN (numExpr)
</code></pre></td><td>Returns the trigonometric sine of an expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>SQRT (numExpr)
</code></pre></td><td>Returns the square root of <em>numExpr</em></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TANH (numExpr)
</code></pre></td><td>Returns the hyperbolic tangent of an expression</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TAN (numExpr)
</code></pre></td><td>Returns the trigonometric tangent of an expression</td><td>✓</td><td>✓</td></tr></tbody></table>

### Date and time functions  <a href="#date-and-time-functions" id="date-and-time-functions"></a>

<table><thead><tr><th>Name</th><th>Description</th><th>Streaming</th><th>Snapshot</th></tr></thead><tbody><tr><td><pre><code>TIME_MILLIS(millis)
</code></pre></td><td>Builds a time-millis value from a long or int value</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TIME_MICROS(micros)
</code></pre></td><td>Builds a time-micros value from a long or int value</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DATE(days)
</code></pre></td><td>Builds a local date value from a long or int value. This function can also be used with no parameters (see below)</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TIMESTAMP_MILLIS(millis)
</code></pre></td><td>Builds a timestamp-millis value from a long or int value</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TIMESTAMP_MICROS(micros)
</code></pre></td><td>Builds a timestamp-micros value from a long or int value</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>PARSE_TIME_MILLIS(millis, pattern)
</code></pre></td><td>Builds a time-millis value given a time string representation and a time pattern</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>PARSE_TIME_MICROS(micros, pattern)
</code></pre></td><td>Builds a time-micros value given a time string representation and a time pattern</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>PARSE_DATE(string, pattern)
</code></pre></td><td>Builds a date value given a date string representation and a date pattern</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>PARSE_TIMESTAMP_MILLIS(string, input_pattern)
</code></pre></td><td>Builds a timestamp-millis value given a datetime string representation and a date time pattern</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>PARSE_TIMESTAMP_MICROS(string, input_pattern)
</code></pre></td><td>Builds a timestamp-micros value given a datetime string representation and a date time pattern</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>EXTRACT_TIME(timestamp)
</code></pre></td><td>Extracts the time portion of a timestamp-micros or timestamp-millis returning a time-millis or time-micros value depending on the timestamp precision</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>EXTRACT_DATE(timestamp)
</code></pre></td><td>Extracts the dat portion of a timestamp-micros or timestamp-millis returning a date value</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TIMESTAMP(date, time, zoneStr)
</code></pre></td><td>Returns a timestamp for a given date and time at a specific zone id</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FORMAT_TIMESTAMP(timestamp,output_pattern)
</code></pre></td><td>Returns a string representation of a timestamp value according to a given a pattern</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FORMAT_DATE(date,output_pattern)
</code></pre></td><td>Returns a string representation of a date value according to a given a pattern</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FORMAT_TIME(time, output_pattern)
</code></pre></td><td>Returns a string representation of a time value according to a given a pattern</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DATE ()
</code></pre></td><td>Provides the current ISO date value</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CONVERT_DATETIME (strExpr, fromPattern, toPattern)
</code></pre></td><td>Converts the string format of a date [and time] to another using the pattern provided</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>CONVERTDATETIME (strExpr, fromPattern, toPattern)
</code></pre></td><td>Alias for CONVERT_DATETIME</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DATETIME ()
</code></pre></td><td>Provides the current ISO date and time</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DATE_TO_STR (strExpr, pattern)
</code></pre></td><td>Converts a date time value (epoch in milliseconds) to a string using the pattern provided</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>DAY (expr)
</code></pre></td><td>Extracts the day component of an expression that is of type timestamp</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>HOUR (expr)
</code></pre></td><td>Extracts the hour component of an expression that is of type timestamp</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>MINUTE (dataExpr)
</code></pre></td><td>Extracts the minute component of an expression that is of type timestamp</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>MONTH_TEXT (dataExpr)
</code></pre></td><td>Returns the month name</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>MONTH (dataExpr)
</code></pre></td><td>Extracts the month component of an expression that is of type timestamp</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>SECOND (dataExpr)
</code></pre></td><td>Extracts the second component of an expression that is of type timestamp</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TOMORROW ()
</code></pre></td><td>Returns the current date time plus <em>1 day</em></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TO_DATETIME (strExpr, pattern)
</code></pre></td><td>Converts a string representation of a datetime into epoch value using the pattern provided</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TO_DATE (strExpr, pattern)
</code></pre></td><td>Converts a string representation of a date into epoch value using the pattern provided</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TO_TIMESTAMP (longExpr)
</code></pre></td><td>Converts a long (epoch) to a date and time type</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TO_TIMESTAMP (strExpr, pattern)
</code></pre></td><td>Converts a string using a pattern to a date and time type</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>YEAR (expr)
</code></pre></td><td>Extracts the year component of an expression that is of type timestamp</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>YESTERDAY ()
</code></pre></td><td>Returns the current date time minus <em>1 day</em></td><td>✓</td><td>✓</td></tr></tbody></table>

Note\
The proper notation for date patterns can be found in [SimpleDateFormat](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)

### Array functions  <a href="#array-functions" id="array-functions"></a>

<table><thead><tr><th>Name</th><th>Description</th><th>Streaming</th><th>Snapshot</th></tr></thead><tbody><tr><td><pre><code>ELEMENT_OF (array, index)
</code></pre></td><td>Return the element of <code>array</code> at <code>index</code></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FLATTEN (array)
</code></pre></td><td>Flatten an array of arrays into an array</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>IN_ARRAY (element, array)
</code></pre></td><td>Check if <code>element</code> as an element of <code>array</code></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>REPEAT (element, n)
</code></pre></td><td>Build an array repeating <code>element</code> <code>n</code> times</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ZIP (array1, field1, array2, field2, …)
</code></pre></td><td>Zip two or more arrays into a single one.<br>Example: <code>ZIP([1, 2], 'x', [3, 4, 5], 'y')</code> will be evaluated to <code>[{ x: 1, y: 3 }, { x: 2, y: 4 }]</code></td><td>✓</td><td>✓</td></tr><tr><td><pre><code>ZIP_ALL (array1, field1, array2, field2, …)
</code></pre></td><td>Zip two or more arrays into a single one, returning <code>null</code>s when an array is not long enough.<br>Example: <code>ZIP_ALL([1, 2], 'x', [3, 4, 5], 'y')</code> will be evaluated to <code>[{ x: 1, y: 3 }, { x: 2, y: 4 }, { x: null, y: 5 }]</code></td><td>✓</td><td>✓</td></tr></tbody></table>

### Aggregated functions  <a href="#aggregated-functions" id="aggregated-functions"></a>

An aggregate function calculates a set of values and returns a single value. They cannot be used without a GROUP BY statement.

Lenses SQL engine Snapshot and Streaming modes have different semantics. Thus, some functions are available for Streaming and do not apply to Snapshot.

<table><thead><tr><th>Name</th><th>Description</th><th>Streaming (stateless)</th><th>Streaming (stateful)</th><th>Snapshot</th></tr></thead><tbody><tr><td><pre><code>AVG (numExpr)
</code></pre></td><td>Returns the average of the values in a group. It ignores null value. It can be used with numeric input only</td><td>✓</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>BOTTOMK (numExpr, N)
</code></pre></td><td>Returns the last K lowest ranked values. The ranking is based on how many times a value has been seen</td><td>no</td><td>no</td><td>✓</td></tr><tr><td><pre><code>COLLECT (expr, maxN)
</code></pre></td><td>Returns an array in which each value in the input set is assigned to an element of the array</td><td>✓</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>COLLECT_UNIQUE (expr, maxN)
</code></pre></td><td>Returns an array of unique values in which each value in the input set is assigned to an element of the array</td><td>✓</td><td>no</td><td>✓</td></tr><tr><td><pre><code>COUNT (*) AS total
</code></pre></td><td>Returns the number of records returned by a query or the records in a group as a result of a <code>GROUP BY</code> statement</td><td>✓</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>FIRST (expr)
</code></pre></td><td>Returns the first item seen in a group</td><td>no</td><td>no</td><td>✓</td></tr><tr><td><pre><code>LAST (expr)
</code></pre></td><td>Returns the last item seen in a group</td><td>no</td><td>no</td><td>✓</td></tr><tr><td><pre><code>MAXK (numExpr, N)
</code></pre></td><td>Returns the N largest values of an <em>numExpr</em></td><td>✓</td><td>no</td><td>no</td></tr><tr><td><pre><code>MAXK_UNIQUE (numExpr, N)
</code></pre></td><td>Returns the N largest unique values of an <em>numExpr</em></td><td>✓</td><td>no</td><td>no</td></tr><tr><td><pre><code>MINK (numExpr, N)
</code></pre></td><td>Returns the N smallest values of an <em>numExpr</em></td><td>✓</td><td>no</td><td>no</td></tr><tr><td><pre><code>MINK_UNIQUE (numExpr, N)
</code></pre></td><td>Returns the N smallest unique values of an <em>numExpr</em></td><td>✓</td><td>no</td><td>no</td></tr><tr><td><pre><code>SUM (numExpr)
</code></pre></td><td>Returns the sum of all the values, in the expression. It can be used with numeric input only. Null values are ignored</td><td>✓</td><td>✓</td><td>✓</td></tr><tr><td><pre><code>TOPK (numExpr, N)
</code></pre></td><td>Returns the K highest ranked values. The ranking is based on how many times a value has been seen</td><td>no</td><td>no</td><td>✓</td></tr></tbody></table>

### Record headers functions  <a href="#record-headers-functions" id="record-headers-functions"></a>

<table data-header-hidden><thead><tr><th></th><th></th><th></th><th></th></tr></thead><tbody><tr><td>Name</td><td>Description</td><td>Streaming</td><td>Snapshot</td></tr><tr><td><pre><code>HEADERASSTRING (keyStr)
</code></pre></td><td>Returns the value of the record header key as a STRING value</td><td>no</td><td>✓</td></tr><tr><td><pre><code>HEADERASINT (keyStr)
</code></pre></td><td>Returns the value of the record header key as an INT value</td><td>no</td><td>✓</td></tr><tr><td><pre><code>HEADERASLONG (keyStr)
</code></pre></td><td>Returns the value of the record header key as a LONG value</td><td>no</td><td>✓</td></tr><tr><td><pre><code>HEADERASDOUBLE (keyStr)
</code></pre></td><td>Returns the value of the record header key as a DOUBLE value</td><td>no</td><td>✓</td></tr><tr><td><pre><code>HEADERASFLOAT (keyStr)
</code></pre></td><td>Returns the value of the record header key as a FLOAT value</td><td>no</td><td>✓</td></tr><tr><td><pre><code>HEADERKEYS ()
</code></pre></td><td>Returns all the header keys for the current record</td><td>no</td><td>✓</td></tr></tbody></table>

### Record offset functions  <a href="#record-offset-functions" id="record-offset-functions"></a>

<table><thead><tr><th width="194">Name</th><th width="326">Description</th><th width="129">Streaming</th><th>Snapshot</th></tr></thead><tbody><tr><td><pre><code>LAST_OFFSET ()
</code></pre></td><td>Returns the last offset of the current record’s partition</td><td>no</td><td>✓</td></tr></tbody></table>

### Date math  <a href="#date-math" id="date-math"></a>

Lenses offer direct support for manipulating dates, times, and timestamps. This functionality is named Date Math. The expressions can be used in any SQL statement where a general-purpose expression is allowed - for example, as part of a where filter, in a group by clause, a select projection, and so on.

Every Date Math expression starts with a base date or time followed by adding or subtracting one or more durations. Most of the time, the base would come from a field, but sometimes the base date may need to be modified - for example extracting the minutes component only from a time value. In addition to this, the base can be generated using a function unrelated to any field in the data record. Check the Date and Time functions

field's valueConsider a scenario where it is required to compute a month from the value of `subscription_date` the field. A naive implementation might be to add (1000 `*` 60 `*` 60 `*` 24 `*` 31) milliseconds to the subscription\_date. But what is a month - a month can have 28 days, 30 days or 31 days (in the Gregorian calendar). In daylight savings timezones, a month may include an extra hour or might miss an hour. Handling the uneven nature of date units in a query is hard.

#### Syntax  <a href="#syntax" id="syntax"></a>

Every Date Math expression starts with a base date or time followed by adding or subtracting one or more durations.

The base date or time (from here onward) is derived from a field in a table or a function such as `now()` or `yesterday()` that generates datetime values.

Duration operations translate the base date by either adding or subtracting a duration defined using the special Date Math shorthand - this is where the math part of the Date Math name comes from.

The shorthand syntax is a unit value followed by a unit symbol. The symbols are:

* y (year)
* M (month)
* w (week)
* d (day)
* h (hour)
* m (minute)
* s (second)

For example, 60s would constitute 60 seconds, equivalent to 1m, and 48h would be equivalent to 2d.

A full Date Math expression would look like the following examples:

```sql
SELECT [STREAM/TABLE]
    now() + 1d as tomorrow
FROM <source>




SELECT [STREAM/TABLE]
    customers.age + '1y' as next_year
FROM <source>


SELECT [STREAM/TABLE]
    orders.delivery.date + '1d' - '1h' as in23Hours
FROM <source>


    //adding a month to a subscription date
SELECT [STREAM/TABLE]
    subscription_date + '1M' AS renewal
FROM <subscriptions>


    //adding a month and a day to a subscription date
SELECT [STREAM/TABLE]
    subscription_date + '1M' + '1d' AS renewal
FROM <subscriptions>

```
