Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PostgreSQL Source Code interpretation (204)-query # 117 (data structure SelectStmt&Value)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This section briefly introduces two important data structures of PostgreSQL in the execution of parse: SelectStmt&Value.

I. data structure

SelectStmt

The "simple" SELECT can be converted to a SelectStmt node. Queries containing collection operations (UNION, INTERSECT, EXCEPT) are represented by a tree of SelectStmt nodes, in which leaf nodes are SELECTs components and internal nodes represent UNION, INTERSECT, and or EXCEPT operators. The internal node and the leaf node are the same node type.

/ *-* Select Statement * A "simple" SELECT is represented in the output of gram.y by a single * SelectStmt node; so is a VALUES construct. A query containing set * operators (UNION, INTERSECT, EXCEPT) is represented by a tree of SelectStmt * nodes, in which the leaf nodes are component SELECTs and the internal nodes * represent UNION, INTERSECT, or EXCEPT operators. Using the same node * type for both leaf and internal nodes allows gram.y to stick ORDER BY, * LIMIT, etc, clause values into a SELECT statement without worrying * whether it is a simple or compound SELECT. * * "simple" SELECT can be converted to SelectStmt node. * queries containing collection operations (UNION, INTERSECT, EXCEPT) are represented by the SelectStmt node tree. * in this tree, the leaf node is the SELECTs component, while the internal node represents the UNION, INTERSECT, and or EXCEPT operators. * the internal node and the leaf node are the same node type. *-* / typedef enum SetOperation {SETOP_NONE = 0, SETOP_UNION, SETOP_INTERSECT, SETOP_EXCEPT} SetOperation;typedef struct SelectStmt {NodeTag type; / * * These fields are used only in "leaf" SelectStmts. * Field * / NULL,DISTINCT ON expression linked list used by the "leaf" SelectStmts node, or all (SELECT DISTINCT) lcons (NIL,NIL) List * distinctClause; / * NULL, list of DISTINCT ON exprs, or * lcons (NIL,NIL) for all (SELECT DISTINCT) * / / target IntoClause * intoClause; / * target for SELECT INTO * / target linked list (element is ResTarget) List * targetList / * the target list (of ResTarget) * / From clause List * fromClause; / * the FROM clause * / WHERE Node * whereClause; / * WHERE qualification * / / GROUP BY clause List * groupClause; / * GROUP BY clauses * / / HAVING conditional expression Node * havingClause; / * HAVING conditional-expression * / / window function list List * windowClause / * WINDOW window_name AS (...),... * / * * In a "leaf" node representing a VALUES list, the above fields are all * null, and instead this field is set. Note that the elements of the * sublists are just expressions, without ResTarget decoration. Also note * that a list element can be DEFAULT (represented as a SetToDefault * node), regardless of the context of the VALUES list. It's up to parse * analysis to reject that where not valid. * if the Leaf node represents a VALUES linked list, the above fields are all NULL, and this field stores relevant information. * / List * valuesLists; / * untransformed list of expression lists * / / * These fields are used in both "leaf" SelectStmts and upper-level * SelectStmts. * the following fields are used for the "left" node and the upper node * / / sort clause List * sortClause; / * sort clause (a list of SortBy's) * / / limit offset Node * limitOffset; / * # of result tuples to skip * / limit number Node * limitCount; / * # of result tuples to return * / FOR UPDATE List * lockingClause / * FOR UPDATE (list of LockingClause's) * / / CTE WithClause * withClause; / * WITH clause * / / * * These fields are used only in upper-level SelectStmts. * the following fields are used for high-level SelectStmts * / operation type SetOperation op; / * type of set op * / bool all; / * ALL specified? * / / left tree struct SelectStmt * larg; / * left child * / right tree struct SelectStmt * rarg; / * right child * / / * Eventually add fields for CORRESPONDING spec here * /} SelectStmt

Value

The same Value structure is used for node types in 5: T_Integer, T_Float, T_String, T_BitString, T_Null

/ *-* value.h * interface for Value nodes * Copyright (c) 2003-2019 PostgreSQL Global Development Group * * src/include/nodes/value.h * *-* / # ifndef VALUE_H#define VALUE_H#include "nodes/nodes.h" / * -* Value node * * The same Value struct is used for five node types: T_Integer * T_Float, T_String, T_BitString, T_Null. * the same Value structure is used for node types in 5: T_Integer, T_Float, T_String, T_BitString, T_Null * * Integral values are actually represented by a machine integer, * but both floats and strings are represented as strings. * Using T_Float as the node type simply indicates that * the contents of the string look like a valid numeric literal. * Integer values are actually represented by machine integer, but both floats and strings are represented by strings. * using T_Float as the node type simply represents the literal amount of memory in a string that seems to be valid. * * (Before Postgres 7.0,we used a double to represent T_Float, * but that creates loss-of-precision problems when the value is * ultimately destined to be converted to NUMERIC. Since Value nodes * are only used in the parsing process, not for runtime data, it's * better to use the more general representation.) * (before PG 7.0, double was used to represent T_Float, but there was a problem of loss of precision) * * Note that an integer-looking string will get lexed as T_Float if * the value is too large to fit in an 'int'. * Note: seemingly integer strings can be semantically regarded as T_Float * * Nulls, of course, don't need the value part at all when int storage is not available. * Nulls, no value is required *-* / typedef struct Value {NodeTag type; / * tag appropriately (eg. T_String) * / union ValUnion {int ival; / * machine integer * / char * str; / * string * /} val;} Value;#define intVal (v) (Value *) (v))-> val.ival) # define floatVal (v) atof (Value *) (v))-> val.str) # define strVal (v) ((Value *) (v))-> val.str) extern Value * makeInteger (int I) Extern Value * makeFloat (char * numericStr); extern Value * makeString (char * str); extern Value * makeBitString (char * str); # endif / * VALUE_H * / II. Source code interpretation

N/A

Third, follow-up analysis

Test the SQL statement:

-- the query statement used for testing: testdb=# select tweak dwxx.dwmc redirection tpurgrxxx.grbhretricomgrxx.xmretricjfxx.ny testdb-# order by t_grxx.grbhtestdb-# limit # from tdistribudfxx.jetestdbashi # where t_dwxx.dwbh = t_grxx.dwbh testdb-# and t_grxx.grbh = t_jfxx.grbhtestdb-# and t_dwxx.dwbh IN ('1001ZhoneZhe 1002') Dwmc | grbh | xm | ny | je-+-X Co., Ltd. | 901 | Zhang San | 201801 | 401.3 X Co., Ltd. | 901 | Zhang San | 201802 | 401.3 X Co., Ltd. | 901 | Zhang San | 201803 | 401.3 Y Co., Ltd. | 902 | Li Si | 201801 | 513 | . 1 Y Co., Ltd. | 902 | Li Si | 201802 | 513.3 Y Co., Ltd. | 513.3 | Li Si | 201804 | 513.3 Y Co., Ltd. | 372.22 (8 rows) | Wang Wu | 201801 | 372.22 Y Co., Ltd.

The sample data are as follows:

(gdb) p * (RawStmt *) (raw_parsetree_list- > head.data- > ptr_value) $7 = {type = T_RawStmt, stmt = 0x1a48c00, stmt_location = 0 Stmt_len = 232} (gdb) p * ((RawStmt *) (raw_parsetree_list- > head.data- > ptr_value)-> stmt$8 = {type = T_SelectStmt} # actual type SelectStmt (gdb) p * (SelectStmt *) ((RawStmt *) (raw_parsetree_list- > head.data- > ptr_value))-> stmt$16 = {type = T_SelectStmt, distinctClause = 0x0, intoClause = 0x0, targetList = 0x1a47b18, fromClause = 0x1a48900, whereClause = 0x1a48b40, groupClause = 0x0, havingClause = 0x0, windowClause = 0x0 ValuesLists = 0x0, sortClause = 0x1afd858, limitOffset = 0x0, limitCount = 0x1afd888, lockingClause = 0x0, withClause = 0x0, op = SETOP_NONE, all = false, larg = 0x0, rarg = 0x0} IV.

N/A

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report