CS257_209_16.1 Parsing

Download Report

Transcript CS257_209_16.1 Parsing

Query Compiler
16.1: Parsing
By
Cancheevaram Kuppuswamy JaiSarvanan
ID: 209
Agenda
• Query Compiler
• Parsing
• Syntax Analysis
– Atoms
– Syntactic Categories
• Grammar for Simple Subset of SQL
– Queries
– SFW
• Select-Lists
• From-Lists
• Conditions
• Tuple
– Base Syntactic Categories
• Example
• Preprocessor
Query Compiler
Three steps taken by Query processor…
1.
Parsing (SQL to parse tree)
2.
Parse tree to Expression tree of Relation Algebra (logical query
plan)
3.
Logical Query Plan to Physical Query Plan
Parsing
• The result of parsing is a parse tree for the query.
• This step is immediately followed by preprocessing step
Query
Parser
Preprocessor
Syntax Analysis
Nodes of a parse tree corresponds to either atoms or Syntactic
categories
• Atoms:
– Lexical elements such as keywords (eg: Select), names of
attributes or relations, constants, parentheses, operators and
other schema elements
• Syntactic categories:
– Names of families of query subparts that play a similar role in a
query.
– Represented by triangular brackets around a descriptive name.
– Eg: <SFW> , <Condition> etc
Grammar for Simple Subset of SQL
The children of syntactic categories are described by one of the rules of
the grammar for SQL.
Queries:
<Query>::=<SFW>
Query can be a select-from-where-form.
<Query>::= (<Query>)
Query can be a pair of parenthesis surrounding another query.
• In a full SQL grammar, we would also need rules that allowed a
query to be a single relation or an expression involving relations and
operations, such as UNION and JOIN.
SFW (Select-From-Where-Forms):
<SFW>::=SELECT <SelList> FROM <FromList> WHERE
<Condition>
• <SelList> and <FromList> represents the list that can follow
SELECT and FROM respectively, and <Condition> represents
expressions that are either true or false.
• In real SQL grammar, the rule will also provide for optional clauses
such as GROUP BY, HAVING, DISTINCT etc
• Select-Lists
<SelList>::= <Attribute>,<SelList>
<SelList>::=<Attribute>
Select-List can be either a attribute or an attribute, a comma, and
any list of one or more attributes.
• From-Lists
<FromList>::= <Relation>,< FromList >
<FromList >::=< Relation >
From-List can be either a relation or an relation, a comma, and any
list of one or more relations.
• Conditions
<Condition>::= <Condition> AND <Condition>
<Condition>::=<Tuple> IN <Query>
<Condition>::= <Attribute> = <Attribute>
<Condition>::=< Attribute > LIKE <Pattern>
In a full SQL grammar, Conditions have lot more rules such as rules
introducing operators OR, NOT, and EXISTS, comparisons, constant
operands etc.
• Tuple
<Tuple>::= <Attribute>
Tuple can be a single attribute.
• Base Syntactic Categories
Syntactic categories <Attribute>, <Relation>, <Pattern> are special,
they are npt defined by grammatical rules, instead by rules about the
atoms for which they can stand.
• Example:
SELECT movieTitle FROM StarsIn WHERE starName IN ( SELECT
name FROM MovieStar WHERE birthdate LIKE ‘%1960’);
Preprocessor
• Preprocessor is responsible for semantic checking.
• It must check for the following:
– Check relation uses
– Check and resolve attribute uses
– Check types
Questions?
Thank you