Posts

Showing posts with the label sql parser

Disassembling the SQL Server parser. The LGetToken procedure - part 1

Image
Happy Sunday my dear friends and welcome back to this blog! Today we will talk about the parser component of SQL Server. We keep disassemble the parser as we already done here: SQL Server, inside the parser: the Get_Gen_Lex procedure   SQL Server, A bit of reverse engineering inside the parser: the Parser and the GetChar procedure. (attention contains news published for the first time)   I don't think it has ever been done before and I don't even know if I will end up ...maybe I could probably write a book about it, who knows!  ...but today I want to show you a few interesting things. So start your management studio enter a simple select command, start a debugger like WINDBG ...and of course "happy reading"! The LGetToken procedure The LGetToken procedure is so defined: void LGetToken(uint **param_1,uint *param_2,ushort **param_3) The param_2 contain the pointer to the sql command. This procedure is called by the yylex procedure in order to parse the T-SQL commands...

SQL Server, inside the parser: the Get_Gen_Lex procedure

Image
Hello friends, Well, we have arrived on Mars! The Perseverance probe designed by NASA reached the Red Planet after 203 days! "The human being by his nature always wants to overcome his limits and go beyond. If curiosity is the great engine that makes humanity progress, science is the engine. Today is a big day for science!" To this and to those who do not want to have limits I dedicate this post .   Yes, again about the Parser of SQL Server   Today we talk about another procedure of the SqlLang.dll the Get_Gen_Lex procedure. This is the second part of a series of posts about the parser and you can find the first part here Enjoy the reading! The CParser::Get_GenLex procedure This procedure, part of the CParser class, is usually called after the GetChar procedure and it is used to classify the character just read. In many parts of the LGetToken routine for example we have: Procedure and parameters Looking at the source below   we can say that the declaration is: int CP...

SQL Server, A bit of reverse engineering inside the parser: the Parser and the GetChar procedure. (attention contains news published for the first time)

Image
Hello friends, Welcome to another post about the Parser. We started talking about it in this posts: SQL Server is a compiler! & Where T-SQL tokens are stored? What did we say? Briefly we say that every batch you send to the engine is forst analized and then traduced into a series of more simple instructions called the input tree . Each command is parsed by a parser routine coded into the CParser class that is located in the SqlLang.dll .   SO the job of the Parser is to retrieve characters from the input string, break then into token and finally add the operation to be executed into the Tree structure input tree . I know you are curious to enter in greater details! Well you are right this is a first! The CParser::GetChar procedure What is the GetChar procedure? The Getchar procedure is a routine part of the CParser class that read every character you send to the sql server engine. From the picture below we can take a look to the call stack of the SQLLang.dll. The Getchar p...

SQL Server is a compiler! & Where T-SQL tokens are stored?

Image
Hello friends, First of all i want to wish you an happy new yea r! I know 2020 hitted hard for many of us and hitted hard also for me too! But it is necessary to be positive, go on and working hard. In this manner who can stop us? nobody can! For the first post of this 2021 i would talk about the SQL Server parser exploring how it works . We have already talked about this topic in the beginning of the 2020 with with a series of posts: Inside the SQL Server Query Optimizer - part 1 Introduction and the input tree Inside the SQL Server Query Optimizer - part 2 All about the Simplification   Inside the SQL Server Query Optimizer - part 3 Cardinality estimation etc But now it is the time  to go into a deep detail! Are you ready? go! SQL Server is a compiler! Yes, SQL Server is a compiler. The input of the compiler is the T-SQL command you send to the engine. Every batch you send is analized and traduced into a series of more simple instructions. The heart of the compiler is a pars...