Search in sources :

Example 1 with ParsingException

use of io.trino.sql.parser.ParsingException in project trino by trinodb.

the class SqlFormatterUtil method getFormattedSql.

public static String getFormattedSql(Statement statement, SqlParser sqlParser) {
    String sql = SqlFormatter.formatSql(statement);
    // verify round-trip
    Statement parsed;
    try {
        ParsingOptions parsingOptions = new ParsingOptions(REJECT);
        parsed = sqlParser.createStatement(sql, parsingOptions);
    } catch (ParsingException e) {
        throw formattingFailure(e, "Formatted query does not parse", statement, sql);
    }
    if (!statement.equals(parsed)) {
        throw formattingFailure(null, "Query does not round-trip", statement, sql);
    }
    return sql;
}
Also used : ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) ParsingException(io.trino.sql.parser.ParsingException)

Example 2 with ParsingException

use of io.trino.sql.parser.ParsingException in project trino by trinodb.

the class HttpRequestSessionContextFactory method parsePreparedStatementsHeaders.

private Map<String, String> parsePreparedStatementsHeaders(ProtocolHeaders protocolHeaders, MultivaluedMap<String, String> headers) {
    ImmutableMap.Builder<String, String> preparedStatements = ImmutableMap.builder();
    parseProperty(headers, protocolHeaders.requestPreparedStatement()).forEach((key, value) -> {
        String statementName;
        try {
            statementName = urlDecode(key);
        } catch (IllegalArgumentException e) {
            throw badRequest(format("Invalid %s header: %s", protocolHeaders.requestPreparedStatement(), e.getMessage()));
        }
        String sqlString = preparedStatementEncoder.decodePreparedStatementFromHeader(value);
        // Validate statement
        SqlParser sqlParser = new SqlParser();
        try {
            sqlParser.createStatement(sqlString, new ParsingOptions(AS_DOUBLE));
        } catch (ParsingException e) {
            throw badRequest(format("Invalid %s header: %s", protocolHeaders.requestPreparedStatement(), e.getMessage()));
        }
        preparedStatements.put(statementName, sqlString);
    });
    return preparedStatements.buildOrThrow();
}
Also used : ParsingOptions(io.trino.sql.parser.ParsingOptions) ParsingException(io.trino.sql.parser.ParsingException) SqlParser(io.trino.sql.parser.SqlParser) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 3 with ParsingException

use of io.trino.sql.parser.ParsingException in project trino by trinodb.

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(io.trino.sql.parser.ParsingException) BigInteger(java.math.BigInteger)

Aggregations

ParsingException (io.trino.sql.parser.ParsingException)3 ParsingOptions (io.trino.sql.parser.ParsingOptions)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 SqlParser (io.trino.sql.parser.SqlParser)1 Statement (io.trino.sql.tree.Statement)1 BigInteger (java.math.BigInteger)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1