|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.sql.gen.columnar; |
| 15 | + |
| 16 | +import com.google.common.annotations.VisibleForTesting; |
| 17 | +import com.google.common.collect.ImmutableList; |
| 18 | +import io.airlift.slice.Slice; |
| 19 | +import io.airlift.slice.XxHash64; |
| 20 | +import io.trino.operator.project.InputChannels; |
| 21 | +import io.trino.spi.Page; |
| 22 | +import io.trino.spi.block.Block; |
| 23 | +import io.trino.spi.block.ValueBlock; |
| 24 | +import io.trino.spi.connector.ConnectorSession; |
| 25 | +import io.trino.spi.predicate.Domain; |
| 26 | +import io.trino.spi.type.CharType; |
| 27 | +import io.trino.spi.type.Type; |
| 28 | +import io.trino.spi.type.VarbinaryType; |
| 29 | +import io.trino.spi.type.VarcharType; |
| 30 | + |
| 31 | +import java.util.List; |
| 32 | +import java.util.function.Supplier; |
| 33 | + |
| 34 | +import static com.google.common.base.Verify.verify; |
| 35 | +import static java.util.Objects.requireNonNull; |
| 36 | + |
| 37 | +public class BloomFilter |
| 38 | +{ |
| 39 | + private BloomFilter() {} |
| 40 | + |
| 41 | + public static boolean canUseBloomFilter(Domain domain) |
| 42 | + { |
| 43 | + Type type = domain.getType(); |
| 44 | + if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType) { |
| 45 | + verify(type.getJavaType() == Slice.class, "Type is not backed by Slice"); |
| 46 | + return !domain.isNone() && !domain.isAll() && domain.isNullableDiscreteSet(); |
| 47 | + } |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + public static Supplier<FilterEvaluator> createBloomFilterEvaluator(Domain domain, int inputChannel) |
| 52 | + { |
| 53 | + return () -> new ColumnarFilterEvaluator( |
| 54 | + new DictionaryAwareColumnarFilter( |
| 55 | + new ColumnarBloomFilter(domain.getNullableDiscreteSet(), inputChannel, domain.getType()))); |
| 56 | + } |
| 57 | + |
| 58 | + private static final class ColumnarBloomFilter |
| 59 | + implements ColumnarFilter |
| 60 | + { |
| 61 | + private final SliceBloomFilter filter; |
| 62 | + private final boolean isNullAllowed; |
| 63 | + private final InputChannels inputChannels; |
| 64 | + |
| 65 | + public ColumnarBloomFilter(Domain.DiscreteSet discreteSet, int inputChannel, Type type) |
| 66 | + { |
| 67 | + this.isNullAllowed = discreteSet.containsNull(); |
| 68 | + this.filter = new SliceBloomFilter((List<Slice>) (List<?>) discreteSet.getNonNullValues(), type); |
| 69 | + this.inputChannels = new InputChannels(ImmutableList.of(inputChannel), ImmutableList.of(inputChannel)); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public int filterPositionsRange(ConnectorSession session, int[] outputPositions, int offset, int size, Page page) |
| 74 | + { |
| 75 | + ValueBlock block = (ValueBlock) page.getBlock(0); |
| 76 | + int selectedPositionsCount = 0; |
| 77 | + for (int position = offset; position < offset + size; position++) { |
| 78 | + boolean result = block.isNull(position) ? isNullAllowed : filter.test(block, position); |
| 79 | + outputPositions[selectedPositionsCount] = position; |
| 80 | + selectedPositionsCount += result ? 1 : 0; |
| 81 | + } |
| 82 | + return selectedPositionsCount; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public int filterPositionsList(ConnectorSession session, int[] outputPositions, int[] activePositions, int offset, int size, Page page) |
| 87 | + { |
| 88 | + ValueBlock block = (ValueBlock) page.getBlock(0); |
| 89 | + int selectedPositionsCount = 0; |
| 90 | + for (int index = offset; index < offset + size; index++) { |
| 91 | + int position = activePositions[index]; |
| 92 | + boolean result = block.isNull(position) ? isNullAllowed : filter.test(block, position); |
| 93 | + outputPositions[selectedPositionsCount] = position; |
| 94 | + selectedPositionsCount += result ? 1 : 0; |
| 95 | + } |
| 96 | + return selectedPositionsCount; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public InputChannels getInputChannels() |
| 101 | + { |
| 102 | + return inputChannels; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static final class SliceBloomFilter |
| 107 | + { |
| 108 | + private final long[] bloom; |
| 109 | + private final int bloomSizeMask; |
| 110 | + private final Type type; |
| 111 | + |
| 112 | + /** |
| 113 | + * A Bloom filter for a set of Slice values. |
| 114 | + * This is approx 2X faster than the Bloom filter implementations in ORC and parquet because |
| 115 | + * it uses single hash function and uses that to set 3 bits within a 64 bit word. |
| 116 | + * The memory footprint is up to (4 * values.size()) bytes, which is much smaller than maintaining a hash set of strings. |
| 117 | + * |
| 118 | + * @param values List of values used for filtering |
| 119 | + */ |
| 120 | + public SliceBloomFilter(List<Slice> values, Type type) |
| 121 | + { |
| 122 | + this.type = requireNonNull(type, "type is null"); |
| 123 | + int bloomSize = getBloomFilterSize(values.size()); |
| 124 | + bloom = new long[bloomSize]; |
| 125 | + bloomSizeMask = bloomSize - 1; |
| 126 | + for (Slice value : values) { |
| 127 | + long hashCode = XxHash64.hash(value); |
| 128 | + // Set 3 bits in a 64 bit word |
| 129 | + bloom[bloomIndex(hashCode)] |= bloomMask(hashCode); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static int getBloomFilterSize(int valuesCount) |
| 134 | + { |
| 135 | + // Linear hash table size is the highest power of two less than or equal to number of values * 4. This means that the |
| 136 | + // table is under half full, e.g. 127 elements gets 256 slots. |
| 137 | + int hashTableSize = Integer.highestOneBit(valuesCount * 4); |
| 138 | + // We will allocate 8 bits in the bloom filter for every slot in a comparable hash table. |
| 139 | + // The bloomSize is a count of longs, hence / 8. |
| 140 | + return Math.max(1, hashTableSize / 8); |
| 141 | + } |
| 142 | + |
| 143 | + public boolean test(Block block, int position) |
| 144 | + { |
| 145 | + return contains(type.getSlice(block, position)); |
| 146 | + } |
| 147 | + |
| 148 | + public boolean contains(Slice data) |
| 149 | + { |
| 150 | + long hashCode = XxHash64.hash(data); |
| 151 | + long mask = bloomMask(hashCode); |
| 152 | + return mask == (bloom[bloomIndex(hashCode)] & mask); |
| 153 | + } |
| 154 | + |
| 155 | + @VisibleForTesting |
| 156 | + public boolean contains(Slice data, int offset, int length) |
| 157 | + { |
| 158 | + long hashCode = XxHash64.hash(data, offset, length); |
| 159 | + long mask = bloomMask(hashCode); |
| 160 | + return mask == (bloom[bloomIndex(hashCode)] & mask); |
| 161 | + } |
| 162 | + |
| 163 | + private int bloomIndex(long hashCode) |
| 164 | + { |
| 165 | + // Lower 21 bits are not used by bloomMask |
| 166 | + // These are enough for the maximum size array that will be used here |
| 167 | + return (int) (hashCode & bloomSizeMask); |
| 168 | + } |
| 169 | + |
| 170 | + private static long bloomMask(long hashCode) |
| 171 | + { |
| 172 | + // returned mask sets 3 bits based on portions of given hash |
| 173 | + // Extract 38th to 43rd bits |
| 174 | + return (1L << ((hashCode >> 21) & 63)) |
| 175 | + // Extract 32nd to 37th bits |
| 176 | + | (1L << ((hashCode >> 27) & 63)) |
| 177 | + // Extract 26th to 31st bits |
| 178 | + | (1L << ((hashCode >> 33) & 63)); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments