-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add predicate pushdown using filter2 api #296
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8ca447
Add predicate pushdown using filter2 api
7b019a6
update tests and logging
2666849
Fixed test to check the actual number of materialized rows from the r…
a39fdff
WIP: Handle a few error cases in Pig predicate pushdown.
rdblue f1ef73e
Fixed binary type and storing filter predicate
6b405b4
Merge remote-tracking branch 'rdblue/pig-predicate-pushdown' into pig…
388099b
Cleaning up imports
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,9 +30,13 @@ | |
| import static org.apache.parquet.pig.TupleReadSupport.PARQUET_COLUMN_INDEX_ACCESS; | ||
| import static org.apache.parquet.pig.TupleReadSupport.getPigSchemaFromMultipleFiles; | ||
|
|
||
| import static org.apache.parquet.filter2.predicate.FilterApi.*; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.ref.Reference; | ||
| import java.lang.ref.SoftReference; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.WeakHashMap; | ||
|
|
@@ -42,9 +46,13 @@ | |
| import org.apache.hadoop.mapreduce.Job; | ||
| import org.apache.hadoop.mapreduce.RecordReader; | ||
| import org.apache.hadoop.mapreduce.TaskAttemptContext; | ||
| import org.apache.parquet.filter2.predicate.FilterPredicate; | ||
| import org.apache.parquet.filter2.predicate.Operators; | ||
| import org.apache.parquet.io.api.Binary; | ||
| import org.apache.pig.Expression; | ||
| import org.apache.pig.LoadFunc; | ||
| import org.apache.pig.LoadMetadata; | ||
| import org.apache.pig.LoadPredicatePushdown; | ||
| import org.apache.pig.LoadPushDown; | ||
| import org.apache.pig.ResourceSchema; | ||
| import org.apache.pig.ResourceStatistics; | ||
|
|
@@ -57,6 +65,11 @@ | |
| import org.apache.pig.impl.util.UDFContext; | ||
| import org.apache.pig.parser.ParserException; | ||
|
|
||
| import static org.apache.pig.Expression.BinaryExpression; | ||
| import static org.apache.pig.Expression.Column; | ||
| import static org.apache.pig.Expression.Const; | ||
| import static org.apache.pig.Expression.OpType; | ||
|
|
||
| import org.apache.parquet.Log; | ||
| import org.apache.parquet.hadoop.ParquetInputFormat; | ||
| import org.apache.parquet.hadoop.metadata.GlobalMetaData; | ||
|
|
@@ -70,9 +83,12 @@ | |
| * @author Julien Le Dem | ||
| * | ||
| */ | ||
| public class ParquetLoader extends LoadFunc implements LoadMetadata, LoadPushDown { | ||
| public class ParquetLoader extends LoadFunc implements LoadMetadata, LoadPushDown, LoadPredicatePushdown { | ||
| private static final Log LOG = Log.getLog(ParquetLoader.class); | ||
|
|
||
| public static final String ENABLE_PREDICATE_FILTER_PUSHDOWN = "parquet.pig.predicate.pushdown.enable"; | ||
| private static final boolean DEFAULT_PREDICATE_PUSHDOWN_ENABLED = false; | ||
|
|
||
| // Using a weak hash map will ensure that the cache will be gc'ed when there is memory pressure | ||
| static final Map<String, Reference<ParquetInputFormat<Tuple>>> inputFormatCache = new WeakHashMap<String, Reference<ParquetInputFormat<Tuple>>>(); | ||
|
|
||
|
|
@@ -169,6 +185,11 @@ private void setInput(String location, Job job) throws IOException { | |
| getConfiguration(job).set(PARQUET_PIG_SCHEMA, pigSchemaToString(schema)); | ||
| getConfiguration(job).set(PARQUET_PIG_REQUIRED_FIELDS, serializeRequiredFieldList(requiredFieldList)); | ||
| getConfiguration(job).set(PARQUET_COLUMN_INDEX_ACCESS, Boolean.toString(columnIndexAccess)); | ||
|
|
||
| FilterPredicate filterPredicate = (FilterPredicate) getFromUDFContext(ParquetInputFormat.FILTER_PREDICATE); | ||
| if(filterPredicate != null) { | ||
| ParquetInputFormat.setFilterPredicate(getConfiguration(job), filterPredicate); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -380,4 +401,149 @@ private Schema getSchemaFromRequiredFieldList(Schema schema, List<RequiredField> | |
| return s; | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getPredicateFields(String s, Job job) throws IOException { | ||
| if(!job.getConfiguration().getBoolean(ENABLE_PREDICATE_FILTER_PUSHDOWN, DEFAULT_PREDICATE_PUSHDOWN_ENABLED)) { | ||
| return null; | ||
| } | ||
|
|
||
| List<String> fields = new ArrayList<String>(); | ||
|
|
||
| for(FieldSchema field : schema.getFields()) { | ||
| switch(field.type) { | ||
| case DataType.BOOLEAN: | ||
| case DataType.INTEGER: | ||
| case DataType.LONG: | ||
| case DataType.FLOAT: | ||
| case DataType.DOUBLE: | ||
| case DataType.CHARARRAY: | ||
| fields.add(field.alias); | ||
| break; | ||
| default: | ||
| // Skip BYTEARRAY, TUPLE, MAP, BAG, DATETIME, BIGINTEGER, BIGDECIMAL | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return fields; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Expression.OpType> getSupportedExpressionTypes() { | ||
| OpType supportedTypes [] = { | ||
| OpType.OP_EQ, | ||
| OpType.OP_GT, | ||
| OpType.OP_GE, | ||
| OpType.OP_LT, | ||
| OpType.OP_LE, | ||
| OpType.OP_AND, | ||
| OpType.OP_OR | ||
| }; | ||
|
|
||
| return Arrays.asList(supportedTypes); | ||
| } | ||
|
|
||
| @Override | ||
| public void setPushdownPredicate(Expression e) throws IOException { | ||
| LOG.info("Pig pushdown expression: " + e); | ||
|
|
||
| FilterPredicate pred = buildFilter(e); | ||
| LOG.info("Parquet filter predicate expression: " + pred); | ||
|
|
||
| storeInUDFContext(ParquetInputFormat.FILTER_PREDICATE, pred); | ||
| } | ||
|
|
||
| private FilterPredicate buildFilter(Expression e) { | ||
| if (e instanceof BinaryExpression) { | ||
| Expression lhs = ((BinaryExpression) e).getLhs(); | ||
| Expression rhs = ((BinaryExpression) e).getRhs(); | ||
| OpType op = e.getOpType(); | ||
|
|
||
| FilterPredicate lfp; | ||
| FilterPredicate rfp; | ||
| switch (op) { | ||
| case OP_AND: | ||
| lfp = buildFilter(lhs); | ||
| rfp = buildFilter(rhs); | ||
| if (lfp == null || rfp == null) { | ||
| return null; | ||
| } | ||
| return and(lfp, rfp); | ||
| case OP_OR: | ||
| lfp = buildFilter(lhs); | ||
| rfp = buildFilter(rhs); | ||
| if (lfp == null || rfp == null) { | ||
| return null; | ||
| } | ||
| return or(lfp, rfp); | ||
| } | ||
|
|
||
| if (lhs instanceof Column && rhs instanceof Const) { | ||
| return buildFilter(op, (Column) lhs, (Const) rhs); | ||
| } else if (lhs instanceof Const && rhs instanceof Column) { | ||
| return buildFilter(op, (Column) rhs, (Const) lhs); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private FilterPredicate buildFilter(OpType op, Column col, Const value) { | ||
| String name = col.getName(); | ||
| try { | ||
| FieldSchema f = schema.getField(name); | ||
| switch (f.type) { | ||
| case DataType.BOOLEAN: | ||
| Operators.BooleanColumn boolCol = booleanColumn(name); | ||
| switch(op) { | ||
| case OP_EQ: return eq(boolCol, getValue(value, boolCol.getColumnType())); | ||
| case OP_NE: return notEq(boolCol, getValue(value, boolCol.getColumnType())); | ||
| } | ||
| case DataType.INTEGER: | ||
| Operators.IntColumn intCol = intColumn(name); | ||
| return op(op, intCol, value); | ||
| case DataType.LONG: | ||
| Operators.LongColumn longCol = longColumn(name); | ||
| return op(op, longCol, value); | ||
| case DataType.FLOAT: | ||
| Operators.FloatColumn floatCol = floatColumn(name); | ||
| return op(op, floatCol, value); | ||
| case DataType.DOUBLE: | ||
| Operators.DoubleColumn doubleCol = doubleColumn(name); | ||
| return op(op, doubleCol, value); | ||
| case DataType.CHARARRAY: | ||
| Operators.BinaryColumn binaryCol = binaryColumn(name); | ||
| return op(op, binaryCol, value); | ||
|
|
||
| } | ||
| } catch (FrontendException e) { | ||
| throw new RuntimeException("Error processing pushdown for column:" + col, e); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private <C extends Comparable<C>, COL extends Operators.Column<C> & Operators.SupportsLtGt> | ||
| FilterPredicate op(Expression.OpType op, COL col, Expression valueExpr) { | ||
| C value = getValue(valueExpr, col.getColumnType()); | ||
| switch (op) { | ||
| case OP_EQ: return eq(col, value); | ||
| case OP_GT: return gt(col, value); | ||
| case OP_GE: return gtEq(col, value); | ||
| case OP_LT: return lt(col, value); | ||
| case OP_LE: return ltEq(col, value); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private <C extends Comparable<C>> C getValue(Expression expr, Class<C> type) { | ||
| Comparable value = (Comparable) ((Const) expr).getValue(); | ||
|
Member
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.
|
||
|
|
||
| if (value instanceof String) { | ||
| value = Binary.fromString((String) value); | ||
| } | ||
|
|
||
| return type.cast(value); | ||
| } | ||
|
|
||
| } | ||
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
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
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.
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.
What's the rationale behind not using predicate pushdown by default? Not breaking existing jobs until it is well tested?
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.
Yes, but if other who are more familiar with pig pushdown or filter api think it's worth turning on by default, I'm OK with that as well.
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.
I think it makes sense to have it on by default once we're confident.
Possibly once it has run in production and works well?