IndexScan prevents sorting for GROUP BY/ORDER BY queries, causing inconsistent results
Author: iranna90Created Oct 2, 2025Updated Aug 4, 2026
LabelsType: DefectSource: Community
Describe the bug When using GROUP BY queries with an ORDER BY clause that relies on an IndexScan, the resulting order appears to be random. The query optimizer seems to be incorrectly skipping the sort operation, despite the underlying index being sorted.
explain select
indexed,
sum(amount) totalValue
from
"test.data"
group by
indexed
order by
indexed
AggregateCombineByKeyPhysicalRel(group=[{0}], totalValue=[SUM($1)])
AggregateAccumulateByKeyPhysicalRel(group=[{0}])
IndexScanMapPhysicalRel(table=[[hazelcast, public, test.data[projects=[$1, $3]]]], index=[test.data_sorted_indexed], indexExp=[null], remainderExp=[null], requiresSort=[true])Expected behavior We expect the query to return data that is both ordered and consistent.
This behavior is observed when ordering by non-indexed columns, as the query engine performs a full table scan and sorts the results correctly afterward.
explain select
nonIndexed,
sum(amount) totalValue
from
"test.data"
group by
nonIndexed
order by
nonIndexed
SortPhysicalRel(sort0=[$0], dir0=[ASC])
AggregateCombineByKeyPhysicalRel(group=[{0}], totalValue=[SUM($1)])
AggregateAccumulateByKeyPhysicalRel(group=[{0}])
FullScanPhysicalRel(table=[[hazelcast, public, test.data[projects=[$2, $3]]]], discriminator=[0])To Reproduce
private static void reproduce() {
HazelcastInstance myHazelcastInstance = HazelcastClient.newHazelcastClient();
IMap<String, GenericRecord> data = myHazelcastInstance.getMap("test.data");
data.addIndex(new IndexConfig(IndexType.SORTED, "indexed"));
data.clear();
try {
Set<String> names = Set.of("First.Value", "Second.Value", "Third.value", "Fourth.value", "Fifth.value");
for (String name : names) {
Map<String, GenericRecord> nameData = IntStream.rangeClosed(1, 1000).boxed().map(aId -> {
GenericRecordBuilder myBuilder = GenericRecordBuilder.compact("test-record");
myBuilder.setString("indexed", name);
myBuilder.setString("nonIndexed", name);
myBuilder.setInt64("amount", aId);
return myBuilder.build();
}).collect(toMap(__ -> UUID.randomUUID().toString(), Function.identity()));
data.setAll(nameData);
}
String myMapping = """
CREATE
OR REPLACE EXTERNAL MAPPING "test.data" (
"indexed" VARCHAR,
"nonIndexed" VARCHAR,
"amount" BIGINT
) TYPE IMap OPTIONS (
'keyFormat' = 'java',
'keyJavaClass' = 'java.lang.String',
'valueFormat' = 'compact',
'valueCompactTypeName' = 'test-record'
)
""";
myHazelcastInstance.getSql().execute(myMapping);
} finally {
myHazelcastInstance.shutdown();
}
}Steps to reproduce the behavior:
- Execute the provided code to create records, ensuring one column is a sorted indexed column.
- Run the aggregation query using either the Hazelcast Management Center interface or a Java client.
select
indexed,
sum(amount) totalValue
from
"test.data"
group by
indexed
order by
indexed- Running multiple times will show different order
Source: hazelcast/hazelcast