Search in sources :

Example 1 with ParsingOptions

use of io.trino.sql.parser.ParsingOptions 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 ParsingOptions

use of io.trino.sql.parser.ParsingOptions 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 ParsingOptions

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

the class TestSetRoleTask method executeSetRole.

private QueryStateMachine executeSetRole(String statement) {
    SetRole setRole = (SetRole) parser.createStatement(statement, new ParsingOptions());
    QueryStateMachine stateMachine = QueryStateMachine.begin(Optional.empty(), statement, Optional.empty(), testSessionBuilder().setIdentity(Identity.ofUser(USER_NAME)).build(), URI.create("fake://uri"), new ResourceGroupId("test"), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP, Optional.empty());
    new SetRoleTask(metadata, accessControl).execute(setRole, stateMachine, ImmutableList.of(), WarningCollector.NOOP);
    return stateMachine;
}
Also used : SetRole(io.trino.sql.tree.SetRole) ResourceGroupId(io.trino.spi.resourcegroups.ResourceGroupId) ParsingOptions(io.trino.sql.parser.ParsingOptions)

Example 4 with ParsingOptions

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

the class TestSimplifyExpressions method assertSimplifiesNumericTypes.

private static void assertSimplifiesNumericTypes(String expression, String expected) {
    ParsingOptions parsingOptions = new ParsingOptions();
    Expression actualExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expression, parsingOptions));
    Expression expectedExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expected, parsingOptions));
    Expression rewritten = rewrite(actualExpression, TEST_SESSION, new SymbolAllocator(numericAndBooleanSymbolTypeMapFor(actualExpression)), PLANNER_CONTEXT, createTestingTypeAnalyzer(PLANNER_CONTEXT));
    assertEquals(normalize(rewritten), normalize(expectedExpression));
}
Also used : SymbolAllocator(io.trino.sql.planner.SymbolAllocator) ParsingOptions(io.trino.sql.parser.ParsingOptions) ExpressionUtils.logicalExpression(io.trino.sql.ExpressionUtils.logicalExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) Expression(io.trino.sql.tree.Expression)

Example 5 with ParsingOptions

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

the class TestParameterExtractor method testShowStats.

@Test
public void testShowStats() {
    Statement statement = sqlParser.createStatement("SHOW STATS FOR (SELECT c1, c2 FROM test_table WHERE c1 = ? AND c2 > ?)", new ParsingOptions());
    assertThat(ParameterExtractor.getParameters(statement)).containsExactly(new Parameter(new NodeLocation(1, 57), 0), new Parameter(new NodeLocation(1, 68), 1));
    assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(2);
}
Also used : NodeLocation(io.trino.sql.tree.NodeLocation) ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) Parameter(io.trino.sql.tree.Parameter) Test(org.testng.annotations.Test)

Aggregations

ParsingOptions (io.trino.sql.parser.ParsingOptions)15 Expression (io.trino.sql.tree.Expression)6 Statement (io.trino.sql.tree.Statement)6 Test (org.testng.annotations.Test)4 Symbol (io.trino.sql.planner.Symbol)3 NodeLocation (io.trino.sql.tree.NodeLocation)3 Parameter (io.trino.sql.tree.Parameter)3 ExpressionFormatter.formatExpression (io.trino.sql.ExpressionFormatter.formatExpression)2 ExpressionTestUtils.planExpression (io.trino.sql.ExpressionTestUtils.planExpression)2 ExpressionUtils.logicalExpression (io.trino.sql.ExpressionUtils.logicalExpression)2 ParsingUtil.createParsingOptions (io.trino.sql.ParsingUtil.createParsingOptions)2 ParsingException (io.trino.sql.parser.ParsingException)2 TypeProvider (io.trino.sql.planner.TypeProvider)2 LogicalExpression (io.trino.sql.tree.LogicalExpression)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 FeaturesConfig (io.trino.FeaturesConfig)1 ResourceGroupId (io.trino.spi.resourcegroups.ResourceGroupId)1 SqlParser (io.trino.sql.parser.SqlParser)1 SymbolAllocator (io.trino.sql.planner.SymbolAllocator)1