`

PostgreSQL服务过程中的那些事二:Pg服务进程处理简单查询二:SQL解析为parsetree

阅读更多

话说 查询“ select cname, comp from test1, test2 where test1.id=test2.id; 发送到服务器端, 走查询分支 exec_simple_query ,先调用 start_xact_command 初始化了事务管理相关对象和资源,接着调用 pg_parse_query ,通过 Lex Yacc 对传入 SQL 语句进行词法语法解析,生成解析树。

 

1

下面是解析 SQL 语句的调用序列图。




Postgres 服务进程简查之开始事务调用序列图

 

         上图红色方框中显示了解析 SQL 语句的方法调用过程,在 raw_parser 方法中对 SQL 语句进行词法、语法解析并返回解析树 parsetree 的列表, 因为传入的 SQL 串(从客户端传入)可能有多个命令

 

通过 Lex Yacc 对传入 SQL 语句进行词法语法解析,主要是把 SQL 语句里的目前字段、 DISTINCT 子句、 FROM 子句、 WHERE 子句、 GROUP BY 子句、 HAVING 子句、 ORDER BY 子句等解析到各自对应的结构中,组织成各自的数据结构,最后组成解析树 parsetree 。分析完后对应 SQL 语句 select cname, comp from test1, test2 where test1.id=test2.id; 的解析树结构如下图:



例子中 SQL 语句对应的解析树结构

pg 输出的解析树如下:

2011-11-23 06:57:39 HKT DETAIL:    

            {QUERY

            :commandType 1

            :querySource 0

            :canSetTag true

            :utilityStmt <>

            :resultRelation 0

            :intoClause <>

            :hasAggs false

            :hasWindowFuncs false

            :hasSubLinks false

            :hasDistinctOn false

            :hasRecursive false

            :hasModifyingCTE false

            :hasForUpdate false

            :cteList <>

            :rtable (

               {RTE

               :alias <>

               :eref

                  {ALIAS

                  :aliasname pg_type

                  :colnames ("typname" "typnamespace" "typowner" "typlen" "typbyval" "t

                  yptype" "typcategory" "typispreferred" "typisdefined" "typdelim" "typ

                  relid" "typelem" "typarray" "typinput" "typoutput" "typreceive" "typs

                  end" "typmodin" "typmodout" "typanalyze" "typalign" "typstorage" "typ

                  notnull" "typbasetype" "typtypmod" "typndims" "typcollation" "typdefa

                  ultbin" "typdefault")

                  }

                :rtekind 0

               :relid 1247

               :relkind r

               :inh true

               :inFromCl true

               :requiredPerms 2

               :checkAsUser 0

               :selectedCols (b 6)

               :modifiedCols (b)

               }

            )

            :jointree

               {FROMEXPR

               :fromlist (

                  {RANGETBLREF

                  :rtindex 1

                  }

               )

               :quals

                  {OPEXPR

                  :opno 607

                  :opfuncid 184

                  :opresulttype 16

                  :opretset false

                  :opcollid 0

                  :inputcollid 0

                  :args (

                     {VAR

                     :varno 1

                     :varattno -2

                     :vartype 26

                     :vartypmod -1

                     :varcollid 0

                     :varlevelsup 0

                     :varnoold 1

                     :varoattno -2

                      :location 57

                     }

                     {RELABELTYPE

                     :arg

                        {CONST

                        :consttype 23

                        :consttypmod -1

                        :constcollid 0

                        :constlen 4

                        :constbyval true

                        :constisnull false

                        :location 63

                        :constvalue 4 [ 19 4 0 0 ]

                        }

                     :resulttype 26

                     :resulttypmod -1

                     :resultcollid 0

                     :relabelformat 2

                     :location -1

                     }

                  )

                  :location 61

                  }

               }

            :targetList (

               {TARGETENTRY

               :expr

                  {FUNCEXPR

                  :funcid 1081

                  :funcresulttype 25

                  :funcretset false

                  :funcformat 0

                  :funccollid 100

                  :inputcollid 0

                  :args (

                     {VAR

                     :varno 1

                     :varattno -2

                     :vartype 26

                     :vartypmod -1

                     :varcollid 0

                      :varlevelsup 0

                     :varnoold 1

                     :varoattno -2

                     :location 19

                     }

                     {CONST

                     :consttype 23

                     :consttypmod -1

                     :constcollid 0

                     :constlen 4

                      :constbyval true

                     :constisnull false

                     :location 23

                     :constvalue 4 [ 34 0 0 0 ]

                     }

                  )

                  :location 7

                  }

               :resno 1

               :resname typname

               :ressortgroupref 0

                :resorigtbl 0

               :resorigcol 0

               :resjunk false

               }

            )

            :returningList <>

            :groupClause <>

            :havingQual <>

            :windowClause <>

            :distinctClause <>

            :sortClause <>

            :limitOffset <>

            :limitCount <>

            :rowMarks <>

            :setOperations <>

            :constraintDeps <>

            }

 

         关于进行词法语法分析的 Lex Yacc 请参考下面的资料,这些内容引自博客文章《一天之内不再畏惧 lex&yacc 之必备参考资料》, url http://blog.sciencenet.cn/blog-419883-309595.html

  1.  Lex and YACC primer/HOWTO http://tldp.org/HOWTO/Lex-YACC-HOWTO.html ,短短20 页,足够让你建立自信,一个上午足够了吧。
  2. A Compact Guide to Lex & Yacc http://epaperpress.com/lexandyacc/ , 修行再提高一步,一个下午搞定。
  3. 创始人写的书你得看看吧,Lex and yacc, By John R. Levine , Tony Mason, Doug Brown ,不能下载,没关系,这里有:http://www.filefront.com/16046001/lex--yacc.pdf/ ,第五章Parsing SQL 你不看一看?晚上吃完饭在教室里摆开架势三个小时敲定这短短30 页的第5 章,一气呵成,搞定。

有这三篇文档给你揣在兜里,我想你走路的时候应该 保劲 吧。


 

 

------------
转载请注明出处,来自博客:
blog.csdn.net/beiigang
beigang.iteye.com

  • 大小: 62.5 KB
  • 大小: 137 KB
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics