Other functions
Miscellaneous functions for various purposes.
This section covers miscellaneous ClickHouse utility functions that include::
- Query formatting (e.g.,
formatQuery
,formatQuerySingleLine
) - File path manipulation (
basename
) - Version information (
version()
) - Block and row numbering (
blockNumber()
,rowNumberInBlock()
) - Size formatting (
formatReadableSize
,formatReadableDecimalSize
) - Mathematical and type-checking operations (
least
,greatest
,isFinite
,isConstant
)
ClickHouse function reference
basename
Extracts the filename from a path.
Syntax:
Arguments:
path
(String
): A string containing a file path.
Returns:
- The filename extracted from the path. [
String
]
Example:
Result:
This function extracts the filename portion from a given path:
- If the path ends with a slash or backslash, it returns an empty string.
- If there are no slashes or backslashes, it returns the original string.
- Backslashes must be escaped in string literals.
Additional Examples:
Result:
When using Windows-style paths, remember to escape backslashes:
Result:
visibleWidth
Calculates the approximate width when outputting values to the console in text format (tab-separated).
Syntax:
Arguments:
x
(any type): The value to calculate the width for.
Returns:
- The approximate width of the value when displayed in the console. [
UInt64
]
Details:
- This function is used by the system to implement [Pretty formats].
- For NULL values, it returns the width of the string ‘NULL’ in Pretty formats.
Example:
Result:
In this example:
- ‘Crunchy Taco’ has a visible width of 12 characters.
- The number 42 has a visible width of 2 characters.
- NULL has a visible width of 4 characters (the length of ‘NULL’ in Pretty formats).
This function is particularly useful when formatting output or calculating column widths for display purposes.
toTypeName
Returns the name of the data type of the passed argument.
Syntax
Arguments
x
(any type): Value of any type.
Returns:
- A string containing the name of the type.
Example
Result:
In this example:
42
is identified asUInt8
- The string ‘Carne asada’ is identified as
String
- The
now()
function returns aDateTime
type
The function returns the type name exactly as it appears in the ClickHouse source code. Therefore, for some data types, it may return a name that differs from the one used in SQL syntax.
For NULL
values, the function returns the type Nullable(Nothing)
, which corresponds to ClickHouse’s internal NULL representation.
blockSize
Returns the number of rows in the current data block.
Syntax:
Returns:
The number of rows in the current block. [UInt64
]
Example:
Result:
In this example, the block size is 3 to demonstrate how blockSize()
returns the size of each processed block. The last block contains only one row because there are only 10 numbers in total.
The blockSize()
function is primarily used for debugging and testing purposes. In production queries, the block size can vary and should not be relied upon for business logic.
byteSize
Returns an estimation of the uncompressed byte size of its arguments in memory.
Syntax:
Arguments:
argument
(any type): Value of any type.
Returns:
- Estimation of byte size of the arguments in memory. (
UInt64
)
Examples:
For (String
) arguments, the function returns the string length + 9 (terminating zero + length).
Result:
Query demonstrating byteSize for various data types:
Result:
If the function has multiple arguments, it accumulates their byte sizes:
Result:
This function can be useful for estimating memory usage of different data structures and optimizing storage or query performance.
currentDatabase
Returns the name of the current database.
Syntax:
Returns:
- The name of the current database. (
String
)
Example:
Result:
isConstant
Checks whether the argument is a constant expression.
Syntax:
Arguments:
x
: Expression to check.
Returns:
- 1 if
x
is constant. (UInt8
) - 0 if
x
is non-constant. (UInt8
)
This function is primarily intended for development, debugging, and demonstration purposes.
Example:
Result:
In this example, isConstant
returns 0 because number
is not a constant expression.
Result:
Here, isConstant
returns 1 because 42 is a constant value.
Result:
In this case, isConstant
returns 1 because cos(pi)
is a constant expression, as pi
is defined as a constant in the WITH clause.
Note that isConstant
evaluates the constancy of an expression during query analysis, before actual execution. This makes it useful for optimizing queries and understanding how ClickHouse processes different types of expressions.
isFinite
Checks whether a floating-point value is finite.
Syntax:
Arguments:
x
(Float32
orFloat64
): The value to check.
Returns:
1
if x
is finite, 0
otherwise. Note that 0
is returned for NaN values. [UInt8
]
Example:
Result:
In this example, isFinite
checks if each taco_price
is finite. The result 1
indicates a finite price, while 0
indicates an infinite price or NaN.
isInfinite
Checks if a floating-point value is infinite.
Syntax:
Arguments:
x
(Float32
orFloat64
): The value to check.
Returns:
1
if x
is infinite, 0
otherwise.
0
is returned for NaN values.UInt8
]
Example:
Result:
In this example, isInfinite
checks if each taco_price
is infinite. The result 1
indicates an infinite price, while 0
indicates a finite price or NaN.
ifNotFinite
Checks if a value is finite and returns an alternative value if it’s not.
Syntax:
Arguments:
x
(Float32
orFloat64
): The value to check.y
(Float32
orFloat64
): The value to return ifx
is not finite.
Returns:
- If
x
is finite, returnsx
. - If
x
is infinite or NaN, returnsy
.
Example:
Result:
In this example:
finite
returns 0.0 because it’s a finite number.infinite
returns 42.0 because 1/0 is infinity.nan
returns 42.0 because 0/0 is NaN (Not a Number).
This function is useful for handling potential infinite or NaN values in floating-point calculations, especially when dealing with division operations or mathematical functions that might produce such results.
isNaN
Checks whether the argument is NaN (Not a Number).
Syntax
Arguments
x
(Float32
orFloat64
): Value to check.
Returns:
- 1 if the argument is NaN.
- 0 otherwise.
Type: UInt8
Example
Query:
Result:
In this example:
0/0
results in NaN, soisNaN(0/0)
returns 1.1
and0/1
are not NaN, soisNaN
returns 0 for both.tacos_eaten / total_tacos
(5/0) results in a division by zero, which is NaN, soisNaN
returns 1.
This function is useful for detecting undefined mathematical operations or missing data in floating-point calculations, particularly in scenarios involving consumption metrics.
bar
Builds a bar chart.
Syntax:
Arguments:
x
(numeric): Size to display.min
(Int32
): Minimum value for the bar.max
(Int32
): Maximum value for the bar.width
(UInt8
): Width of the bar in characters.
Returns:
A string representing a bar chart.
The function draws a band with width proportional to (x - min)
and equal to width
characters when x = max
. The band is drawn with accuracy to one eighth of a symbol.
Example:
Result:
This example generates a bar chart showing the distribution of taco orders throughout the day. Each bar represents the number of orders for a given hour, with the maximum width (20 characters) corresponding to 2000 orders.
formatReadableDecimalSize
Formats a size in bytes into a human-readable string using decimal (base 10) units.
Syntax:
Arguments:
bytes
(UInt64
): The number of bytes to format.
Returns:
A human-readable string representing the size with an appropriate decimal unit suffix (B, KB, MB, GB, TB, PB, or EB). [String
]
Example:
Result:
In this example, we format various taco order payload sizes from bytes to human-readable strings. The function automatically chooses the most appropriate unit for each value.
This function uses decimal (base 10) units, where 1 KB = 1000 bytes. For binary (base 2) units, use the formatReadableSize
function instead.
formatReadableSize
Formats a size in bytes into a human-readable string with appropriate units (B, KiB, MiB, etc.).
Syntax:
Alias:
- FORMAT_BYTES
Arguments:
x
(UInt64
): Size in bytes.
Returns:
A human-readable string representing the size with appropriate units. (String
)
Example:
Result:
In this example, we format various file sizes from bytes into human-readable formats.
- This function uses binary prefixes (KiB, MiB, etc.) rather than decimal prefixes (KB, MB, etc.).
The inverse operations of this function are [parseReadableSize], [parseReadableSizeOrZero], and [parseReadableSizeOrNull].
Arguments:
x
(numeric): The number to format.
Returns:
A human-readable string representation of the number with an appropriate suffix. (String
)
Example:
Result:
This function is particularly useful when displaying large numbers in a more digestible format.
The function rounds the number and chooses an appropriate suffix to make it more readable. It’s ideal for presenting statistics or large quantities in user interfaces or reports.
formatReadableTimeDelta
Formats a time interval (delta) in seconds into a human-readable string representation.
Syntax:
Arguments:
column
(numeric) — A column with a numeric time delta in seconds.maximum_unit
(String
, optional) — Maximum unit to show. Default value: years.- Acceptable values: nanoseconds, microseconds, milliseconds, seconds, minutes, hours, days, months, years.
minimum_unit
(String
, optional) — Minimum unit to show. All smaller units are truncated.- Acceptable values: nanoseconds, microseconds, milliseconds, seconds, minutes, hours, days, months, years.
- Default value: seconds if maximum_unit is seconds or bigger, nanoseconds otherwise.
Returns:
A string containing the formatted time delta with year/month/day/hour/minute/second/millisecond/microsecond/nanosecond components.
Example:
Result:
This example demonstrates formatting various time deltas into readable strings.
You can also specify custom maximum and minimum units:
Result:
In this case, the function formats the time delta using minutes as the maximum unit and nanoseconds as the minimum unit.
parseTimeDelta
Parses a string representing a time duration and converts it to a number of seconds.
Syntax:
Arguments:
time_string
(String
) — A string containing a sequence of numbers followed by time unit indicators.
Returns:
- The total number of seconds represented by the input string. (
Float64
)
Example:
Result:
In this example, we calculate the preparation time for a gourmet taco recipe. The function parses ‘2h30m’ (2 hours and 30 minutes) and returns the equivalent in seconds.
- Supported time units include:
ns
(nanoseconds)us
(microseconds)ms
(milliseconds)s
orsec
(seconds)m
ormin
(minutes)h
orhour
(hours)d
orday
(days)w
orweek
(weeks)mo
ormonth
(months, assuming 30 days per month)y
oryear
(years, assuming 365 days per year)
- The function is case-insensitive for unit indicators.
- Multiple duration components can be combined, e.g., ‘1h30m15s’.
- If the input string is invalid or cannot be parsed, the function will throw an exception.
This function is useful for converting human-readable time durations into a standardized seconds format, which can be used in calculations or comparisons involving time intervals.
least
Returns the smaller value of a and b.
Syntax:
Arguments:
a
(any comparable type): First value to compare.b
(any comparable type): Second value to compare. Must be of the same type asa
.
Returns:
The smaller of a
and b
. The return type is the same as the input type.
Example:
Result:
In this example:
min_value
returns 3, the smaller of 5 and 3.min_string
returns ‘guacamole’, which comes before ‘salsa’ lexicographically.min_date
returns the earlier date, ‘2023-04-15’.
The least
function can be particularly useful in queries where you need to find the minimum value across multiple columns or when setting a lower bound for a value.
greatest
Returns the larger value of two arguments.
Syntax:
Arguments:
a
(any comparable type): First value to compare.b
(any comparable type): Second value to compare.
Returns:
The larger of the two input values.
Example:
Result:
In this example:
- For numeric values,
greatest
returns 5 as it’s larger than 3. - For strings,
greatest
performs a lexicographical comparison, returning ‘salsa’ as it comes after ‘guacamole’ alphabetically.
The greatest
function can be used with various data types, including numbers, strings, and dates. When comparing different data types, type conversion rules apply.
version
Returns the current version of ClickHouse as a string.
Syntax:
Returns:
A string in the format:
Example:
Result:
This function returns the version of the ClickHouse server currently running. It can be useful for:
- Debugging version-specific issues
- Ensuring compatibility with certain features
- Logging or reporting purposes
If executed in the context of a distributed table, this function generates a normal column with values relevant to each shard. Otherwise, it produces a constant value.
blockNumber
Returns a monotonically increasing sequence number of the block containing the row. The returned block number is updated on a best-effort basis, i.e. it may not be fully accurate.
Syntax:
Returns:
- Sequence number of the data block where the row is located. [
UInt64
]
Example:
Result:
In this example, blockNumber()
returns the sequence number of each data block processed.
rowNumberInBlock
Returns the row number within the current data block being processed.
Syntax:
Returns:
The ordinal number of the row within the current data block, starting from 0. [UInt64
]
Example:
Result:
In this example, rowNumberInBlock()
returns the row number within each block. The numbering restarts at 0 for each new block.
The result of this function depends on the affected data blocks and the order of data in the block. It’s primarily used for debugging and testing purposes.
rowNumberInAllBlocks
Returns a unique row number for each row processed by the function. The returned numbers start at 0.
Syntax:
Returns:
- Ordinal number of the row in the data block starting from 0. [
UInt64
]
Example:
Result:
In this example, rowNumberInAllBlocks()
assigns a unique number to each taco order, regardless of the block size. This can be useful for generating unique identifiers or for analyzing the overall order of rows in a query result.
The order of rows during calculation of rowNumberInAllBlocks()
can differ from the order of rows returned to the user. To ensure a specific order, use an ORDER BY
clause in your query.
identity
Returns the input value unchanged. This function is primarily used for debugging and testing purposes.
Syntax:
Arguments:
x
(any type): A value of any type.
Returns:
- The input value, unchanged.
Example:
Result:
This function can be useful in several scenarios:
- Debugging: When you want to examine the exact value being passed through a complex query without any transformations.
- Testing: To verify that data is being processed correctly without any unintended modifications.
- Query optimization: The
identity
function can be used to cancel index usage, allowing you to test query performance with a full scan.
When analyzing a query for potential index usage, the query analyzer ignores everything within identity
functions. It also disables constant folding.
countDigits
Counts the number of decimal digits needed to represent a value.
Syntax:
Arguments:
x
(numeric) — An integer or decimal value.
Returns:
The number of decimal digits. Type: UInt8
.
Example:
Result:
In this example:
regular_int
: 42 requires 2 digits to represent.decimal
: 1.5 is treated as 15, requiring 2 digits.large_int
: 1234567890 requires 10 digits to represent.
For decimal values, the function takes into account their scale. For example:
countDigits(42.000) = 5
countDigits(0.04200) = 4
This behavior allows you to check for decimal overflow. For instance, to check if a Decimal64
value might overflow, you can use: countDigits(x) > 18
.
formatQuery
Formats a SQL query string, improving its readability.
Syntax:
Arguments:
query
(String
): The SQL query to be formatted.
Returns:
- The formatted query. (
String
)
Example:
Result:
This function improves the readability of SQL queries by adding proper indentation and line breaks. It’s particularly useful for formatting complex queries or when preparing SQL for documentation or code reviews.
If the input query is not well-formed, the function throws an exception. To return NULL
instead of an exception for invalid queries, use the formatQueryOrNull()
function.
formatQueryOrNull
Returns a formatted, possibly multi-line, version of the given SQL query. If the query is not well-formed, it returns NULL instead of throwing an exception.
Syntax:
Arguments:
query
(String
): The SQL query to be formatted.
Returns:
- The formatted query, or NULL if the input query is not well-formed. [
String
]
Example:
Result:
This function is useful for prettifying SQL queries or for handling potentially malformed queries without causing errors. It can be particularly helpful when working with user-generated queries or when debugging complex SQL statements in your data applications.
formatQuerySingleLine
Formats a SQL query as a single line, removing line breaks and extra whitespace.
Syntax:
Arguments:
query
(String
): The SQL query to be formatted.
Returns:
A formatted version of the input query as a single line. [String
]
Example:
Result:
This function is useful for logging, debugging, or when you need to pass a query as a single line to another system or application. It preserves the semantic meaning of the query while removing unnecessary whitespace and line breaks.
If the input query is not well-formed, the function will throw an exception. To return NULL instead, use the formatQuerySingleLineOrNull()
function.
formatQuerySingleLineOrNull
Formats a SQL query as a single line, removing unnecessary whitespace. If the input is not a valid query, it returns NULL instead of throwing an exception.
Syntax:
Arguments:
query
(String
): The SQL query to format.
Returns:
- A formatted single-line version of the query, or NULL if the input is invalid. [
String
]
Example:
Result:
This function is useful for normalizing queries for logging, comparison, or storage purposes. It can handle invalid input gracefully, making it suitable for processing user-generated or potentially malformed queries.
Was this page helpful?