Uploaded image for project: 'Cassandra'
  1. Cassandra
  2. CASSANDRA-14286

IndexOutOfBoundsException with SELECT JSON using IN and ORDER BY

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Normal
    • Resolution: Fixed
    • 2.2.13, 3.0.17, 3.11.3, 4.0-alpha1, 4.0
    • Legacy/CQL
    • None
    • Kubernetes cluster using cassandra:3.11.1 Docker image.

    • Correctness - API / Semantic Implementation
    • Normal

    Description

      When running the following code:

      public class CassandraJsonOrderingBug {
          public static void main(String[] args) {
              Session session = CassandraFactory.getSession();
      
              session.execute("CREATE TABLE thebug ( PRIMARY KEY (a, b), a INT, b INT)");
              try {
                  session.execute("INSERT INTO thebug (a, b) VALUES (20, 30)");
                  session.execute("INSERT INTO thebug (a, b) VALUES (100, 200)");
                  Statement statement = new SimpleStatement("SELECT JSON a, b FROM thebug WHERE a IN (20, 100) ORDER BY b");
                  statement.setFetchSize(Integer.MAX_VALUE);
                  for (Row w: session.execute(statement)) {
                      System.out.println(w.toString());
                  }
              } finally {
                  session.execute("DROP TABLE thebug");
              }
          }
      }
      

      The following exception is thrown server-side:

      java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
      	at java.util.Collections$SingletonList.get(Collections.java:4815) ~[na:1.8.0_151]
      	at org.apache.cassandra.cql3.statements.SelectStatement$SingleColumnComparator.compare(SelectStatement.java:1297) ~[apache-cassandra-3.11.1.jar:3.11.1]
      	at org.apache.cassandra.cql3.statements.SelectStatement$SingleColumnComparator.compare(SelectStatement.java:1284) ~[apache-cassandra-3.11.1.jar:3.11.1]
      	at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:1.8.0_151]
      	at java.util.TimSort.sort(TimSort.java:220) ~[na:1.8.0_151]
      	at java.util.Arrays.sort(Arrays.java:1512) ~[na:1.8.0_151]
      	at java.util.ArrayList.sort(ArrayList.java:1460) ~[na:1.8.0_151]
      	at java.util.Collections.sort(Collections.java:175) ~[na:1.8.0_151]
      

      (full traceback attached)

      The accessed index is the index of the sorted column in the SELECT JSON fields list.
      Similarly, if the select clause is changed to

      SELECT JSON b, a FROM thebug WHERE a IN (20, 100) ORDER BY b

      then the query finishes, but the output is sorted incorrectly (by textual JSON representation):

      Row[{"b": 200, "a": 100}]
      Row[{"b": 30, "a": 20}]
      

      Attachments

        1. orderbug-traceback.txt
          12 kB
          Szymon Acedański

        Activity

          People

            fcofdezc Francisco Fernandez
            accek-invinets Szymon Acedański
            Francisco Fernandez
            Benjamin Lerer
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: