Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize numeric column null value checking for low filter selectivity (more rows) #8822

Merged
merged 11 commits into from Nov 13, 2019
@@ -0,0 +1,182 @@
/*
* 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.benchmark;

import org.apache.druid.collections.bitmap.ImmutableBitmap;
import org.apache.druid.collections.bitmap.WrappedImmutableConciseBitmap;
import org.apache.druid.collections.bitmap.WrappedImmutableRoaringBitmap;
import org.apache.druid.extendedset.intset.ConciseSet;
import org.apache.druid.extendedset.intset.ImmutableConciseSet;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Timeout;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.roaringbitmap.IntIterator;
import org.roaringbitmap.PeekableIntIterator;
import org.roaringbitmap.buffer.MutableRoaringBitmap;

import java.util.BitSet;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@Fork(value = 1)
@Warmup(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 20, time = 10, timeUnit = TimeUnit.SECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Timeout(time = 30, timeUnit = TimeUnit.SECONDS)
public class NullHandlingBitmapGetVsIteratorBenchmark
{
@Param({
//"concise",
"roaring"
})
String bitmapType;

@Param({"500000"})
private int numRows;

@Param({
"0.001",
"0.01",
"0.1",
"0.25",
"0.5",
"0.75",
"0.99999"
})
private double filterMatch;

@Param({
"0",
"0.1",
"0.25",
"0.5",
"0.75",
"0.99"
})
private double nullDensity;

ImmutableBitmap bitmap;
BitSet pretendFilterOffsets;

@Setup
public void setup()
{
pretendFilterOffsets = new BitSet(numRows);
switch (bitmapType) {
case "concise":
ConciseSet conciseSet = new ConciseSet();
for (int i = 0; i < numRows; i++) {
double rando = ThreadLocalRandom.current().nextDouble(0.0, 1.0);
if (filterMatch == 1.0 || rando < filterMatch) {
pretendFilterOffsets.set(i);
}
if (rando < nullDensity) {
conciseSet.add(i);
}
}
bitmap = new WrappedImmutableConciseBitmap(ImmutableConciseSet.newImmutableFromMutable(conciseSet));
break;
case "roaring":
MutableRoaringBitmap roaringBitmap = new MutableRoaringBitmap();

for (int i = 0; i < numRows; i++) {
double rando = ThreadLocalRandom.current().nextDouble(0.0, 1.0);
if (filterMatch == 1.0 || rando < filterMatch) {
pretendFilterOffsets.set(i);
}
if (rando < nullDensity) {
roaringBitmap.add(i);
}
}
bitmap = new WrappedImmutableRoaringBitmap(roaringBitmap.toImmutableRoaringBitmap());
break;
}
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void get(final Blackhole blackhole)
{
for (int i = pretendFilterOffsets.nextSetBit(0); i >= 0; i = pretendFilterOffsets.nextSetBit(i + 1)) {
final boolean isNull = bitmap.get(i);
if (isNull) {
blackhole.consume(null);
} else {
blackhole.consume(i);
}
}
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void iterator(final Blackhole blackhole)
{
IntIterator nullIterator = bitmap.iterator();
int nullMark = -1;
for (int i = pretendFilterOffsets.nextSetBit(0); i >= 0; i = pretendFilterOffsets.nextSetBit(i + 1)) {
while (nullMark < i && nullIterator.hasNext()) {
nullMark = nullIterator.next();
}
final boolean isNull = nullMark == i;
if (isNull) {
blackhole.consume(null);
} else {
blackhole.consume(i);
}
}
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void peekableIterator(final Blackhole blackhole)
{
PeekableIntIterator nullIterator = bitmap.peekableIterator();
int nullMark = -1;
for (int i = pretendFilterOffsets.nextSetBit(0); i >= 0; i = pretendFilterOffsets.nextSetBit(i + 1)) {
if (nullMark < i) {
nullIterator.advanceIfNeeded(i);
if (nullIterator.hasNext()) {
nullMark = nullIterator.next();
}
}
final boolean isNull = nullMark == i;
if (isNull) {
blackhole.consume(i);
} else {
blackhole.consume(null);
}
}
}
}
@@ -0,0 +1,53 @@
/*
* 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.bitmap;

import org.apache.druid.extendedset.intset.IntSet;
import org.roaringbitmap.PeekableIntIterator;

public class ConcisePeekableIteratorAdapter extends PeekableIteratorAdapter<IntSet.IntIterator>
{
ConcisePeekableIteratorAdapter(IntSet.IntIterator iterator)
{
super(iterator);
}

ConcisePeekableIteratorAdapter(IntSet.IntIterator iterator, int mark)
{
super(iterator, mark);
}

@Override
public void advanceIfNeeded(int i)
{
if (mark == null || i > mark) {
baseIterator.skipAllBefore(i);
if (baseIterator.hasNext()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i is past the end of the set, I'm guessing baseIterator.hasNext will be false and the mark will remain unchanged. That means next will return it, even though it's not >= i. Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops good catch, if there is no next it should reset the mark

mark = baseIterator.next();
}
}
}

@Override
public PeekableIntIterator clone()
{
return new ConcisePeekableIteratorAdapter(baseIterator.clone(), mark);
}
}
Expand Up @@ -21,6 +21,7 @@

import org.roaringbitmap.BatchIterator;
import org.roaringbitmap.IntIterator;
import org.roaringbitmap.PeekableIntIterator;

/**
* This class is meant to represent a simple wrapper around an immutable bitmap
Expand All @@ -33,6 +34,14 @@ public interface ImmutableBitmap
*/
IntIterator iterator();

/**
* @return a peekable iterator which can skip to a position
*/
default PeekableIntIterator peekableIterator()
{
return new PeekableIteratorAdapter(iterator());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: IntelliJ is complaining about raw types here

return new PeekableIteratorAdapter<>(iterator());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, will change

}

/**
* @return a batched iterator over the set bits of this bitmap
*/
Expand Down
@@ -0,0 +1,83 @@
/*
* 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.bitmap;

import com.google.common.base.Preconditions;
import org.roaringbitmap.IntIterator;
import org.roaringbitmap.PeekableIntIterator;

public class PeekableIteratorAdapter<TIntIterator extends IntIterator> implements PeekableIntIterator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you please add javadoc?

{
final TIntIterator baseIterator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really want all of these to be package private instead of protected?

also nit: empty line between static and class variables

Integer mark;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might perform better if you make mark an int and then use -1 to signify not being set.


PeekableIteratorAdapter(TIntIterator iterator)
{
this.baseIterator = Preconditions.checkNotNull(iterator, "iterator");
}

PeekableIteratorAdapter(TIntIterator iterator, int mark)
{
this(iterator);
this.mark = mark;
this.advanceIfNeeded(mark);
}

@Override
public void advanceIfNeeded(int i)
{
while ((mark == null || mark < i) && baseIterator.hasNext()) {
mark = baseIterator.next();
}
}

@Override
public int peekNext()
{
Preconditions.checkArgument(mark != null || baseIterator.hasNext());
if (mark == null) {
mark = baseIterator.next();
}
return mark;
}

@Override
public PeekableIntIterator clone()
{
return new PeekableIteratorAdapter(baseIterator.clone(), mark);
}

@Override
public boolean hasNext()
{
return mark != null || baseIterator.hasNext();
}

@Override
public int next()
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: even though it's missing in the javadoc of IntIterator, it would be a good convention to throw NoSuchElementException if hasNext() returns false.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking into the implementations of IntIterator this is wrapping, it looks like they will throw this exception, so I don't need to have the check here. I also removed the check from peekNext for the same reason.

if (mark != null) {
final int currentBit = mark;
mark = null;
return currentBit;
}
return baseIterator.next();
}
}
Expand Up @@ -22,6 +22,7 @@
import org.apache.druid.extendedset.intset.ConciseSet;
import org.apache.druid.extendedset.intset.ImmutableConciseSet;
import org.roaringbitmap.IntIterator;
import org.roaringbitmap.PeekableIntIterator;

public class WrappedConciseBitmap implements MutableBitmap
{
Expand Down Expand Up @@ -109,6 +110,12 @@ public IntIterator iterator()
return bitmap.iterator();
}

@Override
public PeekableIntIterator peekableIterator()
{
return new ConcisePeekableIteratorAdapter(bitmap.iterator());
}

@Override
public boolean isEmpty()
{
Expand Down
Expand Up @@ -19,9 +19,9 @@

package org.apache.druid.collections.bitmap;


import org.apache.druid.extendedset.intset.ImmutableConciseSet;
import org.roaringbitmap.IntIterator;
import org.roaringbitmap.PeekableIntIterator;

import java.nio.IntBuffer;

Expand Down Expand Up @@ -76,6 +76,12 @@ public IntIterator iterator()
return bitmap.iterator();
}

@Override
public PeekableIntIterator peekableIterator()
{
return new ConcisePeekableIteratorAdapter(bitmap.iterator());
}

@Override
public int size()
{
Expand Down