Search in sources :

Example 1 with ParsingException

use of com.facebook.presto.sql.parser.ParsingException in project presto by prestodb.

the class TypeCalculation method calculateLiteralValue.

public static Long calculateLiteralValue(String calculation, Map<String, Long> inputs) {
    try {
        ParserRuleContext tree = parseTypeCalculation(calculation);
        CalculateTypeVisitor visitor = new CalculateTypeVisitor(inputs);
        BigInteger result = visitor.visit(tree);
        return result.longValueExact();
    } catch (StackOverflowError e) {
        throw new ParsingException("Type calculation is too large (stack overflow while parsing)");
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParsingException(com.facebook.presto.sql.parser.ParsingException) BigInteger(java.math.BigInteger)

Example 2 with ParsingException

use of com.facebook.presto.sql.parser.ParsingException in project presto by prestodb.

the class VerificationManager method filterQueryType.

private List<SourceQuery> filterQueryType(List<SourceQuery> sourceQueries) {
    if (explain) {
        return sourceQueries;
    }
    ImmutableList.Builder<SourceQuery> selected = ImmutableList.builder();
    for (SourceQuery sourceQuery : sourceQueries) {
        try {
            Statement controlStatement = sqlParser.createStatement(sourceQuery.getQuery(CONTROL), PARSING_OPTIONS);
            QueryType controlQueryType = QueryType.of(controlStatement);
            QueryType testQueryType = QueryType.of(sqlParser.createStatement(sourceQuery.getQuery(TEST), PARSING_OPTIONS));
            if (controlQueryType == UNSUPPORTED || testQueryType == UNSUPPORTED) {
                postEvent(VerifierQueryEvent.skipped(sourceQuery.getSuite(), testId, sourceQuery, UNSUPPORTED_QUERY_TYPE, skipControl));
            } else if (controlQueryType != testQueryType) {
                postEvent(VerifierQueryEvent.skipped(sourceQuery.getSuite(), testId, sourceQuery, MISMATCHED_QUERY_TYPE, skipControl));
            } else if (isLimitWithoutOrderBy(controlStatement, sourceQuery.getName())) {
                log.debug("LimitWithoutOrderByChecker Skipped %s", sourceQuery.getName());
                postEvent(VerifierQueryEvent.skipped(sourceQuery.getSuite(), testId, sourceQuery, NON_DETERMINISTIC, skipControl));
            } else {
                selected.add(sourceQuery);
            }
        } catch (ParsingException e) {
            log.warn("Failed to parse query: %s", sourceQuery.getName());
            postEvent(VerifierQueryEvent.skipped(sourceQuery.getSuite(), testId, sourceQuery, SYNTAX_ERROR, skipControl));
        }
    }
    List<SourceQuery> selectQueries = selected.build();
    log.info("Filtering query type... Remaining queries: %s", selectQueries.size());
    return selectQueries;
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Statement(com.facebook.presto.sql.tree.Statement) ParsingException(com.facebook.presto.sql.parser.ParsingException)

Example 3 with ParsingException

use of com.facebook.presto.sql.parser.ParsingException in project presto by prestodb.

the class HttpRequestSessionContext method parsePreparedStatementsHeaders.

private static Map<String, String> parsePreparedStatementsHeaders(HttpServletRequest servletRequest, SqlParserOptions sqlParserOptions) {
    ImmutableMap.Builder<String, String> preparedStatements = ImmutableMap.builder();
    for (String header : splitSessionHeader(servletRequest.getHeaders(PRESTO_PREPARED_STATEMENT))) {
        List<String> nameValue = Splitter.on('=').limit(2).trimResults().splitToList(header);
        assertRequest(nameValue.size() == 2, "Invalid %s header", PRESTO_PREPARED_STATEMENT);
        String statementName;
        String sqlString;
        try {
            statementName = urlDecode(nameValue.get(0));
            sqlString = urlDecode(nameValue.get(1));
        } catch (IllegalArgumentException e) {
            throw badRequest(format("Invalid %s header: %s", PRESTO_PREPARED_STATEMENT, e.getMessage()));
        }
        // Validate statement
        SqlParser sqlParser = new SqlParser(sqlParserOptions);
        try {
            sqlParser.createStatement(sqlString, new ParsingOptions(AS_DOUBLE));
        } catch (ParsingException e) {
            throw badRequest(format("Invalid %s header: %s", PRESTO_PREPARED_STATEMENT, e.getMessage()));
        }
        preparedStatements.put(statementName, sqlString);
    }
    return preparedStatements.build();
}
Also used : ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) ParsingException(com.facebook.presto.sql.parser.ParsingException) SqlParser(com.facebook.presto.sql.parser.SqlParser) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 4 with ParsingException

use of com.facebook.presto.sql.parser.ParsingException in project presto by prestodb.

the class TestThriftTaskStatus method getExecutionFailureInfos.

private List<ExecutionFailureInfo> getExecutionFailureInfos() {
    IOException ioException = new IOException("Remote call timed out");
    ioException.addSuppressed(new IOException("Thrift call timed out"));
    PrestoTransportException prestoTransportException = new PrestoTransportException(TOO_MANY_REQUESTS_FAILED, REMOTE_HOST, "Too many requests failed", new PrestoException(REMOTE_TASK_ERROR, "Remote Task Error"));
    ParsingException parsingException = new ParsingException("Parsing Exception", new NodeLocation(100, 1));
    return Failures.toFailures(ImmutableList.of(ioException, prestoTransportException, parsingException));
}
Also used : NodeLocation(com.facebook.presto.sql.tree.NodeLocation) ParsingException(com.facebook.presto.sql.parser.ParsingException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) PrestoTransportException(com.facebook.presto.spi.PrestoTransportException)

Example 5 with ParsingException

use of com.facebook.presto.sql.parser.ParsingException 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)

Aggregations

ParsingException (com.facebook.presto.sql.parser.ParsingException)9 Statement (com.facebook.presto.sql.tree.Statement)4 PrestoException (com.facebook.presto.spi.PrestoException)3 ParsingOptions (com.facebook.presto.sql.parser.ParsingOptions)2 SqlParser (com.facebook.presto.sql.parser.SqlParser)2 ImmutableList (com.google.common.collect.ImmutableList)2 IOException (java.io.IOException)2 URI (java.net.URI)2 JobState (com.airbnb.airpal.api.JobState)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 QueryTimeOutException (com.airbnb.airpal.core.execution.QueryClient.QueryTimeOutException)1 BasicQueryInfo (com.airbnb.airpal.presto.QueryInfoClient.BasicQueryInfo)1 Table (com.airbnb.airpal.presto.Table)1 Session (com.facebook.presto.Session)1 ErrorLocation (com.facebook.presto.client.ErrorLocation)1 QueryError (com.facebook.presto.client.QueryError)1