-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Dereference projections and predicate pushdown in Hive #1720
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aa6c54e
Fix spelling in PlanOptimizers class
phd3 62a82f7
Encode BigDecimal to Slice in StructuralTestUtil::appendToBlockBuilder
phd3 de5d550
Support projected columns in Hive
phd3 29a3f67
Implement pushdown of dereference projections into hive connector
phd3 4479caa
Add dereference pushdown integration tests for all hive formats
phd3 6bd9709
Add projectSufficientColumns method to ReaderProjections
phd3 77590e9
Create uniquely named test columns in TestOrcPageSourceMemoryTracking
phd3 3cbeec4
Add plan and rule test for Hive Projection Pushdown
phd3 c80c121
Add tests for schema mismatch with dereference projections
phd3 90243f3
Remove unused parameter in createTestFile method
phd3 1dc3447
Fix inaccurate test assertion message
phd3 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
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
189 changes: 189 additions & 0 deletions
189
presto-hive/src/main/java/io/prestosql/plugin/hive/HiveApplyProjectionUtil.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,189 @@ | ||
| /* | ||
| * Licensed 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 io.prestosql.plugin.hive; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.collect.ImmutableList; | ||
| import io.prestosql.spi.connector.ColumnHandle; | ||
| import io.prestosql.spi.expression.ConnectorExpression; | ||
| import io.prestosql.spi.expression.FieldDereference; | ||
| import io.prestosql.spi.expression.Variable; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| final class HiveApplyProjectionUtil | ||
| { | ||
| private HiveApplyProjectionUtil() {} | ||
|
|
||
| public static List<ConnectorExpression> extractSupportedProjectedColumns(ConnectorExpression expression) | ||
| { | ||
| requireNonNull(expression, "expression is null"); | ||
| ImmutableList.Builder<ConnectorExpression> supportedSubExpressions = ImmutableList.builder(); | ||
| fillSupportedProjectedColumns(expression, supportedSubExpressions); | ||
| return supportedSubExpressions.build(); | ||
| } | ||
|
|
||
| private static void fillSupportedProjectedColumns(ConnectorExpression expression, ImmutableList.Builder<ConnectorExpression> supportedSubExpressions) | ||
| { | ||
| if (isPushDownSupported(expression)) { | ||
| supportedSubExpressions.add(expression); | ||
| return; | ||
| } | ||
|
|
||
| // If the whole expression is not supported, look for a partially supported projection | ||
| if (expression instanceof FieldDereference) { | ||
| fillSupportedProjectedColumns(((FieldDereference) expression).getTarget(), supportedSubExpressions); | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static boolean isPushDownSupported(ConnectorExpression expression) | ||
| { | ||
| return expression instanceof Variable || | ||
| (expression instanceof FieldDereference && isPushDownSupported(((FieldDereference) expression).getTarget())); | ||
| } | ||
|
|
||
| public static ProjectedColumnRepresentation createProjectedColumnRepresentation(ConnectorExpression expression) | ||
| { | ||
| ImmutableList.Builder<Integer> ordinals = ImmutableList.builder(); | ||
|
|
||
| Variable target; | ||
| while (true) { | ||
| if (expression instanceof Variable) { | ||
| target = (Variable) expression; | ||
| break; | ||
| } | ||
| else if (expression instanceof FieldDereference) { | ||
| FieldDereference dereference = (FieldDereference) expression; | ||
| ordinals.add(dereference.getField()); | ||
| expression = dereference.getTarget(); | ||
| } | ||
| else { | ||
| throw new IllegalArgumentException("expression is not a valid dereference chain"); | ||
| } | ||
| } | ||
|
|
||
| return new ProjectedColumnRepresentation(target, ordinals.build().reverse()); | ||
| } | ||
|
|
||
| /** | ||
| * Replace all connector expressions with variables as given by {@param expressionToVariableMappings} in a top down manner. | ||
| * i.e. if the replacement occurs for the parent, the children will not be visited. | ||
| */ | ||
| public static ConnectorExpression replaceWithNewVariables(ConnectorExpression expression, Map<ConnectorExpression, Variable> expressionToVariableMappings) | ||
| { | ||
| if (expressionToVariableMappings.containsKey(expression)) { | ||
| return expressionToVariableMappings.get(expression); | ||
| } | ||
|
|
||
| if (expression instanceof FieldDereference) { | ||
| ConnectorExpression newTarget = replaceWithNewVariables(((FieldDereference) expression).getTarget(), expressionToVariableMappings); | ||
| return new FieldDereference(expression.getType(), newTarget, ((FieldDereference) expression).getField()); | ||
| } | ||
|
|
||
| return expression; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the assignment key corresponding to the column represented by {@param projectedColumn} in the {@param assignments}, if one exists. | ||
| * The variable in the {@param projectedColumn} can itself be a representation of another projected column. For example, | ||
| * say a projected column representation has variable "x" and a dereferenceIndices=[0]. "x" can in-turn map to a projected | ||
| * column handle with base="a" and [1, 2] as dereference indices. Then the method searches for a column handle in | ||
| * {@param assignments} with base="a" and dereferenceIndices=[1, 2, 0]. | ||
| */ | ||
| public static Optional<String> find(Map<String, ColumnHandle> assignments, ProjectedColumnRepresentation projectedColumn) | ||
| { | ||
| HiveColumnHandle variableColumn = (HiveColumnHandle) assignments.get(projectedColumn.getVariable().getName()); | ||
|
|
||
| if (variableColumn == null) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| String baseColumnName = variableColumn.getBaseColumnName(); | ||
|
|
||
| List<Integer> variableColumnIndices = variableColumn.getHiveColumnProjectionInfo() | ||
| .map(HiveColumnProjectionInfo::getDereferenceIndices) | ||
| .orElse(ImmutableList.of()); | ||
|
|
||
| List<Integer> projectionIndices = ImmutableList.<Integer>builder() | ||
| .addAll(variableColumnIndices) | ||
| .addAll(projectedColumn.getDereferenceIndices()) | ||
| .build(); | ||
|
|
||
| for (Map.Entry<String, ColumnHandle> entry : assignments.entrySet()) { | ||
| HiveColumnHandle column = (HiveColumnHandle) entry.getValue(); | ||
| if (column.getBaseColumnName().equals(baseColumnName) && | ||
| column.getHiveColumnProjectionInfo() | ||
| .map(HiveColumnProjectionInfo::getDereferenceIndices) | ||
| .orElse(ImmutableList.of()) | ||
| .equals(projectionIndices)) { | ||
| return Optional.of(entry.getKey()); | ||
| } | ||
| } | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
|
||
| public static class ProjectedColumnRepresentation | ||
| { | ||
| private final Variable variable; | ||
| private final List<Integer> dereferenceIndices; | ||
|
|
||
| public ProjectedColumnRepresentation(Variable variable, List<Integer> dereferenceIndices) | ||
| { | ||
| this.variable = requireNonNull(variable, "variable is null"); | ||
| this.dereferenceIndices = ImmutableList.copyOf(requireNonNull(dereferenceIndices, "dereferenceIndices is null")); | ||
| } | ||
|
|
||
| public Variable getVariable() | ||
| { | ||
| return variable; | ||
| } | ||
|
|
||
| public List<Integer> getDereferenceIndices() | ||
| { | ||
| return dereferenceIndices; | ||
| } | ||
|
|
||
| public boolean isVariable() | ||
| { | ||
| return dereferenceIndices.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) | ||
| { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
| if ((obj == null) || (getClass() != obj.getClass())) { | ||
| return false; | ||
| } | ||
| ProjectedColumnRepresentation that = (ProjectedColumnRepresentation) obj; | ||
| return Objects.equals(variable, that.variable) && | ||
| Objects.equals(dereferenceIndices, that.dereferenceIndices); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() | ||
| { | ||
| return Objects.hash(variable, dereferenceIndices); | ||
| } | ||
| } | ||
| } | ||
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
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.
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.
Does this comment reflect new behavior? If not, move it to the commit that introduced the method.
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.
resolved the mixup: 26b83a5#diff-537e8077e259dbc890e77332fc3fa6ceR105