-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add query optimization using materialized view
Adding query optimization using materialized view. It consumes existing candidate extractor and query optimizations utilities to optimize the original queries if possible. This optimization is controlled by a hidden configuration, which is disabled by default.
- Loading branch information
Showing
7 changed files
with
226 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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
83 changes: 83 additions & 0 deletions
83
...in/src/main/java/com/facebook/presto/sql/rewrite/MaterializedViewOptimizationRewrite.java
This file contains 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,83 @@ | ||
/* | ||
* 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 com.facebook.presto.sql.rewrite; | ||
|
||
import com.facebook.presto.Session; | ||
import com.facebook.presto.SystemSessionProperties; | ||
import com.facebook.presto.metadata.Metadata; | ||
import com.facebook.presto.security.AccessControl; | ||
import com.facebook.presto.spi.WarningCollector; | ||
import com.facebook.presto.sql.analyzer.QueryExplainer; | ||
import com.facebook.presto.sql.parser.SqlParser; | ||
import com.facebook.presto.sql.tree.AstVisitor; | ||
import com.facebook.presto.sql.tree.Expression; | ||
import com.facebook.presto.sql.tree.Node; | ||
import com.facebook.presto.sql.tree.Query; | ||
import com.facebook.presto.sql.tree.Statement; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static com.facebook.presto.sql.rewrite.MaterializedViewOptimizationRewriteUtils.optimizeQueryUsingMaterializedView; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class MaterializedViewOptimizationRewrite | ||
implements StatementRewrite.Rewrite | ||
{ | ||
@Override | ||
public Statement rewrite( | ||
Session session, | ||
Metadata metadata, | ||
SqlParser parser, | ||
Optional<QueryExplainer> queryExplainer, | ||
Statement node, | ||
List<Expression> parameters, | ||
AccessControl accessControl, | ||
WarningCollector warningCollector) | ||
{ | ||
return (Statement) new MaterializedViewOptimizationRewrite.Visitor(metadata, session, parser).process(node, null); | ||
} | ||
|
||
private static final class Visitor | ||
extends AstVisitor<Node, Void> | ||
{ | ||
private final Metadata metadata; | ||
private final Session session; | ||
private final SqlParser sqlParser; | ||
|
||
public Visitor( | ||
Metadata metadata, | ||
Session session, | ||
SqlParser parser) | ||
{ | ||
this.metadata = requireNonNull(metadata, "metadata is null"); | ||
this.session = requireNonNull(session, "session is null"); | ||
this.sqlParser = requireNonNull(parser, "queryPreparer is null"); | ||
} | ||
|
||
@Override | ||
protected Node visitNode(Node node, Void context) | ||
{ | ||
return node; | ||
} | ||
|
||
protected Node visitQuery(Query query, Void context) | ||
{ | ||
if (SystemSessionProperties.isQueryOptimizationWithMaterializedViewEnabled(session)) { | ||
return optimizeQueryUsingMaterializedView(metadata, session, sqlParser, query).orElse(query); | ||
} | ||
return query; | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...c/main/java/com/facebook/presto/sql/rewrite/MaterializedViewOptimizationRewriteUtils.java
This file contains 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,74 @@ | ||
/* | ||
* 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 com.facebook.presto.sql.rewrite; | ||
|
||
import com.facebook.presto.Session; | ||
import com.facebook.presto.common.QualifiedObjectName; | ||
import com.facebook.presto.metadata.Metadata; | ||
import com.facebook.presto.spi.ConnectorMaterializedViewDefinition; | ||
import com.facebook.presto.sql.analyzer.MaterializedViewCandidateExtractor; | ||
import com.facebook.presto.sql.analyzer.MaterializedViewQueryOptimizer; | ||
import com.facebook.presto.sql.parser.SqlParser; | ||
import com.facebook.presto.sql.tree.QualifiedName; | ||
import com.facebook.presto.sql.tree.Query; | ||
import com.facebook.presto.sql.tree.Table; | ||
import com.google.common.collect.ImmutableMap; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
public class MaterializedViewOptimizationRewriteUtils | ||
{ | ||
private MaterializedViewOptimizationRewriteUtils() {} | ||
|
||
public static Optional<Query> optimizeQueryUsingMaterializedView( | ||
Metadata metadata, | ||
Session session, | ||
SqlParser sqlParser, | ||
Query node) | ||
{ | ||
Map<QualifiedObjectName, List<QualifiedObjectName>> baseTableToMaterializedViews = getBaseTableToMaterializedViews(); | ||
MaterializedViewCandidateExtractor materializedViewCandidateExtractor = new MaterializedViewCandidateExtractor(session, baseTableToMaterializedViews); | ||
materializedViewCandidateExtractor.process(node); | ||
Set<QualifiedObjectName> materializedViewCandidates = materializedViewCandidateExtractor.getMaterializedViewCandidates(); | ||
if (materializedViewCandidates.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
// TODO: Select the most compatible and efficient materialized view for query rewrite optimization https://github.com/prestodb/presto/issues/16431 | ||
Query optimizedQuery = getQueryWithMaterializedViewOptimization(metadata, session, sqlParser, node, materializedViewCandidates.iterator().next()); | ||
return Optional.of(optimizedQuery); | ||
} | ||
|
||
private static Query getQueryWithMaterializedViewOptimization( | ||
Metadata metadata, | ||
Session session, | ||
SqlParser sqlParser, | ||
Query statement, | ||
QualifiedObjectName materializedViewQualifiedObjectName) | ||
{ | ||
ConnectorMaterializedViewDefinition materializedView = metadata.getMaterializedView(session, materializedViewQualifiedObjectName).get(); | ||
Table materializedViewTable = new Table(QualifiedName.of(materializedView.getTable())); | ||
|
||
Query materializedViewDefinition = (Query) sqlParser.createStatement(materializedView.getOriginalSql()); | ||
return (Query) new MaterializedViewQueryOptimizer(materializedViewTable, materializedViewDefinition).rewrite(statement); | ||
} | ||
|
||
// TODO: The mapping should be fetched from metastore https://github.com/prestodb/presto/issues/16430 | ||
private static Map<QualifiedObjectName, List<QualifiedObjectName>> getBaseTableToMaterializedViews() | ||
{ | ||
return ImmutableMap.of(); | ||
} | ||
} |
This file contains 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 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