Database Reference
In-Depth Information
Example 1. A basic rewrite example
Input query:
(SELECT a, b
FROM T1)
DIVIDE
(SELECT b
FROM T2)
Rewritten query:
SELECT a
FROM T1
WHERE b IN
(SELECT b
FROM T2)
GROUP BY a
HAVING COUNT(a) = (SELECT COUNT(b)
FROM T2)
• Step 1:
Are the brackets matched? Yes - This step checks that the number of left brackets equals
the number of right brackets.
• Step 2:
Is the
divide
keyword present? Yes - If missing, no rewrite is performed. A trivial regular
expression checks for this keyword in any case.
• Step 3:
Locate the position in the query where the first
divide
keyword occurs. Actually, the
divide
keyword and its immediate surrounding brackets must be matched. This is to prevent a rogue table
with the word
divide
in it from damaging the rewrite.
(SELECT a, b
FROM T1)
DIVIDE
(SELECT b
FROM T2)
• Step 4:
Scan leftward until the number of left brackets matches the number of right brackets from
the beginning of the
divide
keyword.
(SELECT a, b
FROM T1)
DIVIDE
(SELECT b
FROM T2)
• Step 5:
Scan rightward until the number of right brackets matches the number of left brackets
from the end of the
divide
keyword.