ClickHouse/docs/en/sql_reference/operators.md

276 lines
8.4 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
toc_priority: 37
toc_title: Operators
---
2020-03-20 10:10:48 +00:00
# Operators {#operators}
2020-04-10 13:55:00 +00:00
ClickHouse transforms operators to their corresponding functions at the query parsing stage according to their priority, precedence, and associativity.
2020-03-20 10:10:48 +00:00
## Access Operators {#access-operators}
`a[N]` Access to an element of an array. The `arrayElement(a, N)` function.
`a.N` Access to a tuple element. The `tupleElement(a, N)` function.
2020-03-20 10:10:48 +00:00
## Numeric Negation Operator {#numeric-negation-operator}
`-a` The `negate (a)` function.
2020-03-20 10:10:48 +00:00
## Multiplication and Division Operators {#multiplication-and-division-operators}
`a * b` The `multiply (a, b)` function.
`a / b` The `divide(a, b)` function.
`a % b` The `modulo(a, b)` function.
2020-03-20 10:10:48 +00:00
## Addition and Subtraction Operators {#addition-and-subtraction-operators}
`a + b` The `plus(a, b)` function.
`a - b` The `minus(a, b)` function.
2020-03-20 10:10:48 +00:00
## Comparison Operators {#comparison-operators}
`a = b` The `equals(a, b)` function.
`a == b` The `equals(a, b)` function.
`a != b` The `notEquals(a, b)` function.
`a <> b` The `notEquals(a, b)` function.
`a <= b` The `lessOrEquals(a, b)` function.
`a >= b` The `greaterOrEquals(a, b)` function.
`a < b` The `less(a, b)` function.
`a > b` The `greater(a, b)` function.
`a LIKE s` The `like(a, b)` function.
`a NOT LIKE s` The `notLike(a, b)` function.
`a BETWEEN b AND c` The same as `a >= b AND a <= c`.
`a NOT BETWEEN b AND c` The same as `a < b OR a > c`.
2019-03-26 12:44:25 +00:00
## Operators for Working with Data Sets {#operators-for-working-with-data-sets}
2020-04-03 13:23:32 +00:00
*See [IN operators](statements/select.md#select-in-operators).*
`a IN ...` The `in(a, b)` function.
`a NOT IN ...` The `notIn(a, b)` function.
`a GLOBAL IN ...` The `globalIn(a, b)` function.
`a GLOBAL NOT IN ...` The `globalNotIn(a, b)` function.
## Operators for Working with Dates and Times {#operators-datetime}
2020-03-18 18:43:51 +00:00
### EXTRACT {#operator-extract}
2020-03-20 10:10:48 +00:00
``` sql
EXTRACT(part FROM date);
```
2020-04-10 13:55:00 +00:00
Extract parts from a given date. For example, you can retrieve a month from a given date, or a second from a time.
The `part` parameter specifies which part of the date to retrieve. The following values are available:
- `DAY` — The day of the month. Possible values: 131.
- `MONTH` — The number of a month. Possible values: 112.
- `YEAR` — The year.
- `SECOND` — The second. Possible values: 059.
- `MINUTE` — The minute. Possible values: 059.
- `HOUR` — The hour. Possible values: 023.
The `part` parameter is case-insensitive.
2020-04-03 13:23:32 +00:00
The `date` parameter specifies the date or the time to process. Either [Date](../sql_reference/data_types/date.md) or [DateTime](../sql_reference/data_types/datetime.md) type is supported.
Examples:
2020-03-20 10:10:48 +00:00
``` sql
SELECT EXTRACT(DAY FROM toDate('2017-06-15'));
SELECT EXTRACT(MONTH FROM toDate('2017-06-15'));
SELECT EXTRACT(YEAR FROM toDate('2017-06-15'));
```
In the following example we create a table and insert into it a value with the `DateTime` type.
2020-03-20 10:10:48 +00:00
``` sql
CREATE TABLE test.Orders
(
OrderId UInt64,
OrderName String,
OrderDate DateTime
)
ENGINE = Log;
```
2020-03-20 10:10:48 +00:00
``` sql
INSERT INTO test.Orders VALUES (1, 'Jarlsberg Cheese', toDateTime('2008-10-11 13:23:44'));
```
2020-03-20 10:10:48 +00:00
``` sql
SELECT
toYear(OrderDate) AS OrderYear,
toMonth(OrderDate) AS OrderMonth,
toDayOfMonth(OrderDate) AS OrderDay,
toHour(OrderDate) AS OrderHour,
toMinute(OrderDate) AS OrderMinute,
toSecond(OrderDate) AS OrderSecond
FROM test.Orders;
DOCAPI-8530: Code blocks markup fix (#7060) * Typo fix. * Links fix. * Fixed links in docs. * More fixes. * docs/en: cleaning some files * docs/en: cleaning data_types * docs/en: cleaning database_engines * docs/en: cleaning development * docs/en: cleaning getting_started * docs/en: cleaning interfaces * docs/en: cleaning operations * docs/en: cleaning query_lamguage * docs/en: cleaning en * docs/ru: cleaning data_types * docs/ru: cleaning index * docs/ru: cleaning database_engines * docs/ru: cleaning development * docs/ru: cleaning general * docs/ru: cleaning getting_started * docs/ru: cleaning interfaces * docs/ru: cleaning operations * docs/ru: cleaning query_language * docs: cleaning interfaces/http * Update docs/en/data_types/array.md decorated ``` Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/getting_started/example_datasets/nyc_taxi.md fixed typo Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/getting_started/example_datasets/ontime.md fixed typo Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/interfaces/formats.md fixed error Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/table_engines/custom_partitioning_key.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/utils/clickhouse-local.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/dicts/external_dicts_dict_sources.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/utils/clickhouse-local.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/json_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/json_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/other_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/other_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/date_time_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/table_engines/jdbc.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * docs: fixed error * docs: fixed error
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
┌─OrderYear─┬─OrderMonth─┬─OrderDay─┬─OrderHour─┬─OrderMinute─┬─OrderSecond─┐
│ 2008 │ 10 │ 11 │ 13 │ 23 │ 44 │
└───────────┴────────────┴──────────┴───────────┴─────────────┴─────────────┘
```
You can see more examples in [tests](https://github.com/ClickHouse/ClickHouse/blob/master/tests/queries/0_stateless/00619_extract.sql).
2020-03-18 18:43:51 +00:00
### INTERVAL {#operator-interval}
2020-04-03 13:23:32 +00:00
Creates an [Interval](../sql_reference/data_types/special_data_types/interval.md)-type value that should be used in arithmetical operations with [Date](../sql_reference/data_types/date.md) and [DateTime](../sql_reference/data_types/datetime.md)-type values.
Types of intervals:
- `SECOND`
- `MINUTE`
- `HOUR`
- `DAY`
- `WEEK`
- `MONTH`
- `QUARTER`
- `YEAR`
!!! warning "Warning"
2020-04-10 13:55:00 +00:00
Intervals with different types cant be combined. You cant use expressions like `INTERVAL 4 DAY 1 HOUR`. Specify intervals in units that are smaller or equal to the smallest unit of the interval, for example, `INTERVAL 25 HOUR`. You can use consecutive operations, like in the example below.
Example:
2020-03-20 10:10:48 +00:00
``` sql
SELECT now() AS current_date_time, current_date_time + INTERVAL 4 DAY + INTERVAL 3 HOUR
```
2020-03-20 10:10:48 +00:00
``` text
┌───current_date_time─┬─plus(plus(now(), toIntervalDay(4)), toIntervalHour(3))─┐
│ 2019-10-23 11:16:28 │ 2019-10-27 14:16:28 │
└─────────────────────┴────────────────────────────────────────────────────────┘
```
**See Also**
2020-04-03 13:23:32 +00:00
- [Interval](../sql_reference/data_types/special_data_types/interval.md) data type
- [toInterval](../sql_reference/functions/type_conversion_functions.md#function-tointerval) type convertion functions
2020-03-20 10:10:48 +00:00
## Logical Negation Operator {#logical-negation-operator}
`NOT a` The `not(a)` function.
2020-03-20 10:10:48 +00:00
## Logical AND Operator {#logical-and-operator}
`a AND b` The`and(a, b)` function.
2020-03-20 10:10:48 +00:00
## Logical OR Operator {#logical-or-operator}
`a OR b` The `or(a, b)` function.
2020-03-20 10:10:48 +00:00
## Conditional Operator {#conditional-operator}
`a ? b : c` The `if(a, b, c)` function.
Note:
2020-04-03 13:23:32 +00:00
The conditional operator calculates the values of b and c, then checks whether condition a is met, and then returns the corresponding value. If `b` or `C` is an [arrayJoin()](../sql_reference/functions/array_join.md#functions_arrayjoin) function, each row will be replicated regardless of the “a” condition.
## Conditional Expression {#operator_case}
2020-03-20 10:10:48 +00:00
``` sql
CASE [x]
WHEN a THEN b
[WHEN ... THEN ...]
[ELSE c]
END
```
If `x` is specified, then `transform(x, [a, ...], [b, ...], c)` function is used. Otherwise `multiIf(a, b, ..., c)`.
If there is no `ELSE c` clause in the expression, the default value is `NULL`.
The `transform` function does not work with `NULL`.
2020-03-20 10:10:48 +00:00
## Concatenation Operator {#concatenation-operator}
`s1 || s2` The `concat(s1, s2) function.`
2020-03-20 10:10:48 +00:00
## Lambda Creation Operator {#lambda-creation-operator}
`x -> expr` The `lambda(x, expr) function.`
2020-04-10 13:55:00 +00:00
The following operators do not have a priority since they are brackets:
2020-03-20 10:10:48 +00:00
## Array Creation Operator {#array-creation-operator}
`[x1, ...]` The `array(x1, ...) function.`
2020-03-20 10:10:48 +00:00
## Tuple Creation Operator {#tuple-creation-operator}
`(x1, x2, ...)` The `tuple(x2, x2, ...) function.`
2020-03-20 10:10:48 +00:00
## Associativity {#associativity}
All binary operators have left associativity. For example, `1 + 2 + 3` is transformed to `plus(plus(1, 2), 3)`.
2020-03-20 10:10:48 +00:00
Sometimes this doesnt work the way you expect. For example, `SELECT 4 > 2 > 3` will result in 0.
2020-04-10 13:55:00 +00:00
For efficiency, the `and` and `or` functions accept any number of arguments. The corresponding chains of `AND` and `OR` operators are transformed into a single call of these functions.
## Checking for `NULL` {#checking-for-null}
ClickHouse supports the `IS NULL` and `IS NOT NULL` operators.
2020-03-18 18:43:51 +00:00
### IS NULL {#operator-is-null}
2020-04-03 13:23:32 +00:00
- For [Nullable](../sql_reference/data_types/nullable.md) type values, the `IS NULL` operator returns:
- `1`, if the value is `NULL`.
- `0` otherwise.
- For other values, the `IS NULL` operator always returns `0`.
2020-03-20 10:10:48 +00:00
<!-- -->
``` sql
DOCAPI-8530: Code blocks markup fix (#7060) * Typo fix. * Links fix. * Fixed links in docs. * More fixes. * docs/en: cleaning some files * docs/en: cleaning data_types * docs/en: cleaning database_engines * docs/en: cleaning development * docs/en: cleaning getting_started * docs/en: cleaning interfaces * docs/en: cleaning operations * docs/en: cleaning query_lamguage * docs/en: cleaning en * docs/ru: cleaning data_types * docs/ru: cleaning index * docs/ru: cleaning database_engines * docs/ru: cleaning development * docs/ru: cleaning general * docs/ru: cleaning getting_started * docs/ru: cleaning interfaces * docs/ru: cleaning operations * docs/ru: cleaning query_language * docs: cleaning interfaces/http * Update docs/en/data_types/array.md decorated ``` Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/getting_started/example_datasets/nyc_taxi.md fixed typo Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/getting_started/example_datasets/ontime.md fixed typo Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/interfaces/formats.md fixed error Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/table_engines/custom_partitioning_key.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/utils/clickhouse-local.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/dicts/external_dicts_dict_sources.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/utils/clickhouse-local.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/json_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/json_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/other_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/other_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/date_time_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/table_engines/jdbc.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * docs: fixed error * docs: fixed error
2019-09-23 15:31:46 +00:00
SELECT x+100 FROM t_null WHERE y IS NULL
```
2020-03-20 10:10:48 +00:00
``` text
┌─plus(x, 100)─┐
│ 101 │
└──────────────┘
```
2020-03-20 10:10:48 +00:00
### IS NOT NULL {#is-not-null}
2020-04-03 13:23:32 +00:00
- For [Nullable](../sql_reference/data_types/nullable.md) type values, the `IS NOT NULL` operator returns:
- `0`, if the value is `NULL`.
- `1` otherwise.
- For other values, the `IS NOT NULL` operator always returns `1`.
2020-03-20 10:10:48 +00:00
<!-- -->
``` sql
DOCAPI-8530: Code blocks markup fix (#7060) * Typo fix. * Links fix. * Fixed links in docs. * More fixes. * docs/en: cleaning some files * docs/en: cleaning data_types * docs/en: cleaning database_engines * docs/en: cleaning development * docs/en: cleaning getting_started * docs/en: cleaning interfaces * docs/en: cleaning operations * docs/en: cleaning query_lamguage * docs/en: cleaning en * docs/ru: cleaning data_types * docs/ru: cleaning index * docs/ru: cleaning database_engines * docs/ru: cleaning development * docs/ru: cleaning general * docs/ru: cleaning getting_started * docs/ru: cleaning interfaces * docs/ru: cleaning operations * docs/ru: cleaning query_language * docs: cleaning interfaces/http * Update docs/en/data_types/array.md decorated ``` Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/getting_started/example_datasets/nyc_taxi.md fixed typo Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/getting_started/example_datasets/ontime.md fixed typo Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/interfaces/formats.md fixed error Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/table_engines/custom_partitioning_key.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/utils/clickhouse-local.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/dicts/external_dicts_dict_sources.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/utils/clickhouse-local.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/json_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/json_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/other_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/other_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/query_language/functions/date_time_functions.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/operations/table_engines/jdbc.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * docs: fixed error * docs: fixed error
2019-09-23 15:31:46 +00:00
SELECT * FROM t_null WHERE y IS NOT NULL
```
2020-03-20 10:10:48 +00:00
``` text
┌─x─┬─y─┐
│ 2 │ 3 │
└───┴───┘
```
2020-01-30 10:34:55 +00:00
[Original article](https://clickhouse.tech/docs/en/query_language/operators/) <!--hide-->