Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2dot5
ClickHouse
提交
84a89b3b
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,发现更多精彩内容 >>
提交
84a89b3b
编写于
11月 24, 2016
作者:
P
Pavel Kartavyy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ExpressionAnalyzer: optimize if with constant condition [#METR-23545]
上级
3569176c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
88 addition
and
0 deletion
+88
-0
dbms/include/DB/Interpreters/ExpressionAnalyzer.h
dbms/include/DB/Interpreters/ExpressionAnalyzer.h
+5
-0
dbms/src/Interpreters/ExpressionAnalyzer.cpp
dbms/src/Interpreters/ExpressionAnalyzer.cpp
+74
-0
dbms/tests/queries/0_stateless/00393_if_with_constant_condition.reference
...es/0_stateless/00393_if_with_constant_condition.reference
+4
-0
dbms/tests/queries/0_stateless/00393_if_with_constant_condition.sql
.../queries/0_stateless/00393_if_with_constant_condition.sql
+5
-0
未找到文件。
dbms/include/DB/Interpreters/ExpressionAnalyzer.h
浏览文件 @
84a89b3b
...
...
@@ -230,6 +230,11 @@ private:
/// Удалить из ORDER BY повторяющиеся элементы.
void
optimizeOrderBy
();
/// remove Function_if AST if condition is constant
void
optimizeIfWithConstantCondition
();
void
optimizeIfWithConstantConditionImpl
(
ASTPtr
&
current_ast
)
const
;
bool
tryExtractConstValueFromCondition
(
const
ASTPtr
&
condition
,
bool
&
value
)
const
;
/// Превратить перечисление значений или подзапрос в ASTSet. node - функция in или notIn.
void
makeSet
(
ASTFunction
*
node
,
const
Block
&
sample_block
);
...
...
dbms/src/Interpreters/ExpressionAnalyzer.cpp
浏览文件 @
84a89b3b
...
...
@@ -169,6 +169,9 @@ void ExpressionAnalyzer::init()
/// Выполнение скалярных подзапросов - замена их на значения-константы.
executeScalarSubqueries
();
/// Optimize if with constant condition after constats are substituted instead of sclalar subqueries
optimizeIfWithConstantCondition
();
/// GROUP BY injective function elimination.
optimizeGroupBy
();
...
...
@@ -195,6 +198,77 @@ void ExpressionAnalyzer::init()
analyzeAggregation
();
}
void
ExpressionAnalyzer
::
optimizeIfWithConstantCondition
()
{
optimizeIfWithConstantConditionImpl
(
ast
);
}
bool
ExpressionAnalyzer
::
tryExtractConstValueFromCondition
(
const
ASTPtr
&
condition
,
bool
&
value
)
const
{
/// numeric constant in condition
if
(
const
ASTLiteral
*
literal
=
typeid_cast
<
ASTLiteral
*>
(
condition
.
get
()))
{
if
(
literal
->
value
.
getType
()
==
Field
::
Types
::
Int64
||
literal
->
value
.
getType
()
==
Field
::
Types
::
UInt64
)
{
value
=
literal
->
value
.
get
<
Int64
>
();
return
true
;
}
}
/// cast of numeric constant in condition to UInt8
if
(
const
ASTFunction
*
function
=
typeid_cast
<
ASTFunction
*
>
(
condition
.
get
()))
{
if
(
function
->
name
==
FunctionCast
::
name
)
{
if
(
ASTExpressionList
*
expr_list
=
typeid_cast
<
ASTExpressionList
*>
(
function
->
arguments
.
get
()))
{
const
ASTPtr
&
type_ast
=
expr_list
->
children
.
at
(
1
);
if
(
const
ASTLiteral
*
type_literal
=
typeid_cast
<
ASTLiteral
*>
(
type_ast
.
get
()))
{
if
(
type_literal
->
value
.
getType
()
==
Field
::
Types
::
String
&&
type_literal
->
value
.
get
<
std
::
string
>
()
==
"UInt8"
)
return
tryExtractConstValueFromCondition
(
expr_list
->
children
.
at
(
0
),
value
);
}
}
}
}
return
false
;
}
void
ExpressionAnalyzer
::
optimizeIfWithConstantConditionImpl
(
ASTPtr
&
current_ast
)
const
{
if
(
!
current_ast
)
return
;
for
(
ASTPtr
&
child
:
current_ast
->
children
)
{
ASTFunction
*
function_node
=
typeid_cast
<
ASTFunction
*>
(
child
.
get
());
if
(
!
function_node
||
function_node
->
name
!=
FunctionIf
::
name
)
{
optimizeIfWithConstantConditionImpl
(
child
);
continue
;
}
optimizeIfWithConstantConditionImpl
(
function_node
->
arguments
);
ASTExpressionList
*
args
=
typeid_cast
<
ASTExpressionList
*>
(
function_node
->
arguments
.
get
());
ASTPtr
condition_expr
=
args
->
children
.
at
(
0
);
ASTPtr
then_expr
=
args
->
children
.
at
(
1
);
ASTPtr
else_expr
=
args
->
children
.
at
(
2
);
bool
condition
;
if
(
tryExtractConstValueFromCondition
(
condition_expr
,
condition
))
{
if
(
condition
)
child
=
then_expr
;
else
child
=
else_expr
;
}
}
}
void
ExpressionAnalyzer
::
analyzeAggregation
()
{
...
...
dbms/tests/queries/0_stateless/00393_if_with_constant_condition.reference
0 → 100644
浏览文件 @
84a89b3b
1
1
2
42
dbms/tests/queries/0_stateless/00393_if_with_constant_condition.sql
0 → 100644
浏览文件 @
84a89b3b
SELECT
1
?
1
:
0
;
SELECT
0
?
not_existing_column
:
1
FROM
system
.
numbers
LIMIT
1
;
SELECT
if
(
1
,
if
(
0
,
not_existing_column
,
2
),
0
)
FROM
system
.
numbers
LIMIT
1
;
SELECT
(
SELECT
hasColumnInTable
(
'system'
,
'numbers'
,
'not_existing'
))
?
not_existing
:
42
FROM
system
.
numbers
LIMIT
1
;
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录