-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Speed up IN list types analysis #11907
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
6 commits
Select commit
Hold shift + click to select a range
e62df96
Pass TypeAnalyzer singleton in LocalQueryRunner
wendigo 98c881b
Speed up coercion to a single type for IN predicates
wendigo 16e9ad9
Generate unique query id in the tests
wendigo 3963111
Cache resolved expression types in TypeAnalyzer
wendigo 49d2449
Remove expensive stream collection for known list size
wendigo d94e061
Do not create a new instance of InPredicate if it doesn't simplify
sopel39 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,10 +13,12 @@ | |
| */ | ||
| package io.trino.sql.analyzer; | ||
|
|
||
| import com.google.common.collect.ArrayListMultimap; | ||
| import com.google.common.collect.HashMultimap; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.collect.Iterables; | ||
| import com.google.common.collect.ListMultimap; | ||
| import com.google.common.collect.Multimap; | ||
| import com.google.common.collect.Streams; | ||
| import io.trino.Session; | ||
|
|
@@ -150,6 +152,7 @@ | |
| import static com.google.common.base.Preconditions.checkState; | ||
| import static com.google.common.base.Verify.verify; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
| import static com.google.common.collect.ImmutableSet.toImmutableSet; | ||
| import static com.google.common.collect.Iterables.getOnlyElement; | ||
| import static io.trino.spi.StandardErrorCode.AMBIGUOUS_NAME; | ||
|
|
@@ -297,7 +300,7 @@ public class ExpressionAnalyzer | |
| private final Function<Node, ResolvedWindow> getResolvedWindow; | ||
| private final List<Field> sourceFields = new ArrayList<>(); | ||
|
|
||
| private ExpressionAnalyzer( | ||
| public ExpressionAnalyzer( | ||
| PlannerContext plannerContext, | ||
| AccessControl accessControl, | ||
| StatementAnalyzerFactory statementAnalyzerFactory, | ||
|
|
@@ -2218,19 +2221,16 @@ protected Type visitInPredicate(InPredicate node, StackableAstVisitorContext<Con | |
| }); | ||
| } | ||
|
|
||
| Type declaredValueType = process(value, context); | ||
|
|
||
| if (valueList instanceof InListExpression) { | ||
| process(valueList, context); | ||
| InListExpression inListExpression = (InListExpression) valueList; | ||
|
|
||
| coerceToSingleType(context, | ||
| Type type = coerceToSingleType(context, | ||
| "IN value and list items must be the same type: %s", | ||
| ImmutableList.<Expression>builder().add(value).addAll(inListExpression.getValues()).build()); | ||
| setExpressionType(inListExpression, type); | ||
| } | ||
| else if (valueList instanceof SubqueryExpression) { | ||
| subqueryInPredicates.add(NodeRef.of(node)); | ||
| analyzePredicateWithSubquery(node, declaredValueType, (SubqueryExpression) valueList, context); | ||
| analyzePredicateWithSubquery(node, process(value, context), (SubqueryExpression) valueList, context); | ||
| } | ||
| else { | ||
| throw new IllegalArgumentException("Unexpected value list type for InPredicate: " + node.getValueList().getClass().getName()); | ||
|
|
@@ -2239,15 +2239,6 @@ else if (valueList instanceof SubqueryExpression) { | |
| return setExpressionType(node, BOOLEAN); | ||
| } | ||
|
|
||
| @Override | ||
| protected Type visitInListExpression(InListExpression node, StackableAstVisitorContext<Context> context) | ||
| { | ||
| Type type = coerceToSingleType(context, "All IN list values must be the same type: %s", node.getValues()); | ||
|
|
||
| setExpressionType(node, type); | ||
| return type; // TODO: this really should a be relation type | ||
| } | ||
|
|
||
| @Override | ||
| protected Type visitSubqueryExpression(SubqueryExpression node, StackableAstVisitorContext<Context> context) | ||
| { | ||
|
|
@@ -2568,22 +2559,32 @@ private Type coerceToSingleType(StackableAstVisitorContext<Context> context, Str | |
| { | ||
| // determine super type | ||
| Type superType = UNKNOWN; | ||
|
|
||
| ListMultimap<Type, Expression> typeExpressions = ArrayListMultimap.create(); | ||
| for (Expression expression : expressions) { | ||
| Optional<Type> newSuperType = typeCoercion.getCommonSuperType(superType, process(expression, context)); | ||
| typeExpressions.put(process(expression, context), expression); | ||
| } | ||
|
|
||
| // We need an explicit copy to avoid ConcurrentModificationException | ||
| Set<Type> types = typeExpressions.keySet(); | ||
|
Comment on lines
2568
to
2569
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. i don't see explicit copy here. |
||
|
|
||
| for (Type type : types) { | ||
| Optional<Type> newSuperType = typeCoercion.getCommonSuperType(superType, type); | ||
| if (newSuperType.isEmpty()) { | ||
| throw semanticException(TYPE_MISMATCH, expression, message, superType); | ||
| throw semanticException(TYPE_MISMATCH, typeExpressions.get(type).get(0), message, superType); | ||
| } | ||
| superType = newSuperType.get(); | ||
| } | ||
|
|
||
| // verify all expressions can be coerced to the superType | ||
|
sopel39 marked this conversation as resolved.
Outdated
|
||
| for (Expression expression : expressions) { | ||
| Type type = process(expression, context); | ||
| for (Type type : types) { | ||
| List<Expression> coercionCandidates = typeExpressions.get(type); | ||
|
|
||
| if (!type.equals(superType)) { | ||
| if (!typeCoercion.canCoerce(type, superType)) { | ||
| throw semanticException(TYPE_MISMATCH, expression, message, superType); | ||
| throw semanticException(TYPE_MISMATCH, coercionCandidates.get(0), message, superType); | ||
| } | ||
| addOrReplaceExpressionCoercion(expression, type, superType); | ||
| addOrReplaceExpressionsCoercion(coercionCandidates, type, superType); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2592,13 +2593,20 @@ private Type coerceToSingleType(StackableAstVisitorContext<Context> context, Str | |
|
|
||
| private void addOrReplaceExpressionCoercion(Expression expression, Type type, Type superType) | ||
| { | ||
| NodeRef<Expression> ref = NodeRef.of(expression); | ||
| expressionCoercions.put(ref, superType); | ||
|
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.
|
||
| addOrReplaceExpressionsCoercion(List.of(expression), type, superType); | ||
|
wendigo marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private void addOrReplaceExpressionsCoercion(List<Expression> expressions, Type type, Type superType) | ||
| { | ||
| Map<NodeRef<Expression>, Type> expressionRefTypes = expressions.stream() | ||
| .collect(toImmutableMap(NodeRef::of, expression -> superType)); | ||
|
|
||
| expressionCoercions.putAll(expressionRefTypes); | ||
| if (typeCoercion.isTypeOnlyCoercion(type, superType)) { | ||
| typeOnlyCoercions.add(ref); | ||
| typeOnlyCoercions.addAll(expressionRefTypes.keySet()); | ||
| } | ||
| else { | ||
| typeOnlyCoercions.remove(ref); | ||
| expressionRefTypes.keySet().forEach(typeOnlyCoercions::remove); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -2773,8 +2781,13 @@ public static ExpressionAnalysis analyzeExpressions( | |
| WarningCollector warningCollector, | ||
| QueryType queryType) | ||
| { | ||
| Analysis analysis = new Analysis(null, parameters, queryType); | ||
| ExpressionAnalyzer analyzer = new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, analysis, session, types, warningCollector); | ||
| return analyzeExpressions( | ||
| new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, new Analysis(null, parameters, queryType), session, types, warningCollector), | ||
| expressions); | ||
| } | ||
|
|
||
| public static ExpressionAnalysis analyzeExpressions(ExpressionAnalyzer analyzer, Iterable<Expression> expressions) | ||
| { | ||
| for (Expression expression : expressions) { | ||
| analyzer.analyze( | ||
| expression, | ||
|
|
||
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
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.
remove obsolete comment