MySQL Source parser

Source: Internet
Author: User

The SQL request is sent to the server side, and the internal data structure object needs to be generated by the parser to facilitate optimization and generation of execution plans. Parser mainly do two things, lexical analysis and grammar analysis. lexical and syntactic analysis: MySQL uses the lex lexical analyzer, YACC parser for parsing, and finally saves it in the Lex object structure. For example: Select ID, name from xpchild where Id=11. Use Lex_start to initialize the Thd->lex object in the Mysql_parse function, and then invoke the Mysqlparse function in YACC for lexical and syntactic analysis. sql_yacc.cc is a source file generated by YACC after compiling, and a large number of syntax rules are defined in the Sql_yacc.yy file. The syntax definition for select is found below.2. Syntax parsing section:  Based on the parsing results of the lexical analyzer, the following syntax rules are applied  
%token Select_symSelect: select_init {LEX*lex=Lex; Lex->sql_command=Sqlcom_select;     }; Select_item_list:select_item_list','Select_item|Select_item|'*'{THD*thd=YYTHD; Item*item=New(Thd->mem_root) Item_field (&thd->lex->current_select->context,null, NULL,"*"); if(Item = =NULL)            Mysql_yyabort; if(Add_item_to_list (THD, item)) Mysql_yyabort; (THD->lex->current_select->with_wild) + +; };where_clause:/*Empty*/{select->where=0; } |WHERE {Select->parsing_place=In_where; } expr {Select_lex*Select=Select; Select-where= $3; Select->parsing_place=No_matter; if($3)              $3-Top_level_item (); };
You can see that the above SQL statement is parsed out of several parts: (saved in the lex structure) 1.     Sql_command=sqlcom_select; 2. WHERE clause: Select_lex->where 3. Table list: Select_lex->table_list 4.  Field List: Select_lex->item_list specific content: 1. Table_list
     (gdb) Print select_lex->table_list       $= {            <Sql_alloc> = {<no data Fields>},                 members of Sql_i_list <TABLE_LIST>:                 1,                  0x8ca04818 ,                  0x8ca04818
only one table, db = 0x8ca04bc8 "Test", table_name = 0x8ca047f0 "Xpchild" 2. Where
          (gdb) Print select_lex->where-type ()                    $=  item::func_item          (GDB) p Select_lex,where)->args),type ()                    $=  item::field_item          (GDB) p Select_lex,where)->args++),type ()                    $= Item :: Int_item

The structure is as follows:

where     | --         func_item|-->field_item ("ID")         |-->int_item ( 1)

3. Item_list

          (GDB) print * (item_field*) (select_lex->item_list->first->info)                         0x8ca04758"  ID",          * (item_field*) (select_lex->item_list->first->next->  info)                          0x8cb047f8"name"

The structure is as follows:

item_list:     | -->item_field ("ID")     |-->item_field ("name") )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.