colinwerner opened a new issue, #13905:
URL: https://github.com/apache/ignite/issues/13905

   Ignite 2.18.0; the same code is on master (447e554a).
   
   ## Summary
   A cache created through the Java thin client 
(`IgniteClient.createCache(ClientCacheConfiguration)`) with a `QueryEntity` 
index gets each index field in the opposite order to the one requested: 
`QueryIndex` fields marked ascending (`true`) are built descending, and the 
other way round. The same `QueryEntity` created through the server API builds 
the order requested.
   
   Reading the configuration back through the thin client shows the requested 
order, so the inversion is only visible in `SYS.INDEXES` or in query plans.
   
   ## Cause
   The client writes each field's flag as "ascending", while the server reads 
it as "descending":
   
   - `ClientUtils` (thin client, 
`modules/thin-client/impl/.../thin/ClientUtils.java`) writes 
`w.writeBoolean(f.getValue())`, and reads `reader.readBoolean()` back unchanged.
   - `ClientCacheConfigurationSerializer` (server) reads indexes with 
`PlatformConfigurationUtils.readQueryIndex`, which does 
`fields.put(in.readString(), !in.readBoolean())`, and writes them back with 
`writeQueryIndex`, which does `writer.writeBoolean(!field.getValue())`.
   
   `PlatformConfigurationUtils` follows the platform protocol, where the flag 
means "descending" (the .NET client sends it that way). The Java thin client 
sends "ascending" without inverting it, so the server inverts it once on the 
way in. On the way back the server inverts it again and the client does not, so 
a round trip looks correct.
   
   ## Reproducer
   One server node with `ignite-indexing` on the classpath, and a thin client 
on 127.0.0.1:10800:
   ```java
   LinkedHashMap<String, String> fields = new LinkedHashMap<>();
   fields.put("id", Integer.class.getName());
   fields.put("a", Integer.class.getName());
   fields.put("b", Integer.class.getName());
   
   LinkedHashMap<String, Boolean> idxFields = new LinkedHashMap<>();
   idxFields.put("a", true); // ascending
   idxFields.put("b", true); // ascending
   
   QueryEntity entity = new QueryEntity(Integer.class.getName(), "Val")
       .setTableName("T")
       .setKeyFieldName("id")
       .setFields(fields)
       .setIndexes(List.of(new QueryIndex(idxFields, 
QueryIndexType.SORTED).setName("T_A_B_IDX")));
   
   client.createCache(new 
ClientCacheConfiguration().setName("t").setSqlSchema("PUBLIC").setQueryEntities(entity));
   
   // The same entity through the server API, for comparison
   QueryEntity same = new QueryEntity(entity).setTableName("S");
   same.setIndexes(List.of(new QueryIndex(idxFields, 
QueryIndexType.SORTED).setName("S_A_B_IDX")));
   server.createCache(new 
CacheConfiguration<>("s").setSqlSchema("PUBLIC").setQueryEntities(List.of(same)));
   
   client.query(new SqlFieldsQuery(
       "SELECT INDEX_NAME, COLUMNS FROM SYS.INDEXES WHERE INDEX_NAME IN 
('T_A_B_IDX', 'S_A_B_IDX')"))
       .getAll().forEach(System.out::println);
   
   
System.out.println(client.cache("t").getConfiguration().getQueryEntities()[0].getIndexes().iterator().next().getFields());
   ```
   
   Output:
   
   ```text
   [S_A_B_IDX, "A" ASC, "B" ASC, "_KEY" ASC]
   [T_A_B_IDX, "A" DESC, "B" DESC, "_KEY" ASC]
   {a=true, b=true}
   ```
   
   ## Impact
   Results are correct, but queries that should read an ascending index in 
order sort instead. For a paged walk such as `WHERE a = ? AND b > ? ORDER BY a, 
b LIMIT n`, the plan uses `T_A_B_IDX` for the range but cannot use it for the 
order, so every page reads the rest of the range and sorts it. On a 
`REPLICATED` cache the plan shows it directly (no `/* index sorted */`); on a 
`PARTITIONED` cache the reduce step's merge prints `/* index sorted */` and 
hides it.
   
   An index's order cannot change once the cache exists, so caches already 
created this way keep their inverted indexes.
   
   ## Workaround
   Pass the opposite flag from the thin client (`false` for ascending) and 
check the result in `SYS.INDEXES`.
   
   ## Suggested fix
   Invert the flag in `ClientUtils` both when writing and when reading index 
fields, so the wire format matches the platform protocol: 
`w.writeBoolean(!f.getValue())` and `new SimpleEntry<>(reader.readString(), 
!reader.readBoolean())`. Clients that already work around the bug would then 
get the inverted order again, so the change needs a release note.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to