2017-04-26 19:16:38 +00:00
Other functions
2017-06-13 04:15:47 +00:00
---------------
2017-04-03 19:49:50 +00:00
hostName()
2017-06-13 04:15:47 +00:00
~~~~~~~~~~
2017-04-26 19:16:38 +00:00
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.
2017-04-03 19:49:50 +00:00
visibleWidth(x)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
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.
2017-04-03 19:49:50 +00:00
toTypeName(x)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Gets the type name. Returns a string containing the type name of the passed argument.
2017-04-03 19:49:50 +00:00
blockSize()
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
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.
2017-04-03 19:49:50 +00:00
materialize(x)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
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.
2017-04-03 19:49:50 +00:00
ignore(...)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
A function that accepts any arguments and always returns 0.
However, the argument is still calculated. This can be used for benchmarks.
2017-04-03 19:49:50 +00:00
sleep(seconds)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Sleeps 'seconds' seconds on each data block. You can specify an integer or a floating-point number.
2017-04-03 19:49:50 +00:00
currentDatabase()
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
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..
2017-04-03 19:49:50 +00:00
isFinite(x)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Accepts Float32 and Float64 and returns UInt8 equal to 1 if the argument is not infinite and not a NaN, otherwise 0.
2017-04-03 19:49:50 +00:00
isInfinite(x)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
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
2017-04-03 19:49:50 +00:00
isNaN(x)
2017-06-13 04:15:47 +00:00
~~~~~~~~
2017-04-26 19:16:38 +00:00
Accepts Float32 and Float64 and returns UInt8 equal to 1 if the argument is a NaN, otherwise 0.
2017-04-03 19:49:50 +00:00
2017-07-19 21:01:57 +00:00
hasColumnInTable(['hostname'[, 'username'[, 'password']],] 'database', 'table', 'column')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Accepts constant String columns - database name, table name and column name. Returns constant UInt8 value, equal to 1 if column exists,
2017-07-19 21:01:57 +00:00
otherwise 0. If hostname is specified, the check will proceed on remote host.
2017-04-26 19:16:38 +00:00
If table doesn't exist than exception is thrown.
For elements of nested data structure function checks existence of column. For nested data structure 0 is returned.
2017-04-03 19:49:50 +00:00
bar
2017-06-13 04:15:47 +00:00
~~~
2017-04-26 19:16:38 +00:00
Allows building a unicode-art diagram.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
`` bar(x, min, max, width) `` - Draws a band with a width proportional to (x - min) and equal to 'width' characters when x == max.
`` min, max `` - Integer constants. The value must fit in Int64.
`` width `` - Constant, positive number, may be a fraction.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
The band is drawn with accuracy to one eighth of a symbol. Example:
2017-04-03 19:49:50 +00:00
.. code-block :: sql
SELECT
toHour(EventTime) AS h,
count() AS c,
bar(c, 0, 600000, 20) AS bar
FROM test.hits
GROUP BY h
ORDER BY h ASC
2017-06-13 04:15:47 +00:00
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌──h─┬──────c─┬─bar────────────────┐
│ 0 │ 292907 │ █████████▋ │
│ 1 │ 180563 │ ██████ │
│ 2 │ 114861 │ ███▋ │
│ 3 │ 85069 │ ██▋ │
│ 4 │ 68543 │ ██▎ │
│ 5 │ 78116 │ ██▌ │
│ 6 │ 113474 │ ███▋ │
│ 7 │ 170678 │ █████▋ │
│ 8 │ 278380 │ █████████▎ │
│ 9 │ 391053 │ █████████████ │
│ 10 │ 457681 │ ███████████████▎ │
│ 11 │ 493667 │ ████████████████▍ │
│ 12 │ 509641 │ ████████████████▊ │
│ 13 │ 522947 │ █████████████████▍ │
│ 14 │ 539954 │ █████████████████▊ │
│ 15 │ 528460 │ █████████████████▌ │
│ 16 │ 539201 │ █████████████████▊ │
│ 17 │ 523539 │ █████████████████▍ │
│ 18 │ 506467 │ ████████████████▊ │
│ 19 │ 520915 │ █████████████████▎ │
│ 20 │ 521665 │ █████████████████▍ │
│ 21 │ 542078 │ ██████████████████ │
│ 22 │ 493642 │ ████████████████▍ │
│ 23 │ 400397 │ █████████████▎ │
└────┴────────┴────────────────────┘
transform
2017-06-13 04:15:47 +00:00
~~~~~~~~~
2017-04-26 19:16:38 +00:00
Transforms a value according to the explicitly defined mapping of some elements to other ones.
There are two variations of this function:
2017-04-03 19:49:50 +00:00
1. `` transform(x, array_from, array_to, default) ``
2017-04-26 19:16:38 +00:00
`` x `` - What to transform
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
`` array_from `` - Constant array of values for converting.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
`` array_to `` - Constant array of values to convert the values in 'from' to.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
`` default `` - Constant. Which value to use if 'x' is not equal to one of the values in 'from'
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
`` 'array_from' `` and `` 'array_to' `` are arrays of the same size.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Types:
2017-04-03 19:49:50 +00:00
`` transform(T, Array(T), Array(U), U) -> U ``
2017-04-26 19:16:38 +00:00
`` '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.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
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.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Example:
2017-04-03 19:49:50 +00:00
.. code-block :: sql
SELECT
transform(SearchEngineID, [2, 3], ['Яндекс', 'Google'], 'Остальные') AS title,
count() AS c
FROM test.hits
WHERE SearchEngineID != 0
GROUP BY title
ORDER BY c DESC
2017-06-13 04:15:47 +00:00
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌─title─────┬──────c─┐
│ Яндекс │ 498635 │
│ Google │ 229872 │
│ Остальные │ 104472 │
└───────────┴────────┘
2. `` transform(x, array_from, array_to) ``
2017-04-26 19:16:38 +00:00
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'.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Types:
2017-04-03 19:49:50 +00:00
`` transform(T, Array(T), Array(T)) -> T ``
2017-04-26 19:16:38 +00:00
Example:
2017-04-03 19:49:50 +00:00
.. code-block :: sql
SELECT
transform(domain(Referer), ['yandex.ru', 'google.ru', 'vk.com'], ['www.yandex', 'ввв.яндекс.рф', 'example.com']) AS s,
count() AS c
FROM test.hits
GROUP BY domain(Referer)
ORDER BY count() DESC
LIMIT 10
2017-06-13 04:15:47 +00:00
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌─s──────────────┬───────c─┐
│ │ 2906259 │
│ www.yandex │ 867767 │
│ ███████.ru │ 313599 │
│ mail.yandex.ru │ 107147 │
│ ввв.яндекс.рф │ 105668 │
│ ██████.ru │ 100355 │
│ █████████.ru │ 65040 │
│ news.yandex.ru │ 64515 │
│ ██████.net │ 59141 │
│ example.com │ 57316 │
└────────────────┴─────────┘
formatReadableSize(x)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Gets a size (number of bytes). Returns a string that contains rounded size with the suffix (KiB, MiB etc.).
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Example:
2017-04-03 19:49:50 +00:00
.. code-block :: sql
SELECT
arrayJoin([1, 1024, 1024*1024, 192851925]) AS filesize_bytes,
formatReadableSize(filesize_bytes) AS filesize
2017-06-13 04:15:47 +00:00
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌─filesize_bytes─┬─filesize───┐
│ 1 │ 1.00 B │
│ 1024 │ 1.00 KiB │
│ 1048576 │ 1.00 MiB │
│ 192851925 │ 183.92 MiB │
└────────────────┴────────────┘
least(a, b)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns the least element of a and b.
2017-04-03 19:49:50 +00:00
greatest(a, b)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns the greatest element of a and b
2017-04-03 19:49:50 +00:00
uptime()
2017-06-13 04:15:47 +00:00
~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns server's uptime in seconds.
2017-04-03 19:49:50 +00:00
version()
2017-06-13 04:15:47 +00:00
~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns server's version as a string.
2017-04-03 19:49:50 +00:00
rowNumberInAllBlocks()
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns an incremental row number within all blocks that were processed by this function.
2017-04-03 19:49:50 +00:00
runningDifference(x)
2017-06-13 04:15:47 +00:00
~~~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Calculates the difference between consecutive values in the data block.
Result of the function depends on the order of the data in the blocks.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
It works only inside of the each processed block of data. Data splitting in the blocks is not explicitly controlled by the user.
2017-06-13 04:15:47 +00:00
If you specify `` ORDER BY `` in subquery and call runningDifference outside of it, you could get an expected result.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Example:
2017-04-03 19:49:50 +00:00
.. code-block :: sql
SELECT
EventID,
EventTime,
runningDifference(EventTime) AS delta
FROM
(
SELECT
EventID,
EventTime
FROM events
WHERE EventDate = '2016-11-24'
ORDER BY EventTime ASC
LIMIT 5
)
2017-06-13 04:15:47 +00:00
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌─EventID─┬───────────EventTime─┬─delta─┐
│ 1106 │ 2016-11-24 00:00:04 │ 0 │
│ 1107 │ 2016-11-24 00:00:05 │ 1 │
│ 1108 │ 2016-11-24 00:00:05 │ 0 │
│ 1109 │ 2016-11-24 00:00:09 │ 4 │
│ 1110 │ 2016-11-24 00:00:10 │ 1 │
└─────────┴─────────────────────┴───────┘
2017-05-10 15:33:41 +00:00
MACNumToString(num)
2017-05-15 14:52:47 +00:00
~~~~~~~~~~~~~~~~~~~
2017-05-10 15:33:41 +00:00
Takes a UInt64 number. Interprets it as an 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)
2017-05-15 14:52:47 +00:00
~~~~~~~~~~~~~~~~~
2017-05-10 15:33:41 +00:00
The reverse function of MACNumToString. If the MAC address has an invalid format, it returns 0.
2017-05-15 14:52:47 +00:00
MACStringToOUI(s)
~~~~~~~~~~~~~~~~~
Takes MAC address in the format AA:BB:CC:DD:EE:FF (colon-separated numbers in hexadecimal form). Returns first three octets as UInt64 number. If the MAC address has an invalid format, it returns 0.