Skip to main content

Functions

The below functions are currently supported. More can be added on request.

FunctionDescriptionArguments & Input TypesOutput TypeExample
TRY_CASTAttempts to cast a value to a specified type, returning null on failure.(value: T, type: STRING)TSELECT TRY_CAST('123' AS INTEGER);
APPROX_DISTINCTReturns the approximate number of distinct input values.(expr: T)BIGINTSELECT APPROX_DISTINCT(column_name) FROM table_name;
ARRAY_DISTINCTRemoves duplicate values from an array.(array: ARRAY<T>)ARRAY<T>SELECT ARRAY_DISTINCT(ARRAY[1, 2, 2, 3]);
AT_TIMEZONEConverts a timestamp to the specified timezone.(timestamp: TIMESTAMP, timezone: STRING)TIMESTAMPSELECT AT_TIMEZONE(TIMESTAMP '2023-10-01 12:00:00', 'America/New_York');
AVGReturns the average value of the input expression.(expr: T)DOUBLESELECT AVG(column_name) FROM table_name;
CASEEvaluates a list of conditions and returns the first matching result.(conditions: BOOLEAN, results: T, ...)TSELECT CASE WHEN condition THEN result ELSE default END;
CASTConverts a value to a specified type.(value: T, type: STRING)TSELECT CAST('123' AS INTEGER);
CHRReturns the character with the specified ASCII code.(code: INTEGER)STRINGSELECT CHR(65);
COALESCEReturns the first non-null value in the list.(value1: T, value2: T, ...)TSELECT COALESCE(NULL, 'default');
CONCATConcatenates two or more strings.(string1: STRING, string2: STRING, ...)STRINGSELECT CONCAT('Hello', ' ', 'World');
CONCAT_WSConcatenates strings with a separator.(separator: STRING, string1: STRING, ...)STRINGSELECT CONCAT_WS(',', 'a', 'b', 'c');
COUNTReturns the number of input rows for which the expression is not null.(expr: T)BIGINTSELECT COUNT(column_name) FROM table_name;
COUNT(DISTINCT)Returns the number of distinct input values.(expr: T)BIGINTSELECT COUNT(DISTINCT column_name) FROM table_name;
DATE_ADDAdds a specified time interval to a date.(unit: STRING, value: INTEGER, date: DATE)DATESELECT DATE_ADD('day', 1, DATE '2023-10-01');
DATE_DIFFReturns the difference between two dates in specified units.(unit: STRING, date1: DATE, date2: DATE)BIGINTSELECT DATE_DIFF('day', DATE '2023-10-02', DATE '2023-10-01');
DENSE_RANKAssigns a rank to each row within a partition of a result set.()BIGINTSELECT DENSE_RANK() OVER (ORDER BY column_name) FROM table_name;
ELEMENT_ATReturns the element of an array at a specified index.(array: ARRAY<T>, index: INTEGER)TSELECT ELEMENT_AT(ARRAY[1, 2, 3], 2);
FLATTENFlattens a nested array into a single array.(array: ARRAY<ARRAY<T>>)ARRAY<T>SELECT FLATTEN(ARRAY[ARRAY[1, 2], ARRAY[3, 4]]);
GREATESTReturns the greatest value among the arguments.(value1: T, value2: T, ...)T SELECT GREATEST(1, 2, 3);
HASHReturns a hash value for the given expression.(expr: T)BIGINTSELECT HASH('example');
HISTOGRAMComputes a histogram of the input values.(expr: T)MAP<T,BIGINT>SELECT HISTOGRAM(column_name) FROM table_name;
JSON_EXTRACTExtracts a JSON object from a JSON string based on a path.(json: JSON, path: STRING)JSONSELECT JSON_EXTRACT('{"key": "value"}', '$.key');
JSON_EXTRACT_SCALARExtracts a scalar value from a JSON string based on a path.(json: JSON, path: STRING)STRINGSELECT JSON_EXTRACT_SCALAR('{"key": "value"}', '$.key');
JSON_FORMATFormats a JSON object as a string.(json: JSON)STRINGSELECT JSON_FORMAT(CAST('{"key": "value"}' AS JSON));
LAGReturns the value of an expression from a preceding row.(expr: T, offset: INTEGER, default: T)TSELECT LAG(column_name, 1) OVER (ORDER BY column_name) FROM table_name;
LAST_DAY_OF_MONTHReturns the last day of the month for a given date.(date: DATE)DATESELECT LASTDAY(DATE '2023-10-01');
LEADReturns the value of an expression from a following row.(expr: T, offset: INTEGER, default: T)TSELECT LEAD(column_name, 1) OVER (ORDER BY column_name) FROM table_name;
LEASTReturns the least value among the arguments.(value1: T, value2: T, ...)TSELECT LEAST(1, 2, 3);
LOWERConverts a string to lowercase.(string: STRING)STRINGSELECT LOWER('HELLO');
MAPCreates a map from the given key-value pairs.(key: K, value: V)MAP<K,V>SELECT MAP(ARRAY['key1', 'key2'], ARRAY['value1', 'value2']);
MAP_CONCATConcatenates two maps into a single map.(map1: MAP<K, V>, map2: MAP<K, V>)MAP<K,V>SELECT MAP_CONCAT(MAP(ARRAY['a'], ARRAY[1]), MAP(ARRAY['b'], ARRAY[2]));
MAP_ENTRIESReturns an array of key-value pairs from a map.(map: MAP<K, V>)ARRAY<ROW>SELECT MAP_ENTRIES(MAP(ARRAY['a'], ARRAY[1]));
MAP_FROM_ENTRIESCreates a map from an array of key-value pairs.(entries: ARRAY<ROW<K, V>>)MAP<K,V>SELECT MAP_FROM_ENTRIES(ARRAY[ROW('a', 1), ROW('b', 2)]);
MAP_KEYSReturns an array of keys from a map.(map: MAP<K, V>)ARRAY<K>SELECT MAP_KEYS(MAP(ARRAY['a'], ARRAY[1]));
MAP_VALUESReturns an array of values from a map.(map: MAP<K, V>)ARRAY<V>SELECT MAP_VALUES(MAP(ARRAY['a'], ARRAY[1]));
MAXReturns the maximum value of the input expression.(expr: T)TSELECT MAX(column_name) FROM table_name;
MAX_BYReturns the value of the first argument for which the second argument is maximum.(expr: T, weight: U)TSELECT MAX_BY(column_name, weight_column) FROM table_name;
MINReturns the minimum value of the input expression.(expr: T)TSELECT MIN(column_name) FROM table_name;
MIN_BYReturns the value of the first argument for which the second argument is minimum.(expr: T, weight: U)TSELECT MIN_BY(column_name, weight_column) FROM table_name;
MODEReturns the most frequent value in the input expression.(expr: T)TSELECT MODE(column_name) FROM table_name;
POWERRaises the base to the power of the exponent.(base: DOUBLE, exponent: DOUBLE)DOUBLESELECT POWER(2, 3);
RANKAssigns a rank to each row within a partition of a result set.()BIGINTSELECT RANK() OVER (ORDER BY column_name) FROM table_name;
REGEXP_EXTRACTExtracts a substring matching a regular expression.(string: STRING, pattern: STRING)STRINGSELECT REGEXP_EXTRACT('abc123', '\\d+');
REGEXP_EXTRACT_ALLExtracts all substrings matching a regular expression.(string: STRING, pattern: STRING)ARRAY<STRING>SELECT REGEXP_EXTRACT_ALL('abc123', '\\d+');
REGEXP_LIKEChecks if a string matches a regular expression pattern.(string: STRING, pattern: STRING)BOOLEANSELECT REGEXP_LIKE('abc123', '\\d+');
REGEXP_REPLACEReplaces substrings matching a regular expression with a replacement.(string: STRING, pattern: STRING, replacement: STRING)STRINGSELECT REGEXP_REPLACE('abc123', '\\d+', '');
REVERSEReverses the characters in a string.(string: STRING)STRINGSELECT REVERSE('hello');
ROUNDRounds a number to a specified number of decimal places.(number: DOUBLE, decimals: INTEGER)DOUBLESELECT ROUND(123.456, 2);
ROW_NUMBERAssigns a unique number to each row within a partition of a result set.()BIGINTSELECT ROW_NUMBER() OVER (ORDER BY column_name) FROM table_name;
SEQUENCEGenerates a sequence of numbers.(start: BIGINT, stop: BIGINT, step: BIGINT)ARRAY<BIGINT>SELECT SEQUENCE(1, 10, 2);
SUBSTRINGExtracts a substring from a string.(string: STRING, start: INTEGER, length: INTEGER)STRINGSELECT SUBSTRING('hello', 1, 3);
SUMReturns the sum of the input expression.(expr: T)TSELECT SUM(column_name) FROM table_name;
TO_MILLISECONDSConverts a timestamp to milliseconds since epoch.(timestamp: TIMESTAMP)BIGINTSELECT TO_MILLISECONDS(TIMESTAMP '2023-10-01 12:00:00');
CARDINALITYReturns the number of elements in an array.(array: ARRAY<T>)BIGINTSELECT CARDINALITY(ARRAY[1, 2, 3]);
ARRAY_JOINJoins array elements into a single string with a delimiter.(array: ARRAY<STRING>, delimiter: STRING, nullReplacement: STRING)STRINGSELECT ARRAY_JOIN(ARRAY['a', 'b', 'c'], ',');
ARRAY_MAXReturns the maximum value in an array.(array: ARRAY<T>)TSELECT ARRAY_MAX(ARRAY[1, 2, 3]);
ARRAY_MINReturns the minimum value in an array.(array: ARRAY<T>)TSELECT ARRAY_MIN(ARRAY[1, 2, 3]);
ARRAY_SORTSorts the elements of an array.(array: ARRAY<T>)ARRAY<T>SELECT ARRAY_SORT(ARRAY[3, 1, 2]);
CONTAINSChecks if an array contains a specified value.(array: ARRAY<T>, value: T)BOOLEANSELECT CONTAINS(ARRAY[1, 2, 3], 2);
SEQUENCEGenerates a sequence of numbers.(start: BIGINT, stop: BIGINT, step: BIGINT)ARRAY<BIGINT>SELECT SEQUENCE(1, 10, 2);
VARIANCEReturns the variance of the input expression.(expr: T)DOUBLESELECT VARIANCE(column_name) FROM table_name;
APPROX_PERCENTILEReturns an approximate percentile of the input expression.(expr: T, percentile: DOUBLE)T SELECT APPROX_PERCENTILE(column_name, 0.95) FROM table_name;
ARRAY_AGGAggregates all the input values into an array.(expr: T)ARRAY<T>SELECT ARRAY_AGG(column_name) FROM table_name;
STDDEVReturns the standard deviation of the input expression.(expr: T)DOUBLESELECT STDDEV(column_name) FROM table_name;
NULLIFReturns null if the two values are equal, otherwise returns the first value.(value1: T, value2: T)TSELECT NULLIF(column_name, 0) FROM table_name;
LTRIMRemoves leading whitespace from a string.(string: STRING)STRINGSELECT LTRIM(' hello')
RTRIMRemoves trailing whitespace from a string.(string: STRING)STRINGSELECT RTRIM('hello ');
ABSReturns the absolute value of a number.(number: DOUBLE)DOUBLESELECT ABS(-5);
ANY_VALUEReturns any value from the input expression.(expr: T)TSELECT ANY_VALUE(column_name) FROM table_name;
ARRAYCreates an empty array of the specified type.()ARRAY<T>SELECT ARRAY();
CEILRounds a number up to the nearest integer.(number: DOUBLE)DOUBLESELECT CEIL(3.14);
COUNTRYReturns the country name for a given country code.(code: STRING)STRINGSELECT COUNTRY('US');
DATE_FORMATFormats a date according to the specified format string.(date: DATE, format: STRING)STRINGSELECT DATE_FORMAT(DATE '2023-10-01', '%Y-%m-%d');
DATE_TRUNCTruncates a date to the specified precision.(unit: STRING, timestamp: TIMESTAMP)TIMESTAMPSELECT DATE_TRUNC('month', TIMESTAMP '2023-10-15 10:30:00');
FILTERFilters elements from an array based on a lambda predicate.(array: ARRAY<T>, lambda: T -> BOOLEAN)ARRAY<T>SELECT FILTER(ARRAY[1, 2, 3], x -> x > 1);
FLOORRounds a number down to the nearest integer.(number: DOUBLE)DOUBLESELECT FLOOR(3.14);
FROM_UNIXTIMEConverts Unix timestamp to timestamp.(unixtime: BIGINT)TIMESTAMPSELECT FROM_UNIXTIME(1633046400);
IFReturns second argument if first is true, third argument otherwise.(condition: BOOLEAN, true_value: T, false_value: T)TSELECT IF(x > 0, 'positive', 'negative');
LENGTHReturns the length of a string.(string: STRING)INTEGERSELECT LENGTH('hello');
NOWReturns the current timestamp.()TIMESTAMPSELECT NOW();
REDUCEReduces array elements to a single value using a lambda function.(array: ARRAY<T>, initial: S, lambda: (S,T) -> S)SSELECT REDUCE(ARRAY[1, 2, 3], 0, (s, x) -> s + x);
REPLACEReplaces all occurrences of search string with replacement string.(string: STRING, search: STRING, replacement: STRING)STRINGSELECT REPLACE('hello world', 'world', 'there');
SHIFT_BACKWARDShifts timestamp backward by specified interval.(timestamp: TIMESTAMP, interval: STRING)TIMESTAMPSELECT SHIFT_BACKWARD(TIMESTAMP '2023-10-01 12:00:00', '1 day');
SHIFT_FORWARDShifts timestamp forward by specified interval.(timestamp: TIMESTAMP, interval: STRING)TIMESTAMPSELECT SHIFT_FORWARD(TIMESTAMP '2023-10-01 12:00:00', '1 day');
SPLITSplits string into array using delimiter.(string: STRING, delimiter: STRING)ARRAY<STRING>SELECT SPLIT('a,b,c', ',');
SQRTReturns the square root of a number.(number: DOUBLE)DOUBLESELECT SQRT(16);
TO_UNIXTIMEConverts timestamp to Unix timestamp.(timestamp: TIMESTAMP)BIGINTSELECT TO_UNIXTIME(TIMESTAMP '2023-10-01 12:00:00');
TRANSFORMTransforms array elements using a lambda function.(array: ARRAY<T>, lambda: T -> U)ARRAY<U>SELECT TRANSFORM(ARRAY[1, 2, 3], x -> x * 2);
UPPERConverts a string to uppercase.(string: STRING)STRINGSELECT UPPER('hello');