Debian Codegraph @

whelp/AQL/internal.shtml

raw
1 <HTML>
2
3 <HEAD>
4 <TITLE>AQL examples - internals</TITLE>
5 <LINK REL="stylesheet" TYPE="text/css" HREF="./aql.css" TITLE="Style sheet for AQL docs">
6 </HEAD>
7 <BODY>
8
9 <H2>AQL - internals</H2>
10
11
12 <H3>Part 1 : Parsing</H3>
13
14 <P>An AQL query is sumbitted as plain text that contains keywords,
15 database indentifiers (e.g. names of tags and classes) and
16 language specific tokens (e.g. ->, ::, []) which are translated
17 into an internal representation of the query for further
18 processing.</P>
19
20 <P>The <A HREF="http://www.combo.org/lex_yacc_page/">lex/yacc system</A>
21 is used to generate program fragments that build the AQL parser.
22 This strategy allowed us to build a comprehensive parser in a well
23 structured and almost self-documenting manner, as the source-files
24 for lex/yacc closely relate the documentation of the
25 <A HREF="syntax.shtml">AQL syntax</A>).
26 <BR>
27 The drawback is the inablity of a lex/yacc generated parser to
28 present meaningful error-messages to the user in the event of
29 a syntax error. However, errors messages reported during the
30 preprocessing stage are much more
31 <A HREF="errorcodes.shtml">explanatory</A>.</P>
32
33 <H4>The lexer</H4>
34
35 <P>The source file for
36 <A HREF="http://www.combo.org/lex_yacc_page/lex.html"><I>lex</I></A>
37 is <A HREF="ftp://ftp.sanger.ac.uk/pub/acedb_src/waql/aqlparse.l"><EM>aqlparse.l</EM></A>.
38 It defines all language keywords, operators and lexical rules
39 for identifiers, numeral constants and date values.</P>
40
41 <P>The interface to the lex/yacc system isn't threadsafe and the C-module
42 which is comprised of lex and yacc-generated code has a global static
43 variable <TT>aql_L</TT> which holds all global status of the query
44 that the various code-snippets access.</P>
45
46 <P>The API to lex/yacc is quite restrictive and its POSIX standard
47 requires that the input has to be a <TT>FILE*</TT> pointer.
48 Various loopholes in the API allowed us to read the lex input
49 directly from a text-buffer containing the query-string.
50 Note that those loopholes are different in AT&mp;T based <I>lex</I>
51 implemetations and the GNU lex-clone <I>flex</I>. See the
52 comments at the top of <EM>aqlparse.l</EM> for details.
53 </P>
54
55 <P>Furthermore, the limited API only allows the programmer
56 to keep track of the line-number of each token.
57 This is very restrictive for AQL queries which in most cases
58 constist only of one line of text.
59 For better error-reporting the array <TT>tokPos[]</TT>
60 is used to keep track of the position of a token in the
61 query-string. Errors reported in the preprocessing stage
62 can therefore point to the specific word in the querystring
63 that was responsible for the error.
64 (see <TT>aqlerror.c:aqlError()</TT>)</P>
65
66
67
68 <H4>The yacc parser</H4>
69
70 <P>The source file for
71 <A HREF="http://www.combo.org/lex_yacc_page/yacc.html"><I>yacc</I></A>
72 is <A HREF="ftp://ftp.sanger.ac.uk/pub/acedb_src/waql/aqlparse.y"><EM>aqlparse.y</EM></A>.
73 It defines the language syntax in BNF grammar and contains the code sippets
74 that build the parse-tree.<P>
75
76 <P>The parse-tree is made up from nodes of various types wich are enumerated
77 by the enum-type <TT>AqlNodeType</TT> in in the header-file
78 <A HREF="ftp://ftp.sanger.ac.uk/pub/acedb_src/waql/aql_.h"><EM>aql_.h</EM></A>.
79 The is no direct relationship between the enum-identifiers used in
80 aqlparse.l and the node-types. The token-identifiers used by the lexer
81 generally correspond directly to the keyword that they represent,
82 whereas the identifiers of the node-types decribe the language construct.</P>
83
84
85 <H4>The initial parse-tree</H4>
86
87 <P>The utility <TT>taql</TT> is a test-bed for aql queries
88 and provides a few hooks into the AQL internals than <TT>tace</TT>
89 which allows nothing but the input of a query-text. The most useful
90 function is the ability to turn on the debugging flags in the
91 AQL kernel.<P>
92
93 <PRE>
94 % taql -d 2 $ACEDB
95 </PRE>
96
97 <P>The debug level 2 will output a textual parse-tree, which is useful
98 to study how the query is processed at each stage. Let's study
99 an example of a parse-tree at the parsing stage, i.e. the tree
100 directly constructed by <EM>aqlparse.y</EM><P>
101
102 <P>This example query will be converted into the following parse-tree :<BR><BR>
103 <CODE><EM>select</EM> a, a->paper <EM>from</EM> a <EM>in class</EM> author <EM>where</EM> a.<EM>name</EM> <EM>like</EM> "m*"</CODE>
104 </P>
105
106 <PRE>
107 <B>TABLE_SFW</B> <I>1405c0460</I>
108 left <B>FROM_LOC</B> name "a" <I>1404da3a0</I>
109 left <B>CLASS</B> name "author" <I>1404da440</I>
110 right <B>BOOL_COMPARISON</B> op like <I>1405c03c0</I>
111 left <B>LOC_VAR</B> name "a" <I>1404da4e0</I>
112 next <B>LOC_METHOD</B> name "name" <I>1404da580</I>
113 right <B>TEXT</B> m* <I>1405c0320</I>
114 right <B>SELECT_FIELD</B> <I>1404da120</I>
115 left <B>LOC_VAR</B> name "a" <I>1404c7380</I>
116 next <B>SELECT_FIELD</B> <I>1404da300</I>
117 left <B>LOC_VAR</B> name "a" <I>1404da1c0</I>
118 next <B>LOC_FOLLOW_TAG_NAME</B> name "paper" <I>1404da260</I>
119 </PRE>
120
121 <P>The nodes in the tree are linked by pointers <I>left</I>, <I>right</I> and <I>next</I>
122 and the tree is traversed recursivly in that order.
123 The memory addresses in italics may be ignored for now, they can be used
124 when nodes-structures are inspected in the debugger. The words in bold
125 are the names of each node as declared by the <TT>AqlNodeType</TT>
126 enumerator in <EM>aql_.h</EM></P>
127
128 <P>The graphical representation of the debug-output would look like this :<BR>
129
130 <!-- close the wrapper table of the sanger-header -->
131 </TD></TR></TABLE>
132
133 <IMG SRC="parse-tree.gif"><BR>
134
135 <!-- re-open the header-table -->
136 <TABLE BORDER="0" WIDTH="80%"><TR><TD ALIGN="LEFT" HALIGN="TOP">
137
138 The graphical tree has 4 levels, that correspond to the 4 indentation levels
139 of the debug output. The types of nodes that are allowed for the
140 <I>left</I>, <I>right</I> and <I>next</I> pointers varies for each type
141 and implies a different meaning depending on each type of node.</P>
142
143 <P>At this stage the parse tree is still very unorganised and needs to
144 be pre-processed before it can be executed to produce the query-results.</P>
145
146 <H3>Part 2 : Preprocessing</H3>
147
148 <P>The parse-tree is quite dramatically altered at this stage. The goal is to
149 turn the parsetree into a canonical form which can be executed and is
150 guaranteed to be syntactically and semantically correct.
151 The pre-processing is divided into 2 main parts, which are performed by
152 the functions <TT>aqlcheck.c:aqlCheck1()</TT> and
153 <TT>aqlrun.c:aqlCheck2()</TT>. During check1 the parse-tree
154 is converted into a canonical form ready for query execution
155 and check2 accesses the database to verify the validity of
156 identifiers such as class-names and tag-names.
157 </P>
158
159 <P>An important concept to the internals of the AQL engine is the <B>locator</B>.
160 A locator is a data-carrier for certain nodes, which provides valuable
161 linkage information across the tree. In the debug-ouput below, one can see,
162 that the <B>LOC_VAR</B> <TT>"a"</TT> is mentioned multiple times in the
163 tree, but careful examination of the locator address reveals that all these
164 node carry the same locator <I>(loc=1405d2e20)</I>. Therefore the state
165 of the variable <TT>"a"</TT> will be the same whichever part of the parse is looking
166 at it, e.g. <TT>a.<EM>name</EM></TT> and <TT>a->paper</TT> will eventually
167 only have to eveluate <TT>"a"</TT> once, and then the result of the <EM>name</EM>-method
168 and the dereferncing to the tag <TT>"paper"</TT> will be based on the same value for <TT>"a"</TT>.<BR>
169 The assignment of locators to certain nodes is a vital part of the work
170 done during pre-processing.</P>
171
172
173 <H4>Check1> :</H4>
174 <P>The debug output of the canonical parse-tree
175 after semantic pre-processing :</P>
176
177 <PRE>
178 <B>TABLE_SFW</B> <I>1405c0460</I>
179 left <B>FROM_LOC</B> name "a" <I>1404da3a0 (loc=1405d2e20)</I>
180 left <B>CLASS</B> name "author" <I>1404da440</I>
181 next <B>FROM_LOC</B> name "_1" <I>1405d1660 (loc=1405d2ec0)</I>
182 left <B>LOC_VAR</B> name "a" <I>1405d1700 (loc=1405d2e20)</I>
183 next <B>LOC_METHOD</B> name "name" <I>1404da580</I>
184 next <B>FROM_LOC</B> name "_2" <I>1405d17a0 (loc=1405d2f60)</I>
185 left <B>LOC_VAR</B> name "a" <I>1405d1840 (loc=1405d2e20)</I>
186 next <B>LOC_FOLLOW_TAG_NAME</B> name "paper" <I>1404da260</I>
187 next <B>FROM_LOC</B> name "_3" <I>1405d18e0 (loc=1405d30a0)</I>
188 left <B>LOC_VAR</B> name "_2" <I>1405d1980 (loc=1405d2f60)</I>
189 next <B>LOC_LOCAL_POS</B> number 1 <I>1405d1520</I>
190 right <B>BOOL_COMPARISON</B> op like <I>1405c03c0</I>
191 left <B>LOC_VAR</B> name "_1" <I>1404da4e0 (loc=1405d2ec0)</I>
192 right <B>TEXT</B> m* <I>1405c0320</I>
193 right <B>LOC_VAR</B> name "a" <I>1404c7380 (loc=1405d2e20)</I>
194 next <B>LOC_VAR</B> name "_3" <I>1404da1c0 (loc=1405d30a0)</I>
195 </PRE>
196
197 <P>The graphical representation of the debug-output would look like this :<BR>
198
199 <!-- close the wrapper table of the sanger-header -->
200 </TD></TR></TABLE>
201
202 <IMG SRC="parse-tree2.gif"><BR>
203
204 <!-- re-open the header-table -->
205 <TABLE BORDER="0" WIDTH="80%"><TR><TD ALIGN="LEFT" HALIGN="TOP">
206
207 The parse-tree has gained a lot of new nodes and some nodes have even been lost.
208 The guts of the query have been converted to a series of <B>FROM_LOC</B>
209 nodes which are reminiscent of the <EM>from</EM> keyword. They are the declarations
210 of internal iterators. The user has already declared one iterator (<TT>"a"</TT>)
211 in the <U>from-clause</U> of the query. This iterator is class-based and will
212 therefore loop over all objects in the class <I>"Author"</I>.<BR>
213 The other iterators (with names like <TT>"_1"</TT>) have been added by the
214 check1 pre-processor.
215 </P>
216
217 <P>The reader is reminded of the convention on positions in an Acedb-model.
218 From every tag you can move a certain number of steps to the right
219 to retrieve a specific data-field. In AQL the default tag-position is 1,
220 i.e. the expression <TT>a->paper</TT> really means <TT>a->paper[1]</TT> and will
221 therefore return the data-element which is one position to the right of
222 the tag <TT>"paper"</TT> in an Author-object. The tag-position 0 refers to the
223 tag itself. The pre-processor will create some extra tag-positiong nodes
224 where the user has not specified one.</P>
225
226 <P>The <B>SELECT_FIELD</B> nodes have disappeared, after some processing
227 they are no longer required, because the node to the right of
228 a <B>TABLE_SFW</B> node automatically identifies itself as a select-field.
229 The 2 select-fields are preserved as <B>LOC_VAR</B> nodes.
230 This query will report 2 columns with the values of the locators <TT>"a"</TT> and <TT>"_3"</TT>
231 at each iteration cycle. Where did the <TT>"_3"</TT> variable come from ?</P>
232
233 <P>The variable with names that start with an underscore "_" are internal variables.
234 They could have been declared by the user, but usually the query
235 is presented in a somewhat abbreviated form.
236 Internally, the query generates 4 results :
237 <OL>
238 <LI>the iterator "a" itself ( "a" )
239 <LI>the method "a.name" ( "_1" )
240 <LI>the tag itself "a->paper[0]" ( "_2" )
241 <LI>the datafield to its right "[1]" ( "_3" )
242 </OL>
243
244 One can now see from the select-fields what the 2 output columns will contain.
245 The internal iterators <TT>"_1"</TT> and <TT>"_2"</TT> can be thought of as hidden columns.
246 They aren't requested by the select-cause and will therefore not be reported
247 but nevertheless must be computed.</P>
248
249 <P>So, all the internal iterators become a linked list of <B>FROM_LOC</B>
250 nodes attached to the left of the <B>TABLE_SFW</B> node, which is
251 the root-node. If a variable is declared to be based on a table-variable
252 (e.g. <CODE>select a:1 from a in @results</CODE>) rather than a class, those
253 iterators are <B>FROM_TABLE</B> nodes.</P>
254
255 <P>The where-clause was parsed straight into the appropriate <B>BOOL_...</B>
256 node by the yacc-parser without the need of intermediate <B>WHERE_...</B>
257 nodes (like those needed for the <N>SELECT_FIELD</B>s).
258 The where-clause is represented by a node attached to the right of a
259 <B>FROM_LOC</B>. This right node represents a condition on the
260 from-clause. In the example the <B>FROM_LOC</B> called <TT>"_3"</TT>
261 is only worth reporting if the expression to its right evaluates TRUE.
262 </P>
263
264 <P>The operands of the where-clause expression can only refer to previously
265 declared <B>LOC_VAR</B>s. That's why the previous declaration of
266 <B>FROM_LOC</B> "_1" is required. The <B>BOOL_COMPARISON</B>
267 node is fairly straight forward - it's operator is the <EM>like</EM>
268 function which compares the left node's value to the right node's value.
269 If TRUE, the locator "_3" is valid and the <B>LOC_VAR</B> node "_3"
270 in the select-clause will therefor report the result.
271 </P>
272
273
274 <P>The parse-tree now represents the query in a canonical form which contains
275 all the internal declarations needed for the query execution.</P>
276
277 <P><B>Check2</B> : The parse-trees is checked against the database.
278 It also determines how the internals locators will be executed.
279 This is the debug output of the parse-tree after <TT>aqlCheck2()</TT> :</P>
280
281 <PRE>
282 <B>TABLE_SFW</B> <I>1405c0460</I>
283 left <B>FROM_LOC</B> name "a" <I>1404da3a0 (loc=1405d2e20, IS_CLASS)</I>
284 left <B>CLASS</B> name "author" <I>1404da440</I>
285 next <B>FROM_LOC</B> name "_1" <I>1405d1660 (loc=1405d2ec0, IS_METHOD)</I>
286 left <B>LOC_VAR</B> name "a" <I>1405d1700 (loc=1405d2e20, IS_CLASS)</I>
287 next <B>LOC_METHOD</B> name "name" <I>1404da580</I>
288 next <B>FROM_LOC</B> name "_2" <I>1405d17a0 (loc=1405d2f60, IS_FOLLOW_TAG)</I>
289 left <B>LOC_VAR</B> name "a" <I>1405d1840 (loc=1405d2e20, IS_CLASS)</I>
290 next <B>LOC_FOLLOW_TAG</B> name "paper" <I>1404da260</I>
291 next <B>FROM_LOC</B> name "_3" <I>1405d18e0 (loc=1405d30a0, IS_LOCAL_POS)</I>
292 left <B>LOC_VAR</B> name "_2" <I>1405d1980 (loc=1405d2f60, IS_FOLLOW_TAG)</I>
293 next <B>LOC_LOCAL_POS</B> number 1 <I>1405d1520</I>
294 right <B>BOOL_COMPARISON</B> op like <I>1405c03c0</I>
295 left <B>LOC_VAR</B> name "_1" <I>1404da4e0 (loc=1405d2ec0, IS_METHOD)</I>
296 right <B>TEXT</B> m* <I>1405c0320</I>
297 right <B>LOC_VAR</B> name "a" <I>1404c7380 (loc=1405d2e20, IS_CLASS)</I>
298 next <B>LOC_VAR</B> name "_3" <I>1404da1c0 (loc=1405d30a0, IS_LOCAL_POS)</I>
299 </PRE>
300
301 <P>The parse-tree has largely unchanged, but the locators have been assigned types. Those
302 types will dictate how a node is treated during query execution.</P>
303
304
305
306 <H3>Part 3 : Query execution</H3>
307
308 <P>The parse-tree can now be passed to the function <TT>aqlrun.c:aqlEval()</TT>
309 which executes the query starting from the root.</P>
310
311 <P><B>Fuzzy logic</B> : The query is executed twice, in fuzzy and non-fuzzy mode.
312 The <B>first-pass</B> in fuzzy mode will never open any objects and therefore
313 execute very quickly. Any result in fuzzy mode can have one of 3 states :
314 <B>NULL</B>, <B>FUZZY</B> or <B>KNOWN</B>. Some queries can be satisfied
315 without ever having to open any objects and will return a KNOWN-value
316 after the fuzzy-pass (which saves time).<BR>
317 Most queries however, will have to open an object at some time to
318 get a result. During the fuzzy-run the result will have the FUZZY state.
319 Any expression using a FUZZY value will be tainted as FUZZY too, so if the
320 result of the root-node is still fuzzy after the first-pass a re-run
321 is required.<BR>
322 The <B>second-pass</B> runs the query in non-fuzzy (i.e. exact) mode.
323 By opening objects (the time-consuming part) all FUZZY result will
324 be evaluated as either NULL or KNOWN. The results of the non-fuzzy re-run
325 can never be fuzzy.</P>
326
327 <P><B>The execution loop</B> : The guts of the query-execution is the linked list of
328 <B>FROM_LOC</B> nodes which is attached to the left of
329 the <B>TABLE_SFW</B> node.<BR>
330 The query-execution will loop over those nodes in turn and at every
331 <B>FROM_LOC</B> it will exhaust all possibilities
332 for that locator. The locator <TT>"a"</TT> will re-loop over <TT>"_1"</TT> to <TT>"_3"</TT>
333 for every KEY found in the given class. The locator <TT>"_3"</TT> will
334 be repeatedly processed for all its results, i.e. all data-fields
335 that are found one position to the right of the <TT>"paper"</TT>-tag.<P>
336
337 <P>The type of each locator specifies the way it is being processed.
338 There are single-valued and multi-valued locators.
339 In our example there are the following types :
340 <UL>
341 <LI><B>IS_CLASS</B> - multi-valued - loops over all KEYS in the given class
342 <LI><B>IS_METHOD</B> - single-valued - performs given method
343 <LI><B>IS_FOLLOW_TAG</B> - single-valued - find the given tag in the object
344 <LI><B>IS_LOCAL_POS</B> - multi-valued - position to the right, then loop down
345 </UL>
346 For details on the other types, see the function <TT>aqlrun.c:loopVariables()</TT>.
347 </P>
348
349 <P>The other important part of the query-execution loop is the processing of
350 each locator at every stage in the loop. Every locator is subjected to this
351 (<TT>aqlrun.c:processThisLoc()</TT>) at the end of a loop-cycle.
352 To find out whether the locator is valid we need to check for its where-clause
353 and evaluate it if necessary. The boolean value of the expression-node
354 which is attached to the right of a <B>FROM_LOC</B> node is used
355 for this decision. If the where-clause expression is TRUE we can move
356 on to the next <B>FROM_LOC</B> node to advance the cycle.
357 <BR>
358 When the cycle gets to its end (the <B>FROM_LOC</B> is the end in the chain
359 or the where-clause failed and we terminate the cycle) we have everything
360 we need for this particualr state in the recursion cycle.
361 It is now time to look at the select-field nodes to the right of the
362 top-level table-expression (the <B>TABLE_SFW</B> node) and copy
363 the results into the results table before continuing to loop.
364 The select-fields are expressions, which are evaluated and their results
365 are added to the end of the table. A <B>LOC_VAR</B> expression
366 is easy to evaluate - just copy the value of its locator into the table.
367 </P>
368
369 <P>The loop can now continue and the recursion will bubble up to the
370 last multi-valued node that wasn't yet exhausted. Thereby every possible
371 combination is visited and the results are gathered row-by-row in the
372 results-table which is attached to the top-level <B>TABLE_SFW</B> node.
373 When the recursion has finished this table-value is returned as the final result.
374 </P>
375
376
377
378 <HR>
379 written by Fred Wobus, 06/Dec/1999
380
381 <ADDRESS><A HREF="mailto:[email protected]">[email protected]</A></ADDRESS>
382
383 </BODY>
384 </HTML>
385