Other Functions

hostName()

Returns a string with the name of the host that this function was performed on. For distributed processing, this is the name of the remote server host, if the function is performed on a remote server.

getMacro

Gets a named value from the macros section of the server configuration.

Syntax

  1. getMacro(name);

Arguments

  • name — Name to retrieve from the macros section. String.

Returned value

  • Value of the specified macro.

Type: String.

Example

The example macros section in the server configuration file:

  1. <macros>
  2. <test>Value</test>
  3. </macros>

Query:

  1. SELECT getMacro('test');

Result:

  1. ┌─getMacro('test')─┐
  2. Value
  3. └──────────────────┘

An alternative way to get the same value:

  1. SELECT * FROM system.macros
  2. WHERE macro = 'test';
  1. ┌─macro─┬─substitution─┐
  2. test Value
  3. └───────┴──────────────┘

FQDN

Returns the fully qualified domain name.

Syntax

  1. fqdn();

This function is case-insensitive.

Returned value

  • String with the fully qualified domain name.

Type: String.

Example

Query:

  1. SELECT FQDN();

Result:

  1. ┌─FQDN()──────────────────────────┐
  2. clickhouse.ru-central1.internal
  3. └─────────────────────────────────┘

basename

Extracts the trailing part of a string after the last slash or backslash. This function if often used to extract the filename from a path.

  1. basename( expr )

Arguments

  • expr — Expression resulting in a String type value. All the backslashes must be escaped in the resulting value.

Returned Value

A string that contains:

  • The trailing part of a string after the last slash or backslash.

    1. If the input string contains a path ending with slash or backslash, for example, `/` or `c:\`, the function returns an empty string.
  • The original string if there are no slashes or backslashes.

Example

  1. SELECT 'some/long/path/to/file' AS a, basename(a)
  1. ┌─a──────────────────────┬─basename('some\\long\\path\\to\\file')─┐
  2. some\long\path\to\file file
  3. └────────────────────────┴────────────────────────────────────────┘
  1. SELECT 'some\\long\\path\\to\\file' AS a, basename(a)
  1. ┌─a──────────────────────┬─basename('some\\long\\path\\to\\file')─┐
  2. some\long\path\to\file file
  3. └────────────────────────┴────────────────────────────────────────┘
  1. SELECT 'some-file-name' AS a, basename(a)
  1. ┌─a──────────────┬─basename('some-file-name')─┐
  2. some-file-name some-file-name
  3. └────────────────┴────────────────────────────┘

visibleWidth(x)

Calculates the approximate width when outputting values to the console in text format (tab-separated).
This function is used by the system for implementing Pretty formats.

NULL is represented as a string corresponding to NULL in Pretty formats.

  1. SELECT visibleWidth(NULL)
  1. ┌─visibleWidth(NULL)─┐
  2. 4
  3. └────────────────────┘

toTypeName(x)

Returns a string containing the type name of the passed argument.

If NULL is passed to the function as input, then it returns the Nullable(Nothing) type, which corresponds to an internal NULL representation in ClickHouse.

blockSize()

Gets the size of the block.
In ClickHouse, queries are always run on blocks (sets of column parts). This function allows getting the size of the block that you called it for.

byteSize

Returns estimation of uncompressed byte size of its arguments in memory.

Syntax

  1. byteSize(argument [, ...])

Arguments

  • argument — Value.

Returned value

  • Estimation of byte size of the arguments in memory.

Type: UInt64.

Examples

For String arguments the funtion returns the string length + 9 (terminating zero + length).

Query:

  1. SELECT byteSize('string');

Result:

  1. ┌─byteSize('string')─┐
  2. 15
  3. └────────────────────┘

Query:

  1. CREATE TABLE test
  2. (
  3. `key` Int32,
  4. `u8` UInt8,
  5. `u16` UInt16,
  6. `u32` UInt32,
  7. `u64` UInt64,
  8. `i8` Int8,
  9. `i16` Int16,
  10. `i32` Int32,
  11. `i64` Int64,
  12. `f32` Float32,
  13. `f64` Float64
  14. )
  15. ENGINE = MergeTree
  16. ORDER BY key;
  17. INSERT INTO test VALUES(1, 8, 16, 32, 64, -8, -16, -32, -64, 32.32, 64.64);
  18. SELECT key, byteSize(u8) AS `byteSize(UInt8)`, byteSize(u16) AS `byteSize(UInt16)`, byteSize(u32) AS `byteSize(UInt32)`, byteSize(u64) AS `byteSize(UInt64)`, byteSize(i8) AS `byteSize(Int8)`, byteSize(i16) AS `byteSize(Int16)`, byteSize(i32) AS `byteSize(Int32)`, byteSize(i64) AS `byteSize(Int64)`, byteSize(f32) AS `byteSize(Float32)`, byteSize(f64) AS `byteSize(Float64)` FROM test ORDER BY key ASC FORMAT Vertical;

Result:

  1. Row 1:
  2. ──────
  3. key: 1
  4. byteSize(UInt8): 1
  5. byteSize(UInt16): 2
  6. byteSize(UInt32): 4
  7. byteSize(UInt64): 8
  8. byteSize(Int8): 1
  9. byteSize(Int16): 2
  10. byteSize(Int32): 4
  11. byteSize(Int64): 8
  12. byteSize(Float32): 4
  13. byteSize(Float64): 8

If the function takes multiple arguments, it returns their combined byte size.

Query:

  1. SELECT byteSize(NULL, 1, 0.3, '');

Result:

  1. ┌─byteSize(NULL, 1, 0.3, '')─┐
  2. 19
  3. └────────────────────────────┘

materialize(x)

Turns a constant into a full column containing just one value.
In ClickHouse, full columns and constants are represented differently in memory. Functions work differently for constant arguments and normal arguments (different code is executed), although the result is almost always the same. This function is for debugging this behavior.

ignore(…)

Accepts any arguments, including NULL. Always returns 0.
However, the argument is still evaluated. This can be used for benchmarks.

sleep(seconds)

Sleeps ‘seconds’ seconds on each data block. You can specify an integer or a floating-point number.

sleepEachRow(seconds)

Sleeps ‘seconds’ seconds on each row. You can specify an integer or a floating-point number.

currentDatabase()

Returns the name of the current database.
You can use this function in table engine parameters in a CREATE TABLE query where you need to specify the database.

currentUser()

Returns the login of current user. Login of user, that initiated query, will be returned in case distibuted query.

  1. SELECT currentUser();

Alias: user(), USER().

Returned values

  • Login of current user.
  • Login of user that initiated query in case of disributed query.

Type: String.

Example

Query:

  1. SELECT currentUser();

Result:

  1. ┌─currentUser()─┐
  2. default
  3. └───────────────┘

isConstant

Checks whether the argument is a constant expression.

A constant expression means an expression whose resulting value is known at the query analysis (i.e. before execution). For example, expressions over literals are constant expressions.

The function is intended for development, debugging and demonstration.

Syntax

  1. isConstant(x)

Arguments

  • x — Expression to check.

Returned values

  • 1x is constant.
  • 0x is non-constant.

Type: UInt8.

Examples

Query:

  1. SELECT isConstant(x + 1) FROM (SELECT 43 AS x)

Result:

  1. ┌─isConstant(plus(x, 1))─┐
  2. 1
  3. └────────────────────────┘

Query:

  1. WITH 3.14 AS pi SELECT isConstant(cos(pi))

Result:

  1. ┌─isConstant(cos(pi))─┐
  2. 1
  3. └─────────────────────┘

Query:

  1. SELECT isConstant(number) FROM numbers(1)

Result:

  1. ┌─isConstant(number)─┐
  2. 0
  3. └────────────────────┘

isFinite(x)

Accepts Float32 and Float64 and returns UInt8 equal to 1 if the argument is not infinite and not a NaN, otherwise 0.

isInfinite(x)

Accepts Float32 and Float64 and returns UInt8 equal to 1 if the argument is infinite, otherwise 0. Note that 0 is returned for a NaN.

ifNotFinite

Checks whether floating point value is finite.

Syntax

  1. ifNotFinite(x,y)

Arguments

  • x — Value to be checked for infinity. Type: Float*.
  • y — Fallback value. Type: Float*.

Returned value

  • x if x is finite.
  • y if x is not finite.

Example

Query:

  1. SELECT 1/0 as infimum, ifNotFinite(infimum,42)

Result:

  1. ┌─infimum─┬─ifNotFinite(divide(1, 0), 42)─┐
  2. inf 42
  3. └─────────┴───────────────────────────────┘

You can get similar result by using ternary operator: isFinite(x) ? x : y.

isNaN(x)

Accepts Float32 and Float64 and returns UInt8 equal to 1 if the argument is a NaN, otherwise 0.

hasColumnInTable([‘hostname’[, ‘username’[, ‘password’]],] ‘database’, ‘table’, ‘column’)

Accepts constant strings: database name, table name, and column name. Returns a UInt8 constant expression equal to 1 if there is a column, otherwise 0. If the hostname parameter is set, the test will run on a remote server.
The function throws an exception if the table does not exist.
For elements in a nested data structure, the function checks for the existence of a column. For the nested data structure itself, the function returns 0.

bar

Allows building a unicode-art diagram.

bar(x, min, max, width) draws a band with a width proportional to (x - min) and equal to width characters when x = max.

Arguments

  • x — Size to display.
  • min, max — Integer constants. The value must fit in Int64.
  • width — Constant, positive integer, can be fractional.

The band is drawn with accuracy to one eighth of a symbol.

Example:

  1. SELECT
  2. toHour(EventTime) AS h,
  3. count() AS c,
  4. bar(c, 0, 600000, 20) AS bar
  5. FROM test.hits
  6. GROUP BY h
  7. ORDER BY h ASC
  1. ┌──h─┬──────c─┬─bar────────────────┐
  2. 0 292907 █████████▋
  3. 1 180563 ██████
  4. 2 114861 ███▋
  5. 3 85069 ██▋
  6. 4 68543 ██▎
  7. 5 78116 ██▌
  8. 6 113474 ███▋
  9. 7 170678 █████▋
  10. 8 278380 █████████▎
  11. 9 391053 █████████████
  12. 10 457681 ███████████████▎
  13. 11 493667 ████████████████▍
  14. 12 509641 ████████████████▊
  15. 13 522947 █████████████████▍
  16. 14 539954 █████████████████▊
  17. 15 528460 █████████████████▌
  18. 16 539201 █████████████████▊
  19. 17 523539 █████████████████▍
  20. 18 506467 ████████████████▊
  21. 19 520915 █████████████████▎
  22. 20 521665 █████████████████▍
  23. 21 542078 ██████████████████
  24. 22 493642 ████████████████▍
  25. 23 400397 █████████████▎
  26. └────┴────────┴────────────────────┘

transform

Transforms a value according to the explicitly defined mapping of some elements to other ones.
There are two variations of this function:

transform(x, array_from, array_to, default)

x – What to transform.

array_from – Constant array of values for converting.

array_to – Constant array of values to convert the values in ‘from’ to.

default – Which value to use if ‘x’ is not equal to any of the values in ‘from’.

array_from and array_to – Arrays of the same size.

Types:

transform(T, Array(T), Array(U), U) -> U

T and U can be numeric, string, or Date or DateTime types.
Where the same letter is indicated (T or U), for numeric types these might not be matching types, but types that have a common type.
For example, the first argument can have the Int64 type, while the second has the Array(UInt16) type.

If the ‘x’ value is equal to one of the elements in the ‘array_from’ array, it returns the existing element (that is numbered the same) from the ‘array_to’ array. Otherwise, it returns ‘default’. If there are multiple matching elements in ‘array_from’, it returns one of the matches.

Example:

  1. SELECT
  2. transform(SearchEngineID, [2, 3], ['Yandex', 'Google'], 'Other') AS title,
  3. count() AS c
  4. FROM test.hits
  5. WHERE SearchEngineID != 0
  6. GROUP BY title
  7. ORDER BY c DESC
  1. ┌─title─────┬──────c─┐
  2. Yandex 498635
  3. Google 229872
  4. Other 104472
  5. └───────────┴────────┘

transform(x, array_from, array_to)

Differs from the first variation in that the ‘default’ argument is omitted.
If the ‘x’ value is equal to one of the elements in the ‘array_from’ array, it returns the matching element (that is numbered the same) from the ‘array_to’ array. Otherwise, it returns ‘x’.

Types:

transform(T, Array(T), Array(T)) -> T

Example:

  1. SELECT
  2. transform(domain(Referer), ['yandex.ru', 'google.ru', 'vk.com'], ['www.yandex', 'example.com']) AS s,
  3. count() AS c
  4. FROM test.hits
  5. GROUP BY domain(Referer)
  6. ORDER BY count() DESC
  7. LIMIT 10
  1. ┌─s──────────────┬───────c─┐
  2. 2906259
  3. www.yandex 867767
  4. ███████.ru 313599
  5. mail.yandex.ru 107147
  6. ██████.ru 100355
  7. █████████.ru 65040
  8. news.yandex.ru 64515
  9. ██████.net 59141
  10. example.com 57316
  11. └────────────────┴─────────┘

formatReadableSize(x)

Accepts the size (number of bytes). Returns a rounded size with a suffix (KiB, MiB, etc.) as a string.

Example:

  1. SELECT
  2. arrayJoin([1, 1024, 1024*1024, 192851925]) AS filesize_bytes,
  3. formatReadableSize(filesize_bytes) AS filesize
  1. ┌─filesize_bytes─┬─filesize───┐
  2. 1 1.00 B
  3. 1024 1.00 KiB
  4. 1048576 1.00 MiB
  5. 192851925 183.92 MiB
  6. └────────────────┴────────────┘

formatReadableQuantity(x)

Accepts the number. Returns a rounded number with a suffix (thousand, million, billion, etc.) as a string.

It is useful for reading big numbers by human.

Example:

  1. SELECT
  2. arrayJoin([1024, 1234 * 1000, (4567 * 1000) * 1000, 98765432101234]) AS number,
  3. formatReadableQuantity(number) AS number_for_humans
  1. ┌─────────number─┬─number_for_humans─┐
  2. 1024 1.02 thousand
  3. 1234000 1.23 million
  4. 4567000000 4.57 billion
  5. 98765432101234 98.77 trillion
  6. └────────────────┴───────────────────┘

formatReadableTimeDelta

Accepts the time delta in seconds. Returns a time delta with (year, month, day, hour, minute, second) as a string.

Syntax

  1. formatReadableTimeDelta(column[, maximum_unit])

Arguments

  • column — A column with numeric time delta.
  • maximum_unit — Optional. Maximum unit to show. Acceptable values seconds, minutes, hours, days, months, years.

Example:

  1. SELECT
  2. arrayJoin([100, 12345, 432546534]) AS elapsed,
  3. formatReadableTimeDelta(elapsed) AS time_delta
  1. ┌────elapsed─┬─time_delta ─────────────────────────────────────────────────────┐
  2. 100 1 minute and 40 seconds
  3. 12345 3 hours, 25 minutes and 45 seconds
  4. 432546534 13 years, 8 months, 17 days, 7 hours, 48 minutes and 54 seconds
  5. └────────────┴─────────────────────────────────────────────────────────────────┘
  1. SELECT
  2. arrayJoin([100, 12345, 432546534]) AS elapsed,
  3. formatReadableTimeDelta(elapsed, 'minutes') AS time_delta
  1. ┌────elapsed─┬─time_delta ─────────────────────────────────────────────────────┐
  2. 100 1 minute and 40 seconds
  3. 12345 205 minutes and 45 seconds
  4. 432546534 7209108 minutes and 54 seconds
  5. └────────────┴─────────────────────────────────────────────────────────────────┘

least(a, b)

Returns the smallest value from a and b.

greatest(a, b)

Returns the largest value of a and b.

uptime()

Returns the server’s uptime in seconds.

version()

Returns the version of the server as a string.

timezone()

Returns the timezone of the server.

blockNumber

Returns the sequence number of the data block where the row is located.

rowNumberInBlock

Returns the ordinal number of the row in the data block. Different data blocks are always recalculated.

rowNumberInAllBlocks()

Returns the ordinal number of the row in the data block. This function only considers the affected data blocks.

neighbor

The window function that provides access to a row at a specified offset which comes before or after the current row of a given column.

Syntax

  1. neighbor(column, offset[, default_value])

The result of the function depends on the affected data blocks and the order of data in the block.

Warning

It can reach the neighbor rows only inside the currently processed data block.

The rows order used during the calculation of neighbor can differ from the order of rows returned to the user.
To prevent that you can make a subquery with ORDER BY and call the function from outside the subquery.

Arguments

  • column — A column name or scalar expression.
  • offset — The number of rows forwards or backwards from the current row of column. Int64.
  • default_value — Optional. The value to be returned if offset goes beyond the scope of the block. Type of data blocks affected.

Returned values

  • Value for column in offset distance from current row if offset value is not outside block bounds.
  • Default value for column if offset value is outside block bounds. If default_value is given, then it will be used.

Type: type of data blocks affected or default value type.

Example

Query:

  1. SELECT number, neighbor(number, 2) FROM system.numbers LIMIT 10;

Result:

  1. ┌─number─┬─neighbor(number, 2)─┐
  2. 0 2
  3. 1 3
  4. 2 4
  5. 3 5
  6. 4 6
  7. 5 7
  8. 6 8
  9. 7 9
  10. 8 0
  11. 9 0
  12. └────────┴─────────────────────┘

Query:

  1. SELECT number, neighbor(number, 2, 999) FROM system.numbers LIMIT 10;

Result:

  1. ┌─number─┬─neighbor(number, 2, 999)─┐
  2. 0 2
  3. 1 3
  4. 2 4
  5. 3 5
  6. 4 6
  7. 5 7
  8. 6 8
  9. 7 9
  10. 8 999
  11. 9 999
  12. └────────┴──────────────────────────┘

This function can be used to compute year-over-year metric value:

Query:

  1. WITH toDate('2018-01-01') AS start_date
  2. SELECT
  3. toStartOfMonth(start_date + (number * 32)) AS month,
  4. toInt32(month) % 100 AS money,
  5. neighbor(money, -12) AS prev_year,
  6. round(prev_year / money, 2) AS year_over_year
  7. FROM numbers(16)

Result:

  1. ┌──────month─┬─money─┬─prev_year─┬─year_over_year─┐
  2. 2018-01-01 32 0 0
  3. 2018-02-01 63 0 0
  4. 2018-03-01 91 0 0
  5. 2018-04-01 22 0 0
  6. 2018-05-01 52 0 0
  7. 2018-06-01 83 0 0
  8. 2018-07-01 13 0 0
  9. 2018-08-01 44 0 0
  10. 2018-09-01 75 0 0
  11. 2018-10-01 5 0 0
  12. 2018-11-01 36 0 0
  13. 2018-12-01 66 0 0
  14. 2019-01-01 97 32 0.33
  15. 2019-02-01 28 63 2.25
  16. 2019-03-01 56 91 1.62
  17. 2019-04-01 87 22 0.25
  18. └────────────┴───────┴───────────┴────────────────┘

runningDifference(x)

Calculates the difference between successive row values ​​in the data block.
Returns 0 for the first row and the difference from the previous row for each subsequent row.

Warning

It can reach the previous row only inside the currently processed data block.

The result of the function depends on the affected data blocks and the order of data in the block.

The rows order used during the calculation of runningDifference can differ from the order of rows returned to the user.
To prevent that you can make a subquery with ORDER BY and call the function from outside the subquery.

Example:

  1. SELECT
  2. EventID,
  3. EventTime,
  4. runningDifference(EventTime) AS delta
  5. FROM
  6. (
  7. SELECT
  8. EventID,
  9. EventTime
  10. FROM events
  11. WHERE EventDate = '2016-11-24'
  12. ORDER BY EventTime ASC
  13. LIMIT 5
  14. )
  1. ┌─EventID─┬───────────EventTime─┬─delta─┐
  2. 1106 2016-11-24 00:00:04 0
  3. 1107 2016-11-24 00:00:05 1
  4. 1108 2016-11-24 00:00:05 0
  5. 1109 2016-11-24 00:00:09 4
  6. 1110 2016-11-24 00:00:10 1
  7. └─────────┴─────────────────────┴───────┘

Please note - block size affects the result. With each new block, the runningDifference state is reset.

  1. SELECT
  2. number,
  3. runningDifference(number + 1) AS diff
  4. FROM numbers(100000)
  5. WHERE diff != 1
  1. ┌─number─┬─diff─┐
  2. 0 0
  3. └────────┴──────┘
  4. ┌─number─┬─diff─┐
  5. 65536 0
  6. └────────┴──────┘
  1. set max_block_size=100000 -- default value is 65536!
  2. SELECT
  3. number,
  4. runningDifference(number + 1) AS diff
  5. FROM numbers(100000)
  6. WHERE diff != 1
  1. ┌─number─┬─diff─┐
  2. 0 0
  3. └────────┴──────┘

runningDifferenceStartingWithFirstValue

Same as for runningDifference, the difference is the value of the first row, returned the value of the first row, and each subsequent row returns the difference from the previous row.

runningConcurrency

Given a series of beginning time and ending time of events, this function calculates concurrency of the events at each of the data point, that is, the beginning time.

Warning

Events spanning multiple data blocks will not be processed correctly. The function resets its state for each new data block.

The result of the function depends on the order of data in the block. It assumes the beginning time is sorted in ascending order.

Syntax

  1. runningConcurrency(begin, end)

Arguments

Note that two columns begin and end must have the same type.

Returned values

  • The concurrency of events at the data point.

Type: UInt32

Example

Input table:

  1. ┌───────────────begin─┬─────────────────end─┐
  2. 2020-12-01 00:00:00 2020-12-01 00:59:59
  3. 2020-12-01 00:30:00 2020-12-01 00:59:59
  4. 2020-12-01 00:40:00 2020-12-01 01:30:30
  5. 2020-12-01 01:10:00 2020-12-01 01:30:30
  6. 2020-12-01 01:50:00 2020-12-01 01:59:59
  7. └─────────────────────┴─────────────────────┘

Query:

  1. SELECT runningConcurrency(begin, end) FROM example

Result:

  1. ┌─runningConcurrency(begin, end)─┐
  2. 1
  3. 2
  4. 3
  5. 2
  6. 1
  7. └────────────────────────────────┘

MACNumToString(num)

Accepts a UInt64 number. Interprets it as a MAC address in big endian. Returns a string containing the corresponding MAC address in the format AA:BB:CC:DD:EE:FF (colon-separated numbers in hexadecimal form).

MACStringToNum(s)

The inverse function of MACNumToString. If the MAC address has an invalid format, it returns 0.

MACStringToOUI(s)

Accepts a MAC address in the format AA:BB:CC:DD:EE:FF (colon-separated numbers in hexadecimal form). Returns the first three octets as a UInt64 number. If the MAC address has an invalid format, it returns 0.

getSizeOfEnumType

Returns the number of fields in Enum.

  1. getSizeOfEnumType(value)

Arguments:

  • value — Value of type Enum.

Returned values

  • The number of fields with Enum input values.
  • An exception is thrown if the type is not Enum.

Example

  1. SELECT getSizeOfEnumType( CAST('a' AS Enum8('a' = 1, 'b' = 2) ) ) AS x
  1. ┌─x─┐
  2. 2
  3. └───┘

blockSerializedSize

Returns size on disk (without taking into account compression).

  1. blockSerializedSize(value[, value[, ...]])

Arguments

  • value — Any value.

Returned values

  • The number of bytes that will be written to disk for block of values (without compression).

Example

Query:

  1. SELECT blockSerializedSize(maxState(1)) as x

Result:

  1. ┌─x─┐
  2. 2
  3. └───┘

toColumnTypeName

Returns the name of the class that represents the data type of the column in RAM.

  1. toColumnTypeName(value)

Arguments:

  • value — Any type of value.

Returned values

  • A string with the name of the class that is used for representing the value data type in RAM.

Example of the difference betweentoTypeName ' and ' toColumnTypeName

  1. SELECT toTypeName(CAST('2018-01-01 01:02:03' AS DateTime))
  1. ┌─toTypeName(CAST('2018-01-01 01:02:03', 'DateTime'))─┐
  2. DateTime
  3. └─────────────────────────────────────────────────────┘
  1. SELECT toColumnTypeName(CAST('2018-01-01 01:02:03' AS DateTime))
  1. ┌─toColumnTypeName(CAST('2018-01-01 01:02:03', 'DateTime'))─┐
  2. Const(UInt32)
  3. └───────────────────────────────────────────────────────────┘

The example shows that the DateTime data type is stored in memory as Const(UInt32).

dumpColumnStructure

Outputs a detailed description of data structures in RAM

  1. dumpColumnStructure(value)

Arguments:

  • value — Any type of value.

Returned values

  • A string describing the structure that is used for representing the value data type in RAM.

Example

  1. SELECT dumpColumnStructure(CAST('2018-01-01 01:02:03', 'DateTime'))
  1. ┌─dumpColumnStructure(CAST('2018-01-01 01:02:03', 'DateTime'))─┐
  2. DateTime, Const(size = 1, UInt32(size = 1))
  3. └──────────────────────────────────────────────────────────────┘

defaultValueOfArgumentType

Outputs the default value for the data type.

Does not include default values for custom columns set by the user.

  1. defaultValueOfArgumentType(expression)

Arguments:

  • expression — Arbitrary type of value or an expression that results in a value of an arbitrary type.

Returned values

  • 0 for numbers.
  • Empty string for strings.
  • ᴺᵁᴸᴸ for Nullable.

Example

  1. SELECT defaultValueOfArgumentType( CAST(1 AS Int8) )
  1. ┌─defaultValueOfArgumentType(CAST(1, 'Int8'))─┐
  2. 0
  3. └─────────────────────────────────────────────┘
  1. SELECT defaultValueOfArgumentType( CAST(1 AS Nullable(Int8) ) )
  1. ┌─defaultValueOfArgumentType(CAST(1, 'Nullable(Int8)'))─┐
  2. ᴺᵁᴸᴸ
  3. └───────────────────────────────────────────────────────┘

defaultValueOfTypeName

Outputs the default value for given type name.

Does not include default values for custom columns set by the user.

  1. defaultValueOfTypeName(type)

Arguments:

  • type — A string representing a type name.

Returned values

  • 0 for numbers.
  • Empty string for strings.
  • ᴺᵁᴸᴸ for Nullable.

Example

  1. SELECT defaultValueOfTypeName('Int8')
  1. ┌─defaultValueOfTypeName('Int8')─┐
  2. 0
  3. └────────────────────────────────┘
  1. SELECT defaultValueOfTypeName('Nullable(Int8)')
  1. ┌─defaultValueOfTypeName('Nullable(Int8)')─┐
  2. ᴺᵁᴸᴸ
  3. └──────────────────────────────────────────┘

replicate

Creates an array with a single value.

Used for internal implementation of arrayJoin.

  1. SELECT replicate(x, arr);

Arguments:

  • arr — Original array. ClickHouse creates a new array of the same length as the original and fills it with the value x.
  • x — The value that the resulting array will be filled with.

Returned value

An array filled with the value x.

Type: Array.

Example

Query:

  1. SELECT replicate(1, ['a', 'b', 'c'])

Result:

  1. ┌─replicate(1, ['a', 'b', 'c'])─┐
  2. [1,1,1]
  3. └───────────────────────────────┘

filesystemAvailable

Returns amount of remaining space on the filesystem where the files of the databases located. It is always smaller than total free space (filesystemFree) because some space is reserved for OS.

Syntax

  1. filesystemAvailable()

Returned value

  • The amount of remaining space available in bytes.

Type: UInt64.

Example

Query:

  1. SELECT formatReadableSize(filesystemAvailable()) AS "Available space", toTypeName(filesystemAvailable()) AS "Type";

Result:

  1. ┌─Available space─┬─Type───┐
  2. 30.75 GiB UInt64
  3. └─────────────────┴────────┘

filesystemFree

Returns total amount of the free space on the filesystem where the files of the databases located. See also filesystemAvailable

Syntax

  1. filesystemFree()

Returned value

  • Amount of free space in bytes.

Type: UInt64.

Example

Query:

  1. SELECT formatReadableSize(filesystemFree()) AS "Free space", toTypeName(filesystemFree()) AS "Type";

Result:

  1. ┌─Free space─┬─Type───┐
  2. 32.39 GiB UInt64
  3. └────────────┴────────┘

filesystemCapacity

Returns the capacity of the filesystem in bytes. For evaluation, the path to the data directory must be configured.

Syntax

  1. filesystemCapacity()

Returned value

  • Capacity information of the filesystem in bytes.

Type: UInt64.

Example

Query:

  1. SELECT formatReadableSize(filesystemCapacity()) AS "Capacity", toTypeName(filesystemCapacity()) AS "Type"

Result:

  1. ┌─Capacity──┬─Type───┐
  2. 39.32 GiB UInt64
  3. └───────────┴────────┘

finalizeAggregation

Takes state of aggregate function. Returns result of aggregation (or finalized state when using-State combinator).

Syntax

  1. finalizeAggregation(state)

Arguments

Returned value(s)

  • Value/values that was aggregated.

Type: Value of any types that was aggregated.

Examples

Query:

  1. SELECT finalizeAggregation(( SELECT countState(number) FROM numbers(10)));

Result:

  1. ┌─finalizeAggregation(_subquery16)─┐
  2. 10
  3. └──────────────────────────────────┘

Query:

  1. SELECT finalizeAggregation(( SELECT sumState(number) FROM numbers(10)));

Result:

  1. ┌─finalizeAggregation(_subquery20)─┐
  2. 45
  3. └──────────────────────────────────┘

Note that NULL values are ignored.

Query:

  1. SELECT finalizeAggregation(arrayReduce('anyState', [NULL, 2, 3]));

Result:

  1. ┌─finalizeAggregation(arrayReduce('anyState', [NULL, 2, 3]))─┐
  2. 2
  3. └────────────────────────────────────────────────────────────┘

Combined example:

Query:

  1. WITH initializeAggregation('sumState', number) AS one_row_sum_state
  2. SELECT
  3. number,
  4. finalizeAggregation(one_row_sum_state) AS one_row_sum,
  5. runningAccumulate(one_row_sum_state) AS cumulative_sum
  6. FROM numbers(10);

Result:

  1. ┌─number─┬─one_row_sum─┬─cumulative_sum─┐
  2. 0 0 0
  3. 1 1 1
  4. 2 2 3
  5. 3 3 6
  6. 4 4 10
  7. 5 5 15
  8. 6 6 21
  9. 7 7 28
  10. 8 8 36
  11. 9 9 45
  12. └────────┴─────────────┴────────────────┘

See Also

runningAccumulate

Accumulates states of an aggregate function for each row of a data block.

Warning

The state is reset for each new data block.

Syntax

  1. runningAccumulate(agg_state[, grouping]);

Arguments

  • agg_state — State of the aggregate function. AggregateFunction.
  • grouping — Grouping key. Optional. The state of the function is reset if the grouping value is changed. It can be any of the supported data types for which the equality operator is defined.

Returned value

  • Each resulting row contains a result of the aggregate function, accumulated for all the input rows from 0 to the current position. runningAccumulate resets states for each new data block or when the grouping value changes.

Type depends on the aggregate function used.

Examples

Consider how you can use runningAccumulate to find the cumulative sum of numbers without and with grouping.

Query:

  1. SELECT k, runningAccumulate(sum_k) AS res FROM (SELECT number as k, sumState(k) AS sum_k FROM numbers(10) GROUP BY k ORDER BY k);

Result:

  1. ┌─k─┬─res─┐
  2. 0 0
  3. 1 1
  4. 2 3
  5. 3 6
  6. 4 10
  7. 5 15
  8. 6 21
  9. 7 28
  10. 8 36
  11. 9 45
  12. └───┴─────┘

The subquery generates sumState for every number from 0 to 9. sumState returns the state of the sum function that contains the sum of a single number.

The whole query does the following:

  1. For the first row, runningAccumulate takes sumState(0) and returns 0.
  2. For the second row, the function merges sumState(0) and sumState(1) resulting in sumState(0 + 1), and returns 1 as a result.
  3. For the third row, the function merges sumState(0 + 1) and sumState(2) resulting in sumState(0 + 1 + 2), and returns 3 as a result.
  4. The actions are repeated until the block ends.

The following example shows the groupping parameter usage:

Query:

  1. SELECT
  2. grouping,
  3. item,
  4. runningAccumulate(state, grouping) AS res
  5. FROM
  6. (
  7. SELECT
  8. toInt8(number / 4) AS grouping,
  9. number AS item,
  10. sumState(number) AS state
  11. FROM numbers(15)
  12. GROUP BY item
  13. ORDER BY item ASC
  14. );

Result:

  1. ┌─grouping─┬─item─┬─res─┐
  2. 0 0 0
  3. 0 1 1
  4. 0 2 3
  5. 0 3 6
  6. 1 4 4
  7. 1 5 9
  8. 1 6 15
  9. 1 7 22
  10. 2 8 8
  11. 2 9 17
  12. 2 10 27
  13. 2 11 38
  14. 3 12 12
  15. 3 13 25
  16. 3 14 39
  17. └──────────┴──────┴─────┘

As you can see, runningAccumulate merges states for each group of rows separately.

joinGet

The function lets you extract data from the table the same way as from a dictionary.

Gets data from Join tables using the specified join key.

Only supports tables created with the ENGINE = Join(ANY, LEFT, <join_keys>) statement.

Syntax

  1. joinGet(join_storage_table_name, `value_column`, join_keys)

Arguments

  • join_storage_table_name — an identifier indicates where search is performed. The identifier is searched in the default database (see parameter default_database in the config file). To override the default database, use the USE db_name or specify the database and the table through the separator db_name.db_table, see the example.
  • value_column — name of the column of the table that contains required data.
  • join_keys — list of keys.

Returned value

Returns list of values corresponded to list of keys.

If certain doesn’t exist in source table then 0 or null will be returned based on join_use_nulls setting.

More info about join_use_nulls in Join operation.

Example

Input table:

  1. CREATE DATABASE db_test
  2. CREATE TABLE db_test.id_val(`id` UInt32, `val` UInt32) ENGINE = Join(ANY, LEFT, id) SETTINGS join_use_nulls = 1
  3. INSERT INTO db_test.id_val VALUES (1,11)(2,12)(4,13)
  1. ┌─id─┬─val─┐
  2. 4 13
  3. 2 12
  4. 1 11
  5. └────┴─────┘

Query:

  1. SELECT joinGet(db_test.id_val,'val',toUInt32(number)) from numbers(4) SETTINGS join_use_nulls = 1

Result:

  1. ┌─joinGet(db_test.id_val, 'val', toUInt32(number))─┐
  2. 0
  3. 11
  4. 12
  5. 0
  6. └──────────────────────────────────────────────────┘

modelEvaluate(model_name, …)

Evaluate external model.
Accepts a model name and model arguments. Returns Float64.

throwIf(x[, custom_message])

Throw an exception if the argument is non zero.
custom_message - is an optional parameter: a constant string, provides an error message

  1. SELECT throwIf(number = 3, 'Too many') FROM numbers(10);
  1. Progress: 0.00 rows, 0.00 B (0.00 rows/s., 0.00 B/s.) Received exception from server (version 19.14.1):
  2. Code: 395. DB::Exception: Received from localhost:9000. DB::Exception: Too many.

identity

Returns the same value that was used as its argument. Used for debugging and testing, allows to cancel using index, and get the query performance of a full scan. When query is analyzed for possible use of index, the analyzer doesn’t look inside identity functions. Also constant folding is not applied too.

Syntax

  1. identity(x)

Example

Query:

  1. SELECT identity(42)

Result:

  1. ┌─identity(42)─┐
  2. 42
  3. └──────────────┘

randomPrintableASCII

Generates a string with a random set of ASCII printable characters.

Syntax

  1. randomPrintableASCII(length)

Arguments

  • length — Resulting string length. Positive integer.

    1. If you pass `length < 0`, behavior of the function is undefined.

Returned value

  • String with a random set of ASCII printable characters.

Type: String

Example

  1. SELECT number, randomPrintableASCII(30) as str, length(str) FROM system.numbers LIMIT 3
  1. ┌─number─┬─str────────────────────────────┬─length(randomPrintableASCII(30))─┐
  2. 0 SuiCOSTvC0csfABSw=UcSzp2.`rv8x │ 30 │
  3. │ 1 │ 1Ag NlJ &RCN:*>HVPG;PE-nO"SUFD │ 30 │
  4. │ 2 │ /"+<"wUTh:=LjJ Vm!c&hI*m#XTfzz │ 30 │
  5. └────────┴────────────────────────────────┴──────────────────────────────────┘

randomString

Generates a binary string of the specified length filled with random bytes (including zero bytes).

Syntax

  1. randomString(length)

Arguments

  • length — String length. Positive integer.

Returned value

  • String filled with random bytes.

Type: String.

Example

Query:

  1. SELECT randomString(30) AS str, length(str) AS len FROM numbers(2) FORMAT Vertical;

Result:

  1. Row 1:
  2. ──────
  3. str: 3 G : pT ?w тi k aV f6
  4. len: 30
  5. Row 2:
  6. ──────
  7. str: 9 ,] ^ ) ]?? 8
  8. len: 30

See Also

randomFixedString

Generates a binary string of the specified length filled with random bytes (including zero bytes).

Syntax

  1. randomFixedString(length);

Arguments

  • length — String length in bytes. UInt64.

Returned value(s)

  • String filled with random bytes.

Type: FixedString.

Example

Query:

  1. SELECT randomFixedString(13) as rnd, toTypeName(rnd)

Result:

  1. ┌─rnd──────┬─toTypeName(randomFixedString(13))─┐
  2. jhHɨZ'▒ │ FixedString(13) │
  3. └──────────┴───────────────────────────────────┘

randomStringUTF8

Generates a random string of a specified length. Result string contains valid UTF-8 code points. The value of code points may be outside of the range of assigned Unicode.

Syntax

  1. randomStringUTF8(length);

Arguments

  • length — Required length of the resulting string in code points. UInt64.

Returned value(s)

  • UTF-8 random string.

Type: String.

Example

Query:

  1. SELECT randomStringUTF8(13)

Result:

  1. ┌─randomStringUTF8(13)─┐
  2. 𘤗𙉝д兠庇󡅴󱱎󦐪􂕌𔊹𓰛
  3. └──────────────────────┘

getSetting

Returns the current value of a custom setting.

Syntax

  1. getSetting('custom_setting');

Parameter

  • custom_setting — The setting name. String.

Returned value

  • The setting current value.

Example

  1. SET custom_a = 123;
  2. SELECT getSetting('custom_a');

Result

  1. 123

See Also

isDecimalOverflow

Checks whether the Decimal value is out of its (or specified) precision.

Syntax

  1. isDecimalOverflow(d, [p])

Arguments

  • d — value. Decimal.
  • p — precision. Optional. If omitted, the initial precision of the first argument is used. Using of this paratemer could be helpful for data extraction to another DBMS or file. UInt8.

Returned values

  • 1 — Decimal value has more digits then it’s precision allow,
  • 0 — Decimal value satisfies the specified precision.

Example

Query:

  1. SELECT isDecimalOverflow(toDecimal32(1000000000, 0), 9),
  2. isDecimalOverflow(toDecimal32(1000000000, 0)),
  3. isDecimalOverflow(toDecimal32(-1000000000, 0), 9),
  4. isDecimalOverflow(toDecimal32(-1000000000, 0));

Result:

  1. 1 1 1 1

countDigits

Returns number of decimal digits you need to represent the value.

Syntax

  1. countDigits(x)

Arguments

Returned value

Number of digits.

Type: UInt8.

!!! note “Note”
For Decimal values takes into account their scales: calculates result over underlying integer type which is (value * scale). For example: countDigits(42) = 2, countDigits(42.000) = 5, countDigits(0.04200) = 4. I.e. you may check decimal overflow for Decimal64 with countDecimal(x) > 18. It’s a slow variant of isDecimalOverflow.

Example

Query:

  1. SELECT countDigits(toDecimal32(1, 9)), countDigits(toDecimal32(-1, 9)),
  2. countDigits(toDecimal64(1, 18)), countDigits(toDecimal64(-1, 18)),
  3. countDigits(toDecimal128(1, 38)), countDigits(toDecimal128(-1, 38));

Result:

  1. 10 10 19 19 39 39

errorCodeToName

Returned value

  • Variable name for the error code.

Type: LowCardinality(String).

Syntax

  1. errorCodeToName(1)

Result:

  1. UNSUPPORTED_METHOD

tcpPort

Returns native interface TCP port number listened by this server.

Syntax

  1. tcpPort()

Arguments

  • None.

Returned value

  • The TCP port number.

Type: UInt16.

Example

Query:

  1. SELECT tcpPort();

Result:

  1. ┌─tcpPort()─┐
  2. 9000
  3. └───────────┘

See Also

Original article