Overview

Datafusion Functions

Since GreptimeDB’s query engine is built based on Apache Arrow DataFusion, GreptimeDB inherits all built-in functions in DataFusion. These functions include:

  • Aggregate functions: such as COUNT, SUM, MIN, MAX, etc. For a detailed list, please refer to Aggregate Functions
  • Scalar functions: such as ABS, COS, FLOOR, etc. For a detailed list, please refer to Scalar Functions
  • Window functions: performs a calculation across a set of table rows that are somehow related to the current row. For a detailed list, please refer to Window Functions

To find all the DataFusion functions, please refer to DataFusion Functions.

arrow_cast

arrow_cast function is from DataFusion’s arrow_cast. It’s illustrated as:

  1. arrow_cast(expression, datatype)

Where the datatype can be any valid Arrow data type in this list. The four timestamp types are:

  • Timestamp(Second, None)
  • Timestamp(Millisecond, None)
  • Timestamp(Microsecond, None)
  • Timestamp(Nanosecond, None)

(Notice that the None means the timestamp is timezone naive)

GreptimeDB Functions

String Functions

DataFusion String Function.GreptimeDB provides:

  • matches(expression, pattern) for full text search.

For details, read the Query Logs.

Math Functions

DataFusion Math Function.

GreptimeDB provides:

  • clamp(value, lower, upper) to restrict a given value between a lower and upper bound:
  1. SELECT CLAMP(10, 0, 1);
  2. +------------------------------------+
  3. | clamp(Int64(10),Int64(0),Int64(1)) |
  4. +------------------------------------+
  5. | 1 |
  6. +------------------------------------+
  1. SELECT CLAMP(0.5, 0, 1)
  2. +---------------------------------------+
  3. | clamp(Float64(0.5),Int64(0),Int64(1)) |
  4. +---------------------------------------+
  5. | 0.5 |
  6. +---------------------------------------+
  • mod(x, y) to get the remainder of a number divided by another number:
  1. SELECT mod(18, 4);
  2. +-------------------------+
  3. | mod(Int64(18),Int64(4)) |
  4. +-------------------------+
  5. | 2 |
  6. +-------------------------+
  • pow(x, y) to get the value of a number raised to the power of another number:
  1. SELECT pow(2, 10);
  2. +-------------------------+
  3. | pow(Int64(2),Int64(10)) |
  4. +-------------------------+
  5. | 1024 |
  6. +-------------------------+

Date and Time Functions

DataFusion Time and Date Function. GreptimeDB provides:

  • date_add(expression, interval) to add an interval value to Timestamp, Date, or DateTime
  1. SELECT date_add('2023-12-06'::DATE, '3 month 5 day');
  1. +----------------------------------------------------+
  2. | date_add(Utf8("2023-12-06"),Utf8("3 month 5 day")) |
  3. +----------------------------------------------------+
  4. | 2024-03-11 |
  5. +----------------------------------------------------+
  • date_sub(expression, interval) to subtract an interval value to Timestamp, Date, or DateTime
  1. SELECT date_sub('2023-12-06 07:39:46.222'::TIMESTAMP_MS, INTERVAL '5 day');
  1. +-----------------------------------------------------------------------------------------------------------------------------------------+
  2. | date_sub(arrow_cast(Utf8("2023-12-06 07:39:46.222"),Utf8("Timestamp(Millisecond, None)")),IntervalMonthDayNano("92233720368547758080")) |
  3. +-----------------------------------------------------------------------------------------------------------------------------------------+
  4. | 2023-12-01 07:39:46.222000 |
  5. +-----------------------------------------------------------------------------------------------------------------------------------------+
  • date_format(expression, fmt) to format Timestamp, Date, or DateTime into string by the format:
  1. SELECT date_format('2023-12-06 07:39:46.222'::TIMESTAMP, '%Y-%m-%d %H:%M:%S:%3f');
  1. +-----------------------------------------------------------------------------------------------------------------------------+
  2. | date_format(arrow_cast(Utf8("2023-12-06 07:39:46.222"),Utf8("Timestamp(Millisecond, None)")),Utf8("%Y-%m-%d %H:%M:%S:%3f")) |
  3. +-----------------------------------------------------------------------------------------------------------------------------+
  4. | 2023-12-06 07:39:46:222 |
  5. +-----------------------------------------------------------------------------------------------------------------------------+

Supported specifiers refer to the chrono::format::strftime module.

  • to_unixtime(expression) to convert the expression into the Unix timestamp in seconds. The argument can be integers (Unix timestamp in milliseconds), Timestamp, Date, DateTime, or String. If the argument is the string type, the function will first try to convert it into a DateTime, Timestamp, or Date.
  1. select to_unixtime('2023-03-01T06:35:02Z');
  1. +-------------------------------------------+
  2. | to_unixtime(Utf8("2023-03-01T06:35:02Z")) |
  3. +-------------------------------------------+
  4. | 1677652502 |
  5. +-------------------------------------------+
  1. select to_unixtime('2023-03-01'::date);
  1. +---------------------------------+
  2. | to_unixtime(Utf8("2023-03-01")) |
  3. +---------------------------------+
  4. | 1677628800 |
  5. +---------------------------------+
  • to_timezone(expression, timezone) to convert the expression by the timezone. The argument can be integers (Unix timestamp in milliseconds), Timestamp, or String. If the argument is the string type, the function will first try to convert it into a Timestamp.
  1. SELECT to_timezone('2022-09-20T14:16:43.012345+08:00', 'Europe/Berlin');
  1. +-----------------------------------------------------------------------------+
  2. | to_timezone(Utf8("2022-09-20T14:16:43.012345+08:00"),Utf8("Europe/Berlin")) |
  3. +-----------------------------------------------------------------------------+
  4. | 2022-09-20 08:16:43.012345 |
  5. +-----------------------------------------------------------------------------+
  1. SELECT to_timezone(1709992225000, 'Asia/Shanghai');
  1. +---------------------------------------------------------+
  2. | to_timezone(Int64(1709992225000),Utf8("Asia/Shanghai")) |
  3. +---------------------------------------------------------+
  4. | 2024-03-09 21:50:25 |
  5. +---------------------------------------------------------+
  • timezone() to retrieve the current session timezone:
  1. select timezone();
  1. +------------+
  2. | timezone() |
  3. +------------+
  4. | UTC |
  5. +------------+

System Functions

  • isnull(expression) to check whether an expression is NULL:
  1. SELECT isnull(1);
  2. +------------------+
  3. | isnull(Int64(1)) |
  4. +------------------+
  5. | 0 |
  6. +------------------+
  1. SELECT isnull(NULL);
  2. +--------------+
  3. | isnull(NULL) |
  4. +--------------+
  5. | 1 |
  6. +--------------+
  • build() retrieves the GreptimeDB build info.
  • version() retrieves the GreptimeDB version.
  • database() retrieves the current session database:
  1. select database();
  2. +------------+
  3. | database() |
  4. +------------+
  5. | public |
  6. +------------+

Admin Functions

GreptimeDB provides ADMIN statement to run the administration functions, please refer to [ADMIN](.(/reference/sql/admin.md) reference.