-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Query rewriter plugin - design only #24509
Open
ScrapCodes
wants to merge
1
commit into
prestodb:ibm
Choose a base branch
from
ScrapCodes:query_rewriter_plugin
base: ibm
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 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
49 changes: 49 additions & 0 deletions
49
presto-main/src/main/java/com/facebook/presto/dispatcher/QueryAndSessionProperties.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,49 @@ | ||
/* | ||
* 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.dispatcher; | ||
|
||
import com.facebook.presto.common.analyzer.PreparedQuery; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class QueryAndSessionProperties | ||
{ | ||
private final String query; | ||
private final Map<String, String> systemSessionProperties; | ||
private final Optional<PreparedQuery> preparedQuery; | ||
|
||
public QueryAndSessionProperties(String query, Map<String, String> systemSessionProperties, Optional<PreparedQuery> preparedQuery) | ||
{ | ||
this.query = query; | ||
this.systemSessionProperties = systemSessionProperties; | ||
this.preparedQuery = preparedQuery; | ||
} | ||
|
||
public String getQuery() | ||
{ | ||
return query; | ||
} | ||
|
||
public Map<String, String> getSystemSessionProperties() | ||
{ | ||
return systemSessionProperties; | ||
} | ||
|
||
public Optional<PreparedQuery> getPreparedQuery() | ||
{ | ||
return preparedQuery; | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
presto-main/src/main/java/com/facebook/presto/dispatcher/QueryRewriterManager.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,139 @@ | ||
/* | ||
* 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.dispatcher; | ||
|
||
import com.facebook.airlift.log.Logger; | ||
import com.facebook.presto.Session; | ||
import com.facebook.presto.common.analyzer.PreparedQuery; | ||
import com.facebook.presto.eventlistener.EventListenerManager; | ||
import com.facebook.presto.server.SessionContext; | ||
import com.facebook.presto.spi.QueryId; | ||
import com.facebook.presto.spi.analyzer.AnalyzerOptions; | ||
import com.facebook.presto.spi.analyzer.AnalyzerProvider; | ||
import com.facebook.presto.spi.analyzer.QueryPreparerProvider; | ||
import com.facebook.presto.spi.eventlistener.EventListener; | ||
import com.facebook.presto.spi.rewriter.QueryRewriterInput; | ||
import com.facebook.presto.spi.rewriter.QueryRewriterOutput; | ||
import com.facebook.presto.spi.rewriter.QueryRewriterProvider; | ||
import com.facebook.presto.spi.rewriter.QueryRewriterProviderFactory; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.inject.Inject; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static com.facebook.presto.SystemSessionProperties.IS_QUERY_REWRITER_PLUGIN_ENABLED; | ||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.lang.String.format; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* To provide a query rewriter plugin, i.e. a plugin that inputs a set of session properties and query and returns the | ||
* updated session properties and rewritten query. | ||
* 1) Provide implementation for QueryRewriterProviderFactory | ||
* 2) Provide implementation for QueryRewriterProvider | ||
* 3) Implement com.facebook.presto.spi.Plugin and provide implementation for Iterable<QueryRewriterProviderFactory> getQueryRewriterProviderFactory() | ||
* 4) Finally provide implementation of QueryRewriter, which does actual query rewrite. | ||
* For example: | ||
* {@link presto-tests/com.facebook.presto.plugin.rewriter.UpperCasingQueryRewriterPlugin} | ||
*/ | ||
public class QueryRewriterManager | ||
{ | ||
private static final Logger log = Logger.get(QueryRewriterManager.class); | ||
|
||
private final AtomicReference<Optional<QueryRewriterProviderFactory>> providerFactory = new AtomicReference<>(Optional.empty()); | ||
private final AtomicReference<Optional<QueryRewriterProvider>> provider = new AtomicReference<>(Optional.empty()); | ||
private final EventListenerManager eventListenerManager; | ||
|
||
@Inject | ||
public QueryRewriterManager(EventListenerManager eventListenerManager) | ||
{ | ||
this.eventListenerManager = requireNonNull(eventListenerManager, "eventListenerManager is null"); | ||
} | ||
|
||
public static Boolean isQueryRewriterPluginEnabled(SessionContext sessionContext) | ||
{ | ||
return Boolean.parseBoolean(sessionContext.getSystemProperties().getOrDefault(IS_QUERY_REWRITER_PLUGIN_ENABLED, "false")); | ||
} | ||
|
||
public void addQueryRewriterProviderFactory(QueryRewriterProviderFactory queryRewriterProviderFactory) | ||
{ | ||
requireNonNull(queryRewriterProviderFactory, "queryRewriterProviderFactory is null"); | ||
checkState(providerFactory.compareAndSet(Optional.empty(), Optional.of(queryRewriterProviderFactory)), | ||
format("A query rewriter factory is already registered with name %s", queryRewriterProviderFactory.getName())); | ||
} | ||
|
||
public void loadQueryRewriterProvider() | ||
{ | ||
List<EventListener> configuredEventListener = ImmutableList.of(); | ||
if (eventListenerManager.getConfiguredEventListener().isPresent()) { | ||
configuredEventListener = ImmutableList.of(eventListenerManager.getConfiguredEventListener().get()); | ||
} | ||
Optional<QueryRewriterProviderFactory> queryRewriterProviderFactory = providerFactory.get(); | ||
if (queryRewriterProviderFactory.isPresent()) { | ||
QueryRewriterProvider queryRewriterProvider = queryRewriterProviderFactory.get().create(configuredEventListener); | ||
checkState(provider.compareAndSet(Optional.empty(), Optional.of(queryRewriterProvider)), | ||
format("A query rewriter provider is already registered %s", queryRewriterProvider)); | ||
} | ||
} | ||
|
||
public Optional<QueryRewriterProvider> getQueryRewriterProvider() | ||
{ | ||
return provider.get(); | ||
} | ||
|
||
public QueryAndSessionProperties rewriteQueryAndSession( | ||
String query, | ||
Session session, | ||
AnalyzerOptions analyzerOptions, | ||
AnalyzerProvider analyzerProvider, | ||
QueryPreparerProvider queryPreparerProvider) | ||
{ | ||
QueryId queryId = session.getQueryId(); | ||
QueryAndSessionProperties rewrittenQueryAndSessionProperties = new QueryAndSessionProperties(query, session.getSystemProperties(), Optional.empty()); | ||
if (getQueryRewriterProvider().isPresent()) { | ||
QueryRewriterInput queryRewriterInput = new QueryRewriterInput.Builder() | ||
.setQuery(query) | ||
.setQueryId(queryId.getId()) | ||
.setCatalog(session.getCatalog()) | ||
.setSchema(session.getSchema()) | ||
.setPreparedStatements(session.getPreparedStatements()) | ||
.setWarningCollector(session.getWarningCollector()) | ||
.setSessionProperties(session.getSystemProperties()) | ||
.setAnalyzerOptions(analyzerOptions) | ||
.setAnalyzerProvider(analyzerProvider) | ||
.setQueryPreparer(queryPreparerProvider.getQueryPreparer()) | ||
.build(); | ||
try { | ||
QueryRewriterProvider provider = getQueryRewriterProvider().get(); | ||
QueryRewriterOutput queryRewriterOutput = provider.getQueryRewriter().rewriteSQL(queryRewriterInput); | ||
String rewrittenQuery = queryRewriterOutput.getRewrittenQuery(); | ||
// Checking if the rewritten query is parseable. | ||
PreparedQuery preparedQuery = queryPreparerProvider.getQueryPreparer() | ||
.prepareQuery(analyzerOptions, rewrittenQuery, session.getPreparedStatements(), session.getWarningCollector()); | ||
// apply updated session properties. | ||
Map<String, String> systemPropertyOverrides = queryRewriterOutput.getSessionProperties(); | ||
rewrittenQueryAndSessionProperties = new QueryAndSessionProperties(rewrittenQuery, systemPropertyOverrides, Optional.of(preparedQuery)); | ||
log.info("createQueryInternal :: QueryId [%s] - Replacing with optimized query", queryId.getId()); | ||
} | ||
catch (Exception e) { | ||
// TODO : Implement a better way to test if rewritten query is parseable. | ||
log.warn(format("rewritten query for query with id %s is discarded", session.getQueryId()), e); | ||
} | ||
} | ||
return rewrittenQueryAndSessionProperties; | ||
} | ||
} |
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
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.
This is done differently, the reason is we have SessionBuilder at dispatchManager, which can directly set the required properties.