Search in sources :

Example 1 with QueryRewriteException

use of io.prestosql.verifier.QueryRewriter.QueryRewriteException in project hetu-core by openlookeng.

the class VerifyCommand method rewriteQueries.

@VisibleForTesting
static List<QueryPair> rewriteQueries(SqlParser parser, VerifierConfig config, List<QueryPair> queries) {
    QueryRewriter testRewriter = new QueryRewriter(parser, config.getTestGateway(), config.getShadowTestTablePrefix(), Optional.ofNullable(config.getTestCatalogOverride()), Optional.ofNullable(config.getTestSchemaOverride()), Optional.ofNullable(config.getTestUsernameOverride()), Optional.ofNullable(config.getTestPasswordOverride()), config.getDoublePrecision(), config.getTestTimeout());
    QueryRewriter controlRewriter = new QueryRewriter(parser, config.getControlGateway(), config.getShadowControlTablePrefix(), Optional.ofNullable(config.getControlCatalogOverride()), Optional.ofNullable(config.getControlSchemaOverride()), Optional.ofNullable(config.getControlUsernameOverride()), Optional.ofNullable(config.getControlPasswordOverride()), config.getDoublePrecision(), config.getControlTimeout());
    LOG.info("Rewriting %s queries using %s threads", queries.size(), config.getThreadCount());
    ExecutorService executor = newFixedThreadPool(config.getThreadCount());
    CompletionService<Optional<QueryPair>> completionService = new ExecutorCompletionService<>(executor);
    List<QueryPair> rewritten = new ArrayList<>();
    for (QueryPair pair : queries) {
        completionService.submit(() -> {
            try {
                return Optional.of(new QueryPair(pair.getSuite(), pair.getName(), testRewriter.shadowQuery(pair.getTest()), controlRewriter.shadowQuery(pair.getControl())));
            } catch (QueryRewriteException | SQLException e) {
                if (!config.isQuiet()) {
                    LOG.warn(e, "Failed to rewrite %s for shadowing. Skipping.", pair.getName());
                }
                return Optional.empty();
            }
        });
    }
    executor.shutdown();
    try {
        Stopwatch stopwatch = Stopwatch.createStarted();
        for (int n = 1; n <= queries.size(); n++) {
            completionService.take().get().ifPresent(rewritten::add);
            if (!config.isQuiet() && (stopwatch.elapsed(MINUTES) > 0)) {
                stopwatch.reset().start();
                LOG.info("Rewrite progress: %s valid, %s skipped, %.2f%% done", rewritten.size(), n - rewritten.size(), (((double) n) / queries.size()) * 100);
            }
        }
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException("Query rewriting failed", e);
    }
    LOG.info("Rewrote %s queries into %s queries", queries.size(), rewritten.size());
    return rewritten;
}
Also used : QueryRewriteException(io.prestosql.verifier.QueryRewriter.QueryRewriteException) Optional(java.util.Optional) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Stopwatch (com.google.common.base.Stopwatch)1 QueryRewriteException (io.prestosql.verifier.QueryRewriter.QueryRewriteException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 ExecutorService (java.util.concurrent.ExecutorService)1