-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Changes from 4 commits
e8c5644
53e8891
8d6133e
58f9803
175b809
e1a98a7
7d9063e
e14d61b
d6cd124
52a6f3b
0dbc0cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) { | ||
mark = baseIterator.next(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public PeekableIntIterator clone() | ||
{ | ||
return new ConcisePeekableIteratorAdapter(baseIterator.clone(), mark); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: IntelliJ is complaining about raw types here
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you please add javadoc? |
||
{ | ||
final TIntIterator baseIterator; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might perform better if you make |
||
|
||
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() | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: even though it's missing in the javadoc of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looking into the implementations of |
||
if (mark != null) { | ||
final int currentBit = mark; | ||
mark = null; | ||
return currentBit; | ||
} | ||
return baseIterator.next(); | ||
} | ||
} |
There was a problem hiding this comment.
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 guessingbaseIterator.hasNext
will be false and themark
will remain unchanged. That meansnext
will return it, even though it's not>= i
. Is that right?There was a problem hiding this comment.
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