You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by bl...@apache.org on 2018/04/17 10:37:51 UTC

[01/10] cassandra git commit: Fix JSON queries with IN restrictions and ORDER BY clause

Repository: cassandra
Updated Branches:
  refs/heads/cassandra-2.2 b3ac7937e -> 594cde793
  refs/heads/cassandra-3.0 22bb413ba -> 598008d43
  refs/heads/cassandra-3.11 845243db9 -> 95cfee623
  refs/heads/trunk 09f3c969b -> 05d7661d0


Fix JSON queries with IN restrictions and ORDER BY clause

patch by Francisco Fernandez; reviewed by Benjamin Lerer for CASSANDRA-14286


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/594cde79
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/594cde79
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/594cde79

Branch: refs/heads/cassandra-2.2
Commit: 594cde7937de6f848998bac35d35591f8a18aa10
Parents: b3ac793
Author: Francisco Fernandez <fr...@gmail.com>
Authored: Tue Apr 17 12:02:25 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:04:33 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../org/apache/cassandra/cql3/ResultSet.java    |  5 ++
 .../cassandra/cql3/selection/Selection.java     | 69 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 19 +++---
 .../cql3/validation/entities/JsonTest.java      | 21 ++++++
 5 files changed, 96 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 5221b1e..967ee05 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.2.13
+ * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
  * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
  * Fix query pager DEBUG log leak causing hit in paged reads throughput (CASSANDRA-14318)
  * Backport circleci yaml (CASSANDRA-14240)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/ResultSet.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/ResultSet.java b/src/java/org/apache/cassandra/cql3/ResultSet.java
index 028691f..16f0d1b 100644
--- a/src/java/org/apache/cassandra/cql3/ResultSet.java
+++ b/src/java/org/apache/cassandra/cql3/ResultSet.java
@@ -254,6 +254,11 @@ public class ResultSet
             return new ResultMetadata(EnumSet.copyOf(flags), names, columnCount, pagingState);
         }
 
+        public int getColumnCount()
+        {
+            return columnCount;
+        }
+
         /**
          * Return only the column names requested by the user, excluding those added for post-query re-orderings,
          * see definition of names and columnCount.

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/selection/Selection.java b/src/java/org/apache/cassandra/cql3/selection/Selection.java
index dc4e240..5385fc6 100644
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@ -24,6 +24,7 @@ import com.google.common.base.Objects;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Iterators;
+import com.google.common.collect.Lists;
 
 import org.apache.cassandra.config.CFMetaData;
 import org.apache.cassandra.config.ColumnDefinition;
@@ -56,6 +57,8 @@ public abstract class Selection
     private final ResultSet.ResultMetadata metadata;
     private final boolean collectTimestamps;
     private final boolean collectTTLs;
+    // Columns used to order the result set for multi-partition queries
+    private Map<ColumnDefinition, Integer> orderingIndex;
 
     protected Selection(CFMetaData cfm,
                         List<ColumnDefinition> columns,
@@ -130,6 +133,24 @@ public abstract class Selection
         return false;
     }
 
+    public Map<ColumnDefinition, Integer> getOrderingIndex(boolean isJson)
+    {
+        if (!isJson)
+            return orderingIndex;
+
+        // If we order post-query in json, the first and only column that we ship to the client is the json column.
+        // In that case, we should keep ordering columns around to perform the ordering, then these columns will
+        // be placed after the json column. As a consequence of where the colums are placed, we should give the
+        // ordering index a value based on their position in the json encoding and discard the original index.
+        // (CASSANDRA-14286)
+        int columnIndex = 1;
+        Map<ColumnDefinition, Integer> jsonOrderingIndex = new LinkedHashMap<>(orderingIndex.size());
+        for (ColumnDefinition column : orderingIndex.keySet())
+            jsonOrderingIndex.put(column, columnIndex++);
+
+        return jsonOrderingIndex;
+    }
+
     public ResultSet.ResultMetadata getResultMetadata(boolean isJson)
     {
         if (!isJson)
@@ -137,7 +158,13 @@ public abstract class Selection
 
         ColumnSpecification firstColumn = metadata.names.get(0);
         ColumnSpecification jsonSpec = new ColumnSpecification(firstColumn.ksName, firstColumn.cfName, Json.JSON_COLUMN_ID, UTF8Type.instance);
-        return new ResultSet.ResultMetadata(Arrays.asList(jsonSpec));
+        ResultSet.ResultMetadata resultMetadata = new ResultSet.ResultMetadata(Lists.newArrayList(jsonSpec));
+        if (orderingIndex != null)
+        {
+            for (ColumnDefinition orderingColumn : orderingIndex.keySet())
+                resultMetadata.addNonSerializedColumn(orderingColumn);
+        }
+        return resultMetadata;
     }
 
     public static Selection wildcard(CFMetaData cfm)
@@ -152,7 +179,20 @@ public abstract class Selection
         return new SimpleSelection(cfm, columns, false);
     }
 
-    public int addColumnForOrdering(ColumnDefinition c)
+    public void addColumnForOrdering(ColumnDefinition c)
+    {
+        if (orderingIndex == null)
+            orderingIndex = new LinkedHashMap<>();
+
+        int index = getResultSetIndex(c);
+
+        if (index < 0)
+            index = addOrderingColumn(c);
+
+        orderingIndex.put(c, index);
+    }
+
+    protected int addOrderingColumn(ColumnDefinition c)
     {
         columns.add(c);
         metadata.addNonSerializedColumn(c);
@@ -356,14 +396,25 @@ public abstract class Selection
         private List<ByteBuffer> getOutputRow(int protocolVersion)
         {
             List<ByteBuffer> outputRow = selectors.getOutputRow(protocolVersion);
-            return isJson ? rowToJson(outputRow, protocolVersion)
-                          : outputRow;
+            if (isJson)
+            {
+                List<ByteBuffer> jsonRow = rowToJson(outputRow, protocolVersion);
+
+                // Keep ordering columns around for possible post-query ordering. (CASSANDRA-14286)
+                if (orderingIndex != null)
+                {
+                    for (Integer orderingColumnIndex : orderingIndex.values())
+                        jsonRow.add(outputRow.get(orderingColumnIndex));
+                }
+                outputRow = jsonRow;
+            }
+            return outputRow;
         }
 
         private List<ByteBuffer> rowToJson(List<ByteBuffer> row, int protocolVersion)
         {
             StringBuilder sb = new StringBuilder("{");
-            for (int i = 0; i < metadata.names.size(); i++)
+            for (int i = 0; i < metadata.getColumnCount(); i++)
             {
                 if (i > 0)
                     sb.append(", ");
@@ -383,7 +434,9 @@ public abstract class Selection
                     sb.append(spec.type.toJSONString(buffer, protocolVersion));
             }
             sb.append("}");
-            return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+            List<ByteBuffer> jsonRow = new ArrayList<>();
+            jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+            return jsonRow;
         }
 
         private ByteBuffer value(Cell c)
@@ -515,9 +568,9 @@ public abstract class Selection
         }
 
         @Override
-        public int addColumnForOrdering(ColumnDefinition c)
+        protected int addOrderingColumn(ColumnDefinition c)
         {
-            int index = super.addColumnForOrdering(c);
+            int index = super.addOrderingColumn(c);
             factories.addSelectorForOrdering(c, index);
             return factories.size() - 1;
         }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index 13276c7..729cf83 100644
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@ -842,7 +842,7 @@ public class SelectStatement implements CQLStatement
             if (!parameters.orderings.isEmpty())
             {
                 verifyOrderingIsAllowed(restrictions);
-                orderingComparator = getOrderingComparator(cfm, selection, restrictions);
+                orderingComparator = getOrderingComparator(cfm, selection, restrictions, parameters.isJson);
                 isReversed = isReversed(cfm);
             }
 
@@ -942,13 +942,14 @@ public class SelectStatement implements CQLStatement
 
         private Comparator<List<ByteBuffer>> getOrderingComparator(CFMetaData cfm,
                                                                    Selection selection,
-                                                                   StatementRestrictions restrictions)
+                                                                   StatementRestrictions restrictions,
+                                                                   boolean isJson)
                                                                    throws InvalidRequestException
         {
             if (!restrictions.keyIsInRelation())
                 return null;
 
-            Map<ColumnIdentifier, Integer> orderingIndexes = getOrderingIndex(cfm, selection);
+            Map<ColumnDefinition, Integer> orderingIndexes = getOrderingIndex(cfm, selection, isJson);
 
             List<Integer> idToSort = new ArrayList<Integer>();
             List<Comparator<ByteBuffer>> sorters = new ArrayList<Comparator<ByteBuffer>>();
@@ -957,32 +958,28 @@ public class SelectStatement implements CQLStatement
             {
                 ColumnIdentifier identifier = raw.prepare(cfm);
                 ColumnDefinition orderingColumn = cfm.getColumnDefinition(identifier);
-                idToSort.add(orderingIndexes.get(orderingColumn.name));
+                idToSort.add(orderingIndexes.get(orderingColumn));
                 sorters.add(orderingColumn.type);
             }
             return idToSort.size() == 1 ? new SingleColumnComparator(idToSort.get(0), sorters.get(0))
                     : new CompositeComparator(sorters, idToSort);
         }
 
-        private Map<ColumnIdentifier, Integer> getOrderingIndex(CFMetaData cfm, Selection selection)
+        private Map<ColumnDefinition, Integer> getOrderingIndex(CFMetaData cfm, Selection selection, boolean isJson)
                 throws InvalidRequestException
         {
             // If we order post-query (see orderResults), the sorted column needs to be in the ResultSet for sorting,
             // even if we don't
             // ultimately ship them to the client (CASSANDRA-4911).
-            Map<ColumnIdentifier, Integer> orderingIndexes = new HashMap<>();
             for (ColumnIdentifier.Raw raw : parameters.orderings.keySet())
             {
                 ColumnIdentifier column = raw.prepare(cfm);
                 final ColumnDefinition def = cfm.getColumnDefinition(column);
                 if (def == null)
                     handleUnrecognizedOrderingColumn(column);
-                int index = selection.getResultSetIndex(def);
-                if (index < 0)
-                    index = selection.addColumnForOrdering(def);
-                orderingIndexes.put(def.name, index);
+                selection.addColumnForOrdering(def);
             }
-            return orderingIndexes;
+            return selection.getOrderingIndex(isJson);
         }
 
         private boolean isReversed(CFMetaData cfm) throws InvalidRequestException

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
index 46cdd52..9d40038 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
@@ -1264,4 +1264,25 @@ public class JsonTest extends CQLTester
         executor.shutdown();
         Assert.assertTrue(executor.awaitTermination(30, TimeUnit.SECONDS));
     }
+
+    // CASSANDRA-14286
+    @Test
+    public void testJsonOrdering() throws Throwable
+    {
+        createTable("CREATE TABLE %s( PRIMARY KEY (a, b), a INT, b INT);");
+        execute("INSERT INTO %s(a, b) VALUES (20, 30);");
+        execute("INSERT INTO %s(a, b) VALUES (100, 200);");
+
+        assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b"),
+                   row("{\"a\": 20, \"b\": 30}"),
+                   row("{\"a\": 100, \"b\": 200}"));
+
+        assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                   row("{\"a\": 100, \"b\": 200}"),
+                   row("{\"a\": 20, \"b\": 30}"));
+
+        assertRows(execute("SELECT JSON a FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                   row("{\"a\": 100}"),
+                   row("{\"a\": 20}"));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


[05/10] cassandra git commit: Merge branch cassandra-2.2 into cassandra-3.0

Posted by bl...@apache.org.
Merge branch cassandra-2.2 into cassandra-3.0


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/598008d4
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/598008d4
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/598008d4

Branch: refs/heads/cassandra-3.11
Commit: 598008d43ce88ea27dddd6974ec0f247faa0d79c
Parents: 22bb413 594cde7
Author: Benjamin Lerer <b....@gmail.com>
Authored: Tue Apr 17 12:12:48 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:12:48 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../org/apache/cassandra/cql3/ResultSet.java    |  5 ++
 .../cassandra/cql3/selection/Selection.java     | 69 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 19 +++---
 .../cql3/validation/entities/JsonTest.java      | 21 ++++++
 5 files changed, 97 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/CHANGES.txt
----------------------------------------------------------------------
diff --cc CHANGES.txt
index 9012f8c,967ee05..d3d8036
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@@ -1,20 -1,7 +1,22 @@@
 -2.2.13
 +3.0.17
 + * Avoid deadlock when running nodetool refresh before node is fully up (CASSANDRA-14310)
 + * Handle all exceptions when opening sstables (CASSANDRA-14202)
 + * Handle incompletely written hint descriptors during startup (CASSANDRA-14080)
 + * Handle repeat open bound from SRP in read repair (CASSANDRA-14330)
 + * Use zero as default score in DynamicEndpointSnitch (CASSANDRA-14252)
 + * Respect max hint window when hinting for LWT (CASSANDRA-14215)
 + * Adding missing WriteType enum values to v3, v4, and v5 spec (CASSANDRA-13697)
 + * Don't regenerate bloomfilter and summaries on startup (CASSANDRA-11163)
 + * Fix NPE when performing comparison against a null frozen in LWT (CASSANDRA-14087)
 + * Log when SSTables are deleted (CASSANDRA-14302)
 + * Fix batch commitlog sync regression (CASSANDRA-14292)
 + * Write to pending endpoint when view replica is also base replica (CASSANDRA-14251)
 + * Chain commit log marker potential performance regression in batch commit mode (CASSANDRA-14194)
 + * Fully utilise specified compaction threads (CASSANDRA-14210)
 + * Pre-create deletion log records to finish compactions quicker (CASSANDRA-12763)
 +Merged from 2.2:
+  * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
+  * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
 - * Fix query pager DEBUG log leak causing hit in paged reads throughput (CASSANDRA-14318)
   * Backport circleci yaml (CASSANDRA-14240)
  Merged from 2.1:
   * Check checksum before decompressing data (CASSANDRA-14284)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/src/java/org/apache/cassandra/cql3/ResultSet.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/selection/Selection.java
index 406f849,5385fc6..6227158
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@@ -399,8 -434,17 +450,10 @@@ public abstract class Selectio
                      sb.append(spec.type.toJSONString(buffer, protocolVersion));
              }
              sb.append("}");
-             return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+             List<ByteBuffer> jsonRow = new ArrayList<>();
+             jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+             return jsonRow;
          }
 -
 -        private ByteBuffer value(Cell c)
 -        {
 -            return (c instanceof CounterCell)
 -                ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value()))
 -                : c.value();
 -        }
      }
  
      private static interface Selectors

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index 1e867bc,729cf83..a5e6254
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@@ -911,14 -841,14 +911,14 @@@ public class SelectStatement implement
  
              if (!parameters.orderings.isEmpty())
              {
 +                assert !forView;
                  verifyOrderingIsAllowed(restrictions);
-                 orderingComparator = getOrderingComparator(cfm, selection, restrictions);
+                 orderingComparator = getOrderingComparator(cfm, selection, restrictions, parameters.isJson);
                  isReversed = isReversed(cfm);
 +                if (isReversed)
 +                    orderingComparator = Collections.reverseOrder(orderingComparator);
              }
  
 -            if (isReversed)
 -                restrictions.reverse();
 -
              checkNeedsFiltering(restrictions);
  
              SelectStatement stmt = new SelectStatement(cfm,

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


[07/10] cassandra git commit: Merge branch cassandra-2.2 into cassandra-3.0

Posted by bl...@apache.org.
Merge branch cassandra-2.2 into cassandra-3.0


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/598008d4
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/598008d4
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/598008d4

Branch: refs/heads/trunk
Commit: 598008d43ce88ea27dddd6974ec0f247faa0d79c
Parents: 22bb413 594cde7
Author: Benjamin Lerer <b....@gmail.com>
Authored: Tue Apr 17 12:12:48 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:12:48 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../org/apache/cassandra/cql3/ResultSet.java    |  5 ++
 .../cassandra/cql3/selection/Selection.java     | 69 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 19 +++---
 .../cql3/validation/entities/JsonTest.java      | 21 ++++++
 5 files changed, 97 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/CHANGES.txt
----------------------------------------------------------------------
diff --cc CHANGES.txt
index 9012f8c,967ee05..d3d8036
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@@ -1,20 -1,7 +1,22 @@@
 -2.2.13
 +3.0.17
 + * Avoid deadlock when running nodetool refresh before node is fully up (CASSANDRA-14310)
 + * Handle all exceptions when opening sstables (CASSANDRA-14202)
 + * Handle incompletely written hint descriptors during startup (CASSANDRA-14080)
 + * Handle repeat open bound from SRP in read repair (CASSANDRA-14330)
 + * Use zero as default score in DynamicEndpointSnitch (CASSANDRA-14252)
 + * Respect max hint window when hinting for LWT (CASSANDRA-14215)
 + * Adding missing WriteType enum values to v3, v4, and v5 spec (CASSANDRA-13697)
 + * Don't regenerate bloomfilter and summaries on startup (CASSANDRA-11163)
 + * Fix NPE when performing comparison against a null frozen in LWT (CASSANDRA-14087)
 + * Log when SSTables are deleted (CASSANDRA-14302)
 + * Fix batch commitlog sync regression (CASSANDRA-14292)
 + * Write to pending endpoint when view replica is also base replica (CASSANDRA-14251)
 + * Chain commit log marker potential performance regression in batch commit mode (CASSANDRA-14194)
 + * Fully utilise specified compaction threads (CASSANDRA-14210)
 + * Pre-create deletion log records to finish compactions quicker (CASSANDRA-12763)
 +Merged from 2.2:
+  * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
+  * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
 - * Fix query pager DEBUG log leak causing hit in paged reads throughput (CASSANDRA-14318)
   * Backport circleci yaml (CASSANDRA-14240)
  Merged from 2.1:
   * Check checksum before decompressing data (CASSANDRA-14284)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/src/java/org/apache/cassandra/cql3/ResultSet.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/selection/Selection.java
index 406f849,5385fc6..6227158
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@@ -399,8 -434,17 +450,10 @@@ public abstract class Selectio
                      sb.append(spec.type.toJSONString(buffer, protocolVersion));
              }
              sb.append("}");
-             return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+             List<ByteBuffer> jsonRow = new ArrayList<>();
+             jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+             return jsonRow;
          }
 -
 -        private ByteBuffer value(Cell c)
 -        {
 -            return (c instanceof CounterCell)
 -                ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value()))
 -                : c.value();
 -        }
      }
  
      private static interface Selectors

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index 1e867bc,729cf83..a5e6254
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@@ -911,14 -841,14 +911,14 @@@ public class SelectStatement implement
  
              if (!parameters.orderings.isEmpty())
              {
 +                assert !forView;
                  verifyOrderingIsAllowed(restrictions);
-                 orderingComparator = getOrderingComparator(cfm, selection, restrictions);
+                 orderingComparator = getOrderingComparator(cfm, selection, restrictions, parameters.isJson);
                  isReversed = isReversed(cfm);
 +                if (isReversed)
 +                    orderingComparator = Collections.reverseOrder(orderingComparator);
              }
  
 -            if (isReversed)
 -                restrictions.reverse();
 -
              checkNeedsFiltering(restrictions);
  
              SelectStatement stmt = new SelectStatement(cfm,

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


[02/10] cassandra git commit: Fix JSON queries with IN restrictions and ORDER BY clause

Posted by bl...@apache.org.
Fix JSON queries with IN restrictions and ORDER BY clause

patch by Francisco Fernandez; reviewed by Benjamin Lerer for CASSANDRA-14286


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/594cde79
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/594cde79
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/594cde79

Branch: refs/heads/cassandra-3.0
Commit: 594cde7937de6f848998bac35d35591f8a18aa10
Parents: b3ac793
Author: Francisco Fernandez <fr...@gmail.com>
Authored: Tue Apr 17 12:02:25 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:04:33 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../org/apache/cassandra/cql3/ResultSet.java    |  5 ++
 .../cassandra/cql3/selection/Selection.java     | 69 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 19 +++---
 .../cql3/validation/entities/JsonTest.java      | 21 ++++++
 5 files changed, 96 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 5221b1e..967ee05 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.2.13
+ * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
  * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
  * Fix query pager DEBUG log leak causing hit in paged reads throughput (CASSANDRA-14318)
  * Backport circleci yaml (CASSANDRA-14240)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/ResultSet.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/ResultSet.java b/src/java/org/apache/cassandra/cql3/ResultSet.java
index 028691f..16f0d1b 100644
--- a/src/java/org/apache/cassandra/cql3/ResultSet.java
+++ b/src/java/org/apache/cassandra/cql3/ResultSet.java
@@ -254,6 +254,11 @@ public class ResultSet
             return new ResultMetadata(EnumSet.copyOf(flags), names, columnCount, pagingState);
         }
 
+        public int getColumnCount()
+        {
+            return columnCount;
+        }
+
         /**
          * Return only the column names requested by the user, excluding those added for post-query re-orderings,
          * see definition of names and columnCount.

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/selection/Selection.java b/src/java/org/apache/cassandra/cql3/selection/Selection.java
index dc4e240..5385fc6 100644
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@ -24,6 +24,7 @@ import com.google.common.base.Objects;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Iterators;
+import com.google.common.collect.Lists;
 
 import org.apache.cassandra.config.CFMetaData;
 import org.apache.cassandra.config.ColumnDefinition;
@@ -56,6 +57,8 @@ public abstract class Selection
     private final ResultSet.ResultMetadata metadata;
     private final boolean collectTimestamps;
     private final boolean collectTTLs;
+    // Columns used to order the result set for multi-partition queries
+    private Map<ColumnDefinition, Integer> orderingIndex;
 
     protected Selection(CFMetaData cfm,
                         List<ColumnDefinition> columns,
@@ -130,6 +133,24 @@ public abstract class Selection
         return false;
     }
 
+    public Map<ColumnDefinition, Integer> getOrderingIndex(boolean isJson)
+    {
+        if (!isJson)
+            return orderingIndex;
+
+        // If we order post-query in json, the first and only column that we ship to the client is the json column.
+        // In that case, we should keep ordering columns around to perform the ordering, then these columns will
+        // be placed after the json column. As a consequence of where the colums are placed, we should give the
+        // ordering index a value based on their position in the json encoding and discard the original index.
+        // (CASSANDRA-14286)
+        int columnIndex = 1;
+        Map<ColumnDefinition, Integer> jsonOrderingIndex = new LinkedHashMap<>(orderingIndex.size());
+        for (ColumnDefinition column : orderingIndex.keySet())
+            jsonOrderingIndex.put(column, columnIndex++);
+
+        return jsonOrderingIndex;
+    }
+
     public ResultSet.ResultMetadata getResultMetadata(boolean isJson)
     {
         if (!isJson)
@@ -137,7 +158,13 @@ public abstract class Selection
 
         ColumnSpecification firstColumn = metadata.names.get(0);
         ColumnSpecification jsonSpec = new ColumnSpecification(firstColumn.ksName, firstColumn.cfName, Json.JSON_COLUMN_ID, UTF8Type.instance);
-        return new ResultSet.ResultMetadata(Arrays.asList(jsonSpec));
+        ResultSet.ResultMetadata resultMetadata = new ResultSet.ResultMetadata(Lists.newArrayList(jsonSpec));
+        if (orderingIndex != null)
+        {
+            for (ColumnDefinition orderingColumn : orderingIndex.keySet())
+                resultMetadata.addNonSerializedColumn(orderingColumn);
+        }
+        return resultMetadata;
     }
 
     public static Selection wildcard(CFMetaData cfm)
@@ -152,7 +179,20 @@ public abstract class Selection
         return new SimpleSelection(cfm, columns, false);
     }
 
-    public int addColumnForOrdering(ColumnDefinition c)
+    public void addColumnForOrdering(ColumnDefinition c)
+    {
+        if (orderingIndex == null)
+            orderingIndex = new LinkedHashMap<>();
+
+        int index = getResultSetIndex(c);
+
+        if (index < 0)
+            index = addOrderingColumn(c);
+
+        orderingIndex.put(c, index);
+    }
+
+    protected int addOrderingColumn(ColumnDefinition c)
     {
         columns.add(c);
         metadata.addNonSerializedColumn(c);
@@ -356,14 +396,25 @@ public abstract class Selection
         private List<ByteBuffer> getOutputRow(int protocolVersion)
         {
             List<ByteBuffer> outputRow = selectors.getOutputRow(protocolVersion);
-            return isJson ? rowToJson(outputRow, protocolVersion)
-                          : outputRow;
+            if (isJson)
+            {
+                List<ByteBuffer> jsonRow = rowToJson(outputRow, protocolVersion);
+
+                // Keep ordering columns around for possible post-query ordering. (CASSANDRA-14286)
+                if (orderingIndex != null)
+                {
+                    for (Integer orderingColumnIndex : orderingIndex.values())
+                        jsonRow.add(outputRow.get(orderingColumnIndex));
+                }
+                outputRow = jsonRow;
+            }
+            return outputRow;
         }
 
         private List<ByteBuffer> rowToJson(List<ByteBuffer> row, int protocolVersion)
         {
             StringBuilder sb = new StringBuilder("{");
-            for (int i = 0; i < metadata.names.size(); i++)
+            for (int i = 0; i < metadata.getColumnCount(); i++)
             {
                 if (i > 0)
                     sb.append(", ");
@@ -383,7 +434,9 @@ public abstract class Selection
                     sb.append(spec.type.toJSONString(buffer, protocolVersion));
             }
             sb.append("}");
-            return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+            List<ByteBuffer> jsonRow = new ArrayList<>();
+            jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+            return jsonRow;
         }
 
         private ByteBuffer value(Cell c)
@@ -515,9 +568,9 @@ public abstract class Selection
         }
 
         @Override
-        public int addColumnForOrdering(ColumnDefinition c)
+        protected int addOrderingColumn(ColumnDefinition c)
         {
-            int index = super.addColumnForOrdering(c);
+            int index = super.addOrderingColumn(c);
             factories.addSelectorForOrdering(c, index);
             return factories.size() - 1;
         }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index 13276c7..729cf83 100644
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@ -842,7 +842,7 @@ public class SelectStatement implements CQLStatement
             if (!parameters.orderings.isEmpty())
             {
                 verifyOrderingIsAllowed(restrictions);
-                orderingComparator = getOrderingComparator(cfm, selection, restrictions);
+                orderingComparator = getOrderingComparator(cfm, selection, restrictions, parameters.isJson);
                 isReversed = isReversed(cfm);
             }
 
@@ -942,13 +942,14 @@ public class SelectStatement implements CQLStatement
 
         private Comparator<List<ByteBuffer>> getOrderingComparator(CFMetaData cfm,
                                                                    Selection selection,
-                                                                   StatementRestrictions restrictions)
+                                                                   StatementRestrictions restrictions,
+                                                                   boolean isJson)
                                                                    throws InvalidRequestException
         {
             if (!restrictions.keyIsInRelation())
                 return null;
 
-            Map<ColumnIdentifier, Integer> orderingIndexes = getOrderingIndex(cfm, selection);
+            Map<ColumnDefinition, Integer> orderingIndexes = getOrderingIndex(cfm, selection, isJson);
 
             List<Integer> idToSort = new ArrayList<Integer>();
             List<Comparator<ByteBuffer>> sorters = new ArrayList<Comparator<ByteBuffer>>();
@@ -957,32 +958,28 @@ public class SelectStatement implements CQLStatement
             {
                 ColumnIdentifier identifier = raw.prepare(cfm);
                 ColumnDefinition orderingColumn = cfm.getColumnDefinition(identifier);
-                idToSort.add(orderingIndexes.get(orderingColumn.name));
+                idToSort.add(orderingIndexes.get(orderingColumn));
                 sorters.add(orderingColumn.type);
             }
             return idToSort.size() == 1 ? new SingleColumnComparator(idToSort.get(0), sorters.get(0))
                     : new CompositeComparator(sorters, idToSort);
         }
 
-        private Map<ColumnIdentifier, Integer> getOrderingIndex(CFMetaData cfm, Selection selection)
+        private Map<ColumnDefinition, Integer> getOrderingIndex(CFMetaData cfm, Selection selection, boolean isJson)
                 throws InvalidRequestException
         {
             // If we order post-query (see orderResults), the sorted column needs to be in the ResultSet for sorting,
             // even if we don't
             // ultimately ship them to the client (CASSANDRA-4911).
-            Map<ColumnIdentifier, Integer> orderingIndexes = new HashMap<>();
             for (ColumnIdentifier.Raw raw : parameters.orderings.keySet())
             {
                 ColumnIdentifier column = raw.prepare(cfm);
                 final ColumnDefinition def = cfm.getColumnDefinition(column);
                 if (def == null)
                     handleUnrecognizedOrderingColumn(column);
-                int index = selection.getResultSetIndex(def);
-                if (index < 0)
-                    index = selection.addColumnForOrdering(def);
-                orderingIndexes.put(def.name, index);
+                selection.addColumnForOrdering(def);
             }
-            return orderingIndexes;
+            return selection.getOrderingIndex(isJson);
         }
 
         private boolean isReversed(CFMetaData cfm) throws InvalidRequestException

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
index 46cdd52..9d40038 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
@@ -1264,4 +1264,25 @@ public class JsonTest extends CQLTester
         executor.shutdown();
         Assert.assertTrue(executor.awaitTermination(30, TimeUnit.SECONDS));
     }
+
+    // CASSANDRA-14286
+    @Test
+    public void testJsonOrdering() throws Throwable
+    {
+        createTable("CREATE TABLE %s( PRIMARY KEY (a, b), a INT, b INT);");
+        execute("INSERT INTO %s(a, b) VALUES (20, 30);");
+        execute("INSERT INTO %s(a, b) VALUES (100, 200);");
+
+        assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b"),
+                   row("{\"a\": 20, \"b\": 30}"),
+                   row("{\"a\": 100, \"b\": 200}"));
+
+        assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                   row("{\"a\": 100, \"b\": 200}"),
+                   row("{\"a\": 20, \"b\": 30}"));
+
+        assertRows(execute("SELECT JSON a FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                   row("{\"a\": 100}"),
+                   row("{\"a\": 20}"));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


[04/10] cassandra git commit: Fix JSON queries with IN restrictions and ORDER BY clause

Posted by bl...@apache.org.
Fix JSON queries with IN restrictions and ORDER BY clause

patch by Francisco Fernandez; reviewed by Benjamin Lerer for CASSANDRA-14286


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/594cde79
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/594cde79
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/594cde79

Branch: refs/heads/trunk
Commit: 594cde7937de6f848998bac35d35591f8a18aa10
Parents: b3ac793
Author: Francisco Fernandez <fr...@gmail.com>
Authored: Tue Apr 17 12:02:25 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:04:33 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../org/apache/cassandra/cql3/ResultSet.java    |  5 ++
 .../cassandra/cql3/selection/Selection.java     | 69 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 19 +++---
 .../cql3/validation/entities/JsonTest.java      | 21 ++++++
 5 files changed, 96 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 5221b1e..967ee05 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.2.13
+ * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
  * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
  * Fix query pager DEBUG log leak causing hit in paged reads throughput (CASSANDRA-14318)
  * Backport circleci yaml (CASSANDRA-14240)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/ResultSet.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/ResultSet.java b/src/java/org/apache/cassandra/cql3/ResultSet.java
index 028691f..16f0d1b 100644
--- a/src/java/org/apache/cassandra/cql3/ResultSet.java
+++ b/src/java/org/apache/cassandra/cql3/ResultSet.java
@@ -254,6 +254,11 @@ public class ResultSet
             return new ResultMetadata(EnumSet.copyOf(flags), names, columnCount, pagingState);
         }
 
+        public int getColumnCount()
+        {
+            return columnCount;
+        }
+
         /**
          * Return only the column names requested by the user, excluding those added for post-query re-orderings,
          * see definition of names and columnCount.

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/selection/Selection.java b/src/java/org/apache/cassandra/cql3/selection/Selection.java
index dc4e240..5385fc6 100644
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@ -24,6 +24,7 @@ import com.google.common.base.Objects;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Iterators;
+import com.google.common.collect.Lists;
 
 import org.apache.cassandra.config.CFMetaData;
 import org.apache.cassandra.config.ColumnDefinition;
@@ -56,6 +57,8 @@ public abstract class Selection
     private final ResultSet.ResultMetadata metadata;
     private final boolean collectTimestamps;
     private final boolean collectTTLs;
+    // Columns used to order the result set for multi-partition queries
+    private Map<ColumnDefinition, Integer> orderingIndex;
 
     protected Selection(CFMetaData cfm,
                         List<ColumnDefinition> columns,
@@ -130,6 +133,24 @@ public abstract class Selection
         return false;
     }
 
+    public Map<ColumnDefinition, Integer> getOrderingIndex(boolean isJson)
+    {
+        if (!isJson)
+            return orderingIndex;
+
+        // If we order post-query in json, the first and only column that we ship to the client is the json column.
+        // In that case, we should keep ordering columns around to perform the ordering, then these columns will
+        // be placed after the json column. As a consequence of where the colums are placed, we should give the
+        // ordering index a value based on their position in the json encoding and discard the original index.
+        // (CASSANDRA-14286)
+        int columnIndex = 1;
+        Map<ColumnDefinition, Integer> jsonOrderingIndex = new LinkedHashMap<>(orderingIndex.size());
+        for (ColumnDefinition column : orderingIndex.keySet())
+            jsonOrderingIndex.put(column, columnIndex++);
+
+        return jsonOrderingIndex;
+    }
+
     public ResultSet.ResultMetadata getResultMetadata(boolean isJson)
     {
         if (!isJson)
@@ -137,7 +158,13 @@ public abstract class Selection
 
         ColumnSpecification firstColumn = metadata.names.get(0);
         ColumnSpecification jsonSpec = new ColumnSpecification(firstColumn.ksName, firstColumn.cfName, Json.JSON_COLUMN_ID, UTF8Type.instance);
-        return new ResultSet.ResultMetadata(Arrays.asList(jsonSpec));
+        ResultSet.ResultMetadata resultMetadata = new ResultSet.ResultMetadata(Lists.newArrayList(jsonSpec));
+        if (orderingIndex != null)
+        {
+            for (ColumnDefinition orderingColumn : orderingIndex.keySet())
+                resultMetadata.addNonSerializedColumn(orderingColumn);
+        }
+        return resultMetadata;
     }
 
     public static Selection wildcard(CFMetaData cfm)
@@ -152,7 +179,20 @@ public abstract class Selection
         return new SimpleSelection(cfm, columns, false);
     }
 
-    public int addColumnForOrdering(ColumnDefinition c)
+    public void addColumnForOrdering(ColumnDefinition c)
+    {
+        if (orderingIndex == null)
+            orderingIndex = new LinkedHashMap<>();
+
+        int index = getResultSetIndex(c);
+
+        if (index < 0)
+            index = addOrderingColumn(c);
+
+        orderingIndex.put(c, index);
+    }
+
+    protected int addOrderingColumn(ColumnDefinition c)
     {
         columns.add(c);
         metadata.addNonSerializedColumn(c);
@@ -356,14 +396,25 @@ public abstract class Selection
         private List<ByteBuffer> getOutputRow(int protocolVersion)
         {
             List<ByteBuffer> outputRow = selectors.getOutputRow(protocolVersion);
-            return isJson ? rowToJson(outputRow, protocolVersion)
-                          : outputRow;
+            if (isJson)
+            {
+                List<ByteBuffer> jsonRow = rowToJson(outputRow, protocolVersion);
+
+                // Keep ordering columns around for possible post-query ordering. (CASSANDRA-14286)
+                if (orderingIndex != null)
+                {
+                    for (Integer orderingColumnIndex : orderingIndex.values())
+                        jsonRow.add(outputRow.get(orderingColumnIndex));
+                }
+                outputRow = jsonRow;
+            }
+            return outputRow;
         }
 
         private List<ByteBuffer> rowToJson(List<ByteBuffer> row, int protocolVersion)
         {
             StringBuilder sb = new StringBuilder("{");
-            for (int i = 0; i < metadata.names.size(); i++)
+            for (int i = 0; i < metadata.getColumnCount(); i++)
             {
                 if (i > 0)
                     sb.append(", ");
@@ -383,7 +434,9 @@ public abstract class Selection
                     sb.append(spec.type.toJSONString(buffer, protocolVersion));
             }
             sb.append("}");
-            return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+            List<ByteBuffer> jsonRow = new ArrayList<>();
+            jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+            return jsonRow;
         }
 
         private ByteBuffer value(Cell c)
@@ -515,9 +568,9 @@ public abstract class Selection
         }
 
         @Override
-        public int addColumnForOrdering(ColumnDefinition c)
+        protected int addOrderingColumn(ColumnDefinition c)
         {
-            int index = super.addColumnForOrdering(c);
+            int index = super.addOrderingColumn(c);
             factories.addSelectorForOrdering(c, index);
             return factories.size() - 1;
         }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index 13276c7..729cf83 100644
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@ -842,7 +842,7 @@ public class SelectStatement implements CQLStatement
             if (!parameters.orderings.isEmpty())
             {
                 verifyOrderingIsAllowed(restrictions);
-                orderingComparator = getOrderingComparator(cfm, selection, restrictions);
+                orderingComparator = getOrderingComparator(cfm, selection, restrictions, parameters.isJson);
                 isReversed = isReversed(cfm);
             }
 
@@ -942,13 +942,14 @@ public class SelectStatement implements CQLStatement
 
         private Comparator<List<ByteBuffer>> getOrderingComparator(CFMetaData cfm,
                                                                    Selection selection,
-                                                                   StatementRestrictions restrictions)
+                                                                   StatementRestrictions restrictions,
+                                                                   boolean isJson)
                                                                    throws InvalidRequestException
         {
             if (!restrictions.keyIsInRelation())
                 return null;
 
-            Map<ColumnIdentifier, Integer> orderingIndexes = getOrderingIndex(cfm, selection);
+            Map<ColumnDefinition, Integer> orderingIndexes = getOrderingIndex(cfm, selection, isJson);
 
             List<Integer> idToSort = new ArrayList<Integer>();
             List<Comparator<ByteBuffer>> sorters = new ArrayList<Comparator<ByteBuffer>>();
@@ -957,32 +958,28 @@ public class SelectStatement implements CQLStatement
             {
                 ColumnIdentifier identifier = raw.prepare(cfm);
                 ColumnDefinition orderingColumn = cfm.getColumnDefinition(identifier);
-                idToSort.add(orderingIndexes.get(orderingColumn.name));
+                idToSort.add(orderingIndexes.get(orderingColumn));
                 sorters.add(orderingColumn.type);
             }
             return idToSort.size() == 1 ? new SingleColumnComparator(idToSort.get(0), sorters.get(0))
                     : new CompositeComparator(sorters, idToSort);
         }
 
-        private Map<ColumnIdentifier, Integer> getOrderingIndex(CFMetaData cfm, Selection selection)
+        private Map<ColumnDefinition, Integer> getOrderingIndex(CFMetaData cfm, Selection selection, boolean isJson)
                 throws InvalidRequestException
         {
             // If we order post-query (see orderResults), the sorted column needs to be in the ResultSet for sorting,
             // even if we don't
             // ultimately ship them to the client (CASSANDRA-4911).
-            Map<ColumnIdentifier, Integer> orderingIndexes = new HashMap<>();
             for (ColumnIdentifier.Raw raw : parameters.orderings.keySet())
             {
                 ColumnIdentifier column = raw.prepare(cfm);
                 final ColumnDefinition def = cfm.getColumnDefinition(column);
                 if (def == null)
                     handleUnrecognizedOrderingColumn(column);
-                int index = selection.getResultSetIndex(def);
-                if (index < 0)
-                    index = selection.addColumnForOrdering(def);
-                orderingIndexes.put(def.name, index);
+                selection.addColumnForOrdering(def);
             }
-            return orderingIndexes;
+            return selection.getOrderingIndex(isJson);
         }
 
         private boolean isReversed(CFMetaData cfm) throws InvalidRequestException

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
index 46cdd52..9d40038 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
@@ -1264,4 +1264,25 @@ public class JsonTest extends CQLTester
         executor.shutdown();
         Assert.assertTrue(executor.awaitTermination(30, TimeUnit.SECONDS));
     }
+
+    // CASSANDRA-14286
+    @Test
+    public void testJsonOrdering() throws Throwable
+    {
+        createTable("CREATE TABLE %s( PRIMARY KEY (a, b), a INT, b INT);");
+        execute("INSERT INTO %s(a, b) VALUES (20, 30);");
+        execute("INSERT INTO %s(a, b) VALUES (100, 200);");
+
+        assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b"),
+                   row("{\"a\": 20, \"b\": 30}"),
+                   row("{\"a\": 100, \"b\": 200}"));
+
+        assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                   row("{\"a\": 100, \"b\": 200}"),
+                   row("{\"a\": 20, \"b\": 30}"));
+
+        assertRows(execute("SELECT JSON a FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                   row("{\"a\": 100}"),
+                   row("{\"a\": 20}"));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


[06/10] cassandra git commit: Merge branch cassandra-2.2 into cassandra-3.0

Posted by bl...@apache.org.
Merge branch cassandra-2.2 into cassandra-3.0


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/598008d4
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/598008d4
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/598008d4

Branch: refs/heads/cassandra-3.0
Commit: 598008d43ce88ea27dddd6974ec0f247faa0d79c
Parents: 22bb413 594cde7
Author: Benjamin Lerer <b....@gmail.com>
Authored: Tue Apr 17 12:12:48 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:12:48 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../org/apache/cassandra/cql3/ResultSet.java    |  5 ++
 .../cassandra/cql3/selection/Selection.java     | 69 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 19 +++---
 .../cql3/validation/entities/JsonTest.java      | 21 ++++++
 5 files changed, 97 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/CHANGES.txt
----------------------------------------------------------------------
diff --cc CHANGES.txt
index 9012f8c,967ee05..d3d8036
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@@ -1,20 -1,7 +1,22 @@@
 -2.2.13
 +3.0.17
 + * Avoid deadlock when running nodetool refresh before node is fully up (CASSANDRA-14310)
 + * Handle all exceptions when opening sstables (CASSANDRA-14202)
 + * Handle incompletely written hint descriptors during startup (CASSANDRA-14080)
 + * Handle repeat open bound from SRP in read repair (CASSANDRA-14330)
 + * Use zero as default score in DynamicEndpointSnitch (CASSANDRA-14252)
 + * Respect max hint window when hinting for LWT (CASSANDRA-14215)
 + * Adding missing WriteType enum values to v3, v4, and v5 spec (CASSANDRA-13697)
 + * Don't regenerate bloomfilter and summaries on startup (CASSANDRA-11163)
 + * Fix NPE when performing comparison against a null frozen in LWT (CASSANDRA-14087)
 + * Log when SSTables are deleted (CASSANDRA-14302)
 + * Fix batch commitlog sync regression (CASSANDRA-14292)
 + * Write to pending endpoint when view replica is also base replica (CASSANDRA-14251)
 + * Chain commit log marker potential performance regression in batch commit mode (CASSANDRA-14194)
 + * Fully utilise specified compaction threads (CASSANDRA-14210)
 + * Pre-create deletion log records to finish compactions quicker (CASSANDRA-12763)
 +Merged from 2.2:
+  * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
+  * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
 - * Fix query pager DEBUG log leak causing hit in paged reads throughput (CASSANDRA-14318)
   * Backport circleci yaml (CASSANDRA-14240)
  Merged from 2.1:
   * Check checksum before decompressing data (CASSANDRA-14284)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/src/java/org/apache/cassandra/cql3/ResultSet.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/selection/Selection.java
index 406f849,5385fc6..6227158
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@@ -399,8 -434,17 +450,10 @@@ public abstract class Selectio
                      sb.append(spec.type.toJSONString(buffer, protocolVersion));
              }
              sb.append("}");
-             return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+             List<ByteBuffer> jsonRow = new ArrayList<>();
+             jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+             return jsonRow;
          }
 -
 -        private ByteBuffer value(Cell c)
 -        {
 -            return (c instanceof CounterCell)
 -                ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value()))
 -                : c.value();
 -        }
      }
  
      private static interface Selectors

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index 1e867bc,729cf83..a5e6254
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@@ -911,14 -841,14 +911,14 @@@ public class SelectStatement implement
  
              if (!parameters.orderings.isEmpty())
              {
 +                assert !forView;
                  verifyOrderingIsAllowed(restrictions);
-                 orderingComparator = getOrderingComparator(cfm, selection, restrictions);
+                 orderingComparator = getOrderingComparator(cfm, selection, restrictions, parameters.isJson);
                  isReversed = isReversed(cfm);
 +                if (isReversed)
 +                    orderingComparator = Collections.reverseOrder(orderingComparator);
              }
  
 -            if (isReversed)
 -                restrictions.reverse();
 -
              checkNeedsFiltering(restrictions);
  
              SelectStatement stmt = new SelectStatement(cfm,

http://git-wip-us.apache.org/repos/asf/cassandra/blob/598008d4/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


[03/10] cassandra git commit: Fix JSON queries with IN restrictions and ORDER BY clause

Posted by bl...@apache.org.
Fix JSON queries with IN restrictions and ORDER BY clause

patch by Francisco Fernandez; reviewed by Benjamin Lerer for CASSANDRA-14286


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/594cde79
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/594cde79
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/594cde79

Branch: refs/heads/cassandra-3.11
Commit: 594cde7937de6f848998bac35d35591f8a18aa10
Parents: b3ac793
Author: Francisco Fernandez <fr...@gmail.com>
Authored: Tue Apr 17 12:02:25 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:04:33 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../org/apache/cassandra/cql3/ResultSet.java    |  5 ++
 .../cassandra/cql3/selection/Selection.java     | 69 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 19 +++---
 .../cql3/validation/entities/JsonTest.java      | 21 ++++++
 5 files changed, 96 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 5221b1e..967ee05 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.2.13
+ * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
  * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
  * Fix query pager DEBUG log leak causing hit in paged reads throughput (CASSANDRA-14318)
  * Backport circleci yaml (CASSANDRA-14240)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/ResultSet.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/ResultSet.java b/src/java/org/apache/cassandra/cql3/ResultSet.java
index 028691f..16f0d1b 100644
--- a/src/java/org/apache/cassandra/cql3/ResultSet.java
+++ b/src/java/org/apache/cassandra/cql3/ResultSet.java
@@ -254,6 +254,11 @@ public class ResultSet
             return new ResultMetadata(EnumSet.copyOf(flags), names, columnCount, pagingState);
         }
 
+        public int getColumnCount()
+        {
+            return columnCount;
+        }
+
         /**
          * Return only the column names requested by the user, excluding those added for post-query re-orderings,
          * see definition of names and columnCount.

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/selection/Selection.java b/src/java/org/apache/cassandra/cql3/selection/Selection.java
index dc4e240..5385fc6 100644
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@ -24,6 +24,7 @@ import com.google.common.base.Objects;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Iterators;
+import com.google.common.collect.Lists;
 
 import org.apache.cassandra.config.CFMetaData;
 import org.apache.cassandra.config.ColumnDefinition;
@@ -56,6 +57,8 @@ public abstract class Selection
     private final ResultSet.ResultMetadata metadata;
     private final boolean collectTimestamps;
     private final boolean collectTTLs;
+    // Columns used to order the result set for multi-partition queries
+    private Map<ColumnDefinition, Integer> orderingIndex;
 
     protected Selection(CFMetaData cfm,
                         List<ColumnDefinition> columns,
@@ -130,6 +133,24 @@ public abstract class Selection
         return false;
     }
 
+    public Map<ColumnDefinition, Integer> getOrderingIndex(boolean isJson)
+    {
+        if (!isJson)
+            return orderingIndex;
+
+        // If we order post-query in json, the first and only column that we ship to the client is the json column.
+        // In that case, we should keep ordering columns around to perform the ordering, then these columns will
+        // be placed after the json column. As a consequence of where the colums are placed, we should give the
+        // ordering index a value based on their position in the json encoding and discard the original index.
+        // (CASSANDRA-14286)
+        int columnIndex = 1;
+        Map<ColumnDefinition, Integer> jsonOrderingIndex = new LinkedHashMap<>(orderingIndex.size());
+        for (ColumnDefinition column : orderingIndex.keySet())
+            jsonOrderingIndex.put(column, columnIndex++);
+
+        return jsonOrderingIndex;
+    }
+
     public ResultSet.ResultMetadata getResultMetadata(boolean isJson)
     {
         if (!isJson)
@@ -137,7 +158,13 @@ public abstract class Selection
 
         ColumnSpecification firstColumn = metadata.names.get(0);
         ColumnSpecification jsonSpec = new ColumnSpecification(firstColumn.ksName, firstColumn.cfName, Json.JSON_COLUMN_ID, UTF8Type.instance);
-        return new ResultSet.ResultMetadata(Arrays.asList(jsonSpec));
+        ResultSet.ResultMetadata resultMetadata = new ResultSet.ResultMetadata(Lists.newArrayList(jsonSpec));
+        if (orderingIndex != null)
+        {
+            for (ColumnDefinition orderingColumn : orderingIndex.keySet())
+                resultMetadata.addNonSerializedColumn(orderingColumn);
+        }
+        return resultMetadata;
     }
 
     public static Selection wildcard(CFMetaData cfm)
@@ -152,7 +179,20 @@ public abstract class Selection
         return new SimpleSelection(cfm, columns, false);
     }
 
-    public int addColumnForOrdering(ColumnDefinition c)
+    public void addColumnForOrdering(ColumnDefinition c)
+    {
+        if (orderingIndex == null)
+            orderingIndex = new LinkedHashMap<>();
+
+        int index = getResultSetIndex(c);
+
+        if (index < 0)
+            index = addOrderingColumn(c);
+
+        orderingIndex.put(c, index);
+    }
+
+    protected int addOrderingColumn(ColumnDefinition c)
     {
         columns.add(c);
         metadata.addNonSerializedColumn(c);
@@ -356,14 +396,25 @@ public abstract class Selection
         private List<ByteBuffer> getOutputRow(int protocolVersion)
         {
             List<ByteBuffer> outputRow = selectors.getOutputRow(protocolVersion);
-            return isJson ? rowToJson(outputRow, protocolVersion)
-                          : outputRow;
+            if (isJson)
+            {
+                List<ByteBuffer> jsonRow = rowToJson(outputRow, protocolVersion);
+
+                // Keep ordering columns around for possible post-query ordering. (CASSANDRA-14286)
+                if (orderingIndex != null)
+                {
+                    for (Integer orderingColumnIndex : orderingIndex.values())
+                        jsonRow.add(outputRow.get(orderingColumnIndex));
+                }
+                outputRow = jsonRow;
+            }
+            return outputRow;
         }
 
         private List<ByteBuffer> rowToJson(List<ByteBuffer> row, int protocolVersion)
         {
             StringBuilder sb = new StringBuilder("{");
-            for (int i = 0; i < metadata.names.size(); i++)
+            for (int i = 0; i < metadata.getColumnCount(); i++)
             {
                 if (i > 0)
                     sb.append(", ");
@@ -383,7 +434,9 @@ public abstract class Selection
                     sb.append(spec.type.toJSONString(buffer, protocolVersion));
             }
             sb.append("}");
-            return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+            List<ByteBuffer> jsonRow = new ArrayList<>();
+            jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
+            return jsonRow;
         }
 
         private ByteBuffer value(Cell c)
@@ -515,9 +568,9 @@ public abstract class Selection
         }
 
         @Override
-        public int addColumnForOrdering(ColumnDefinition c)
+        protected int addOrderingColumn(ColumnDefinition c)
         {
-            int index = super.addColumnForOrdering(c);
+            int index = super.addOrderingColumn(c);
             factories.addSelectorForOrdering(c, index);
             return factories.size() - 1;
         }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index 13276c7..729cf83 100644
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@ -842,7 +842,7 @@ public class SelectStatement implements CQLStatement
             if (!parameters.orderings.isEmpty())
             {
                 verifyOrderingIsAllowed(restrictions);
-                orderingComparator = getOrderingComparator(cfm, selection, restrictions);
+                orderingComparator = getOrderingComparator(cfm, selection, restrictions, parameters.isJson);
                 isReversed = isReversed(cfm);
             }
 
@@ -942,13 +942,14 @@ public class SelectStatement implements CQLStatement
 
         private Comparator<List<ByteBuffer>> getOrderingComparator(CFMetaData cfm,
                                                                    Selection selection,
-                                                                   StatementRestrictions restrictions)
+                                                                   StatementRestrictions restrictions,
+                                                                   boolean isJson)
                                                                    throws InvalidRequestException
         {
             if (!restrictions.keyIsInRelation())
                 return null;
 
-            Map<ColumnIdentifier, Integer> orderingIndexes = getOrderingIndex(cfm, selection);
+            Map<ColumnDefinition, Integer> orderingIndexes = getOrderingIndex(cfm, selection, isJson);
 
             List<Integer> idToSort = new ArrayList<Integer>();
             List<Comparator<ByteBuffer>> sorters = new ArrayList<Comparator<ByteBuffer>>();
@@ -957,32 +958,28 @@ public class SelectStatement implements CQLStatement
             {
                 ColumnIdentifier identifier = raw.prepare(cfm);
                 ColumnDefinition orderingColumn = cfm.getColumnDefinition(identifier);
-                idToSort.add(orderingIndexes.get(orderingColumn.name));
+                idToSort.add(orderingIndexes.get(orderingColumn));
                 sorters.add(orderingColumn.type);
             }
             return idToSort.size() == 1 ? new SingleColumnComparator(idToSort.get(0), sorters.get(0))
                     : new CompositeComparator(sorters, idToSort);
         }
 
-        private Map<ColumnIdentifier, Integer> getOrderingIndex(CFMetaData cfm, Selection selection)
+        private Map<ColumnDefinition, Integer> getOrderingIndex(CFMetaData cfm, Selection selection, boolean isJson)
                 throws InvalidRequestException
         {
             // If we order post-query (see orderResults), the sorted column needs to be in the ResultSet for sorting,
             // even if we don't
             // ultimately ship them to the client (CASSANDRA-4911).
-            Map<ColumnIdentifier, Integer> orderingIndexes = new HashMap<>();
             for (ColumnIdentifier.Raw raw : parameters.orderings.keySet())
             {
                 ColumnIdentifier column = raw.prepare(cfm);
                 final ColumnDefinition def = cfm.getColumnDefinition(column);
                 if (def == null)
                     handleUnrecognizedOrderingColumn(column);
-                int index = selection.getResultSetIndex(def);
-                if (index < 0)
-                    index = selection.addColumnForOrdering(def);
-                orderingIndexes.put(def.name, index);
+                selection.addColumnForOrdering(def);
             }
-            return orderingIndexes;
+            return selection.getOrderingIndex(isJson);
         }
 
         private boolean isReversed(CFMetaData cfm) throws InvalidRequestException

http://git-wip-us.apache.org/repos/asf/cassandra/blob/594cde79/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
index 46cdd52..9d40038 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
@@ -1264,4 +1264,25 @@ public class JsonTest extends CQLTester
         executor.shutdown();
         Assert.assertTrue(executor.awaitTermination(30, TimeUnit.SECONDS));
     }
+
+    // CASSANDRA-14286
+    @Test
+    public void testJsonOrdering() throws Throwable
+    {
+        createTable("CREATE TABLE %s( PRIMARY KEY (a, b), a INT, b INT);");
+        execute("INSERT INTO %s(a, b) VALUES (20, 30);");
+        execute("INSERT INTO %s(a, b) VALUES (100, 200);");
+
+        assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b"),
+                   row("{\"a\": 20, \"b\": 30}"),
+                   row("{\"a\": 100, \"b\": 200}"));
+
+        assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                   row("{\"a\": 100, \"b\": 200}"),
+                   row("{\"a\": 20, \"b\": 30}"));
+
+        assertRows(execute("SELECT JSON a FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                   row("{\"a\": 100}"),
+                   row("{\"a\": 20}"));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


[09/10] cassandra git commit: Merge branch cassandra-3.0 into cassandra-3.11

Posted by bl...@apache.org.
Merge branch cassandra-3.0 into cassandra-3.11


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/95cfee62
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/95cfee62
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/95cfee62

Branch: refs/heads/trunk
Commit: 95cfee623456f1df51ce548cc6cf42fe5a78050c
Parents: 845243d 598008d
Author: Benjamin Lerer <b....@gmail.com>
Authored: Tue Apr 17 12:14:58 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:27:25 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../org/apache/cassandra/cql3/ResultSet.java    |  5 ++
 .../cassandra/cql3/selection/Selection.java     | 70 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 20 +++---
 .../cql3/validation/entities/JsonTest.java      | 41 +++++++++++-
 5 files changed, 116 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/CHANGES.txt
----------------------------------------------------------------------
diff --cc CHANGES.txt
index 4c513d7,d3d8036..42ea3b4
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@@ -25,6 -15,8 +25,7 @@@ Merged from 3.0
   * Fully utilise specified compaction threads (CASSANDRA-14210)
   * Pre-create deletion log records to finish compactions quicker (CASSANDRA-12763)
  Merged from 2.2:
+  * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
 - * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
   * Backport circleci yaml (CASSANDRA-14240)
  Merged from 2.1:
   * Check checksum before decompressing data (CASSANDRA-14284)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/src/java/org/apache/cassandra/cql3/ResultSet.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/selection/Selection.java
index 7b4e80c,6227158..fb0b60c
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@@ -271,41 -288,13 +311,43 @@@ public abstract class Selectio
      @Override
      public String toString()
      {
 -        return Objects.toStringHelper(this)
 -                .add("columns", columns)
 -                .add("columnMapping", columnMapping)
 -                .add("metadata", metadata)
 -                .add("collectTimestamps", collectTimestamps)
 -                .add("collectTTLs", collectTTLs)
 -                .toString();
 +        return MoreObjects.toStringHelper(this)
 +                          .add("columns", columns)
 +                          .add("columnMapping", columnMapping)
 +                          .add("metadata", metadata)
 +                          .add("collectTimestamps", collectTimestamps)
 +                          .add("collectTTLs", collectTTLs)
 +                          .toString();
 +    }
 +
 +    public static List<ByteBuffer> rowToJson(List<ByteBuffer> row, ProtocolVersion protocolVersion, ResultSet.ResultMetadata metadata)
 +    {
 +        StringBuilder sb = new StringBuilder("{");
-         for (int i = 0; i < metadata.names.size(); i++)
++        for (int i = 0; i < metadata.getColumnCount(); i++)
 +        {
 +            if (i > 0)
 +                sb.append(", ");
 +
 +            ColumnSpecification spec = metadata.names.get(i);
 +            String columnName = spec.name.toString();
 +            if (!columnName.equals(columnName.toLowerCase(Locale.US)))
 +                columnName = "\"" + columnName + "\"";
 +
 +            ByteBuffer buffer = row.get(i);
 +            sb.append('"');
 +            sb.append(Json.quoteAsJsonString(columnName));
 +            sb.append("\": ");
 +            if (buffer == null)
 +                sb.append("null");
 +            else if (!buffer.hasRemaining())
 +                sb.append("\"\"");
 +            else
 +                sb.append(spec.type.toJSONString(buffer, protocolVersion));
 +        }
 +        sb.append("}");
-         return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
++        List<ByteBuffer> jsonRow = new ArrayList<>();
++        jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
++        return jsonRow;
      }
  
      public class ResultSetBuilder
@@@ -445,12 -409,51 +487,24 @@@
              return resultSet;
          }
  
 -        private List<ByteBuffer> getOutputRow(int protocolVersion)
 +        private List<ByteBuffer> getOutputRow()
          {
              List<ByteBuffer> outputRow = selectors.getOutputRow(protocolVersion);
-             return isJson ? rowToJson(outputRow, protocolVersion, metadata)
-                           : outputRow;
+             if (isJson)
+             {
 -                List<ByteBuffer> jsonRow = rowToJson(outputRow, protocolVersion);
++                // Keep all columns around for possible post-query ordering. (CASSANDRA-14286)
++                List<ByteBuffer> jsonRow = rowToJson(outputRow, protocolVersion, metadata);
+ 
+                 // Keep ordering columns around for possible post-query ordering. (CASSANDRA-14286)
+                 if (orderingIndex != null)
+                 {
+                     for (Integer orderingColumnIndex : orderingIndex.values())
+                         jsonRow.add(outputRow.get(orderingColumnIndex));
+                 }
+                 outputRow = jsonRow;
+             }
+             return outputRow;
          }
 -
 -        private List<ByteBuffer> rowToJson(List<ByteBuffer> row, int protocolVersion)
 -        {
 -            StringBuilder sb = new StringBuilder("{");
 -            for (int i = 0; i < metadata.getColumnCount(); i++)
 -            {
 -                if (i > 0)
 -                    sb.append(", ");
 -
 -                ColumnSpecification spec = metadata.names.get(i);
 -                String columnName = spec.name.toString();
 -                if (!columnName.equals(columnName.toLowerCase(Locale.US)))
 -                    columnName = "\"" + columnName + "\"";
 -
 -                ByteBuffer buffer = row.get(i);
 -                sb.append('"');
 -                sb.append(Json.quoteAsJsonString(columnName));
 -                sb.append("\": ");
 -                if (buffer == null || !buffer.hasRemaining())
 -                    sb.append("null");
 -                else
 -                    sb.append(spec.type.toJSONString(buffer, protocolVersion));
 -            }
 -            sb.append("}");
 -            List<ByteBuffer> jsonRow = new ArrayList<>();
 -            jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
 -            return jsonRow;
 -        }
      }
  
      private static interface Selectors

http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index a7ba6aa,a5e6254..348fa52
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@@ -22,7 -22,7 +22,6 @@@ import java.util.ArrayList
  import java.util.Collection;
  import java.util.Collections;
  import java.util.Comparator;
--import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
@@@ -1159,10 -1028,11 +1159,10 @@@ public class SelectStatement implement
              List<Integer> idToSort = new ArrayList<Integer>();
              List<Comparator<ByteBuffer>> sorters = new ArrayList<Comparator<ByteBuffer>>();
  
 -            for (ColumnIdentifier.Raw raw : parameters.orderings.keySet())
 +            for (ColumnDefinition.Raw raw : parameters.orderings.keySet())
              {
 -                ColumnIdentifier identifier = raw.prepare(cfm);
 -                ColumnDefinition orderingColumn = cfm.getColumnDefinition(identifier);
 +                ColumnDefinition orderingColumn = raw.prepare(cfm);
-                 idToSort.add(orderingIndexes.get(orderingColumn.name));
+                 idToSort.add(orderingIndexes.get(orderingColumn));
                  sorters.add(orderingColumn.type);
              }
              return idToSort.size() == 1 ? new SingleColumnComparator(idToSort.get(0), sorters.get(0))
@@@ -1175,16 -1045,15 +1175,12 @@@
              // If we order post-query (see orderResults), the sorted column needs to be in the ResultSet for sorting,
              // even if we don't
              // ultimately ship them to the client (CASSANDRA-4911).
-             Map<ColumnIdentifier, Integer> orderingIndexes = new HashMap<>();
 -            for (ColumnIdentifier.Raw raw : parameters.orderings.keySet())
 +            for (ColumnDefinition.Raw raw : parameters.orderings.keySet())
              {
 -                ColumnIdentifier column = raw.prepare(cfm);
 -                final ColumnDefinition def = cfm.getColumnDefinition(column);
 -                if (def == null)
 -                    handleUnrecognizedOrderingColumn(column);
 +                final ColumnDefinition def = raw.prepare(cfm);
-                 int index = selection.getResultSetIndex(def);
-                 if (index < 0)
-                     index = selection.addColumnForOrdering(def);
-                 orderingIndexes.put(def.name, index);
+                 selection.addColumnForOrdering(def);
              }
-             return orderingIndexes;
+             return selection.getOrderingIndex(isJson);
          }
  
          private boolean isReversed(CFMetaData cfm) throws InvalidRequestException

http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------
diff --cc test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
index a475671,e6537d0..16df6e1
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
@@@ -1356,17 -1267,24 +1356,56 @@@ public class JsonTest extends CQLTeste
          Assert.assertTrue(executor.awaitTermination(30, TimeUnit.SECONDS));
      }
  
-     @Test
++   @Test
 +    public void emptyStringJsonSerializationTest() throws Throwable
 +    {
 +        createTable("create table %s(id INT, name TEXT, PRIMARY KEY(id));");
 +        execute("insert into %s(id, name) VALUES (0, 'Foo');");
 +        execute("insert into %s(id, name) VALUES (2, '');");
 +        execute("insert into %s(id, name) VALUES (3, null);");
 +
 +        assertRows(execute("SELECT JSON * FROM %s"),
 +                   row("{\"id\": 0, \"name\": \"Foo\"}"),
 +                   row("{\"id\": 2, \"name\": \"\"}"),
 +                   row("{\"id\": 3, \"name\": null}"));
 +    }
++
+     // CASSANDRA-14286
+     @Test
+     public void testJsonOrdering() throws Throwable
+     {
+         createTable("CREATE TABLE %s( PRIMARY KEY (a, b), a INT, b INT);");
+         execute("INSERT INTO %s(a, b) VALUES (20, 30);");
+         execute("INSERT INTO %s(a, b) VALUES (100, 200);");
+ 
+         assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b"),
+                    row("{\"a\": 20, \"b\": 30}"),
+                    row("{\"a\": 100, \"b\": 200}"));
+ 
+         assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                    row("{\"a\": 100, \"b\": 200}"),
+                    row("{\"a\": 20, \"b\": 30}"));
+ 
+         assertRows(execute("SELECT JSON a FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                    row("{\"a\": 100}"),
+                    row("{\"a\": 20}"));
++
++        // Check ordering with alias 
++        assertRows(execute("SELECT JSON a, b as c FROM %s WHERE a IN (20, 100) ORDER BY b"),
++                   row("{\"a\": 20, \"c\": 30}"),
++                   row("{\"a\": 100, \"c\": 200}"));
++
++        assertRows(execute("SELECT JSON a, b as c FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
++                   row("{\"a\": 100, \"c\": 200}"),
++                   row("{\"a\": 20, \"c\": 30}"));
++
++        // Check ordering with CAST 
++        assertRows(execute("SELECT JSON a, CAST(b AS FLOAT) FROM %s WHERE a IN (20, 100) ORDER BY b"),
++                   row("{\"a\": 20, \"cast(b as float)\": 30.0}"),
++                   row("{\"a\": 100, \"cast(b as float)\": 200.0}"));
++
++        assertRows(execute("SELECT JSON a, CAST(b AS FLOAT) FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
++                   row("{\"a\": 100, \"cast(b as float)\": 200.0}"),
++                   row("{\"a\": 20, \"cast(b as float)\": 30.0}"));
+     }
  }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


[10/10] cassandra git commit: Merge branch cassandra-3.11 into trunk

Posted by bl...@apache.org.
Merge branch cassandra-3.11 into trunk


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/05d7661d
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/05d7661d
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/05d7661d

Branch: refs/heads/trunk
Commit: 05d7661d04b2cd47b29a767986d4b6d283419db2
Parents: 09f3c96 95cfee6
Author: Benjamin Lerer <b....@gmail.com>
Authored: Tue Apr 17 12:28:16 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:30:53 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  6 +-
 .../cassandra/cql3/selection/Selection.java     | 66 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 23 +------
 .../cql3/validation/entities/JsonTest.java      | 39 ++++++++++++
 4 files changed, 103 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/05d7661d/CHANGES.txt
----------------------------------------------------------------------
diff --cc CHANGES.txt
index 71d947a,42ea3b4..4c557b4
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@@ -1,229 -1,7 +1,228 @@@
 +4.0
 + * Use standard Amazon naming for datacenter and rack in Ec2Snitch (CASSANDRA-7839)
 + * Fix junit failure for SSTableReaderTest (CASSANDRA-14387)
 + * Abstract write path for pluggable storage (CASSANDRA-14118)
 + * nodetool describecluster should be more informative (CASSANDRA-13853)
 + * Compaction performance improvements (CASSANDRA-14261) 
 + * Refactor Pair usage to avoid boxing ints/longs (CASSANDRA-14260)
 + * Add options to nodetool tablestats to sort and limit output (CASSANDRA-13889)
 + * Rename internals to reflect CQL vocabulary (CASSANDRA-14354)
 + * Add support for hybrid MIN(), MAX() speculative retry policies
 +   (CASSANDRA-14293, CASSANDRA-14338, CASSANDRA-14352)
 + * Fix some regressions caused by 14058 (CASSANDRA-14353)
 + * Abstract repair for pluggable storage (CASSANDRA-14116)
 + * Add meaningful toString() impls (CASSANDRA-13653)
 + * Add sstableloader option to accept target keyspace name (CASSANDRA-13884)
 + * Move processing of EchoMessage response to gossip stage (CASSANDRA-13713)
 + * Add coordinator write metric per CF (CASSANDRA-14232)
 + * Correct and clarify SSLFactory.getSslContext method and call sites (CASSANDRA-14314)
 + * Handle static and partition deletion properly on ThrottledUnfilteredIterator (CASSANDRA-14315)
 + * NodeTool clientstats should show SSL Cipher (CASSANDRA-14322)
 + * Add ability to specify driver name and version (CASSANDRA-14275)
 + * Abstract streaming for pluggable storage (CASSANDRA-14115)
 + * Forced incremental repairs should promote sstables if they can (CASSANDRA-14294)
 + * Use Murmur3 for validation compactions (CASSANDRA-14002)
 + * Comma at the end of the seed list is interpretated as localhost (CASSANDRA-14285)
 + * Refactor read executor and response resolver, abstract read repair (CASSANDRA-14058)
 + * Add optional startup delay to wait until peers are ready (CASSANDRA-13993)
 + * Add a few options to nodetool verify (CASSANDRA-14201)
 + * CVE-2017-5929 Security vulnerability and redefine default log rotation policy (CASSANDRA-14183)
 + * Use JVM default SSL validation algorithm instead of custom default (CASSANDRA-13259)
 + * Better document in code InetAddressAndPort usage post 7544, incorporate port into UUIDGen node (CASSANDRA-14226)
 + * Fix sstablemetadata date string for minLocalDeletionTime (CASSANDRA-14132)
 + * Make it possible to change neverPurgeTombstones during runtime (CASSANDRA-14214)
 + * Remove GossipDigestSynVerbHandler#doSort() (CASSANDRA-14174)
 + * Add nodetool clientlist (CASSANDRA-13665)
 + * Revert ProtocolVersion changes from CASSANDRA-7544 (CASSANDRA-14211)
 + * Non-disruptive seed node list reload (CASSANDRA-14190)
 + * Nodetool tablehistograms to print statics for all the tables (CASSANDRA-14185)
 + * Migrate dtests to use pytest and python3 (CASSANDRA-14134)
 + * Allow storage port to be configurable per node (CASSANDRA-7544)
 + * Make sub-range selection for non-frozen collections return null instead of empty (CASSANDRA-14182)
 + * BloomFilter serialization format should not change byte ordering (CASSANDRA-9067)
 + * Remove unused on-heap BloomFilter implementation (CASSANDRA-14152)
 + * Delete temp test files on exit (CASSANDRA-14153)
 + * Make PartitionUpdate and Mutation immutable (CASSANDRA-13867)
 + * Fix CommitLogReplayer exception for CDC data (CASSANDRA-14066)
 + * Fix cassandra-stress startup failure (CASSANDRA-14106)
 + * Remove initialDirectories from CFS (CASSANDRA-13928)
 + * Fix trivial log format error (CASSANDRA-14015)
 + * Allow sstabledump to do a json object per partition (CASSANDRA-13848)
 + * Add option to optimise merkle tree comparison across replicas (CASSANDRA-3200)
 + * Remove unused and deprecated methods from AbstractCompactionStrategy (CASSANDRA-14081)
 + * Fix Distribution.average in cassandra-stress (CASSANDRA-14090)
 + * Support a means of logging all queries as they were invoked (CASSANDRA-13983)
 + * Presize collections (CASSANDRA-13760)
 + * Add GroupCommitLogService (CASSANDRA-13530)
 + * Parallelize initial materialized view build (CASSANDRA-12245)
 + * Fix flaky SecondaryIndexManagerTest.assert[Not]MarkedAsBuilt (CASSANDRA-13965)
 + * Make LWTs send resultset metadata on every request (CASSANDRA-13992)
 + * Fix flaky indexWithFailedInitializationIsNotQueryableAfterPartialRebuild (CASSANDRA-13963)
 + * Introduce leaf-only iterator (CASSANDRA-9988)
 + * Upgrade Guava to 23.3 and Airline to 0.8 (CASSANDRA-13997)
 + * Allow only one concurrent call to StatusLogger (CASSANDRA-12182)
 + * Refactoring to specialised functional interfaces (CASSANDRA-13982)
 + * Speculative retry should allow more friendly params (CASSANDRA-13876)
 + * Throw exception if we send/receive repair messages to incompatible nodes (CASSANDRA-13944)
 + * Replace usages of MessageDigest with Guava's Hasher (CASSANDRA-13291)
 + * Add nodetool cmd to print hinted handoff window (CASSANDRA-13728)
 + * Fix some alerts raised by static analysis (CASSANDRA-13799)
 + * Checksum sstable metadata (CASSANDRA-13321, CASSANDRA-13593)
 + * Add result set metadata to prepared statement MD5 hash calculation (CASSANDRA-10786)
 + * Refactor GcCompactionTest to avoid boxing (CASSANDRA-13941)
 + * Expose recent histograms in JmxHistograms (CASSANDRA-13642)
 + * Fix buffer length comparison when decompressing in netty-based streaming (CASSANDRA-13899)
 + * Properly close StreamCompressionInputStream to release any ByteBuf (CASSANDRA-13906)
 + * Add SERIAL and LOCAL_SERIAL support for cassandra-stress (CASSANDRA-13925)
 + * LCS needlessly checks for L0 STCS candidates multiple times (CASSANDRA-12961)
 + * Correctly close netty channels when a stream session ends (CASSANDRA-13905)
 + * Update lz4 to 1.4.0 (CASSANDRA-13741)
 + * Optimize Paxos prepare and propose stage for local requests (CASSANDRA-13862)
 + * Throttle base partitions during MV repair streaming to prevent OOM (CASSANDRA-13299)
 + * Use compaction threshold for STCS in L0 (CASSANDRA-13861)
 + * Fix problem with min_compress_ratio: 1 and disallow ratio < 1 (CASSANDRA-13703)
 + * Add extra information to SASI timeout exception (CASSANDRA-13677)
 + * Add incremental repair support for --hosts, --force, and subrange repair (CASSANDRA-13818)
 + * Rework CompactionStrategyManager.getScanners synchronization (CASSANDRA-13786)
 + * Add additional unit tests for batch behavior, TTLs, Timestamps (CASSANDRA-13846)
 + * Add keyspace and table name in schema validation exception (CASSANDRA-13845)
 + * Emit metrics whenever we hit tombstone failures and warn thresholds (CASSANDRA-13771)
 + * Make netty EventLoopGroups daemon threads (CASSANDRA-13837)
 + * Race condition when closing stream sessions (CASSANDRA-13852)
 + * NettyFactoryTest is failing in trunk on macOS (CASSANDRA-13831)
 + * Allow changing log levels via nodetool for related classes (CASSANDRA-12696)
 + * Add stress profile yaml with LWT (CASSANDRA-7960)
 + * Reduce memory copies and object creations when acting on ByteBufs (CASSANDRA-13789)
 + * Simplify mx4j configuration (Cassandra-13578)
 + * Fix trigger example on 4.0 (CASSANDRA-13796)
 + * Force minumum timeout value (CASSANDRA-9375)
 + * Use netty for streaming (CASSANDRA-12229)
 + * Use netty for internode messaging (CASSANDRA-8457)
 + * Add bytes repaired/unrepaired to nodetool tablestats (CASSANDRA-13774)
 + * Don't delete incremental repair sessions if they still have sstables (CASSANDRA-13758)
 + * Fix pending repair manager index out of bounds check (CASSANDRA-13769)
 + * Don't use RangeFetchMapCalculator when RF=1 (CASSANDRA-13576)
 + * Don't optimise trivial ranges in RangeFetchMapCalculator (CASSANDRA-13664)
 + * Use an ExecutorService for repair commands instead of new Thread(..).start() (CASSANDRA-13594)
 + * Fix race / ref leak in anticompaction (CASSANDRA-13688)
 + * Expose tasks queue length via JMX (CASSANDRA-12758)
 + * Fix race / ref leak in PendingRepairManager (CASSANDRA-13751)
 + * Enable ppc64le runtime as unsupported architecture (CASSANDRA-13615)
 + * Improve sstablemetadata output (CASSANDRA-11483)
 + * Support for migrating legacy users to roles has been dropped (CASSANDRA-13371)
 + * Introduce error metrics for repair (CASSANDRA-13387)
 + * Refactoring to primitive functional interfaces in AuthCache (CASSANDRA-13732)
 + * Update metrics to 3.1.5 (CASSANDRA-13648)
 + * batch_size_warn_threshold_in_kb can now be set at runtime (CASSANDRA-13699)
 + * Avoid always rebuilding secondary indexes at startup (CASSANDRA-13725)
 + * Upgrade JMH from 1.13 to 1.19 (CASSANDRA-13727)
 + * Upgrade SLF4J from 1.7.7 to 1.7.25 (CASSANDRA-12996)
 + * Default for start_native_transport now true if not set in config (CASSANDRA-13656)
 + * Don't add localhost to the graph when calculating where to stream from (CASSANDRA-13583)
 + * Make CDC availability more deterministic via hard-linking (CASSANDRA-12148)
 + * Allow skipping equality-restricted clustering columns in ORDER BY clause (CASSANDRA-10271)
 + * Use common nowInSec for validation compactions (CASSANDRA-13671)
 + * Improve handling of IR prepare failures (CASSANDRA-13672)
 + * Send IR coordinator messages synchronously (CASSANDRA-13673)
 + * Flush system.repair table before IR finalize promise (CASSANDRA-13660)
 + * Fix column filter creation for wildcard queries (CASSANDRA-13650)
 + * Add 'nodetool getbatchlogreplaythrottle' and 'nodetool setbatchlogreplaythrottle' (CASSANDRA-13614)
 + * fix race condition in PendingRepairManager (CASSANDRA-13659)
 + * Allow noop incremental repair state transitions (CASSANDRA-13658)
 + * Run repair with down replicas (CASSANDRA-10446)
 + * Added started & completed repair metrics (CASSANDRA-13598)
 + * Added started & completed repair metrics (CASSANDRA-13598)
 + * Improve secondary index (re)build failure and concurrency handling (CASSANDRA-10130)
 + * Improve calculation of available disk space for compaction (CASSANDRA-13068)
 + * Change the accessibility of RowCacheSerializer for third party row cache plugins (CASSANDRA-13579)
 + * Allow sub-range repairs for a preview of repaired data (CASSANDRA-13570)
 + * NPE in IR cleanup when columnfamily has no sstables (CASSANDRA-13585)
 + * Fix Randomness of stress values (CASSANDRA-12744)
 + * Allow selecting Map values and Set elements (CASSANDRA-7396)
 + * Fast and garbage-free Streaming Histogram (CASSANDRA-13444)
 + * Update repairTime for keyspaces on completion (CASSANDRA-13539)
 + * Add configurable upper bound for validation executor threads (CASSANDRA-13521)
 + * Bring back maxHintTTL propery (CASSANDRA-12982)
 + * Add testing guidelines (CASSANDRA-13497)
 + * Add more repair metrics (CASSANDRA-13531)
 + * RangeStreamer should be smarter when picking endpoints for streaming (CASSANDRA-4650)
 + * Avoid rewrapping an exception thrown for cache load functions (CASSANDRA-13367)
 + * Log time elapsed for each incremental repair phase (CASSANDRA-13498)
 + * Add multiple table operation support to cassandra-stress (CASSANDRA-8780)
 + * Fix incorrect cqlsh results when selecting same columns multiple times (CASSANDRA-13262)
 + * Fix WriteResponseHandlerTest is sensitive to test execution order (CASSANDRA-13421)
 + * Improve incremental repair logging (CASSANDRA-13468)
 + * Start compaction when incremental repair finishes (CASSANDRA-13454)
 + * Add repair streaming preview (CASSANDRA-13257)
 + * Cleanup isIncremental/repairedAt usage (CASSANDRA-13430)
 + * Change protocol to allow sending key space independent of query string (CASSANDRA-10145)
 + * Make gc_log and gc_warn settable at runtime (CASSANDRA-12661)
 + * Take number of files in L0 in account when estimating remaining compaction tasks (CASSANDRA-13354)
 + * Skip building views during base table streams on range movements (CASSANDRA-13065)
 + * Improve error messages for +/- operations on maps and tuples (CASSANDRA-13197)
 + * Remove deprecated repair JMX APIs (CASSANDRA-11530)
 + * Fix version check to enable streaming keep-alive (CASSANDRA-12929)
 + * Make it possible to monitor an ideal consistency level separate from actual consistency level (CASSANDRA-13289)
 + * Outbound TCP connections ignore internode authenticator (CASSANDRA-13324)
 + * Upgrade junit from 4.6 to 4.12 (CASSANDRA-13360)
 + * Cleanup ParentRepairSession after repairs (CASSANDRA-13359)
 + * Upgrade snappy-java to 1.1.2.6 (CASSANDRA-13336)
 + * Incremental repair not streaming correct sstables (CASSANDRA-13328)
 + * Upgrade the jna version to 4.3.0 (CASSANDRA-13300)
 + * Add the currentTimestamp, currentDate, currentTime and currentTimeUUID functions (CASSANDRA-13132)
 + * Remove config option index_interval (CASSANDRA-10671)
 + * Reduce lock contention for collection types and serializers (CASSANDRA-13271)
 + * Make it possible to override MessagingService.Verb ids (CASSANDRA-13283)
 + * Avoid synchronized on prepareForRepair in ActiveRepairService (CASSANDRA-9292)
 + * Adds the ability to use uncompressed chunks in compressed files (CASSANDRA-10520)
 + * Don't flush sstables when streaming for incremental repair (CASSANDRA-13226)
 + * Remove unused method (CASSANDRA-13227)
 + * Fix minor bugs related to #9143 (CASSANDRA-13217)
 + * Output warning if user increases RF (CASSANDRA-13079)
 + * Remove pre-3.0 streaming compatibility code for 4.0 (CASSANDRA-13081)
 + * Add support for + and - operations on dates (CASSANDRA-11936)
 + * Fix consistency of incrementally repaired data (CASSANDRA-9143)
 + * Increase commitlog version (CASSANDRA-13161)
 + * Make TableMetadata immutable, optimize Schema (CASSANDRA-9425)
 + * Refactor ColumnCondition (CASSANDRA-12981)
 + * Parallelize streaming of different keyspaces (CASSANDRA-4663)
 + * Improved compactions metrics (CASSANDRA-13015)
 + * Speed-up start-up sequence by avoiding un-needed flushes (CASSANDRA-13031)
 + * Use Caffeine (W-TinyLFU) for on-heap caches (CASSANDRA-10855)
 + * Thrift removal (CASSANDRA-11115)
 + * Remove pre-3.0 compatibility code for 4.0 (CASSANDRA-12716)
 + * Add column definition kind to dropped columns in schema (CASSANDRA-12705)
 + * Add (automate) Nodetool Documentation (CASSANDRA-12672)
 + * Update bundled cqlsh python driver to 3.7.0 (CASSANDRA-12736)
 + * Reject invalid replication settings when creating or altering a keyspace (CASSANDRA-12681)
 + * Clean up the SSTableReader#getScanner API wrt removal of RateLimiter (CASSANDRA-12422)
 + * Use new token allocation for non bootstrap case as well (CASSANDRA-13080)
 + * Avoid byte-array copy when key cache is disabled (CASSANDRA-13084)
 + * Require forceful decommission if number of nodes is less than replication factor (CASSANDRA-12510)
 + * Allow IN restrictions on column families with collections (CASSANDRA-12654)
 + * Log message size in trace message in OutboundTcpConnection (CASSANDRA-13028)
 + * Add timeUnit Days for cassandra-stress (CASSANDRA-13029)
 + * Add mutation size and batch metrics (CASSANDRA-12649)
 + * Add method to get size of endpoints to TokenMetadata (CASSANDRA-12999)
 + * Expose time spent waiting in thread pool queue (CASSANDRA-8398)
 + * Conditionally update index built status to avoid unnecessary flushes (CASSANDRA-12969)
 + * cqlsh auto completion: refactor definition of compaction strategy options (CASSANDRA-12946)
 + * Add support for arithmetic operators (CASSANDRA-11935)
 + * Add histogram for delay to deliver hints (CASSANDRA-13234)
 + * Fix cqlsh automatic protocol downgrade regression (CASSANDRA-13307)
 + * Changing `max_hint_window_in_ms` at runtime (CASSANDRA-11720)
 + * Trivial format error in StorageProxy (CASSANDRA-13551)
 + * Nodetool repair can hang forever if we lose the notification for the repair completing/failing (CASSANDRA-13480)
 + * Anticompaction can cause noisy log messages (CASSANDRA-13684)
 + * Switch to client init for sstabledump (CASSANDRA-13683)
 + * CQLSH: Don't pause when capturing data (CASSANDRA-13743)
 + * nodetool clearsnapshot requires --all to clear all snapshots (CASSANDRA-13391)
 + * Correctly count range tombstones in traces and tombstone thresholds (CASSANDRA-8527)
 + * cqlshrc.sample uses incorrect option for time formatting (CASSANDRA-14243)
 +
 +
  3.11.3
 - * Fix cqlsh to read connection.ssl cqlshrc option again (CASSANDRA-14299)
   * Downgrade log level to trace for CommitLogSegmentManager (CASSANDRA-14370)
-- * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
   * Serialize empty buffer as empty string for json output format (CASSANDRA-14245)
   * Allow logging implementation to be interchanged for embedded testing (CASSANDRA-13396)
   * SASI tokenizer for simple delimiter based entries (CASSANDRA-14247)
@@@ -246,8 -24,12 +245,11 @@@ Merged from 3.0
   * Chain commit log marker potential performance regression in batch commit mode (CASSANDRA-14194)
   * Fully utilise specified compaction threads (CASSANDRA-14210)
   * Pre-create deletion log records to finish compactions quicker (CASSANDRA-12763)
-  Merged from 2.1:
+ Merged from 2.2:
+  * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
 - * Backport circleci yaml (CASSANDRA-14240)
++ * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
+ Merged from 2.1:
   * Check checksum before decompressing data (CASSANDRA-14284)
 - * CVE-2017-5929 Security vulnerability in Logback warning in NEWS.txt (CASSANDRA-14183)
  
  
  3.11.2

http://git-wip-us.apache.org/repos/asf/cassandra/blob/05d7661d/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/selection/Selection.java
index 38cde1e,fb0b60c..1849b22
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@@ -40,33 -46,35 +40,38 @@@ public abstract class Selectio
      /**
       * A predicate that returns <code>true</code> for static columns.
       */
 -    private static final Predicate<ColumnDefinition> STATIC_COLUMN_FILTER = new Predicate<ColumnDefinition>()
 -    {
 -        public boolean apply(ColumnDefinition def)
 -        {
 -            return def.isStatic();
 -        }
 -    };
 +    private static final Predicate<ColumnMetadata> STATIC_COLUMN_FILTER = (column) -> column.isStatic();
  
 -    private final CFMetaData cfm;
 -    private final List<ColumnDefinition> columns;
 +    private final TableMetadata table;
 +    private final List<ColumnMetadata> columns;
      private final SelectionColumnMapping columnMapping;
 -    private final ResultSet.ResultMetadata metadata;
 -    private final boolean collectTimestamps;
 -    private final boolean collectTTLs;
 -    // Columns used to order the result set for multi-partition queries
 -    private Map<ColumnDefinition, Integer> orderingIndex;
 -
 -    protected Selection(CFMetaData cfm,
 -                        List<ColumnDefinition> columns,
 +    protected final ResultSet.ResultMetadata metadata;
 +    protected final ColumnFilterFactory columnFilterFactory;
 +    protected final boolean isJson;
 +
++    // Columns used to order the result set for JSON queries with post ordering.
++    protected final List<ColumnMetadata> orderingColumns;
++
 +    protected Selection(TableMetadata table,
 +                        List<ColumnMetadata> selectedColumns,
 +                        Set<ColumnMetadata> orderingColumns,
                          SelectionColumnMapping columnMapping,
 -                        boolean collectTimestamps,
 -                        boolean collectTTLs)
 +                        ColumnFilterFactory columnFilterFactory,
 +                        boolean isJson)
      {
 -        this.cfm = cfm;
 -        this.columns = columns;
 +        this.table = table;
 +        this.columns = selectedColumns;
          this.columnMapping = columnMapping;
          this.metadata = new ResultSet.ResultMetadata(columnMapping.getColumnSpecifications());
 -        this.collectTimestamps = collectTimestamps;
 -        this.collectTTLs = collectTTLs;
 +        this.columnFilterFactory = columnFilterFactory;
 +        this.isJson = isJson;
 +
 +        // If we order post-query, the sorted column needs to be in the ResultSet for sorting,
 +        // even if we don't ultimately ship them to the client (CASSANDRA-4911).
 +        this.columns.addAll(orderingColumns);
 +        this.metadata.addNonSerializedColumns(orderingColumns);
++
++        this.orderingColumns = orderingColumns.isEmpty() ? Collections.emptyList() : new ArrayList<>(orderingColumns);
      }
  
      // Overriden by SimpleSelection when appropriate.
@@@ -90,39 -98,117 +95,59 @@@
          return !Iterables.isEmpty(Iterables.filter(columns, STATIC_COLUMN_FILTER));
      }
  
+     /**
 -     * Checks if this selection contains only static columns.
 -     * @return <code>true</code> if this selection contains only static columns, <code>false</code> otherwise;
++     * Returns the corresponding column index used for post query ordering
++     * @param c ordering column
++     * @return
+      */
 -    public boolean containsOnlyStaticColumns()
 -    {
 -        if (!containsStaticColumns())
 -            return false;
 -
 -        if (isWildcard())
 -            return false;
 -
 -        for (ColumnDefinition def : getColumns())
 -        {
 -            if (!def.isPartitionKey() && !def.isStatic())
 -                return false;
 -        }
 -
 -        return true;
 -    }
 -
 -    /**
 -     * Checks if this selection contains a complex column.
 -     *
 -     * @return <code>true</code> if this selection contains a multicell collection or UDT, <code>false</code> otherwise.
 -     */
 -    public boolean containsAComplexColumn()
 -    {
 -        for (ColumnDefinition def : getColumns())
 -            if (def.isComplex())
 -                return true;
 -
 -        return false;
 -    }
 -
 -    public Map<ColumnDefinition, Integer> getOrderingIndex(boolean isJson)
++    public Integer getOrderingIndex(ColumnMetadata c)
+     {
+         if (!isJson)
 -            return orderingIndex;
++            return getResultSetIndex(c);
+ 
+         // If we order post-query in json, the first and only column that we ship to the client is the json column.
+         // In that case, we should keep ordering columns around to perform the ordering, then these columns will
+         // be placed after the json column. As a consequence of where the colums are placed, we should give the
+         // ordering index a value based on their position in the json encoding and discard the original index.
+         // (CASSANDRA-14286)
 -        int columnIndex = 1;
 -        Map<ColumnDefinition, Integer> jsonOrderingIndex = new LinkedHashMap<>(orderingIndex.size());
 -        for (ColumnDefinition column : orderingIndex.keySet())
 -            jsonOrderingIndex.put(column, columnIndex++);
 -
 -        return jsonOrderingIndex;
++        return orderingColumns.indexOf(c) + 1;
+     }
+ 
 -    public ResultSet.ResultMetadata getResultMetadata(boolean isJson)
 +    public ResultSet.ResultMetadata getResultMetadata()
      {
          if (!isJson)
              return metadata;
  
          ColumnSpecification firstColumn = metadata.names.get(0);
          ColumnSpecification jsonSpec = new ColumnSpecification(firstColumn.ksName, firstColumn.cfName, Json.JSON_COLUMN_ID, UTF8Type.instance);
-         return new ResultSet.ResultMetadata(Arrays.asList(jsonSpec));
+         ResultSet.ResultMetadata resultMetadata = new ResultSet.ResultMetadata(Lists.newArrayList(jsonSpec));
 -        if (orderingIndex != null)
 -        {
 -            for (ColumnDefinition orderingColumn : orderingIndex.keySet())
 -                resultMetadata.addNonSerializedColumn(orderingColumn);
 -        }
++        resultMetadata.addNonSerializedColumns(orderingColumns);
+         return resultMetadata;
      }
  
 -    public static Selection wildcard(CFMetaData cfm)
 +    public static Selection wildcard(TableMetadata table, boolean isJson)
      {
 -        List<ColumnDefinition> all = new ArrayList<>(cfm.allColumns().size());
 -        Iterators.addAll(all, cfm.allColumnsInSelectOrder());
 -        return new SimpleSelection(cfm, all, true);
 +        List<ColumnMetadata> all = new ArrayList<>(table.columns().size());
 +        Iterators.addAll(all, table.allColumnsInSelectOrder());
 +        return new SimpleSelection(table, all, Collections.emptySet(), true, isJson);
      }
  
 -    public static Selection wildcardWithGroupBy(CFMetaData cfm, VariableSpecifications boundNames)
 +    public static Selection wildcardWithGroupBy(TableMetadata table,
 +                                                VariableSpecifications boundNames,
 +                                                boolean isJson)
      {
 -        List<RawSelector> rawSelectors = new ArrayList<>(cfm.allColumns().size());
 -        Iterator<ColumnDefinition> iter = cfm.allColumnsInSelectOrder();
 -        while (iter.hasNext())
 -        {
 -            ColumnDefinition.Raw raw = ColumnDefinition.Raw.forColumn(iter.next());
 -            rawSelectors.add(new RawSelector(raw, null));
 -        }
 -        return fromSelectors(cfm, rawSelectors, boundNames, true);
 -    }
 -
 -    public static Selection forColumns(CFMetaData cfm, List<ColumnDefinition> columns)
 -    {
 -        return new SimpleSelection(cfm, columns, false);
 -    }
 -
 -    public void addColumnForOrdering(ColumnDefinition c)
 -    {
 -        if (orderingIndex == null)
 -            orderingIndex = new LinkedHashMap<>();
 -
 -        int index = getResultSetIndex(c);
 -
 -        if (index < 0)
 -            index = addOrderingColumn(c);
 -
 -        orderingIndex.put(c, index);
 +        return fromSelectors(table,
 +                             Lists.newArrayList(table.allColumnsInSelectOrder()),
 +                             boundNames,
 +                             Collections.emptySet(),
 +                             Collections.emptySet(),
 +                             true,
 +                             isJson);
      }
  
 -    protected int addOrderingColumn(ColumnDefinition c)
 +    public static Selection forColumns(TableMetadata table, List<ColumnMetadata> columns)
      {
 -        columns.add(c);
 -        metadata.addNonSerializedColumn(c);
 -        return columns.size() - 1;
 +        return new SimpleSelection(table, columns, Collections.emptySet(), false, false);
      }
  
      public void addFunctionsTo(List<Function> functions)
@@@ -139,62 -225,17 +164,67 @@@
          return false;
      }
  
 -    public static Selection fromSelectors(CFMetaData cfm, List<RawSelector> rawSelectors, VariableSpecifications boundNames, boolean hasGroupBy)
 +    public static Selection fromSelectors(TableMetadata table,
 +                                          List<Selectable> selectables,
 +                                          VariableSpecifications boundNames,
 +                                          Set<ColumnMetadata> orderingColumns,
 +                                          Set<ColumnMetadata> nonPKRestrictedColumns,
 +                                          boolean hasGroupBy,
 +                                          boolean isJson)
      {
 -        List<ColumnDefinition> defs = new ArrayList<>();
 +        List<ColumnMetadata> selectedColumns = new ArrayList<>();
  
          SelectorFactories factories =
 -                SelectorFactories.createFactoriesAndCollectColumnDefinitions(RawSelector.toSelectables(rawSelectors, cfm), null, cfm, defs, boundNames);
 -        SelectionColumnMapping mapping = collectColumnMappings(cfm, rawSelectors, factories);
 +                SelectorFactories.createFactoriesAndCollectColumnDefinitions(selectables, null, table, selectedColumns, boundNames);
 +        SelectionColumnMapping mapping = collectColumnMappings(table, factories);
 +
 +        Set<ColumnMetadata> filteredOrderingColumns = filterOrderingColumns(orderingColumns,
 +                                                                            selectedColumns,
-                                                                             factories);
++                                                                            factories,
++                                                                            isJson);
 +
 +        return (processesSelection(selectables) || selectables.size() != selectedColumns.size() || hasGroupBy)
 +            ? new SelectionWithProcessing(table,
 +                                          selectedColumns,
 +                                          filteredOrderingColumns,
 +                                          nonPKRestrictedColumns,
 +                                          mapping,
 +                                          factories,
 +                                          isJson)
 +            : new SimpleSelection(table,
 +                                  selectedColumns,
 +                                  filteredOrderingColumns,
 +                                  nonPKRestrictedColumns,
 +                                  mapping,
 +                                  isJson);
 +    }
 +
 +    /**
 +     * Removes the ordering columns that are already selected.
 +     *
 +     * @param orderingColumns the columns used to order the results
 +     * @param selectedColumns the selected columns
 +     * @param factories the factory used to create the selectors
 +     * @return the ordering columns that are not part of the selection
 +     */
 +    private static Set<ColumnMetadata> filterOrderingColumns(Set<ColumnMetadata> orderingColumns,
 +                                                             List<ColumnMetadata> selectedColumns,
-                                                              SelectorFactories factories)
++                                                             SelectorFactories factories,
++                                                             boolean isJson)
 +    {
++        // CASSANDRA-14286
++        if (isJson)
++            return orderingColumns;
 +        Set<ColumnMetadata> filteredOrderingColumns = new LinkedHashSet<>(orderingColumns.size());
 +        for (ColumnMetadata orderingColumn : orderingColumns)
 +        {
 +            int index = selectedColumns.indexOf(orderingColumn);
 +            if (index >= 0 && factories.indexOfSimpleSelectorFactory(index) >= 0)
 +                continue;
  
 -        return (processesSelection(rawSelectors) || rawSelectors.size() != defs.size() || hasGroupBy)
 -               ? new SelectionWithProcessing(cfm, defs, mapping, factories)
 -               : new SimpleSelection(cfm, defs, mapping, false);
 +            filteredOrderingColumns.add(orderingColumn);
 +        }
 +        return filteredOrderingColumns;
      }
  
      /**
@@@ -259,20 -318,22 +289,34 @@@
                            .toString();
      }
  
-     private static List<ByteBuffer> rowToJson(List<ByteBuffer> row, ProtocolVersion protocolVersion, ResultSet.ResultMetadata metadata)
 -    public static List<ByteBuffer> rowToJson(List<ByteBuffer> row, ProtocolVersion protocolVersion, ResultSet.ResultMetadata metadata)
++    private static List<ByteBuffer> rowToJson(List<ByteBuffer> row,
++                                              ProtocolVersion protocolVersion,
++                                              ResultSet.ResultMetadata metadata,
++                                              List<ColumnMetadata> orderingColumns)
      {
++        ByteBuffer[] jsonRow = new ByteBuffer[orderingColumns.size() + 1];
          StringBuilder sb = new StringBuilder("{");
 -        for (int i = 0; i < metadata.getColumnCount(); i++)
 +        for (int i = 0; i < metadata.names.size(); i++)
          {
++            ColumnSpecification spec = metadata.names.get(i);
++            ByteBuffer buffer = row.get(i);
++
++            // If it is an ordering column we need to keep it in case we need it for post ordering
++            int index = orderingColumns.indexOf(spec);
++            if (index >= 0)
++                jsonRow[index + 1] = buffer;
++
++            // If the column is only used for ordering we can stop here.
++            if (i >= metadata.getColumnCount())
++                continue;
++
              if (i > 0)
                  sb.append(", ");
  
--            ColumnSpecification spec = metadata.names.get(i);
              String columnName = spec.name.toString();
              if (!columnName.equals(columnName.toLowerCase(Locale.US)))
                  columnName = "\"" + columnName + "\"";
  
--            ByteBuffer buffer = row.get(i);
              sb.append('"');
              sb.append(Json.quoteAsJsonString(columnName));
              sb.append("\": ");
@@@ -284,40 -345,171 +328,42 @@@
                  sb.append(spec.type.toJSONString(buffer, protocolVersion));
          }
          sb.append("}");
-         return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
 -        List<ByteBuffer> jsonRow = new ArrayList<>();
 -        jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
 -        return jsonRow;
++
++        jsonRow[0] = UTF8Type.instance.getSerializer().serialize(sb.toString());
++        return Arrays.asList(jsonRow);
      }
  
 -    public class ResultSetBuilder
 +    public static interface Selectors
      {
 -        private final ResultSet resultSet;
 -        private final ProtocolVersion protocolVersion;
 -
          /**
 -         * As multiple thread can access a <code>Selection</code> instance each <code>ResultSetBuilder</code> will use
 -         * its own <code>Selectors</code> instance.
 +         * Returns the {@code ColumnFilter} corresponding to those selectors
 +         * @return the {@code ColumnFilter} corresponding to those selectors
           */
 -        private final Selectors selectors;
 +        public ColumnFilter getColumnFilter();
  
          /**
 -         * The <code>GroupMaker</code> used to build the aggregates.
 +         * Checks if one of the selectors perform some aggregations.
 +         * @return {@code true} if one of the selectors perform some aggregations, {@code false} otherwise.
           */
 -        private final GroupMaker groupMaker;
 -
 -        /*
 -         * We'll build CQL3 row one by one.
 -         * The currentRow is the values for the (CQL3) columns we've fetched.
 -         * We also collect timestamps and ttls for the case where the writetime and
 -         * ttl functions are used. Note that we might collect timestamp and/or ttls
 -         * we don't care about, but since the array below are allocated just once,
 -         * it doesn't matter performance wise.
 -         */
 -        List<ByteBuffer> current;
 -        final long[] timestamps;
 -        final int[] ttls;
 -
 -        private final boolean isJson;
 -
 -        private ResultSetBuilder(QueryOptions options, boolean isJson)
 -        {
 -            this(options, isJson, null);
 -        }
 -
 -        private ResultSetBuilder(QueryOptions options, boolean isJson, GroupMaker groupMaker)
 -        {
 -            this.resultSet = new ResultSet(getResultMetadata(isJson).copy(), new ArrayList<List<ByteBuffer>>());
 -            this.protocolVersion = options.getProtocolVersion();
 -            this.selectors = newSelectors(options);
 -            this.groupMaker = groupMaker;
 -            this.timestamps = collectTimestamps ? new long[columns.size()] : null;
 -            this.ttls = collectTTLs ? new int[columns.size()] : null;
 -            this.isJson = isJson;
 -
 -            // We use MIN_VALUE to indicate no timestamp and -1 for no ttl
 -            if (timestamps != null)
 -                Arrays.fill(timestamps, Long.MIN_VALUE);
 -            if (ttls != null)
 -                Arrays.fill(ttls, -1);
 -        }
 -
 -        public void add(ByteBuffer v)
 -        {
 -            current.add(v);
 -        }
 -
 -        public void add(Cell c, int nowInSec)
 -        {
 -            if (c == null)
 -            {
 -                current.add(null);
 -                return;
 -            }
 -
 -            current.add(value(c));
 -
 -            if (timestamps != null)
 -                timestamps[current.size() - 1] = c.timestamp();
 -
 -            if (ttls != null)
 -                ttls[current.size() - 1] = remainingTTL(c, nowInSec);
 -        }
 -
 -        private int remainingTTL(Cell c, int nowInSec)
 -        {
 -            if (!c.isExpiring())
 -                return -1;
 -
 -            int remaining = c.localDeletionTime() - nowInSec;
 -            return remaining >= 0 ? remaining : -1;
 -        }
 -
 -        private ByteBuffer value(Cell c)
 -        {
 -            return c.isCounterCell()
 -                 ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value()))
 -                 : c.value();
 -        }
 +        public boolean isAggregate();
  
          /**
 -         * Notifies this <code>Builder</code> that a new row is being processed.
 -         *
 -         * @param partitionKey the partition key of the new row
 -         * @param clustering the clustering of the new row
 +         * Returns the number of fetched columns
 +         * @return the number of fetched columns
           */
 -        public void newRow(DecoratedKey partitionKey, Clustering clustering)
 -        {
 -            // The groupMaker needs to be called for each row
 -            boolean isNewAggregate = groupMaker == null || groupMaker.isNewGroup(partitionKey, clustering);
 -            if (current != null)
 -            {
 -                selectors.addInputRow(protocolVersion, this);
 -                if (isNewAggregate)
 -                {
 -                    resultSet.addRow(getOutputRow());
 -                    selectors.reset();
 -                }
 -            }
 -            current = new ArrayList<>(columns.size());
 -
 -            // Timestamps and TTLs are arrays per row, we must null them out between rows
 -            if (timestamps != null)
 -                Arrays.fill(timestamps, Long.MIN_VALUE);
 -            if (ttls != null)
 -                Arrays.fill(ttls, -1);
 -        }
 +        public int numberOfFetchedColumns();
  
          /**
 -         * Builds the <code>ResultSet</code>
 +         * Checks if one of the selectors collect TTLs.
 +         * @return {@code true} if one of the selectors collect TTLs, {@code false} otherwise.
           */
 -        public ResultSet build()
 -        {
 -            if (current != null)
 -            {
 -                selectors.addInputRow(protocolVersion, this);
 -                resultSet.addRow(getOutputRow());
 -                selectors.reset();
 -                current = null;
 -            }
 -
 -            // For aggregates we need to return a row even it no records have been found
 -            if (resultSet.isEmpty() && groupMaker != null && groupMaker.returnAtLeastOneRow())
 -                resultSet.addRow(getOutputRow());
 -            return resultSet;
 -        }
 +        public boolean collectTTLs();
  
 -        private List<ByteBuffer> getOutputRow()
 -        {
 -            List<ByteBuffer> outputRow = selectors.getOutputRow(protocolVersion);
 -            if (isJson)
 -            {
 -                // Keep all columns around for possible post-query ordering. (CASSANDRA-14286)
 -                List<ByteBuffer> jsonRow = rowToJson(outputRow, protocolVersion, metadata);
 -
 -                // Keep ordering columns around for possible post-query ordering. (CASSANDRA-14286)
 -                if (orderingIndex != null)
 -                {
 -                    for (Integer orderingColumnIndex : orderingIndex.values())
 -                        jsonRow.add(outputRow.get(orderingColumnIndex));
 -                }
 -                outputRow = jsonRow;
 -            }
 -            return outputRow;
 -        }
 -    }
 -
 -    private static interface Selectors
 -    {
 -        public boolean isAggregate();
 +        /**
 +         * Checks if one of the selectors collect timestamps.
 +         * @return {@code true} if one of the selectors collect timestamps, {@code false} otherwise.
 +         */
 +        public boolean collectTimestamps();
  
          /**
           * Adds the current row of the specified <code>ResultSetBuilder</code>.
@@@ -408,12 -571,12 +454,14 @@@
                      current = null;
                  }
  
 -                public List<ByteBuffer> getOutputRow(ProtocolVersion protocolVersion)
 +                public List<ByteBuffer> getOutputRow()
                  {
-                     return isJson ? rowToJson(current, options.getProtocolVersion(), metadata) : current;
++                    if (isJson)
++                        return rowToJson(current, options.getProtocolVersion(), metadata, orderingColumns);
+                     return current;
                  }
  
 -                public void addInputRow(ProtocolVersion protocolVersion, ResultSetBuilder rs) throws InvalidRequestException
 +                public void addInputRow(ResultSetBuilder rs) throws InvalidRequestException
                  {
                      current = rs.current;
                  }
@@@ -522,39 -663,15 +570,39 @@@
                      List<ByteBuffer> outputRow = new ArrayList<>(selectors.size());
  
                      for (Selector selector: selectors)
 -                        outputRow.add(selector.getOutput(protocolVersion));
 +                        outputRow.add(selector.getOutput(options.getProtocolVersion()));
  
-                     return isJson ? rowToJson(outputRow, options.getProtocolVersion(), metadata) : outputRow;
 -                    return outputRow;
++                    return isJson ? rowToJson(outputRow, options.getProtocolVersion(), metadata, orderingColumns) : outputRow;
                  }
  
 -                public void addInputRow(ProtocolVersion protocolVersion, ResultSetBuilder rs) throws InvalidRequestException
 +                public void addInputRow(ResultSetBuilder rs) throws InvalidRequestException
                  {
                      for (Selector selector : selectors)
 -                        selector.addInput(protocolVersion, rs);
 +                        selector.addInput(options.getProtocolVersion(), rs);
 +                }
 +
 +                @Override
 +                public int numberOfFetchedColumns()
 +                {
 +                    return getColumns().size();
 +                }
 +
 +                @Override
 +                public boolean collectTTLs()
 +                {
 +                    return collectTTLs;
 +                }
 +
 +                @Override
 +                public boolean collectTimestamps()
 +                {
 +                    return collectTimestamps;
 +                }
 +
 +                @Override
 +                public ColumnFilter getColumnFilter()
 +                {
 +                    return columnFilterFactory.newInstance(selectors);
                  }
              };
          }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/05d7661d/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index 8272eb5,348fa52..84fa1d7
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@@ -18,10 -18,17 +18,9 @@@
  package org.apache.cassandra.cql3.statements;
  
  import java.nio.ByteBuffer;
 -import java.util.ArrayList;
 -import java.util.Collection;
 -import java.util.Collections;
 -import java.util.Comparator;
 -import java.util.Iterator;
 -import java.util.List;
 -import java.util.Map;
 -import java.util.NavigableSet;
 -import java.util.SortedSet;
 +import java.util.*;
  
  import com.google.common.base.MoreObjects;
- import com.google.common.collect.Maps;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
@@@ -979,8 -982,8 +978,8 @@@ public class SelectStatement implement
              {
                  assert !forView;
                  verifyOrderingIsAllowed(restrictions);
-                 orderingComparator = getOrderingComparator(table, selection, restrictions, orderingColumns);
 -                orderingComparator = getOrderingComparator(cfm, selection, restrictions, parameters.isJson);
 -                isReversed = isReversed(cfm);
++                orderingComparator = getOrderingComparator(selection, restrictions, orderingColumns);
 +                isReversed = isReversed(table, orderingColumns, restrictions);
                  if (isReversed)
                      orderingComparator = Collections.reverseOrder(orderingComparator);
              }
@@@ -1178,52 -1142,54 +1177,36 @@@
              checkFalse(clusteringPrefixSize > 0 && isDistinct,
                         "Grouping on clustering columns is not allowed for SELECT DISTINCT queries");
  
 -            return AggregationSpecification.aggregatePkPrefix(cfm.comparator, clusteringPrefixSize);
 +            return AggregationSpecification.aggregatePkPrefix(metadata.comparator, clusteringPrefixSize);
          }
  
-         private Comparator<List<ByteBuffer>> getOrderingComparator(TableMetadata metadata,
 -        private Comparator<List<ByteBuffer>> getOrderingComparator(CFMetaData cfm,
--                                                                   Selection selection,
++        private Comparator<List<ByteBuffer>> getOrderingComparator(Selection selection,
                                                                     StatementRestrictions restrictions,
 -                                                                   boolean isJson)
 +                                                                   Map<ColumnMetadata, Boolean> orderingColumns)
                                                                     throws InvalidRequestException
          {
              if (!restrictions.keyIsInRelation())
                  return null;
  
-             Map<ColumnIdentifier, Integer> orderingIndexes = getOrderingIndex(metadata, selection, orderingColumns);
- 
 -            Map<ColumnDefinition, Integer> orderingIndexes = getOrderingIndex(cfm, selection, isJson);
 +            List<Integer> idToSort = new ArrayList<Integer>(orderingColumns.size());
 +            List<Comparator<ByteBuffer>> sorters = new ArrayList<Comparator<ByteBuffer>>(orderingColumns.size());
  
 -            List<Integer> idToSort = new ArrayList<Integer>();
 -            List<Comparator<ByteBuffer>> sorters = new ArrayList<Comparator<ByteBuffer>>();
 -
 -            for (ColumnDefinition.Raw raw : parameters.orderings.keySet())
 +            for (ColumnMetadata orderingColumn : orderingColumns.keySet())
              {
-                 idToSort.add(orderingIndexes.get(orderingColumn.name));
 -                ColumnDefinition orderingColumn = raw.prepare(cfm);
 -                idToSort.add(orderingIndexes.get(orderingColumn));
++                idToSort.add(selection.getOrderingIndex(orderingColumn));
                  sorters.add(orderingColumn.type);
              }
              return idToSort.size() == 1 ? new SingleColumnComparator(idToSort.get(0), sorters.get(0))
                      : new CompositeComparator(sorters, idToSort);
          }
  
-         private Map<ColumnIdentifier, Integer> getOrderingIndex(TableMetadata table,
-                                                                 Selection selection,
-                                                                 Map<ColumnMetadata, Boolean> orderingColumns)
-         {
-             Map<ColumnIdentifier, Integer> orderingIndexes = Maps.newHashMapWithExpectedSize(orderingColumns.size());
-             for (ColumnMetadata def : orderingColumns.keySet())
-             {
-                 int index = selection.getResultSetIndex(def);
-                 orderingIndexes.put(def.name, index);
-             }
-             return orderingIndexes;
-         }
- 
 -        private Map<ColumnDefinition, Integer> getOrderingIndex(CFMetaData cfm, Selection selection, boolean isJson)
 -                throws InvalidRequestException
 +        private boolean isReversed(TableMetadata table, Map<ColumnMetadata, Boolean> orderingColumns, StatementRestrictions restrictions) throws InvalidRequestException
          {
 -            // If we order post-query (see orderResults), the sorted column needs to be in the ResultSet for sorting,
 -            // even if we don't
 -            // ultimately ship them to the client (CASSANDRA-4911).
 -            for (ColumnDefinition.Raw raw : parameters.orderings.keySet())
 -            {
 -                final ColumnDefinition def = raw.prepare(cfm);
 -                selection.addColumnForOrdering(def);
 -            }
 -            return selection.getOrderingIndex(isJson);
 -        }
 -
 -        private boolean isReversed(CFMetaData cfm) throws InvalidRequestException
 -        {
 -            Boolean[] reversedMap = new Boolean[cfm.clusteringColumns().size()];
 +            Boolean[] reversedMap = new Boolean[table.clusteringColumns().size()];
              int i = 0;
 -            for (Map.Entry<ColumnDefinition.Raw, Boolean> entry : parameters.orderings.entrySet())
 +            for (Map.Entry<ColumnMetadata, Boolean> entry : orderingColumns.entrySet())
              {
 -                ColumnDefinition def = entry.getKey().prepare(cfm);
 +                ColumnMetadata def = entry.getKey();
                  boolean reversed = entry.getValue();
  
                  checkTrue(def.isClusteringColumn(),

http://git-wip-us.apache.org/repos/asf/cassandra/blob/05d7661d/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------
diff --cc test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
index e696899,16df6e1..ec42f5d
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
@@@ -1377,4 -1369,43 +1377,43 @@@ public class JsonTest extends CQLTeste
                     row("{\"id\": 2, \"name\": \"\"}"),
                     row("{\"id\": 3, \"name\": null}"));
      }
+ 
+     // CASSANDRA-14286
+     @Test
+     public void testJsonOrdering() throws Throwable
+     {
 -        createTable("CREATE TABLE %s( PRIMARY KEY (a, b), a INT, b INT);");
++        createTable("CREATE TABLE %s(a INT, b INT, PRIMARY KEY (a, b))");
+         execute("INSERT INTO %s(a, b) VALUES (20, 30);");
+         execute("INSERT INTO %s(a, b) VALUES (100, 200);");
+ 
+         assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b"),
+                    row("{\"a\": 20, \"b\": 30}"),
+                    row("{\"a\": 100, \"b\": 200}"));
+ 
+         assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                    row("{\"a\": 100, \"b\": 200}"),
+                    row("{\"a\": 20, \"b\": 30}"));
+ 
+         assertRows(execute("SELECT JSON a FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                    row("{\"a\": 100}"),
+                    row("{\"a\": 20}"));
+ 
 -        // Check ordering with alias 
++        // Check ordering with alias
+         assertRows(execute("SELECT JSON a, b as c FROM %s WHERE a IN (20, 100) ORDER BY b"),
+                    row("{\"a\": 20, \"c\": 30}"),
+                    row("{\"a\": 100, \"c\": 200}"));
+ 
+         assertRows(execute("SELECT JSON a, b as c FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                    row("{\"a\": 100, \"c\": 200}"),
+                    row("{\"a\": 20, \"c\": 30}"));
+ 
 -        // Check ordering with CAST 
++        // Check ordering with CAST
+         assertRows(execute("SELECT JSON a, CAST(b AS FLOAT) FROM %s WHERE a IN (20, 100) ORDER BY b"),
+                    row("{\"a\": 20, \"cast(b as float)\": 30.0}"),
+                    row("{\"a\": 100, \"cast(b as float)\": 200.0}"));
+ 
+         assertRows(execute("SELECT JSON a, CAST(b AS FLOAT) FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                    row("{\"a\": 100, \"cast(b as float)\": 200.0}"),
+                    row("{\"a\": 20, \"cast(b as float)\": 30.0}"));
+     }
  }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org


[08/10] cassandra git commit: Merge branch cassandra-3.0 into cassandra-3.11

Posted by bl...@apache.org.
Merge branch cassandra-3.0 into cassandra-3.11


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/95cfee62
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/95cfee62
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/95cfee62

Branch: refs/heads/cassandra-3.11
Commit: 95cfee623456f1df51ce548cc6cf42fe5a78050c
Parents: 845243d 598008d
Author: Benjamin Lerer <b....@gmail.com>
Authored: Tue Apr 17 12:14:58 2018 +0200
Committer: Benjamin Lerer <b....@gmail.com>
Committed: Tue Apr 17 12:27:25 2018 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../org/apache/cassandra/cql3/ResultSet.java    |  5 ++
 .../cassandra/cql3/selection/Selection.java     | 70 +++++++++++++++++---
 .../cql3/statements/SelectStatement.java        | 20 +++---
 .../cql3/validation/entities/JsonTest.java      | 41 +++++++++++-
 5 files changed, 116 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/CHANGES.txt
----------------------------------------------------------------------
diff --cc CHANGES.txt
index 4c513d7,d3d8036..42ea3b4
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@@ -25,6 -15,8 +25,7 @@@ Merged from 3.0
   * Fully utilise specified compaction threads (CASSANDRA-14210)
   * Pre-create deletion log records to finish compactions quicker (CASSANDRA-12763)
  Merged from 2.2:
+  * Fix JSON queries with IN restrictions and ORDER BY clause (CASSANDRA-14286)
 - * CQL fromJson(null) throws NullPointerException (CASSANDRA-13891)
   * Backport circleci yaml (CASSANDRA-14240)
  Merged from 2.1:
   * Check checksum before decompressing data (CASSANDRA-14284)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/src/java/org/apache/cassandra/cql3/ResultSet.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/src/java/org/apache/cassandra/cql3/selection/Selection.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/selection/Selection.java
index 7b4e80c,6227158..fb0b60c
--- a/src/java/org/apache/cassandra/cql3/selection/Selection.java
+++ b/src/java/org/apache/cassandra/cql3/selection/Selection.java
@@@ -271,41 -288,13 +311,43 @@@ public abstract class Selectio
      @Override
      public String toString()
      {
 -        return Objects.toStringHelper(this)
 -                .add("columns", columns)
 -                .add("columnMapping", columnMapping)
 -                .add("metadata", metadata)
 -                .add("collectTimestamps", collectTimestamps)
 -                .add("collectTTLs", collectTTLs)
 -                .toString();
 +        return MoreObjects.toStringHelper(this)
 +                          .add("columns", columns)
 +                          .add("columnMapping", columnMapping)
 +                          .add("metadata", metadata)
 +                          .add("collectTimestamps", collectTimestamps)
 +                          .add("collectTTLs", collectTTLs)
 +                          .toString();
 +    }
 +
 +    public static List<ByteBuffer> rowToJson(List<ByteBuffer> row, ProtocolVersion protocolVersion, ResultSet.ResultMetadata metadata)
 +    {
 +        StringBuilder sb = new StringBuilder("{");
-         for (int i = 0; i < metadata.names.size(); i++)
++        for (int i = 0; i < metadata.getColumnCount(); i++)
 +        {
 +            if (i > 0)
 +                sb.append(", ");
 +
 +            ColumnSpecification spec = metadata.names.get(i);
 +            String columnName = spec.name.toString();
 +            if (!columnName.equals(columnName.toLowerCase(Locale.US)))
 +                columnName = "\"" + columnName + "\"";
 +
 +            ByteBuffer buffer = row.get(i);
 +            sb.append('"');
 +            sb.append(Json.quoteAsJsonString(columnName));
 +            sb.append("\": ");
 +            if (buffer == null)
 +                sb.append("null");
 +            else if (!buffer.hasRemaining())
 +                sb.append("\"\"");
 +            else
 +                sb.append(spec.type.toJSONString(buffer, protocolVersion));
 +        }
 +        sb.append("}");
-         return Collections.singletonList(UTF8Type.instance.getSerializer().serialize(sb.toString()));
++        List<ByteBuffer> jsonRow = new ArrayList<>();
++        jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
++        return jsonRow;
      }
  
      public class ResultSetBuilder
@@@ -445,12 -409,51 +487,24 @@@
              return resultSet;
          }
  
 -        private List<ByteBuffer> getOutputRow(int protocolVersion)
 +        private List<ByteBuffer> getOutputRow()
          {
              List<ByteBuffer> outputRow = selectors.getOutputRow(protocolVersion);
-             return isJson ? rowToJson(outputRow, protocolVersion, metadata)
-                           : outputRow;
+             if (isJson)
+             {
 -                List<ByteBuffer> jsonRow = rowToJson(outputRow, protocolVersion);
++                // Keep all columns around for possible post-query ordering. (CASSANDRA-14286)
++                List<ByteBuffer> jsonRow = rowToJson(outputRow, protocolVersion, metadata);
+ 
+                 // Keep ordering columns around for possible post-query ordering. (CASSANDRA-14286)
+                 if (orderingIndex != null)
+                 {
+                     for (Integer orderingColumnIndex : orderingIndex.values())
+                         jsonRow.add(outputRow.get(orderingColumnIndex));
+                 }
+                 outputRow = jsonRow;
+             }
+             return outputRow;
          }
 -
 -        private List<ByteBuffer> rowToJson(List<ByteBuffer> row, int protocolVersion)
 -        {
 -            StringBuilder sb = new StringBuilder("{");
 -            for (int i = 0; i < metadata.getColumnCount(); i++)
 -            {
 -                if (i > 0)
 -                    sb.append(", ");
 -
 -                ColumnSpecification spec = metadata.names.get(i);
 -                String columnName = spec.name.toString();
 -                if (!columnName.equals(columnName.toLowerCase(Locale.US)))
 -                    columnName = "\"" + columnName + "\"";
 -
 -                ByteBuffer buffer = row.get(i);
 -                sb.append('"');
 -                sb.append(Json.quoteAsJsonString(columnName));
 -                sb.append("\": ");
 -                if (buffer == null || !buffer.hasRemaining())
 -                    sb.append("null");
 -                else
 -                    sb.append(spec.type.toJSONString(buffer, protocolVersion));
 -            }
 -            sb.append("}");
 -            List<ByteBuffer> jsonRow = new ArrayList<>();
 -            jsonRow.add(UTF8Type.instance.getSerializer().serialize(sb.toString()));
 -            return jsonRow;
 -        }
      }
  
      private static interface Selectors

http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --cc src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
index a7ba6aa,a5e6254..348fa52
--- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@@ -22,7 -22,7 +22,6 @@@ import java.util.ArrayList
  import java.util.Collection;
  import java.util.Collections;
  import java.util.Comparator;
--import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
@@@ -1159,10 -1028,11 +1159,10 @@@ public class SelectStatement implement
              List<Integer> idToSort = new ArrayList<Integer>();
              List<Comparator<ByteBuffer>> sorters = new ArrayList<Comparator<ByteBuffer>>();
  
 -            for (ColumnIdentifier.Raw raw : parameters.orderings.keySet())
 +            for (ColumnDefinition.Raw raw : parameters.orderings.keySet())
              {
 -                ColumnIdentifier identifier = raw.prepare(cfm);
 -                ColumnDefinition orderingColumn = cfm.getColumnDefinition(identifier);
 +                ColumnDefinition orderingColumn = raw.prepare(cfm);
-                 idToSort.add(orderingIndexes.get(orderingColumn.name));
+                 idToSort.add(orderingIndexes.get(orderingColumn));
                  sorters.add(orderingColumn.type);
              }
              return idToSort.size() == 1 ? new SingleColumnComparator(idToSort.get(0), sorters.get(0))
@@@ -1175,16 -1045,15 +1175,12 @@@
              // If we order post-query (see orderResults), the sorted column needs to be in the ResultSet for sorting,
              // even if we don't
              // ultimately ship them to the client (CASSANDRA-4911).
-             Map<ColumnIdentifier, Integer> orderingIndexes = new HashMap<>();
 -            for (ColumnIdentifier.Raw raw : parameters.orderings.keySet())
 +            for (ColumnDefinition.Raw raw : parameters.orderings.keySet())
              {
 -                ColumnIdentifier column = raw.prepare(cfm);
 -                final ColumnDefinition def = cfm.getColumnDefinition(column);
 -                if (def == null)
 -                    handleUnrecognizedOrderingColumn(column);
 +                final ColumnDefinition def = raw.prepare(cfm);
-                 int index = selection.getResultSetIndex(def);
-                 if (index < 0)
-                     index = selection.addColumnForOrdering(def);
-                 orderingIndexes.put(def.name, index);
+                 selection.addColumnForOrdering(def);
              }
-             return orderingIndexes;
+             return selection.getOrderingIndex(isJson);
          }
  
          private boolean isReversed(CFMetaData cfm) throws InvalidRequestException

http://git-wip-us.apache.org/repos/asf/cassandra/blob/95cfee62/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
----------------------------------------------------------------------
diff --cc test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
index a475671,e6537d0..16df6e1
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java
@@@ -1356,17 -1267,24 +1356,56 @@@ public class JsonTest extends CQLTeste
          Assert.assertTrue(executor.awaitTermination(30, TimeUnit.SECONDS));
      }
  
-     @Test
++   @Test
 +    public void emptyStringJsonSerializationTest() throws Throwable
 +    {
 +        createTable("create table %s(id INT, name TEXT, PRIMARY KEY(id));");
 +        execute("insert into %s(id, name) VALUES (0, 'Foo');");
 +        execute("insert into %s(id, name) VALUES (2, '');");
 +        execute("insert into %s(id, name) VALUES (3, null);");
 +
 +        assertRows(execute("SELECT JSON * FROM %s"),
 +                   row("{\"id\": 0, \"name\": \"Foo\"}"),
 +                   row("{\"id\": 2, \"name\": \"\"}"),
 +                   row("{\"id\": 3, \"name\": null}"));
 +    }
++
+     // CASSANDRA-14286
+     @Test
+     public void testJsonOrdering() throws Throwable
+     {
+         createTable("CREATE TABLE %s( PRIMARY KEY (a, b), a INT, b INT);");
+         execute("INSERT INTO %s(a, b) VALUES (20, 30);");
+         execute("INSERT INTO %s(a, b) VALUES (100, 200);");
+ 
+         assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b"),
+                    row("{\"a\": 20, \"b\": 30}"),
+                    row("{\"a\": 100, \"b\": 200}"));
+ 
+         assertRows(execute("SELECT JSON a, b FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                    row("{\"a\": 100, \"b\": 200}"),
+                    row("{\"a\": 20, \"b\": 30}"));
+ 
+         assertRows(execute("SELECT JSON a FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
+                    row("{\"a\": 100}"),
+                    row("{\"a\": 20}"));
++
++        // Check ordering with alias 
++        assertRows(execute("SELECT JSON a, b as c FROM %s WHERE a IN (20, 100) ORDER BY b"),
++                   row("{\"a\": 20, \"c\": 30}"),
++                   row("{\"a\": 100, \"c\": 200}"));
++
++        assertRows(execute("SELECT JSON a, b as c FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
++                   row("{\"a\": 100, \"c\": 200}"),
++                   row("{\"a\": 20, \"c\": 30}"));
++
++        // Check ordering with CAST 
++        assertRows(execute("SELECT JSON a, CAST(b AS FLOAT) FROM %s WHERE a IN (20, 100) ORDER BY b"),
++                   row("{\"a\": 20, \"cast(b as float)\": 30.0}"),
++                   row("{\"a\": 100, \"cast(b as float)\": 200.0}"));
++
++        assertRows(execute("SELECT JSON a, CAST(b AS FLOAT) FROM %s WHERE a IN (20, 100) ORDER BY b DESC"),
++                   row("{\"a\": 100, \"cast(b as float)\": 200.0}"),
++                   row("{\"a\": 20, \"cast(b as float)\": 30.0}"));
+     }
  }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org