egor-ryashin commented on a change in pull request #6794: Query vectorization.
URL: https://github.com/apache/incubator-druid/pull/6794#discussion_r302070676
 
 

 ##########
 File path: 
processing/src/main/java/org/apache/druid/segment/QueryableIndexCursorSequenceBuilder.java
 ##########
 @@ -0,0 +1,618 @@
+/*
+ * 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.segment;
+
+import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.apache.druid.collections.bitmap.ImmutableBitmap;
+import org.apache.druid.java.util.common.granularity.Granularity;
+import org.apache.druid.java.util.common.guava.Sequence;
+import org.apache.druid.java.util.common.guava.Sequences;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.query.BaseQuery;
+import org.apache.druid.query.filter.Filter;
+import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector;
+import org.apache.druid.segment.column.BaseColumn;
+import org.apache.druid.segment.column.ColumnHolder;
+import org.apache.druid.segment.column.NumericColumn;
+import org.apache.druid.segment.data.Offset;
+import org.apache.druid.segment.data.ReadableOffset;
+import org.apache.druid.segment.historical.HistoricalCursor;
+import org.apache.druid.segment.vector.BitmapVectorOffset;
+import org.apache.druid.segment.vector.FilteredVectorOffset;
+import org.apache.druid.segment.vector.NoFilterVectorOffset;
+import 
org.apache.druid.segment.vector.QueryableIndexVectorColumnSelectorFactory;
+import org.apache.druid.segment.vector.VectorColumnSelectorFactory;
+import org.apache.druid.segment.vector.VectorCursor;
+import org.apache.druid.segment.vector.VectorOffset;
+import org.joda.time.DateTime;
+import org.joda.time.Interval;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class QueryableIndexCursorSequenceBuilder
+{
+  // At this threshold, timestamp searches switch from binary to linear. The 
idea is to avoid too much decompression
+  // buffer thrashing. The default value is chosen to be similar to the 
typical number of timestamps per block.
+  private static final int TOO_CLOSE_FOR_MISSILES = 15000;
+
+  private final QueryableIndex index;
+  private final Interval interval;
+  private final VirtualColumns virtualColumns;
+  @Nullable
+  private final ImmutableBitmap filterBitmap;
+  private final long minDataTimestamp;
+  private final long maxDataTimestamp;
+  private final boolean descending;
+  @Nullable
+  private final Filter postFilter;
+  private final ColumnSelectorBitmapIndexSelector bitmapIndexSelector;
+
+  public QueryableIndexCursorSequenceBuilder(
+      QueryableIndex index,
+      Interval interval,
+      VirtualColumns virtualColumns,
+      @Nullable ImmutableBitmap filterBitmap,
+      long minDataTimestamp,
+      long maxDataTimestamp,
+      boolean descending,
+      @Nullable Filter postFilter,
+      ColumnSelectorBitmapIndexSelector bitmapIndexSelector
+  )
+  {
+    this.index = index;
+    this.interval = interval;
+    this.virtualColumns = virtualColumns;
+    this.filterBitmap = filterBitmap;
+    this.minDataTimestamp = minDataTimestamp;
+    this.maxDataTimestamp = maxDataTimestamp;
+    this.descending = descending;
+    this.postFilter = postFilter;
+    this.bitmapIndexSelector = bitmapIndexSelector;
+  }
+
+  public Sequence<Cursor> build(final Granularity gran)
+  {
+    final Offset baseOffset;
+
+    if (filterBitmap == null) {
+      baseOffset = descending
+                   ? new SimpleDescendingOffset(index.getNumRows())
+                   : new SimpleAscendingOffset(index.getNumRows());
+    } else {
+      baseOffset = BitmapOffset.of(filterBitmap, descending, 
index.getNumRows());
+    }
+
+    // Column caches shared amongst all cursors in this sequence.
+    final Map<String, BaseColumn> columnCache = new HashMap<>();
+
+    final NumericColumn timestamps = (NumericColumn) 
index.getColumnHolder(ColumnHolder.TIME_COLUMN_NAME).getColumn();
+
+    final Closer closer = Closer.create();
+    closer.register(timestamps);
+
+    Iterable<Interval> iterable = gran.getIterable(interval);
+    if (descending) {
+      iterable = Lists.reverse(ImmutableList.copyOf(iterable));
+    }
+
+    return Sequences.withBaggage(
+        Sequences.map(
+            Sequences.simple(iterable),
+            new Function<Interval, Cursor>()
+            {
+              @Override
+              public Cursor apply(final Interval inputInterval)
+              {
+                final long timeStart = Math.max(interval.getStartMillis(), 
inputInterval.getStartMillis());
+                final long timeEnd = Math.min(
+                    interval.getEndMillis(),
+                    gran.increment(inputInterval.getStart()).getMillis()
+                );
+
+                if (descending) {
+                  for (; baseOffset.withinBounds(); baseOffset.increment()) {
+                    if 
(timestamps.getLongSingleValueRow(baseOffset.getOffset()) < timeEnd) {
+                      break;
+                    }
+                  }
+                } else {
+                  for (; baseOffset.withinBounds(); baseOffset.increment()) {
+                    if 
(timestamps.getLongSingleValueRow(baseOffset.getOffset()) >= timeStart) {
+                      break;
+                    }
+                  }
+                }
+
+                final Offset offset = descending ?
+                                      new DescendingTimestampCheckingOffset(
+                                          baseOffset,
+                                          timestamps,
+                                          timeStart,
+                                          minDataTimestamp >= timeStart
+                                      ) :
+                                      new AscendingTimestampCheckingOffset(
+                                          baseOffset,
+                                          timestamps,
+                                          timeEnd,
+                                          maxDataTimestamp < timeEnd
+                                      );
+
+
+                final Offset baseCursorOffset = offset.clone();
+                final ColumnSelectorFactory columnSelectorFactory = new 
QueryableIndexColumnSelectorFactory(
+                    index,
+                    virtualColumns,
+                    descending,
+                    closer,
+                    baseCursorOffset.getBaseReadableOffset(),
+                    columnCache
+                );
+                final DateTime myBucket = 
gran.toDateTime(inputInterval.getStartMillis());
+
+                if (postFilter == null) {
+                  return new QueryableIndexCursor(baseCursorOffset, 
columnSelectorFactory, myBucket);
+                } else {
+                  FilteredOffset filteredOffset = new FilteredOffset(
+                      baseCursorOffset,
+                      columnSelectorFactory,
+                      descending,
+                      postFilter,
+                      bitmapIndexSelector
+                  );
+                  return new QueryableIndexCursor(filteredOffset, 
columnSelectorFactory, myBucket);
+                }
+
+              }
+            }
+        ),
+        closer
+    );
+  }
+
+  public VectorCursor buildVectorized(final int vectorSize)
+  {
+    // Sanity check - matches QueryableIndexStorageAdapter.canVectorize
+    Preconditions.checkState(virtualColumns.size() == 0, "virtualColumns.size 
== 0");
+    Preconditions.checkState(!descending, "!descending");
+
+    final Map<String, BaseColumn> columnCache = new HashMap<>();
+    final Closer closer = Closer.create();
+
+    NumericColumn timestamps = null;
+
+    final int startOffset;
+    final int endOffset;
+
+    if (interval.getStartMillis() > minDataTimestamp) {
+      timestamps = (NumericColumn) 
index.getColumnHolder(ColumnHolder.TIME_COLUMN_NAME).getColumn();
+      closer.register(timestamps);
+
+      final int result = timeSearch(timestamps, interval.getStartMillis(), 0, 
index.getNumRows());
+      if (result >= 0) {
+        startOffset = result;
+      } else {
+        startOffset = -(result + 1);
+      }
+    } else {
+      startOffset = 0;
+    }
+
+    if (interval.getEndMillis() <= maxDataTimestamp) {
+      if (timestamps == null) {
+        timestamps = (NumericColumn) 
index.getColumnHolder(ColumnHolder.TIME_COLUMN_NAME).getColumn();
+        closer.register(timestamps);
+      }
+
+      final int result = timeSearch(timestamps, interval.getEndMillis(), 
startOffset, index.getNumRows());
+      if (result >= 0) {
 
 Review comment:
   Hmm, looks like this block is still there:
   
![image](https://user-images.githubusercontent.com/14215045/60973727-7c8f4580-a331-11e9-96d5-abf521511580.png)
   

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to