ITPub博客

首页 > 数据库 > PostgreSQL > PostgreSQL 源码解读(206)- 查询#119(数据结构RangSubselect等)

PostgreSQL 源码解读(206)- 查询#119(数据结构RangSubselect等)

原创 PostgreSQL 作者:husthxd 时间:2019-06-21 14:11:21 0 删除 编辑

本节简单介绍了PostgreSQL在执行逻辑优化中相关的数据结构,包括RangeSubselect/Alias/RangeVar/ResTarget/ColumnRef.

一、数据结构

RangeSubselect
出现在FROM语句中的子查询


/*
 * RangeSubselect - subquery appearing in a FROM clause
 * 出现在FROM语句中的子查询
 */
typedef struct RangeSubselect
{
  NodeTag   type;
  //是否有LATERAL?
  bool    lateral;    /* does it have LATERAL prefix? */
  //未转换的子查询语句
  Node     *subquery;   /* the untransformed sub-select clause */
  //表别名和可选的列别名
  Alias    *alias;      /* table alias & optional column aliases */
} RangeSubselect;

Alias
为RangeVar指定别名.别名可能同时重命名了表列.


/*
 * Alias -
 *    specifies an alias for a range variable; the alias might also
 *    specify renaming of columns within the table.
 *    为RangeVar指定别名.别名可能同时重命名了表列.
 *
 * Note: colnames is a list of Value nodes (always strings).  In Alias structs
 * associated with RTEs, there may be entries corresponding to dropped
 * columns; these are normally empty strings ("").  See parsenodes.h for info.
 * 注意:colnames是Value节点(通常是字符串)链表.
 *      在与RTEs相关的Alias结构体中,可能有跟已删除的列对应的条目.
 *    这些通常是空字符串("").详细可参考parsenodes.h
 */
typedef struct Alias
{
  NodeTag   type;
  //别名
  char     *aliasname;    /* aliased rel name (never qualified) */
  //列别名链表
  List     *colnames;   /* optional list of column aliases */
} Alias;

RangeVar
range variable,用于FROM语句中.


/*
 * RangeVar - range variable, used in FROM clauses
 * range variable,用于FROM语句中.
 *
 * Also used to represent table names in utility statements; there, the alias
 * field is not used, and inh tells whether to apply the operation
 * recursively to child tables.  In some contexts it is also useful to carry
 * a TEMP table indication here.
 * 在工具语句中,用于表示表名,这时alias字段没有使用.
 * inh用于判断是否在子表中递归应用相关的操作.
 */
typedef struct RangeVar
{
  NodeTag   type;
  char     *catalogname;  /* the catalog (database) name, or NULL */
  char     *schemaname;   /* the schema name, or NULL */
  char     *relname;    /* the relation/sequence name */
  bool    inh;      /* expand rel by inheritance? recursively act
                 * on children? */
  char    relpersistence; /* see RELPERSISTENCE_* in pg_class.h */
  Alias    *alias;      /* table alias & optional column aliases */
  int     location;   /* token location, or -1 if unknown */
} RangeVar;

ResTarget
结果目标列(用于先前已转换的解析树的目标链表中)


/*
 * ResTarget -
 *    result target (used in target list of pre-transformed parse trees)
 *    结果目标列(用于先前已转换的解析树的目标链表中)
 *
 * In a SELECT target list, 'name' is the column label from an
 * 'AS ColumnLabel' clause, or NULL if there was none, and 'val' is the
 * value expression itself.  The 'indirection' field is not used.
 * 在SELECT的目标链表中,
 *   'name'是'AS ColumnLabel'语句中的列标签,如无则为NULL,
 *   'val'是value表达式本身.
 *   'indirection'未使用.
 *
 * INSERT uses ResTarget in its target-column-names list.  Here, 'name' is
 * the name of the destination column, 'indirection' stores any subscripts
 * attached to the destination, and 'val' is not used.
 * INSERT在target-column-names链表中使用ResTarget.
 * 这里'name'是目标列名称,'indirection'存储了所有与目标相关的子脚本,'val'未使用.
 *
 * In an UPDATE target list, 'name' is the name of the destination column,
 * 'indirection' stores any subscripts attached to the destination, and
 * 'val' is the expression to assign.
 * 在UPDATE目标链表中,'name'是目标列名称,'indirection'存储了所有与目标相关的子脚本,
 * 'val'是与赋值相关的表达式.
 *
 * See A_Indirection for more info about what can appear in 'indirection'.
 * 详细参见A_Indirection('indirection')
 */
typedef struct ResTarget
{
  NodeTag   type;
  //列名或NULL
  char     *name;     /* column name or NULL */
  //子脚本,字段名称,'*'或NIL
  List     *indirection;  /* subscripts, field names, and '*', or NIL */
  //需要计算或赋值的值表达式
  Node     *val;      /* the value expression to compute or assign */
  //token的位置,-1表示未知
  int     location;   /* token location, or -1 if unknown */
} ResTarget;

ColumnRef
指定对列或整个元组的引用


/*
 * ColumnRef - specifies a reference to a column, or possibly a whole tuple
 * 指定对列或整个元组的引用
 *
 * The "fields" list must be nonempty.  It can contain string Value nodes
 * (representing names) and A_Star nodes (representing occurrence of a '*').
 * Currently, A_Star must appear only as the last list element --- the grammar
 * is responsible for enforcing this!
 * "fields"链表不能为空.可能包含字符串Value节点和A_Star节点(表示出现了*).
 * 截止到目前为止,A_Star必须出现在最后一个链表元素中.
 *
 * Note: any container subscripting or selection of fields from composite columns
 * is represented by an A_Indirection node above the ColumnRef.  However,
 * for simplicity in the normal case, initial field selection from a table
 * name is represented within ColumnRef and not by adding A_Indirection.
 */
typedef struct ColumnRef
{
  NodeTag   type;
  //字段名称(字符串值)链表或A_Star
  List     *fields;     /* field names (Value strings) or A_Star */
  //token位置
  int     location;   /* token location, or -1 if unknown */
} ColumnRef;

二、源码解读

N/A

三、跟踪分析

RangeSubselect/Alias


(gdb) p *(Node *)($stmt->fromClause->head.data->ptr_value)
$15 = {type = T_RangeSubselect} #实际类型是范围子查询RangeSubselect
(gdb) set $fromclause=(RangeSubselect *)($stmt->fromClause->head.data->ptr_value)
(gdb) p *$fromclause
$16 = {type = T_RangeSubselect, lateral = false, subquery = 0x1666c18, alias = 0x1666d40}
(gdb) p *($fromclause->subquery) #subquery,子查询是SelectStmt类型的节点
$17 = {type = T_SelectStmt}
(gdb) p *($fromclause->alias) #alias,别名,实际值是字符串ret
$18 = {type = T_Alias, aliasname = 0x1666d28 "ret", colnames = 0x0}

RangeVar


...
$43 = {type = T_RangeVar}
(gdb) p *(RangeVar *)((JoinExpr *)($joinexpr->larg))->larg
$44 = {type = T_RangeVar, catalogname = 0x0, schemaname = 0x0, relname = 0x1643380 "t_dwxx", inh = true, 
  relpersistence = 112 'p', alias = 0x0, location = 82}
...

ResTarget


...
(gdb) p *(Node *)($subquerylarg->targetList->head.data->ptr_value)
$26 = {type = T_ResTarget}
(gdb) set $subvar=(ResTarget *)($subquerylarg->targetList->head.data->ptr_value)
(gdb) p *$subvar 
{type = T_ResTarget, name = 0x0, indirection = 0x0, val = 0x1642c70, location = 23}
...

ColumnRef


...
(gdb) p *$restarget->val
$25 = {type = T_ColumnRef}
(gdb) p *(ColumnRef *)$restarget->val
$26 = {type = T_ColumnRef, fields = 0x1a47a08, location = 7}
(gdb) p *((ColumnRef *)$restarget->val)->fields
$27 = {type = T_List, length = 2, head = 0x1a47a88, tail = 0x1a479e8}
(gdb) p *(Node *)(((ColumnRef *)$restarget->val)->fields)->head.data->ptr_value
$32 = {type = T_String}
#fields链表的第1个元素是数据表,第2个元素是数据列
(gdb) p *(Value *)(((ColumnRef *)$restarget->val)->fields)->head.data->ptr_value
$37 = {type = T_String, val = {ival = 27556248, str = 0x1a47998 "t_dwxx"}}
(gdb) p *(Value *)(((ColumnRef *)$restarget->val)->fields)->tail.data->ptr_value
$38 = {type = T_String, val = {ival = 27556272, str = 0x1a479b0 "dwmc"}}
...

四、参考资料

N/A

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2648329/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
长期从事政务、金融等行业产品研发和架构设计工作,ITPUB数据库版块资深版主,对Oracle、PostgreSQL有深入研究。现就职于广州云图数据技术有限公司,系统架构师。

注册时间:2007-12-28

  • 博文量
    1306
  • 访问量
    3782436