Search in sources :

Example 1 with Table

use of com.airbnb.airpal.presto.Table in project airpal by airbnb.

the class Execution method doExecute.

private Job doExecute() throws ExecutionFailureException {
    final String userQuery = QUERY_SPLITTER.splitToList(getJob().getQuery()).get(0);
    final JobOutputBuilder outputBuilder;
    job.setQueryStats(createNoOpQueryStats());
    try {
        outputBuilder = outputBuilderFactory.forJob(job);
    } catch (IOException e) {
        throw new ExecutionFailureException(job, "Could not create output builder for job", e);
    } catch (InvalidQueryException e) {
        throw new ExecutionFailureException(job, e.getMessage(), e);
    }
    final Persistor persistor = persistorFactory.getPersistor(job, job.getOutput());
    final String query = job.getOutput().processQuery(userQuery);
    if (!persistor.canPersist(authorizer)) {
        throw new ExecutionFailureException(job, "Not authorized to create tables", null);
    }
    final List<List<Object>> outputPreview = new ArrayList<>(maxRowsPreviewOutput);
    final Set<Table> tables = new HashSet<>();
    try {
        tables.addAll(authorizer.tablesUsedByQuery(query));
    } catch (ParsingException e) {
        job.setError(new QueryError(e.getMessage(), null, -1, null, null, new ErrorLocation(e.getLineNumber(), e.getColumnNumber()), null));
        throw new ExecutionFailureException(job, "Invalid query, could not parse", e);
    }
    if (!authorizer.isAuthorizedRead(tables)) {
        job.setQueryStats(createNoOpQueryStats());
        throw new ExecutionFailureException(job, "Cannot access tables", null);
    }
    QueryClient queryClient = new QueryClient(queryRunner, timeout, query);
    try {
        queryClient.executeWith(new Function<StatementClient, Void>() {

            @Nullable
            @Override
            public Void apply(@Nullable StatementClient client) {
                if (client == null) {
                    return null;
                }
                QueryResults results = client.current();
                List<Column> resultColumns = null;
                JobState jobState = null;
                QueryError queryError = null;
                QueryStats queryStats = null;
                if (isCancelled) {
                    throw new ExecutionFailureException(job, "Query was cancelled", null);
                }
                if (results.getError() != null) {
                    queryError = results.getError();
                    jobState = JobState.FAILED;
                }
                if ((results.getInfoUri() != null) && (jobState != JobState.FAILED)) {
                    BasicQueryInfo queryInfo = queryInfoClient.from(results.getInfoUri());
                    if (queryInfo != null) {
                        queryStats = queryInfo.getQueryStats();
                    }
                }
                if (results.getStats() != null) {
                    jobState = JobState.fromStatementState(results.getStats().getState());
                }
                try {
                    if (results.getColumns() != null) {
                        resultColumns = results.getColumns();
                        outputBuilder.addColumns(resultColumns);
                    }
                    if (results.getData() != null) {
                        List<List<Object>> resultsData = ImmutableList.copyOf(results.getData());
                        for (List<Object> row : resultsData) {
                            outputBuilder.addRow(row);
                        }
                    }
                } catch (FileTooLargeException e) {
                    throw new ExecutionFailureException(job, "Output file exceeded maximum configured filesize", e);
                }
                rlUpdateJobInfo(tables, resultColumns, queryStats, jobState, queryError, outputPreview);
                return null;
            }
        });
    } catch (QueryTimeOutException e) {
        throw new ExecutionFailureException(job, format("Query exceeded maximum execution time of %s minutes", Duration.millis(e.getElapsedMs()).getStandardMinutes()), e);
    }
    QueryResults finalResults = queryClient.finalResults();
    if (finalResults != null && finalResults.getInfoUri() != null) {
        BasicQueryInfo queryInfo = queryInfoClient.from(finalResults.getInfoUri());
        if (queryInfo != null) {
            updateJobInfo(null, null, queryInfo.getQueryStats(), JobState.fromStatementState(finalResults.getStats().getState()), finalResults.getError(), outputPreview, true);
        }
    }
    if (job.getState() != JobState.FAILED) {
        URI location = persistor.persist(outputBuilder, job);
        if (location != null) {
            job.getOutput().setLocation(location);
        }
    } else {
        throw new ExecutionFailureException(job, null, null);
    }
    return getJob();
}
Also used : ErrorLocation(com.facebook.presto.client.ErrorLocation) ArrayList(java.util.ArrayList) URI(java.net.URI) ExecutionFailureException(com.airbnb.airpal.core.execution.ExecutionClient.ExecutionFailureException) ParsingException(com.facebook.presto.sql.parser.ParsingException) FileTooLargeException(com.airbnb.airpal.api.output.builders.FileTooLargeException) JobState(com.airbnb.airpal.api.JobState) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) QueryError(com.facebook.presto.client.QueryError) JobOutputBuilder(com.airbnb.airpal.api.output.builders.JobOutputBuilder) HashSet(java.util.HashSet) Table(com.airbnb.airpal.presto.Table) QueryTimeOutException(com.airbnb.airpal.core.execution.QueryClient.QueryTimeOutException) BasicQueryInfo(com.airbnb.airpal.presto.QueryInfoClient.BasicQueryInfo) StatementClient(com.facebook.presto.client.StatementClient) IOException(java.io.IOException) Persistor(com.airbnb.airpal.api.output.persistors.Persistor) QueryResults(com.facebook.presto.client.QueryResults) QueryStats(com.facebook.presto.execution.QueryStats) InvalidQueryException(com.airbnb.airpal.api.output.InvalidQueryException) Nullable(javax.annotation.Nullable)

Example 2 with Table

use of com.airbnb.airpal.presto.Table in project airpal by airbnb.

the class ExecutionClient method jobFinished.

protected void jobFinished(Job job) {
    job.setQueryFinished(new DateTime());
    activeJobsStore.jobFinished(job);
    historyStore.addRun(job);
    for (Table t : job.getTablesUsed()) {
        usageStore.markUsage(t);
    }
    if (job.getOutput() instanceof HiveTablePersistentOutput && job.getOutput().getLocation() != null) {
        String[] parts = job.getOutput().getLocation().toString().split("\\.");
        if (parts.length == 2) {
            Map<String, List<String>> cache = schemaCache.getSchemaMap("hive");
            List<String> tables = cache.get(parts[0]);
            tables.add(parts[1]);
        }
    }
    eventBus.post(new JobFinishedEvent(job));
    executionMap.remove(job.getUuid());
}
Also used : Table(com.airbnb.airpal.presto.Table) HiveTablePersistentOutput(com.airbnb.airpal.api.output.HiveTablePersistentOutput) JobFinishedEvent(com.airbnb.airpal.api.event.JobFinishedEvent) List(java.util.List) DateTime(org.joda.time.DateTime)

Example 3 with Table

use of com.airbnb.airpal.presto.Table in project airpal by airbnb.

the class QueriesResource method getQueries.

@GET
public Response getQueries(@Auth AirpalUser user, @QueryParam("results") int numResults, @QueryParam("table") List<PartitionedTable> tables) {
    Iterable<Job> recentlyRun;
    int results = Optional.of(numResults).or(200);
    if (tables.size() < 1) {
        recentlyRun = jobHistoryStore.getRecentlyRun(results);
    } else {
        recentlyRun = jobHistoryStore.getRecentlyRun(results, Iterables.transform(tables, new PartitionedTable.PartitionedTableToTable()));
    }
    ImmutableList.Builder<Job> filtered = ImmutableList.builder();
    for (Job job : recentlyRun) {
        if (job.getTablesUsed().isEmpty() && (job.getState() == JobState.FAILED)) {
            filtered.add(job);
            continue;
        }
        for (Table table : job.getTablesUsed()) {
            if (AuthorizationUtil.isAuthorizedRead(user, table)) {
                filtered.add(new Job(job.getUser(), job.getQuery(), job.getUuid(), job.getOutput(), job.getQueryStats(), job.getState(), Collections.<Column>emptyList(), Collections.<Table>emptySet(), job.getQueryStartedDateTime(), job.getError(), job.getQueryFinishedDateTime()));
            }
        }
    }
    List<Job> sortedResult = Ordering.natural().nullsLast().onResultOf(JOB_ORDERING).reverse().immutableSortedCopy(filtered.build());
    return Response.ok(sortedResult).build();
}
Also used : Table(com.airbnb.airpal.presto.Table) PartitionedTable(com.airbnb.airpal.presto.PartitionedTable) Column(com.facebook.presto.client.Column) ImmutableList(com.google.common.collect.ImmutableList) PartitionedTable(com.airbnb.airpal.presto.PartitionedTable) Job(com.airbnb.airpal.api.Job) GET(javax.ws.rs.GET)

Example 4 with Table

use of com.airbnb.airpal.presto.Table in project airpal by airbnb.

the class TablesResource method getTableUpdates.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTableUpdates(@Auth AirpalUser user, @QueryParam("catalog") Optional<String> catalogOptional) {
    final String catalog = catalogOptional.or(defaultCatalog);
    final Map<String, List<String>> schemaMap = schemaCache.getSchemaMap(catalog);
    final ImmutableList.Builder<Table> builder = ImmutableList.builder();
    for (Map.Entry<String, List<String>> entry : schemaMap.entrySet()) {
        String schema = entry.getKey();
        for (String table : entry.getValue()) {
            if (isAuthorizedRead(user, catalog, schema, table)) {
                builder.add(new Table(catalog, schema, table));
            }
        }
    }
    final List<Table> tables = builder.build();
    final Map<Table, Long> allUsages = usageStore.getUsages(tables);
    final Map<PartitionedTable, DateTime> updateMap = Collections.emptyMap();
    return Response.ok(createTablesWithMetaData(tables, allUsages, updateMap)).build();
}
Also used : Table(com.airbnb.airpal.presto.Table) PartitionedTable(com.airbnb.airpal.presto.PartitionedTable) ImmutableList(com.google.common.collect.ImmutableList) PartitionedTable(com.airbnb.airpal.presto.PartitionedTable) DateTime(org.joda.time.DateTime) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with Table

use of com.airbnb.airpal.presto.Table in project airpal by airbnb.

the class UsersResource method getUserQueries.

@GET
@Path("queries")
public Response getUserQueries(@Auth AirpalUser user, @PathParam("id") String userId, @QueryParam("results") int numResults, @QueryParam("table") List<PartitionedTable> tables) {
    Iterable<Job> recentlyRun;
    int results = Optional.of(numResults).or(0);
    if (results <= 0) {
        results = 100;
    }
    if (tables.size() < 1) {
        recentlyRun = jobHistoryStore.getRecentlyRunForUser(userId, results);
    } else {
        recentlyRun = jobHistoryStore.getRecentlyRunForUser(userId, results, Iterables.transform(tables, new PartitionedTableToTable()));
    }
    ImmutableList.Builder<Job> filtered = ImmutableList.builder();
    for (Job job : recentlyRun) {
        if (job.getTablesUsed().isEmpty() && (job.getState() == JobState.FAILED)) {
            filtered.add(job);
            continue;
        }
        for (Table table : job.getTablesUsed()) {
            if (AuthorizationUtil.isAuthorizedRead(user, table)) {
                filtered.add(new Job(job.getUser(), job.getQuery(), job.getUuid(), job.getOutput(), job.getQueryStats(), job.getState(), Collections.<Column>emptyList(), Collections.<Table>emptySet(), job.getQueryStartedDateTime(), job.getError(), job.getQueryFinishedDateTime()));
            }
        }
    }
    List<Job> sortedResult = Ordering.natural().nullsLast().onResultOf(JOB_ORDERING).reverse().immutableSortedCopy(filtered.build());
    return Response.ok(sortedResult).build();
}
Also used : Table(com.airbnb.airpal.presto.Table) PartitionedTableToTable(com.airbnb.airpal.presto.PartitionedTable.PartitionedTableToTable) PartitionedTable(com.airbnb.airpal.presto.PartitionedTable) Column(com.facebook.presto.client.Column) ImmutableList(com.google.common.collect.ImmutableList) PartitionedTableToTable(com.airbnb.airpal.presto.PartitionedTable.PartitionedTableToTable) Job(com.airbnb.airpal.api.Job) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

Table (com.airbnb.airpal.presto.Table)28 Test (org.junit.Test)17 ImmutableList (com.google.common.collect.ImmutableList)6 PartitionedTable (com.airbnb.airpal.presto.PartitionedTable)5 Job (com.airbnb.airpal.api.Job)4 GET (javax.ws.rs.GET)4 Column (com.facebook.presto.client.Column)3 List (java.util.List)3 DateTime (org.joda.time.DateTime)3 ArrayList (java.util.ArrayList)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 JobState (com.airbnb.airpal.api.JobState)1 JobFinishedEvent (com.airbnb.airpal.api.event.JobFinishedEvent)1 HiveTablePersistentOutput (com.airbnb.airpal.api.output.HiveTablePersistentOutput)1 InvalidQueryException (com.airbnb.airpal.api.output.InvalidQueryException)1 FileTooLargeException (com.airbnb.airpal.api.output.builders.FileTooLargeException)1 JobOutputBuilder (com.airbnb.airpal.api.output.builders.JobOutputBuilder)1 Persistor (com.airbnb.airpal.api.output.persistors.Persistor)1 ExecutionFailureException (com.airbnb.airpal.core.execution.ExecutionClient.ExecutionFailureException)1