-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PARQUET-1391: Integrate Bloom filter logic #619
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...column/src/main/java/org/apache/parquet/column/values/bloomfilter/BloomFilterUtility.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.parquet.column.values.bloomfilter; | ||
|
|
||
| import org.apache.parquet.io.api.Binary; | ||
|
|
||
| public abstract class BloomFilterUtility<T extends Comparable<T>> { | ||
| public BloomFilter bloomFilter; | ||
| public abstract boolean contains(T value); | ||
|
|
||
| public static class IntBloomFilter extends BloomFilterUtility<Integer> { | ||
| public IntBloomFilter(BloomFilter bloomFilter) { | ||
| this.bloomFilter = bloomFilter; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean contains(Integer value) { | ||
| return bloomFilter.findHash(bloomFilter.hash(value.intValue())); | ||
| } | ||
| } | ||
|
|
||
| public static class LongBloomFilter extends BloomFilterUtility<Long> { | ||
| public LongBloomFilter(BloomFilter bloomFilter) { | ||
| this.bloomFilter = bloomFilter; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean contains(Long value) { | ||
| return bloomFilter.findHash(bloomFilter.hash(value.longValue())); | ||
| } | ||
| } | ||
|
|
||
| public static class DoubleBloomFilter extends BloomFilterUtility<Double> { | ||
| public DoubleBloomFilter(BloomFilter bloomFilter) { | ||
| this.bloomFilter = bloomFilter; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean contains(Double value) { | ||
| return bloomFilter.findHash(bloomFilter.hash(value.doubleValue())); | ||
| } | ||
| } | ||
|
|
||
| public static class FloatBloomFilter extends BloomFilterUtility<Float> { | ||
| public FloatBloomFilter(BloomFilter bloomFilter) { | ||
| this.bloomFilter = bloomFilter; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean contains(Float value) { | ||
| return bloomFilter.findHash(bloomFilter.hash(value.floatValue())); | ||
| } | ||
| } | ||
|
|
||
| public static class BinaryBloomFilter extends BloomFilterUtility<Binary> { | ||
| public BinaryBloomFilter(BloomFilter bloomFilter) { | ||
| this.bloomFilter = bloomFilter; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean contains(Binary value) { | ||
| return bloomFilter.findHash(bloomFilter.hash(value)); | ||
| } | ||
| } | ||
| } | ||
194 changes: 194 additions & 0 deletions
194
...uet-hadoop/src/main/java/org/apache/parquet/filter2/BloomFilterLevel/BloomFilterImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| /* | ||
| * 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.parquet.filter2.BloomFilterLevel; | ||
|
|
||
| import org.apache.parquet.column.ColumnDescriptor; | ||
| import org.apache.parquet.column.values.bloomfilter.BloomFilterReader; | ||
| import org.apache.parquet.column.values.bloomfilter.BloomFilterUtility; | ||
| import org.apache.parquet.filter2.predicate.FilterPredicate; | ||
| import org.apache.parquet.filter2.predicate.Operators; | ||
| import org.apache.parquet.filter2.predicate.UserDefinedPredicate; | ||
| import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData; | ||
| import org.apache.parquet.hadoop.metadata.ColumnPath; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.apache.parquet.Preconditions.checkNotNull; | ||
|
|
||
|
|
||
| public class BloomFilterImpl implements FilterPredicate.Visitor<Boolean>{ | ||
| private static final boolean BLOCK_MIGHT_MATCH = false; | ||
| private static final boolean BLOCK_CANNOT_MATCH = true; | ||
|
|
||
| private final Map<ColumnPath, ColumnChunkMetaData> columns = new HashMap<ColumnPath, ColumnChunkMetaData>(); | ||
|
|
||
| public static boolean canDrop(FilterPredicate pred, List<ColumnChunkMetaData> columns, BloomFilterReader bloomFilterReader) { | ||
| checkNotNull(pred, "pred"); | ||
| checkNotNull(columns, "columns"); | ||
| return pred.accept(new BloomFilterImpl(columns, bloomFilterReader)); | ||
| } | ||
|
|
||
| private BloomFilterImpl(List<ColumnChunkMetaData> columnsList, BloomFilterReader bloomFilterReader) { | ||
| for (ColumnChunkMetaData chunk : columnsList) { | ||
| columns.put(chunk.getPath(), chunk); | ||
| } | ||
|
|
||
| this.bloomFilterReader = bloomFilterReader; | ||
| } | ||
|
|
||
| private BloomFilterReader bloomFilterReader; | ||
|
|
||
| private ColumnChunkMetaData getColumnChunk(ColumnPath columnPath) { | ||
| return columns.get(columnPath); | ||
| } | ||
|
|
||
| // is this column chunk composed entirely of nulls? | ||
| // assumes the column chunk's statistics is not empty | ||
| private boolean isAllNulls(ColumnChunkMetaData column) { | ||
gszadovszky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
gszadovszky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return column.getStatistics().getNumNulls() == column.getValueCount(); | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends Comparable<T>> Boolean visit(Operators.Eq<T> eq) { | ||
| T value = eq.getValue(); | ||
|
|
||
| if (value == null) { | ||
| // the bloom filter bitset contains only non-null values so isn't helpful. this | ||
| // could check the column stats, but the StatisticsFilter is responsible | ||
| return BLOCK_MIGHT_MATCH; | ||
| } | ||
|
|
||
| Operators.Column<T> filterColumn = eq.getColumn(); | ||
| ColumnChunkMetaData meta = getColumnChunk(filterColumn.getColumnPath()); | ||
| ColumnDescriptor col = new ColumnDescriptor(meta.getPath().toArray(), meta.getType(), -1, -1); | ||
| if (meta == null) { | ||
| // the column isn't in this file so all values are null, but the value | ||
| // must be non-null because of the above check. | ||
| return BLOCK_CANNOT_MATCH; | ||
| } | ||
|
|
||
| BloomFilterUtility bloomFilterUtility = bloomFilterReader.buildBloomFilterUtility(col); | ||
gszadovszky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (bloomFilterUtility != null && !bloomFilterUtility.contains(value)) { | ||
| return BLOCK_CANNOT_MATCH; | ||
| } | ||
|
|
||
| return BLOCK_MIGHT_MATCH; | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends Comparable<T>> Boolean visit(Operators.NotEq<T> notEq) { | ||
| Operators.Column<T> filterColumn = notEq.getColumn(); | ||
| ColumnChunkMetaData meta = getColumnChunk(filterColumn.getColumnPath()); | ||
|
|
||
| T value = notEq.getValue(); | ||
|
|
||
| if (meta == null) { | ||
| if (value == null) { | ||
| // null is always equal to null | ||
| return BLOCK_CANNOT_MATCH; | ||
| } | ||
| return BLOCK_MIGHT_MATCH; | ||
| } | ||
|
|
||
| if (value == null) { | ||
| // we are looking for records where v notEq(null) | ||
| // so, if this is a column of all nulls, we can drop it | ||
| return isAllNulls(meta); | ||
| } | ||
|
|
||
| return BLOCK_MIGHT_MATCH; | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends Comparable<T>> Boolean visit(Operators.Lt<T> lt) { | ||
| return BLOCK_MIGHT_MATCH; | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends Comparable<T>> Boolean visit(Operators.LtEq<T> ltEq) { | ||
| return BLOCK_MIGHT_MATCH; | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends Comparable<T>> Boolean visit(Operators.Gt<T> gt) { | ||
| return BLOCK_MIGHT_MATCH; | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends Comparable<T>> Boolean visit(Operators.GtEq<T> gtEq) { | ||
| return BLOCK_MIGHT_MATCH; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visit(Operators.And and) { | ||
| return and.getLeft().accept(this) || and.getRight().accept(this); | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visit(Operators.Or or) { | ||
| return or.getLeft().accept(this) && or.getRight().accept(this); | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visit(Operators.Not not) { | ||
| throw new IllegalArgumentException( | ||
| "This predicate contains a not! Did you forget to run this predicate through LogicalInverseRewriter? " + not); | ||
| } | ||
|
|
||
|
|
||
| private <T extends Comparable<T>, U extends UserDefinedPredicate<T>> Boolean visit(Operators.UserDefined<T, U> ud, boolean inverted) { | ||
| Operators.Column<T> filterColumn = ud.getColumn(); | ||
| ColumnChunkMetaData columnChunk = getColumnChunk(filterColumn.getColumnPath()); | ||
| U udp = ud.getUserDefinedPredicate(); | ||
|
|
||
| if (columnChunk == null) { | ||
| // the column isn't in this file so all values are null. | ||
| // lets run the udp with null value to see if it keeps null or not. | ||
| if (inverted) { | ||
| return udp.keep(null); | ||
| } else { | ||
| return !udp.keep(null); | ||
| } | ||
| } | ||
|
|
||
| if (isAllNulls(columnChunk)) { | ||
| // lets run the udp with null value to see if it keeps null or not. | ||
| if (inverted) { | ||
| return udp.keep(null); | ||
| } else { | ||
| return !udp.keep(null); | ||
| } | ||
| } | ||
|
|
||
| return BLOCK_MIGHT_MATCH; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public <T extends Comparable<T>, U extends UserDefinedPredicate<T>> Boolean visit(Operators.UserDefined<T, U> udp) { | ||
| return visit(udp, false); | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends Comparable<T>, U extends UserDefinedPredicate<T>> Boolean visit(Operators.LogicalNotUserDefined<T, U> udp) { | ||
| return visit(udp.getUserDefined(), true); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.