程序代码说明文档.docx
《程序代码说明文档.docx》由会员分享,可在线阅读,更多相关《程序代码说明文档.docx(39页珍藏版)》请在冰豆网上搜索。
程序代码说明文档
clauses.c源码分析
作者:
审阅者:
修改日期:
2005年5月20日
clauses.c源码分析1
1.文件简介2
2.基本概念2
3.数据结构3
4.函数和宏3
4.1.函数分类3
4.2.接口说明4
4.2.1.make_opclause4
4.2.2.get_leftop5
4.2.3.get_rightop5
4.2.4.not_clause5
4.2.5.make_notclause5
4.2.6.get_notclausearg5
4.2.7.or_clause6
4.2.8.make_orclause6
4.2.9.and_clause6
4.2.10.make_andclause6
4.2.11.make_and_qual6
4.2.12.make_ands_explicit7
4.2.13.make_ands_implicit7
4.2.14.contain_agg_clause7
4.2.15.contain_agg_clause_walker7
4.2.16.count_agg_clauses7
4.2.17.count_agg_clauses_walker8
4.2.18.expression_returns_set8
4.2.19.expression_returns_set_walker8
4.2.20.contain_subplans8
4.2.21.contain_subplans_walker8
4.2.22.contain_mutable_functions9
4.2.23.contain_mutable_functions_walker9
4.2.24.contain_volatile_functions9
4.2.25.contain_volatile_functions_walker9
4.2.26.contain_nonstrict_functions9
4.2.27.contain_nonstrict_functions_walker10
4.2.28.is_pseudo_constant_clause10
4.2.29.is_pseudo_constant_clause_relids10
4.2.30.pull_constant_clauses10
4.2.31.has_distinct_on_clause10
4.2.32.has_distinct_clause11
4.2.33.NumRelids11
4.2.34.CommuteClause11
4.2.35.strip_implicit_coercions11
4.2.36.set_coercionform_dontcare11
4.2.37.set_coercionform_dontcare_walker12
4.2.38.rowtype_field_matches12
4.2.39.eval_const_expressions12
4.2.40.estimate_expression_value12
4.2.41.eval_const_expressions_mutator12
4.2.42.simplify_or_arguments13
4.2.43.simplify_and_arguments13
4.2.44.simplify_function13
4.2.45.evaluate_function13
4.2.46.inline_function14
4.2.47.substitute_actual_parameters14
4.2.48.substitute_actual_parameters_mutator14
4.2.49.sql_inline_error_callback14
4.2.50.evaluate_expr14
4.2.51.expression_tree_walker15
4.2.52.query_tree_walker15
4.2.53.expression_tree_mutator15
4.2.54.query_tree_mutator16
4.2.55.query_or_expression_tree_walker17
4.2.56.query_or_expression_tree_mutator17
4.3.算法/流程17
5.遗留问题18
5.1.几点说明18
1.文件简介
(该文件解决了什么问题?
如何解决?
)
(在PG帮助手册文档的什么地方还可以找到相关资料?
并做好到相关文件的链接链接(不要修改帮助手册文档))
源文件:
src\backend\optimizer\util\clauses.c
2.基本概念
说明整个文件涉及的基本概念、术语和总体思想,尤其是在涉及数据结构操作或算法时。
这一部分是文件的核心。
包括一些重要的缩写?
node:
节点,代码中非常宽泛的观念,包括primnodes,parsenodes,plannodes,execodes等多种类型,详细见include\nodes.h
subnode:
子节点,node中包含的其它节点(不严格的观念)。
mutator:
转换节点,一般是在节点的拷贝上进行转换。
walker:
遍历节点,判断是否有节点满足某种条件。
一旦满足,立刻返回true。
这与mutator不同。
context.sublevels_up:
当前查询中变量引用的范围表所在的层次,在同一层,则为0,否则大于0(即在父级查询中)。
select*fromA
whereexists(select*fromBwhereB.b1=A.a1)
在子查询中变量a1引用了父级查询中的范围表A,所以sublevels_up=1
inline:
内联函数
关于数据类型的转换
CREATECAST(sourcetypeAStargettype)
WITHFUNCTIONfuncname(argtypes)
[ASASSIGNMENT|ASIMPLICIT]
CREATECAST(sourcetypeAStargettype)
WITHOUTFUNCTION
[ASASSIGNMENT|ASIMPLICIT]
WITHOUTFUNCTION
表示源数据类型和目标数据类型是二进制兼容的,所以不需要什么函数来执行转换。
ASASSIGNMENT
表示转换可以在赋值环境里隐含调用。
ASIMPLICIT
表示这个转换可以在任何环境里隐含调用。
binary-compatible:
二进制兼容
RelabelType:
类型标记,二进制兼容数据类型之间的隐式转换,执行前通过这些标记确定当前语句环境下某表达式(比如变量)的实际数据类型。
CaseExpr:
分支判断,支持以下两种语法
CASEWHENboolexprTHENexpr[WHENboolexprTHENexpr...]
CASEtestexprWHENcompexprTHENexpr[WHENcompexprTHENexpr...]
ArrayExpr-anARRAY[]expression
COALESCE(value[,...])COALESCE返回它的第一个非NULL的参数的值
FieldSelect:
字段选择,从rowtype类型的元组中抽取一个字段值
3.数据结构
列出文件中利用到的主要的全局变量、常量和数据结构、,并对数据结构在中各字段的含义及使用时的注意点进行说明。
4.函数和宏
第一部分
例子(包括外部函数和主要内部函数,最好分类)
第二部分
外部函数的接口说明(按接口说明的形式写,参照jdk),使用场景说明
第三部分
重要函数的说明(包括外部和内部函数,内容比如为算法\流程)
4.1.函数分类
函数名称
功能简介
make_opclause
get_leftop
get_rightop
not_clause
make_notclause
get_notclausearg
or_clause
make_orclause
and_clause
make_andclause
make_and_qual
make_ands_explicit
make_ands_implicit
contain_agg_clause
contain_agg_clause_walker
count_agg_clauses
count_agg_clauses_walker
expression_returns_set
expression_returns_set_walker
contain_subplans
contain_subplans_walker
contain_mutable_functions
contain_mutable_functions_walker
contain_volatile_functions
contain_volatile_functions_walker
contain_nonstrict_functions
contain_nonstrict_functions_walker
is_pseudo_constant_clause
is_pseudo_constant_clause_relids
pull_constant_clauses
has_distinct_on_clause
has_distinct_clause
NumRelids
CommuteClause
strip_implicit_coercions
set_coercionform_dontcare
set_coercionform_dontcare_walker
rowtype_field_matches
eval_const_expressions
estimate_expression_value
eval_const_expressions_mutator
simplify_or_arguments
simplify_and_arguments
simplify_function
evaluate_function
inline_function
substitute_actual_parameters
substitute_actual_parameters_mutator
sql_inline_error_callback
evaluate_expr
4.2.接口说明
4.2.1.make_opclause
函数声明
Expr*
make_opclause(Oidopno,Oidopresulttype,boolopretset,
Expr*leftop,Expr*rightop)
opno操作符oid,opresulttype操作返回结果的数据类型,opretset操作是否返回集合,leftop左操作数,rightop右操作数
函数功能
创建操作子句(operatorclause)OpExpr
处理逻辑
分配节点空间,根据给定参数操作符oid,左右操作数(一元操作右操作数为空)给节点的域赋值
使用方法
如y=3x+4可用二元操作树表示
4.2.2.get_leftop
函数声明
Node*
get_leftop(Expr*clause)
函数功能
得到操作子句的左操作数
处理逻辑
返回OpExpr.larg
使用方法
3+4返回左操作数3
4.2.3.get_rightop
函数声明
Node*
get_rightop(Expr*clause)
函数功能
得到操作子句的右操作数
处理逻辑
返回OpExpr.rarg(如果一元操作,则为空)
使用方法
3+4返回右操作数4
4.2.4.not_clause
函数声明
bool
not_clause(Node*clause)
函数功能
判断子句是否not子句(NOTexpr)
处理逻辑
BoolExpr,且BoolExpr.boolop=NOT_EXPR
使用方法
nota=4
4.2.5.make_notclause
函数声明
Expr*
make_notclause(Expr*notclause)
notclause为not子句的参数
函数功能
创建not子句BoolExpr
处理逻辑
分配节点BoolExpr的空间,给域赋值.boolop=NOT_EXPR等
使用方法
a=4->nota=4
4.2.6.get_notclausearg
函数声明
Expr*
get_notclausearg(Expr*notclause)
函数功能
得到not子句的参数
处理逻辑
返回BoolExpr.args
使用方法
nota=4得到a=4
4.2.7.or_clause
函数声明
bool
or_clause(Node*clause)
函数功能
判断是否是or子句(OR{expr})
处理逻辑
BoolExpr,且BoolExpr.boolop=OR_EXPR
使用方法
a=4orb=5
4.2.8.make_orclause
函数声明
Expr*
make_orclause(List*orclauses)
函数功能
创建or子句BoolExpr
处理逻辑
分配节点BoolExpr的空间,给域赋值.boolop=OR_EXPR等
使用方法
a=4,b=5->a=4orb=5
4.2.9.and_clause
函数声明
bool
and_clause(Node*clause)
函数功能
判断是否是and子句(AND{expr})
处理逻辑
BoolExpr,且BoolExpr.boolop=AND_EXPR
使用方法
a=4andb=5
4.2.10.make_andclause
函数声明
Expr*
make_andclause(List*andclauses)
函数功能
创建and子句BoolExpr
处理逻辑
分配节点BoolExpr的空间,给域赋值.boolop=AND_EXPR等
使用方法
a=4,b=5->a=4andb=5
4.2.11.make_and_qual
函数声明
Node*
make_and_qual(Node*qual1,Node*qual2)
函数功能
创建and条件节点
处理逻辑
根据给定的两个条件,创建and条件节点。
注意NULL被解释为true,所以qual1或qual2任意一个为NULL,就返回另一个条件,否则调用make_andclause创建and子句BoolExpr
使用方法
a=4,NULL->a=4
4.2.12.make_ands_explicit
函数声明
Expr*
make_ands_explicit(List*andclauses)
函数功能
创建显式的and条件表达式
处理逻辑
根据andclause的取值,分情况考虑:
1、NIL,创建true常量返回(NULL被解释为true)
2、andclauses只有一个参数,直接返回此唯一的参数
3、调用make_andclause创建and子句
使用方法
4.2.13.make_ands_implicit
函数声明
List*
make_ands_implicit(Expr*clause)
函数功能
创建隐式的and条件(List形式返回结果,隐含and语义)
处理逻辑
根据clause的取值,分情况考虑:
1、NULL,返回NIL(NILList解释成true)
2、and_clause,返回and参数
3、true常量,返回NIL
4、其它,返回一个元素的列表,list_make1(clause)
使用方法
注意与显式and条件表达形式上的差别
4.2.14.contain_agg_clause
函数声明
bool
contain_agg_clause(Node*clause)
函数功能
是否包含Aggref节点?
处理逻辑
调用contain_agg_clause_walker遍历节点
使用方法
4.2.15.contain_agg_clause_walker
函数声明
staticbool
contain_agg_clause_walker(Node*node,void*context)
函数功能
遍历节点,判断是否包含Aggref节点
处理逻辑
调用expression_tree_walker递归地遍历节点并判断
要求Aggref.agglevelsup==0
使用方法
contain_agg_clause的子例程
4.2.16.count_agg_clauses
函数声明
void
count_agg_clauses(Node*clause,AggClauseCounts*counts)
函数功能
包含多少个Aggref节点?
处理逻辑
调用count_agg_clauses_walker遍历节点
使用方法
4.2.17.count_agg_clauses_walker
函数声明
staticbool
count_agg_clauses_walker(Node*node,AggClauseCounts*counts)
函数功能
遍历节点,判断是否包含Aggref节点
处理逻辑
调用expression_tree_walker递归地遍历节点,若找到Aggref且Aggref.agglevelsup==0则计数加1
使用方法
contain_agg_clause的子例程
4.2.18.expression_returns_set
函数声明
bool
expression_returns_set(Node*clause)
函数功能
表达式是否返回集合?
处理逻辑
调用expression_returns_set_walker
使用方法
4.2.19.expression_returns_set_walker
函数声明
staticbool
expression_returns_set_walker(Node*node,void*context)
函数功能
遍历节点,判断是否有节点的返回结果为集合
处理逻辑
若包含FuncExpr且FuncExpr.funcretset为true,返回true
若包含OpExpr且OpExpr.funcretset为true,返回true
其它情形继续遍历子节点
使用方法
4.2.20.contain_subplans
函数声明
bool
contain_subplans(Node*clause)
函数功能
是否包含SubPlan节点?
处理逻辑
调用contain_subplans_walker遍历子节点
使用方法
4.2.21.contain_subplans_walker
函数声明
staticbool
contain_subplans_walker(Node*node,void*context)
函数功能
遍历节点,判断是否包含SubPlan
处理逻辑
如果找到某节点是SubLink或SubPlan,则返回true,否则继续遍历判断
使用方法
4.2.22.contain_mutable_functions
函数声明
bool
contain_mutable_functions(Node*clause)
函数功能
是否包含mutable函数?
处理逻辑
调用contain_mutable_functions_walker
使用方法
4.2.23.contain_mutable_functions_walker
函数声明
staticbool
contain_mutable_functions_walker(Node*node,void*context)
函数功能
遍历节点,判断是否有mutable节点
处理逻辑
根据节点类型,分情况考虑:
1、FuncExpr
func_volatile(expr->funcid)!
=PROVOLATILE_IMMUTABLE
2、OpExpr,DistinctExpr,ScalarArrayOpExpr,NullIfExpr
op_volatile(expr->opno)!
=PROVOLATILE_IMMUTABLE
3、SubLink,对SubLink中的每个操作符opid
op_volatile(lfirst_oid(opid))!
=PROVOLATILE_IMMUTABLE
4、其它类型继续遍历
使用方法
4.2.24.contain_volatile_functions
函数声明
bool
contain_volatile_functions(Node*clause)
函数功能
是否包含volatile函数?
处理逻辑
调用contain_volatile_functions_walker
使用方法
4.2.25.contain_volatile_functions_walker
函数声明
staticbool
contain_volatile_functions_walker(Node*node,void*context)
函数功能
遍历节点,判断是否有volatile节点
处理逻辑
根据节点类型,分情况考虑:
1、FuncExpr
func_volatile(expr->funcid)==PROVOLATILE_VOLATILE
2、OpExpr,DistinctExpr,ScalarArrayOpExpr,NullIfExpr
op_volatile(expr->opno)==PROVOLATI