Github user jhuynh1 commented on a diff in the pull request: https://github.com/apache/geode/pull/591#discussion_r123388223 --- Diff: geode-core/src/main/java/org/apache/geode/cache/query/internal/CompiledSelect.java --- @@ -925,79 +917,89 @@ private SelectResults applyProjectionOnCollection(SelectResults resultSet, private SelectResults prepareEmptyResultSet(ExecutionContext context, boolean ignoreOrderBy) throws TypeMismatchException, AmbiguousNameException { - // if no projection attributes or '*'as projection attribute - // & more than one/RunTimeIterator then create a StrcutSet. - // If attribute is null or '*' & only one RuntimeIterator then create a - // ResultSet. - // If single attribute is present without alias name , then create - // ResultSet - // Else if more than on attribute or single attribute with alias is - // present then return a StrcutSet - // create StructSet which will contain root objects of all iterators in - // from clause + // If no projection attributes or '*' as projection attribute & more than one/RunTimeIterator + // then create a StructSet. + // If attribute is null or '*' & only one RuntimeIterator then create a ResultSet. + // If single attribute is present without alias name, then create ResultSet. + // Else if more than on attribute or single attribute with alias is present then return a + // StructSet. + // Create StructSet which will contain root objects of all iterators in from clause. ObjectType elementType = this.cachedElementTypeForOrderBy != null ? this.cachedElementTypeForOrderBy : prepareResultType(context); SelectResults results; - if (this.distinct || !this.count) { - if (this.orderByAttrs != null) { - boolean nullValuesAtStart = !((CompiledSortCriterion) orderByAttrs.get(0)).getCriterion(); - if (elementType.isStructType()) { - if (ignoreOrderBy) { - results = this.distinct ? new LinkedStructSet((StructTypeImpl) elementType) - : new SortedResultsBag(elementType, nullValuesAtStart); - } else { - OrderByComparator comparator = this.hasUnmappedOrderByCols - ? new OrderByComparatorUnmapped(this.orderByAttrs, (StructTypeImpl) elementType, - context) - : new OrderByComparator(this.orderByAttrs, (StructTypeImpl) elementType, context); - results = this.distinct ? new SortedStructSet(comparator, (StructTypeImpl) elementType) - : new SortedStructBag(comparator, (StructType) elementType, nullValuesAtStart); + if (!this.distinct && this.count) { + // Shobhit: If it's a 'COUNT' query and no End processing required Like for 'DISTINCT' + // we can directly keep count in ResultSet and ResultBag is good enough for that. + countStartQueryResult = 0; + return new ResultsBag(new ObjectTypeImpl(Integer.class), 1, context.getCachePerfStats()); + } - } - } else { - if (ignoreOrderBy) { - results = - this.distinct ? new LinkedResultSet() : new SortedResultsBag(nullValuesAtStart); + // Potential edge-case: Could this be non-null but empty? + boolean nullValuesAtStart = orderByAttrs != null && !orderByAttrs.get(0).getCriterion(); + OrderByComparator comparator; + switch (convertToStringCase(elementType, ignoreOrderBy)) { + case "UNORDERED DISTINCT STRUCT": + return new StructSet((StructType) elementType); + case "UNORDERED DISTINCT RESULTS": + return new ResultsSet(elementType); + case "UNORDERED INDISTINCT STRUCT": + return new StructBag((StructType) elementType, context.getCachePerfStats()); + case "UNORDERED INDISTINCT RESULTS": + return new ResultsBag(elementType, context.getCachePerfStats()); + + case "ORDERED DISTINCT STRUCT IGNORED": + return new LinkedStructSet((StructTypeImpl) elementType); + case "ORDERED INDISTINCT STRUCT IGNORED": + return new SortedResultsBag(elementType, nullValuesAtStart); + case "ORDERED DISTINCT STRUCT UNIGNORED": --- End diff -- I think the ignoreOrderBy flag is a bit misleading, otherwise UNORDERED DISTINCT STRUCT should technically be the same as ORDERED DISTINCT STRUCT IGNORED (since ignoreOrderby is true). It's almost like a "sort at data structure" level or delay the sort until later flag. If you decide to keep the naming, would it make sense to not have UNIGNORED but rather a no string. So ORDERED DISTINCT STRUCT == ORDERED DISTINCT STRUCT UNIGNORED
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---