Data

Operator precedence

Restriction, projection, renaming and extension all bind closely to the relation on their left. This means when combining relations, e.g. using a join, we need to add parentheses if we want to bind to the results of the combined relations instead.

For example, this fails:

$S & $SP{*, extra:=STATUS*2}
ERROR: ::1:21: identifier not found: STATUS

because the { } extension binds to $SP and not to the result of the join between $S and $SP where the STATUS attribute can be found.

So instead:

($S & $SP){*, extra:=STATUS*2}
extraSNOSNAMESTATUSCITYPNOQTY
20S2Jones10ParisP1300
20S2Jones10ParisP2400
40S1Smith20LondonP1300
40S1Smith20LondonP2200
40S1Smith20LondonP3400
40S1Smith20LondonP4200
40S1Smith20LondonP5100
40S1Smith20LondonP6100
40S4Clark20LondonP2200
40S4Clark20LondonP4300
40S4Clark20LondonP5400
60S3Blake30ParisP2200

works by adding the ( ) to ensure the extension binds to the result of the join between $S and $SP.

As with other programming languages there are also precedences for binary operators, e.g. * binds tighter than +, and again, adding parentheses around things can prioritise alternative evaluations.