ClickHouse/docs/en/sql-reference/functions/index.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
4.1 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/
2023-04-19 17:05:55 +00:00
sidebar_position: 1
sidebar_label: Overview
2020-04-03 13:23:32 +00:00
---
2023-04-19 17:05:55 +00:00
# Regular Functions
2017-04-03 19:49:50 +00:00
2021-05-27 19:44:11 +00:00
There are at least\* two types of functions - regular functions (they are just called “functions”) and aggregate functions. These are completely different concepts. Regular functions work as if they are applied to each row separately (for each row, the result of the function does not depend on the other rows). Aggregate functions accumulate a set of values from various rows (i.e. they depend on the entire set of rows).
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
In this section we discuss regular functions. For aggregate functions, see the section “Aggregate functions”.
CLICKHOUSE-2720: progress on website and reference (#886) * update presentations * CLICKHOUSE-2936: redirect from clickhouse.yandex.ru and clickhouse.yandex.com * update submodule * lost files * CLICKHOUSE-2981: prefer sphinx docs over original reference * CLICKHOUSE-2981: docs styles more similar to main website + add flags to switch language links * update presentations * Less confusing directory structure (docs -> doc/reference/) * Minify sphinx docs too * Website release script: fail fast + pass docker hash on deploy * Do not underline links in docs * shorter * cleanup docker images * tune nginx config * CLICKHOUSE-3043: get rid of habrastorage links * Lost translation * CLICKHOUSE-2936: temporary client-side redirect * behaves weird in test * put redirect back * CLICKHOUSE-3047: copy docs txts to public too * move to proper file * remove old pages to avoid confusion * Remove reference redirect warning for now * Refresh README.md * Yellow buttons in docs * Use svg flags instead of unicode ones in docs * fix test website instance * Put flags to separate files * wrong flag * Copy Yandex.Metrica introduction from main page to docs * Yet another home page structure change, couple new blocks (CLICKHOUSE-3045) * Update Contacts section * CLICKHOUSE-2849: more detailed legal information * CLICKHOUSE-2978 preparation - split by files * More changes in Contacts block * Tune texts on index page * update presentations * One more benchmark * Add usage sections to index page, adapted from slides * Get the roadmap started, based on slides from last ClickHouse Meetup * CLICKHOUSE-2977: some rendering tuning * Get rid of excessive section in the end of getting started * Make headers linkable * CLICKHOUSE-2981: links to editing reference - https://github.com/yandex/ClickHouse/issues/849 * CLICKHOUSE-2981: fix mobile styles in docs * Ban crawling of duplicating docs * Open some external links in new tab * Ban old docs too * Lots of trivial fixes in english docs * Lots of trivial fixes in russian docs * Remove getting started copies in markdown * Add Yandex.Webmaster * Fix some sphinx warnings * More warnings fixed in english docs * More sphinx warnings fixed * Add code-block:: text * More code-block:: text * These headers look not that well * Better switch between documentation languages * merge use_case.rst into ya_metrika_task.rst * Edit the agg_functions.rst texts * Add lost empty lines * Lost blank lines * Add new logo sizes * update presentations * Next step in migrating to new documentation * Fix all warnings in en reference * Fix all warnings in ru reference * Re-arrange existing reference * Move operation tips to main reference * Fix typos noticed by milovidov@ * Get rid of zookeeper.md * Looks like duplicate of tutorial.html * Fix some mess with html tags in tutorial * No idea why nobody noticed this before, but it was completely not clear whet to get the data * Match code block styling between main and tutorial pages (in favor of the latter) * Get rid of some copypaste in tutorial * Normalize header styles * Move example_datasets to sphinx * Move presentations submodule to website * Move and update README.md * No point in duplicating articles from habrahabr here * Move development-related docs as is for now * doc/reference/ -> docs/ (to match the URL on website) * Adapt links to match the previous commit * Adapt development docs to rst (still lacks translation and strikethrough support) * clean on release * blacklist presentations in gulp * strikethrough support in sphinx * just copy development folder for now * fix weird introduction in style article * Style guide translation (WIP) * Finish style guide translation to English * gulp clean separately * Update year in LICENSE * Initial CONTRIBUTING.md * Fix remaining links to old docs in tutorial * Some tutorial fixes * Typo * Another typo * Update list of authors from yandex-team accoding to git log
2017-06-20 14:19:03 +00:00
2023-06-06 21:51:03 +00:00
:::note
2023-06-06 22:10:22 +00:00
There is a third type of function that the [arrayJoin function](/docs/en/sql-reference/functions/array-join.md) belongs to. And [table functions](/docs/en/sql-reference/table-functions/index.md) can also be mentioned separately.
2023-06-06 21:51:03 +00:00
:::
2017-04-03 19:49:50 +00:00
2022-06-02 10:55:18 +00:00
## Strong Typing
2017-04-03 19:49:50 +00:00
2021-05-27 19:44:11 +00:00
In contrast to standard SQL, ClickHouse has strong typing. In other words, it does not make implicit conversions between types. Each function works for a specific set of types. This means that sometimes you need to use type conversion functions.
2017-04-03 19:49:50 +00:00
2022-06-02 10:55:18 +00:00
## Common Subexpression Elimination
2017-04-26 19:16:38 +00:00
All expressions in a query that have the same AST (the same record or same result of syntactic parsing) are considered to have identical values. Such expressions are concatenated and executed once. Identical subqueries are also eliminated this way.
2017-04-03 19:49:50 +00:00
2022-06-02 10:55:18 +00:00
## Types of Results
2017-04-26 19:16:38 +00:00
All functions return a single return as the result (not several values, and not zero values). The type of result is usually defined only by the types of arguments, not by the values. Exceptions are the tupleElement function (the a.N operator), and the toFixedString function.
2017-04-03 19:49:50 +00:00
2022-06-02 10:55:18 +00:00
## Constants
2017-04-26 19:16:38 +00:00
For simplicity, certain functions can only work with constants for some arguments. For example, the right argument of the LIKE operator must be a constant.
Almost all functions return a constant for constant arguments. The exception is functions that generate random numbers.
2020-03-20 10:10:48 +00:00
The now function returns different values for queries that were run at different times, but the result is considered a constant, since constancy is only important within a single query.
2017-04-26 19:16:38 +00:00
A constant expression is also considered a constant (for example, the right half of the LIKE operator can be constructed from multiple constants).
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Functions can be implemented in different ways for constant and non-constant arguments (different code is executed). But the results for a constant and for a true column containing only the same value should match each other.
2017-04-03 19:49:50 +00:00
2022-06-02 10:55:18 +00:00
## NULL Processing
Functions have the following behaviors:
- If at least one of the arguments of the function is `NULL`, the function result is also `NULL`.
- Special behavior that is specified individually in the description of each function. In the ClickHouse source code, these functions have `UseDefaultImplementationForNulls=false`.
2022-06-02 10:55:18 +00:00
## Constancy
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
Functions cant change the values of their arguments any changes are returned as the result. Thus, the result of calculating separate functions does not depend on the order in which the functions are written in the query.
2017-04-03 19:49:50 +00:00
2022-06-02 10:55:18 +00:00
## Higher-order functions, `->` operator and lambda(params, expr) function
Higher-order functions can only accept lambda functions as their functional argument. To pass a lambda function to a higher-order function use `->` operator. The left side of the arrow has a formal parameter, which is any ID, or multiple formal parameters any IDs in a tuple. The right side of the arrow has an expression that can use these formal parameters, as well as any table columns.
2021-07-29 15:20:55 +00:00
Examples:
```
x -> 2 * x
str -> str != Referer
```
A lambda function that accepts multiple arguments can also be passed to a higher-order function. In this case, the higher-order function is passed several arrays of identical length that these arguments will correspond to.
For some functions the first argument (the lambda function) can be omitted. In this case, identical mapping is assumed.
2023-05-12 12:05:38 +00:00
## User Defined Functions (UDFs)
2023-05-12 12:05:38 +00:00
ClickHouse supports user-defined functions. See [UDFs](/docs/en/sql-reference/functions/udf.md).