Eli Mesika has uploaded a new change for review.

Change subject: core: do not use distinct if sort expr have func
......................................................................

core: do not use distinct if sort expr have func

A former patch made by Omer F added distinct to the search in order to
prevent duplicates.
In the case the search is in the format :

SELECT DISTINCT * FROM T .... ORDER BY f({arguments})

There is a problem since the DISTINCT is performed first and then the
sorting, so f is unknown since it is not part of the result set.

This patch checks if the sort clause has a function call and removes the
DISTINCT if it finds one.

Change-Id: I7c036b2b9ee94266b6e3df54f2c50167e454ed6a
Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1198506
Signed-off-by: emesika <emes...@redhat.com>
---
M 
backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/SyntaxChecker.java
1 file changed, 12 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/59/38459/1

diff --git 
a/backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/SyntaxChecker.java
 
b/backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/SyntaxChecker.java
index 4659d2a..108b033 100644
--- 
a/backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/SyntaxChecker.java
+++ 
b/backend/manager/modules/searchbackend/src/main/java/org/ovirt/engine/core/searchbackend/SyntaxChecker.java
@@ -923,11 +923,9 @@
                         "SELECT * FROM %1$s WHERE ( %2$s IN (%3$s)",
                                 
mSearchObjectAC.getRelatedTableName(searchObjStr, false),
                                 primeryKey,
-                                getInnerQuery(tableName, primeryKey, 
fromStatement,
-                                wherePhrase));
+                                getInnerQuery(tableName, primeryKey, 
fromStatement, wherePhrase, sortExpr));
             } else {
-                inQuery = "(" + getInnerQuery(tableName, "*", fromStatement,
-                        wherePhrase);
+                inQuery = "(" + getInnerQuery(tableName, "*", fromStatement, 
wherePhrase, sortExpr);
             }
             if (syntax.getSearchFrom() > 0) {
                 inQuery = StringFormat.format("%1$s and  %2$s >  %3$s", 
inQuery, primeryKey, syntax.getSearchFrom());
@@ -948,9 +946,16 @@
         return retval;
     }
 
-    private String getInnerQuery(String tableName, String primeryKey, String 
fromStatement, StringBuilder wherePhrase) {
-        return StringFormat.format("SELECT distinct %1$s.%2$s FROM %3$s %4$s", 
tableName, primeryKey, fromStatement,
-                wherePhrase);
+    private String getInnerQuery(String tableName, String primeryKey, String 
fromStatement, StringBuilder wherePhrase, StringBuilder sortExpr) {
+        // prevent using distinct when the sort expression has a function call 
since when distinct is used it is performed first and sorting
+        // is done on the result, so all fields in the sort clause should 
appear in the result set after distinct is applied
+
+        if (sortExpr.indexOf("(") > 0) {
+            return StringFormat.format("SELECT %1$s.%2$s FROM %3$s %4$s", 
tableName, primeryKey, fromStatement, wherePhrase);
+        }
+        else {
+            return StringFormat.format("SELECT distinct %1$s.%2$s FROM %3$s 
%4$s", tableName, primeryKey, fromStatement, wherePhrase);
+        }
     }
 
     protected String getPagePhrase(SyntaxContainer syntax, String pageNumber) {


-- 
To view, visit https://gerrit.ovirt.org/38459
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7c036b2b9ee94266b6e3df54f2c50167e454ed6a
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Eli Mesika <emes...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to