You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2020/08/04 22:33:25 UTC

[GitHub] [druid] clintropolis commented on a change in pull request #10235: Add "offset" parameter to GroupBy query.

clintropolis commented on a change in pull request #10235:
URL: https://github.com/apache/druid/pull/10235#discussion_r465346819



##########
File path: core/src/main/java/org/apache/druid/collections/StableLimitingSorter.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.collections;
+
+import com.google.common.collect.MinMaxPriorityQueue;
+import com.google.common.collect.Ordering;
+
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.Objects;
+
+/**
+ * Simultaneously sorts and limits its input.
+ *
+ * The sort is stable, meaning that equal elements (as determined by the comparator) will not be reordered.
+ *
+ * Not thread-safe.
+ *
+ * Note: this class doesn't have its own unit tests. It is tested along with
+ * {@link org.apache.druid.java.util.common.guava.TopNSequence} in "TopNSequenceTest".
+ */
+public class StableLimitingSorter<T>
+{
+  private final MinMaxPriorityQueue<NumberedElement<T>> queue;
+
+  private long count = 0;
+
+  public StableLimitingSorter(final Comparator<T> comparator, final int limit)
+  {
+    this.queue = MinMaxPriorityQueue
+        .orderedBy(
+            Ordering.from(
+                Comparator.<NumberedElement<T>, T>comparing(NumberedElement::getElement, comparator)
+                    .thenComparing(NumberedElement::getNumber)
+            )
+        )
+        .maximumSize(limit)
+        .create();
+  }
+
+  /**
+   * Offer an element to the sorter.
+   */
+  public void add(T element)
+  {
+    queue.offer(new NumberedElement<>(element, count++));
+  }
+
+  /**
+   * Drain elements in sorted order (least first).

Review comment:
       What does 'least first' mean? Is this referring to `. thenComparing` by the number?

##########
File path: docs/querying/limitspec.md
##########
@@ -35,11 +35,23 @@ The default limit spec takes a limit and the list of columns to do an orderBy op
 ```json
 {
     "type"    : "default",
-    "limit"   : <integer_value>,
-    "columns" : [list of OrderByColumnSpec],
+    "limit"   : <optional integer>,
+    "offset"  : <optional integer>,
+    "columns" : [<optional list of OrderByColumnSpec>],
 }
 ```
 
+The "limit" parameter is the maximum number of rows to return.
+
+The "offset" parameter tells Druid to skip this many rows when returning results. If both "limit" and "offset" are
+provided, then "offset" will be applied first, followed by "limit". For example, a spec with limit 100 and offset 10
+will return 100 rows starting from row number 10. Skipped rows will still need to be generated internally and then
+discarded, meaning that raising offsets to high values can cause queries to use additional resources.

Review comment:
       Any idea on numbers that are too large, or is this a try it and find out what your cluster can handle kind of thing?

##########
File path: processing/src/test/java/org/apache/druid/query/groupby/GroupByQueryRunnerTest.java
##########
@@ -3700,7 +3702,7 @@ public void testGroupByWithOrderOnHyperUnique()
             query,
             "1970-01-01T00:00:00.000Z",
             "market",
-            "upfront",
+            "total_market",

Review comment:
       Are these changes from the stabilization?

##########
File path: sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidQuery.java
##########
@@ -928,6 +928,7 @@ public GroupByQuery toGroupByQuery()
         sorting != null
         ? new DefaultLimitSpec(
             sorting.getOrderBys(),
+            0,

Review comment:
       Is the plan to update `Sorting` to store the offset, and `computeSorting` to set it when creating it `Sorting` from the calcite `Sort` in a follow-up so that this works in SQL?

##########
File path: sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidQuery.java
##########
@@ -928,6 +928,7 @@ public GroupByQuery toGroupByQuery()
         sorting != null
         ? new DefaultLimitSpec(
             sorting.getOrderBys(),
+            0,

Review comment:
       Is the plan to update `Sorting` to store the offset, and `computeSorting` to set it when creating it from the calcite `Sort` in a follow-up so that this works in SQL?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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