Bug 1080171

Summary: CriteriaQuery generation fails when only a disabled NonBindingFilter is present
Product: [Other] RHQ Project Reporter: Heiko W. Rupp <hrupp>
Component: Core ServerAssignee: Heiko W. Rupp <hrupp>
Status: CLOSED CURRENTRELEASE QA Contact: Mike Foley <mfoley>
Severity: urgent Docs Contact:
Priority: unspecified    
Version: 4.10CC: hrupp
Target Milestone: ---   
Target Release: RHQ 4.11   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2014-07-21 10:13:54 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1003211    
Attachments:
Description Flags
Possible patch none

Description Heiko W. Rupp 2014-03-24 20:24:57 UTC
While looking at https://github.com/rhq-project/rhq/pull/2

I got failed executions of the rest-tests because of

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ORDER near line 4, column 1 [SELECT alert
FROM org.rhq.core.domain.alert.Alert alert
WHERE
ORDER BY alert.ctime DESC, alert.id ASC
]

Here the WHERE clause is empty.
Looking at the code reveals that we do some incomplete checking here

This is the code in question
org/rhq/enterprise/server/util/CriteriaQueryGenerator.java:550



        Map<String, Object> filterFields = getFilterFields(criteria);
        if (filterFields.size() > 0 || authorizationPermsFragment != null
            || authorizationCustomConditionFragment != null || searchExpressionWhereClause != null) {
            results.append("WHERE ");  <<<<-----------  (1)
        }

        String conjunctiveFragment = criteria.isFiltersOptional() ? "OR " : "AND ";
        boolean wantCaseInsensitiveMatch = !criteria.isCaseSensitive();

        // criteria
        StringBuilder conjunctiveResults = new StringBuilder();
        boolean firstCrit = true;
        for (Map.Entry<String, Object> filterField : filterFields.entrySet()) {
            Object filterFieldValue = filterField.getValue();

            // if this filter field is non-binding (that is, the query has no parameter whose value is to be bound for the field)
            // and that filter field is turned off, do nothing and continue to the next filter.
            // this in effect does not filter on this field at all.
            if (Criteria.NonBindingOverrideFilter.OFF.equals(filterFieldValue)) {
                continue;  <<<<<<<<<<<<<---- (2) 
            }


At 1) we check if the filterList is not empty and put a 'WHERE' in  then at 2) if the filter is 'OFF', we do not put it in the WHERE clause. 
If that was the only filter statement in the list, we are left with a WHERE keyword without a clause.

Comment 1 Heiko W. Rupp 2014-03-26 15:44:07 UTC
Created attachment 879069 [details]
Possible patch

Comment 2 Heiko W. Rupp 2014-03-28 11:42:21 UTC
Merged by Lukas from https://github.com/rhq-project/rhq/pull/17

Comment 3 Heiko W. Rupp 2014-07-21 10:13:54 UTC
Bulk closing of RHQ 4.11 issues, now that RHQ 4.12 is out.

If you find an issue with those, please open a new BZ, linking to the old one.