-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Enable subfield pushdown for map_subset function #25394
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
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
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 |
|---|---|---|
|
|
@@ -18,9 +18,11 @@ | |
| import com.facebook.presto.common.Subfield; | ||
| import com.facebook.presto.common.Subfield.NestedField; | ||
| import com.facebook.presto.common.Subfield.PathElement; | ||
| import com.facebook.presto.common.block.Block; | ||
| import com.facebook.presto.common.type.ArrayType; | ||
| import com.facebook.presto.common.type.MapType; | ||
| import com.facebook.presto.common.type.RowType; | ||
| import com.facebook.presto.common.type.Type; | ||
| import com.facebook.presto.expressions.DefaultRowExpressionTraversalVisitor; | ||
| import com.facebook.presto.metadata.FunctionAndTypeManager; | ||
| import com.facebook.presto.metadata.Metadata; | ||
|
|
@@ -89,10 +91,12 @@ | |
| import java.util.stream.IntStream; | ||
|
|
||
| import static com.facebook.presto.SystemSessionProperties.isLegacyUnnest; | ||
| import static com.facebook.presto.SystemSessionProperties.isPushSubfieldsForMapSubsetEnabled; | ||
| import static com.facebook.presto.SystemSessionProperties.isPushdownSubfieldsEnabled; | ||
| import static com.facebook.presto.SystemSessionProperties.isPushdownSubfieldsFromArrayLambdasEnabled; | ||
| import static com.facebook.presto.common.Subfield.allSubscripts; | ||
| import static com.facebook.presto.common.Subfield.noSubfield; | ||
| import static com.facebook.presto.common.type.TypeUtils.readNativeValue; | ||
| import static com.facebook.presto.common.type.Varchars.isVarcharType; | ||
| import static com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.JAVA_BUILTIN_NAMESPACE; | ||
| import static com.facebook.presto.spi.relation.SpecialFormExpression.Form.DEREFERENCE; | ||
|
|
@@ -151,7 +155,7 @@ private static class Rewriter | |
| { | ||
| private final Session session; | ||
| private final Metadata metadata; | ||
| private final StandardFunctionResolution functionResolution; | ||
| private final FunctionResolution functionResolution; | ||
| private final ExpressionOptimizer expressionOptimizer; | ||
| private final SubfieldExtractor subfieldExtractor; | ||
| private static final QualifiedObjectName ARBITRARY_AGGREGATE_FUNCTION = QualifiedObjectName.valueOf(JAVA_BUILTIN_NAMESPACE, "arbitrary"); | ||
|
|
@@ -169,7 +173,7 @@ public Rewriter(Session session, Metadata metadata, ExpressionOptimizerProvider | |
| expressionOptimizer, | ||
| session.toConnectorSession(), | ||
| metadata.getFunctionAndTypeManager(), | ||
| isPushdownSubfieldsFromArrayLambdasEnabled(session)); | ||
| session); | ||
| } | ||
|
|
||
| public boolean isPlanChanged() | ||
|
|
@@ -307,9 +311,9 @@ public PlanNode visitProject(ProjectNode node, RewriteContext<Context> context) | |
| continue; | ||
| } | ||
|
|
||
| Optional<Subfield> subfield = toSubfield(expression, functionResolution, expressionOptimizer, session.toConnectorSession(), metadata.getFunctionAndTypeManager()); | ||
| Optional<List<Subfield>> subfield = toSubfield(expression, functionResolution, expressionOptimizer, session.toConnectorSession(), metadata.getFunctionAndTypeManager(), isPushSubfieldsForMapSubsetEnabled(session)); | ||
| if (subfield.isPresent()) { | ||
| context.get().addAssignment(variable, subfield.get()); | ||
| subfield.get().forEach(element -> context.get().addAssignment(variable, element)); | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -570,17 +574,18 @@ private static String getColumnName(Session session, Metadata metadata, TableHan | |
| return metadata.getColumnMetadata(session, tableHandle, columnHandle).getName(); | ||
| } | ||
|
|
||
| private static Optional<Subfield> toSubfield( | ||
| private static Optional<List<Subfield>> toSubfield( | ||
|
Contributor
Author
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. map_subset can return a list of subfields |
||
| RowExpression expression, | ||
| StandardFunctionResolution functionResolution, | ||
| FunctionResolution functionResolution, | ||
| ExpressionOptimizer expressionOptimizer, | ||
| ConnectorSession connectorSession, | ||
| FunctionAndTypeManager functionAndTypeManager) | ||
| FunctionAndTypeManager functionAndTypeManager, | ||
| boolean isPushdownSubfieldsForMapSubsetEnabled) | ||
| { | ||
| ImmutableList.Builder<Subfield.PathElement> elements = ImmutableList.builder(); | ||
| while (true) { | ||
| if (expression instanceof VariableReferenceExpression) { | ||
| return Optional.of(new Subfield(((VariableReferenceExpression) expression).getName(), elements.build().reverse())); | ||
| return Optional.of(ImmutableList.of(new Subfield(((VariableReferenceExpression) expression).getName(), elements.build().reverse()))); | ||
| } | ||
| if (expression instanceof CallExpression) { | ||
| ComplexTypeFunctionDescriptor functionDescriptor = functionAndTypeManager.getFunctionMetadata(((CallExpression) expression).getFunctionHandle()).getDescriptor(); | ||
|
|
@@ -650,6 +655,29 @@ private static Optional<Subfield> toSubfield( | |
| } | ||
| return Optional.empty(); | ||
| } | ||
| // map_subset(feature, constant_array) is only accessing fields specified in feature map. | ||
| // For example map_subset(feature, array[1, 2]) is equivalent to calling element_at(feature, 1) and element_at(feature, 2) for subfield extraction | ||
| if (isPushdownSubfieldsForMapSubsetEnabled && expression instanceof CallExpression && isMapSubSetWithConstantArray((CallExpression) expression, functionResolution)) { | ||
| CallExpression call = (CallExpression) expression; | ||
| ConstantExpression constantArray = (ConstantExpression) call.getArguments().get(1); | ||
| checkState(constantArray.getValue() instanceof Block && constantArray.getType() instanceof ArrayType); | ||
| Block arrayValue = (Block) constantArray.getValue(); | ||
| Type arrayElementType = ((ArrayType) constantArray.getType()).getElementType(); | ||
| ImmutableList.Builder<Subfield> arguments = ImmutableList.builder(); | ||
| for (int i = 0; i < arrayValue.getPositionCount(); ++i) { | ||
| Object mapKey = readNativeValue(arrayElementType, arrayValue, i); | ||
| if (mapKey == null) { | ||
| return Optional.empty(); | ||
| } | ||
| if (mapKey instanceof Number) { | ||
| arguments.add(new Subfield(((VariableReferenceExpression) call.getArguments().get(0)).getName(), ImmutableList.of(new Subfield.LongSubscript(((Number) mapKey).longValue())))); | ||
| } | ||
| if (isVarcharType(arrayElementType)) { | ||
| arguments.add(new Subfield(((VariableReferenceExpression) call.getArguments().get(0)).getName(), ImmutableList.of(new Subfield.StringSubscript(((Slice) mapKey).toStringUtf8())))); | ||
| } | ||
| } | ||
| return Optional.of(arguments.build()); | ||
| } | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
|
@@ -663,38 +691,41 @@ private static NestedField nestedField(String name) | |
| private static final class SubfieldExtractor | ||
| extends DefaultRowExpressionTraversalVisitor<Context> | ||
| { | ||
| private final StandardFunctionResolution functionResolution; | ||
| private final FunctionResolution functionResolution; | ||
| private final ExpressionOptimizer expressionOptimizer; | ||
| private final ConnectorSession connectorSession; | ||
| private final FunctionAndTypeManager functionAndTypeManager; | ||
| private final boolean isPushDownSubfieldsFromLambdasEnabled; | ||
| private final boolean isPushdownSubfieldsForMapSubsetEnabled; | ||
|
|
||
| private SubfieldExtractor( | ||
| StandardFunctionResolution functionResolution, | ||
| FunctionResolution functionResolution, | ||
| ExpressionOptimizer expressionOptimizer, | ||
| ConnectorSession connectorSession, | ||
| FunctionAndTypeManager functionAndTypeManager, | ||
| boolean isPushDownSubfieldsFromLambdasEnabled) | ||
| Session session) | ||
| { | ||
| this.functionResolution = requireNonNull(functionResolution, "functionResolution is null"); | ||
| this.expressionOptimizer = requireNonNull(expressionOptimizer, "expressionOptimizer is null"); | ||
| this.connectorSession = connectorSession; | ||
| this.functionAndTypeManager = requireNonNull(functionAndTypeManager, "functionAndTypeManager is null"); | ||
| this.isPushDownSubfieldsFromLambdasEnabled = isPushDownSubfieldsFromLambdasEnabled; | ||
| requireNonNull(session); | ||
| this.isPushDownSubfieldsFromLambdasEnabled = isPushdownSubfieldsFromArrayLambdasEnabled(session); | ||
| this.isPushdownSubfieldsForMapSubsetEnabled = isPushSubfieldsForMapSubsetEnabled(session); | ||
| } | ||
|
|
||
| @Override | ||
| public Void visitCall(CallExpression call, Context context) | ||
| { | ||
| ComplexTypeFunctionDescriptor functionDescriptor = functionAndTypeManager.getFunctionMetadata(call.getFunctionHandle()).getDescriptor(); | ||
| if (isSubscriptOrElementAtFunction(call, functionResolution, functionAndTypeManager)) { | ||
| Optional<Subfield> subfield = toSubfield(call, functionResolution, expressionOptimizer, connectorSession, functionAndTypeManager); | ||
| if (isSubscriptOrElementAtFunction(call, functionResolution, functionAndTypeManager) || isMapSubSetWithConstantArray(call, functionResolution)) { | ||
| Optional<List<Subfield>> subfield = toSubfield(call, functionResolution, expressionOptimizer, connectorSession, functionAndTypeManager, isPushdownSubfieldsForMapSubsetEnabled); | ||
| if (subfield.isPresent()) { | ||
| if (context.isPruningLambdaSubfieldsPossible()) { | ||
| addRequiredLambdaSubfields(context, subfield.get()); | ||
| subfield.get().forEach(item -> addRequiredLambdaSubfields(context, item)); | ||
| } | ||
| else { | ||
| context.subfields.add(subfield.get()); | ||
| context.subfields.addAll(subfield.get()); | ||
| } | ||
| } | ||
| else { | ||
|
|
@@ -847,14 +878,14 @@ else if (specialForm.getForm() != DEREFERENCE) { | |
| return null; | ||
| } | ||
|
|
||
| Optional<Subfield> subfield = toSubfield(specialForm, functionResolution, expressionOptimizer, connectorSession, functionAndTypeManager); | ||
| Optional<List<Subfield>> subfield = toSubfield(specialForm, functionResolution, expressionOptimizer, connectorSession, functionAndTypeManager, isPushdownSubfieldsForMapSubsetEnabled); | ||
|
|
||
| if (subfield.isPresent()) { | ||
| if (context.isPruningLambdaSubfieldsPossible()) { | ||
| addRequiredLambdaSubfields(context, subfield.get()); | ||
| subfield.get().forEach(item -> addRequiredLambdaSubfields(context, item)); | ||
| } | ||
| else { | ||
| context.subfields.add(subfield.get()); | ||
| context.subfields.addAll(subfield.get()); | ||
| } | ||
| } | ||
| else { | ||
|
|
@@ -887,7 +918,7 @@ private void addRequiredLambdaSubfields(Context context, Subfield input) | |
| public Void visitVariableReference(VariableReferenceExpression reference, Context context) | ||
| { | ||
| if (context.isPruningLambdaSubfieldsPossible()) { | ||
| addRequiredLambdaSubfields(context, toSubfield(reference, functionResolution, expressionOptimizer, connectorSession, functionAndTypeManager).get()); | ||
| toSubfield(reference, functionResolution, expressionOptimizer, connectorSession, functionAndTypeManager, isPushdownSubfieldsForMapSubsetEnabled).get().forEach(item -> addRequiredLambdaSubfields(context, item)); | ||
| return null; | ||
| } | ||
| context.variables.add(reference); | ||
|
|
@@ -978,4 +1009,11 @@ private static boolean isSubscriptOrElementAtFunction(CallExpression expression, | |
| functionAndTypeManager.getFunctionAndTypeResolver().getFunctionMetadata(expression.getFunctionHandle()).getName() | ||
| .equals(functionAndTypeManager.getFunctionAndTypeResolver().qualifyObjectName(QualifiedName.of("element_at"))); | ||
| } | ||
|
|
||
| private static boolean isMapSubSetWithConstantArray(CallExpression expression, FunctionResolution functionResolution) | ||
| { | ||
| return functionResolution.isMapSubSetFunction(expression.getFunctionHandle()) | ||
| && expression.getArguments().get(0) instanceof VariableReferenceExpression | ||
| && expression.getArguments().get(1) instanceof ConstantExpression; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
can you add a test where the specified keys to subset are not also the index of that key? Also for non-numeric map keys.
Uh oh!
There was an error while loading. Please reload this page.
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 do you mean by not also the index of that key? Do you mean the type of index in array is different from map key?
If map key is of integer type, and element in array is of bigint type, the map will be wrapped with a cast and thie pushdown field will not work (and this needs to be resolved with this PR #25395)
If element type of array is not compatible with map key type, for example integer vs. varchar, query will fail during during parsing
Added tests for varchar keys too