Fixed RCCriteria bug with generating invalid SQL and added a test case against it