Merge remote-tracking branch 'origin/master' into consistent_metadata4

This commit is contained in:
alesapin 2020-06-13 11:50:27 +03:00
commit 5d3de747cd
69 changed files with 1461 additions and 1007 deletions

View File

@ -97,5 +97,37 @@ if (USE_INTERNAL_BOOST_LIBRARY)
add_library (boost::system ALIAS _boost_system)
target_include_directories (_boost_system PRIVATE ${LIBRARY_DIR})
else ()
message (FATAL_ERROR "TODO: external Boost library is not supported!")
# 1.70 like in contrib/boost
# 1.67 on CI
set(BOOST_VERSION 1.67)
find_package(Boost ${BOOST_VERSION} COMPONENTS
system
filesystem
iostreams
program_options
regex
REQUIRED)
add_library (_boost_headers_only INTERFACE)
add_library (boost::headers_only ALIAS _boost_headers_only)
target_include_directories (_boost_headers_only SYSTEM BEFORE INTERFACE ${Boost_INCLUDE_DIR})
add_library (_boost_filesystem INTERFACE)
add_library (_boost_iostreams INTERFACE)
add_library (_boost_program_options INTERFACE)
add_library (_boost_regex INTERFACE)
add_library (_boost_system INTERFACE)
target_link_libraries (_boost_filesystem INTERFACE ${Boost_FILESYSTEM_LIBRARY})
target_link_libraries (_boost_iostreams INTERFACE ${Boost_IOSTREAMS_LIBRARY})
target_link_libraries (_boost_program_options INTERFACE ${Boost_PROGRAM_OPTIONS_LIBRARY})
target_link_libraries (_boost_regex INTERFACE ${Boost_REGEX_LIBRARY})
target_link_libraries (_boost_system INTERFACE ${Boost_SYSTEM_LIBRARY})
add_library (boost::filesystem ALIAS _boost_filesystem)
add_library (boost::iostreams ALIAS _boost_iostreams)
add_library (boost::program_options ALIAS _boost_program_options)
add_library (boost::regex ALIAS _boost_regex)
add_library (boost::system ALIAS _boost_system)
endif ()

View File

@ -142,7 +142,7 @@ def parse_env_variables(build_type, compiler, sanitizer, package_type, image_typ
if unbundled:
# TODO: fix build with ENABLE_RDKAFKA
cmake_flags.append('-DUNBUNDLED=1 -DENABLE_MYSQL=0 -DENABLE_ODBC=0 -DENABLE_REPLXX=0 -DENABLE_RDKAFKA=0 -DUSE_INTERNAL_BOOST_LIBRARY=1')
cmake_flags.append('-DUNBUNDLED=1 -DENABLE_MYSQL=0 -DENABLE_ODBC=0 -DENABLE_REPLXX=0 -DENABLE_RDKAFKA=0')
if split_binary:
cmake_flags.append('-DUSE_STATIC_LIBRARIES=0 -DSPLIT_SHARED_LIBRARIES=1 -DCLICKHOUSE_SPLIT_BINARY=1')

View File

@ -110,7 +110,7 @@ if [ -n "$(ls /docker-entrypoint-initdb.d/)" ] || [ -n "$CLICKHOUSE_DB" ]; then
# create default database, if defined
if [ -n "$CLICKHOUSE_DB" ]; then
echo "$0: create database '$CLICKHOUSE_DB'"
"${clickhouseclient[@]}" "CREATE DATABASE IF NOT EXISTS $CLICKHOUSE_DB";
"${clickhouseclient[@]}" -q "CREATE DATABASE IF NOT EXISTS $CLICKHOUSE_DB";
fi
for f in /docker-entrypoint-initdb.d/*; do

View File

@ -87,7 +87,7 @@ then
# tests for use by compare.sh. Compare to merge base, because master might be
# far in the future and have unrelated test changes.
base=$(git -C ch merge-base "$SHA_TO_TEST" master)
git -C ch diff --name-only "$SHA_TO_TEST" "$base" | tee changed-tests.txt
git -C ch diff --name-only "$base" "$SHA_TO_TEST" | tee changed-tests.txt
if grep -vq '^tests/performance' changed-tests.txt
then
# Have some other changes besides the tests, so truncate the test list,
@ -131,5 +131,5 @@ done
dmesg -T > dmesg.log
7z a '-x!*/tmp' /output/output.7z ./*.{log,tsv,html,txt,rep,svg,columns} {right,left}/{performance,db/preprocessed_configs,scripts} report analyze
7z a '-x!*/tmp' /output/output.7z ./*.{log,tsv,html,txt,rep,svg,columns} {right,left}/{performance,db/preprocessed_configs,scripts} report analyze benchmark
cp compare.log /output

View File

@ -316,7 +316,7 @@ Result:
The function takes as arguments a set of conditions from 1 to 32 arguments of type `UInt8` that indicate whether a certain condition was met for the event.
Any condition can be specified as an argument (as in [WHERE](../../sql-reference/statements/select/where.md#select-where)).
The conditions, except the first, apply in pairs: the result of the second will be true if the first and second are true, of the third if the first and fird are true, etc.
The conditions, except the first, apply in pairs: the result of the second will be true if the first and second are true, of the third if the first and third are true, etc.
**Syntax**

View File

@ -1805,7 +1805,7 @@ For more information see [parameters](#agg_functions-stochasticlinearregression-
stochasticLogisticRegression(1.0, 1.0, 10, 'SGD')
```
1. Fitting
**1.** Fitting
<!-- -->
@ -1813,7 +1813,7 @@ stochasticLogisticRegression(1.0, 1.0, 10, 'SGD')
Predicted labels have to be in \[-1, 1\].
1. Predicting
**2.** Predicting
<!-- -->

View File

@ -28,7 +28,7 @@ CREATE TABLE t
) ENGINE = ...
```
[uniq](../../sql-reference/aggregate-functions/reference.md#agg_function-uniq), anyIf ([any](../../sql-reference/aggregate-functions/reference.md#agg_function-any)+[If](../../sql-reference/aggregate-functions/combinators.md#agg-functions-combinator-if)) and [quantiles](../../sql-reference/aggregate-functions/reference.md) are the aggregate functions supported in ClickHouse.
[uniq](../../sql-reference/aggregate-functions/reference.md#agg_function-uniq), anyIf ([any](../../sql-reference/aggregate-functions/reference.md#agg_function-any)+[If](../../sql-reference/aggregate-functions/combinators.md#agg-functions-combinator-if)) and [quantiles](../../sql-reference/aggregate-functions/reference.md#quantiles) are the aggregate functions supported in ClickHouse.
## Usage {#usage}

View File

@ -92,9 +92,11 @@ def test_single_page(input_path, lang):
logging.warning('Found %d duplicate anchor points' % duplicate_anchor_points)
if links_to_nowhere:
logging.warning(f'Found {links_to_nowhere} links to nowhere in {lang}')
if lang == 'en': # TODO: check all languages again
logging.error(f'Found {links_to_nowhere} links to nowhere in {lang}')
sys.exit(1)
else:
logging.warning(f'Found {links_to_nowhere} links to nowhere in {lang}')
if len(anchor_points) <= 10:
logging.error('Html parsing is probably broken')

View File

@ -1,15 +1,13 @@
---
machine_translated: true
machine_translated_rev: 72537a2d527c63c07aa5d2361a8829f3895cf2bd
toc_priority: 31
toc_title: "\u61D2\u60F0"
---
# 懒惰 {#lazy}
# 延时引擎Lazy {#lazy}
仅将表保留在RAM中 `expiration_time_in_seconds` 上次访问后几秒钟。 只能与\*日志表一起使用。
在距最近一次访问间隔`expiration_time_in_seconds`时间段内,将表保存在内存中,仅适用于 \*Log引擎表
它针对存储许多小\*日志表进行了优化,访问之间存在较长的时间间隔。
由于针对这类表的访问间隔较长,对保存大量小的 \*Log引擎表进行了优化
## 创建数据库 {#creating-a-database}

View File

@ -1,11 +1,9 @@
---
machine_translated: true
machine_translated_rev: 72537a2d527c63c07aa5d2361a8829f3895cf2bd
toc_priority: 61
toc_title: "\uFF82\u6697\uFF6A\uFF82\u6C3E\u73AF\u50AC\uFF82\u56E3"
toc_title: "性能测试"
---
# ツ暗ェツ氾环催ツ団 {#clickhouse-benchmark}
# 性能测试 {#clickhouse-benchmark}
连接到ClickHouse服务器并重复发送指定的查询。
@ -21,7 +19,7 @@ $ echo "single query" | clickhouse-benchmark [keys]
$ clickhouse-benchmark [keys] <<< "single query"
```
如果要发送一组查询,请创建一个文本文件,并将每个查询放在此文件中的单个字符串上。 例如:
如果要发送一组查询,请创建一个文本文件,并将每个查询的字符串放在此文件中。 例如:
``` sql
SELECT * FROM system.numbers LIMIT 10000000
@ -34,15 +32,15 @@ SELECT 1
clickhouse-benchmark [keys] < queries_file
```
## {#clickhouse-benchmark-keys}
## keys参数 {#clickhouse-benchmark-keys}
- `-c N`, `--concurrency=N` — Number of queries that `clickhouse-benchmark` 同时发送。 默认值1。
- `-d N`, `--delay=N` — Interval in seconds between intermediate reports (set 0 to disable reports). Default value: 1.
- `-h WORD`, `--host=WORD` — Server host. Default value: `localhost`. 为 [比较模式](#clickhouse-benchmark-comparison-mode) 您可以使用多个 `-h` 钥匙
- `-h WORD`, `--host=WORD` — Server host. Default value: `localhost`. 为 [比较模式](#clickhouse-benchmark-comparison-mode) 您可以使用多个 `-h` 参数
- `-p N`, `--port=N` — Server port. Default value: 9000. For the [比较模式](#clickhouse-benchmark-comparison-mode) 您可以使用多个 `-p` 钥匙
- `-i N`, `--iterations=N`Total number of queries. Default value: 0.
- `-r`, `--randomize`Random order of queries execution if there is more then one input query.
- `-s`, `--secure`Using TLS connection.
- `-i N`, `--iterations=N`查询的总次数. Default value: 0.
- `-r`, `--randomize`有多个查询时,以随机顺序执行.
- `-s`, `--secure`使用TLS安全连接.
- `-t N`, `--timelimit=N` — Time limit in seconds. `clickhouse-benchmark` 达到指定的时间限制时停止发送查询。 默认值0禁用时间限制
- `--confidence=N` — Level of confidence for T-test. Possible values: 0 (80%), 1 (90%), 2 (95%), 3 (98%), 4 (99%), 5 (99.5%). Default value: 5. In the [比较模式](#clickhouse-benchmark-comparison-mode) `clickhouse-benchmark` 执行 [独立双样本学生的t测试](https://en.wikipedia.org/wiki/Student%27s_t-test#Independent_two-sample_t-test) 测试以确定两个分布是否与所选置信水平没有不同。
- `--cumulative` — Printing cumulative data instead of data per interval.
@ -51,14 +49,14 @@ clickhouse-benchmark [keys] < queries_file
- `--user=USERNAME` — ClickHouse user name. Default value: `default`.
- `--password=PSWD` — ClickHouse user password. Default value: empty string.
- `--stacktrace` — Stack traces output. When the key is set, `clickhouse-bencmark` 输出异常的堆栈跟踪。
- `--stage=WORD`Query processing stage at server. ClickHouse stops query processing and returns answer to `clickhouse-benchmark` 在指定的阶段。 可能的值: `complete`, `fetch_columns`, `with_mergeable_state`. 默认值: `complete`.
- `--stage=WORD`查询请求的服务端处理状态. 在特定阶段Clickhouse会停止查询处理并返回结果给`clickhouse-benchmark`。 可能的值: `complete`, `fetch_columns`, `with_mergeable_state`. 默认值: `complete`.
- `--help` — Shows the help message.
如果你想申请一些 [设置](../../operations/settings/index.md) 对于查询,请将它们作为键传递 `--<session setting name>= SETTING_VALUE`. 例如, `--max_memory_usage=1048576`.
如果你想在查询时应用上述的部分参数 [设置](../../operations/settings/index.md) ,请将它们作为键传递 `--<session setting name>= SETTING_VALUE`. 例如, `--max_memory_usage=1048576`.
## 输出 {#clickhouse-benchmark-output}
默认情况下, `clickhouse-benchmark` 每个报表 `--delay` 间隔
默认情况下, `clickhouse-benchmark` 按照 `--delay` 参数间隔输出结果
报告示例:
@ -83,27 +81,27 @@ localhost:9000, queries 10, QPS: 6.772, RPS: 67904487.440, MiB/s: 518.070, resul
99.990% 0.150 sec.
```
在报告中,您可以找到:
结果报告中,您可以找到:
- 在查询的数量 `Queries executed:`
- 查询数量:参见`Queries executed:`字段
- 状态字符串包含(按顺序):
- 状态码(按顺序给出):
- ClickHouse服务器的端点
- ClickHouse服务器的连接信息
- 已处理的查询数。
- QPSQPS在指定的时间段内每秒执行多少个查询服务器 `--delay` 争论。
- RPS在指定的时间段内,服务器每秒读取多少行 `--delay` 争论。
- MiB/s在指定的时间段内每秒读取多少mebibytes服务器 `--delay` 争论。
- 结果RPS在指定的时间段内,服务器每秒放置到查询结果的行数 `--delay` 争论。
- 结果MiB/s.在指定的时间段内服务器每秒将多少mebibytes放置到查询结果中 `--delay` 争论。
- QPS服务端每秒处理的查询数量
- RPS服务器每秒读取多少行
- MiB/s服务器每秒读取多少字节的数据
- 结果RPS服务端每秒生成多少行的结果集数据
- 结果MiB/s.服务端每秒生成多少字节的结果集数据
- 查询执行时间的百分位数
- 查询执行时间的百分
## 比模式 {#clickhouse-benchmark-comparison-mode}
## 比模式 {#clickhouse-benchmark-comparison-mode}
`clickhouse-benchmark` 可以比较两个正在运行的ClickHouse服务器的性能。
要使用比较模式,请通过以下两对指定两个服务器的端点 `--host`, `--port` 钥匙 键在参数列表中的位置匹配在一起,第一 `--host` 与第一匹配 `--port` 等等。 `clickhouse-benchmark` 建立到两个服务器的连接,然后发送查询。 每个查询寻址到随机选择的服务器。 每个服务器的结果分别显示。
要使用对比模式,分别为每个服务器配置各自的`--host`, `--port`参数。`clickhouse-benchmark` 会根据设置的参数建立到各个Server的连接并发送请求。每个查询请求会随机发送到某个服务器。输出结果会按服务器分组输出
## 示例 {#clickhouse-benchmark-example}

View File

@ -1,51 +1,49 @@
---
machine_translated: true
machine_translated_rev: 72537a2d527c63c07aa5d2361a8829f3895cf2bd
toc_priority: 37
toc_title: "\u7EC4\u5408\u5668"
toc_title: 聚合函数组合器
---
# 聚合函数组合器 {#aggregate_functions_combinators}
聚合函数的名称可以附加一个后缀。 这改变了聚合函数的工作方式。
## -如果 {#agg-functions-combinator-if}
## -If {#agg-functions-combinator-if}
The suffix -If can be appended to the name of any aggregate function. In this case, the aggregate function accepts an extra argument a condition (Uint8 type). The aggregate function processes only the rows that trigger the condition. If the condition was not triggered even once, it returns a default value (usually zeros or empty strings).
-If可以加到任何聚合函数之后。加了-If之后聚合函数需要接受一个额外的参数一个条件Uint8类型如果条件满足那聚合函数处理当前的行数据如果不满足那返回默认值通常是0或者空字符串
: `sumIf(column, cond)`, `countIf(cond)`, `avgIf(x, cond)`, `quantilesTimingIf(level1, level2)(x, cond)`, `argMinIf(arg, val, cond)` 等等。
`sumIf(column, cond)`, `countIf(cond)`, `avgIf(x, cond)`, `quantilesTimingIf(level1, level2)(x, cond)`, `argMinIf(arg, val, cond)` 等等。
使用条件聚合函数,您可以一次计算多个条件的聚合,而无需使用子查询和 `JOIN`例如在Yandex的。Metrica条件聚合函数用于实现段比较功能。
使用条件聚合函数,您可以一次计算多个条件的聚合,而无需使用子查询和 `JOIN`例如在Yandex.Metrica条件聚合函数用于实现段比较功能。
## -阵列 {#agg-functions-combinator-array}
## -Array {#agg-functions-combinator-array}
-Array后缀可以附加到任何聚合函数。 在这种情况下,聚合函数采用的参数 Array(T) 类型(数组)而不是 T 类型参数。 如果聚合函数接受多个参数,则它必须是长度相等的数组。 在处理数组时,聚合函数的工作方式与所有数组元素的原始聚合函数类似。
示例1: `sumArray(arr)` -总计所有的所有元素 arr 阵列。 在这个例子中,它可以更简单地编写: `sum(arraySum(arr))`.
示例1 `sumArray(arr)` -总计所有的所有元素 arr 阵列。在这个例子中,它可以更简单地编写: `sum(arraySum(arr))`.
示例2: `uniqArray(arr)` Counts the number of unique elements in all arr 阵列。 这可以做一个更简单的方法: `uniq(arrayJoin(arr))`,但它并不总是可以添加 arrayJoin 到查询。
示例2 `uniqArray(arr)` 计算arr中唯一元素的个数。这可以是一个更简单的方法 `uniq(arrayJoin(arr))`,但它并不总是可以添加 arrayJoin 到查询。
-如果和-阵列可以组合。 然而, Array 必须先来,然后 If. 例: `uniqArrayIf(arr, cond)`, `quantilesTimingArrayIf(level1, level2)(arr, cond)`. 由于这个顺序,该 cond 参数不会是数组。
如果和-If组合Array 必须先来,然后 If. 例: `uniqArrayIf(arr, cond)` `quantilesTimingArrayIf(level1, level2)(arr, cond)`由于这个顺序,该 cond 参数不会是数组。
## - {#agg-functions-combinator-state}
## -State {#agg-functions-combinator-state}
如果应用此combinator则聚合函数不会返回结果值例如唯一值的数量 [uniq](reference.md#agg_function-uniq) 函数),但聚合的中间状态(用于 `uniq`,这是用于计算唯一值的数量的散列表)。 这是一个 `AggregateFunction(...)` 可用于进一步处理或存储在表中以完成聚合。
如果应用此combinator则聚合函数不会返回结果值例如唯一值的数量 [uniq](reference.md#agg_function-uniq) 函数),但是返回聚合的中间状态(对于 `uniq`,返回的是计算唯一值的数量的哈希表)。 这是一个 `AggregateFunction(...)` 可用于进一步处理或存储在表中以完成稍后的聚合。
要使用这些状态,请使用:
- [AggregatingMergeTree](../../engines/table-engines/mergetree-family/aggregatingmergetree.md) 表引擎。
- [最后聚会](../../sql-reference/functions/other-functions.md#function-finalizeaggregation) 功能。
- [跑累积](../../sql-reference/functions/other-functions.md#function-runningaccumulate) 功能。
- [-合并](#aggregate_functions_combinators-merge) combinator
- [finalizeAggregation](../../sql-reference/functions/other-functions.md#function-finalizeaggregation) 功能。
- [runningAccumulate](../../sql-reference/functions/other-functions.md#function-runningaccumulate) 功能。
- [-Merge](#aggregate_functions_combinators-merge) combinator
- [-MergeState](#aggregate_functions_combinators-mergestate) combinator
## -合并 {#aggregate_functions_combinators-merge}
## -Merge {#aggregate_functions_combinators-merge}
如果应用此组合器,则聚合函数将中间聚合状态作为参数,组合状态以完成聚合,并返回结果值。
## -MergeState {#aggregate_functions_combinators-mergestate}
以与-Merge combinator相同的方式合并中间聚合状态。 但是,它不会返回结果值,而是返回中间聚合状态,类似于-State combinator
以与-Merge 相同的方式合并中间聚合状态。 但是,它不会返回结果值,而是返回中间聚合状态,类似于-State。
## -ForEach {#agg-functions-combinator-foreach}
@ -55,7 +53,7 @@ The suffix -If can be appended to the name of any aggregate function. In this ca
更改聚合函数的行为。
如果聚合函数没有输入值,则使用此combinator它返回其返回数据类型的默认值。 适用于可以采用空输入数据的聚合函数。
如果聚合函数没有输入值,则使用此组合器它返回其返回数据类型的默认值。 适用于可以采用空输入数据的聚合函数。
`-OrDefault` 可与其他组合器一起使用。
@ -67,7 +65,7 @@ The suffix -If can be appended to the name of any aggregate function. In this ca
**参数**
- `x`Aggregate function parameters.
- `x`聚合函数参数。
**返回值**
@ -174,7 +172,7 @@ FROM
└────────────────────────────────┘
```
## -重新采样 {#agg-functions-combinator-resample}
## -Resample {#agg-functions-combinator-resample}
允许您将数据划分为组,然后单独聚合这些组中的数据。 通过将一列中的值拆分为间隔来创建组。
@ -184,19 +182,19 @@ FROM
**参数**
- `start`Starting value of the whole required interval for `resampling_key` 值。
- `stop`Ending value of the whole required interval for `resampling_key` 值。 整个时间间隔不包括 `stop` 价值 `[start, stop)`.
- `step`Step for separating the whole interval into subintervals. The `aggFunction` 在每个子区间上独立执行。
- `resampling_key`Column whose values are used for separating data into intervals.
- `start``resampling_key` 开始值。
- `stop``resampling_key` 结束边界。 区间内部不包含 `stop` 值,即 `[start, stop)`.
- `step`分组的步长。 The `aggFunction` 在每个子区间上独立执行。
- `resampling_key`取样列,被用来分组.
- `aggFunction_params``aggFunction` 参数。
**返回值**
- 阵列 `aggFunction` 每个子区间的结果。
- `aggFunction` 每个子区间的结果,结果为数组
**示例**
考虑一下 `people` 具有以下数据的表:
考虑一下 `people` 表具有以下数据的表结构:
``` text
┌─name───┬─age─┬─wage─┐
@ -209,9 +207,9 @@ FROM
└────────┴─────┴──────┘
```
让我们得到的人的名字,他们的年龄在于的时间间隔 `[30,60)``[60,75)`. 由于我们使用整数表示的年龄,我们得到的年龄 `[30, 59]``[60,74]` 间隔。
让我们得到的人的名字,他们的年龄在于的时间间隔 `[30,60)``[60,75)` 由于我们使用整数表示的年龄,我们得到的年龄 `[30, 59]``[60,74]` 间隔。
要在数组中聚合名称,我们使用 [groupArray](reference.md#agg_function-grouparray) 聚合函数。 这需要一个参数。 在我们的例子中,它是 `name` 列。 `groupArrayResample` 函数应该使用 `age` 按年龄聚合名称的列。 要定义所需的时间间隔,我们通过 `30, 75, 30` 参数到 `groupArrayResample` 功能
要在数组中聚合名称,我们使用 [groupArray](reference.md#agg_function-grouparray) 聚合函数。 这需要一个参数。 在我们的例子中,它是 `name` 列。 `groupArrayResample` 函数应该使用 `age` 按年龄聚合名称 要定义所需的时间间隔,我们传入 `30, 75, 30` 参数给 `groupArrayResample` 函数
``` sql
SELECT groupArrayResample(30, 75, 30)(name, age) FROM people
@ -225,7 +223,7 @@ SELECT groupArrayResample(30, 75, 30)(name, age) FROM people
考虑结果。
`Jonh` 是因为他太年轻了 其他人按照指定的年龄间隔进行分配。
`Jonh` 没有被选中,因为他太年轻了。 其他人按照指定的年龄间隔进行分配。
现在让我们计算指定年龄间隔内的总人数和平均工资。

View File

@ -1,9 +1,6 @@
---
machine_translated: true
machine_translated_rev: 72537a2d527c63c07aa5d2361a8829f3895cf2bd
toc_folder_title: "\u805A\u5408\u51FD\u6570"
toc_priority: 33
toc_title: "\u5BFC\u8A00"
toc_title: 简介
---
# 聚合函数 {#aggregate-functions}

View File

@ -1,15 +1,13 @@
---
machine_translated: true
machine_translated_rev: 72537a2d527c63c07aa5d2361a8829f3895cf2bd
toc_priority: 38
toc_title: "\u53C2\u6570"
toc_title: 参数聚合函数
---
# 参数聚合函数 {#aggregate_functions_parametric}
Some aggregate functions can accept not only argument columns (used for compression), but a set of parameters constants for initialization. The syntax is two pairs of brackets instead of one. The first is for parameters, and the second is for arguments.
一些聚合函数不仅可以接受参数列(用于压缩),也可以接收常量的初始化参数。这种语法是接受两个括号的参数,第一个数初始化参数,第二个是入参。
## 直方图 {#histogram}
## histogram {#histogram}
计算自适应直方图。 它不能保证精确的结果。
@ -21,20 +19,21 @@ histogram(number_of_bins)(values)
**参数**
`number_of_bins` — Upper limit for the number of bins in the histogram. The function automatically calculates the number of bins. It tries to reach the specified number of bins, but if it fails, it uses fewer bins.
`values` — [表达式](../syntax.md#syntax-expressions) 导致输入值。
`number_of_bins` — 直方图bin个数这个函数会自动计算bin的数量而且会尽量使用指定值如果无法做到那就使用更小的bin个数。
`values` — [表达式](../syntax.md#syntax-expressions) 输入值。
**返回值**
- [阵列](../../sql-reference/data-types/array.md) 的 [元组](../../sql-reference/data-types/tuple.md) 下面的格式:
- [Array](../../sql-reference/data-types/array.md) 的 [Tuples](../../sql-reference/data-types/tuple.md) 如下:
```
[(lower_1, upper_1, height_1), ... (lower_N, upper_N, height_N)]
```
- `lower`Lower bound of the bin.
- `upper`Upper bound of the bin.
- `height`Calculated height of the bin.
- `lower`bin的下边界。
- `upper`bin的上边界。
- `height`bin的计算权重。
**示例**
@ -53,7 +52,7 @@ FROM (
└─────────────────────────────────────────────────────────────────────────┘
```
您可以使用 [酒吧](../../sql-reference/functions/other-functions.md#function-bar) 功能,例如:
您可以使用 [bar](../../sql-reference/functions/other-functions.md#function-bar) 功能,例如:
``` sql
WITH histogram(5)(rand() % 100) AS hist
@ -93,11 +92,11 @@ sequenceMatch(pattern)(timestamp, cond1, cond2, ...)
**参数**
- `pattern`Pattern string. See [模式语法](#sequence-function-pattern-syntax).
- `pattern`模式字符串。 参考 [模式语法](#sequence-function-pattern-syntax).
- `timestamp`Column considered to contain time data. Typical data types are `Date``DateTime`. 您还可以使用任何支持的 [UInt](../../sql-reference/data-types/int-uint.md) 数据类型。
- `timestamp`包含时间的列。典型的时间类型是: `Date``DateTime`您还可以使用任何支持的 [UInt](../../sql-reference/data-types/int-uint.md) 数据类型。
- `cond1`, `cond2`Conditions that describe the chain of events. Data type: `UInt8`. 最多可以传递32个条件参数。 该函数只考虑这些条件中描述的事件。 如果序列包含未在条件中描述的数据,则函数将跳过这些数据。
- `cond1`, `cond2`事件链的约束条件。 数据类型是: `UInt8` 最多可以传递32个条件参数。 该函数只考虑这些条件中描述的事件。 如果序列包含未在条件中描述的数据,则函数将跳过这些数据。
**返回值**
@ -109,11 +108,11 @@ sequenceMatch(pattern)(timestamp, cond1, cond2, ...)
<a name="sequence-function-pattern-syntax"></a>
**模式语法**
- `(?N)`Matches the condition argument at position `N`. 条件在编号 `[1, 32]` 范围。 例如, `(?1)` 匹配传递给 `cond1` 参数。
- `(?N)`在位置`N`匹配条件参数。 条件在编号 `[1, 32]` 范围。 例如, `(?1)` 匹配传递给 `cond1` 参数。
- `.*`Matches any number of events. You don't need conditional arguments to match this element of the pattern.
- `.*`匹配任何事件的数字。 不需要条件参数来匹配这个模式。
- `(?t operator value)`Sets the time in seconds that should separate two events. For example, pattern `(?1)(?t>1800)(?2)` 匹配彼此发生超过1800秒的事件。 这些事件之间可以存在任意数量的任何事件。 您可以使用 `>=`, `>`, `<`, `<=`营商
- `(?t operator value)`分开两个事件的时间。 例如: `(?1)(?t>1800)(?2)` 匹配彼此发生超过1800秒的事件。 这些事件之间可以存在任意数量的任何事件。 您可以使用 `>=`, `>`, `<`, `<=`算符
**例**
@ -169,7 +168,7 @@ SELECT sequenceMatch('(?1)(?2)')(time, number = 1, number = 2, number = 4) FROM
## sequenceCount(pattern)(time, cond1, cond2, …) {#function-sequencecount}
数与模式匹配的事件链的数量。 该函数搜索不重叠的事件链。 当前链匹配后,它开始搜索下一个链。
算与模式匹配的事件链的数量。该函数搜索不重叠的事件链。当前链匹配后,它开始搜索下一个链。
!!! warning "警告"
在同一秒钟发生的事件可能以未定义的顺序排列在序列中,影响结果。
@ -180,11 +179,11 @@ sequenceCount(pattern)(timestamp, cond1, cond2, ...)
**参数**
- `pattern`Pattern string. See [模式语法](#sequence-function-pattern-syntax).
- `pattern`模式字符串。 参考:[模式语法](#sequence-function-pattern-syntax).
- `timestamp`Column considered to contain time data. Typical data types are `Date``DateTime`. 您还可以使用任何支持的 [UInt](../../sql-reference/data-types/int-uint.md) 数据类型。
- `timestamp`包含时间的列。典型的时间类型是: `Date``DateTime`您还可以使用任何支持的 [UInt](../../sql-reference/data-types/int-uint.md) 数据类型。
- `cond1`, `cond2`Conditions that describe the chain of events. Data type: `UInt8`. 最多可以传递32个条件参数。 该函数只考虑这些条件中描述的事件。 如果序列包含未在条件中描述的数据,则函数将跳过这些数据。
- `cond1`, `cond2`事件链的约束条件。 数据类型是: `UInt8`。 最多可以传递32个条件参数。该函数只考虑这些条件中描述的事件。 如果序列包含未在条件中描述的数据,则函数将跳过这些数据。
**返回值**
@ -227,9 +226,9 @@ SELECT sequenceCount('(?1).*(?2)')(time, number = 1, number = 2) FROM t
搜索滑动时间窗中的事件链,并计算从链中发生的最大事件数。
该函数根据算法工作:
该函数采用如下算法:
- 该函数搜索触发链中的第一个条件并将事件计数器设置为1的数据。 这是滑动窗口启动的时刻。
- 该函数搜索触发链中的第一个条件并将事件计数器设置为1。 这是滑动窗口启动的时刻。
- 如果来自链的事件在窗口内顺序发生,则计数器将递增。 如果事件序列中断,则计数器不会增加。
@ -243,11 +242,11 @@ windowFunnel(window, [mode])(timestamp, cond1, cond2, ..., condN)
**参数**
- `window`Length of the sliding window in seconds.
- `mode` -这是一个可选的参数。
- `'strict'` -当 `'strict'` 设置时windowFunnel()仅对唯一值应用条件。
- `timestamp`Name of the column containing the timestamp. Data types supported: [日期](../../sql-reference/data-types/date.md), [日期时间](../../sql-reference/data-types/datetime.md#data_type-datetime) 和其他无符号整数类型(请注意,即使时间戳支持 `UInt64` 类型它的值不能超过Int64最大值即2^63-1
- `cond`Conditions or data describing the chain of events. [UInt8](../../sql-reference/data-types/int-uint.md).
- `window`滑动窗户的大小,单位是秒。
- `mode` - 这是一个可选的参数。
- `'strict'` - `'strict'` 设置时windowFunnel()仅对唯一值应用匹配条件。
- `timestamp`包含时间的列。 数据类型支持: [日期](../../sql-reference/data-types/date.md), [日期时间](../../sql-reference/data-types/datetime.md#data_type-datetime) 和其他无符号整数类型(请注意,即使时间戳支持 `UInt64` 类型它的值不能超过Int64最大值即2^63-1
- `cond`事件链的约束条件。 [UInt8](../../sql-reference/data-types/int-uint.md) 类型。
**返回值**
@ -284,7 +283,7 @@ windowFunnel(window, [mode])(timestamp, cond1, cond2, ..., condN)
└────────────┴─────────┴─────────────────────┴─────────┴─────────┘
```
了解用户有多远 `user_id` 可以在2019的1-2月期间通过链条。
了解用户`user_id` 可以在2019的1-2月期间通过链条多远
查询:
@ -315,10 +314,10 @@ ORDER BY level ASC
## Retention {#retention}
该函数将一组条件作为参数类型为1到32个参数 `UInt8` 表示事件是否满足特定条件。
该函数将一组条件作为参数类型为1到32个 `UInt8` 类型的参数,用来表示事件是否满足特定条件。
任何条件都可以指定为参数(如 [WHERE](../../sql-reference/statements/select/where.md#select-where)).
除了第一个以外,条件成对适用:如果第一个和第二个是真的,第二个结果将是真的,如果第一个和fird是真的,第三个结果将是真的,等等。
除了第一个以外,条件成对适用:如果第一个和第二个是真的,第二个结果将是真的,如果第一个和第三个是真的,第三个结果将是真的,等等。
**语法**
@ -328,22 +327,22 @@ retention(cond1, cond2, ..., cond32);
**参数**
- `cond`an expression that returns a `UInt8` 结果1或0
- `cond`返回 `UInt8` 结果1或0的表达式
**返回值**
数组为1或0。
- 1 — condition was met for the event.
- 0 — condition wasn't met for the event.
- 1 — 条件满足。
- 0 — 条件不满足。
类型: `UInt8`.
**示例**
让我们考虑计算的一个例子 `retention` 功能,以确定网站流量。
让我们考虑使用 `retention` 功能的一个例子 ,以确定网站流量。
**1.** Сreate a table to illustrate an example.
**1.** 举例说明,先创建一张表。
``` sql
CREATE TABLE retention_test(date Date, uid Int32) ENGINE = Memory;
@ -402,7 +401,7 @@ SELECT * FROM retention_test
└────────────┴─────┘
```
**2.** 按唯一ID对用户进行分组 `uid` 使用 `retention` 功能。
**2.** 按唯一ID `uid` 对用户进行分组,使用 `retention` 功能。
查询:
@ -466,7 +465,7 @@ FROM
└────┴────┴────┘
```
哪里:
条件:
- `r1`-2020-01-01期间访问该网站的独立访问者数量 `cond1` 条件)。
- `r2`-在2020-01-01和2020-01-02之间的特定时间段内访问该网站的唯一访问者的数量 (`cond1` 和 `cond2` 条件)。
@ -474,9 +473,9 @@ FROM
## uniqUpTo(N)(x) {#uniquptonx}
Calculates the number of different argument values if it is less than or equal to N. If the number of different argument values is greater than N, it returns N + 1.
计算小于或者等于N的不同参数的个数。如果结果大于N那返回N+1。
建议使用小Ns高达10。 N的最大值为100。
建议使用较小的Ns比如10。N的最大值为100。
对于聚合函数的状态它使用的内存量等于1+N\*一个字节值的大小。
对于字符串它存储8个字节的非加密哈希。 也就是说,计算是近似的字符串。
@ -488,12 +487,12 @@ Calculates the number of different argument values if it is less than or e
用法示例:
``` text
Problem: Generate a report that shows only keywords that produced at least 5 unique users.
Solution: Write in the GROUP BY query SearchPhrase HAVING uniqUpTo(4)(UserID) >= 5
问题:产出一个不少于五个唯一用户的关键字报告
解决方案: 写group by查询语句 HAVING uniqUpTo(4)(UserID) >= 5
```
## sumMapFiltered(keys\_to\_keep)(keys, values) {#summapfilteredkeys-to-keepkeys-values}
和 [sumMap](reference.md#agg_functions-summap) 基本一致, 除了一个键数组作为参数传递。这在使用高基数key时尤其有用。
[原始文章](https://clickhouse.tech/docs/en/query_language/agg_functions/parametric_functions/) <!--hide-->
## sumMapFiltered(keys\_to\_keep)(键值) {#summapfilteredkeys-to-keepkeys-values}
同样的行为 [sumMap](reference.md#agg_functions-summap) 除了一个键数组作为参数传递。 这在使用高基数密钥时尤其有用。

View File

@ -1,13 +1,11 @@
---
machine_translated: true
machine_translated_rev: 72537a2d527c63c07aa5d2361a8829f3895cf2bd
toc_priority: 36
toc_title: "\u53C2\u8003\u8D44\u6599"
toc_title: 聚合函数
---
# 聚合函数引用 {#aggregate-functions-reference}
## 计数 {#agg_function-count}
## count {#agg_function-count}
计数行数或非空值。
@ -73,7 +71,7 @@ SELECT count(DISTINCT num) FROM t
这个例子表明 `count(DISTINCT num)` 由执行 `uniqExact` 根据功能 `count_distinct_implementation` 设定值。
## 任何(x) {#agg_function-any}
## any(x) {#agg_function-any}
选择第一个遇到的值。
查询可以以任何顺序执行,甚至每次都以不同的顺序执行,因此此函数的结果是不确定的。
@ -115,7 +113,7 @@ FROM ontime
选择遇到的最后一个值。
其结果是一样不确定的 `any` 功能。
## 集团比特 {#groupbitand}
## groupBitAnd {#groupbitand}
按位应用 `AND` 对于一系列的数字。
@ -337,7 +335,7 @@ SELECT argMin(user, salary) FROM salary
总计 value 数组根据在指定的键 key 阵列。
传递键和值数组的元组与传递两个键和值数组是同义的。
元素的数量 keyvalue 总计的每一行必须相同。
Returns a tuple of two arrays: keys in sorted order, and values summed for the corresponding keys.
返回两个数组的一个二元组: key是排好序的value是对应key的求和。
示例:
@ -374,7 +372,7 @@ GROUP BY timeslot
## skewPop {#skewpop}
计算 [歪斜](https://en.wikipedia.org/wiki/Skewness) 的序列
计算的序列[偏度](https://en.wikipedia.org/wiki/Skewness)
``` sql
skewPop(expr)
@ -386,7 +384,7 @@ skewPop(expr)
**返回值**
The skewness of the given distribution. Type — [Float64](../../sql-reference/data-types/float.md)
给定序列的偏度。类型 — [Float64](../../sql-reference/data-types/float.md)
**示例**
@ -410,7 +408,7 @@ skewSamp(expr)
**返回值**
The skewness of the given distribution. Type — [Float64](../../sql-reference/data-types/float.md). 如果 `n <= 1` (`n` 是样本的大小),则该函数返回 `nan`.
给定序列的偏度。 类型 — [Float64](../../sql-reference/data-types/float.md). 如果 `n <= 1` (`n` 是样本的大小),则该函数返回 `nan`.
**示例**
@ -432,7 +430,7 @@ kurtPop(expr)
**返回值**
The kurtosis of the given distribution. Type — [Float64](../../sql-reference/data-types/float.md)
给定序列的峰度。 类型 — [Float64](../../sql-reference/data-types/float.md)
**示例**
@ -456,7 +454,7 @@ kurtSamp(expr)
**返回值**
The kurtosis of the given distribution. Type — [Float64](../../sql-reference/data-types/float.md). 如果 `n <= 1` (`n` 是样本的大小),则该函数返回 `nan`.
给定序列的峰度。类型 — [Float64](../../sql-reference/data-types/float.md). 如果 `n <= 1` (`n` 是样本的大小),则该函数返回 `nan`.
**示例**
@ -533,7 +531,7 @@ FROM (
只适用于数字。
结果总是Float64。
## 平均加权 {#avgweighted}
## avgWeighted {#avgweighted}
计算 [加权算术平均值](https://en.wikipedia.org/wiki/Weighted_arithmetic_mean).
@ -545,10 +543,10 @@ avgWeighted(x, weight)
**参数**
- `x`Values. [整数](../data-types/int-uint.md) 或 [浮点](../data-types/float.md).
- `weight`Weights of the values. [整数](../data-types/int-uint.md) 或 [浮点](../data-types/float.md).
- `x`值。 [整数](../data-types/int-uint.md) 或 [浮点](../data-types/float.md).
- `weight`值的加权。 [整数](../data-types/int-uint.md) 或 [浮点](../data-types/float.md).
类型 `x``weight` 一定是一样的
`x``weight` 的类型一定是一样的
**返回值**
@ -590,7 +588,7 @@ uniq(x[, ...])
- A [UInt64](../../sql-reference/data-types/int-uint.md)-键入号码。
**实细节**
**实细节**
功能:
@ -598,7 +596,7 @@ uniq(x[, ...])
- 使用自适应采样算法。 对于计算状态该函数使用最多65536个元素哈希值的样本。
This algorithm is very accurate and very efficient on the CPU. When the query contains several of these functions, using `uniq` is almost as fast as using other aggregate functions.
这个算法是非常精确的并且对于CPU来说非常高效。如果查询包含一些这样的函数那和其他聚合函数相比 `uniq` 将是几乎一样快。
- 确定性地提供结果(它不依赖于查询处理顺序)。
@ -629,17 +627,17 @@ uniqCombined(HLL_precision)(x[, ...])
**返回值**
- 一个数字 [UInt64](../../sql-reference/data-types/int-uint.md)-键入号码
- 一个[UInt64](../../sql-reference/data-types/int-uint.md)类型的数字
**实细节**
**实细节**
功能:
- 计算散列64位散列 `String` 否则32位对于聚合中的所有参数然后在计算中使用它。
- 使用三种算法的组合数组、哈希表和HyperLogLog与error错表
- 使用三种算法的组合:数组、哈希表和包含错误修正表的HyperLogLog。
For a small number of distinct elements, an array is used. When the set size is larger, a hash table is used. For a larger number of elements, HyperLogLog is used, which will occupy a fixed amount of memory.
少量的不同的值,使用数组。 值再多一些使用哈希表。对于大量的数据来说使用HyperLogLogHyperLogLog占用一个固定的内存空间。
- 确定性地提供结果(它不依赖于查询处理顺序)。
@ -650,7 +648,7 @@ uniqCombined(HLL_precision)(x[, ...])
- 消耗少几倍的内存。
- 计算精度高出几倍。
- 通常具有略低的性能。 在某些情况下, `uniqCombined` 可以表现得比 `uniq`,例如,使用通过网络传输大量聚合状态的分布式查询。
- 通常具有略低的性能。 在某些情况下, `uniqCombined` 可以表现得比 `uniq`,例如,使用通过网络传输大量聚合状态的分布式查询。
**另请参阅**
@ -679,7 +677,7 @@ uniqHLL12(x[, ...])
- A [UInt64](../../sql-reference/data-types/int-uint.md)-键入号码。
**实细节**
**实细节**
功能:
@ -707,9 +705,9 @@ uniqHLL12(x[, ...])
uniqExact(x[, ...])
```
使用 `uniqExact` 功能,如果你绝对需要一个确切的结果。 否则使用 [uniq](#agg_function-uniq) 功能。
如果你绝对需要一个确切的结果,使用 `uniqExact` 功能。 否则使用 [uniq](#agg_function-uniq) 功能。
`uniqExact` 功能使用更多的内存`uniq`,因为状态的大小随着不同值的数量的增加而无界增长。
`uniqExact``uniq` 使用更多的内存,因为状态的大小随着不同值的数量的增加而无界增长。
**参数**
@ -721,7 +719,7 @@ uniqExact(x[, ...])
- [uniqCombined](#agg_function-uniqcombined)
- [uniqHLL12](#agg_function-uniqhll12)
## 群交(x),群交(max\_size)(x) {#agg_function-grouparray}
## groupArray(x), groupArray(max\_size)(x) {#agg_function-grouparray}
创建参数值的数组。
值可以按任何(不确定)顺序添加到数组中。
@ -748,10 +746,10 @@ groupArrayInsertAt(default_x, size)(x, pos);
**参数**
- `x`Value to be inserted. [表达式](../syntax.md#syntax-expressions) 导致的一个 [支持的数据类型](../../sql-reference/data-types/index.md).
- `pos`Position at which the specified element `x` 将被插入。 数组中的索引编号从零开始。 [UInt32](../../sql-reference/data-types/int-uint.md#uint-ranges).
- `default_x`Default value for substituting in empty positions. Optional parameter. [表达式](../syntax.md#syntax-expressions) 导致为配置的数据类型 `x` 参数。 如果 `default_x` 未定义,则 [默认值](../../sql-reference/statements/create.md#create-default-values) 被使用。
- `size`Length of the resulting array. Optional parameter. When using this parameter, the default value `default_x` 必须指定。 [UInt32](../../sql-reference/data-types/int-uint.md#uint-ranges).
- `x`被插入的值。[表达式](../syntax.md#syntax-expressions) 导致的一个 [支持的数据类型](../../sql-reference/data-types/index.md).
- `pos``x` 将被插入的位置。 数组中的索引编号从零开始。 [UInt32](../../sql-reference/data-types/int-uint.md#uint-ranges).
- `default_x`如果代入值为空,则使用默认值。可选参数。[表达式](../syntax.md#syntax-expressions) 为 `x` 数据类型的数据。 如果 `default_x` 未定义,则 [默认值](../../sql-reference/statements/create.md#create-default-values) 被使用。
- `size`结果数组的长度。可选参数。如果使用该参数,`default_x` 必须指定。 [UInt32](../../sql-reference/data-types/int-uint.md#uint-ranges).
**返回值**
@ -803,7 +801,7 @@ SELECT groupArrayInsertAt('-', 5)(toString(number), number * 2) FROM numbers(5);
└───────────────────────────────────────────────────────────────────┘
```
元件的多线程插入到一个位置
在一个位置多线程插入数据
查询:
@ -832,8 +830,8 @@ groupArrayMovingSum(window_size)(numbers_for_summing)
**参数**
- `numbers_for_summing` — [表达式](../syntax.md#syntax-expressions) 生成数值数据类型值。
- `window_size`Size of the calculation window.
- `numbers_for_summing` — [表达式](../syntax.md#syntax-expressions) 数值数据类型值。
- `window_size`窗口大小。
**返回值**
@ -906,13 +904,13 @@ groupArrayMovingAvg(window_size)(numbers_for_summing)
**参数**
- `numbers_for_summing` — [表达式](../syntax.md#syntax-expressions) 生成数值数据类型值。
- `window_size`Size of the calculation window.
- `window_size`窗口大小。
**返回值**
- 与输入数据大小和类型相同的数组。
该函数使用 [四舍五入到零](https://en.wikipedia.org/wiki/Rounding#Rounding_towards_zero). 它截断结果数据类型的小数位数
该函数使用 [四舍五入到零](https://en.wikipedia.org/wiki/Rounding#Rounding_towards_zero). 它截断无意义的小数位来保证结果数据类型。
**示例**
@ -967,20 +965,20 @@ FROM t
└───────────┴──────────────────────────────────┴───────────────────────┘
```
## 禄,赂麓ta脌麓,):脡,,拢脢,group媒group)galaxy s8碌胫脢)禄煤)酶脱脩) {#groupuniqarrayx-groupuniqarraymax-sizex}
## groupUniqArray(x), groupUniqArray(max\_size)(x) {#groupuniqarrayx-groupuniqarraymax-sizex}
从不同的参数值创建一个数组。 内存消耗是一样的 `uniqExact` 功能。
第二个版本(`max_size` 参数)将结果数组的大小限制为 `max_size` 元素。
第二个版本(`max_size` 参数)将结果数组的大小限制为 `max_size` 元素。
例如, `groupUniqArray(1)(x)` 相当于 `[any(x)]`.
## 分位数 {#quantile}
## quantile {#quantile}
计算近似[分位数](https://en.wikipedia.org/wiki/Quantile) 的数字数据序列
计算数字序列的近似[分位数](https://en.wikipedia.org/wiki/Quantile)。
此功能适用 [油藏采样](https://en.wikipedia.org/wiki/Reservoir_sampling) 随着储存器大小高达8192和随机数发生器进行采样。 结果是非确定性的。 要获得精确的分位数,请使用 [quantileExact](#quantileexact) 功能。
此功能适用 [水塘抽样(](https://en.wikipedia.org/wiki/Reservoir_sampling),使用储存器最大到8192和随机数发生器进行采样。 结果是非确定性的。 要获得精确的分位数,请使用 [quantileExact](#quantileexact) 功能。
使用多个 `quantile*` 在查询中具有不同级别的函数,内部状态不会被组合(即查询的工作效率低于它可以)。 在这种情况下,使用 [分位数](#quantiles) 功能。
在一个查询中使用多个不同层次的 `quantile*` 时,内部状态不会被组合(即查询的工作效率低于组合情况)。在这种情况下,使用[分位数](#quantiles)功能。
**语法**
@ -992,12 +990,12 @@ quantile(level)(expr)
**参数**
- `level`Level of quantile. Optional parameter. Constant floating-point number from 0 to 1. We recommend using a `level` 值的范围 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`Expression over the column values resulting in numeric [数据类型](../../sql-reference/data-types/index.md#data_types), [日期](../../sql-reference/data-types/date.md) 或 [日期时间](../../sql-reference/data-types/datetime.md).
- `level`分位数层次。可选参数。 从0到1的一个float类型的常量。 我们推荐 `level` 值的范围为 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`求职表达式,类型为:数值[数据类型](../../sql-reference/data-types/index.md#data_types),[日期](../../sql-reference/data-types/date.md)数据类型或[时间](../../sql-reference/data-types/datetime.md)数据类型。
**返回值**
- 指定电平的近似分位数。
- 指定层次的近似分位数。
类型:
@ -1037,13 +1035,13 @@ SELECT quantile(val) FROM t
- [中位数](#median)
- [分位数](#quantiles)
## 量化确定 {#quantiledeterministic}
## quantileDeterministic {#quantiledeterministic}
计算近似[分位数](https://en.wikipedia.org/wiki/Quantile) 的数字数据序列
计算数字序列的近似[分位数](https://en.wikipedia.org/wiki/Quantile)。
此功能适用 [油藏采样](https://en.wikipedia.org/wiki/Reservoir_sampling) 与储层大小高达8192和采样的确定性算法。 结果是确定性的。 要获得精确的分位数,请使用 [quantileExact](#quantileexact) 功能。
此功能适用 [水塘抽样(](https://en.wikipedia.org/wiki/Reservoir_sampling)使用储存器最大到8192和随机数发生器进行采样。 结果是非确定性的。 要获得精确的分位数,请使用 [quantileExact](#quantileexact) 功能。
使用多个 `quantile*` 在查询中具有不同级别的函数,内部状态不会被组合(即查询的工作效率低于它可以)。 在这种情况下,使用 [分位数](#quantiles) 功能。
在一个查询中使用多个不同层次的 `quantile*` 时,内部状态不会被组合(即查询的工作效率低于组合情况)。在这种情况下,使用[分位数](#quantiles)功能。
**语法**
@ -1055,13 +1053,13 @@ quantileDeterministic(level)(expr, determinator)
**参数**
- `level`Level of quantile. Optional parameter. Constant floating-point number from 0 to 1. We recommend using a `level` 值的范围 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`Expression over the column values resulting in numeric [数据类型](../../sql-reference/data-types/index.md#data_types), [日期](../../sql-reference/data-types/date.md) 或 [日期时间](../../sql-reference/data-types/datetime.md).
- `determinator`Number whose hash is used instead of a random number generator in the reservoir sampling algorithm to make the result of sampling deterministic. As a determinator you can use any deterministic positive number, for example, a user id or an event id. If the same determinator value occures too often, the function works incorrectly.
- `level`分位数层次。可选参数。 从0到1的一个float类型的常量。 我们推荐 `level` 值的范围为 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`求职表达式,类型为:数值[数据类型](../../sql-reference/data-types/index.md#data_types),[日期](../../sql-reference/data-types/date.md)数据类型或[时间](../../sql-reference/data-types/datetime.md)数据类型。
- `determinator`一个数字其hash被用来代替在水塘抽样中随机生成的数字这样可以保证取样的确定性。你可以使用用户ID或者事件ID等任何正数但是如果相同的 `determinator` 出现多次,那结果很可能不正确。
**返回值**
- 指定电平的近似分位数。
- 指定层次的近似分位数。
类型:
@ -1103,11 +1101,11 @@ SELECT quantileDeterministic(val, 1) FROM t
## quantileExact {#quantileexact}
正是计算 [分位数](https://en.wikipedia.org/wiki/Quantile) 的数字数据序列
准确计算数字序列的[分位数](https://en.wikipedia.org/wiki/Quantile)
To get exact value, all the passed values are combined into an array, which is then partially sorted. Therefore, the function consumes `O(n)` 内存,其中 `n` 是传递的多个值。 然而,对于少量的值,该函数是非常有效的。
为了准确计算,所有输入的数据被合并为一个数组,并且部分的排序。因此该函数需要 `O(n)` 的内存n为输入数据的个数。但是对于少量数据来说该函数还是非常有效的。
使用多个 `quantile*` 在查询中具有不同级别的函数,内部状态不会被组合(即查询的工作效率低于它可以)。 在这种情况下,使用 [分位数](#quantiles) 功能。
在一个查询中使用多个不同层次的 `quantile*` 时,内部状态不会被组合(即查询的工作效率低于组合情况)。在这种情况下,使用[分位数](#quantiles)功能。
**语法**
@ -1119,12 +1117,12 @@ quantileExact(level)(expr)
**参数**
- `level`Level of quantile. Optional parameter. Constant floating-point number from 0 to 1. We recommend using a `level` 值的范围 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`Expression over the column values resulting in numeric [数据类型](../../sql-reference/data-types/index.md#data_types), [日期](../../sql-reference/data-types/date.md) 或 [日期时间](../../sql-reference/data-types/datetime.md).
- `level`分位数层次。可选参数。 从0到1的一个float类型的常量。 我们推荐 `level` 值的范围为 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`求职表达式,类型为:数值[数据类型](../../sql-reference/data-types/index.md#data_types),[日期](../../sql-reference/data-types/date.md)数据类型或[时间](../../sql-reference/data-types/datetime.md)数据类型。
**返回值**
- 指定电平的分位数。
- 指定层次的分位数。
类型:
@ -1153,13 +1151,13 @@ SELECT quantileExact(number) FROM numbers(10)
- [中位数](#median)
- [分位数](#quantiles)
## 分位数加权 {#quantileexactweighted}
## quantileExactWeighted {#quantileexactweighted}
正是计算 [分位数](https://en.wikipedia.org/wiki/Quantile) 数值数据序列,考虑到每个元素的权重
考虑到每个元素的权重,然后准确计算数值序列的[分位数](https://en.wikipedia.org/wiki/Quantile)
To get exact value, all the passed values are combined into an array, which is then partially sorted. Each value is counted with its weight, as if it is present `weight` times. A hash table is used in the algorithm. Because of this, if the passed values are frequently repeated, the function consumes less RAM than [quantileExact](#quantileexact). 您可以使用此功能,而不是 `quantileExact` 并指定重量1。
为了准确计算,所有输入的数据被合并为一个数组,并且部分的排序。每个输入值需要根据 `weight` 计算求和。该算法使用哈希表。正因为如此,在数据重复较多的时候使用的内存是少于[quantileExact](#quantileexact)的。 您可以使用此函数代替 `quantileExact` 并指定重量1。
使用多个 `quantile*` 在查询中具有不同级别的函数,内部状态不会被组合(即查询的工作效率低于它可以)。 在这种情况下,使用 [分位数](#quantiles) 功能。
在一个查询中使用多个不同层次的 `quantile*` 时,内部状态不会被组合(即查询的工作效率低于组合情况)。在这种情况下,使用[分位数](#quantiles)功能。
**语法**
@ -1171,13 +1169,13 @@ quantileExactWeighted(level)(expr, weight)
**参数**
- `level`Level of quantile. Optional parameter. Constant floating-point number from 0 to 1. We recommend using a `level` 值的范围 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`Expression over the column values resulting in numeric [数据类型](../../sql-reference/data-types/index.md#data_types), [日期](../../sql-reference/data-types/date.md) 或 [日期时间](../../sql-reference/data-types/datetime.md).
- `weight`Column with weights of sequence members. Weight is a number of value occurrences.
- `level`分位数层次。可选参数。 从0到1的一个float类型的常量。 我们推荐 `level` 值的范围为 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`求职表达式,类型为:数值[数据类型](../../sql-reference/data-types/index.md#data_types),[日期](../../sql-reference/data-types/date.md)数据类型或[时间](../../sql-reference/data-types/datetime.md)数据类型。
- `weight`权重序列。 权重是一个数据出现的数值。
**返回值**
- 指定电平的分位数。
- 指定层次的分位数。
类型:
@ -1217,13 +1215,13 @@ SELECT quantileExactWeighted(n, val) FROM t
- [中位数](#median)
- [分位数](#quantiles)
## 分位定时 {#quantiletiming}
## quantileTiming {#quantiletiming}
随着确定的精度计算 [分位数](https://en.wikipedia.org/wiki/Quantile) 的数字数据序列
使用确定的精度计算数字数据序列的[分位数](https://en.wikipedia.org/wiki/Quantile)
结果是确定性的(它不依赖于查询处理顺序)。 该函数针对描述加载网页时间或后端响应时间等分布的序列进行了优化。
使用多个 `quantile*` 在查询中具有不同级别的函数,内部状态不会被组合(即查询的工作效率低于它可以)。 在这种情况下,使用 [分位数](#quantiles) 功能。
在一个查询中使用多个不同层次的 `quantile*` 时,内部状态不会被组合(即查询的工作效率低于组合情况)。在这种情况下,使用[分位数](#quantiles)功能。
**语法**
@ -1235,12 +1233,12 @@ quantileTiming(level)(expr)
**参数**
- `level`Level of quantile. Optional parameter. Constant floating-point number from 0 to 1. We recommend using a `level` 值的范围 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `level`分位数层次。可选参数。 从0到1的一个float类型的常量。 我们推荐 `level` 值的范围为 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr` — [表达式](../syntax.md#syntax-expressions) 在一个列值返回 [浮动\*](../../sql-reference/data-types/float.md)-键入号码
- `expr` — [表达式](../syntax.md#syntax-expressions),返回 [浮动\*](../../sql-reference/data-types/float.md)类型数据
- If negative values are passed to the function, the behavior is undefined.
- If the value is greater than 30,000 (a page loading time of more than 30 seconds), it is assumed to be 30,000.
- 如果输入负值,那结果是不可预期的。
- 如果输入值大于30000页面加载时间大于30s那我们假设为30000。
**精度**
@ -1252,16 +1250,16 @@ quantileTiming(level)(expr)
否则计算结果将四舍五入到16毫秒的最接近倍数。
!!! note "注"
对于计算页面加载时间分位数,此函数比 [分位数](#quantile).
对于计算页面加载时间分位数,此函数比 [分位数](#quantile)更有效和准确。
**返回值**
- 指定电平的分位数。
- 指定层次的分位数。
类型: `Float32`.
!!! note "注"
如果没有值传递给函数(当使用 `quantileTimingIf`), [阿南](../../sql-reference/data-types/float.md#data_type-float-nan-inf) 被返回。 这样做的目的是将这些案例与导致零的案例区分开来。 看 [按条款订购](../statements/select/order-by.md#select-order-by) 对于排序注意事项 `NaN` 值。
如果没有值传递给函数(当使用 `quantileTimingIf`), [NaN](../../sql-reference/data-types/float.md#data_type-float-nan-inf) 被返回。 这样做的目的是将这些案例与导致零的案例区分开来。 看 [ORDER BY clause](../statements/select/order-by.md#select-order-by) 对于 `NaN`排序注意事项
**示例**
@ -1300,13 +1298,13 @@ SELECT quantileTiming(response_time) FROM t
- [中位数](#median)
- [分位数](#quantiles)
## 分位时间加权 {#quantiletimingweighted}
## quantileTimingWeighted {#quantiletimingweighted}
随着确定的精度计算 [分位数](https://en.wikipedia.org/wiki/Quantile) 根据每个序列成员的权重对数字数据序列进行处理
根据每个序列成员的权重,使用确定的精度计算数字序列的[分位数](https://en.wikipedia.org/wiki/Quantile)
结果是确定性的(它不依赖于查询处理顺序)。 该函数针对描述加载网页时间或后端响应时间等分布的序列进行了优化。
使用多个 `quantile*` 在查询中具有不同级别的函数,内部状态不会被组合(即查询的工作效率低于它可以)。 在这种情况下,使用 [分位数](#quantiles) 功能。
在一个查询中使用多个不同层次的 `quantile*` 时,内部状态不会被组合(即查询的工作效率低于组合情况)。在这种情况下,使用[分位数](#quantiles)功能。
**语法**
@ -1318,14 +1316,14 @@ quantileTimingWeighted(level)(expr, weight)
**参数**
- `level`Level of quantile. Optional parameter. Constant floating-point number from 0 to 1. We recommend using a `level` 值的范围 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `level`分位数层次。可选参数。 从0到1的一个float类型的常量。 我们推荐 `level` 值的范围为 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr` — [表达式](../syntax.md#syntax-expressions) 在一个列值返回 [浮动\*](../../sql-reference/data-types/float.md)-键入号码
- `expr` — [表达式](../syntax.md#syntax-expressions),返回 [浮动\*](../../sql-reference/data-types/float.md)类型数据
- If negative values are passed to the function, the behavior is undefined.
- If the value is greater than 30,000 (a page loading time of more than 30 seconds), it is assumed to be 30,000.
- 如果输入负值,那结果是不可预期的。
- 如果输入值大于30000页面加载时间大于30s那我们假设为30000。
- `weight`Column with weights of sequence elements. Weight is a number of value occurrences.
- `weight`权重序列。 权重是一个数据出现的数值。
**精度**
@ -1337,16 +1335,16 @@ quantileTimingWeighted(level)(expr, weight)
否则计算结果将四舍五入到16毫秒的最接近倍数。
!!! note "注"
对于计算页面加载时间分位数,此函数比 [分位数](#quantile).
对于计算页面加载时间分位数,此函数比 [分位数](#quantile)更高效和准确。
**返回值**
- 指定电平的分位数。
- 指定层次的分位数。
类型: `Float32`.
!!! note "注"
如果没有值传递给函数(当使用 `quantileTimingIf`), [阿南](../../sql-reference/data-types/float.md#data_type-float-nan-inf) 被返回。 这样做的目的是将这些案例与导致零的案例区分开来。 看 [按条款订购](../statements/select/order-by.md#select-order-by) 对于排序注意事项 `NaN` 值。
如果没有值传递给函数(当使用 `quantileTimingIf`), [NaN](../../sql-reference/data-types/float.md#data_type-float-nan-inf) 被返回。 这样做的目的是将这些案例与导致零的案例区分开来。看 [ORDER BY clause](../statements/select/order-by.md#select-order-by) 对于 `NaN`排序注意事项
**示例**
@ -1384,13 +1382,13 @@ SELECT quantileTimingWeighted(response_time, weight) FROM t
## quantileTDigest {#quantiletdigest}
计算近似值 [分位数](https://en.wikipedia.org/wiki/Quantile) 使用的数字数据序列 [t-digest](https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf) 算法。
使用[t-digest](https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf) 算法计算近似[分位数](https://en.wikipedia.org/wiki/Quantile)
最大误差为1%。 内存消耗 `log(n)`哪里 `n` 是多个值。 结果取决于运行查询的顺序,并且是不确定的。
最大误差为1%。 内存消耗 `log(n)`这里 `n` 是值的个数。 结果取决于运行查询的顺序,并且是不确定的。
该功能的性能低于性能 [分位数](#quantile) 或 [分位定时](#quantiletiming). 在状态大小与精度的比率方面,这个函数比 `quantile`.
该功能的性能低于性能 [分位数](#quantile) 或 [时间分位](#quantiletiming). 在状态大小与精度的比率方面,这个函数比 `quantile`更优秀。
使用多个 `quantile*` 在查询中具有不同级别的函数,内部状态不会被组合(即查询的工作效率低于它可以)。 在这种情况下,使用 [分位数](#quantiles) 功能。
在一个查询中使用多个不同层次的 `quantile*` 时,内部状态不会被组合(即查询的工作效率低于组合情况)。在这种情况下,使用[分位数](#quantiles)功能。
**语法**
@ -1402,12 +1400,12 @@ quantileTDigest(level)(expr)
**参数**
- `level`Level of quantile. Optional parameter. Constant floating-point number from 0 to 1. We recommend using a `level` 值的范围 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`Expression over the column values resulting in numeric [数据类型](../../sql-reference/data-types/index.md#data_types), [日期](../../sql-reference/data-types/date.md) 或 [日期时间](../../sql-reference/data-types/datetime.md).
- `level`分位数层次。可选参数。 从0到1的一个float类型的常量。 我们推荐 `level` 值的范围为 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`求职表达式,类型为:数值[数据类型](../../sql-reference/data-types/index.md#data_types),[日期](../../sql-reference/data-types/date.md)数据类型或[时间](../../sql-reference/data-types/datetime.md)数据类型。
**回值**
- 指定电平的近似分位数。
- 指定层次的分位数。
类型:
@ -1438,13 +1436,13 @@ SELECT quantileTDigest(number) FROM numbers(10)
## quantileTDigestWeighted {#quantiletdigestweighted}
计算近似值 [分位数](https://en.wikipedia.org/wiki/Quantile) 使用的数字数据序列 [t-digest](https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf) 算法。 该函数考虑了每个序列成员的权重。 最大误差为1%。 内存消耗 `log(n)`哪里 `n` 是多个值
使用[t-digest](https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf) 算法计算近似[分位数](https://en.wikipedia.org/wiki/Quantile)。 该函数考虑了每个序列成员的权重。最大误差为1%。 内存消耗 `log(n)`这里 `n` 是值的个数
该功能的性能低于性能 [分位数](#quantile) 或 [分位定时](#quantiletiming). 在状态大小与精度的比率方面,这个函数比 `quantile`.
该功能的性能低于性能 [分位数](#quantile) 或 [时间分位](#quantiletiming). 在状态大小与精度的比率方面,这个函数比 `quantile`更优秀。
结果取决于运行查询的顺序,并且是不确定的。
使用多个 `quantile*` 在查询中具有不同级别的函数,内部状态不会被组合(即查询的工作效率低于它可以)。 在这种情况下,使用 [分位数](#quantiles) 功能。
在一个查询中使用多个不同层次的 `quantile*` 时,内部状态不会被组合(即查询的工作效率低于组合情况)。在这种情况下,使用[分位数](#quantiles)功能
**语法**
@ -1456,13 +1454,13 @@ quantileTDigest(level)(expr)
**参数**
- `level`Level of quantile. Optional parameter. Constant floating-point number from 0 to 1. We recommend using a `level` 值的范围 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`Expression over the column values resulting in numeric [数据类型](../../sql-reference/data-types/index.md#data_types), [日期](../../sql-reference/data-types/date.md) 或 [日期时间](../../sql-reference/data-types/datetime.md).
- `weight`Column with weights of sequence elements. Weight is a number of value occurrences.
- `level`分位数层次。可选参数。 从0到1的一个float类型的常量。 我们推荐 `level` 值的范围为 `[0.01, 0.99]`. 默认值0.5。 在 `level=0.5` 该函数计算 [中位数](https://en.wikipedia.org/wiki/Median).
- `expr`求职表达式,类型为:数值[数据类型](../../sql-reference/data-types/index.md#data_types),[日期](../../sql-reference/data-types/date.md)数据类型或[时间](../../sql-reference/data-types/datetime.md)数据类型。
- `weight`权重序列。 权重是一个数据出现的数值。
**返回值**
- 指定电平的近似分位数。
- 指定层次的分位数。
类型:
@ -1491,20 +1489,20 @@ SELECT quantileTDigestWeighted(number, 1) FROM numbers(10)
- [中位数](#median)
- [分位数](#quantiles)
## 中位数 {#median}
## median {#median}
`median*` 函数是相应的别名 `quantile*` 功能。 它们计算数字数据样本的中位数。
`median*` 函数是 `quantile*` 函数的别名。 它们计算数字数据样本的中位数。
功能:
函数:
- `median`Alias for [分位数](#quantile).
- `medianDeterministic`Alias for [量化确定](#quantiledeterministic).
- `medianExact`Alias for [quantileExact](#quantileexact).
- `medianExactWeighted`Alias for [分位数加权](#quantileexactweighted).
- `medianTiming`Alias for [分位定时](#quantiletiming).
- `medianTimingWeighted`Alias for [分位时间加权](#quantiletimingweighted).
- `medianTDigest`Alias for [quantileTDigest](#quantiletdigest).
- `medianTDigestWeighted`Alias for [quantileTDigestWeighted](#quantiletdigestweighted).
- `median`[quantile](#quantile)别名。
- `medianDeterministic`[quantileDeterministic](#quantiledeterministic)别名。
- `medianExact`[quantileExact](#quantileexact)别名。
- `medianExactWeighted`[quantileExactWeighted](#quantileexactweighted)别名。
- `medianTiming`[quantileTiming](#quantiletiming)别名。
- `medianTimingWeighted`[quantileTimingWeighted](#quantiletimingweighted)别名。
- `medianTDigest` — [quantileTDigest](#quantiletdigest)别名。
- `medianTDigestWeighted` — [quantileTDigestWeighted](#quantiletdigestweighted)别名。
**示例**
@ -1535,11 +1533,11 @@ SELECT medianDeterministic(val, 1) FROM t
## quantiles(level1, level2, …)(x) {#quantiles}
所有分位数函数也有相应的分位数函数: `quantiles`, `quantilesDeterministic`, `quantilesTiming`, `quantilesTimingWeighted`, `quantilesExact`, `quantilesExactWeighted`, `quantilesTDigest`. 这些函数在一遍中计算所列电平的所有分位数,并返回结果值的数组。
所有分位数函数也有相应的函数: `quantiles`, `quantilesDeterministic`, `quantilesTiming`, `quantilesTimingWeighted`, `quantilesExact`, `quantilesExactWeighted`, `quantilesTDigest`。这些函数一次计算所列层次的所有分位数,并返回结果值的数组。
## varSamp(x) {#varsampx}
计算金额 `Σ((x - x̅)^2) / (n - 1)`,哪里 `n` 是样本大小和 `x̅`是平均值 `x`.
计算 `Σ((x - x̅)^2) / (n - 1)`,这里 `n` 是样本大小, `x̅`是`x`的平均值。
它表示随机变量的方差的无偏估计,如果传递的值形成其样本。
@ -1550,23 +1548,23 @@ SELECT medianDeterministic(val, 1) FROM t
## varPop(x) {#varpopx}
计算金额 `Σ((x - x̅)^2) / n`,哪里 `n` 是样本大小和 `x̅`是平均值 `x`.
计算 `Σ((x - x̅)^2) / n`,这里 `n` 是样本大小, `x̅`是`x`的平均值。
换句话说,分散为一组值。 返回 `Float64`.
换句话说,计算一组数据的离差。 返回 `Float64`
!!! note "注"
该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `varPopStable` 功能。 它的工作速度较慢,但提供较低的计算错误。
## stddevSamp(x) {#stddevsampx}
结果等于平方根 `varSamp(x)`.
结果等于平方根 `varSamp(x)`
!!! note "注"
该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `stddevSampStable` 功能。 它的工作速度较慢,但提供较低的计算错误。
## stddevPop(x) {#stddevpopx}
结果等于平方根 `varPop(x)`.
结果等于平方根 `varPop(x)`
!!! note "注"
该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `stddevPopStable` 功能。 它的工作速度较慢,但提供较低的计算错误。
@ -1575,15 +1573,15 @@ SELECT medianDeterministic(val, 1) FROM t
返回指定列中近似最常见值的数组。 生成的数组按值的近似频率降序排序(而不是值本身)。
实现了 [过滤节省空间](http://www.l2f.inesc-id.pt/~fmmb/wiki/uploads/Work/misnis.ref0a.pdf) 基于reduce-and-combine算法的TopK分析算法 [并行节省空间](https://arxiv.org/pdf/1401.0702.pdf).
实现了[过滤节省空间](http://www.l2f.inesc-id.pt/~fmmb/wiki/uploads/Work/misnis.ref0a.pdf)算法, 使用基于reduce-and-combine的算法借鉴[并行节省空间](https://arxiv.org/pdf/1401.0702.pdf).
``` sql
topK(N)(column)
```
此函数不提供保证的结果。 在某些情况下,可能会发生错误,并且可能会返回不是最常见值的常见值。
此函数不提供保证的结果。 在某些情况下,可能会发生错误,并且可能会返回不是最高频的值。
我们建议使用 `N < 10`;性能降低了大 `N` 值。 的最大值 `N = 65536`.
我们建议使用 `N < 10``N` 值越大,性能越低。最大值 `N = 65536`
**参数**
@ -1593,11 +1591,11 @@ topK(N)(column)
**参数**
- ' x ' The value to calculate frequency.
- ' x ' 计算的频率值。
**示例**
就拿 [时间](../../getting-started/example-datasets/ontime.md) 数据集,选择在三个最频繁出现的值 `AirlineID` 列。
就拿 [OnTime](../../getting-started/example-datasets/ontime.md) 数据集来说,选择`AirlineID` 列中出现最频繁的三个
``` sql
SELECT topK(3)(AirlineID) AS res
@ -1612,7 +1610,7 @@ FROM ontime
## topKWeighted {#topkweighted}
类似于 `topK` 但需要一个整数类型的附加参数 - `weight`. 每个价值都被记入 `weight` 次频率计算。
类似于 `topK` 但需要一个整数类型的附加参数 - `weight`. 每个输入都被记入 `weight` 次频率计算。
**语法**
@ -1622,12 +1620,12 @@ topKWeighted(N)(x, weight)
**参数**
- `N`The number of elements to return.
- `N`返回值个数。
**参数**
- `x` The value.
- `weight`The weight. [UInt8](../../sql-reference/data-types/int-uint.md).
- `x` 输入值。
- `weight`权重。 [UInt8](../../sql-reference/data-types/int-uint.md)类型。
**返回值**
@ -1651,36 +1649,36 @@ SELECT topKWeighted(10)(number, number) FROM numbers(1000)
## covarSamp(x,y) {#covarsampx-y}
计算的值 `Σ((x - x̅)(y - y̅)) / (n - 1)`.
计算 `Σ((x - x̅)(y - y̅)) / (n - 1)`
返回Float64。 当 `n <= 1`, returns +∞.
返回Float64。 当 `n <= 1`, returns +∞
!!! note "注"
该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `covarSampStable` 功能。 它的工作速度较慢,但提供较低的计算错误。
## covarPop(x,y) {#covarpopx-y}
计算的值 `Σ((x - x̅)(y - y̅)) / n`.
计算 `Σ((x - x̅)(y - y̅)) / n`
!!! note "注"
该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `covarPopStable` 功能。 它的工作速度较慢,但提供了较低的计算错误。
## corr(x,y) {#corrx-y}
计算Pearson相关系数: `Σ((x - x̅)(y - y̅)) / sqrt(Σ((x - x̅)^2) * Σ((y - y̅)^2))`.
计算Pearson相关系数: `Σ((x - x̅)(y - y̅)) / sqrt(Σ((x - x̅)^2) * Σ((y - y̅)^2))`
!!! note "注"
该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `corrStable` 功能。 它的工作速度较慢,但提供较低的计算错误。
## categoricalInformationValue {#categoricalinformationvalue}
计算的值 `(P(tag = 1) - P(tag = 0))(log(P(tag = 1)) - log(P(tag = 0)))` 对于每个类别
对于每个类别计算 `(P(tag = 1) - P(tag = 0))(log(P(tag = 1)) - log(P(tag = 0)))`
``` sql
categoricalInformationValue(category1, category2, ..., tag)
```
结果指示离散(分类)要素如何使用 `[category1, category2, ...]` 有助于预测的价值的学习模型 `tag`.
结果指示离散(分类)要素如何使用 `[category1, category2, ...]` 有助于使用学习模型预测`tag`的值。
## simpleLinearRegression {#simplelinearregression}
@ -1692,12 +1690,12 @@ simpleLinearRegression(x, y)
参数:
- `x`Column with dependent variable values.
- `y`Column with explanatory variable values.
- `x`x轴。
- `y`y轴。
返回值:
常量 `(a, b)` 结果行的 `y = a*x + b`.
符合`y = a*x + b`的常量 `(a, b)`
**例**
@ -1721,9 +1719,9 @@ SELECT arrayReduce('simpleLinearRegression', [0, 1, 2, 3], [3, 4, 5, 6])
└───────────────────────────────────────────────────────────────────┘
```
## 随机指标线上回归 {#agg_functions-stochasticlinearregression}
## stochasticLinearRegression {#agg_functions-stochasticlinearregression}
该函数实现随机线性回归。 它支持自定义参数的学习率L2正则化系数迷你批量大小并具有更新权重的方法很少 ([亚当](https://en.wikipedia.org/wiki/Stochastic_gradient_descent#Adam) (默认使用), [简单SGD](https://en.wikipedia.org/wiki/Stochastic_gradient_descent), [动量](https://en.wikipedia.org/wiki/Stochastic_gradient_descent#Momentum), [Nesterov](https://mipt.ru/upload/medialibrary/d7e/41-91.pdf)).
该函数实现随机线性回归。 它支持自定义参数的学习率、L2正则化系数、微批并且具有少量更新权重的方法[Adam](https://en.wikipedia.org/wiki/Stochastic_gradient_descent#Adam) (默认), [simple SGD](https://en.wikipedia.org/wiki/Stochastic_gradient_descent) [Momentum](https://en.wikipedia.org/wiki/Stochastic_gradient_descent#Momentum) [Nesterov](https://mipt.ru/upload/medialibrary/d7e/41-91.pdf))。
### 参数 {#agg_functions-stochasticlinearregression-parameters}
@ -1738,14 +1736,14 @@ stochasticLinearRegression(1.0, 1.0, 10, 'SGD')
3. `mini-batch size` 设置元素的数量,这些元素将被计算和求和以执行梯度下降的一个步骤。 纯随机下降使用一个元素但是具有小批量约10个元素使梯度步骤更稳定。 默认值为 `15`.
4. `method for updating weights` 他们是: `Adam` (默认情况下), `SGD`, `Momentum`, `Nesterov`. `Momentum``Nesterov` 需要更多的计算和内存,但是它们恰好在收敛速度和随机梯度方法的稳定性方面是有用的。
### 用 {#agg_functions-stochasticlinearregression-usage}
### 用 {#agg_functions-stochasticlinearregression-usage}
`stochasticLinearRegression` 用于两个步骤:拟合模型和预测新数据。 为了拟合模型并保存其状态以供以后使用,我们使用 `-State` combinator它基本上保存了状态模型权重等
为了预测我们使用函数 [evalMLMethod](../functions/machine-learning-functions.md#machine_learning_methods-evalmlmethod),这需要一个状态作为参数以及特征来预测。
<a name="stochasticlinearregression-usage-fitting"></a>
**1.** 适合
**1.** 安装
可以使用这种查询。
@ -1807,28 +1805,28 @@ evalMLMethod(model, param1, param2) FROM test_data
stochasticLogisticRegression(1.0, 1.0, 10, 'SGD')
```
1. 适合
**1.** 安装
<!-- -->
See the `Fitting` section in the [stochasticLinearRegression](#stochasticlinearregression-usage-fitting) description.
参考stochasticLinearRegression相关文档
Predicted labels have to be in \[-1, 1\].
预测标签的取值范围为[-1, 1]
1. 预测
**2.** 预测
<!-- -->
Using saved state we can predict probability of object having label `1`.
使用已经保存的state我们可以预测标签为 `1` 的对象的概率。
``` sql
WITH (SELECT state FROM your_model) AS model SELECT
evalMLMethod(model, param1, param2) FROM test_data
```
The query will return a column of probabilities. Note that first argument of `evalMLMethod` is `AggregateFunctionState` object, next are columns of features.
查询结果返回一个列的概率。注意 `evalMLMethod` 的第一个参数是 `AggregateFunctionState` 对象,接下来的参数是列的特性。
We can also set a bound of probability, which assigns elements to different labels.
我们也可以设置概率的范围, 这样需要给元素指定不同的标签。
``` sql
SELECT ans < 1.1 AND ans > 0.5 FROM
@ -1836,14 +1834,14 @@ stochasticLogisticRegression(1.0, 1.0, 10, 'SGD')
evalMLMethod(model, param1, param2) AS ans FROM test_data)
```
Then the result will be labels.
结果是标签。
`test_data` is a table like `train_data` but may not contain target value.
`test_data` 是一个像 `train_data` 一样的表,但是不包含目标值。
**另请参阅**
- [随机指标线上回归](#agg_functions-stochasticlinearregression)
- [线性回归和逻辑回归之间的差异](https://stackoverflow.com/questions/12146914/what-is-the-difference-between-linear-regression-and-logistic-regression)
- [线性回归和逻辑回归之间的差异](https://stackoverflow.com/questions/12146914/what-is-the-difference-between-linear-regression-and-logistic-regression)
## groupBitmapAnd {#groupbitmapand}

View File

@ -469,7 +469,7 @@ private:
const auto & info = infos[i];
json_out << double_quote << connections[i]->getDescription() << ": {\n";
json_out << double_quote << "statistics: {\n";
json_out << double_quote << "statistics" << ": {\n";
print_key_value("QPS", info->queries / info->work_time);
print_key_value("RPS", info->read_rows / info->work_time);
@ -479,7 +479,7 @@ private:
print_key_value("num_queries", info->queries.load(), false);
json_out << "},\n";
json_out << double_quote << "query_time_percentiles: {\n";
json_out << double_quote << "query_time_percentiles" << ": {\n";
for (int percent = 0; percent <= 90; percent += 10)
print_percentile(*info, percent);

View File

@ -1920,7 +1920,11 @@ public:
std::string text = e.displayText();
std::cerr << "Code: " << e.code() << ". " << text << std::endl;
std::cerr << "Table №" << i << std::endl << std::endl;
exit(e.code());
/// Avoid the case when error exit code can possibly overflow to normal (zero).
auto exit_code = e.code() % 256;
if (exit_code == 0)
exit_code = 255;
exit(exit_code);
}
}

View File

@ -25,7 +25,7 @@ void ClusterCopier::init()
task_description_watch_callback = [this] (const Coordination::WatchResponse & response)
{
if (response.error != Coordination::ZOK)
if (response.error != Coordination::Error::ZOK)
return;
UInt64 version = ++task_description_version;
LOG_DEBUG(log, "Task description should be updated, local version {}", version);
@ -206,11 +206,11 @@ void ClusterCopier::uploadTaskDescription(const std::string & task_path, const s
zookeeper->createAncestors(local_task_description_path);
auto code = zookeeper->tryCreate(local_task_description_path, task_config_str, zkutil::CreateMode::Persistent);
if (code && force)
if (code != Coordination::Error::ZOK && force)
zookeeper->createOrUpdate(local_task_description_path, task_config_str, zkutil::CreateMode::Persistent);
LOG_DEBUG(log, "Task description {} uploaded to {} with result {} ({})",
((code && !force) ? "not " : ""), local_task_description_path, code, zookeeper->error2string(code));
((code != Coordination::Error::ZOK && !force) ? "not " : ""), local_task_description_path, code, Coordination::errorMessage(code));
}
void ClusterCopier::reloadTaskDescription()
@ -220,10 +220,10 @@ void ClusterCopier::reloadTaskDescription()
String task_config_str;
Coordination::Stat stat{};
int code;
Coordination::Error code;
zookeeper->tryGetWatch(task_description_path, task_config_str, &stat, task_description_watch_callback, &code);
if (code)
if (code != Coordination::Error::ZOK)
throw Exception("Can't get description node " + task_description_path, ErrorCodes::BAD_ARGUMENTS);
LOG_DEBUG(log, "Loading description, zxid={}", task_description_current_stat.czxid);
@ -376,10 +376,10 @@ zkutil::EphemeralNodeHolder::Ptr ClusterCopier::createTaskWorkerNodeAndWaitIfNee
Coordination::Responses responses;
auto code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::ZOK || code == Coordination::ZNODEEXISTS)
if (code == Coordination::Error::ZOK || code == Coordination::Error::ZNODEEXISTS)
return std::make_shared<zkutil::EphemeralNodeHolder>(current_worker_path, *zookeeper, false, false, description);
if (code == Coordination::ZBADVERSION)
if (code == Coordination::Error::ZBADVERSION)
{
++num_bad_version_errors;
@ -545,7 +545,7 @@ TaskStatus ClusterCopier::tryMoveAllPiecesToDestinationTable(const TaskTable & t
}
catch (const Coordination::Exception & e)
{
if (e.code == Coordination::ZNODEEXISTS)
if (e.code == Coordination::Error::ZNODEEXISTS)
{
LOG_DEBUG(log, "Someone is already moving pieces {}", current_partition_attach_is_active);
return TaskStatus::Active;
@ -745,7 +745,7 @@ bool ClusterCopier::tryDropPartitionPiece(
}
catch (const Coordination::Exception & e)
{
if (e.code == Coordination::ZNODEEXISTS)
if (e.code == Coordination::Error::ZNODEEXISTS)
{
LOG_DEBUG(log, "Partition {} piece {} is cleaning now by somebody, sleep", task_partition.name, toString(current_piece_number));
std::this_thread::sleep_for(default_sleep_time);
@ -778,7 +778,7 @@ bool ClusterCopier::tryDropPartitionPiece(
}
catch (const Coordination::Exception & e)
{
if (e.code == Coordination::ZNODEEXISTS)
if (e.code == Coordination::Error::ZNODEEXISTS)
{
LOG_DEBUG(log, "Partition {} is being filled now by somebody, sleep", task_partition.name);
return false;
@ -795,7 +795,7 @@ bool ClusterCopier::tryDropPartitionPiece(
/// Remove all status nodes
{
Strings children;
if (zookeeper->tryGetChildren(current_shards_path, children) == Coordination::ZOK)
if (zookeeper->tryGetChildren(current_shards_path, children) == Coordination::Error::ZOK)
for (const auto & child : children)
{
zookeeper->removeRecursive(current_shards_path + "/" + child);
@ -845,7 +845,7 @@ bool ClusterCopier::tryDropPartitionPiece(
}
LOG_INFO(log, "Partition {} piece {} was dropped on cluster {}", task_partition.name, toString(current_piece_number), task_table.cluster_push_name);
if (zookeeper->tryCreate(current_shards_path, host_id, zkutil::CreateMode::Persistent) == Coordination::ZNODEEXISTS)
if (zookeeper->tryCreate(current_shards_path, host_id, zkutil::CreateMode::Persistent) == Coordination::Error::ZNODEEXISTS)
zookeeper->set(current_shards_path, host_id);
}
@ -1233,7 +1233,7 @@ TaskStatus ClusterCopier::processPartitionPieceTaskImpl(
}
catch (const Coordination::Exception & e)
{
if (e.code == Coordination::ZNODEEXISTS)
if (e.code == Coordination::Error::ZNODEEXISTS)
{
LOG_DEBUG(log, "Someone is already processing {}", current_task_piece_is_active_path);
return TaskStatus::Active;
@ -1271,9 +1271,9 @@ TaskStatus ClusterCopier::processPartitionPieceTaskImpl(
{
String state_finished = TaskStateWithOwner::getData(TaskState::Finished, host_id);
auto res = zookeeper->tryCreate(current_task_piece_status_path, state_finished, zkutil::CreateMode::Persistent);
if (res == Coordination::ZNODEEXISTS)
if (res == Coordination::Error::ZNODEEXISTS)
LOG_DEBUG(log, "Partition {} piece {} is absent on current replica of a shard. But other replicas have already marked it as done.", task_partition.name, current_piece_number);
if (res == Coordination::ZOK)
if (res == Coordination::Error::ZOK)
LOG_DEBUG(log, "Partition {} piece {} is absent on current replica of a shard. Will mark it as done. Other replicas will do the same.", task_partition.name, current_piece_number);
return TaskStatus::Finished;
}
@ -1429,7 +1429,7 @@ TaskStatus ClusterCopier::processPartitionPieceTaskImpl(
{
Coordination::ExistsResponse status = future_is_dirty_checker.get();
if (status.error != Coordination::ZNONODE)
if (status.error != Coordination::Error::ZNONODE)
{
LogicalClock dirt_discovery_epoch (status.stat.mzxid);
if (dirt_discovery_epoch == clean_state_clock.discovery_zxid)

View File

@ -178,7 +178,7 @@ public:
[stale = stale] (const Coordination::WatchResponse & rsp)
{
auto logger = &Poco::Logger::get("ClusterCopier");
if (rsp.error == Coordination::ZOK)
if (rsp.error == Coordination::Error::ZOK)
{
switch (rsp.type)
{

View File

@ -23,7 +23,7 @@ namespace ProfileEvents
namespace Coordination
{
Exception::Exception(const std::string & msg, const int32_t code_, int)
Exception::Exception(const std::string & msg, const Error code_, int)
: DB::Exception(msg, DB::ErrorCodes::KEEPER_EXCEPTION), code(code_)
{
if (Coordination::isUserError(code))
@ -34,17 +34,17 @@ Exception::Exception(const std::string & msg, const int32_t code_, int)
ProfileEvents::increment(ProfileEvents::ZooKeeperOtherExceptions);
}
Exception::Exception(const std::string & msg, const int32_t code_)
Exception::Exception(const std::string & msg, const Error code_)
: Exception(msg + " (" + errorMessage(code_) + ")", code_, 0)
{
}
Exception::Exception(const int32_t code_)
Exception::Exception(const Error code_)
: Exception(errorMessage(code_), code_, 0)
{
}
Exception::Exception(const int32_t code_, const std::string & path)
Exception::Exception(const Error code_, const std::string & path)
: Exception(std::string{errorMessage(code_)} + ", path: " + path, code_, 0)
{
}
@ -58,10 +58,10 @@ using namespace DB;
static void addRootPath(String & path, const String & root_path)
{
if (path.empty())
throw Exception("Path cannot be empty", ZBADARGUMENTS);
throw Exception("Path cannot be empty", Error::ZBADARGUMENTS);
if (path[0] != '/')
throw Exception("Path must begin with /", ZBADARGUMENTS);
throw Exception("Path must begin with /", Error::ZBADARGUMENTS);
if (root_path.empty())
return;
@ -78,64 +78,62 @@ static void removeRootPath(String & path, const String & root_path)
return;
if (path.size() <= root_path.size())
throw Exception("Received path is not longer than root_path", ZDATAINCONSISTENCY);
throw Exception("Received path is not longer than root_path", Error::ZDATAINCONSISTENCY);
path = path.substr(root_path.size());
}
const char * errorMessage(int32_t code)
const char * errorMessage(Error code)
{
switch (code)
{
case ZOK: return "Ok";
case ZSYSTEMERROR: return "System error";
case ZRUNTIMEINCONSISTENCY: return "Run time inconsistency";
case ZDATAINCONSISTENCY: return "Data inconsistency";
case ZCONNECTIONLOSS: return "Connection loss";
case ZMARSHALLINGERROR: return "Marshalling error";
case ZUNIMPLEMENTED: return "Unimplemented";
case ZOPERATIONTIMEOUT: return "Operation timeout";
case ZBADARGUMENTS: return "Bad arguments";
case ZINVALIDSTATE: return "Invalid zhandle state";
case ZAPIERROR: return "API error";
case ZNONODE: return "No node";
case ZNOAUTH: return "Not authenticated";
case ZBADVERSION: return "Bad version";
case ZNOCHILDRENFOREPHEMERALS: return "No children for ephemerals";
case ZNODEEXISTS: return "Node exists";
case ZNOTEMPTY: return "Not empty";
case ZSESSIONEXPIRED: return "Session expired";
case ZINVALIDCALLBACK: return "Invalid callback";
case ZINVALIDACL: return "Invalid ACL";
case ZAUTHFAILED: return "Authentication failed";
case ZCLOSING: return "ZooKeeper is closing";
case ZNOTHING: return "(not error) no server responses to process";
case ZSESSIONMOVED: return "Session moved to another server, so operation is ignored";
case Error::ZOK: return "Ok";
case Error::ZSYSTEMERROR: return "System error";
case Error::ZRUNTIMEINCONSISTENCY: return "Run time inconsistency";
case Error::ZDATAINCONSISTENCY: return "Data inconsistency";
case Error::ZCONNECTIONLOSS: return "Connection loss";
case Error::ZMARSHALLINGERROR: return "Marshalling error";
case Error::ZUNIMPLEMENTED: return "Unimplemented";
case Error::ZOPERATIONTIMEOUT: return "Operation timeout";
case Error::ZBADARGUMENTS: return "Bad arguments";
case Error::ZINVALIDSTATE: return "Invalid zhandle state";
case Error::ZAPIERROR: return "API error";
case Error::ZNONODE: return "No node";
case Error::ZNOAUTH: return "Not authenticated";
case Error::ZBADVERSION: return "Bad version";
case Error::ZNOCHILDRENFOREPHEMERALS: return "No children for ephemerals";
case Error::ZNODEEXISTS: return "Node exists";
case Error::ZNOTEMPTY: return "Not empty";
case Error::ZSESSIONEXPIRED: return "Session expired";
case Error::ZINVALIDCALLBACK: return "Invalid callback";
case Error::ZINVALIDACL: return "Invalid ACL";
case Error::ZAUTHFAILED: return "Authentication failed";
case Error::ZCLOSING: return "ZooKeeper is closing";
case Error::ZNOTHING: return "(not error) no server responses to process";
case Error::ZSESSIONMOVED: return "Session moved to another server, so operation is ignored";
}
if (code > 0)
return strerror(code);
return "unknown error";
__builtin_unreachable();
}
bool isHardwareError(int32_t zk_return_code)
bool isHardwareError(Error zk_return_code)
{
return zk_return_code == ZINVALIDSTATE
|| zk_return_code == ZSESSIONEXPIRED
|| zk_return_code == ZSESSIONMOVED
|| zk_return_code == ZCONNECTIONLOSS
|| zk_return_code == ZMARSHALLINGERROR
|| zk_return_code == ZOPERATIONTIMEOUT;
return zk_return_code == Error::ZINVALIDSTATE
|| zk_return_code == Error::ZSESSIONEXPIRED
|| zk_return_code == Error::ZSESSIONMOVED
|| zk_return_code == Error::ZCONNECTIONLOSS
|| zk_return_code == Error::ZMARSHALLINGERROR
|| zk_return_code == Error::ZOPERATIONTIMEOUT;
}
bool isUserError(int32_t zk_return_code)
bool isUserError(Error zk_return_code)
{
return zk_return_code == ZNONODE
|| zk_return_code == ZBADVERSION
|| zk_return_code == ZNOCHILDRENFOREPHEMERALS
|| zk_return_code == ZNODEEXISTS
|| zk_return_code == ZNOTEMPTY;
return zk_return_code == Error::ZNONODE
|| zk_return_code == Error::ZBADVERSION
|| zk_return_code == Error::ZNOCHILDRENFOREPHEMERALS
|| zk_return_code == Error::ZNODEEXISTS
|| zk_return_code == Error::ZNOTEMPTY;
}

View File

@ -53,6 +53,57 @@ struct Stat
int64_t pzxid;
};
enum class Error : int32_t
{
ZOK = 0,
/** System and server-side errors.
* This is never thrown by the server, it shouldn't be used other than
* to indicate a range. Specifically error codes greater than this
* value, but lesser than ZAPIERROR, are system errors.
*/
ZSYSTEMERROR = -1,
ZRUNTIMEINCONSISTENCY = -2, /// A runtime inconsistency was found
ZDATAINCONSISTENCY = -3, /// A data inconsistency was found
ZCONNECTIONLOSS = -4, /// Connection to the server has been lost
ZMARSHALLINGERROR = -5, /// Error while marshalling or unmarshalling data
ZUNIMPLEMENTED = -6, /// Operation is unimplemented
ZOPERATIONTIMEOUT = -7, /// Operation timeout
ZBADARGUMENTS = -8, /// Invalid arguments
ZINVALIDSTATE = -9, /// Invliad zhandle state
/** API errors.
* This is never thrown by the server, it shouldn't be used other than
* to indicate a range. Specifically error codes greater than this
* value are API errors.
*/
ZAPIERROR = -100,
ZNONODE = -101, /// Node does not exist
ZNOAUTH = -102, /// Not authenticated
ZBADVERSION = -103, /// Version conflict
ZNOCHILDRENFOREPHEMERALS = -108, /// Ephemeral nodes may not have children
ZNODEEXISTS = -110, /// The node already exists
ZNOTEMPTY = -111, /// The node has children
ZSESSIONEXPIRED = -112, /// The session has been expired by the server
ZINVALIDCALLBACK = -113, /// Invalid callback specified
ZINVALIDACL = -114, /// Invalid ACL specified
ZAUTHFAILED = -115, /// Client authentication failed
ZCLOSING = -116, /// ZooKeeper is closing
ZNOTHING = -117, /// (not error) no server responses to process
ZSESSIONMOVED = -118 /// Session moved to another server, so operation is ignored
};
/// Network errors and similar. You should reinitialize ZooKeeper session in case of these errors
bool isHardwareError(Error code);
/// Valid errors sent from the server about database state (like "no node"). Logical and authentication errors (like "bad arguments") are not here.
bool isUserError(Error code);
const char * errorMessage(Error code);
struct Request;
using RequestPtr = std::shared_ptr<Request>;
using Requests = std::vector<RequestPtr>;
@ -74,7 +125,7 @@ using ResponseCallback = std::function<void(const Response &)>;
struct Response
{
int32_t error = 0;
Error error = Error::ZOK;
Response() = default;
Response(const Response &) = default;
Response & operator=(const Response &) = default;
@ -225,56 +276,6 @@ using CheckCallback = std::function<void(const CheckResponse &)>;
using MultiCallback = std::function<void(const MultiResponse &)>;
enum Error
{
ZOK = 0,
/** System and server-side errors.
* This is never thrown by the server, it shouldn't be used other than
* to indicate a range. Specifically error codes greater than this
* value, but lesser than ZAPIERROR, are system errors.
*/
ZSYSTEMERROR = -1,
ZRUNTIMEINCONSISTENCY = -2, /// A runtime inconsistency was found
ZDATAINCONSISTENCY = -3, /// A data inconsistency was found
ZCONNECTIONLOSS = -4, /// Connection to the server has been lost
ZMARSHALLINGERROR = -5, /// Error while marshalling or unmarshalling data
ZUNIMPLEMENTED = -6, /// Operation is unimplemented
ZOPERATIONTIMEOUT = -7, /// Operation timeout
ZBADARGUMENTS = -8, /// Invalid arguments
ZINVALIDSTATE = -9, /// Invliad zhandle state
/** API errors.
* This is never thrown by the server, it shouldn't be used other than
* to indicate a range. Specifically error codes greater than this
* value are API errors.
*/
ZAPIERROR = -100,
ZNONODE = -101, /// Node does not exist
ZNOAUTH = -102, /// Not authenticated
ZBADVERSION = -103, /// Version conflict
ZNOCHILDRENFOREPHEMERALS = -108, /// Ephemeral nodes may not have children
ZNODEEXISTS = -110, /// The node already exists
ZNOTEMPTY = -111, /// The node has children
ZSESSIONEXPIRED = -112, /// The session has been expired by the server
ZINVALIDCALLBACK = -113, /// Invalid callback specified
ZINVALIDACL = -114, /// Invalid ACL specified
ZAUTHFAILED = -115, /// Client authentication failed
ZCLOSING = -116, /// ZooKeeper is closing
ZNOTHING = -117, /// (not error) no server responses to process
ZSESSIONMOVED = -118 /// Session moved to another server, so operation is ignored
};
/// Network errors and similar. You should reinitialize ZooKeeper session in case of these errors
bool isHardwareError(int32_t code);
/// Valid errors sent from the server about database state (like "no node"). Logical and authentication errors (like "bad arguments") are not here.
bool isUserError(int32_t code);
const char * errorMessage(int32_t code);
/// For watches.
enum State
{
@ -301,19 +302,19 @@ class Exception : public DB::Exception
{
private:
/// Delegate constructor, used to minimize repetition; last parameter used for overload resolution.
Exception(const std::string & msg, const int32_t code_, int);
Exception(const std::string & msg, const Error code_, int);
public:
explicit Exception(const int32_t code_);
Exception(const std::string & msg, const int32_t code_);
Exception(const int32_t code_, const std::string & path);
explicit Exception(const Error code_);
Exception(const std::string & msg, const Error code_);
Exception(const Error code_, const std::string & path);
Exception(const Exception & exc);
const char * name() const throw() override { return "Coordination::Exception"; }
const char * className() const throw() override { return "Coordination::Exception"; }
Exception * clone() const override { return new Exception(*this); }
const int32_t code;
const Error code;
};

View File

@ -29,11 +29,11 @@ public:
if (zookeeper->tryGet(path, result_str, &stat))
{
result = std::stol(result_str) + 1;
success = zookeeper->trySet(path, std::to_string(result), stat.version) == Coordination::ZOK;
success = zookeeper->trySet(path, std::to_string(result), stat.version) == Coordination::Error::ZOK;
}
else
{
success = zookeeper->tryCreate(path, std::to_string(result), zkutil::CreateMode::Persistent) == Coordination::ZOK;
success = zookeeper->tryCreate(path, std::to_string(result), zkutil::CreateMode::Persistent) == Coordination::Error::ZOK;
}
}
while (!success);

View File

@ -21,12 +21,12 @@ public:
/// If it is user error throws KeeperMultiException else throws ordinary KeeperException
/// If it is ZOK does nothing
static void check(int32_t code, const Coordination::Requests & requests, const Coordination::Responses & responses);
static void check(Coordination::Error code, const Coordination::Requests & requests, const Coordination::Responses & responses);
KeeperMultiException(int32_t code, const Coordination::Requests & requests, const Coordination::Responses & responses);
KeeperMultiException(Coordination::Error code, const Coordination::Requests & requests, const Coordination::Responses & responses);
private:
static size_t getFailedOpIndex(int32_t code, const Coordination::Responses & responses);
static size_t getFailedOpIndex(Coordination::Error code, const Coordination::Responses & responses);
};
}

View File

@ -36,7 +36,12 @@ public:
* It means that different participants of leader election have different identifiers
* and existence of more than one ephemeral node with same identifier indicates an error.
*/
LeaderElection(DB::BackgroundSchedulePool & pool_, const std::string & path_, ZooKeeper & zookeeper_, LeadershipHandler handler_, const std::string & identifier_ = "")
LeaderElection(
DB::BackgroundSchedulePool & pool_,
const std::string & path_,
ZooKeeper & zookeeper_,
LeadershipHandler handler_,
const std::string & identifier_)
: pool(pool_), path(path_), zookeeper(zookeeper_), handler(handler_), identifier(identifier_)
, log_name("LeaderElection (" + path + ")")
, log(&Poco::Logger::get(log_name))
@ -121,7 +126,7 @@ private:
{
DB::tryLogCurrentException(log);
if (e.code == Coordination::ZSESSIONEXPIRED)
if (e.code == Coordination::Error::ZSESSIONEXPIRED)
return;
}
catch (...)

View File

@ -16,13 +16,13 @@ bool Lock::tryLock()
else
{
std::string dummy;
int32_t code = zookeeper->tryCreate(lock_path, lock_message, zkutil::CreateMode::Ephemeral, dummy);
Coordination::Error code = zookeeper->tryCreate(lock_path, lock_message, zkutil::CreateMode::Ephemeral, dummy);
if (code == Coordination::ZNODEEXISTS)
if (code == Coordination::Error::ZNODEEXISTS)
{
locked.reset();
}
else if (code == Coordination::ZOK)
else if (code == Coordination::Error::ZOK)
{
locked = std::make_unique<ZooKeeperHandler>(zookeeper);
}

View File

@ -158,7 +158,7 @@ struct TestKeeperMultiRequest final : MultiRequest, TestKeeperRequest
requests.push_back(std::make_shared<TestKeeperCheckRequest>(*concrete_request_check));
}
else
throw Exception("Illegal command as part of multi ZooKeeper request", ZBADARGUMENTS);
throw Exception("Illegal command as part of multi ZooKeeper request", Error::ZBADARGUMENTS);
}
}
@ -338,7 +338,7 @@ ResponsePtr TestKeeperListRequest::process(TestKeeper::Container & container, in
{
auto path_prefix = path;
if (path_prefix.empty())
throw Exception("Logical error: path cannot be empty", ZSESSIONEXPIRED);
throw Exception("Logical error: path cannot be empty", Error::ZSESSIONEXPIRED);
if (path_prefix.back() != '/')
path_prefix += '/';
@ -514,7 +514,7 @@ void TestKeeper::finalize()
WatchResponse response;
response.type = SESSION;
response.state = EXPIRED_SESSION;
response.error = ZSESSIONEXPIRED;
response.error = Error::ZSESSIONEXPIRED;
for (auto & callback : path_watch.second)
{
@ -541,7 +541,7 @@ void TestKeeper::finalize()
if (info.callback)
{
ResponsePtr response = info.request->createResponse();
response->error = ZSESSIONEXPIRED;
response->error = Error::ZSESSIONEXPIRED;
try
{
info.callback(*response);
@ -556,7 +556,7 @@ void TestKeeper::finalize()
WatchResponse response;
response.type = SESSION;
response.state = EXPIRED_SESSION;
response.error = ZSESSIONEXPIRED;
response.error = Error::ZSESSIONEXPIRED;
try
{
info.watch(response);
@ -587,10 +587,10 @@ void TestKeeper::pushRequest(RequestInfo && request)
std::lock_guard lock(push_request_mutex);
if (expired)
throw Exception("Session expired", ZSESSIONEXPIRED);
throw Exception("Session expired", Error::ZSESSIONEXPIRED);
if (!requests_queue.tryPush(std::move(request), operation_timeout.totalMilliseconds()))
throw Exception("Cannot push request to queue within operation timeout", ZOPERATIONTIMEOUT);
throw Exception("Cannot push request to queue within operation timeout", Error::ZOPERATIONTIMEOUT);
}
catch (...)
{

View File

@ -38,9 +38,9 @@ const int CreateMode::PersistentSequential = 2;
const int CreateMode::EphemeralSequential = 3;
static void check(int32_t code, const std::string & path)
static void check(Coordination::Error code, const std::string & path)
{
if (code)
if (code != Coordination::Error::ZOK)
throw KeeperException(code, path);
}
@ -59,7 +59,7 @@ void ZooKeeper::init(const std::string & implementation_, const std::string & ho
if (implementation == "zookeeper")
{
if (hosts.empty())
throw KeeperException("No hosts passed to ZooKeeper constructor.", Coordination::ZBADARGUMENTS);
throw KeeperException("No hosts passed to ZooKeeper constructor.", Coordination::Error::ZBADARGUMENTS);
std::vector<std::string> hosts_strings;
splitInto<','>(hosts_strings, hosts);
@ -84,7 +84,7 @@ void ZooKeeper::init(const std::string & implementation_, const std::string & ho
}
if (nodes.empty())
throw KeeperException("Cannot use any of provided ZooKeeper nodes", Coordination::ZBADARGUMENTS);
throw KeeperException("Cannot use any of provided ZooKeeper nodes", Coordination::Error::ZBADARGUMENTS);
impl = std::make_unique<Coordination::ZooKeeper>(
nodes,
@ -112,7 +112,7 @@ void ZooKeeper::init(const std::string & implementation_, const std::string & ho
}
if (!chroot.empty() && !exists("/"))
throw KeeperException("Zookeeper root doesn't exist. You should create root node " + chroot + " before start.", Coordination::ZNONODE);
throw KeeperException("Zookeeper root doesn't exist. You should create root node " + chroot + " before start.", Coordination::Error::ZNONODE);
}
ZooKeeper::ZooKeeper(const std::string & hosts_, const std::string & identity_, int32_t session_timeout_ms_,
@ -164,7 +164,7 @@ struct ZooKeeperArgs
implementation = config.getString(config_name + "." + key);
}
else
throw KeeperException(std::string("Unknown key ") + key + " in config file", Coordination::ZBADARGUMENTS);
throw KeeperException(std::string("Unknown key ") + key + " in config file", Coordination::Error::ZBADARGUMENTS);
}
/// Shuffle the hosts to distribute the load among ZooKeeper nodes.
@ -182,7 +182,7 @@ struct ZooKeeperArgs
if (!chroot.empty())
{
if (chroot.front() != '/')
throw KeeperException(std::string("Root path in config file should start with '/', but got ") + chroot, Coordination::ZBADARGUMENTS);
throw KeeperException(std::string("Root path in config file should start with '/', but got ") + chroot, Coordination::Error::ZBADARGUMENTS);
if (chroot.back() == '/')
chroot.pop_back();
}
@ -211,17 +211,17 @@ static Coordination::WatchCallback callbackForEvent(const EventPtr & watch)
}
int32_t ZooKeeper::getChildrenImpl(const std::string & path, Strings & res,
Coordination::Error ZooKeeper::getChildrenImpl(const std::string & path, Strings & res,
Coordination::Stat * stat,
Coordination::WatchCallback watch_callback)
{
int32_t code = 0;
Coordination::Error code = Coordination::Error::ZOK;
Poco::Event event;
auto callback = [&](const Coordination::ListResponse & response)
{
code = response.error;
if (!code)
if (code == Coordination::Error::ZOK)
{
res = response.names;
if (stat)
@ -251,37 +251,37 @@ Strings ZooKeeper::getChildrenWatch(
return res;
}
int32_t ZooKeeper::tryGetChildren(const std::string & path, Strings & res,
Coordination::Error ZooKeeper::tryGetChildren(const std::string & path, Strings & res,
Coordination::Stat * stat, const EventPtr & watch)
{
int32_t code = getChildrenImpl(path, res, stat, callbackForEvent(watch));
Coordination::Error code = getChildrenImpl(path, res, stat, callbackForEvent(watch));
if (!(code == Coordination::ZOK || code == Coordination::ZNONODE))
if (!(code == Coordination::Error::ZOK || code == Coordination::Error::ZNONODE))
throw KeeperException(code, path);
return code;
}
int32_t ZooKeeper::tryGetChildrenWatch(const std::string & path, Strings & res,
Coordination::Error ZooKeeper::tryGetChildrenWatch(const std::string & path, Strings & res,
Coordination::Stat * stat, Coordination::WatchCallback watch_callback)
{
int32_t code = getChildrenImpl(path, res, stat, watch_callback);
Coordination::Error code = getChildrenImpl(path, res, stat, watch_callback);
if (!(code == Coordination::ZOK || code == Coordination::ZNONODE))
if (!(code == Coordination::Error::ZOK || code == Coordination::Error::ZNONODE))
throw KeeperException(code, path);
return code;
}
int32_t ZooKeeper::createImpl(const std::string & path, const std::string & data, int32_t mode, std::string & path_created)
Coordination::Error ZooKeeper::createImpl(const std::string & path, const std::string & data, int32_t mode, std::string & path_created)
{
int32_t code = 0;
Coordination::Error code = Coordination::Error::ZOK;
Poco::Event event;
auto callback = [&](const Coordination::CreateResponse & response)
{
code = response.error;
if (!code)
if (code == Coordination::Error::ZOK)
path_created = response.path_created;
event.set();
};
@ -298,20 +298,20 @@ std::string ZooKeeper::create(const std::string & path, const std::string & data
return path_created;
}
int32_t ZooKeeper::tryCreate(const std::string & path, const std::string & data, int32_t mode, std::string & path_created)
Coordination::Error ZooKeeper::tryCreate(const std::string & path, const std::string & data, int32_t mode, std::string & path_created)
{
int32_t code = createImpl(path, data, mode, path_created);
Coordination::Error code = createImpl(path, data, mode, path_created);
if (!(code == Coordination::ZOK ||
code == Coordination::ZNONODE ||
code == Coordination::ZNODEEXISTS ||
code == Coordination::ZNOCHILDRENFOREPHEMERALS))
if (!(code == Coordination::Error::ZOK ||
code == Coordination::Error::ZNONODE ||
code == Coordination::Error::ZNODEEXISTS ||
code == Coordination::Error::ZNOCHILDRENFOREPHEMERALS))
throw KeeperException(code, path);
return code;
}
int32_t ZooKeeper::tryCreate(const std::string & path, const std::string & data, int32_t mode)
Coordination::Error ZooKeeper::tryCreate(const std::string & path, const std::string & data, int32_t mode)
{
std::string path_created;
return tryCreate(path, data, mode, path_created);
@ -320,9 +320,9 @@ int32_t ZooKeeper::tryCreate(const std::string & path, const std::string & data,
void ZooKeeper::createIfNotExists(const std::string & path, const std::string & data)
{
std::string path_created;
int32_t code = createImpl(path, data, CreateMode::Persistent, path_created);
Coordination::Error code = createImpl(path, data, CreateMode::Persistent, path_created);
if (code == Coordination::ZOK || code == Coordination::ZNODEEXISTS)
if (code == Coordination::Error::ZOK || code == Coordination::Error::ZNODEEXISTS)
return;
else
throw KeeperException(code, path);
@ -341,14 +341,14 @@ void ZooKeeper::createAncestors(const std::string & path)
}
}
int32_t ZooKeeper::removeImpl(const std::string & path, int32_t version)
Coordination::Error ZooKeeper::removeImpl(const std::string & path, int32_t version)
{
int32_t code = 0;
Coordination::Error code = Coordination::Error::ZOK;
Poco::Event event;
auto callback = [&](const Coordination::RemoveResponse & response)
{
if (response.error)
if (response.error != Coordination::Error::ZOK)
code = response.error;
event.set();
};
@ -363,26 +363,26 @@ void ZooKeeper::remove(const std::string & path, int32_t version)
check(tryRemove(path, version), path);
}
int32_t ZooKeeper::tryRemove(const std::string & path, int32_t version)
Coordination::Error ZooKeeper::tryRemove(const std::string & path, int32_t version)
{
int32_t code = removeImpl(path, version);
if (!(code == Coordination::ZOK ||
code == Coordination::ZNONODE ||
code == Coordination::ZBADVERSION ||
code == Coordination::ZNOTEMPTY))
Coordination::Error code = removeImpl(path, version);
if (!(code == Coordination::Error::ZOK ||
code == Coordination::Error::ZNONODE ||
code == Coordination::Error::ZBADVERSION ||
code == Coordination::Error::ZNOTEMPTY))
throw KeeperException(code, path);
return code;
}
int32_t ZooKeeper::existsImpl(const std::string & path, Coordination::Stat * stat, Coordination::WatchCallback watch_callback)
Coordination::Error ZooKeeper::existsImpl(const std::string & path, Coordination::Stat * stat, Coordination::WatchCallback watch_callback)
{
int32_t code = 0;
Coordination::Error code = Coordination::Error::ZOK;
Poco::Event event;
auto callback = [&](const Coordination::ExistsResponse & response)
{
code = response.error;
if (!code && stat)
if (code == Coordination::Error::ZOK && stat)
*stat = response.stat;
event.set();
};
@ -399,22 +399,22 @@ bool ZooKeeper::exists(const std::string & path, Coordination::Stat * stat, cons
bool ZooKeeper::existsWatch(const std::string & path, Coordination::Stat * stat, Coordination::WatchCallback watch_callback)
{
int32_t code = existsImpl(path, stat, watch_callback);
Coordination::Error code = existsImpl(path, stat, watch_callback);
if (!(code == Coordination::ZOK || code == Coordination::ZNONODE))
if (!(code == Coordination::Error::ZOK || code == Coordination::Error::ZNONODE))
throw KeeperException(code, path);
return code != Coordination::ZNONODE;
return code != Coordination::Error::ZNONODE;
}
int32_t ZooKeeper::getImpl(const std::string & path, std::string & res, Coordination::Stat * stat, Coordination::WatchCallback watch_callback)
Coordination::Error ZooKeeper::getImpl(const std::string & path, std::string & res, Coordination::Stat * stat, Coordination::WatchCallback watch_callback)
{
int32_t code = 0;
Coordination::Error code = Coordination::Error::ZOK;
Poco::Event event;
auto callback = [&](const Coordination::GetResponse & response)
{
code = response.error;
if (!code)
if (code == Coordination::Error::ZOK)
{
res = response.data;
if (stat)
@ -431,7 +431,7 @@ int32_t ZooKeeper::getImpl(const std::string & path, std::string & res, Coordina
std::string ZooKeeper::get(const std::string & path, Coordination::Stat * stat, const EventPtr & watch)
{
int32_t code = 0;
Coordination::Error code = Coordination::Error::ZOK;
std::string res;
if (tryGet(path, res, stat, watch, &code))
return res;
@ -441,7 +441,7 @@ std::string ZooKeeper::get(const std::string & path, Coordination::Stat * stat,
std::string ZooKeeper::getWatch(const std::string & path, Coordination::Stat * stat, Coordination::WatchCallback watch_callback)
{
int32_t code = 0;
Coordination::Error code = Coordination::Error::ZOK;
std::string res;
if (tryGetWatch(path, res, stat, watch_callback, &code))
return res;
@ -449,34 +449,44 @@ std::string ZooKeeper::getWatch(const std::string & path, Coordination::Stat * s
throw KeeperException("Can't get data for node " + path + ": node doesn't exist", code);
}
bool ZooKeeper::tryGet(const std::string & path, std::string & res, Coordination::Stat * stat, const EventPtr & watch, int * return_code)
bool ZooKeeper::tryGet(
const std::string & path,
std::string & res,
Coordination::Stat * stat,
const EventPtr & watch,
Coordination::Error * return_code)
{
return tryGetWatch(path, res, stat, callbackForEvent(watch), return_code);
}
bool ZooKeeper::tryGetWatch(const std::string & path, std::string & res, Coordination::Stat * stat, Coordination::WatchCallback watch_callback, int * return_code)
bool ZooKeeper::tryGetWatch(
const std::string & path,
std::string & res,
Coordination::Stat * stat,
Coordination::WatchCallback watch_callback,
Coordination::Error * return_code)
{
int32_t code = getImpl(path, res, stat, watch_callback);
Coordination::Error code = getImpl(path, res, stat, watch_callback);
if (!(code == Coordination::ZOK || code == Coordination::ZNONODE))
if (!(code == Coordination::Error::ZOK || code == Coordination::Error::ZNONODE))
throw KeeperException(code, path);
if (return_code)
*return_code = code;
return code == Coordination::ZOK;
return code == Coordination::Error::ZOK;
}
int32_t ZooKeeper::setImpl(const std::string & path, const std::string & data,
Coordination::Error ZooKeeper::setImpl(const std::string & path, const std::string & data,
int32_t version, Coordination::Stat * stat)
{
int32_t code = 0;
Coordination::Error code = Coordination::Error::ZOK;
Poco::Event event;
auto callback = [&](const Coordination::SetResponse & response)
{
code = response.error;
if (!code && stat)
if (code == Coordination::Error::ZOK && stat)
*stat = response.stat;
event.set();
};
@ -493,34 +503,34 @@ void ZooKeeper::set(const std::string & path, const std::string & data, int32_t
void ZooKeeper::createOrUpdate(const std::string & path, const std::string & data, int32_t mode)
{
int32_t code = trySet(path, data, -1);
if (code == Coordination::ZNONODE)
Coordination::Error code = trySet(path, data, -1);
if (code == Coordination::Error::ZNONODE)
{
create(path, data, mode);
}
else if (code != Coordination::ZOK)
else if (code != Coordination::Error::ZOK)
throw KeeperException(code, path);
}
int32_t ZooKeeper::trySet(const std::string & path, const std::string & data,
Coordination::Error ZooKeeper::trySet(const std::string & path, const std::string & data,
int32_t version, Coordination::Stat * stat)
{
int32_t code = setImpl(path, data, version, stat);
Coordination::Error code = setImpl(path, data, version, stat);
if (!(code == Coordination::ZOK ||
code == Coordination::ZNONODE ||
code == Coordination::ZBADVERSION))
if (!(code == Coordination::Error::ZOK ||
code == Coordination::Error::ZNONODE ||
code == Coordination::Error::ZBADVERSION))
throw KeeperException(code, path);
return code;
}
int32_t ZooKeeper::multiImpl(const Coordination::Requests & requests, Coordination::Responses & responses)
Coordination::Error ZooKeeper::multiImpl(const Coordination::Requests & requests, Coordination::Responses & responses)
{
if (requests.empty())
return Coordination::ZOK;
return Coordination::Error::ZOK;
int32_t code = 0;
Coordination::Error code = Coordination::Error::ZOK;
Poco::Event event;
auto callback = [&](const Coordination::MultiResponse & response)
@ -538,15 +548,15 @@ int32_t ZooKeeper::multiImpl(const Coordination::Requests & requests, Coordinati
Coordination::Responses ZooKeeper::multi(const Coordination::Requests & requests)
{
Coordination::Responses responses;
int32_t code = multiImpl(requests, responses);
Coordination::Error code = multiImpl(requests, responses);
KeeperMultiException::check(code, requests, responses);
return responses;
}
int32_t ZooKeeper::tryMulti(const Coordination::Requests & requests, Coordination::Responses & responses)
Coordination::Error ZooKeeper::tryMulti(const Coordination::Requests & requests, Coordination::Responses & responses)
{
int32_t code = multiImpl(requests, responses);
if (code && !Coordination::isUserError(code))
Coordination::Error code = multiImpl(requests, responses);
if (code != Coordination::Error::ZOK && !Coordination::isUserError(code))
throw KeeperException(code);
return code;
}
@ -587,7 +597,7 @@ void ZooKeeper::removeChildrenRecursive(const std::string & path)
void ZooKeeper::tryRemoveChildrenRecursive(const std::string & path)
{
Strings children;
if (tryGetChildren(path, children) != Coordination::ZOK)
if (tryGetChildren(path, children) != Coordination::Error::ZOK)
return;
while (!children.empty())
{
@ -609,7 +619,7 @@ void ZooKeeper::tryRemoveChildrenRecursive(const std::string & path)
/// this means someone is concurrently removing these children and we will have
/// to remove them one by one.
Coordination::Responses responses;
if (tryMulti(ops, responses) != Coordination::ZOK)
if (tryMulti(ops, responses) != Coordination::Error::ZOK)
for (const std::string & child : batch)
tryRemove(child);
}
@ -645,7 +655,7 @@ bool ZooKeeper::waitForDisappear(const std::string & path, const WaitCondition &
auto callback = [state](const Coordination::ExistsResponse & response)
{
state->code = response.error;
state->code = int32_t(response.error);
if (state->code)
state->event.set();
};
@ -654,7 +664,7 @@ bool ZooKeeper::waitForDisappear(const std::string & path, const WaitCondition &
{
if (!state->code)
{
state->code = response.error;
state->code = int32_t(response.error);
if (!state->code)
state->event_type = response.type;
state->event.set();
@ -670,11 +680,11 @@ bool ZooKeeper::waitForDisappear(const std::string & path, const WaitCondition &
else if (!state->event.tryWait(1000))
continue;
if (state->code == Coordination::ZNONODE)
if (state->code == int32_t(Coordination::Error::ZNONODE))
return true;
if (state->code)
throw KeeperException(state->code, path);
throw KeeperException(static_cast<Coordination::Error>(state->code.load(std::memory_order_seq_cst)), path);
if (state->event_type == Coordination::DELETED)
return true;
@ -688,11 +698,6 @@ ZooKeeperPtr ZooKeeper::startNewSession() const
}
std::string ZooKeeper::error2string(int32_t code)
{
return Coordination::errorMessage(code);
}
bool ZooKeeper::expired()
{
return impl->isExpired();
@ -712,7 +717,7 @@ std::future<Coordination::CreateResponse> ZooKeeper::asyncCreate(const std::stri
auto callback = [promise, path](const Coordination::CreateResponse & response) mutable
{
if (response.error)
if (response.error != Coordination::Error::ZOK)
promise->set_exception(std::make_exception_ptr(KeeperException(path, response.error)));
else
promise->set_value(response);
@ -730,7 +735,7 @@ std::future<Coordination::GetResponse> ZooKeeper::asyncGet(const std::string & p
auto callback = [promise, path](const Coordination::GetResponse & response) mutable
{
if (response.error)
if (response.error != Coordination::Error::ZOK)
promise->set_exception(std::make_exception_ptr(KeeperException(path, response.error)));
else
promise->set_value(response);
@ -748,7 +753,7 @@ std::future<Coordination::GetResponse> ZooKeeper::asyncTryGet(const std::string
auto callback = [promise, path](const Coordination::GetResponse & response) mutable
{
if (response.error && response.error != Coordination::ZNONODE)
if (response.error != Coordination::Error::ZOK && response.error != Coordination::Error::ZNONODE)
promise->set_exception(std::make_exception_ptr(KeeperException(path, response.error)));
else
promise->set_value(response);
@ -765,7 +770,7 @@ std::future<Coordination::ExistsResponse> ZooKeeper::asyncExists(const std::stri
auto callback = [promise, path](const Coordination::ExistsResponse & response) mutable
{
if (response.error && response.error != Coordination::ZNONODE)
if (response.error != Coordination::Error::ZOK && response.error != Coordination::Error::ZNONODE)
promise->set_exception(std::make_exception_ptr(KeeperException(path, response.error)));
else
promise->set_value(response);
@ -782,7 +787,7 @@ std::future<Coordination::SetResponse> ZooKeeper::asyncSet(const std::string & p
auto callback = [promise, path](const Coordination::SetResponse & response) mutable
{
if (response.error)
if (response.error != Coordination::Error::ZOK)
promise->set_exception(std::make_exception_ptr(KeeperException(path, response.error)));
else
promise->set_value(response);
@ -799,7 +804,7 @@ std::future<Coordination::ListResponse> ZooKeeper::asyncGetChildren(const std::s
auto callback = [promise, path](const Coordination::ListResponse & response) mutable
{
if (response.error)
if (response.error != Coordination::Error::ZOK)
promise->set_exception(std::make_exception_ptr(KeeperException(path, response.error)));
else
promise->set_value(response);
@ -816,7 +821,7 @@ std::future<Coordination::RemoveResponse> ZooKeeper::asyncRemove(const std::stri
auto callback = [promise, path](const Coordination::RemoveResponse & response) mutable
{
if (response.error)
if (response.error != Coordination::Error::ZOK)
promise->set_exception(std::make_exception_ptr(KeeperException(path, response.error)));
else
promise->set_value(response);
@ -833,8 +838,13 @@ std::future<Coordination::RemoveResponse> ZooKeeper::asyncTryRemove(const std::s
auto callback = [promise, path](const Coordination::RemoveResponse & response) mutable
{
if (response.error && response.error != Coordination::ZNONODE && response.error != Coordination::ZBADVERSION && response.error != Coordination::ZNOTEMPTY)
if (response.error != Coordination::Error::ZOK
&& response.error != Coordination::Error::ZNONODE
&& response.error != Coordination::Error::ZBADVERSION
&& response.error != Coordination::Error::ZNOTEMPTY)
{
promise->set_exception(std::make_exception_ptr(KeeperException(path, response.error)));
}
else
promise->set_value(response);
};
@ -864,7 +874,7 @@ std::future<Coordination::MultiResponse> ZooKeeper::asyncMulti(const Coordinatio
auto callback = [promise](const Coordination::MultiResponse & response) mutable
{
if (response.error)
if (response.error != Coordination::Error::ZOK)
promise->set_exception(std::make_exception_ptr(KeeperException(response.error)));
else
promise->set_value(response);
@ -874,7 +884,7 @@ std::future<Coordination::MultiResponse> ZooKeeper::asyncMulti(const Coordinatio
return future;
}
int32_t ZooKeeper::tryMultiNoThrow(const Coordination::Requests & requests, Coordination::Responses & responses)
Coordination::Error ZooKeeper::tryMultiNoThrow(const Coordination::Requests & requests, Coordination::Responses & responses)
{
try
{
@ -887,24 +897,24 @@ int32_t ZooKeeper::tryMultiNoThrow(const Coordination::Requests & requests, Coor
}
size_t KeeperMultiException::getFailedOpIndex(int32_t exception_code, const Coordination::Responses & responses)
size_t KeeperMultiException::getFailedOpIndex(Coordination::Error exception_code, const Coordination::Responses & responses)
{
if (responses.empty())
throw DB::Exception("Responses for multi transaction is empty", DB::ErrorCodes::LOGICAL_ERROR);
for (size_t index = 0, size = responses.size(); index < size; ++index)
if (responses[index]->error)
if (responses[index]->error != Coordination::Error::ZOK)
return index;
if (!Coordination::isUserError(exception_code))
throw DB::Exception("There are no failed OPs because '" + ZooKeeper::error2string(exception_code) + "' is not valid response code for that",
throw DB::Exception("There are no failed OPs because '" + std::string(Coordination::errorMessage(exception_code)) + "' is not valid response code for that",
DB::ErrorCodes::LOGICAL_ERROR);
throw DB::Exception("There is no failed OpResult", DB::ErrorCodes::LOGICAL_ERROR);
}
KeeperMultiException::KeeperMultiException(int32_t exception_code, const Coordination::Requests & requests_, const Coordination::Responses & responses_)
KeeperMultiException::KeeperMultiException(Coordination::Error exception_code, const Coordination::Requests & requests_, const Coordination::Responses & responses_)
: KeeperException("Transaction failed", exception_code),
requests(requests_), responses(responses_), failed_op_index(getFailedOpIndex(exception_code, responses))
{
@ -917,9 +927,10 @@ std::string KeeperMultiException::getPathForFirstFailedOp() const
return requests[failed_op_index]->getPath();
}
void KeeperMultiException::check(int32_t exception_code, const Coordination::Requests & requests, const Coordination::Responses & responses)
void KeeperMultiException::check(
Coordination::Error exception_code, const Coordination::Requests & requests, const Coordination::Responses & responses)
{
if (!exception_code)
if (exception_code == Coordination::Error::ZOK)
return;
if (Coordination::isUserError(exception_code))

View File

@ -99,8 +99,8 @@ public:
/// * The parent is ephemeral.
/// * The node already exists.
/// In case of other errors throws an exception.
int32_t tryCreate(const std::string & path, const std::string & data, int32_t mode, std::string & path_created);
int32_t tryCreate(const std::string & path, const std::string & data, int32_t mode);
Coordination::Error tryCreate(const std::string & path, const std::string & data, int32_t mode, std::string & path_created);
Coordination::Error tryCreate(const std::string & path, const std::string & data, int32_t mode);
/// Create a Persistent node.
/// Does nothing if the node already exists.
@ -117,7 +117,7 @@ public:
/// * The node doesn't exist
/// * Versions don't match
/// * The node has children.
int32_t tryRemove(const std::string & path, int32_t version = -1);
Coordination::Error tryRemove(const std::string & path, int32_t version = -1);
bool exists(const std::string & path, Coordination::Stat * stat = nullptr, const EventPtr & watch = nullptr);
bool existsWatch(const std::string & path, Coordination::Stat * stat, Coordination::WatchCallback watch_callback);
@ -127,9 +127,11 @@ public:
/// Doesn't not throw in the following cases:
/// * The node doesn't exist. Returns false in this case.
bool tryGet(const std::string & path, std::string & res, Coordination::Stat * stat = nullptr, const EventPtr & watch = nullptr, int * code = nullptr);
bool tryGet(const std::string & path, std::string & res, Coordination::Stat * stat = nullptr, const EventPtr & watch = nullptr,
Coordination::Error * code = nullptr);
bool tryGetWatch(const std::string & path, std::string & res, Coordination::Stat * stat, Coordination::WatchCallback watch_callback, int * code = nullptr);
bool tryGetWatch(const std::string & path, std::string & res, Coordination::Stat * stat, Coordination::WatchCallback watch_callback,
Coordination::Error * code = nullptr);
void set(const std::string & path, const std::string & data,
int32_t version = -1, Coordination::Stat * stat = nullptr);
@ -140,7 +142,7 @@ public:
/// Doesn't not throw in the following cases:
/// * The node doesn't exist.
/// * Versions do not match.
int32_t trySet(const std::string & path, const std::string & data,
Coordination::Error trySet(const std::string & path, const std::string & data,
int32_t version = -1, Coordination::Stat * stat = nullptr);
Strings getChildren(const std::string & path,
@ -153,11 +155,11 @@ public:
/// Doesn't not throw in the following cases:
/// * The node doesn't exist.
int32_t tryGetChildren(const std::string & path, Strings & res,
Coordination::Error tryGetChildren(const std::string & path, Strings & res,
Coordination::Stat * stat = nullptr,
const EventPtr & watch = nullptr);
int32_t tryGetChildrenWatch(const std::string & path, Strings & res,
Coordination::Error tryGetChildrenWatch(const std::string & path, Strings & res,
Coordination::Stat * stat,
Coordination::WatchCallback watch_callback);
@ -166,9 +168,9 @@ public:
Coordination::Responses multi(const Coordination::Requests & requests);
/// Throws only if some operation has returned an "unexpected" error
/// - an error that would cause the corresponding try- method to throw.
int32_t tryMulti(const Coordination::Requests & requests, Coordination::Responses & responses);
Coordination::Error tryMulti(const Coordination::Requests & requests, Coordination::Responses & responses);
/// Throws nothing (even session expired errors)
int32_t tryMultiNoThrow(const Coordination::Requests & requests, Coordination::Responses & responses);
Coordination::Error tryMultiNoThrow(const Coordination::Requests & requests, Coordination::Responses & responses);
Int64 getClientID();
@ -238,8 +240,6 @@ public:
/// Like the previous one but don't throw any exceptions on future.get()
FutureMulti tryAsyncMulti(const Coordination::Requests & ops);
static std::string error2string(int32_t code);
private:
friend class EphemeralNodeHolder;
@ -250,13 +250,15 @@ private:
void tryRemoveChildrenRecursive(const std::string & path);
/// The following methods don't throw exceptions but return error codes.
int32_t createImpl(const std::string & path, const std::string & data, int32_t mode, std::string & path_created);
int32_t removeImpl(const std::string & path, int32_t version);
int32_t getImpl(const std::string & path, std::string & res, Coordination::Stat * stat, Coordination::WatchCallback watch_callback);
int32_t setImpl(const std::string & path, const std::string & data, int32_t version, Coordination::Stat * stat);
int32_t getChildrenImpl(const std::string & path, Strings & res, Coordination::Stat * stat, Coordination::WatchCallback watch_callback);
int32_t multiImpl(const Coordination::Requests & requests, Coordination::Responses & responses);
int32_t existsImpl(const std::string & path, Coordination::Stat * stat_, Coordination::WatchCallback watch_callback);
Coordination::Error createImpl(const std::string & path, const std::string & data, int32_t mode, std::string & path_created);
Coordination::Error removeImpl(const std::string & path, int32_t version);
Coordination::Error getImpl(
const std::string & path, std::string & res, Coordination::Stat * stat, Coordination::WatchCallback watch_callback);
Coordination::Error setImpl(const std::string & path, const std::string & data, int32_t version, Coordination::Stat * stat);
Coordination::Error getChildrenImpl(
const std::string & path, Strings & res, Coordination::Stat * stat, Coordination::WatchCallback watch_callback);
Coordination::Error multiImpl(const Coordination::Requests & requests, Coordination::Responses & responses);
Coordination::Error existsImpl(const std::string & path, Coordination::Stat * stat_, Coordination::WatchCallback watch_callback);
std::unique_ptr<Coordination::IKeeper> impl;

View File

@ -52,13 +52,6 @@ namespace CurrentMetrics
extern const Metric ZooKeeperWatch;
}
namespace DB
{
namespace ErrorCodes
{
extern const int SUPPORT_IS_DISABLED;
}
}
/** ZooKeeper wire protocol.
@ -335,6 +328,13 @@ static void read(int32_t & x, ReadBuffer & in)
x = __builtin_bswap32(x);
}
static void read(Error & x, ReadBuffer & in)
{
int32_t code;
read(code, in);
x = Error(code);
}
static void read(bool & x, ReadBuffer & in)
{
readBinary(x, in);
@ -353,10 +353,10 @@ static void read(String & s, ReadBuffer & in)
}
if (size < 0)
throw Exception("Negative size while reading string from ZooKeeper", ZMARSHALLINGERROR);
throw Exception("Negative size while reading string from ZooKeeper", Error::ZMARSHALLINGERROR);
if (size > MAX_STRING_OR_ARRAY_SIZE)
throw Exception("Too large string size while reading from ZooKeeper", ZMARSHALLINGERROR);
throw Exception("Too large string size while reading from ZooKeeper", Error::ZMARSHALLINGERROR);
s.resize(size);
in.read(s.data(), size);
@ -367,7 +367,7 @@ template <size_t N> void read(std::array<char, N> & s, ReadBuffer & in)
int32_t size = 0;
read(size, in);
if (size != N)
throw Exception("Unexpected array size while reading from ZooKeeper", ZMARSHALLINGERROR);
throw Exception("Unexpected array size while reading from ZooKeeper", Error::ZMARSHALLINGERROR);
in.read(s.data(), N);
}
@ -391,9 +391,9 @@ template <typename T> void read(std::vector<T> & arr, ReadBuffer & in)
int32_t size = 0;
read(size, in);
if (size < 0)
throw Exception("Negative size while reading array from ZooKeeper", ZMARSHALLINGERROR);
throw Exception("Negative size while reading array from ZooKeeper", Error::ZMARSHALLINGERROR);
if (size > MAX_STRING_OR_ARRAY_SIZE)
throw Exception("Too large array size while reading from ZooKeeper", ZMARSHALLINGERROR);
throw Exception("Too large array size while reading from ZooKeeper", Error::ZMARSHALLINGERROR);
arr.resize(size);
for (auto & elem : arr)
read(elem, in);
@ -489,7 +489,7 @@ struct ZooKeeperCloseResponse final : ZooKeeperResponse
{
void readImpl(ReadBuffer &) override
{
throw Exception("Received response for close request", ZRUNTIMEINCONSISTENCY);
throw Exception("Received response for close request", Error::ZRUNTIMEINCONSISTENCY);
}
};
@ -650,12 +650,12 @@ struct ZooKeeperErrorResponse final : ErrorResponse, ZooKeeperResponse
{
void readImpl(ReadBuffer & in) override
{
int32_t read_error;
Coordination::Error read_error;
Coordination::read(read_error, in);
if (read_error != error)
throw Exception("Error code in ErrorResponse (" + toString(read_error) + ") doesn't match error code in header (" + toString(error) + ")",
ZMARSHALLINGERROR);
throw Exception(fmt::format("Error code in ErrorResponse ({}) doesn't match error code in header ({})", read_error, error),
Error::ZMARSHALLINGERROR);
}
};
@ -691,7 +691,7 @@ struct ZooKeeperMultiRequest final : MultiRequest, ZooKeeperRequest
requests.push_back(std::make_shared<ZooKeeperCheckRequest>(*concrete_request_check));
}
else
throw Exception("Illegal command as part of multi ZooKeeper request", ZBADARGUMENTS);
throw Exception("Illegal command as part of multi ZooKeeper request", Error::ZBADARGUMENTS);
}
}
@ -739,14 +739,14 @@ struct ZooKeeperMultiResponse final : MultiResponse, ZooKeeperResponse
{
ZooKeeper::OpNum op_num;
bool done;
int32_t op_error;
Error op_error;
Coordination::read(op_num, in);
Coordination::read(done, in);
Coordination::read(op_error, in);
if (done)
throw Exception("Not enough results received for multi transaction", ZMARSHALLINGERROR);
throw Exception("Not enough results received for multi transaction", Error::ZMARSHALLINGERROR);
/// op_num == -1 is special for multi transaction.
/// For unknown reason, error code is duplicated in header and in response body.
@ -754,18 +754,18 @@ struct ZooKeeperMultiResponse final : MultiResponse, ZooKeeperResponse
if (op_num == -1)
response = std::make_shared<ZooKeeperErrorResponse>();
if (op_error)
if (op_error != Error::ZOK)
{
response->error = op_error;
/// Set error for whole transaction.
/// If some operations fail, ZK send global error as zero and then send details about each operation.
/// It will set error code for first failed operation and it will set special "runtime inconsistency" code for other operations.
if (!error && op_error != ZRUNTIMEINCONSISTENCY)
if (error == Error::ZOK && op_error != Error::ZRUNTIMEINCONSISTENCY)
error = op_error;
}
if (!op_error || op_num == -1)
if (op_error == Error::ZOK || op_num == -1)
dynamic_cast<ZooKeeperResponse &>(*response).readImpl(in);
}
@ -780,11 +780,11 @@ struct ZooKeeperMultiResponse final : MultiResponse, ZooKeeperResponse
Coordination::read(error_read, in);
if (!done)
throw Exception("Too many results received for multi transaction", ZMARSHALLINGERROR);
throw Exception("Too many results received for multi transaction", Error::ZMARSHALLINGERROR);
if (op_num != -1)
throw Exception("Unexpected op_num received at the end of results for multi transaction", ZMARSHALLINGERROR);
throw Exception("Unexpected op_num received at the end of results for multi transaction", Error::ZMARSHALLINGERROR);
if (error_read != -1)
throw Exception("Unexpected error value received at the end of results for multi transaction", ZMARSHALLINGERROR);
throw Exception("Unexpected error value received at the end of results for multi transaction", Error::ZMARSHALLINGERROR);
}
}
};
@ -883,7 +883,7 @@ void ZooKeeper::connect(
Poco::Timespan connection_timeout)
{
if (nodes.empty())
throw Exception("No nodes passed to ZooKeeper constructor", ZBADARGUMENTS);
throw Exception("No nodes passed to ZooKeeper constructor", Error::ZBADARGUMENTS);
static constexpr size_t num_tries = 3;
bool connected = false;
@ -901,7 +901,8 @@ void ZooKeeper::connect(
#if USE_SSL
socket = Poco::Net::SecureStreamSocket();
#else
throw Exception{"Communication with ZooKeeper over SSL is disabled because poco library was built without NetSSL support.", ErrorCodes::SUPPORT_IS_DISABLED};
throw Poco::Exception(
"Communication with ZooKeeper over SSL is disabled because poco library was built without NetSSL support.");
#endif
}
else
@ -970,7 +971,7 @@ void ZooKeeper::connect(
}
message << fail_reasons.str() << "\n";
throw Exception(message.str(), ZCONNECTIONLOSS);
throw Exception(message.str(), Error::ZCONNECTIONLOSS);
}
}
@ -1005,11 +1006,11 @@ void ZooKeeper::receiveHandshake()
read(handshake_length);
if (handshake_length != 36)
throw Exception("Unexpected handshake length received: " + toString(handshake_length), ZMARSHALLINGERROR);
throw Exception("Unexpected handshake length received: " + toString(handshake_length), Error::ZMARSHALLINGERROR);
read(protocol_version_read);
if (protocol_version_read != protocol_version)
throw Exception("Unexpected protocol version: " + toString(protocol_version_read), ZMARSHALLINGERROR);
throw Exception("Unexpected protocol version: " + toString(protocol_version_read), Error::ZMARSHALLINGERROR);
read(timeout);
if (timeout != session_timeout.totalMilliseconds())
@ -1032,7 +1033,7 @@ void ZooKeeper::sendAuth(const String & scheme, const String & data)
int32_t length;
XID read_xid;
int64_t zxid;
int32_t err;
Error err;
read(length);
size_t count_before_event = in->count();
@ -1042,16 +1043,16 @@ void ZooKeeper::sendAuth(const String & scheme, const String & data)
if (read_xid != auth_xid)
throw Exception("Unexpected event received in reply to auth request: " + toString(read_xid),
ZMARSHALLINGERROR);
Error::ZMARSHALLINGERROR);
int32_t actual_length = in->count() - count_before_event;
if (length != actual_length)
throw Exception("Response length doesn't match. Expected: " + toString(length) + ", actual: " + toString(actual_length),
ZMARSHALLINGERROR);
Error::ZMARSHALLINGERROR);
if (err)
throw Exception("Error received in reply to auth request. Code: " + toString(err) + ". Message: " + String(errorMessage(err)),
ZMARSHALLINGERROR);
if (err != Error::ZOK)
throw Exception("Error received in reply to auth request. Code: " + toString(int32_t(err)) + ". Message: " + String(errorMessage(err)),
Error::ZMARSHALLINGERROR);
}
@ -1154,7 +1155,7 @@ void ZooKeeper::receiveThread()
earliest_operation = operations.begin()->second;
auto earliest_operation_deadline = earliest_operation->time + std::chrono::microseconds(operation_timeout.totalMicroseconds());
if (now > earliest_operation_deadline)
throw Exception("Operation timeout (deadline already expired) for path: " + earliest_operation->request->getPath(), ZOPERATIONTIMEOUT);
throw Exception("Operation timeout (deadline already expired) for path: " + earliest_operation->request->getPath(), Error::ZOPERATIONTIMEOUT);
max_wait = std::chrono::duration_cast<std::chrono::microseconds>(earliest_operation_deadline - now).count();
}
}
@ -1170,10 +1171,10 @@ void ZooKeeper::receiveThread()
else
{
if (earliest_operation)
throw Exception("Operation timeout (no response) for path: " + earliest_operation->request->getPath(), ZOPERATIONTIMEOUT);
throw Exception("Operation timeout (no response) for path: " + earliest_operation->request->getPath(), Error::ZOPERATIONTIMEOUT);
waited += max_wait;
if (waited >= session_timeout.totalMicroseconds())
throw Exception("Nothing is received in session timeout", ZOPERATIONTIMEOUT);
throw Exception("Nothing is received in session timeout", Error::ZOPERATIONTIMEOUT);
}
@ -1193,7 +1194,7 @@ void ZooKeeper::receiveEvent()
int32_t length;
XID xid;
int64_t zxid;
int32_t err;
Error err;
read(length);
size_t count_before_event = in->count();
@ -1206,8 +1207,8 @@ void ZooKeeper::receiveEvent()
if (xid == ping_xid)
{
if (err)
throw Exception("Received error in heartbeat response: " + String(errorMessage(err)), ZRUNTIMEINCONSISTENCY);
if (err != Error::ZOK)
throw Exception("Received error in heartbeat response: " + String(errorMessage(err)), Error::ZRUNTIMEINCONSISTENCY);
response = std::make_shared<ZooKeeperHeartbeatResponse>();
}
@ -1252,7 +1253,7 @@ void ZooKeeper::receiveEvent()
auto it = operations.find(xid);
if (it == operations.end())
throw Exception("Received response for unknown xid", ZRUNTIMEINCONSISTENCY);
throw Exception("Received response for unknown xid", Error::ZRUNTIMEINCONSISTENCY);
/// After this point, we must invoke callback, that we've grabbed from 'operations'.
/// Invariant: all callbacks are invoked either in case of success or in case of error.
@ -1272,7 +1273,7 @@ void ZooKeeper::receiveEvent()
if (!response)
response = request_info.request->makeResponse();
if (err)
if (err != Error::ZOK)
response->error = err;
else
{
@ -1282,7 +1283,7 @@ void ZooKeeper::receiveEvent()
int32_t actual_length = in->count() - count_before_event;
if (length != actual_length)
throw Exception("Response length doesn't match. Expected: " + toString(length) + ", actual: " + toString(actual_length), ZMARSHALLINGERROR);
throw Exception("Response length doesn't match. Expected: " + toString(length) + ", actual: " + toString(actual_length), Error::ZMARSHALLINGERROR);
}
catch (...)
{
@ -1294,7 +1295,7 @@ void ZooKeeper::receiveEvent()
/// In case we cannot read the response, we should indicate it as the error of that type
/// when the user cannot assume whether the request was processed or not.
response->error = ZCONNECTIONLOSS;
response->error = Error::ZCONNECTIONLOSS;
if (request_info.callback)
request_info.callback(*response);
@ -1361,8 +1362,8 @@ void ZooKeeper::finalize(bool error_send, bool error_receive)
ResponsePtr response = request_info.request->makeResponse();
response->error = request_info.request->probably_sent
? ZCONNECTIONLOSS
: ZSESSIONEXPIRED;
? Error::ZCONNECTIONLOSS
: Error::ZSESSIONEXPIRED;
if (request_info.callback)
{
@ -1390,7 +1391,7 @@ void ZooKeeper::finalize(bool error_send, bool error_receive)
WatchResponse response;
response.type = SESSION;
response.state = EXPIRED_SESSION;
response.error = ZSESSIONEXPIRED;
response.error = Error::ZSESSIONEXPIRED;
for (auto & callback : path_watches.second)
{
@ -1421,7 +1422,7 @@ void ZooKeeper::finalize(bool error_send, bool error_receive)
ResponsePtr response = info.request->makeResponse();
if (response)
{
response->error = ZSESSIONEXPIRED;
response->error = Error::ZSESSIONEXPIRED;
try
{
info.callback(*response);
@ -1437,7 +1438,7 @@ void ZooKeeper::finalize(bool error_send, bool error_receive)
WatchResponse response;
response.type = SESSION;
response.state = EXPIRED_SESSION;
response.error = ZSESSIONEXPIRED;
response.error = Error::ZSESSIONEXPIRED;
try
{
info.watch(response);
@ -1466,9 +1467,9 @@ void ZooKeeper::pushRequest(RequestInfo && info)
{
info.request->xid = next_xid.fetch_add(1);
if (info.request->xid == close_xid)
throw Exception("xid equal to close_xid", ZSESSIONEXPIRED);
throw Exception("xid equal to close_xid", Error::ZSESSIONEXPIRED);
if (info.request->xid < 0)
throw Exception("XID overflow", ZSESSIONEXPIRED);
throw Exception("XID overflow", Error::ZSESSIONEXPIRED);
}
/// We must serialize 'pushRequest' and 'finalize' (from sendThread, receiveThread) calls
@ -1478,10 +1479,10 @@ void ZooKeeper::pushRequest(RequestInfo && info)
std::lock_guard lock(push_request_mutex);
if (expired)
throw Exception("Session expired", ZSESSIONEXPIRED);
throw Exception("Session expired", Error::ZSESSIONEXPIRED);
if (!requests_queue.tryPush(std::move(info), operation_timeout.totalMilliseconds()))
throw Exception("Cannot push request to queue within operation timeout", ZOPERATIONTIMEOUT);
throw Exception("Cannot push request to queue within operation timeout", Error::ZOPERATIONTIMEOUT);
}
catch (...)
{
@ -1651,7 +1652,7 @@ void ZooKeeper::close()
request_info.request = std::make_shared<ZooKeeperCloseRequest>(std::move(request));
if (!requests_queue.tryPush(std::move(request_info), operation_timeout.totalMilliseconds()))
throw Exception("Cannot push close request to queue within operation timeout", ZOPERATIONTIMEOUT);
throw Exception("Cannot push close request to queue within operation timeout", Error::ZOPERATIONTIMEOUT);
ProfileEvents::increment(ProfileEvents::ZooKeeperClose);
}

View File

@ -86,7 +86,7 @@ TEST(zkutil, MultiAsync)
ops.clear();
auto res = fut.get();
ASSERT_EQ(res.error, Coordination::ZOK);
ASSERT_EQ(res.error, Coordination::Error::ZOK);
ASSERT_EQ(res.responses.size(), 2);
}
@ -126,15 +126,15 @@ TEST(zkutil, MultiAsync)
/// The test is quite heavy. It is normal if session is expired during this test.
/// If we don't check that, the test will be flacky.
if (res.error != Coordination::ZSESSIONEXPIRED && res.error != Coordination::ZCONNECTIONLOSS)
if (res.error != Coordination::Error::ZSESSIONEXPIRED && res.error != Coordination::Error::ZCONNECTIONLOSS)
{
ASSERT_EQ(res.error, Coordination::ZNODEEXISTS);
ASSERT_EQ(res.error, Coordination::Error::ZNODEEXISTS);
ASSERT_EQ(res.responses.size(), 2);
}
}
catch (const Coordination::Exception & e)
{
if (e.code != Coordination::ZSESSIONEXPIRED && e.code != Coordination::ZCONNECTIONLOSS)
if (e.code != Coordination::Error::ZSESSIONEXPIRED && e.code != Coordination::Error::ZCONNECTIONLOSS)
throw;
}
}

View File

@ -39,12 +39,12 @@ int main(int argc, char ** argv)
ops.emplace_back(zkutil::makeRemoveRequest("/test/zk_expiration_test", -1));
Coordination::Responses responses;
int32_t code = zk.tryMultiNoThrow(ops, responses);
Coordination::Error code = zk.tryMultiNoThrow(ops, responses);
std::cout << time(nullptr) - time0 << "s: " << zkutil::ZooKeeper::error2string(code) << std::endl;
std::cout << time(nullptr) - time0 << "s: " << Coordination::errorMessage(code) << std::endl;
try
{
if (code)
if (code != Coordination::Error::ZOK)
std::cout << "Path: " << zkutil::KeeperMultiException(code, ops, responses).getPathForFirstFailedOp() << std::endl;
}
catch (...)

View File

@ -49,8 +49,8 @@ try
zk.create("/test", "old", false, false, {},
[&](const CreateResponse & response)
{
if (response.error)
std::cerr << "Error (create) " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Error (create): " << errorMessage(response.error) << '\n';
else
std::cerr << "Created path: " << response.path_created << '\n';
@ -64,8 +64,8 @@ try
zk.get("/test",
[&](const GetResponse & response)
{
if (response.error)
std::cerr << "Error (get) " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Error (get): " << errorMessage(response.error) << '\n';
else
std::cerr << "Value: " << response.data << '\n';
@ -73,8 +73,8 @@ try
},
[](const WatchResponse & response)
{
if (response.error)
std::cerr << "Watch (get) on /test, Error " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Watch (get) on /test, Error: " << errorMessage(response.error) << '\n';
else
std::cerr << "Watch (get) on /test, path: " << response.path << ", type: " << response.type << '\n';
});
@ -86,8 +86,8 @@ try
zk.set("/test", "new", -1,
[&](const SetResponse & response)
{
if (response.error)
std::cerr << "Error (set) " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Error (set): " << errorMessage(response.error) << '\n';
else
std::cerr << "Set\n";
@ -101,8 +101,8 @@ try
zk.list("/",
[&](const ListResponse & response)
{
if (response.error)
std::cerr << "Error (list) " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Error (list): " << errorMessage(response.error) << '\n';
else
{
std::cerr << "Children:\n";
@ -114,8 +114,8 @@ try
},
[](const WatchResponse & response)
{
if (response.error)
std::cerr << "Watch (list) on /, Error " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Watch (list) on /, Error: " << errorMessage(response.error) << '\n';
else
std::cerr << "Watch (list) on /, path: " << response.path << ", type: " << response.type << '\n';
});
@ -127,8 +127,8 @@ try
zk.exists("/test",
[&](const ExistsResponse & response)
{
if (response.error)
std::cerr << "Error (exists) " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Error (exists): " << errorMessage(response.error) << '\n';
else
std::cerr << "Exists\n";
@ -136,8 +136,8 @@ try
},
[](const WatchResponse & response)
{
if (response.error)
std::cerr << "Watch (exists) on /test, Error " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Watch (exists) on /test, Error: " << errorMessage(response.error) << '\n';
else
std::cerr << "Watch (exists) on /test, path: " << response.path << ", type: " << response.type << '\n';
});
@ -148,8 +148,8 @@ try
zk.remove("/test", -1, [&](const RemoveResponse & response)
{
if (response.error)
std::cerr << "Error (remove) " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Error (remove): " << errorMessage(response.error) << '\n';
else
std::cerr << "Removed\n";
@ -184,13 +184,13 @@ try
zk.multi(ops, [&](const MultiResponse & response)
{
if (response.error)
std::cerr << "Error (multi) " << response.error << ": " << errorMessage(response.error) << '\n';
if (response.error != Coordination::Error::ZOK)
std::cerr << "Error (multi): " << errorMessage(response.error) << '\n';
else
{
for (const auto & elem : response.responses)
if (elem->error)
std::cerr << "Error (elem) " << elem->error << ": " << errorMessage(elem->error) << '\n';
if (elem->error != Coordination::Error::ZOK)
std::cerr << "Error (elem): " << errorMessage(elem->error) << '\n';
std::cerr << "Created path: " << dynamic_cast<const CreateResponse &>(*response.responses[0]).path_created << '\n';
}

View File

@ -9,8 +9,8 @@ try
zookeeper.create("/test", "hello", false, false, {}, [](const Coordination::CreateResponse & response)
{
if (response.error)
std::cerr << "Error " << response.error << ": " << Coordination::errorMessage(response.error) << "\n";
if (response.error != Coordination::Error::ZOK)
std::cerr << "Error: " << Coordination::errorMessage(response.error) << "\n";
else
std::cerr << "Path created: " << response.path_created << "\n";
});

View File

@ -14,11 +14,6 @@ static bool operator==(const IDataType & left, const IDataType & right)
return left.equals(right);
}
std::ostream & operator<<(std::ostream & ostr, const IDataType & dt)
{
return ostr << dt.getName();
}
}
using namespace DB;

View File

@ -1,127 +0,0 @@
#include <Columns/ColumnString.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnConst.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/Regexps.h>
#include <memory>
#include <string>
#include <vector>
namespace DB
{
namespace ErrorCodes
{
extern const int ARGUMENT_OUT_OF_BOUND;
extern const int BAD_ARGUMENTS;
}
/** Match all groups of given input string with given re, return array of arrays of matches.
*
* SELECT extractAllGroups('abc=111, def=222, ghi=333', '("[^"]+"|\\w+)=("[^"]+"|\\w+)')
* should produce:
* [['abc', '111'], ['def', '222'], ['ghi', '333']]
*/
class FunctionExtractAllGroups : public IFunction
{
public:
static constexpr auto name = "extractAllGroups";
static FunctionPtr create(const Context &) { return std::make_shared<FunctionExtractAllGroups>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 2; }
bool useDefaultImplementationForConstants() const override { return false; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
FunctionArgumentDescriptors args{
{"haystack", isStringOrFixedString, nullptr, "const String or const FixedString"},
{"needle", isStringOrFixedString, isColumnConst, "const String or const FixedString"},
};
validateFunctionArgumentTypes(*this, arguments, args);
/// Two-dimensional array of strings, each `row` of top array represents matching groups.
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()));
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
{
const ColumnPtr column_haystack = block.getByPosition(arguments[0]).column;
const ColumnPtr column_needle = block.getByPosition(arguments[1]).column;
const auto needle = typeid_cast<const ColumnConst &>(*column_needle).getValue<String>();
if (needle.empty())
throw Exception(getName() + " length of 'needle' argument must be greater than 0.", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
const auto regexp = Regexps::get<false, false>(needle);
const auto & re2 = regexp->getRE2();
if (!re2)
throw Exception("There is no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS);
const size_t groups_count = re2->NumberOfCapturingGroups();
if (!groups_count)
throw Exception("There is no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS);
// Including 0-group, which is the whole regexp.
PODArrayWithStackMemory<re2_st::StringPiece, 128> matched_groups(groups_count + 1);
ColumnArray::ColumnOffsets::MutablePtr root_offsets_col = ColumnArray::ColumnOffsets::create();
ColumnArray::ColumnOffsets::MutablePtr nested_offsets_col = ColumnArray::ColumnOffsets::create();
ColumnString::MutablePtr data_col = ColumnString::create();
auto & root_offsets_data = root_offsets_col->getData();
auto & nested_offsets_data = nested_offsets_col->getData();
root_offsets_data.resize(input_rows_count);
ColumnArray::Offset current_root_offset = 0;
ColumnArray::Offset current_nested_offset = 0;
for (size_t i = 0; i < input_rows_count; ++i)
{
StringRef current_row = column_haystack->getDataAt(i);
// Extract all non-intersecting matches from haystack except group #0.
const auto * pos = current_row.data;
const auto * end = pos + current_row.size;
while (pos < end
&& re2->Match(re2_st::StringPiece(pos, end - pos),
0, end - pos, re2_st::RE2::UNANCHORED, matched_groups.data(), matched_groups.size()))
{
// 1 is to exclude group #0 which is whole re match.
for (size_t group = 1; group <= groups_count; ++group)
data_col->insertData(matched_groups[group].data(), matched_groups[group].size());
pos = matched_groups[0].data() + matched_groups[0].size();
current_nested_offset += groups_count;
nested_offsets_data.push_back(current_nested_offset);
++current_root_offset;
}
root_offsets_data[i] = current_root_offset;
}
ColumnArray::MutablePtr nested_array_col = ColumnArray::create(std::move(data_col), std::move(nested_offsets_col));
ColumnArray::MutablePtr root_array_col = ColumnArray::create(std::move(nested_array_col), std::move(root_offsets_col));
block.getByPosition(result).column = std::move(root_array_col);
}
};
void registerFunctionExtractAllGroups(FunctionFactory & factory)
{
factory.registerFunction<FunctionExtractAllGroups>();
}
}

View File

@ -0,0 +1,238 @@
#include <Columns/ColumnArray.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunctionImpl.h>
#include <Functions/Regexps.h>
#include <memory>
#include <string>
#include <vector>
#include <Core/iostream_debug_helpers.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
enum class ExtractAllGroupsResultKind
{
VERTICAL,
HORIZONTAL
};
/** Match all groups of given input string with given re, return array of arrays of matches.
*
* Depending on `Impl::Kind`, result is either grouped by grop id (Horizontal) or in order of appearance (Vertical):
*
* SELECT extractAllGroupsVertical('abc=111, def=222, ghi=333', '("[^"]+"|\\w+)=("[^"]+"|\\w+)')
* =>
* [['abc', '111'], ['def', '222'], ['ghi', '333']]
*
* SELECT extractAllGroupsHorizontal('abc=111, def=222, ghi=333', '("[^"]+"|\\w+)=("[^"]+"|\\w+)')
* =>
* [['abc', 'def', 'ghi'], ['111', '222', '333']
*/
template <typename Impl>
class FunctionExtractAllGroups : public IFunction
{
public:
static constexpr auto Kind = Impl::Kind;
static constexpr auto name = Impl::Name;
static FunctionPtr create(const Context &) { return std::make_shared<FunctionExtractAllGroups>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 2; }
bool useDefaultImplementationForConstants() const override { return false; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
FunctionArgumentDescriptors args{
{"haystack", isStringOrFixedString, nullptr, "const String or const FixedString"},
{"needle", isStringOrFixedString, isColumnConst, "const String or const FixedString"},
};
validateFunctionArgumentTypes(*this, arguments, args);
/// Two-dimensional array of strings, each `row` of top array represents matching groups.
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()));
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
{
static const auto MAX_GROUPS_COUNT = 128;
const ColumnPtr column_haystack = block.getByPosition(arguments[0]).column;
const ColumnPtr column_needle = block.getByPosition(arguments[1]).column;
const auto needle = typeid_cast<const ColumnConst &>(*column_needle).getValue<String>();
if (needle.empty())
throw Exception("Length of 'needle' argument must be greater than 0.", ErrorCodes::BAD_ARGUMENTS);
using StringPiece = typename Regexps::Regexp::StringPieceType;
const auto & regexp = Regexps::get<false, false>(needle)->getRE2();
if (!regexp)
throw Exception("There are no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS);
const size_t groups_count = regexp->NumberOfCapturingGroups();
if (!groups_count)
throw Exception("There are no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS);
if (groups_count > MAX_GROUPS_COUNT - 1)
throw Exception("Too many groups in regexp: " + std::to_string(groups_count)
+ ", max: " + std::to_string(MAX_GROUPS_COUNT - 1),
ErrorCodes::BAD_ARGUMENTS);
// Including 0-group, which is the whole regexp.
PODArrayWithStackMemory<StringPiece, MAX_GROUPS_COUNT> matched_groups(groups_count + 1);
ColumnArray::ColumnOffsets::MutablePtr root_offsets_col = ColumnArray::ColumnOffsets::create();
ColumnArray::ColumnOffsets::MutablePtr nested_offsets_col = ColumnArray::ColumnOffsets::create();
ColumnString::MutablePtr data_col = ColumnString::create();
auto & root_offsets_data = root_offsets_col->getData();
auto & nested_offsets_data = nested_offsets_col->getData();
ColumnArray::Offset current_root_offset = 0;
ColumnArray::Offset current_nested_offset = 0;
if constexpr (Kind == ExtractAllGroupsResultKind::VERTICAL)
{
root_offsets_data.resize(input_rows_count);
for (size_t i = 0; i < input_rows_count; ++i)
{
StringRef current_row = column_haystack->getDataAt(i);
// Extract all non-intersecting matches from haystack except group #0.
const auto * pos = current_row.data;
const auto * end = pos + current_row.size;
while (pos < end
&& regexp->Match({pos, static_cast<size_t>(end - pos)},
0, end - pos, regexp->UNANCHORED, matched_groups.data(), matched_groups.size()))
{
// 1 is to exclude group #0 which is whole re match.
for (size_t group = 1; group <= groups_count; ++group)
data_col->insertData(matched_groups[group].data(), matched_groups[group].size());
pos = matched_groups[0].data() + matched_groups[0].size();
current_nested_offset += groups_count;
nested_offsets_data.push_back(current_nested_offset);
++current_root_offset;
}
root_offsets_data[i] = current_root_offset;
}
}
else
{
std::vector<StringPiece> all_matches;
// number of times RE matched on each row of haystack column.
std::vector<size_t> number_of_matches_per_row;
// we expect RE to match multiple times on each row, `* 8` is arbitrary to reduce number of re-allocations.
all_matches.reserve(input_rows_count * groups_count * 8);
number_of_matches_per_row.reserve(input_rows_count);
for (size_t i = 0; i < input_rows_count; ++i)
{
size_t matches_per_row = 0;
const auto & current_row = column_haystack->getDataAt(i);
// Extract all non-intersecting matches from haystack except group #0.
const auto * pos = current_row.data;
const auto * end = pos + current_row.size;
while (pos < end
&& regexp->Match({pos, static_cast<size_t>(end - pos)},
0, end - pos, regexp->UNANCHORED, matched_groups.data(), matched_groups.size()))
{
// 1 is to exclude group #0 which is whole re match.
for (size_t group = 1; group <= groups_count; ++group)
all_matches.push_back(matched_groups[group]);
pos = matched_groups[0].data() + matched_groups[0].size();
++matches_per_row;
}
number_of_matches_per_row.push_back(matches_per_row);
}
{
size_t total_matched_groups_string_len = 0;
for (const auto & m : all_matches)
total_matched_groups_string_len += m.length();
data_col->reserve(total_matched_groups_string_len);
}
nested_offsets_col->reserve(matched_groups.size());
root_offsets_col->reserve(groups_count);
// Re-arrange `all_matches` from:
// [
// "ROW 0: 1st group 1st match",
// "ROW 0: 2nd group 1st match",
// ...,
// "ROW 0: 1st group 2nd match",
// "ROW 0: 2nd group 2nd match",
// ...,
// "ROW 1: 1st group 1st match",
// ...
// ]
//
// into column of 2D arrays:
// [
// /* all matchig groups from ROW 0 of haystack column */
// ["ROW 0: 1st group 1st match", "ROW 0: 1st group 2nd match", ...],
// ["ROW 0: 2nd group 1st match", "ROW 0: 2nd group 2nd match", ...],
// ...
// ],
// [
// /* all matchig groups from row 1 of haystack column */
// ["ROW 1: 1st group 1st match", ...],
// ...
// ]
size_t row_offset = 0;
for (const auto matches_per_row : number_of_matches_per_row)
{
const size_t next_row_offset = row_offset + matches_per_row * groups_count;
for (size_t group_id = 0; group_id < groups_count; ++group_id)
{
for (size_t i = row_offset + group_id; i < next_row_offset && i < all_matches.size(); i += groups_count)
{
const auto & match = all_matches[i];
data_col->insertData(match.begin(), match.length());
}
nested_offsets_col->insertValue(data_col->size());
}
root_offsets_col->insertValue(nested_offsets_col->size());
row_offset = next_row_offset;
}
}
DUMP(Kind, needle, column_haystack, root_offsets_col, nested_offsets_col);
ColumnArray::MutablePtr nested_array_col = ColumnArray::create(std::move(data_col), std::move(nested_offsets_col));
ColumnArray::MutablePtr root_array_col = ColumnArray::create(std::move(nested_array_col), std::move(root_offsets_col));
block.getByPosition(result).column = std::move(root_array_col);
}
};
}

View File

@ -0,0 +1,23 @@
#include <Functions/FunctionFactory.h>
#include <Functions/extractAllGroups.h>
namespace
{
struct HorizontalImpl
{
static constexpr auto Kind = DB::ExtractAllGroupsResultKind::HORIZONTAL;
static constexpr auto Name = "extractAllGroupsHorizontal";
};
}
namespace DB
{
void registerFunctionExtractAllGroupsHorizontal(FunctionFactory & factory)
{
factory.registerFunction<FunctionExtractAllGroups<HorizontalImpl>>();
}
}

View File

@ -0,0 +1,24 @@
#include <Functions/FunctionFactory.h>
#include <Functions/extractAllGroups.h>
namespace
{
struct VerticalImpl
{
static constexpr auto Kind = DB::ExtractAllGroupsResultKind::VERTICAL;
static constexpr auto Name = "extractAllGroupsVertical";
};
}
namespace DB
{
void registerFunctionExtractAllGroupsVertical(FunctionFactory & factory)
{
factory.registerFunction<FunctionExtractAllGroups<VerticalImpl>>();
factory.registerAlias("extractAllGroups", VerticalImpl::Name, FunctionFactory::CaseInsensitive);
}
}

View File

@ -17,7 +17,6 @@ namespace DB
namespace ErrorCodes
{
extern const int ARGUMENT_OUT_OF_BOUND;
extern const int BAD_ARGUMENTS;
}
@ -49,7 +48,6 @@ public:
};
validateFunctionArgumentTypes(*this, arguments, args);
/// Two-dimensional array of strings, each `row` of top array represents matching groups.
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>());
}
@ -61,7 +59,7 @@ public:
const auto needle = typeid_cast<const ColumnConst &>(*column_needle).getValue<String>();
if (needle.empty())
throw Exception(getName() + " length of 'needle' argument must be greater than 0.", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
throw Exception(getName() + " length of 'needle' argument must be greater than 0.", ErrorCodes::BAD_ARGUMENTS);
const auto regexp = Regexps::get<false, false>(needle);
const auto & re2 = regexp->getRE2();

View File

@ -18,7 +18,8 @@ void registerFunctionMultiFuzzyMatchAny(FunctionFactory &);
void registerFunctionMultiFuzzyMatchAnyIndex(FunctionFactory &);
void registerFunctionMultiFuzzyMatchAllIndices(FunctionFactory &);
void registerFunctionExtractGroups(FunctionFactory &);
void registerFunctionExtractAllGroups(FunctionFactory &);
void registerFunctionExtractAllGroupsVertical(FunctionFactory &);
void registerFunctionExtractAllGroupsHorizontal(FunctionFactory &);
void registerFunctionsStringRegexp(FunctionFactory & factory)
{
@ -37,7 +38,8 @@ void registerFunctionsStringRegexp(FunctionFactory & factory)
registerFunctionMultiFuzzyMatchAnyIndex(factory);
registerFunctionMultiFuzzyMatchAllIndices(factory);
registerFunctionExtractGroups(factory);
registerFunctionExtractAllGroups(factory);
registerFunctionExtractAllGroupsVertical(factory);
registerFunctionExtractAllGroupsHorizontal(factory);
}
}

View File

@ -144,7 +144,8 @@ SRCS(
exp10.cpp
exp2.cpp
exp.cpp
extractAllGroups.cpp
extractAllGroupsHorizontal.cpp
extractAllGroupsVertical.cpp
extract.cpp
extractGroups.cpp
extractTimeZoneFromFunctionArguments.cpp

View File

@ -422,7 +422,7 @@ void DDLWorker::processTasks()
}
catch (const Coordination::Exception & e)
{
if (server_startup && e.code == Coordination::ZNONODE)
if (server_startup && e.code == Coordination::Error::ZNONODE)
{
LOG_WARNING(log, "ZooKeeper NONODE error during startup. Ignoring entry {} ({}) : {}", task.entry_name, task.entry.query, getCurrentExceptionMessage(true));
}
@ -603,15 +603,15 @@ void DDLWorker::processTask(DDLTask & task, const ZooKeeperPtr & zookeeper)
auto code = zookeeper->tryCreate(active_node_path, "", zkutil::CreateMode::Ephemeral, dummy);
if (code == Coordination::ZOK || code == Coordination::ZNODEEXISTS)
if (code == Coordination::Error::ZOK || code == Coordination::Error::ZNODEEXISTS)
{
// Ok
}
else if (code == Coordination::ZNONODE)
else if (code == Coordination::Error::ZNONODE)
{
/// There is no parent
createStatusDirs(task.entry_path, zookeeper);
if (Coordination::ZOK != zookeeper->tryCreate(active_node_path, "", zkutil::CreateMode::Ephemeral, dummy))
if (Coordination::Error::ZOK != zookeeper->tryCreate(active_node_path, "", zkutil::CreateMode::Ephemeral, dummy))
throw Coordination::Exception(code, active_node_path);
}
else
@ -915,8 +915,9 @@ void DDLWorker::createStatusDirs(const std::string & node_path, const ZooKeeperP
ops.emplace_back(std::make_shared<Coordination::CreateRequest>(std::move(request)));
}
Coordination::Responses responses;
int code = zookeeper->tryMulti(ops, responses);
if (code && code != Coordination::ZNODEEXISTS)
Coordination::Error code = zookeeper->tryMulti(ops, responses);
if (code != Coordination::Error::ZOK
&& code != Coordination::Error::ZNODEEXISTS)
throw Coordination::Exception(code);
}
@ -1013,7 +1014,7 @@ void DDLWorker::runMainThread()
}
}
}
else if (e.code == Coordination::ZNONODE)
else if (e.code == Coordination::Error::ZNONODE)
{
LOG_ERROR(log, "ZooKeeper error: {}", getCurrentExceptionMessage(true));
}
@ -1201,8 +1202,8 @@ private:
static Strings getChildrenAllowNoNode(const std::shared_ptr<zkutil::ZooKeeper> & zookeeper, const String & node_path)
{
Strings res;
int code = zookeeper->tryGetChildren(node_path, res);
if (code && code != Coordination::ZNONODE)
Coordination::Error code = zookeeper->tryGetChildren(node_path, res);
if (code != Coordination::Error::ZOK && code != Coordination::Error::ZNONODE)
throw Coordination::Exception(code, node_path);
return res;
}

View File

@ -104,13 +104,13 @@ EphemeralLocksInAllPartitions::EphemeralLocksInAllPartitions(
lock_ops.push_back(zkutil::makeCheckRequest(block_numbers_path, partitions_stat.version));
Coordination::Responses lock_responses;
int rc = zookeeper.tryMulti(lock_ops, lock_responses);
if (rc == Coordination::ZBADVERSION)
Coordination::Error rc = zookeeper.tryMulti(lock_ops, lock_responses);
if (rc == Coordination::Error::ZBADVERSION)
{
LOG_TRACE(&Poco::Logger::get("EphemeralLocksInAllPartitions"), "Someone has inserted a block in a new partition while we were creating locks. Retry.");
continue;
}
else if (rc != Coordination::ZOK)
else if (rc != Coordination::Error::ZOK)
throw Coordination::Exception(rc);
for (size_t i = 0; i < partitions.size(); ++i)

View File

@ -82,7 +82,6 @@ public:
const AllowedMergingPredicate & can_merge,
String * out_disable_reason = nullptr);
/** Select all the parts in the specified partition for merge, if possible.
* final - choose to merge even a single part - that is, allow to merge one part "with itself".
*/

View File

@ -85,7 +85,7 @@ void ReplicatedMergeTreeBlockOutputStream::checkQuorumPrecondition(zkutil::ZooKe
*/
auto quorum_status = quorum_status_future.get();
if (quorum_status.error != Coordination::ZNONODE)
if (quorum_status.error != Coordination::Error::ZNONODE)
throw Exception("Quorum for previous write has not been satisfied yet. Status: " + quorum_status.data, ErrorCodes::UNSATISFIED_QUORUM_FOR_PREVIOUS_WRITE);
/// Both checks are implicitly made also later (otherwise there would be a race condition).
@ -93,7 +93,7 @@ void ReplicatedMergeTreeBlockOutputStream::checkQuorumPrecondition(zkutil::ZooKe
auto is_active = is_active_future.get();
auto host = host_future.get();
if (is_active.error == Coordination::ZNONODE || host.error == Coordination::ZNONODE)
if (is_active.error == Coordination::Error::ZNONODE || host.error == Coordination::Error::ZNONODE)
throw Exception("Replica is not active right now", ErrorCodes::READONLY);
quorum_info.is_active_node_value = is_active.data;
@ -299,9 +299,9 @@ void ReplicatedMergeTreeBlockOutputStream::commitPart(zkutil::ZooKeeperPtr & zoo
storage.renameTempPartAndAdd(part, nullptr, &transaction);
Coordination::Responses responses;
int32_t multi_code = zookeeper->tryMultiNoThrow(ops, responses); /// 1 RTT
Coordination::Error multi_code = zookeeper->tryMultiNoThrow(ops, responses); /// 1 RTT
if (multi_code == Coordination::ZOK)
if (multi_code == Coordination::Error::ZOK)
{
transaction.commit();
storage.merge_selecting_task->schedule();
@ -309,8 +309,8 @@ void ReplicatedMergeTreeBlockOutputStream::commitPart(zkutil::ZooKeeperPtr & zoo
/// Lock nodes have been already deleted, do not delete them in destructor
block_number_lock->assumeUnlocked();
}
else if (multi_code == Coordination::ZCONNECTIONLOSS
|| multi_code == Coordination::ZOPERATIONTIMEOUT)
else if (multi_code == Coordination::Error::ZCONNECTIONLOSS
|| multi_code == Coordination::Error::ZOPERATIONTIMEOUT)
{
/** If the connection is lost, and we do not know if the changes were applied, we can not delete the local part
* if the changes were applied, the inserted block appeared in `/blocks/`, and it can not be inserted again.
@ -326,7 +326,7 @@ void ReplicatedMergeTreeBlockOutputStream::commitPart(zkutil::ZooKeeperPtr & zoo
{
String failed_op_path = zkutil::KeeperMultiException(multi_code, ops, responses).getPathForFirstFailedOp();
if (multi_code == Coordination::ZNODEEXISTS && deduplicate_block && failed_op_path == block_id_path)
if (multi_code == Coordination::Error::ZNODEEXISTS && deduplicate_block && failed_op_path == block_id_path)
{
/// Block with the same id have just appeared in table (or other replica), rollback thee insertion.
LOG_INFO(log, "Block with ID {} already exists; ignoring it (removing part {})", block_id, part->name);
@ -336,7 +336,7 @@ void ReplicatedMergeTreeBlockOutputStream::commitPart(zkutil::ZooKeeperPtr & zoo
last_block_is_duplicate = true;
ProfileEvents::increment(ProfileEvents::DuplicatedInsertedBlocks);
}
else if (multi_code == Coordination::ZNODEEXISTS && failed_op_path == quorum_info.status_path)
else if (multi_code == Coordination::Error::ZNODEEXISTS && failed_op_path == quorum_info.status_path)
{
transaction.rollback();
@ -347,7 +347,7 @@ void ReplicatedMergeTreeBlockOutputStream::commitPart(zkutil::ZooKeeperPtr & zoo
/// NOTE: We could be here if the node with the quorum existed, but was quickly removed.
transaction.rollback();
throw Exception("Unexpected logical error while adding block " + toString(block_number) + " with ID '" + block_id + "': "
+ zkutil::ZooKeeper::error2string(multi_code) + ", path " + failed_op_path,
+ Coordination::errorMessage(multi_code) + ", path " + failed_op_path,
ErrorCodes::UNEXPECTED_ZOOKEEPER_ERROR);
}
}
@ -355,13 +355,13 @@ void ReplicatedMergeTreeBlockOutputStream::commitPart(zkutil::ZooKeeperPtr & zoo
{
transaction.rollback();
throw Exception("Unrecoverable network error while adding block " + toString(block_number) + " with ID '" + block_id + "': "
+ zkutil::ZooKeeper::error2string(multi_code), ErrorCodes::UNEXPECTED_ZOOKEEPER_ERROR);
+ Coordination::errorMessage(multi_code), ErrorCodes::UNEXPECTED_ZOOKEEPER_ERROR);
}
else
{
transaction.rollback();
throw Exception("Unexpected ZooKeeper error while adding block " + toString(block_number) + " with ID '" + block_id + "': "
+ zkutil::ZooKeeper::error2string(multi_code), ErrorCodes::UNEXPECTED_ZOOKEEPER_ERROR);
+ Coordination::errorMessage(multi_code), ErrorCodes::UNEXPECTED_ZOOKEEPER_ERROR);
}
if (quorum)

View File

@ -40,7 +40,7 @@ void ReplicatedMergeTreeCleanupThread::run()
{
tryLogCurrentException(log, __PRETTY_FUNCTION__);
if (e.code == Coordination::ZSESSIONEXPIRED)
if (e.code == Coordination::Error::ZSESSIONEXPIRED)
return;
}
catch (...)
@ -319,15 +319,15 @@ void ReplicatedMergeTreeCleanupThread::clearOldBlocks()
for (auto & pair : try_remove_futures)
{
const String & path = pair.first;
int32_t rc = pair.second.get().error;
if (rc == Coordination::ZNOTEMPTY)
Coordination::Error rc = pair.second.get().error;
if (rc == Coordination::Error::ZNOTEMPTY)
{
/// Can happen if there are leftover block nodes with children created by previous server versions.
zookeeper->removeRecursive(path);
cached_block_stats.erase(first_outdated_block->node);
}
else if (rc)
LOG_WARNING(log, "Error while deleting ZooKeeper path `{}`: {}, ignoring.", path, zkutil::ZooKeeper::error2string(rc));
else if (rc != Coordination::Error::ZOK)
LOG_WARNING(log, "Error while deleting ZooKeeper path `{}`: {}, ignoring.", path, Coordination::errorMessage(rc));
else
{
/// Successfully removed blocks have to be removed from cache
@ -348,7 +348,7 @@ void ReplicatedMergeTreeCleanupThread::getBlocksSortedByTime(zkutil::ZooKeeper &
Strings blocks;
Coordination::Stat stat;
if (zookeeper.tryGetChildren(storage.zookeeper_path + "/blocks", blocks, &stat))
if (Coordination::Error::ZOK != zookeeper.tryGetChildren(storage.zookeeper_path + "/blocks", blocks, &stat))
throw Exception(storage.zookeeper_path + "/blocks doesn't exist", ErrorCodes::NOT_FOUND_NODE);
/// Seems like this code is obsolete, because we delete blocks from cache
@ -391,7 +391,7 @@ void ReplicatedMergeTreeCleanupThread::getBlocksSortedByTime(zkutil::ZooKeeper &
for (auto & elem : exists_futures)
{
auto status = elem.second.get();
if (status.error != Coordination::ZNONODE)
if (status.error != Coordination::Error::ZNONODE)
{
cached_block_stats.emplace(elem.first, status.stat.ctime);
timed_blocks.emplace_back(elem.first, status.stat.ctime);

View File

@ -368,7 +368,7 @@ void ReplicatedMergeTreePartCheckThread::run()
{
tryLogCurrentException(log, __PRETTY_FUNCTION__);
if (e.code == Coordination::ZSESSIONEXPIRED)
if (e.code == Coordination::Error::ZSESSIONEXPIRED)
return;
task->scheduleAfter(PART_CHECK_ERROR_SLEEP_MS);

View File

@ -319,8 +319,8 @@ void ReplicatedMergeTreeQueue::updateTimesInZooKeeper(
Coordination::Responses responses;
auto code = zookeeper->tryMulti(ops, responses);
if (code)
LOG_ERROR(log, "Couldn't set value of nodes for insert times ({}/min_unprocessed_insert_time, max_processed_insert_time): {}", replica_path, zkutil::ZooKeeper::error2string(code) + ". This shouldn't happen often.");
if (code != Coordination::Error::ZOK)
LOG_ERROR(log, "Couldn't set value of nodes for insert times ({}/min_unprocessed_insert_time, max_processed_insert_time): {}. This shouldn't happen often.", replica_path, Coordination::errorMessage(code));
}
}
@ -364,8 +364,8 @@ void ReplicatedMergeTreeQueue::removeProcessedEntry(zkutil::ZooKeeperPtr zookeep
notifySubscribers(queue_size);
auto code = zookeeper->tryRemove(replica_path + "/queue/" + entry->znode_name);
if (code)
LOG_ERROR(log, "Couldn't remove {}/queue/{}: {}. This shouldn't happen often.", replica_path, entry->znode_name, zkutil::ZooKeeper::error2string(code));
if (code != Coordination::Error::ZOK)
LOG_ERROR(log, "Couldn't remove {}/queue/{}: {}. This shouldn't happen often.", replica_path, entry->znode_name, Coordination::errorMessage(code));
updateTimesInZooKeeper(zookeeper, min_unprocessed_insert_time_changed, max_processed_insert_time_changed);
}
@ -419,7 +419,7 @@ bool ReplicatedMergeTreeQueue::removeFromVirtualParts(const MergeTreePartInfo &
return virtual_parts.remove(part_info);
}
void ReplicatedMergeTreeQueue::pullLogsToQueue(zkutil::ZooKeeperPtr zookeeper, Coordination::WatchCallback watch_callback)
int32_t ReplicatedMergeTreeQueue::pullLogsToQueue(zkutil::ZooKeeperPtr zookeeper, Coordination::WatchCallback watch_callback)
{
std::lock_guard lock(pull_logs_to_queue_mutex);
if (pull_log_blocker.isCancelled())
@ -428,6 +428,10 @@ void ReplicatedMergeTreeQueue::pullLogsToQueue(zkutil::ZooKeeperPtr zookeeper, C
String index_str = zookeeper->get(replica_path + "/log_pointer");
UInt64 index;
/// The version of "/log" is modified when new entries to merge/mutate/drop appear.
Coordination::Stat stat;
zookeeper->get(zookeeper_path + "/log", &stat);
Strings log_entries = zookeeper->getChildrenWatch(zookeeper_path + "/log", nullptr, watch_callback);
/// We update mutations after we have loaded the list of log entries, but before we insert them
@ -561,6 +565,8 @@ void ReplicatedMergeTreeQueue::pullLogsToQueue(zkutil::ZooKeeperPtr zookeeper, C
if (storage.queue_task_handle)
storage.queue_task_handle->signalReadyToRun();
}
return stat.version;
}
@ -720,7 +726,7 @@ ReplicatedMergeTreeMutationEntryPtr ReplicatedMergeTreeQueue::removeMutation(
std::lock_guard lock(update_mutations_mutex);
auto rc = zookeeper->tryRemove(zookeeper_path + "/mutations/" + mutation_id);
if (rc == Coordination::ZOK)
if (rc == Coordination::Error::ZOK)
LOG_DEBUG(log, "Removed mutation {} from ZooKeeper.", mutation_id);
ReplicatedMergeTreeMutationEntryPtr entry;
@ -844,8 +850,8 @@ void ReplicatedMergeTreeQueue::removePartProducingOpsInRange(
if ((*it)->currently_executing)
to_wait.push_back(*it);
auto code = zookeeper->tryRemove(replica_path + "/queue/" + (*it)->znode_name);
if (code)
LOG_INFO(log, "Couldn't remove {}: {}", replica_path + "/queue/" + (*it)->znode_name, zkutil::ZooKeeper::error2string(code));
if (code != Coordination::Error::ZOK)
LOG_INFO(log, "Couldn't remove {}: {}", replica_path + "/queue/" + (*it)->znode_name, Coordination::errorMessage(code));
updateStateOnQueueEntryRemoval(
*it, /* is_successful = */ false,
@ -1625,15 +1631,15 @@ ReplicatedMergeTreeMergePredicate::ReplicatedMergeTreeMergePredicate(
for (auto & block : block_infos)
{
Coordination::GetResponse resp = block.contents_future.get();
if (!resp.error && lock_holder_paths.count(resp.data))
if (resp.error == Coordination::Error::ZOK && lock_holder_paths.count(resp.data))
committing_blocks[block.partition].insert(block.number);
}
}
queue_.pullLogsToQueue(zookeeper);
merges_version = queue_.pullLogsToQueue(zookeeper);
Coordination::GetResponse quorum_status_response = quorum_status_future.get();
if (!quorum_status_response.error)
if (quorum_status_response.error == Coordination::Error::ZOK)
{
ReplicatedMergeTreeQuorumEntry quorum_status;
quorum_status.fromString(quorum_status_response.data);

View File

@ -271,8 +271,9 @@ public:
* If watch_callback is not empty, will call it when new entries appear in the log.
* If there were new entries, notifies storage.queue_task_handle.
* Additionally loads mutations (so that the set of mutations is always more recent than the queue).
* Return the version of "logs" node (that is updated for every merge/mutation/... added to the log)
*/
void pullLogsToQueue(zkutil::ZooKeeperPtr zookeeper, Coordination::WatchCallback watch_callback = {});
int32_t pullLogsToQueue(zkutil::ZooKeeperPtr zookeeper, Coordination::WatchCallback watch_callback = {});
/// Load new mutation entries. If something new is loaded, schedule storage.merge_selecting_task.
/// If watch_callback is not empty, will call it when new mutations appear in ZK.
@ -434,6 +435,9 @@ public:
bool isMutationFinished(const ReplicatedMergeTreeMutationEntry & mutation) const;
/// The version of "log" node that is used to check that no new merges have appeared.
int32_t getVersion() const { return merges_version; }
private:
const ReplicatedMergeTreeQueue & queue;
@ -445,6 +449,8 @@ private:
/// Quorum state taken at some later time than prev_virtual_parts.
String inprogress_quorum_part;
int32_t merges_version = -1;
};

View File

@ -234,7 +234,7 @@ void ReplicatedMergeTreeRestartingThread::removeFailedQuorumParts()
auto zookeeper = storage.getZooKeeper();
Strings failed_parts;
if (zookeeper->tryGetChildren(storage.zookeeper_path + "/quorum/failed_parts", failed_parts) != Coordination::ZOK)
if (zookeeper->tryGetChildren(storage.zookeeper_path + "/quorum/failed_parts", failed_parts) != Coordination::Error::ZOK)
return;
/// Firstly, remove parts from ZooKeeper
@ -294,12 +294,12 @@ void ReplicatedMergeTreeRestartingThread::activateReplica()
{
auto code = zookeeper->tryRemove(is_active_path, stat.version);
if (code == Coordination::ZBADVERSION)
if (code == Coordination::Error::ZBADVERSION)
throw Exception("Another instance of replica " + storage.replica_path + " was created just now."
" You shouldn't run multiple instances of same replica. You need to check configuration files.",
ErrorCodes::REPLICA_IS_ALREADY_ACTIVE);
if (code && code != Coordination::ZNONODE)
if (code != Coordination::Error::ZOK && code != Coordination::Error::ZNONODE)
throw Coordination::Exception(code, is_active_path);
}
@ -314,7 +314,7 @@ void ReplicatedMergeTreeRestartingThread::activateReplica()
}
catch (const Coordination::Exception & e)
{
if (e.code == Coordination::ZNODEEXISTS)
if (e.code == Coordination::Error::ZNODEEXISTS)
throw Exception("Replica " + storage.replica_path + " appears to be already active. If you're sure it's not, "
"try again in a minute or remove znode " + storage.replica_path + "/is_active manually", ErrorCodes::REPLICA_IS_ALREADY_ACTIVE);

View File

@ -441,8 +441,8 @@ bool StorageReplicatedMergeTree::createTableIfNotExists()
LOG_WARNING(log, "Removing leftovers from table {} (this might take several minutes)", zookeeper_path);
Strings children;
int32_t code = zookeeper->tryGetChildren(zookeeper_path, children);
if (code == Coordination::ZNONODE)
Coordination::Error code = zookeeper->tryGetChildren(zookeeper_path, children);
if (code == Coordination::Error::ZNONODE)
{
LOG_WARNING(log, "Table {} is already finished removing by another replica right now", replica_path);
}
@ -458,16 +458,16 @@ bool StorageReplicatedMergeTree::createTableIfNotExists()
ops.emplace_back(zkutil::makeRemoveRequest(zookeeper_path, -1));
code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::ZNONODE)
if (code == Coordination::Error::ZNONODE)
{
LOG_WARNING(log, "Table {} is already finished removing by another replica right now", replica_path);
}
else if (code == Coordination::ZNOTEMPTY)
else if (code == Coordination::Error::ZNOTEMPTY)
{
throw Exception(fmt::format(
"The old table was not completely removed from ZooKeeper, {} still exists and may contain some garbage. But it should never happen according to the logic of operations (it's a bug).", zookeeper_path), ErrorCodes::LOGICAL_ERROR);
}
else if (code != Coordination::ZOK)
else if (code != Coordination::Error::ZOK)
{
/// It is still possible that ZooKeeper session is expired or server is killed in the middle of the delete operation.
zkutil::KeeperMultiException::check(code, ops, responses);
@ -535,12 +535,12 @@ bool StorageReplicatedMergeTree::createTableIfNotExists()
Coordination::Responses responses;
auto code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::ZNODEEXISTS)
if (code == Coordination::Error::ZNODEEXISTS)
{
LOG_WARNING(log, "It looks like the table {} was created by another server at the same moment, will retry", zookeeper_path);
continue;
}
else if (code != Coordination::ZOK)
else if (code != Coordination::Error::ZOK)
{
zkutil::KeeperMultiException::check(code, ops, responses);
}
@ -557,15 +557,14 @@ void StorageReplicatedMergeTree::createReplica()
LOG_DEBUG(log, "Creating replica {}", replica_path);
int32_t code;
Coordination::Error code;
do
{
Coordination::Stat replicas_stat;
String replicas_value;
code = zookeeper->tryGet(zookeeper_path + "/replicas", replicas_value, &replicas_stat);
if (code == Coordination::ZNONODE)
if (!zookeeper->tryGet(zookeeper_path + "/replicas", replicas_value, &replicas_stat))
throw Exception(fmt::format("Cannot create a replica of the table {}, because the last replica of the table was dropped right now",
zookeeper_path), ErrorCodes::ALL_REPLICAS_LOST);
@ -600,15 +599,15 @@ void StorageReplicatedMergeTree::createReplica()
Coordination::Responses responses;
code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::ZNODEEXISTS)
if (code == Coordination::Error::ZNODEEXISTS)
{
throw Exception("Replica " + replica_path + " already exists.", ErrorCodes::REPLICA_IS_ALREADY_EXIST);
}
else if (code == Coordination::ZBADVERSION)
else if (code == Coordination::Error::ZBADVERSION)
{
LOG_ERROR(log, "Retrying createReplica(), because some other replicas were created at the same time");
}
else if (code == Coordination::ZNONODE)
else if (code == Coordination::Error::ZNONODE)
{
throw Exception("Table " + zookeeper_path + " was suddenly removed.", ErrorCodes::ALL_REPLICAS_LOST);
}
@ -616,7 +615,7 @@ void StorageReplicatedMergeTree::createReplica()
{
zkutil::KeeperMultiException::check(code, ops, responses);
}
} while (code == Coordination::ZBADVERSION);
} while (code == Coordination::Error::ZBADVERSION);
}
void StorageReplicatedMergeTree::drop()
@ -641,7 +640,7 @@ void StorageReplicatedMergeTree::drop()
/// Check that `zookeeper_path` exists: it could have been deleted by another replica after execution of previous line.
Strings replicas;
if (Coordination::ZOK == zookeeper->tryGetChildren(zookeeper_path + "/replicas", replicas) && replicas.empty())
if (Coordination::Error::ZOK == zookeeper->tryGetChildren(zookeeper_path + "/replicas", replicas) && replicas.empty())
{
LOG_INFO(log, "{} is the last replica, will remove table", replica_path);
@ -657,17 +656,17 @@ void StorageReplicatedMergeTree::drop()
Coordination::Responses responses;
ops.emplace_back(zkutil::makeRemoveRequest(zookeeper_path + "/replicas", -1));
ops.emplace_back(zkutil::makeCreateRequest(zookeeper_path + "/dropped", "", zkutil::CreateMode::Persistent));
int32_t code = zookeeper->tryMulti(ops, responses);
Coordination::Error code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::ZNONODE || code == Coordination::ZNODEEXISTS)
if (code == Coordination::Error::ZNONODE || code == Coordination::Error::ZNODEEXISTS)
{
LOG_WARNING(log, "Table {} is already started to be removing by another replica right now", replica_path);
}
else if (code == Coordination::ZNOTEMPTY)
else if (code == Coordination::Error::ZNOTEMPTY)
{
LOG_WARNING(log, "Another replica was suddenly created, will keep the table {}", replica_path);
}
else if (code != Coordination::ZOK)
else if (code != Coordination::Error::ZOK)
{
zkutil::KeeperMultiException::check(code, ops, responses);
}
@ -677,7 +676,7 @@ void StorageReplicatedMergeTree::drop()
Strings children;
code = zookeeper->tryGetChildren(zookeeper_path, children);
if (code == Coordination::ZNONODE)
if (code == Coordination::Error::ZNONODE)
{
LOG_WARNING(log, "Table {} is already finished removing by another replica right now", replica_path);
}
@ -693,16 +692,16 @@ void StorageReplicatedMergeTree::drop()
ops.emplace_back(zkutil::makeRemoveRequest(zookeeper_path, -1));
code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::ZNONODE)
if (code == Coordination::Error::ZNONODE)
{
LOG_WARNING(log, "Table {} is already finished removing by another replica right now", replica_path);
}
else if (code == Coordination::ZNOTEMPTY)
else if (code == Coordination::Error::ZNOTEMPTY)
{
LOG_ERROR(log, "Table was not completely removed from ZooKeeper, {} still exists and may contain some garbage.",
zookeeper_path);
}
else if (code != Coordination::ZOK)
else if (code != Coordination::Error::ZOK)
{
/// It is still possible that ZooKeeper session is expired or server is killed in the middle of the delete operation.
zkutil::KeeperMultiException::check(code, ops, responses);
@ -954,7 +953,7 @@ void StorageReplicatedMergeTree::checkParts(bool skip_sanity_checks)
time_t part_create_time = 0;
Coordination::ExistsResponse exists_resp = exists_futures[i].get();
if (!exists_resp.error)
if (exists_resp.error == Coordination::Error::ZOK)
{
part_create_time = exists_resp.stat.ctime / 1000;
removePartFromZooKeeper(part_name, ops, exists_resp.stat.numChildren > 0);
@ -1125,7 +1124,7 @@ MergeTreeData::DataPartsVector StorageReplicatedMergeTree::checkPartChecksumsAnd
size_t num_check_ops = 2 * absent_part_paths_on_replicas.size();
size_t failed_op_index = e.failed_op_index;
if (failed_op_index < num_check_ops && e.code == Coordination::ZNODEEXISTS)
if (failed_op_index < num_check_ops && e.code == Coordination::Error::ZNODEEXISTS)
{
LOG_INFO(log, "The part {} on a replica suddenly appeared, will recheck checksums", e.getPathForFirstFailedOp());
}
@ -1602,15 +1601,15 @@ bool StorageReplicatedMergeTree::executeFetch(LogEntry & entry)
Coordination::Responses responses;
auto code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::ZOK)
if (code == Coordination::Error::ZOK)
{
LOG_DEBUG(log, "Marked quorum for part {} as failed.", entry.new_part_name);
queue.removeFromVirtualParts(part_info);
return true;
}
else if (code == Coordination::ZBADVERSION || code == Coordination::ZNONODE || code == Coordination::ZNODEEXISTS)
else if (code == Coordination::Error::ZBADVERSION || code == Coordination::Error::ZNONODE || code == Coordination::Error::ZNODEEXISTS)
{
LOG_DEBUG(log, "State was changed or isn't expected when trying to mark quorum for part {} as failed. Code: {}", entry.new_part_name, zkutil::ZooKeeper::error2string(code));
LOG_DEBUG(log, "State was changed or isn't expected when trying to mark quorum for part {} as failed. Code: {}", entry.new_part_name, Coordination::errorMessage(code));
}
else
throw Coordination::Exception(code);
@ -2106,7 +2105,7 @@ void StorageReplicatedMergeTree::cloneReplica(const String & source_replica, Coo
auto rc = zookeeper->tryMulti(ops, responses);
if (rc == Coordination::ZOK)
if (rc == Coordination::Error::ZOK)
{
break;
}
@ -2274,7 +2273,7 @@ void StorageReplicatedMergeTree::queueUpdatingTask()
{
tryLogCurrentException(log, __PRETTY_FUNCTION__);
if (e.code == Coordination::ZSESSIONEXPIRED)
if (e.code == Coordination::Error::ZSESSIONEXPIRED)
{
restarting_thread.wakeup();
return;
@ -2300,7 +2299,7 @@ void StorageReplicatedMergeTree::mutationsUpdatingTask()
{
tryLogCurrentException(log, __PRETTY_FUNCTION__);
if (e.code == Coordination::ZSESSIONEXPIRED)
if (e.code == Coordination::Error::ZSESSIONEXPIRED)
return;
mutations_updating_task->scheduleAfter(QUEUE_UPDATE_ERROR_SLEEP_MS);
@ -2420,7 +2419,7 @@ void StorageReplicatedMergeTree::mergeSelectingTask()
const bool deduplicate = false; /// TODO: read deduplicate option from table config
const bool force_ttl = false;
bool success = false;
CreateMergeEntryResult create_result = CreateMergeEntryResult::Other;
try
{
@ -2440,7 +2439,11 @@ void StorageReplicatedMergeTree::mergeSelectingTask()
size_t merges_and_mutations_sum = merges_and_mutations_queued.first + merges_and_mutations_queued.second;
if (merges_and_mutations_sum >= storage_settings_ptr->max_replicated_merges_in_queue)
{
LOG_TRACE(log, "Number of queued merges ({}) and part mutations ({}) is greater than max_replicated_merges_in_queue ({}), so won't select new parts to merge or mutate.", merges_and_mutations_queued.first, merges_and_mutations_queued.second, storage_settings_ptr->max_replicated_merges_in_queue);
LOG_TRACE(log, "Number of queued merges ({}) and part mutations ({})"
" is greater than max_replicated_merges_in_queue ({}), so won't select new parts to merge or mutate.",
merges_and_mutations_queued.first,
merges_and_mutations_queued.second,
storage_settings_ptr->max_replicated_merges_in_queue);
}
else
{
@ -2452,10 +2455,10 @@ void StorageReplicatedMergeTree::mergeSelectingTask()
if (max_source_parts_size_for_merge > 0 &&
merger_mutator.selectPartsToMerge(future_merged_part, false, max_source_parts_size_for_merge, merge_pred, nullptr))
{
success = createLogEntryToMergeParts(zookeeper, future_merged_part.parts,
future_merged_part.name, future_merged_part.type, deduplicate, force_ttl);
create_result = createLogEntryToMergeParts(zookeeper, future_merged_part.parts,
future_merged_part.name, future_merged_part.type, deduplicate, force_ttl, nullptr, merge_pred.getVersion());
}
/// If there are many mutations in queue it may happen, that we cannot enqueue enough merges to merge all new parts
/// If there are many mutations in queue, it may happen, that we cannot enqueue enough merges to merge all new parts
else if (max_source_part_size_for_mutation > 0 && queue.countMutations() > 0
&& merges_and_mutations_queued.second < storage_settings_ptr->max_replicated_mutations_in_queue)
{
@ -2470,11 +2473,11 @@ void StorageReplicatedMergeTree::mergeSelectingTask()
if (!desired_mutation_version)
continue;
if (createLogEntryToMutatePart(*part, desired_mutation_version->first, desired_mutation_version->second))
{
success = true;
create_result = createLogEntryToMutatePart(*part,
desired_mutation_version->first, desired_mutation_version->second, merge_pred.getVersion());
if (create_result == CreateMergeEntryResult::Ok)
break;
}
}
}
}
@ -2487,11 +2490,15 @@ void StorageReplicatedMergeTree::mergeSelectingTask()
if (!is_leader)
return;
if (!success)
if (create_result != CreateMergeEntryResult::Ok
&& create_result != CreateMergeEntryResult::LogUpdated)
{
merge_selecting_task->scheduleAfter(MERGE_SELECTING_SLEEP_MS);
}
else
{
merge_selecting_task->schedule();
}
}
@ -2525,14 +2532,15 @@ void StorageReplicatedMergeTree::mutationsFinalizingTask()
}
bool StorageReplicatedMergeTree::createLogEntryToMergeParts(
StorageReplicatedMergeTree::CreateMergeEntryResult StorageReplicatedMergeTree::createLogEntryToMergeParts(
zkutil::ZooKeeperPtr & zookeeper,
const DataPartsVector & parts,
const String & merged_name,
const MergeTreeDataPartType & merged_part_type,
bool deduplicate,
bool force_ttl,
ReplicatedMergeTreeLogEntryData * out_log_entry)
ReplicatedMergeTreeLogEntryData * out_log_entry,
int32_t log_version)
{
std::vector<std::future<Coordination::ExistsResponse>> exists_futures;
exists_futures.reserve(parts.size());
@ -2543,7 +2551,7 @@ bool StorageReplicatedMergeTree::createLogEntryToMergeParts(
for (size_t i = 0; i < parts.size(); ++i)
{
/// If there is no information about part in ZK, we will not merge it.
if (exists_futures[i].get().error == Coordination::ZNONODE)
if (exists_futures[i].get().error == Coordination::Error::ZNONODE)
{
all_in_zk = false;
@ -2557,7 +2565,7 @@ bool StorageReplicatedMergeTree::createLogEntryToMergeParts(
}
if (!all_in_zk)
return false;
return CreateMergeEntryResult::MissingPart;
ReplicatedMergeTreeLogEntryData entry;
entry.type = LogEntry::MERGE_PARTS;
@ -2569,21 +2577,46 @@ bool StorageReplicatedMergeTree::createLogEntryToMergeParts(
entry.create_time = time(nullptr);
for (const auto & part : parts)
{
entry.source_parts.push_back(part->name);
}
String path_created = zookeeper->create(zookeeper_path + "/log/log-", entry.toString(), zkutil::CreateMode::PersistentSequential);
entry.znode_name = path_created.substr(path_created.find_last_of('/') + 1);
Coordination::Requests ops;
Coordination::Responses responses;
ops.emplace_back(zkutil::makeCreateRequest(
zookeeper_path + "/log/log-", entry.toString(),
zkutil::CreateMode::PersistentSequential));
ops.emplace_back(zkutil::makeSetRequest(
zookeeper_path + "/log", "", log_version)); /// Check and update version.
Coordination::Error code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::Error::ZOK)
{
String path_created = dynamic_cast<const Coordination::CreateResponse &>(*responses.front()).path_created;
entry.znode_name = path_created.substr(path_created.find_last_of('/') + 1);
LOG_TRACE(log, "Created log entry {} for merge {}", path_created, merged_name);
}
else if (code == Coordination::Error::ZBADVERSION)
{
LOG_TRACE(log, "Log entry is not created for merge {} because log was updated", merged_name);
return CreateMergeEntryResult::LogUpdated;
}
else
{
zkutil::KeeperMultiException::check(code, ops, responses);
}
if (out_log_entry)
*out_log_entry = entry;
return true;
return CreateMergeEntryResult::Ok;
}
bool StorageReplicatedMergeTree::createLogEntryToMutatePart(const IMergeTreeDataPart & part, Int64 mutation_version, int alter_version)
StorageReplicatedMergeTree::CreateMergeEntryResult StorageReplicatedMergeTree::createLogEntryToMutatePart(
const IMergeTreeDataPart & part, Int64 mutation_version, int32_t alter_version, int32_t log_version)
{
auto zookeeper = getZooKeeper();
@ -2592,11 +2625,12 @@ bool StorageReplicatedMergeTree::createLogEntryToMutatePart(const IMergeTreeData
{
if (part.modification_time + MAX_AGE_OF_LOCAL_PART_THAT_WASNT_ADDED_TO_ZOOKEEPER < time(nullptr))
{
LOG_WARNING(log, "Part {} (that was selected for mutation) with age {} seconds exists locally but not in ZooKeeper. Won't mutate that part and will check it.", part.name, (time(nullptr) - part.modification_time));
LOG_WARNING(log, "Part {} (that was selected for mutation) with age {} seconds exists locally but not in ZooKeeper."
" Won't mutate that part and will check it.", part.name, (time(nullptr) - part.modification_time));
enqueuePartForCheck(part.name);
}
return false;
return CreateMergeEntryResult::MissingPart;
}
MergeTreePartInfo new_part_info = part.info;
@ -2612,8 +2646,28 @@ bool StorageReplicatedMergeTree::createLogEntryToMutatePart(const IMergeTreeData
entry.create_time = time(nullptr);
entry.alter_version = alter_version;
zookeeper->create(zookeeper_path + "/log/log-", entry.toString(), zkutil::CreateMode::PersistentSequential);
return true;
Coordination::Requests ops;
Coordination::Responses responses;
ops.emplace_back(zkutil::makeCreateRequest(
zookeeper_path + "/log/log-", entry.toString(),
zkutil::CreateMode::PersistentSequential));
ops.emplace_back(zkutil::makeSetRequest(
zookeeper_path + "/log", "", log_version)); /// Check and update version.
Coordination::Error code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::Error::ZBADVERSION)
{
LOG_TRACE(log, "Log entry is not created for mutation {} because log was updated", new_part_name);
return CreateMergeEntryResult::LogUpdated;
}
zkutil::KeeperMultiException::check(code, ops, responses);
LOG_TRACE(log, "Created log entry for mutation {}", new_part_name);
return CreateMergeEntryResult::Ok;
}
@ -2889,16 +2943,16 @@ void StorageReplicatedMergeTree::updateQuorum(const String & part_name)
ops.emplace_back(zkutil::makeSetRequest(quorum_last_part_path, new_added_parts, added_parts_stat.version));
auto code = zookeeper->tryMulti(ops, responses);
if (code == Coordination::ZOK)
if (code == Coordination::Error::ZOK)
{
break;
}
else if (code == Coordination::ZNONODE)
else if (code == Coordination::Error::ZNONODE)
{
/// The quorum has already been achieved.
break;
}
else if (code == Coordination::ZBADVERSION)
else if (code == Coordination::Error::ZBADVERSION)
{
/// Node was updated meanwhile. We must re-read it and repeat all the actions.
continue;
@ -2911,16 +2965,16 @@ void StorageReplicatedMergeTree::updateQuorum(const String & part_name)
/// We update the node, registering there one more replica.
auto code = zookeeper->trySet(quorum_status_path, quorum_entry.toString(), stat.version);
if (code == Coordination::ZOK)
if (code == Coordination::Error::ZOK)
{
break;
}
else if (code == Coordination::ZNONODE)
else if (code == Coordination::Error::ZNONODE)
{
/// The quorum has already been achieved.
break;
}
else if (code == Coordination::ZBADVERSION)
else if (code == Coordination::Error::ZBADVERSION)
{
/// Node was updated meanwhile. We must re-read it and repeat all the actions.
continue;
@ -2964,16 +3018,16 @@ void StorageReplicatedMergeTree::cleanLastPartNode(const String & partition_id)
auto code = zookeeper->trySet(quorum_last_part_path, new_added_parts, added_parts_stat.version);
if (code == Coordination::ZOK)
if (code == Coordination::Error::ZOK)
{
break;
}
else if (code == Coordination::ZNONODE)
else if (code == Coordination::Error::ZNONODE)
{
/// Node is deleted. It is impossible, but it is Ok.
break;
}
else if (code == Coordination::ZBADVERSION)
else if (code == Coordination::Error::ZBADVERSION)
{
/// Node was updated meanwhile. We must re-read it and repeat all the actions.
continue;
@ -3395,7 +3449,8 @@ BlockOutputStreamPtr StorageReplicatedMergeTree::write(const ASTPtr & /*query*/,
}
bool StorageReplicatedMergeTree::optimize(const ASTPtr & query, const ASTPtr & partition, bool final, bool deduplicate, const Context & query_context)
bool StorageReplicatedMergeTree::optimize(
const ASTPtr & query, const ASTPtr & partition, bool final, bool deduplicate, const Context & query_context)
{
assertNotReadonly();
@ -3405,14 +3460,11 @@ bool StorageReplicatedMergeTree::optimize(const ASTPtr & query, const ASTPtr & p
return true;
}
constexpr size_t max_retries = 10;
std::vector<ReplicatedMergeTreeLogEntryData> merge_entries;
{
/// We must select parts for merge under merge_selecting_mutex because other threads
/// (merge_selecting_thread or OPTIMIZE queries) could assign new merges.
std::lock_guard merge_selecting_lock(merge_selecting_mutex);
auto zookeeper = getZooKeeper();
ReplicatedMergeTreeMergePredicate can_merge = queue.getMergePredicate(zookeeper);
auto handle_noop = [&] (const String & message)
{
@ -3436,52 +3488,94 @@ bool StorageReplicatedMergeTree::optimize(const ASTPtr & query, const ASTPtr & p
for (const String & partition_id : partition_ids)
{
FutureMergedMutatedPart future_merged_part;
bool selected = merger_mutator.selectAllPartsToMergeWithinPartition(
future_merged_part, disk_space, can_merge, partition_id, true, nullptr);
ReplicatedMergeTreeLogEntryData merge_entry;
if (selected && !createLogEntryToMergeParts(zookeeper, future_merged_part.parts,
future_merged_part.name, future_merged_part.type, deduplicate, force_ttl, &merge_entry))
return handle_noop("Can't create merge queue node in ZooKeeper");
if (merge_entry.type != ReplicatedMergeTreeLogEntryData::Type::EMPTY)
size_t try_no = 0;
for (; try_no < max_retries; ++try_no)
{
/// We must select parts for merge under merge_selecting_mutex because other threads
/// (merge_selecting_thread or OPTIMIZE queries) could assign new merges.
std::lock_guard merge_selecting_lock(merge_selecting_mutex);
ReplicatedMergeTreeMergePredicate can_merge = queue.getMergePredicate(zookeeper);
FutureMergedMutatedPart future_merged_part;
bool selected = merger_mutator.selectAllPartsToMergeWithinPartition(
future_merged_part, disk_space, can_merge, partition_id, true, nullptr);
if (!selected)
break;
ReplicatedMergeTreeLogEntryData merge_entry;
CreateMergeEntryResult create_result = createLogEntryToMergeParts(
zookeeper, future_merged_part.parts,
future_merged_part.name, future_merged_part.type, deduplicate, force_ttl,
&merge_entry, can_merge.getVersion());
if (create_result == CreateMergeEntryResult::MissingPart)
return handle_noop("Can't create merge queue node in ZooKeeper, because some parts are missing");
if (create_result == CreateMergeEntryResult::LogUpdated)
continue;
merge_entries.push_back(std::move(merge_entry));
break;
}
if (try_no == max_retries)
return handle_noop("Can't create merge queue node in ZooKeeper, because log was updated in every of "
+ toString(max_retries) + " tries");
}
}
else
{
FutureMergedMutatedPart future_merged_part;
String disable_reason;
bool selected = false;
if (!partition)
{
selected = merger_mutator.selectPartsToMerge(
future_merged_part, true, storage_settings_ptr->max_bytes_to_merge_at_max_space_in_pool, can_merge, &disable_reason);
}
else
size_t try_no = 0;
for (; try_no < max_retries; ++try_no)
{
std::lock_guard merge_selecting_lock(merge_selecting_mutex);
ReplicatedMergeTreeMergePredicate can_merge = queue.getMergePredicate(zookeeper);
UInt64 disk_space = getStoragePolicy()->getMaxUnreservedFreeSpace();
String partition_id = getPartitionIDFromQuery(partition, query_context);
selected = merger_mutator.selectAllPartsToMergeWithinPartition(
future_merged_part, disk_space, can_merge, partition_id, final, &disable_reason);
}
FutureMergedMutatedPart future_merged_part;
String disable_reason;
bool selected = false;
if (!partition)
{
selected = merger_mutator.selectPartsToMerge(
future_merged_part, true, storage_settings_ptr->max_bytes_to_merge_at_max_space_in_pool, can_merge, &disable_reason);
}
else
{
if (!selected)
{
std::stringstream message;
message << "Cannot select parts for optimization";
if (!disable_reason.empty())
message << ": " << disable_reason;
LOG_INFO(log, message.str());
return handle_noop(message.str());
}
UInt64 disk_space = getStoragePolicy()->getMaxUnreservedFreeSpace();
String partition_id = getPartitionIDFromQuery(partition, query_context);
selected = merger_mutator.selectAllPartsToMergeWithinPartition(
future_merged_part, disk_space, can_merge, partition_id, final, &disable_reason);
}
if (!selected)
{
std::stringstream message;
message << "Cannot select parts for optimization";
if (!disable_reason.empty())
message << ": " << disable_reason;
LOG_INFO(log, message.str());
return handle_noop(message.str());
}
ReplicatedMergeTreeLogEntryData merge_entry;
CreateMergeEntryResult create_result = createLogEntryToMergeParts(
zookeeper, future_merged_part.parts,
future_merged_part.name, future_merged_part.type, deduplicate, force_ttl,
&merge_entry, can_merge.getVersion());
if (create_result == CreateMergeEntryResult::MissingPart)
return handle_noop("Can't create merge queue node in ZooKeeper, because some parts are missing");
if (create_result == CreateMergeEntryResult::LogUpdated)
continue;
ReplicatedMergeTreeLogEntryData merge_entry;
if (!createLogEntryToMergeParts(zookeeper, future_merged_part.parts,
future_merged_part.name, future_merged_part.type, deduplicate, force_ttl, &merge_entry))
return handle_noop("Can't create merge queue node in ZooKeeper");
if (merge_entry.type != ReplicatedMergeTreeLogEntryData::Type::EMPTY)
merge_entries.push_back(std::move(merge_entry));
break;
}
if (try_no == max_retries)
return handle_noop("Can't create merge queue node in ZooKeeper, because log was updated in every of "
+ toString(max_retries) + " tries");
}
}
@ -3555,7 +3649,8 @@ void StorageReplicatedMergeTree::alter(
return;
}
auto ast_to_str = [](ASTPtr query) -> String {
auto ast_to_str = [](ASTPtr query) -> String
{
if (!query)
return "";
return queryToString(query);
@ -3605,7 +3700,6 @@ void StorageReplicatedMergeTree::alter(
String new_columns_str = future_metadata.columns.toString();
ops.emplace_back(zkutil::makeSetRequest(zookeeper_path + "/columns", new_columns_str, -1));
if (ast_to_str(current_metadata.settings_changes) != ast_to_str(future_metadata.settings_changes))
{
lockStructureExclusively(
@ -3627,10 +3721,12 @@ void StorageReplicatedMergeTree::alter(
alter_entry->alter_version = new_metadata_version;
alter_entry->create_time = time(nullptr);
auto maybe_mutation_commands = params.getMutationCommands(current_metadata, query_context.getSettingsRef().materialize_ttl_after_modify, query_context);
auto maybe_mutation_commands = params.getMutationCommands(
current_metadata, query_context.getSettingsRef().materialize_ttl_after_modify, query_context);
alter_entry->have_mutation = !maybe_mutation_commands.empty();
ops.emplace_back(zkutil::makeCreateRequest(zookeeper_path + "/log/log-", alter_entry->toString(), zkutil::CreateMode::PersistentSequential));
ops.emplace_back(zkutil::makeCreateRequest(
zookeeper_path + "/log/log-", alter_entry->toString(), zkutil::CreateMode::PersistentSequential));
std::optional<EphemeralLocksInAllPartitions> lock_holder;
@ -3661,9 +3757,9 @@ void StorageReplicatedMergeTree::alter(
}
Coordination::Responses results;
int32_t rc = zookeeper->tryMulti(ops, results);
Coordination::Error rc = zookeeper->tryMulti(ops, results);
if (rc == Coordination::ZOK)
if (rc == Coordination::Error::ZOK)
{
if (alter_entry->have_mutation)
{
@ -3683,9 +3779,9 @@ void StorageReplicatedMergeTree::alter(
}
break;
}
else if (rc == Coordination::ZBADVERSION)
else if (rc == Coordination::Error::ZBADVERSION)
{
if (results[0]->error)
if (results[0]->error != Coordination::Error::ZOK)
throw Exception("Metadata on replica is not up to date with common metadata in Zookeeper. Cannot alter", ErrorCodes::CANNOT_ASSIGN_ALTER);
continue;
@ -4005,8 +4101,8 @@ StorageReplicatedMergeTree::allocateBlockNumber(
ops.push_back(zkutil::makeSetRequest(block_numbers_path, "", -1));
Coordination::Responses responses;
int code = zookeeper->tryMulti(ops, responses);
if (code && code != Coordination::ZNODEEXISTS)
Coordination::Error code = zookeeper->tryMulti(ops, responses);
if (code != Coordination::Error::ZOK && code != Coordination::Error::ZNODEEXISTS)
zkutil::KeeperMultiException::check(code, ops, responses);
}
@ -4019,7 +4115,7 @@ StorageReplicatedMergeTree::allocateBlockNumber(
}
catch (const zkutil::KeeperMultiException & e)
{
if (e.code == Coordination::ZNODEEXISTS && e.getPathForFirstFailedOp() == zookeeper_block_id_path)
if (e.code == Coordination::Error::ZNODEEXISTS && e.getPathForFirstFailedOp() == zookeeper_block_id_path)
return {};
throw Exception("Cannot allocate block number in ZooKeeper: " + e.displayText(), ErrorCodes::KEEPER_EXCEPTION);
@ -4033,7 +4129,8 @@ StorageReplicatedMergeTree::allocateBlockNumber(
}
Strings StorageReplicatedMergeTree::waitForAllReplicasToProcessLogEntry(const ReplicatedMergeTreeLogEntryData & entry, bool wait_for_non_active)
Strings StorageReplicatedMergeTree::waitForAllReplicasToProcessLogEntry(
const ReplicatedMergeTreeLogEntryData & entry, bool wait_for_non_active)
{
LOG_DEBUG(log, "Waiting for all replicas to process {}", entry.znode_name);
@ -4058,7 +4155,8 @@ Strings StorageReplicatedMergeTree::waitForAllReplicasToProcessLogEntry(const Re
}
bool StorageReplicatedMergeTree::waitForReplicaToProcessLogEntry(const String & replica, const ReplicatedMergeTreeLogEntryData & entry, bool wait_for_non_active)
bool StorageReplicatedMergeTree::waitForReplicaToProcessLogEntry(
const String & replica, const ReplicatedMergeTreeLogEntryData & entry, bool wait_for_non_active)
{
String entry_str = entry.toString();
String log_node_name;
@ -4708,9 +4806,9 @@ void StorageReplicatedMergeTree::mutate(const MutationCommands & commands, const
mutations_path + "/", entry.toString(), zkutil::CreateMode::PersistentSequential));
Coordination::Responses responses;
int32_t rc = zookeeper->tryMulti(requests, responses);
Coordination::Error rc = zookeeper->tryMulti(requests, responses);
if (rc == Coordination::ZOK)
if (rc == Coordination::Error::ZOK)
{
const String & path_created =
dynamic_cast<const Coordination::CreateResponse *>(responses[1].get())->path_created;
@ -4718,7 +4816,7 @@ void StorageReplicatedMergeTree::mutate(const MutationCommands & commands, const
LOG_TRACE(log, "Created mutation with ID {}", entry.znode_name);
break;
}
else if (rc == Coordination::ZBADVERSION)
else if (rc == Coordination::Error::ZBADVERSION)
{
LOG_TRACE(log, "Version conflict when trying to create a mutation node, retrying...");
continue;
@ -4910,7 +5008,7 @@ bool StorageReplicatedMergeTree::tryRemovePartsFromZooKeeperWithRetries(const St
for (size_t i = 0; i < part_names.size(); ++i)
{
Coordination::ExistsResponse exists_resp = exists_futures[i].get();
if (!exists_resp.error)
if (exists_resp.error == Coordination::Error::ZOK)
{
Coordination::Requests ops;
removePartFromZooKeeper(part_names[i], ops, exists_resp.stat.numChildren > 0);
@ -4922,7 +5020,7 @@ bool StorageReplicatedMergeTree::tryRemovePartsFromZooKeeperWithRetries(const St
{
auto response = future.get();
if (response.error == 0 || response.error == Coordination::ZNONODE)
if (response.error == Coordination::Error::ZOK || response.error == Coordination::Error::ZNONODE)
continue;
if (Coordination::isHardwareError(response.error))
@ -4971,7 +5069,7 @@ void StorageReplicatedMergeTree::removePartsFromZooKeeper(
for (size_t i = 0; i < part_names.size(); ++i)
{
Coordination::ExistsResponse exists_resp = exists_futures[i].get();
if (!exists_resp.error)
if (exists_resp.error == Coordination::Error::ZOK)
{
Coordination::Requests ops;
removePartFromZooKeeper(part_names[i], ops, exists_resp.stat.numChildren > 0);
@ -5000,9 +5098,9 @@ void StorageReplicatedMergeTree::removePartsFromZooKeeper(
continue;
auto response = future.get();
if (response.error == Coordination::ZOK)
if (response.error == Coordination::Error::ZOK)
continue;
else if (response.error == Coordination::ZNONODE)
else if (response.error == Coordination::Error::ZNONODE)
{
LOG_DEBUG(log, "There is no part {} in ZooKeeper, it was only in filesystem", part_names[i]);
continue;
@ -5014,7 +5112,7 @@ void StorageReplicatedMergeTree::removePartsFromZooKeeper(
continue;
}
else
LOG_WARNING(log, "Cannot remove part {} from ZooKeeper: {}", part_names[i], zkutil::ZooKeeper::error2string(response.error));
LOG_WARNING(log, "Cannot remove part {} from ZooKeeper: {}", part_names[i], Coordination::errorMessage(response.error));
}
}
@ -5023,7 +5121,7 @@ void StorageReplicatedMergeTree::clearBlocksInPartition(
zkutil::ZooKeeper & zookeeper, const String & partition_id, Int64 min_block_num, Int64 max_block_num)
{
Strings blocks;
if (zookeeper.tryGetChildren(zookeeper_path + "/blocks", blocks))
if (Coordination::Error::ZOK != zookeeper.tryGetChildren(zookeeper_path + "/blocks", blocks))
throw Exception(zookeeper_path + "/blocks doesn't exist", ErrorCodes::NOT_FOUND_NODE);
String partition_prefix = partition_id + "_";
@ -5043,7 +5141,7 @@ void StorageReplicatedMergeTree::clearBlocksInPartition(
const String & path = pair.first;
auto result = pair.second.get();
if (result.error == Coordination::ZNONODE)
if (result.error == Coordination::Error::ZNONODE)
continue;
ReadBufferFromString buf(result.data);
@ -5056,14 +5154,14 @@ void StorageReplicatedMergeTree::clearBlocksInPartition(
for (auto & pair : to_delete_futures)
{
const String & path = pair.first;
int32_t rc = pair.second.get().error;
if (rc == Coordination::ZNOTEMPTY)
Coordination::Error rc = pair.second.get().error;
if (rc == Coordination::Error::ZNOTEMPTY)
{
/// Can happen if there are leftover block nodes with children created by previous server versions.
zookeeper.removeRecursive(path);
}
else if (rc)
LOG_WARNING(log, "Error while deleting ZooKeeper path `{}`: {}, ignoring.", path, zkutil::ZooKeeper::error2string(rc));
else if (rc != Coordination::Error::ZOK)
LOG_WARNING(log, "Error while deleting ZooKeeper path `{}`: {}, ignoring.", path, Coordination::errorMessage(rc));
}
LOG_TRACE(log, "Deleted {} deduplication block IDs in partition ID {}", to_delete_futures.size(), partition_id);
@ -5203,6 +5301,7 @@ void StorageReplicatedMergeTree::replacePartitionFrom(const StoragePtr & source_
}
}
ops.emplace_back(zkutil::makeSetRequest(zookeeper_path + "/log", "", -1)); /// Just update version
ops.emplace_back(zkutil::makeCreateRequest(zookeeper_path + "/log/log-", entry.toString(), zkutil::CreateMode::PersistentSequential));
Transaction transaction(*this);
@ -5388,7 +5487,8 @@ void StorageReplicatedMergeTree::movePartitionToTable(const StoragePtr & dest_ta
}
}
ops.emplace_back(zkutil::makeCreateRequest(dest_table_storage->zookeeper_path + "/log/log-", entry.toString(), zkutil::CreateMode::PersistentSequential));
ops.emplace_back(zkutil::makeCreateRequest(dest_table_storage->zookeeper_path + "/log/log-",
entry.toString(), zkutil::CreateMode::PersistentSequential));
{
Transaction transaction(*dest_table_storage);
@ -5435,11 +5535,13 @@ void StorageReplicatedMergeTree::movePartitionToTable(const StoragePtr & dest_ta
Coordination::Requests ops_dest;
ops_dest.emplace_back(zkutil::makeCreateRequest(zookeeper_path + "/log/log-", entry_delete.toString(), zkutil::CreateMode::PersistentSequential));
ops_dest.emplace_back(zkutil::makeCreateRequest(
zookeeper_path + "/log/log-", entry_delete.toString(), zkutil::CreateMode::PersistentSequential));
ops_dest.emplace_back(zkutil::makeSetRequest(zookeeper_path + "/log", "", -1)); /// Just update version
op_results = zookeeper->multi(ops_dest);
log_znode_path = dynamic_cast<const Coordination::CreateResponse &>(*op_results.back()).path_created;
log_znode_path = dynamic_cast<const Coordination::CreateResponse &>(*op_results.front()).path_created;
entry_delete.znode_name = log_znode_path.substr(log_znode_path.find_last_of('/') + 1);
if (query_context.getSettingsRef().replication_alter_partitions_sync > 1)
@ -5595,7 +5697,12 @@ bool StorageReplicatedMergeTree::dropPartsInPartition(
entry.detach = detach;
entry.create_time = time(nullptr);
String log_znode_path = zookeeper.create(zookeeper_path + "/log/log-", entry.toString(), zkutil::CreateMode::PersistentSequential);
Coordination::Requests ops;
ops.emplace_back(zkutil::makeCreateRequest(zookeeper_path + "/log/log-", entry.toString(), zkutil::CreateMode::PersistentSequential));
ops.emplace_back(zkutil::makeSetRequest(zookeeper_path + "/log", "", -1)); /// Just update version.
Coordination::Responses responses = zookeeper.multi(ops);
String log_znode_path = dynamic_cast<const Coordination::CreateResponse &>(*responses.front()).path_created;
entry.znode_name = log_znode_path.substr(log_znode_path.find_last_of('/') + 1);
return true;

View File

@ -425,16 +425,23 @@ private:
* Call when merge_selecting_mutex is locked.
* Returns false if any part is not in ZK.
*/
bool createLogEntryToMergeParts(
enum class CreateMergeEntryResult { Ok, MissingPart, LogUpdated, Other };
CreateMergeEntryResult createLogEntryToMergeParts(
zkutil::ZooKeeperPtr & zookeeper,
const DataPartsVector & parts,
const String & merged_name,
const MergeTreeDataPartType & merged_part_type,
bool deduplicate,
bool force_ttl,
ReplicatedMergeTreeLogEntryData * out_log_entry = nullptr);
ReplicatedMergeTreeLogEntryData * out_log_entry,
int32_t log_version);
bool createLogEntryToMutatePart(const IMergeTreeDataPart & part, Int64 mutation_version, int alter_version);
CreateMergeEntryResult createLogEntryToMutatePart(
const IMergeTreeDataPart & part,
Int64 mutation_version,
int32_t alter_version,
int32_t log_version);
/// Exchange parts.

View File

@ -112,8 +112,13 @@ void StorageSystemZooKeeper::fillData(MutableColumns & res_columns, const Contex
zkutil::ZooKeeperPtr zookeeper = context.getZooKeeper();
String path_corrected;
/// path should starts with '/', otherwise ZBADARGUMENTS will be thrown in
/// ZooKeeper::sendThread and the session will fail.
if (path[0] != '/')
path_corrected = '/';
path_corrected += path;
/// In all cases except the root, path must not end with a slash.
String path_corrected = path;
if (path_corrected != "/" && path_corrected.back() == '/')
path_corrected.resize(path_corrected.size() - 1);
@ -131,7 +136,7 @@ void StorageSystemZooKeeper::fillData(MutableColumns & res_columns, const Contex
for (size_t i = 0, size = nodes.size(); i < size; ++i)
{
auto res = futures[i].get();
if (res.error == Coordination::ZNONODE)
if (res.error == Coordination::Error::ZNONODE)
continue; /// Node was deleted meanwhile.
const Coordination::Stat & stat = res.stat;

View File

@ -86,7 +86,7 @@ try
for (BlockInfo & block : block_infos)
{
Coordination::GetResponse resp = block.contents_future.get();
if (!resp.error && lock_holder_paths.count(resp.data))
if (resp.error == Coordination::Error::ZOK && lock_holder_paths.count(resp.data))
{
++total_count;
current_inserts[block.partition].insert(block.number);

View File

@ -76,7 +76,7 @@ try
}
catch (const Coordination::Exception & e)
{
if (e.code == Coordination::ZNONODE)
if (e.code == Coordination::Error::ZNONODE)
continue;
throw;
}

View File

@ -6,5 +6,6 @@
<query>SELECT count() FROM test.hits WHERE NOT ignore(extract(URL, '(\\w+=\\w+)'))</query>
<query>SELECT count() FROM test.hits WHERE NOT ignore(extractAll(URL, '(\\w+=\\w+)'))</query>
<query>SELECT count() FROM test.hits WHERE NOT ignore(extractGroups(URL, '(\\w+)=(\\w+)'))</query>
<query>SELECT count() FROM test.hits WHERE NOT ignore(extractAllGroups(URL, '(\\w+)=(\\w+)'))</query>
<query>SELECT count() FROM test.hits WHERE NOT ignore(extractAllGroupsVertical(URL, '(\\w+)=(\\w+)'))</query>
<query>SELECT count() FROM test.hits WHERE NOT ignore(extractAllGroupsHorizontal(URL, '(\\w+)=(\\w+)'))</query>
</test>

View File

@ -1,51 +0,0 @@
-- error cases
SELECT extractAllGroups(); --{serverError 42} not enough arguments
SELECT extractAllGroups('hello'); --{serverError 42} not enough arguments
SELECT extractAllGroups('hello', 123); --{serverError 43} invalid argument type
SELECT extractAllGroups(123, 'world'); --{serverError 43} invalid argument type
SELECT extractAllGroups('hello world', '((('); --{serverError 427} invalid re
SELECT extractAllGroups('hello world', materialize('\\w+')); --{serverError 44} non-const needle
SELECT '0 groups, zero matches';
SELECT extractAllGroups('hello world', '\\w+'); -- { serverError 36 }
SELECT '1 group, multiple matches, String and FixedString';
SELECT extractAllGroups('hello world', '(\\w+)');
SELECT extractAllGroups('hello world', CAST('(\\w+)' as FixedString(5)));
SELECT extractAllGroups(CAST('hello world' AS FixedString(12)), '(\\w+)');
SELECT extractAllGroups(CAST('hello world' AS FixedString(12)), CAST('(\\w+)' as FixedString(5)));
SELECT extractAllGroups(materialize(CAST('hello world' AS FixedString(12))), '(\\w+)');
SELECT extractAllGroups(materialize(CAST('hello world' AS FixedString(12))), CAST('(\\w+)' as FixedString(5)));
SELECT 'mutiple groups, multiple matches';
SELECT extractAllGroups('abc=111, def=222, ghi=333 "jkl mno"="444 foo bar"', '("[^"]+"|\\w+)=("[^"]+"|\\w+)');
SELECT 'big match';
SELECT
length(haystack), length(matches[1]), length(matches), arrayMap((x) -> length(x), arrayMap(x -> x[1], matches))
FROM (
SELECT
repeat('abcdefghijklmnopqrstuvwxyz', number * 10) AS haystack,
extractAllGroups(haystack, '(abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz)') AS matches
FROM numbers(3)
);
SELECT 'lots of matches';
SELECT
length(haystack), length(matches[1]), length(matches), arrayReduce('sum', arrayMap((x) -> length(x), arrayMap(x -> x[1], matches)))
FROM (
SELECT
repeat('abcdefghijklmnopqrstuvwxyz', number * 10) AS haystack,
extractAllGroups(haystack, '(\\w)') AS matches
FROM numbers(3)
);
SELECT 'lots of groups';
SELECT
length(haystack), length(matches[1]), length(matches), arrayMap((x) -> length(x), arrayMap(x -> x[1], matches))
FROM (
SELECT
repeat('abcdefghijklmnopqrstuvwxyz', number * 10) AS haystack,
extractAllGroups(haystack, repeat('(\\w)', 100)) AS matches
FROM numbers(3)
);

View File

@ -0,0 +1,22 @@
0 groups, zero matches
1 group, multiple matches, String and FixedString
[['hello','world']]
[['hello','world']]
[['hello','world']]
[['hello','world']]
[['hello','world']]
[['hello','world']]
mutiple groups, multiple matches
[['abc','def','ghi','"jkl mno"'],['111','222','333','"444 foo bar"']]
big match
0 1 0 []
260 1 1 [156]
520 1 3 [156,156,156]
lots of matches
0 1 0 0
260 1 260 260
520 1 520 520
lots of groups
0 100 0 []
260 100 2 [1,1]
520 100 5 [1,1,1,1,1]

View File

@ -0,0 +1,51 @@
-- error cases
SELECT extractAllGroupsHorizontal(); --{serverError 42} not enough arguments
SELECT extractAllGroupsHorizontal('hello'); --{serverError 42} not enough arguments
SELECT extractAllGroupsHorizontal('hello', 123); --{serverError 43} invalid argument type
SELECT extractAllGroupsHorizontal(123, 'world'); --{serverError 43} invalid argument type
SELECT extractAllGroupsHorizontal('hello world', '((('); --{serverError 427} invalid re
SELECT extractAllGroupsHorizontal('hello world', materialize('\\w+')); --{serverError 44} non-cons needle
SELECT '0 groups, zero matches';
SELECT extractAllGroupsHorizontal('hello world', '\\w+'); -- { serverError 36 }
SELECT '1 group, multiple matches, String and FixedString';
SELECT extractAllGroupsHorizontal('hello world', '(\\w+)');
SELECT extractAllGroupsHorizontal('hello world', CAST('(\\w+)' as FixedString(5)));
SELECT extractAllGroupsHorizontal(CAST('hello world' AS FixedString(12)), '(\\w+)');
SELECT extractAllGroupsHorizontal(CAST('hello world' AS FixedString(12)), CAST('(\\w+)' as FixedString(5)));
SELECT extractAllGroupsHorizontal(materialize(CAST('hello world' AS FixedString(12))), '(\\w+)');
SELECT extractAllGroupsHorizontal(materialize(CAST('hello world' AS FixedString(12))), CAST('(\\w+)' as FixedString(5)));
SELECT 'mutiple groups, multiple matches';
SELECT extractAllGroupsHorizontal('abc=111, def=222, ghi=333 "jkl mno"="444 foo bar"', '("[^"]+"|\\w+)=("[^"]+"|\\w+)');
SELECT 'big match';
SELECT
length(haystack), length(matches), length(matches[1]), arrayMap((x) -> length(x), matches[1])
FROM (
SELECT
repeat('abcdefghijklmnopqrstuvwxyz', number * 10) AS haystack,
extractAllGroupsHorizontal(haystack, '(abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz)') AS matches
FROM numbers(3)
);
SELECT 'lots of matches';
SELECT
length(haystack), length(matches), length(matches[1]), arrayReduce('sum', arrayMap((x) -> length(x), matches[1]))
FROM (
SELECT
repeat('abcdefghijklmnopqrstuvwxyz', number * 10) AS haystack,
extractAllGroupsHorizontal(haystack, '(\\w)') AS matches
FROM numbers(3)
);
SELECT 'lots of groups';
SELECT
length(haystack), length(matches), length(matches[1]), arrayMap((x) -> length(x), matches[1])
FROM (
SELECT
repeat('abcdefghijklmnopqrstuvwxyz', number * 10) AS haystack,
extractAllGroupsHorizontal(haystack, repeat('(\\w)', 100)) AS matches
FROM numbers(3)
);

View File

@ -0,0 +1,51 @@
-- error cases
SELECT extractAllGroupsVertical(); --{serverError 42} not enough arguments
SELECT extractAllGroupsVertical('hello'); --{serverError 42} not enough arguments
SELECT extractAllGroupsVertical('hello', 123); --{serverError 43} invalid argument type
SELECT extractAllGroupsVertical(123, 'world'); --{serverError 43} invalid argument type
SELECT extractAllGroupsVertical('hello world', '((('); --{serverError 427} invalid re
SELECT extractAllGroupsVertical('hello world', materialize('\\w+')); --{serverError 44} non-const needle
SELECT '0 groups, zero matches';
SELECT extractAllGroupsVertical('hello world', '\\w+'); -- { serverError 36 }
SELECT '1 group, multiple matches, String and FixedString';
SELECT extractAllGroupsVertical('hello world', '(\\w+)');
SELECT extractAllGroupsVertical('hello world', CAST('(\\w+)' as FixedString(5)));
SELECT extractAllGroupsVertical(CAST('hello world' AS FixedString(12)), '(\\w+)');
SELECT extractAllGroupsVertical(CAST('hello world' AS FixedString(12)), CAST('(\\w+)' as FixedString(5)));
SELECT extractAllGroupsVertical(materialize(CAST('hello world' AS FixedString(12))), '(\\w+)');
SELECT extractAllGroupsVertical(materialize(CAST('hello world' AS FixedString(12))), CAST('(\\w+)' as FixedString(5)));
SELECT 'mutiple groups, multiple matches';
SELECT extractAllGroupsVertical('abc=111, def=222, ghi=333 "jkl mno"="444 foo bar"', '("[^"]+"|\\w+)=("[^"]+"|\\w+)');
SELECT 'big match';
SELECT
length(haystack), length(matches[1]), length(matches), arrayMap((x) -> length(x), arrayMap(x -> x[1], matches))
FROM (
SELECT
repeat('abcdefghijklmnopqrstuvwxyz', number * 10) AS haystack,
extractAllGroupsVertical(haystack, '(abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz)') AS matches
FROM numbers(3)
);
SELECT 'lots of matches';
SELECT
length(haystack), length(matches[1]), length(matches), arrayReduce('sum', arrayMap((x) -> length(x), arrayMap(x -> x[1], matches)))
FROM (
SELECT
repeat('abcdefghijklmnopqrstuvwxyz', number * 10) AS haystack,
extractAllGroupsVertical(haystack, '(\\w)') AS matches
FROM numbers(3)
);
SELECT 'lots of groups';
SELECT
length(haystack), length(matches[1]), length(matches), arrayMap((x) -> length(x), arrayMap(x -> x[1], matches))
FROM (
SELECT
repeat('abcdefghijklmnopqrstuvwxyz', number * 10) AS haystack,
extractAllGroupsVertical(haystack, repeat('(\\w)', 100)) AS matches
FROM numbers(3)
);

View File

@ -1,5 +1,5 @@
SELECT extractGroups('hello', ''); -- { serverError 69 }
SELECT extractAllGroups('hello', ''); -- { serverError 69 }
SELECT extractGroups('hello', ''); -- { serverError 36 }
SELECT extractAllGroups('hello', ''); -- { serverError 36 }
SELECT extractGroups('hello', ' '); -- { serverError 36 }
SELECT extractAllGroups('hello', ' '); -- { serverError 36 }

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. $CURDIR/../shell_config.sh
set -e
$CLICKHOUSE_BENCHMARK --iterations 10 --json ${CLICKHOUSE_TMP}/out.json <<< "SELECT 1" 2>/dev/null && cat ${CLICKHOUSE_TMP}/out.json |
$CLICKHOUSE_LOCAL --input-format JSONAsString --structure "s String" --query "SELECT isValidJSON(s) FROM table"

View File

@ -0,0 +1,2 @@
2000 1999000
2000 1999000

View File

@ -0,0 +1,39 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. $CURDIR/../shell_config.sh
set -e
$CLICKHOUSE_CLIENT -n --query "
DROP TABLE IF EXISTS r0;
DROP TABLE IF EXISTS r1;
CREATE TABLE r0 (x UInt64) ENGINE = ReplicatedMergeTree('/test/table', 'r0') ORDER BY x SETTINGS min_bytes_for_wide_part = '10M';
CREATE TABLE r1 (x UInt64) ENGINE = ReplicatedMergeTree('/test/table', 'r1') ORDER BY x SETTINGS min_bytes_for_wide_part = '10M';
"
function thread()
{
REPLICA=$1
ITERATIONS=$2
$CLICKHOUSE_CLIENT --max_block_size 1 --min_insert_block_size_rows 0 --min_insert_block_size_bytes 0 --query "INSERT INTO r$REPLICA SELECT number * 2 + $REPLICA FROM numbers($ITERATIONS)"
}
thread 0 1000 &
thread 1 1000 &
wait
$CLICKHOUSE_CLIENT -n --query "
SYSTEM SYNC REPLICA r0;
SYSTEM SYNC REPLICA r1;
SELECT count(), sum(x) FROM r0;
SELECT count(), sum(x) FROM r1;
DROP TABLE r0;
DROP TABLE r1;
"

View File

@ -8,7 +8,7 @@
# install compiler and libs
sudo apt install -y git bash cmake ninja-build gcc-8 g++-8 libicu-dev libreadline-dev gperf
# for -DUNBUNDLED=1 mode:
#sudo apt install -y libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libboost-thread-dev zlib1g-dev liblz4-dev libdouble-conversion-dev libzstd-dev libre2-dev librdkafka-dev libcapnp-dev libpoco-dev libgoogle-perftools-dev libunwind-dev googletest libcctz-dev
#sudo apt install -y libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libboost-thread-dev libboost-regex-dev libboost-iostreams-dev zlib1g-dev liblz4-dev libdouble-conversion-dev libzstd-dev libre2-dev librdkafka-dev libcapnp-dev libpoco-dev libgoogle-perftools-dev libunwind-dev googletest libcctz-dev
# install testing only stuff if you want:
sudo apt install -y expect python python-lxml python-termcolor python-requests curl perl sudo openssl netcat-openbsd telnet

View File

@ -22,5 +22,5 @@ env TEST_RUN=1 \
`# Use all possible contrib libs from system` \
`# psmisc - killall` \
`# gdb - symbol test in pbuilder` \
EXTRAPACKAGES="psmisc libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libboost-thread-dev libboost-regex-dev zlib1g-dev liblz4-dev libdouble-conversion-dev libsparsehash-dev librdkafka-dev libpoco-dev unixodbc-dev libsparsehash-dev libgoogle-perftools-dev libzstd-dev libre2-dev libunwind-dev googletest libcctz-dev libcapnp-dev libjemalloc-dev libssl-dev libunwind-dev libgsasl7-dev libxml2-dev libbrotli-dev libhyperscan-dev rapidjson-dev $EXTRAPACKAGES" \
EXTRAPACKAGES="psmisc libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libboost-thread-dev libboost-regex-dev libboost-iostreams-dev zlib1g-dev liblz4-dev libdouble-conversion-dev libsparsehash-dev librdkafka-dev libpoco-dev unixodbc-dev libsparsehash-dev libgoogle-perftools-dev libzstd-dev libre2-dev libunwind-dev googletest libcctz-dev libcapnp-dev libjemalloc-dev libssl-dev libunwind-dev libgsasl7-dev libxml2-dev libbrotli-dev libhyperscan-dev rapidjson-dev $EXTRAPACKAGES" \
pdebuild --configfile $ROOT_DIR/debian/.pbuilderrc $PDEBUILD_OPT

View File

@ -18,5 +18,5 @@ unzip -ou ch.zip
# TODO: USE_INTERNAL_DOUBLE_CONVERSION_LIBRARY : cmake test
# Shared because /usr/bin/ld.gold: error: /usr/lib/x86_64-linux-gnu/libcrypto.a(err.o): multiple definition of 'ERR_remove_thread_state'
CMAKE_FLAGS+="-DUSE_STATIC_LIBRARIES=0 -DUSE_INTERNAL_DOUBLE_CONVERSION_LIBRARY=0 $CMAKE_FLAGS"
EXTRAPACKAGES+="libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libboost-thread-dev libboost-regex-dev liblz4-dev libzstd-dev libpoco-dev libdouble-conversion-dev libcctz-dev libre2-dev libsparsehash-dev $EXTRAPACKAGES"
EXTRAPACKAGES+="libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libboost-thread-dev libboost-iostreams-dev libboost-regex-dev liblz4-dev libzstd-dev libpoco-dev libdouble-conversion-dev libcctz-dev libre2-dev libsparsehash-dev $EXTRAPACKAGES"
. $ROOT_DIR/ClickHouse-${BRANCH}/release

View File

@ -45,7 +45,7 @@ try
}
catch (const Coordination::Exception & e)
{
if (e.code == Coordination::ZNONODE)
if (e.code == Coordination::Error::ZNONODE)
continue;
throw;
}