@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Select Ferret fulltext columns in results so fulltext queries work under UNION

Summary:
Ref T13091. In Differential, if you provide a query and "Sort by: Relevance", we build a query like this:

```
((SELECT revision.* FROM ... ORDER BY rank) UNION ALL (SELECT revision.* FROM ... ORDER BY rank)) ORDER BY rank
```

The internal "ORDER BY rank" is technically redundant (probably?), but doesn't hurt anything, and makes construction easier.

The problem is that the outer "ORDER BY rank" at the end, which attempts to order the results of the two parts of the UNION, can't actually order them, since `rank` wasn't selected.

(The column isn't actually "rank", which //is// selected -- it's the document modified/created subcolumns, which are not.)

To fix this, actually select the fulltext columns into the result set.

Test Plan:
- Ran a non-empty fulltext query in Differential with "Bucket: Required Action" selected so the UNION construction fired.
- Ran normal queries in Maniphest and global search.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13091

Differential Revision: https://secure.phabricator.com/D20297

+35 -10
+35 -10
src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php
··· 37 37 private $ferretQuery; 38 38 private $ferretMetadata = array(); 39 39 40 + const FULLTEXT_RANK = '_ft_rank'; 41 + const FULLTEXT_MODIFIED = '_ft_epochModified'; 42 + const FULLTEXT_CREATED = '_ft_epochCreated'; 43 + 40 44 /* -( Cursors )------------------------------------------------------------ */ 41 45 42 46 protected function newExternalCursorStringForResult($object) { ··· 298 302 $metadata = id(new PhabricatorFerretMetadata()) 299 303 ->setPHID($phid) 300 304 ->setEngine($this->ferretEngine) 301 - ->setRelevance(idx($row, '_ft_rank')); 305 + ->setRelevance(idx($row, self::FULLTEXT_RANK)); 302 306 303 307 $this->ferretMetadata[$phid] = $metadata; 304 308 305 - unset($row['_ft_rank']); 309 + unset($row[self::FULLTEXT_RANK]); 310 + unset($row[self::FULLTEXT_MODIFIED]); 311 + unset($row[self::FULLTEXT_CREATED]); 306 312 } 307 313 } 308 314 ··· 1097 1103 if ($this->supportsFerretEngine()) { 1098 1104 $columns['rank'] = array( 1099 1105 'table' => null, 1100 - 'column' => '_ft_rank', 1106 + 'column' => self::FULLTEXT_RANK, 1101 1107 'type' => 'int', 1102 1108 'requires-ferret' => true, 1103 1109 ); 1104 1110 $columns['fulltext-created'] = array( 1105 - 'table' => 'ft_doc', 1106 - 'column' => 'epochCreated', 1111 + 'table' => null, 1112 + 'column' => self::FULLTEXT_CREATED, 1107 1113 'type' => 'int', 1108 1114 'requires-ferret' => true, 1109 1115 ); 1110 1116 $columns['fulltext-modified'] = array( 1111 - 'table' => 'ft_doc', 1112 - 'column' => 'epochModified', 1117 + 'table' => null, 1118 + 'column' => self::FULLTEXT_MODIFIED, 1113 1119 'type' => 'int', 1114 1120 'requires-ferret' => true, 1115 1121 ); ··· 1779 1785 } 1780 1786 1781 1787 if (!$this->ferretEngine) { 1782 - $select[] = qsprintf($conn, '0 _ft_rank'); 1788 + $select[] = qsprintf($conn, '0 AS %T', self::FULLTEXT_RANK); 1783 1789 return $select; 1784 1790 } 1785 1791 ··· 1858 1864 1859 1865 $select[] = qsprintf( 1860 1866 $conn, 1861 - '%Q _ft_rank', 1862 - $sum); 1867 + '%Q AS %T', 1868 + $sum, 1869 + self::FULLTEXT_RANK); 1870 + 1871 + // See D20297. We select these as real columns in the result set so that 1872 + // constructions like this will work: 1873 + // 1874 + // ((SELECT ...) UNION (SELECT ...)) ORDER BY ... 1875 + // 1876 + // If the columns aren't part of the result set, the final "ORDER BY" can 1877 + // not act on them. 1878 + 1879 + $select[] = qsprintf( 1880 + $conn, 1881 + 'ft_doc.epochCreated AS %T', 1882 + self::FULLTEXT_CREATED); 1883 + 1884 + $select[] = qsprintf( 1885 + $conn, 1886 + 'ft_doc.epochModified AS %T', 1887 + self::FULLTEXT_MODIFIED); 1863 1888 1864 1889 return $select; 1865 1890 }