PostgreSQL查询引擎——SELECT STATEMENTS SelectStmt
创始人
2024-05-26 01:57:57
0
SelectStmt: select_no_parens			%prec UMINUS| select_with_parens		%prec UMINUS
select_with_parens:'(' select_no_parens ')'				{ $$ = $2; }| '(' select_with_parens ')'			{ $$ = $2; }			

该规则返回单个SelectStmt节点或它们的树,表示集合操作树(set-operation tree)。The rule returns either a single SelectStmt node or a tree of them, representing a set-operation tree. 当sub-SELECT位于a_expr内并且有多余的括号时,会出现歧义:括号是属于子SELECT还是属于周围的a_exp?我们不在乎,但bison想知道。There is an ambiguity when a sub-SELECT is within an a_expr and there are excess parentheses: do the parentheses belong to the sub-SELECT or to the surrounding a_expr? We don’t really care, but bison wants to know. 为了解决歧义,我们会谨慎地定义语法,以便尽可能长时间地推迟决策:只要我们能继续在sub-SELECT中吸收括号,我们就会这样做,只有当不再可能这样做时,我们才会决定括号属于表达式。To resolve the ambiguity, we are careful to define the grammar so that the decision is staved off as long as possible: as long as we can keep absorbing parentheses into the sub-SELECT, we will do so, and only when it’s no longer possible to do that will we decide that parens belong to the expression. 例如,在“SELECT(((SELECT 2))+3)”中,额外的括号被视为sub-select的一部分。“SELECT (((SELECT 2)) UNION SELECT 2)”显示了这样做的必要性。如果我们将“((SELECT 2))”解析为a_expr,那么当我们看到UNION时再返回SELECT视点就太晚了。For example, in “SELECT (((SELECT 2)) + 3)” the extra parentheses are treated as part of the sub-select. The necessity of doing it that way is shown by “SELECT (((SELECT 2)) UNION SELECT 2)”. Had we parsed “((SELECT 2))” as an a_expr, it’d be too late to go back to the SELECT viewpoint when we see the UNION.

此方法通过定义一个非终端select_with_parens来实现,它表示至少有一个外层括号的select,并注意在表达式语法中使用select_wwith_parens,never ‘(’ SelectStmt ‘)’。然后我们将有shift-reduce冲突,我们可以解决这些冲突,以便始终将’(’ 非终结符减少为其他值。我们使用UMINUS优先级,这是一个相当随意的选择。为了能够无歧义地定义select_with_parens本身,我们需要一个非终端select_no_parens,它表示一个没有最外层括号的select结构。这有点乏味,但它有效。在非表达式上下文中,我们使用SelectStmt,它可以表示带或不带外括号的SELECT。This approach is implemented by defining a nonterminal select_with_parens, which represents a SELECT with at least one outer layer of parentheses, and being careful to use select_with_parens, never ‘(’ SelectStmt ‘)’, in the expression grammar. We will then have shift-reduce conflicts which we can resolve in favor of always treating ‘(’