Miscellaneous functions for various purposes.
This section covers miscellaneous ClickHouse utility functions that include::
formatQuery
, formatQuerySingleLine
)basename
)version()
)blockNumber()
, rowNumberInBlock()
)formatReadableSize
, formatReadableDecimalSize
)least
, greatest
, isFinite
, isConstant
)Extracts the filename from a path.
Syntax:
Arguments:
path
(String
): A string containing a file path.Returns:
String
]Example:
Result:
This function extracts the filename portion from a given path:
Additional Examples:
Result:
When using Windows-style paths, remember to escape backslashes:
Result:
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:
UInt64
]Details:
Example:
Result:
In this example:
This function is particularly useful when formatting output or calculating column widths for display purposes.
Returns the name of the data type of the passed argument.
Syntax
Arguments
x
(any type): Value of any type.Returns:
Example
Result:
In this example:
42
is identified as UInt8
String
now()
function returns a DateTime
typeThe 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.
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.
Returns an estimation of the uncompressed byte size of its arguments in memory.
Syntax:
Arguments:
argument
(any type): Value of any type.Returns:
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.
Returns the name of the current database.
Syntax:
Returns:
String
)Example:
Result:
Checks whether the argument is a constant expression.
Syntax:
Arguments:
x
: Expression to check.Returns:
x
is constant. (UInt8
)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.
Checks whether a floating-point value is finite.
Syntax:
Arguments:
x
(Float32
or Float64
): 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.
Checks if a floating-point value is infinite.
Syntax:
Arguments:
x
(Float32
or Float64
): 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.
Checks if a value is finite and returns an alternative value if it’s not.
Syntax:
Arguments:
x
(Float32
or Float64
): The value to check.y
(Float32
or Float64
): The value to return if x
is not finite.Returns:
x
is finite, returns x
.x
is infinite or NaN, returns y
.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.
Checks whether the argument is NaN (Not a Number).
Syntax
Arguments
x
(Float32
or Float64
): Value to check.Returns:
Type: UInt8
Example
Query:
Result:
In this example:
0/0
results in NaN, so isNaN(0/0)
returns 1.1
and 0/1
are not NaN, so isNaN
returns 0 for both.tacos_eaten / total_tacos
(5/0) results in a division by zero, which is NaN, so isNaN
returns 1.This function is useful for detecting undefined mathematical operations or missing data in floating-point calculations, particularly in scenarios involving consumption metrics.
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.
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.
Formats a size in bytes into a human-readable string with appropriate units (B, KiB, MiB, etc.).
Syntax:
Alias:
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.
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.
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.
minimum_unit
(String
, optional) — Minimum unit to show. All smaller units are truncated.
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.
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:
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.
ns
(nanoseconds)us
(microseconds)ms
(milliseconds)s
or sec
(seconds)m
or min
(minutes)h
or hour
(hours)d
or day
(days)w
or week
(weeks)mo
or month
(months, assuming 30 days per month)y
or year
(years, assuming 365 days per year)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.
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 as a
.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.
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:
greatest
returns 5 as it’s larger than 3.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.
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:
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.
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:
UInt64
]Example:
Result:
In this example, blockNumber()
returns the sequence number of each data block processed.
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.
Returns a unique row number for each row processed by the function. The returned numbers start at 0.
Syntax:
Returns:
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.
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:
Example:
Result:
This function can be useful in several scenarios:
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.
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
.
Formats a SQL query string, improving its readability.
Syntax:
Arguments:
query
(String
): The SQL query to be formatted.Returns:
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.
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:
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.
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.
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:
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.
Miscellaneous functions for various purposes.
This section covers miscellaneous ClickHouse utility functions that include::
formatQuery
, formatQuerySingleLine
)basename
)version()
)blockNumber()
, rowNumberInBlock()
)formatReadableSize
, formatReadableDecimalSize
)least
, greatest
, isFinite
, isConstant
)Extracts the filename from a path.
Syntax:
Arguments:
path
(String
): A string containing a file path.Returns:
String
]Example:
Result:
This function extracts the filename portion from a given path:
Additional Examples:
Result:
When using Windows-style paths, remember to escape backslashes:
Result:
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:
UInt64
]Details:
Example:
Result:
In this example:
This function is particularly useful when formatting output or calculating column widths for display purposes.
Returns the name of the data type of the passed argument.
Syntax
Arguments
x
(any type): Value of any type.Returns:
Example
Result:
In this example:
42
is identified as UInt8
String
now()
function returns a DateTime
typeThe 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.
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.
Returns an estimation of the uncompressed byte size of its arguments in memory.
Syntax:
Arguments:
argument
(any type): Value of any type.Returns:
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.
Returns the name of the current database.
Syntax:
Returns:
String
)Example:
Result:
Checks whether the argument is a constant expression.
Syntax:
Arguments:
x
: Expression to check.Returns:
x
is constant. (UInt8
)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.
Checks whether a floating-point value is finite.
Syntax:
Arguments:
x
(Float32
or Float64
): 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.
Checks if a floating-point value is infinite.
Syntax:
Arguments:
x
(Float32
or Float64
): 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.
Checks if a value is finite and returns an alternative value if it’s not.
Syntax:
Arguments:
x
(Float32
or Float64
): The value to check.y
(Float32
or Float64
): The value to return if x
is not finite.Returns:
x
is finite, returns x
.x
is infinite or NaN, returns y
.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.
Checks whether the argument is NaN (Not a Number).
Syntax
Arguments
x
(Float32
or Float64
): Value to check.Returns:
Type: UInt8
Example
Query:
Result:
In this example:
0/0
results in NaN, so isNaN(0/0)
returns 1.1
and 0/1
are not NaN, so isNaN
returns 0 for both.tacos_eaten / total_tacos
(5/0) results in a division by zero, which is NaN, so isNaN
returns 1.This function is useful for detecting undefined mathematical operations or missing data in floating-point calculations, particularly in scenarios involving consumption metrics.
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.
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.
Formats a size in bytes into a human-readable string with appropriate units (B, KiB, MiB, etc.).
Syntax:
Alias:
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.
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.
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.
minimum_unit
(String
, optional) — Minimum unit to show. All smaller units are truncated.
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.
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:
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.
ns
(nanoseconds)us
(microseconds)ms
(milliseconds)s
or sec
(seconds)m
or min
(minutes)h
or hour
(hours)d
or day
(days)w
or week
(weeks)mo
or month
(months, assuming 30 days per month)y
or year
(years, assuming 365 days per year)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.
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 as a
.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.
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:
greatest
returns 5 as it’s larger than 3.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.
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:
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.
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:
UInt64
]Example:
Result:
In this example, blockNumber()
returns the sequence number of each data block processed.
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.
Returns a unique row number for each row processed by the function. The returned numbers start at 0.
Syntax:
Returns:
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.
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:
Example:
Result:
This function can be useful in several scenarios:
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.
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
.
Formats a SQL query string, improving its readability.
Syntax:
Arguments:
query
(String
): The SQL query to be formatted.Returns:
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.
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:
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.
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.
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:
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.