-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Implement dereference pushdown for MongoDB connector #17710
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
Praveen2112
merged 6 commits into
trinodb:master
from
krvikash:krvikash/mongodb-dereference-pushdown
Jul 4, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a1297bc
Make variable arrangement consistent in MongoTableHandle
krvikash b55bf89
Move ProjectionPushDown util methods to plugin toolkit
Praveen2112 b80e56d
Rename BaseTestFileFormatComplexTypesPredicatePushDown
krvikash d1472d1
Verify testProjectionPushdown with projection pushdown disabled
krvikash 29a6995
Add test for dotted field and dollar prefixed field in mongo connector
krvikash 8a77330
Implement dereference pushdown for MongoDB connector
Praveen2112 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
172 changes: 172 additions & 0 deletions
172
...ino-plugin-toolkit/src/main/java/io/trino/plugin/base/projection/ApplyProjectionUtil.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,172 @@ | ||
/* | ||
* 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.trino.plugin.base.projection; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableList; | ||
import io.trino.spi.expression.Call; | ||
import io.trino.spi.expression.ConnectorExpression; | ||
import io.trino.spi.expression.Constant; | ||
import io.trino.spi.expression.FieldDereference; | ||
import io.trino.spi.expression.Variable; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
|
||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public final class ApplyProjectionUtil | ||
{ | ||
private ApplyProjectionUtil() {} | ||
|
||
public static List<ConnectorExpression> extractSupportedProjectedColumns(ConnectorExpression expression) | ||
{ | ||
return extractSupportedProjectedColumns(expression, connectorExpression -> true); | ||
} | ||
|
||
public static List<ConnectorExpression> extractSupportedProjectedColumns(ConnectorExpression expression, Predicate<ConnectorExpression> expressionPredicate) | ||
{ | ||
requireNonNull(expression, "expression is null"); | ||
ImmutableList.Builder<ConnectorExpression> supportedSubExpressions = ImmutableList.builder(); | ||
fillSupportedProjectedColumns(expression, supportedSubExpressions, expressionPredicate); | ||
return supportedSubExpressions.build(); | ||
} | ||
|
||
private static void fillSupportedProjectedColumns(ConnectorExpression expression, ImmutableList.Builder<ConnectorExpression> supportedSubExpressions, Predicate<ConnectorExpression> expressionPredicate) | ||
{ | ||
if (isPushdownSupported(expression, expressionPredicate)) { | ||
supportedSubExpressions.add(expression); | ||
return; | ||
} | ||
|
||
// If the whole expression is not supported, look for a partially supported projection | ||
for (ConnectorExpression child : expression.getChildren()) { | ||
fillSupportedProjectedColumns(child, supportedSubExpressions, expressionPredicate); | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
static boolean isPushdownSupported(ConnectorExpression expression, Predicate<ConnectorExpression> expressionPredicate) | ||
{ | ||
return expressionPredicate.test(expression) | ||
&& (expression instanceof Variable || | ||
(expression instanceof FieldDereference fieldDereference | ||
&& isPushdownSupported(fieldDereference.getTarget(), expressionPredicate))); | ||
} | ||
|
||
public static ProjectedColumnRepresentation createProjectedColumnRepresentation(ConnectorExpression expression) | ||
{ | ||
ImmutableList.Builder<Integer> ordinals = ImmutableList.builder(); | ||
|
||
Variable target; | ||
while (true) { | ||
if (expression instanceof Variable variable) { | ||
target = variable; | ||
break; | ||
} | ||
if (expression instanceof FieldDereference dereference) { | ||
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 Constant || expression instanceof Variable) { | ||
return expression; | ||
} | ||
|
||
if (expression instanceof FieldDereference fieldDereference) { | ||
ConnectorExpression newTarget = replaceWithNewVariables(fieldDereference.getTarget(), expressionToVariableMappings); | ||
return new FieldDereference(expression.getType(), newTarget, fieldDereference.getField()); | ||
} | ||
|
||
if (expression instanceof Call call) { | ||
return new Call( | ||
call.getType(), | ||
call.getFunctionName(), | ||
call.getArguments().stream() | ||
.map(argument -> replaceWithNewVariables(argument, expressionToVariableMappings)) | ||
.collect(toImmutableList())); | ||
} | ||
|
||
// We cannot skip processing for unsupported expression shapes. This may lead to variables being left in ProjectionApplicationResult | ||
// which are no longer bound. | ||
throw new UnsupportedOperationException("Unsupported expression: " + expression); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
...plugin-toolkit/src/test/java/io/trino/plugin/base/projection/TestApplyProjectionUtil.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,126 @@ | ||
/* | ||
* 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.trino.plugin.base.projection; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.trino.spi.expression.ConnectorExpression; | ||
import io.trino.spi.expression.Constant; | ||
import io.trino.spi.expression.FieldDereference; | ||
import io.trino.spi.expression.Variable; | ||
import io.trino.spi.type.RowType; | ||
import org.testng.annotations.Test; | ||
|
||
import static io.trino.plugin.base.projection.ApplyProjectionUtil.extractSupportedProjectedColumns; | ||
import static io.trino.plugin.base.projection.ApplyProjectionUtil.isPushdownSupported; | ||
import static io.trino.spi.type.IntegerType.INTEGER; | ||
import static io.trino.spi.type.RowType.field; | ||
import static io.trino.spi.type.RowType.rowType; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
public class TestApplyProjectionUtil | ||
{ | ||
private static final ConnectorExpression ROW_OF_ROW_VARIABLE = new Variable("a", rowType(field("b", rowType(field("c", INTEGER))))); | ||
private static final ConnectorExpression LEAF_DOTTED_ROW_OF_ROW_VARIABLE = new Variable("a", rowType(field("b", rowType(field("c.x", INTEGER))))); | ||
private static final ConnectorExpression MID_DOTTED_ROW_OF_ROW_VARIABLE = new Variable("a", rowType(field("b.x", rowType(field("c", INTEGER))))); | ||
|
||
private static final ConnectorExpression ONE_LEVEL_DEREFERENCE = new FieldDereference( | ||
rowType(field("c", INTEGER)), | ||
ROW_OF_ROW_VARIABLE, | ||
0); | ||
|
||
private static final ConnectorExpression TWO_LEVEL_DEREFERENCE = new FieldDereference( | ||
INTEGER, | ||
ONE_LEVEL_DEREFERENCE, | ||
0); | ||
|
||
private static final ConnectorExpression LEAF_DOTTED_ONE_LEVEL_DEREFERENCE = new FieldDereference( | ||
rowType(field("c.x", INTEGER)), | ||
LEAF_DOTTED_ROW_OF_ROW_VARIABLE, | ||
0); | ||
|
||
private static final ConnectorExpression LEAF_DOTTED_TWO_LEVEL_DEREFERENCE = new FieldDereference( | ||
INTEGER, | ||
LEAF_DOTTED_ONE_LEVEL_DEREFERENCE, | ||
0); | ||
|
||
private static final ConnectorExpression MID_DOTTED_ONE_LEVEL_DEREFERENCE = new FieldDereference( | ||
rowType(field("c.x", INTEGER)), | ||
MID_DOTTED_ROW_OF_ROW_VARIABLE, | ||
0); | ||
|
||
private static final ConnectorExpression MID_DOTTED_TWO_LEVEL_DEREFERENCE = new FieldDereference( | ||
INTEGER, | ||
MID_DOTTED_ONE_LEVEL_DEREFERENCE, | ||
0); | ||
|
||
private static final ConnectorExpression INT_VARIABLE = new Variable("a", INTEGER); | ||
private static final ConnectorExpression CONSTANT = new Constant(5, INTEGER); | ||
|
||
@Test | ||
public void testIsProjectionSupported() | ||
{ | ||
assertTrue(isPushdownSupported(ONE_LEVEL_DEREFERENCE, connectorExpression -> true)); | ||
assertTrue(isPushdownSupported(TWO_LEVEL_DEREFERENCE, connectorExpression -> true)); | ||
assertTrue(isPushdownSupported(INT_VARIABLE, connectorExpression -> true)); | ||
assertFalse(isPushdownSupported(CONSTANT, connectorExpression -> true)); | ||
|
||
assertFalse(isPushdownSupported(ONE_LEVEL_DEREFERENCE, connectorExpression -> false)); | ||
assertFalse(isPushdownSupported(TWO_LEVEL_DEREFERENCE, connectorExpression -> false)); | ||
assertFalse(isPushdownSupported(INT_VARIABLE, connectorExpression -> false)); | ||
assertFalse(isPushdownSupported(CONSTANT, connectorExpression -> false)); | ||
|
||
assertTrue(isPushdownSupported(LEAF_DOTTED_ONE_LEVEL_DEREFERENCE, this::isSupportedForPushDown)); | ||
assertFalse(isPushdownSupported(LEAF_DOTTED_TWO_LEVEL_DEREFERENCE, this::isSupportedForPushDown)); | ||
assertFalse(isPushdownSupported(MID_DOTTED_ONE_LEVEL_DEREFERENCE, this::isSupportedForPushDown)); | ||
assertFalse(isPushdownSupported(MID_DOTTED_TWO_LEVEL_DEREFERENCE, this::isSupportedForPushDown)); | ||
} | ||
|
||
@Test | ||
public void testExtractSupportedProjectionColumns() | ||
{ | ||
assertEquals(extractSupportedProjectedColumns(ONE_LEVEL_DEREFERENCE), ImmutableList.of(ONE_LEVEL_DEREFERENCE)); | ||
assertEquals(extractSupportedProjectedColumns(TWO_LEVEL_DEREFERENCE), ImmutableList.of(TWO_LEVEL_DEREFERENCE)); | ||
assertEquals(extractSupportedProjectedColumns(INT_VARIABLE), ImmutableList.of(INT_VARIABLE)); | ||
assertEquals(extractSupportedProjectedColumns(CONSTANT), ImmutableList.of()); | ||
|
||
assertEquals(extractSupportedProjectedColumns(ONE_LEVEL_DEREFERENCE, connectorExpression -> false), ImmutableList.of()); | ||
assertEquals(extractSupportedProjectedColumns(TWO_LEVEL_DEREFERENCE, connectorExpression -> false), ImmutableList.of()); | ||
assertEquals(extractSupportedProjectedColumns(INT_VARIABLE, connectorExpression -> false), ImmutableList.of()); | ||
assertEquals(extractSupportedProjectedColumns(CONSTANT, connectorExpression -> false), ImmutableList.of()); | ||
|
||
// Partial supported projection | ||
assertEquals(extractSupportedProjectedColumns(LEAF_DOTTED_ONE_LEVEL_DEREFERENCE, this::isSupportedForPushDown), ImmutableList.of(LEAF_DOTTED_ONE_LEVEL_DEREFERENCE)); | ||
assertEquals(extractSupportedProjectedColumns(LEAF_DOTTED_TWO_LEVEL_DEREFERENCE, this::isSupportedForPushDown), ImmutableList.of(LEAF_DOTTED_ONE_LEVEL_DEREFERENCE)); | ||
assertEquals(extractSupportedProjectedColumns(MID_DOTTED_ONE_LEVEL_DEREFERENCE, this::isSupportedForPushDown), ImmutableList.of(MID_DOTTED_ROW_OF_ROW_VARIABLE)); | ||
assertEquals(extractSupportedProjectedColumns(MID_DOTTED_TWO_LEVEL_DEREFERENCE, this::isSupportedForPushDown), ImmutableList.of(MID_DOTTED_ROW_OF_ROW_VARIABLE)); | ||
} | ||
|
||
/** | ||
* This method is used to simulate the behavior when the field passed in the connectorExpression might not supported for pushdown. | ||
*/ | ||
private boolean isSupportedForPushDown(ConnectorExpression connectorExpression) | ||
{ | ||
if (connectorExpression instanceof FieldDereference fieldDereference) { | ||
RowType rowType = (RowType) fieldDereference.getTarget().getType(); | ||
RowType.Field field = rowType.getFields().get(fieldDereference.getField()); | ||
String fieldName = field.getName().get(); | ||
if (fieldName.contains(".") || fieldName.contains("$")) { | ||
return false; | ||
} | ||
} | ||
return 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.
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.
No changes requested : This can be moved to a record right ? As a follow-up PR maybe