images/contents.gifimages/index.gif

Grouping and Precedence

A compound query element contains a certain number of constituent query elements and one less than that number of query operators. When evaluated, every operator and its two adjacent elements is considered a group. The grouping can be implicit due to the default order of evaluation or the defined precedence of a particular operator, or explicit through the use of parentheses, ( and ).

Precedence among operators and groups directly affects the order in which various parts of a complex query are evaluated. Obviously, groups must always be evaluated before any of the operations for which the groups value is required. Therefore, a group and its operator always has a higher precedence than any operators adjacent to the group.

Once the grouping (whether implicitly or explicitly) of operators and query elements is understood, then the order of evaluation for the entire query will become clear. There are three ways in which a group can be formed:

The default order for evaluating operators is from left to right among operators which have the same precedence; the first operator encountered from the left (or beginning) of the query will implicitly form a group with its two query elements. That group becomes one of the two query elements used by the next operator, and so on.

By definition, some operators have a higher precedence than others, meaning that they and their two query elements will be evaluated first before other adjacent operators of lesser precedence; in effect, a higher precedence operator implicitly forms its own group by laying claim to the query elements on either side of itself.

The precedence of operators can also be increased by explicitly grouping them (and their two query elements) through the use of parentheses.

Consider the following abstract example containing three query elements and two operators of equal precedence:

q1 OP1 q2 OP2 q3

By default, OP1 will be evaluated first, combining the match-lists represented by q1 and q2 into a single result list. That result list will then be used by OP2, along with q3, to produce the final result list for the query. Notice that the order of evaluation was from left to right. Also notice that as a result of that ordering, OP1 and its two query elements were implicitly grouped together, which is why OP1 was evaluated first. The same evaluation order could have been achieved by explicitly grouping them as follows:

(q1 OP1 q2) OP2 q3

Each time a compound query element is formed through the use of parentheses, the precedence of all operators within the parentheses is increased to a level above that of any of their original peers. This effect can be used to alter the default order of evaluation in a complex query, as in the following example:

q1 OP1 (q2 OP2 q3)

Now OP2 will be evaluated first, combining the match-lists derived from q2 and q3 into a single result list. OP1 will then use q1 and OP2's result list to produce the final result list for the query. By way of comparison, if OP2 in these examples had an intrinsically higher precedence than OP1, the implicit grouping would be equivalent to the explicit grouping just shown.