From a2759acd623f3d29dd2fbba803f4e8239155e19d Mon Sep 17 00:00:00 2001
From: Kazuhiko Shiozaki <kazuhiko@nexedi.com>
Date: Sun, 4 Oct 2015 01:46:31 +0200
Subject: [PATCH] Include table name in fulltext search score's alias.

so that group_by_list or order_by_list using 'full_text.SearchableText__score__' works.
---
 product/ZSQLCatalog/ColumnMap.py                   | 10 ++++++++--
 product/ZSQLCatalog/Operator/ComparisonOperator.py |  7 +------
 product/ZSQLCatalog/SQLExpression.py               |  2 --
 3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/product/ZSQLCatalog/ColumnMap.py b/product/ZSQLCatalog/ColumnMap.py
index e8a2e49252..bc789406ad 100644
--- a/product/ZSQLCatalog/ColumnMap.py
+++ b/product/ZSQLCatalog/ColumnMap.py
@@ -453,7 +453,10 @@ class ColumnMap(object):
   def asSQLColumn(self, raw_column, group=DEFAULT_GROUP_ID):
     if self.catalog_table_name is None or raw_column in self.column_ignore_set or \
        '.' in raw_column or '*' in raw_column:
-      result = raw_column
+      if raw_column.endswith('__score__'):
+        result = raw_column.replace('.', '_')
+      else:
+        result = raw_column
     else:
       if raw_column.endswith('__score__'):
         raw_column = raw_column[:-9]
@@ -464,7 +467,10 @@ class ColumnMap(object):
       if group is DEFAULT_GROUP_ID:
         group, column = self.related_key_dict.get(column, (group, raw_column))
       alias = self.table_alias_dict[(group, self.column_map[(group, column)])]
-      result = '`%s`.`%s%s`' % (alias, column, column_suffix)
+      if column_suffix:
+        result = '%s_%s%s' % (alias, column, column_suffix)
+      else:
+        result = '`%s`.`%s`' % (alias, column)
       if function is not None:
         result = '%s(%s)' % (function, result)
     return result
diff --git a/product/ZSQLCatalog/Operator/ComparisonOperator.py b/product/ZSQLCatalog/Operator/ComparisonOperator.py
index 49e29cca3c..2f0cdb4de5 100644
--- a/product/ZSQLCatalog/Operator/ComparisonOperator.py
+++ b/product/ZSQLCatalog/Operator/ComparisonOperator.py
@@ -116,16 +116,11 @@ class MatchComparisonOperator(MonovaluedComparisonOperator):
     }
     select_dict = {}
     if not only_group_columns:
-      select_dict['%s__score__' % column.replace('`', '').rsplit('.', 1)[-1]] = match_string
-    # Support sort on the relevance by using (column)__score__ key.
-    order_by_dict = {
-      '`%s__score__`' % '`.`'.join([x.strip('`') for x in column.split('.')]): match_string,
-    }
+      select_dict['%s__score__' % column.replace('`', '').replace('.', '_')] = match_string
     return SQLExpression(
       self,
       select_dict=select_dict,
       where_expression=match_string,
-      order_by_dict=order_by_dict,
       can_merge_select_dict=True,
     )
 
diff --git a/product/ZSQLCatalog/SQLExpression.py b/product/ZSQLCatalog/SQLExpression.py
index b86b8d1335..6b5cc3e056 100644
--- a/product/ZSQLCatalog/SQLExpression.py
+++ b/product/ZSQLCatalog/SQLExpression.py
@@ -238,8 +238,6 @@ class SQLExpression(object):
     append = result.append
     order_by_dict = self._getOrderByDict()
     for (column, direction, cast) in self.getOrderByList():
-      if column.endswith('__score__') and column not in order_by_dict:
-        continue
       expression = conflictSafeGet(order_by_dict, column, str(column))
       expression = self._reversed_select_dict.get(expression, expression)
       if cast not in (None, ''):
-- 
2.30.9