Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2dot5
ClickHouse
提交
9b40f384
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 搜索 >>
未验证
提交
9b40f384
编写于
4月 02, 2021
作者:
V
Vladimir
提交者:
GitHub
4月 02, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #20622 from vdimir/issue-20497
上级
3b799c5d
03537750
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
39 addition
and
15 deletion
+39
-15
src/Interpreters/IdentifierSemantic.cpp
src/Interpreters/IdentifierSemantic.cpp
+1
-1
src/Interpreters/PredicateExpressionsOptimizer.cpp
src/Interpreters/PredicateExpressionsOptimizer.cpp
+3
-3
src/Interpreters/PredicateExpressionsOptimizer.h
src/Interpreters/PredicateExpressionsOptimizer.h
+2
-1
src/Interpreters/PredicateRewriteVisitor.cpp
src/Interpreters/PredicateRewriteVisitor.cpp
+12
-7
src/Interpreters/PredicateRewriteVisitor.h
src/Interpreters/PredicateRewriteVisitor.h
+5
-3
tests/queries/0_stateless/00597_push_down_predicate.reference
...s/queries/0_stateless/00597_push_down_predicate.reference
+12
-0
tests/queries/0_stateless/00597_push_down_predicate.sql
tests/queries/0_stateless/00597_push_down_predicate.sql
+4
-0
未找到文件。
src/Interpreters/IdentifierSemantic.cpp
浏览文件 @
9b40f384
...
...
@@ -209,7 +209,7 @@ IdentifierSemantic::ColumnMatch IdentifierSemantic::canReferColumnToTable(const
return
canReferColumnToTable
(
identifier
,
table_with_columns
.
table
);
}
/// Strip qualificat
or
s from left side of column name.
/// Strip qualificat
ion
s from left side of column name.
/// Example: 'database.table.name' -> 'name'.
void
IdentifierSemantic
::
setColumnShortName
(
ASTIdentifier
&
identifier
,
const
DatabaseAndTableWithAlias
&
db_and_table
)
{
...
...
src/Interpreters/PredicateExpressionsOptimizer.cpp
浏览文件 @
9b40f384
...
...
@@ -146,7 +146,7 @@ bool PredicateExpressionsOptimizer::tryRewritePredicatesToTables(ASTs & tables_e
break
;
/// Skip left and right table optimization
is_rewrite_tables
|=
tryRewritePredicatesToTable
(
tables_element
[
table_pos
],
tables_predicates
[
table_pos
],
tables_with_columns
[
table_pos
]
.
columns
.
getNames
()
);
tables_with_columns
[
table_pos
]);
if
(
table_element
->
table_join
&&
isRight
(
table_element
->
table_join
->
as
<
ASTTableJoin
>
()
->
kind
))
break
;
/// Skip left table optimization
...
...
@@ -156,13 +156,13 @@ bool PredicateExpressionsOptimizer::tryRewritePredicatesToTables(ASTs & tables_e
return
is_rewrite_tables
;
}
bool
PredicateExpressionsOptimizer
::
tryRewritePredicatesToTable
(
ASTPtr
&
table_element
,
const
ASTs
&
table_predicates
,
Names
&
&
table_columns
)
const
bool
PredicateExpressionsOptimizer
::
tryRewritePredicatesToTable
(
ASTPtr
&
table_element
,
const
ASTs
&
table_predicates
,
const
TableWithColumnNamesAndTypes
&
table_columns
)
const
{
if
(
!
table_predicates
.
empty
())
{
auto
optimize_final
=
enable_optimize_predicate_expression_to_final_subquery
;
auto
optimize_with
=
allow_push_predicate_when_subquery_contains_with
;
PredicateRewriteVisitor
::
Data
data
(
context
,
table_predicates
,
std
::
move
(
table_columns
)
,
optimize_final
,
optimize_with
);
PredicateRewriteVisitor
::
Data
data
(
context
,
table_predicates
,
table_columns
,
optimize_final
,
optimize_with
);
PredicateRewriteVisitor
(
data
).
visit
(
table_element
);
return
data
.
is_rewrite
;
...
...
src/Interpreters/PredicateExpressionsOptimizer.h
浏览文件 @
9b40f384
...
...
@@ -33,7 +33,8 @@ private:
bool
tryRewritePredicatesToTables
(
ASTs
&
tables_element
,
const
std
::
vector
<
ASTs
>
&
tables_predicates
);
bool
tryRewritePredicatesToTable
(
ASTPtr
&
table_element
,
const
ASTs
&
table_predicates
,
Names
&&
table_columns
)
const
;
bool
tryRewritePredicatesToTable
(
ASTPtr
&
table_element
,
const
ASTs
&
table_predicates
,
const
TableWithColumnNamesAndTypes
&
table_columns
)
const
;
bool
tryMovePredicatesFromHavingToWhere
(
ASTSelectQuery
&
select_query
);
};
...
...
src/Interpreters/PredicateRewriteVisitor.cpp
浏览文件 @
9b40f384
...
...
@@ -17,8 +17,8 @@ namespace DB
{
PredicateRewriteVisitorData
::
PredicateRewriteVisitorData
(
const
Context
&
context_
,
const
ASTs
&
predicates_
,
Names
&&
column_name
s_
,
bool
optimize_final_
,
bool
optimize_with_
)
:
context
(
context_
),
predicates
(
predicates_
),
column_names
(
column_name
s_
),
optimize_final
(
optimize_final_
),
optimize_with
(
optimize_with_
)
const
Context
&
context_
,
const
ASTs
&
predicates_
,
const
TableWithColumnNamesAndTypes
&
table_column
s_
,
bool
optimize_final_
,
bool
optimize_with_
)
:
context
(
context_
),
predicates
(
predicates_
),
table_columns
(
table_column
s_
),
optimize_final
(
optimize_final_
),
optimize_with
(
optimize_with_
)
{
}
...
...
@@ -42,7 +42,8 @@ void PredicateRewriteVisitorData::visit(ASTSelectWithUnionQuery & union_select_q
void
PredicateRewriteVisitorData
::
visitFirstInternalSelect
(
ASTSelectQuery
&
select_query
,
ASTPtr
&
)
{
is_rewrite
|=
rewriteSubquery
(
select_query
,
column_names
,
column_names
);
/// In this case inner_columns same as outer_columns from table_columns
is_rewrite
|=
rewriteSubquery
(
select_query
,
table_columns
.
columns
.
getNames
());
}
void
PredicateRewriteVisitorData
::
visitOtherInternalSelect
(
ASTSelectQuery
&
select_query
,
ASTPtr
&
)
...
...
@@ -65,7 +66,7 @@ void PredicateRewriteVisitorData::visitOtherInternalSelect(ASTSelectQuery & sele
const
Names
&
internal_columns
=
InterpreterSelectQuery
(
temp_internal_select
,
context
,
SelectQueryOptions
().
analyze
()).
getSampleBlock
().
getNames
();
if
(
rewriteSubquery
(
*
temp_select_query
,
column_names
,
internal_columns
))
if
(
rewriteSubquery
(
*
temp_select_query
,
internal_columns
))
{
is_rewrite
|=
true
;
select_query
.
setExpression
(
ASTSelectQuery
::
Expression
::
SELECT
,
std
::
move
(
temp_select_query
->
refSelect
()));
...
...
@@ -89,7 +90,7 @@ static void cleanAliasAndCollectIdentifiers(ASTPtr & predicate, std::vector<ASTI
identifiers
.
emplace_back
(
identifier
);
}
bool
PredicateRewriteVisitorData
::
rewriteSubquery
(
ASTSelectQuery
&
subquery
,
const
Names
&
outer_columns
,
const
Names
&
inner_columns
)
bool
PredicateRewriteVisitorData
::
rewriteSubquery
(
ASTSelectQuery
&
subquery
,
const
Names
&
inner_columns
)
{
if
((
!
optimize_final
&&
subquery
.
final
())
||
(
!
optimize_with
&&
subquery
.
with
())
...
...
@@ -98,6 +99,7 @@ bool PredicateRewriteVisitorData::rewriteSubquery(ASTSelectQuery & subquery, con
||
hasNonRewritableFunction
(
subquery
.
select
(),
context
))
return
false
;
Names
outer_columns
=
table_columns
.
columns
.
getNames
();
for
(
const
auto
&
predicate
:
predicates
)
{
std
::
vector
<
ASTIdentifier
*>
identifiers
;
...
...
@@ -106,13 +108,16 @@ bool PredicateRewriteVisitorData::rewriteSubquery(ASTSelectQuery & subquery, con
for
(
const
auto
&
identifier
:
identifiers
)
{
const
auto
&
column_name
=
identifier
->
shortName
(
);
const
auto
&
outer_column_iterator
=
std
::
find
(
outer_columns
.
begin
(),
outer_columns
.
end
(),
column_name
);
IdentifierSemantic
::
setColumnShortName
(
*
identifier
,
table_columns
.
table
);
const
auto
&
column_name
=
identifier
->
name
(
);
/// For lambda functions, we can't always find them in the list of columns
/// For example: SELECT * FROM system.one WHERE arrayMap(x -> x, [dummy]) = [0]
const
auto
&
outer_column_iterator
=
std
::
find
(
outer_columns
.
begin
(),
outer_columns
.
end
(),
column_name
);
if
(
outer_column_iterator
!=
outer_columns
.
end
())
{
identifier
->
setShortName
(
inner_columns
[
outer_column_iterator
-
outer_columns
.
begin
()]);
}
}
/// We only need to push all the predicates to subquery having
...
...
src/Interpreters/PredicateRewriteVisitor.h
浏览文件 @
9b40f384
...
...
@@ -4,6 +4,7 @@
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Interpreters/InDepthNodeVisitor.h>
#include <Interpreters/DatabaseAndTableWithAlias.h>
namespace
DB
{
...
...
@@ -24,12 +25,13 @@ public:
return
true
;
}
PredicateRewriteVisitorData
(
const
Context
&
context_
,
const
ASTs
&
predicates_
,
Names
&&
column_names_
,
bool
optimize_final_
,
bool
optimize_with_
);
PredicateRewriteVisitorData
(
const
Context
&
context_
,
const
ASTs
&
predicates_
,
const
TableWithColumnNamesAndTypes
&
table_columns_
,
bool
optimize_final_
,
bool
optimize_with_
);
private:
const
Context
&
context
;
const
ASTs
&
predicates
;
const
Names
column_name
s
;
const
TableWithColumnNamesAndTypes
&
table_column
s
;
bool
optimize_final
;
bool
optimize_with
;
...
...
@@ -37,7 +39,7 @@ private:
void
visitOtherInternalSelect
(
ASTSelectQuery
&
select_query
,
ASTPtr
&
);
bool
rewriteSubquery
(
ASTSelectQuery
&
subquery
,
const
Names
&
outer_columns
,
const
Names
&
inner_columns
);
bool
rewriteSubquery
(
ASTSelectQuery
&
subquery
,
const
Names
&
inner_columns
);
};
using
PredicateRewriteMatcher
=
OneTypeMatcher
<
PredicateRewriteVisitorData
,
PredicateRewriteVisitorData
::
needChild
>
;
...
...
tests/queries/0_stateless/00597_push_down_predicate.reference
浏览文件 @
9b40f384
...
...
@@ -585,3 +585,15 @@ SEMI LEFT JOIN
) AS r USING (id)
WHERE r.id = 1
2000-01-01 1 test string 1 1 2000-01-01 test string 1 1
SELECT value + t1.value AS expr
FROM
(
SELECT
value,
t1.value
FROM test_00597 AS t0
ALL FULL OUTER JOIN test_00597 AS t1 USING (date)
WHERE (value + `t1.value`) < 3
)
WHERE expr < 3
2
tests/queries/0_stateless/00597_push_down_predicate.sql
浏览文件 @
9b40f384
...
...
@@ -135,5 +135,9 @@ SELECT * FROM (SELECT * FROM (SELECT * FROM test_00597) AS a ANY LEFT JOIN (SELE
EXPLAIN
SYNTAX
SELECT
*
FROM
(
SELECT
*
FROM
test_00597
)
ANY
INNER
JOIN
(
SELECT
*
FROM
(
SELECT
*
FROM
test_00597
))
as
r
USING
id
WHERE
r
.
id
=
1
;
SELECT
*
FROM
(
SELECT
*
FROM
test_00597
)
ANY
INNER
JOIN
(
SELECT
*
FROM
(
SELECT
*
FROM
test_00597
))
as
r
USING
id
WHERE
r
.
id
=
1
;
-- issue 20497
EXPLAIN
SYNTAX
SELECT
value
+
t1
.
value
AS
expr
FROM
(
SELECT
t0
.
value
,
t1
.
value
FROM
test_00597
AS
t0
FULL
JOIN
test_00597
AS
t1
USING
date
)
WHERE
expr
<
3
;
SELECT
value
+
t1
.
value
AS
expr
FROM
(
SELECT
t0
.
value
,
t1
.
value
FROM
test_00597
AS
t0
FULL
JOIN
test_00597
AS
t1
USING
date
)
WHERE
expr
<
3
;
DROP
TABLE
IF
EXISTS
test_00597
;
DROP
TABLE
IF
EXISTS
test_view_00597
;
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录