-
Notifications
You must be signed in to change notification settings - Fork 3.8k
optimize numeric column null value checking for low filter selectivity (more rows) #8822
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
Changes from all 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,196 @@ | ||
| /* | ||
| * 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(isNull); | ||
| } else { | ||
| blackhole.consume(i); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
| public void iterator(final Blackhole blackhole) | ||
| { | ||
| IntIterator nullIterator = bitmap.iterator(); | ||
| int offsetMark = -1; | ||
| int nullMark = -1; | ||
| for (int i = pretendFilterOffsets.nextSetBit(0); i >= 0; i = pretendFilterOffsets.nextSetBit(i + 1)) { | ||
| // this is totally useless, hopefully this doesn't get optimized out, try to mimic what the selector is doing | ||
| if (i < offsetMark) { | ||
| nullMark = -1; | ||
| nullIterator = bitmap.iterator(); | ||
| } | ||
| offsetMark = i; | ||
| while (nullMark < i && nullIterator.hasNext()) { | ||
| nullMark = nullIterator.next(); | ||
| } | ||
| final boolean isNull = nullMark == offsetMark; | ||
| if (isNull) { | ||
| blackhole.consume(isNull); | ||
| } else { | ||
| blackhole.consume(offsetMark); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
| public void peekableIterator(final Blackhole blackhole) | ||
| { | ||
| PeekableIntIterator nullIterator = bitmap.peekableIterator(); | ||
| int offsetMark = -1; | ||
| int nullMark = -1; | ||
| for (int i = pretendFilterOffsets.nextSetBit(0); i >= 0; i = pretendFilterOffsets.nextSetBit(i + 1)) { | ||
| // this is totally useless, hopefully this doesn't get optimized out, try to mimic what the selector is doing | ||
| if (i < offsetMark) { | ||
| nullMark = -1; | ||
| nullIterator = bitmap.peekableIterator(); | ||
| } | ||
| offsetMark = i; | ||
| if (nullMark < i) { | ||
| nullIterator.advanceIfNeeded(i); | ||
| if (nullIterator.hasNext()) { | ||
| nullMark = nullIterator.next(); | ||
| } | ||
| } | ||
| final boolean isNull = nullMark == offsetMark; | ||
| if (isNull) { | ||
| blackhole.consume(isNull); | ||
| } else { | ||
| blackhole.consume(offsetMark); | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| public class ConcisePeekableIteratorAdapter extends PeekableIteratorAdapter<IntSet.IntIterator> | ||
| { | ||
| ConcisePeekableIteratorAdapter(IntSet.IntIterator iterator) | ||
| { | ||
| super(iterator); | ||
| } | ||
|
|
||
| @Override | ||
| public void advanceIfNeeded(int i) | ||
| { | ||
| if (mark < i) { | ||
| baseIterator.skipAllBefore(i); | ||
| if (baseIterator.hasNext()) { | ||
| mark = baseIterator.next(); | ||
| } else { | ||
| mark = NOT_SET; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * 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 | ||
|
Contributor
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? |
||
| { | ||
| static final int NOT_SET = -1; | ||
| final TIntIterator baseIterator; | ||
|
Contributor
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 |
||
| int mark = NOT_SET; | ||
|
Contributor
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:
Member
Author
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. heh, I originally was calling this some form of 'next', however that was also confusing to me because it's actually the previous value of the
Contributor
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. Either is fine, maybe a javadoc explaining what it is. |
||
|
|
||
| PeekableIteratorAdapter(TIntIterator iterator) | ||
| { | ||
| this.baseIterator = Preconditions.checkNotNull(iterator, "iterator"); | ||
| } | ||
|
|
||
| @Override | ||
| public void advanceIfNeeded(int i) | ||
| { | ||
| while (mark < i && baseIterator.hasNext()) { | ||
|
Contributor
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. is
Member
Author
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. yes,
Contributor
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. Thinking out loud here - feel free to ignore. According to the interface javadocs If we set
Contributor
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. I'd suggest dealing with this, if at all, just through javadocs saying that these interfaces should only be used with nonnegative ints. In practice these iterators are used for iterating over bitmaps that represent row numbers. It's doubly-impossible to see negative numbers: bitmaps cannot store negative ints, and row numbers cannot be negative either.
Contributor
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. sgtm
Member
Author
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. javadocs for this don't seem especially necessary to me, I can add if there is any other changes I need to make to this PR, otherwise I'd prefer to skip to avoid churning through CI again |
||
| mark = baseIterator.next(); | ||
| } | ||
| if (mark < i) { | ||
| mark = NOT_SET; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int peekNext() | ||
| { | ||
| if (mark == NOT_SET) { | ||
| mark = baseIterator.next(); | ||
| } | ||
| return mark; | ||
| } | ||
|
|
||
| @Override | ||
| public PeekableIntIterator clone() | ||
| { | ||
| throw new UnsupportedOperationException( | ||
| "PeekableIteratorAdapter.clone is not implemented, but this should not happen" | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() | ||
| { | ||
| return mark != NOT_SET || baseIterator.hasNext(); | ||
| } | ||
|
|
||
| @Override | ||
| public int next() | ||
| { | ||
|
Contributor
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
Member
Author
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 != NOT_SET) { | ||
| final int currentBit = mark; | ||
| mark = NOT_SET; | ||
| 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
iis past the end of the set, I'm guessingbaseIterator.hasNextwill be false and themarkwill remain unchanged. That meansnextwill 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