ready
Presto has two branches: prestodb and prestosql. The difference between the two found an article , you can understand.
I chose prestodb,git clone https://github.com/prestodb/presto.git
When I was writing a blog, the most recent commit
cba87b4f111983b5483ab2ecf580573ac0afa595
, this submission completes the Materialized Views, see https://github.com/prestosql/presto/commit/88116a4a3fda92f0caa725d9b1a83e9b22f7dcf4 for details
Analyze the master code directly and find the presto-cli modulecom.facebook.presto.cli.Presto
Double-click shift in idea to quickly search
analysis
After opening, the code is very small, mainly for the operation of the console: create, determine whether you need to display help or display version information, and then to the real run, the normal return is 0, otherwise it is 1.
package com.facebook.presto.cli;
import static io.airlift.airline.SingleCommand.singleCommand;
public final class Presto
{
private Presto() {}
public static void main(String[] args)
{
Console console = singleCommand(Console.class).parse(args);
if (console.helpOption.showHelpIfRequested() ||
console.versionOption.showVersionIfRequested()) {
return;
}
System.exit(console.run() ? 0 : 1);
}
}
The console.run() method is not difficult to understand
- First, make some judgments: is the execute parameter submitted? Or was it submitted by file? And do the corresponding treatment.
- If the client terminates, the query also terminates.
// abort any running query if the CLI is terminated
AtomicBoolean exiting = new AtomicBoolean();
ThreadInterruptor interruptor = new ThreadInterruptor();
CountDownLatch exited = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
exiting.set(true);
interruptor.interrupt();
awaitUninterruptibly(exited, EXIT_DELAY.toMillis(), MILLISECONDS);
}));
- Pass the query in and execute the command.
Enter executeCommand and find that it splits the query into one by one and gives it to the process (check StatementSplitter and find that it is split by comma)
private static boolean executeCommand(QueryRunner queryRunner, String query, OutputFormat outputFormat, boolean ignoreErrors)
{
boolean success = true;
StatementSplitter splitter = new StatementSplitter(query);
for (Statement split : splitter.getCompleteStatements()) {
if (!isEmptyStatement(split.statement())) {
if (!process(queryRunner, split.statement(), outputFormat, () -> {}, false)) {
if (!ignoreErrors) {
return false;
}
success = false;
}
}
}
if (!isEmptyStatement(splitter.getPartialStatement())) {
System.err.println("Non-terminated statement: " + splitter.getPartialStatement());
return false;
}
return success;
}
The process calling queryRunner.startQuery(finalSql) is
actually the startQuery level calling startInternalQuery and
finally getting StatementClientV1
public final class StatementClientFactory
{
private StatementClientFactory() {}
public static StatementClient newStatementClient(OkHttpClient httpClient, ClientSession session, String query)
{
return new StatementClientV1(httpClient, session, query);
}
}
Construct the query request in the construction method of StatementClientV1, and
then enter the path where you can see the url.
It is not difficult to locate com/facebook/presto/server/protocol/QueuedStatementResource.java:161
where you have responded to the request and completed the entire submitted request.