Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
*/

package org.apache.parquet.column.values.bloomfilter;

import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.apache.parquet.Preconditions;
import org.apache.parquet.bytes.BytesUtils;
import org.apache.parquet.io.api.Binary;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -139,7 +141,7 @@ private BlockSplitBloomFilter(byte[] bitset, HashStrategy hashStrategy) {
hashFunction = Hashing.murmur3_128(DEFAULT_SEED);
break;
default:
throw new RuntimeException("Not supported hash strategy");
throw new RuntimeException("Unsupported hash strategy");
}
}

Expand Down Expand Up @@ -254,6 +256,38 @@ public static int optimalNumOfBits(long n, double p) {
return numBits;
}

@Override
public long getBitsetSize() {
return this.bitset.length;
}

@Override
public long hash(Object value) {
ByteBuffer plain;

if (value instanceof Binary) {
return hashFunction.hashBytes(((Binary) value).getBytes()).asLong();
}

if (value instanceof Integer) {
plain = ByteBuffer.allocate(Integer.SIZE/Byte.SIZE);
plain.order(ByteOrder.LITTLE_ENDIAN).putInt(((Integer)value).intValue());
} else if (value instanceof Long) {
plain = ByteBuffer.allocate(Long.SIZE/Byte.SIZE);
plain.order(ByteOrder.LITTLE_ENDIAN).putLong(((Long)value).longValue());
} else if (value instanceof Float) {
plain = ByteBuffer.allocate(Float.SIZE/Byte.SIZE);
plain.order(ByteOrder.LITTLE_ENDIAN).putFloat(((Float)value).floatValue());
} else if (value instanceof Double) {
plain = ByteBuffer.allocate(Double.SIZE/ Byte.SIZE);
plain.order(ByteOrder.LITTLE_ENDIAN).putDouble(((Double)value).doubleValue());
} else {
throw new RuntimeException("Parquet Bloom filter: Not supported type");
}

return hashFunction.hashBytes(plain.array()).asLong();
}

@Override
public long hash(int value) {
ByteBuffer plain = ByteBuffer.allocate(Integer.SIZE/Byte.SIZE);
Expand Down Expand Up @@ -286,9 +320,4 @@ public long hash(float value) {
public long hash(Binary value) {
return hashFunction.hashBytes(value.getBytes()).asLong();
}

@Override
public long getBitsetSize() {
return this.bitset.length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.parquet.column.values.bloomfilter;

import org.apache.parquet.io.api.Binary;

import java.io.IOException;
import java.io.OutputStream;

Expand Down Expand Up @@ -70,6 +71,13 @@ enum Algorithm {
*/
boolean findHash(long hash);

/**
* Get the number of bytes for bitset in this Bloom filter.
*
* @return The number of bytes for bitset in this Bloom filter.
*/
long getBitsetSize();

/**
* Compute hash for int value by using its plain encoding result.
*
Expand Down Expand Up @@ -111,9 +119,10 @@ enum Algorithm {
long hash(Binary value);

/**
* Get the number of bytes for bitset in this Bloom filter.
* Compute hash for Object value by using its plain encoding result.
*
* @return The number of bytes for bitset in this Bloom filter.
* @param value the value to hash
* @return hash result
*/
long getBitsetSize();
long hash(Object value);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* 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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.parquet.column.values.bloomfilter.BloomFilter;
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.BloomFilterReader;
import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData;
import org.apache.parquet.hadoop.metadata.ColumnPath;

import static org.apache.parquet.Preconditions.checkNotNull;

public class BloomFilterImpl implements FilterPredicate.Visitor<Boolean>{
private static final Logger LOG = LoggerFactory.getLogger(BloomFilterImpl.class);
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);
}

@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());
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;
}

try {
BloomFilter bloomFilter = bloomFilterReader.readBloomFilter(meta);
if (bloomFilter != null && !bloomFilter.findHash(bloomFilter.hash(value))) {
return BLOCK_CANNOT_MATCH;
}
} catch (RuntimeException e) {
LOG.warn(e.getMessage());
return BLOCK_MIGHT_MATCH;
}

return BLOCK_MIGHT_MATCH;
}

@Override
public <T extends Comparable<T>> Boolean visit(Operators.NotEq<T> notEq) {
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) {
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
/*
* 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
Expand All @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.List;

import org.apache.parquet.filter2.BloomFilterLevel.BloomFilterImpl;
import org.apache.parquet.filter2.compat.FilterCompat.Filter;
import org.apache.parquet.filter2.compat.FilterCompat.NoOpFilter;
import org.apache.parquet.filter2.compat.FilterCompat.Visitor;
Expand All @@ -32,6 +33,8 @@
import org.apache.parquet.hadoop.ParquetFileReader;
import org.apache.parquet.hadoop.metadata.BlockMetaData;
import org.apache.parquet.schema.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.parquet.Preconditions.checkNotNull;

Expand All @@ -45,10 +48,12 @@ public class RowGroupFilter implements Visitor<List<BlockMetaData>> {
private final MessageType schema;
private final List<FilterLevel> levels;
private final ParquetFileReader reader;
private Logger logger = LoggerFactory.getLogger(RowGroupFilter.class);

public enum FilterLevel {
STATISTICS,
DICTIONARY
DICTIONARY,
BLOOMFILTER
}

/**
Expand Down Expand Up @@ -104,6 +109,11 @@ public List<BlockMetaData> visit(FilterCompat.FilterPredicateCompat filterPredic
drop = DictionaryFilter.canDrop(filterPredicate, block.getColumns(), reader.getDictionaryReader(block));
}

if (!drop && levels.contains(FilterLevel.BLOOMFILTER)) {
drop = BloomFilterImpl.canDrop(filterPredicate, block.getColumns(), reader.getBloomFilterDataReader(block));
if (drop) logger.info("Block drop by Bloom filter");
}

if(!drop) {
filteredBlocks.add(block);
}
Expand Down
Loading