Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2dot5
ClickHouse
提交
f58d470d
C
ClickHouse
项目概览
2dot5
/
ClickHouse
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
ClickHouse
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
f58d470d
编写于
3月 25, 2021
作者:
A
Anton Popov
提交者:
GitHub
3月 25, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #21830 from foolchi/optimize_select_with_final
optimize select final with prewhere primary key
上级
f6f739ec
57c6ebc8
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
68 addition
and
13 deletion
+68
-13
src/Interpreters/InterpreterSelectQuery.cpp
src/Interpreters/InterpreterSelectQuery.cpp
+1
-1
src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp
src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp
+18
-9
src/Storages/MergeTree/MergeTreeWhereOptimizer.h
src/Storages/MergeTree/MergeTreeWhereOptimizer.h
+6
-3
tests/queries/0_stateless/01737_move_order_key_to_prewhere_select_final.reference
...s/01737_move_order_key_to_prewhere_select_final.reference
+28
-0
tests/queries/0_stateless/01737_move_order_key_to_prewhere_select_final.sql
...ateless/01737_move_order_key_to_prewhere_select_final.sql
+15
-0
未找到文件。
src/Interpreters/InterpreterSelectQuery.cpp
浏览文件 @
f58d470d
...
...
@@ -393,7 +393,7 @@ InterpreterSelectQuery::InterpreterSelectQuery(
view
=
nullptr
;
}
if
(
try_move_to_prewhere
&&
storage
&&
query
.
where
()
&&
!
query
.
prewhere
()
&&
!
query
.
final
()
)
if
(
try_move_to_prewhere
&&
storage
&&
query
.
where
()
&&
!
query
.
prewhere
())
{
/// PREWHERE optimization: transfer some condition from WHERE to PREWHERE if enabled and viable
if
(
const
auto
&
column_sizes
=
storage
->
getColumnSizes
();
!
column_sizes
.
empty
())
...
...
src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp
浏览文件 @
f58d470d
...
...
@@ -37,6 +37,8 @@ MergeTreeWhereOptimizer::MergeTreeWhereOptimizer(
:
table_columns
{
ext
::
map
<
std
::
unordered_set
>
(
metadata_snapshot
->
getColumns
().
getAllPhysical
(),
[](
const
NameAndTypePair
&
col
)
{
return
col
.
name
;
})}
,
queried_columns
{
queried_columns_
}
,
sorting_key_names
{
NameSet
(
metadata_snapshot
->
getSortingKey
().
column_names
.
begin
(),
metadata_snapshot
->
getSortingKey
().
column_names
.
end
())}
,
block_with_constants
{
KeyCondition
::
getBlockWithConstants
(
query_info
.
query
,
query_info
.
syntax_analyzer_result
,
context
)}
,
log
{
log_
}
,
column_sizes
{
std
::
move
(
column_sizes_
)}
...
...
@@ -114,12 +116,12 @@ static bool isConditionGood(const ASTPtr & condition)
}
void
MergeTreeWhereOptimizer
::
analyzeImpl
(
Conditions
&
res
,
const
ASTPtr
&
node
)
const
void
MergeTreeWhereOptimizer
::
analyzeImpl
(
Conditions
&
res
,
const
ASTPtr
&
node
,
bool
is_final
)
const
{
if
(
const
auto
*
func_and
=
node
->
as
<
ASTFunction
>
();
func_and
&&
func_and
->
name
==
"and"
)
{
for
(
const
auto
&
elem
:
func_and
->
arguments
->
children
)
analyzeImpl
(
res
,
elem
);
analyzeImpl
(
res
,
elem
,
is_final
);
}
else
{
...
...
@@ -133,7 +135,7 @@ void MergeTreeWhereOptimizer::analyzeImpl(Conditions & res, const ASTPtr & node)
cond
.
viable
=
/// Condition depend on some column. Constant expressions are not moved.
!
cond
.
identifiers
.
empty
()
&&
!
cannotBeMoved
(
node
)
&&
!
cannotBeMoved
(
node
,
is_final
)
/// Do not take into consideration the conditions consisting only of the first primary key column
&&
!
hasPrimaryKeyAtoms
(
node
)
/// Only table columns are considered. Not array joined columns. NOTE We're assuming that aliases was expanded.
...
...
@@ -149,10 +151,10 @@ void MergeTreeWhereOptimizer::analyzeImpl(Conditions & res, const ASTPtr & node)
}
/// Transform conjunctions chain in WHERE expression to Conditions list.
MergeTreeWhereOptimizer
::
Conditions
MergeTreeWhereOptimizer
::
analyze
(
const
ASTPtr
&
expression
)
const
MergeTreeWhereOptimizer
::
Conditions
MergeTreeWhereOptimizer
::
analyze
(
const
ASTPtr
&
expression
,
bool
is_final
)
const
{
Conditions
res
;
analyzeImpl
(
res
,
expression
);
analyzeImpl
(
res
,
expression
,
is_final
);
return
res
;
}
...
...
@@ -183,7 +185,7 @@ void MergeTreeWhereOptimizer::optimize(ASTSelectQuery & select) const
if
(
!
select
.
where
()
||
select
.
prewhere
())
return
;
Conditions
where_conditions
=
analyze
(
select
.
where
());
Conditions
where_conditions
=
analyze
(
select
.
where
()
,
select
.
final
()
);
Conditions
prewhere_conditions
;
UInt64
total_size_of_moved_conditions
=
0
;
...
...
@@ -300,6 +302,12 @@ bool MergeTreeWhereOptimizer::isPrimaryKeyAtom(const ASTPtr & ast) const
}
bool
MergeTreeWhereOptimizer
::
isSortingKey
(
const
String
&
column_name
)
const
{
return
sorting_key_names
.
count
(
column_name
);
}
bool
MergeTreeWhereOptimizer
::
isConstant
(
const
ASTPtr
&
expr
)
const
{
const
auto
column_name
=
expr
->
getColumnName
();
...
...
@@ -319,7 +327,7 @@ bool MergeTreeWhereOptimizer::isSubsetOfTableColumns(const NameSet & identifiers
}
bool
MergeTreeWhereOptimizer
::
cannotBeMoved
(
const
ASTPtr
&
ptr
)
const
bool
MergeTreeWhereOptimizer
::
cannotBeMoved
(
const
ASTPtr
&
ptr
,
bool
is_final
)
const
{
if
(
const
auto
*
function_ptr
=
ptr
->
as
<
ASTFunction
>
())
{
...
...
@@ -336,12 +344,13 @@ bool MergeTreeWhereOptimizer::cannotBeMoved(const ASTPtr & ptr) const
{
/// disallow moving result of ARRAY JOIN to PREWHERE
if
(
array_joined_names
.
count
(
*
opt_name
)
||
array_joined_names
.
count
(
Nested
::
extractTableName
(
*
opt_name
)))
array_joined_names
.
count
(
Nested
::
extractTableName
(
*
opt_name
))
||
(
is_final
&&
!
isSortingKey
(
*
opt_name
)))
return
true
;
}
for
(
const
auto
&
child
:
ptr
->
children
)
if
(
cannotBeMoved
(
child
))
if
(
cannotBeMoved
(
child
,
is_final
))
return
true
;
return
false
;
...
...
src/Storages/MergeTree/MergeTreeWhereOptimizer.h
浏览文件 @
f58d470d
...
...
@@ -67,10 +67,10 @@ private:
using
Conditions
=
std
::
list
<
Condition
>
;
void
analyzeImpl
(
Conditions
&
res
,
const
ASTPtr
&
node
)
const
;
void
analyzeImpl
(
Conditions
&
res
,
const
ASTPtr
&
node
,
bool
is_final
)
const
;
/// Transform conjunctions chain in WHERE expression to Conditions list.
Conditions
analyze
(
const
ASTPtr
&
expression
)
const
;
Conditions
analyze
(
const
ASTPtr
&
expression
,
bool
is_final
)
const
;
/// Transform Conditions list to WHERE or PREWHERE expression.
static
ASTPtr
reconstruct
(
const
Conditions
&
conditions
);
...
...
@@ -85,6 +85,8 @@ private:
bool
isPrimaryKeyAtom
(
const
ASTPtr
&
ast
)
const
;
bool
isSortingKey
(
const
String
&
column_name
)
const
;
bool
isConstant
(
const
ASTPtr
&
expr
)
const
;
bool
isSubsetOfTableColumns
(
const
NameSet
&
identifiers
)
const
;
...
...
@@ -95,7 +97,7 @@ private:
*
* Also, disallow moving expressions with GLOBAL [NOT] IN.
*/
bool
cannotBeMoved
(
const
ASTPtr
&
ptr
)
const
;
bool
cannotBeMoved
(
const
ASTPtr
&
ptr
,
bool
is_final
)
const
;
void
determineArrayJoinedNames
(
ASTSelectQuery
&
select
);
...
...
@@ -104,6 +106,7 @@ private:
String
first_primary_key_column
;
const
StringSet
table_columns
;
const
Names
queried_columns
;
const
NameSet
sorting_key_names
;
const
Block
block_with_constants
;
Poco
::
Logger
*
log
;
std
::
unordered_map
<
std
::
string
,
UInt64
>
column_sizes
;
...
...
tests/queries/0_stateless/01737_move_order_key_to_prewhere_select_final.reference
0 → 100644
浏览文件 @
f58d470d
SELECT
x,
y,
z
FROM prewhere_move_select_final
PREWHERE y > 100
SELECT
x,
y,
z
FROM prewhere_move_select_final
FINAL
PREWHERE y > 100
SELECT
x,
y,
z
FROM prewhere_move_select_final
FINAL
WHERE z > 400
SELECT
x,
y,
z
FROM prewhere_move_select_final
FINAL
PREWHERE y > 100
WHERE (y > 100) AND (z > 400)
tests/queries/0_stateless/01737_move_order_key_to_prewhere_select_final.sql
0 → 100644
浏览文件 @
f58d470d
DROP
TABLE
IF
EXISTS
prewhere_move_select_final
;
CREATE
TABLE
prewhere_move_select_final
(
x
Int
,
y
Int
,
z
Int
)
ENGINE
=
ReplacingMergeTree
()
ORDER
BY
(
x
,
y
);
INSERT
INTO
prewhere_move_select_final
SELECT
number
,
number
*
2
,
number
*
3
FROM
numbers
(
1000
);
-- order key can be pushed down with final
EXPLAIN
SYNTAX
SELECT
*
FROM
prewhere_move_select_final
WHERE
y
>
100
;
EXPLAIN
SYNTAX
SELECT
*
FROM
prewhere_move_select_final
FINAL
WHERE
y
>
100
;
-- can not be pushed down
EXPLAIN
SYNTAX
SELECT
*
FROM
prewhere_move_select_final
FINAL
WHERE
z
>
400
;
-- only y can be pushed down
EXPLAIN
SYNTAX
SELECT
*
FROM
prewhere_move_select_final
FINAL
WHERE
y
>
100
and
z
>
400
;
DROP
TABLE
prewhere_move_select_final
;
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录