-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat: Refresh materialized view without predicates #26483
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
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 |
|---|---|---|
|
|
@@ -27,22 +27,27 @@ | |
| import com.facebook.presto.spi.relation.VariableReferenceExpression; | ||
| import com.facebook.presto.spi.security.Identity; | ||
| import com.facebook.presto.sql.analyzer.SemanticException; | ||
| import com.facebook.presto.sql.planner.ExpressionDomainTranslator; | ||
| import com.facebook.presto.sql.planner.LiteralEncoder; | ||
| import com.facebook.presto.sql.tree.ArithmeticBinaryExpression; | ||
| import com.facebook.presto.sql.tree.BooleanLiteral; | ||
| import com.facebook.presto.sql.tree.Cast; | ||
| import com.facebook.presto.sql.tree.ComparisonExpression; | ||
| import com.facebook.presto.sql.tree.Expression; | ||
| import com.facebook.presto.sql.tree.ExpressionRewriter; | ||
| import com.facebook.presto.sql.tree.ExpressionTreeRewriter; | ||
| import com.facebook.presto.sql.tree.FunctionCall; | ||
| import com.facebook.presto.sql.tree.Identifier; | ||
| import com.facebook.presto.sql.tree.IsNullPredicate; | ||
| import com.facebook.presto.sql.tree.LogicalBinaryExpression; | ||
| import com.facebook.presto.sql.tree.QualifiedName; | ||
| import com.facebook.presto.sql.tree.SymbolReference; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import com.google.common.collect.Sets; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
|
|
@@ -61,6 +66,7 @@ | |
| import static com.facebook.presto.sql.tree.ComparisonExpression.Operator.EQUAL; | ||
| import static com.facebook.presto.sql.tree.LogicalBinaryExpression.Operator.AND; | ||
| import static com.facebook.presto.sql.tree.LogicalBinaryExpression.Operator.OR; | ||
| import static com.google.common.base.Preconditions.checkState; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
|
|
||
|
|
@@ -327,4 +333,82 @@ public boolean validate(Identifier baseTableColumn, Map<Expression, Identifier> | |
| return baseToViewColumnMap.containsKey(new Cast(new FunctionCall(APPROX_SET, ImmutableList.of(baseTableColumn)), VARBINARY)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generate WHERE predicates for missing partitions from MaterializedDataPredicates. | ||
| * Used for auto-refresh of materialized views without explicit WHERE clause. | ||
| */ | ||
| public static Map<SchemaTableName, Expression> generatePredicatesForMissingPartitions( | ||
| Map<SchemaTableName, MaterializedViewStatus.MaterializedDataPredicates> missingPartitionsPerTable, | ||
| Metadata metadata) | ||
| { | ||
| Map<SchemaTableName, Expression> predicates = new HashMap<>(); | ||
|
|
||
| for (Map.Entry<SchemaTableName, MaterializedViewStatus.MaterializedDataPredicates> entry : | ||
| missingPartitionsPerTable.entrySet()) { | ||
| SchemaTableName tableName = entry.getKey(); | ||
| MaterializedViewStatus.MaterializedDataPredicates missingPartitions = entry.getValue(); | ||
|
|
||
| Expression predicate = convertMaterializedDataPredicatesToExpression(missingPartitions, metadata); | ||
|
|
||
| predicates.put(tableName, predicate); | ||
| } | ||
|
|
||
| return predicates; | ||
| } | ||
|
|
||
| /** | ||
| * Convert MaterializedDataPredicates to a SQL Expression tree. | ||
| * Builds an OR expression of partition predicates, where each partition is an AND expression of column filters. | ||
| */ | ||
| public static Expression convertMaterializedDataPredicatesToExpression( | ||
| MaterializedViewStatus.MaterializedDataPredicates predicates, | ||
| Metadata metadata) | ||
| { | ||
| List<String> columnNames = predicates.getColumnNames(); | ||
| List<TupleDomain<String>> predicateDisjuncts = predicates.getPredicateDisjuncts(); | ||
|
|
||
| ExpressionDomainTranslator translator = new ExpressionDomainTranslator( | ||
| new LiteralEncoder(metadata.getBlockEncodingSerde())); | ||
|
|
||
| List<Expression> disjuncts = new ArrayList<>(); | ||
|
|
||
| for (TupleDomain<String> tupleDomain : predicateDisjuncts) { | ||
| checkState(!tupleDomain.isAll(), "TupleDomain.isAll() should not appear in MaterializedDataPredicates"); | ||
| if (tupleDomain.isNone()) { | ||
| continue; | ||
| } | ||
|
|
||
| Expression conjunction = translator.toPredicate(tupleDomain); | ||
| conjunction = convertSymbolReferencesToIdentifiers(conjunction); | ||
|
|
||
| disjuncts.add(conjunction); | ||
| } | ||
|
|
||
| if (disjuncts.isEmpty()) { | ||
| throw new IllegalStateException("No predicates generated for missing partitions"); | ||
| } | ||
|
|
||
| if (disjuncts.size() == 1) { | ||
| return disjuncts.get(0); | ||
| } | ||
| else { | ||
| return disjuncts.stream() | ||
|
Contributor
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. Nit: I would use
Contributor
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. Or perhaps |
||
| .reduce((left, right) -> new LogicalBinaryExpression( | ||
| LogicalBinaryExpression.Operator.OR, left, right)) | ||
| .get(); | ||
| } | ||
| } | ||
|
|
||
| private static Expression convertSymbolReferencesToIdentifiers(Expression expression) | ||
| { | ||
| return ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>() | ||
| { | ||
| @Override | ||
| public Expression rewriteSymbolReference(SymbolReference node, Void context, ExpressionTreeRewriter<Void> treeRewriter) | ||
| { | ||
| return new Identifier(node.getName()); | ||
| } | ||
| }, 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
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.
nitpick (testing): Nitpick: Consider using try-finally for cleanup to ensure resources are dropped even if assertions fail.
Wrap the cleanup statements in a try-finally block to ensure resources are always released, even if an assertion fails.