Hackers,

The attached attempts to make comprehension of the code in
"TS_phrase_execute" a bit easier.  I posted similar on the "typo patch"
thread of July 2nd/5th but my comments there reflected my mis-understanding
of the distance operator being exact as opposed to the expected
less-than-or-equal.

I had to make one assumption for the attached - that
"curitem->qoperator.distance" is always >= 0.  In the presence of that
assumption the need for the OR goes away.

I don't follow why LposStart is needed so I removed it...

Not compiled or in any way tested...but its not major surgery either -
mostly code comments to make following the logic easier.

David J.
diff --git a/src/backend/utils/adt/tsvector_op.c 
b/src/backend/utils/adt/tsvector_op.c
index ad5a254..215f58f 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -1474,14 +1474,14 @@ TS_phrase_execute(QueryItem *curitem,
                 */
 
                Rpos = Rdata.pos;
-               LposStart = Ldata.pos;
                while (Rpos < Rdata.pos + Rdata.npos)
                {
                        /*
-                        * We need to check all possible distances, so reset 
Lpos to
-                        * guaranteed not yet satisfied position.
+                        * For each physically positioned right-side operand 
iterate over each
+                        * instance of the left-side operand to locate one at 
exactly the specified
+                        * distance.  As soon as a match is found move onto the 
next right-operand
+                        * and continue searching starting with the next 
left-operand.
                         */
-                       Lpos = LposStart;
                        while (Lpos < Ldata.pos + Ldata.npos)
                        {
                                if (WEP_GETPOS(*Rpos) - WEP_GETPOS(*Lpos) ==
@@ -1490,16 +1490,16 @@ TS_phrase_execute(QueryItem *curitem,
                                        /* MATCH! */
                                        if (data)
                                        {
-                                               /* Store position for upper 
phrase operator */
+                                               /* Store the position of the 
right-operand */
                                                *pos_iter = WEP_GETPOS(*Rpos);
                                                pos_iter++;
 
                                                /*
-                                                * Set left start position to 
next, because current
-                                                * one could not satisfy 
distance for any other right
-                                                * position
+                                                * Move onto the next 
right-operand, and also the next
+                                                * left-operand as the current 
one cannot be a match
+                                                * for any other.
                                                 */
-                                               LposStart = Lpos + 1;
+                                               Lpos++
                                                break;
                                        }
                                        else
@@ -1512,18 +1512,23 @@ TS_phrase_execute(QueryItem *curitem,
                                        }
 
                                }
-                               else if (WEP_GETPOS(*Rpos) <= WEP_GETPOS(*Lpos) 
||
-                                                WEP_GETPOS(*Rpos) - 
WEP_GETPOS(*Lpos) <
+                               else if (WEP_GETPOS(*Rpos) - WEP_GETPOS(*Lpos) <
                                                 curitem->qoperator.distance)
                                {
                                        /*
-                                        * Go to the next Rpos, because Lpos is 
ahead or on less
-                                        * distance than required by current 
operator
+                                        * We are now within the required 
distance so
+                                        * continue onto the next right-operand 
and retry
+                                        * the current left-operand to see if 
the added
+                                        * distance causes it to match.
                                         */
                                        break;
 
                                }
 
+                               /*
+                                * The current left-operand is too far away so
+                                * try the next one.
+                                */
                                Lpos++;
                        }
 
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to