首页 > 数据库 >[code notes] subquery parsing in postgresql

[code notes] subquery parsing in postgresql

时间:2024-04-11 16:26:22浏览次数:26  
标签:pstate code postgresql notes will subquery RangeTblEntry query select

The SQL

select a from (select a from t);

Overview

  • from (select a from t) will map to one RangeTblEntry struct of the outter query.
  • from t will map to one RangeTblEntry struct of the inner query.
  • RangeTblEntry entries together create a range table list. This list will link to Query->rtable or ParseState->p_rtable.
  • Besides RangeTblEntry generated, ParseNamespaceItem items also are generated. Each ParseNamespaceItem correspondes to each table entry after from keyword.
  • subquery is recorded in RangeTblEntry's subquery field.

Parsing

From this article, we could quickly find the semantic rule,

table_ref: select_with_parens opt_alias_clause opt_conversion_clause
    {
      RangeSubselect *n = makeNode(RangeSubselect);
      LtRangeTableRef *cref = (LtRangeTableRef *) $3;
      n->lateral = false;
      n->subquery = $1;
      n->alias = $2;
	  ...
    }
  ;

But RangeSubelect structure is not processed as other structures, we could not find the processing code easily by searching T_RangeSubselect. Anyway, we could search transformFromClause function which will lead you to the implementation function transformRangeSubselect, the core logic is:

static ParseNamespaceItem *
transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
{
  /*
   * `parse_sub_analyze` parses the `r->subquery`, in this case, `select a from t`.
   * Current `pstate` will be a parent ParseState of the subquery.
   */
  Query*query = parse_sub_analyze(r->subquery, pstate, ...);

  /*
   * OK, build an RTE and nsitem for the subquery.
   * After `addRangeTableEntryForSubquery` completed, the parent ParseState(or the current pstate) will have
   * some knowledges about the subquery from its `p_rtable` field. Such as:
   *
   *  What kind of query of the subquery?
   *  How many columns does the subquery return?
   *  Is there a join query in the subquery? 
   */
  return addRangeTableEntryForSubquery(pstate, query, r->alias, r->lateral, true);
}

标签:pstate,code,postgresql,notes,will,subquery,RangeTblEntry,query,select
From: https://www.cnblogs.com/lddcool/p/18129457

相关文章

  • 配置VSCODE
    ***官网配置文件:**https://code.visualstudio.com/docs/cpp/config-mingw点击查看代码gcc--versiong++--versiongdb--version![](https://img2024.cnblogs.com/blog/3425643/202404/3425643-20240411153032565-1541800147.png)其他:https://blog.csdn.net/qq_215673......
  • Educational Codeforces Round 154 (Rated for Div. 2)
    B和C写的太慢了。吃了不该吃的罚时,C还莫名其妙的T了一发,另一发也是不应该T的。B连想了两个假做法,然后甚至都实现了,然后过不了样例,再基于这两个才想到了真做法。当时的思路已经有些模糊了,但是确实是写的太慢了,而且\(O(n^2)\)的限制给的也很宽裕,但是我居然还傻乎乎的去先\(O(n^2)......
  • Windos + Vscode搭建Go开发环境
    本文已Go1.21.9为例安装相关软件1.点击下载Golang2.点击下载Vscode先安装Go,傻瓜式安装即可,安装完成后在cmd中输入goversion确认是否安装成功安装Vscode,傻瓜式安装即可在Vscode插件中搜索go,安装插件设置代理goenv查看当前环境变量goenv-wGO1......
  • ImageDecoder 获取GIF动图帧数
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metahttp-equiv="X-UA-Compatible"content="IE=edge"/><metaname="viewport"content="w......
  • leedcode-两个数组的交集
    自己写的:fromtypingimportList#导入List类型,用于函数参数和返回类型的注解classSolution:defintersection(self,nums1:List[int],nums2:List[int])->List[int]:#初始化一个空列表,用于存储两个列表的交集mylist=[]#遍历num......
  • 算法训练营Day08-LeetCode344. 反转字符串 && 541. 反转字符串 II && 151. 反转字符串
    344.反转字符串题目链接:LeetCode344.反转字符串编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组s的形式给出。不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用O(1)的额外空间解决这一问题思路:字符串首尾字符交换即可完成反转。定......
  • VScode里用MPE插件导出带大纲的HTML和PDF文件
    前置条件:1.在VScode里安装好MarkdownPreviewEnhanced插件2.导出PDF文件,需要用到Prince软件,要先在电脑上安装Prince软件,可以去它的官网下载                Prince-DownloadPrince(princexml.com)windows系统的,选这个就可以了,解压后放在自己想放的......
  • LeetCode 2439. 最小化数组中的最大值
    给你一个下标从 0 开始的数组 nums ,它含有 n 个非负整数。每一步操作中,你需要:选择一个满足 1<=i<n 的整数 i ,且 nums[i]>0 。将 nums[i] 减1。将 nums[i-1] 加1。你可以对数组执行 任意 次上述操作,请你返回可以得到的 nums 数组中 最大值......
  • LeetCode 1760. 袋子里最少数目的球
    给你一个整数数组 nums ,其中 nums[i] 表示第 i 个袋子里球的数目。同时给你一个整数 maxOperations 。你可以进行如下操作至多 maxOperations 次:选择任意一个袋子,并将袋子里的球分到 2个新的袋子中,每个袋子里都有 正整数 个球。比方说,一个袋子里有 5 个......
  • LeetCode 面试经典150题---005
    ####135.分发糖果n个孩子站成一排。给你一个整数数组ratings表示每个孩子的评分。你需要按照以下要求,给这些孩子分发糖果:每个孩子至少分配到1个糖果。相邻两个孩子评分更高的孩子会获得更多的糖果。请你给每个孩子分发糖果,计算并返回需要准备的最少糖果数目。n==rat......