Search in sources :

Example 11 with Statement

use of io.trino.sql.tree.Statement in project trino by trinodb.

the class TestParameterExtractor method testNoParameter.

@Test
public void testNoParameter() {
    Statement statement = sqlParser.createStatement("SELECT c1, c2 FROM test_table WHERE c1 = 1 AND c2 > 2", new ParsingOptions());
    assertThat(ParameterExtractor.getParameters(statement)).isEmpty();
    assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(0);
}
Also used : ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) Test(org.testng.annotations.Test)

Example 12 with Statement

use of io.trino.sql.tree.Statement in project trino by trinodb.

the class TestParameterExtractor method testLambda.

@Test
public void testLambda() {
    Statement statement = sqlParser.createStatement("SELECT * FROM test_table WHERE any_match(items, x -> x > ?)", new ParsingOptions());
    assertThat(ParameterExtractor.getParameters(statement)).containsExactly(new Parameter(new NodeLocation(1, 58), 0));
    assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(1);
}
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)

Example 13 with Statement

use of io.trino.sql.tree.Statement in project trino by trinodb.

the class QueryPreparer method prepareQuery.

public PreparedQuery prepareQuery(Session session, Statement wrappedStatement) throws ParsingException, TrinoException {
    Statement statement = wrappedStatement;
    Optional<String> prepareSql = Optional.empty();
    if (statement instanceof Execute) {
        prepareSql = Optional.of(session.getPreparedStatementFromExecute((Execute) statement));
        statement = sqlParser.createStatement(prepareSql.get(), createParsingOptions(session));
    }
    if (statement instanceof ExplainAnalyze) {
        Statement innerStatement = ((ExplainAnalyze) statement).getStatement();
        Optional<QueryType> innerQueryType = getQueryType(innerStatement);
        if (innerQueryType.isEmpty() || innerQueryType.get() == QueryType.DATA_DEFINITION) {
            throw new TrinoException(NOT_SUPPORTED, "EXPLAIN ANALYZE doesn't support statement type: " + innerStatement.getClass().getSimpleName());
        }
    }
    List<Expression> parameters = ImmutableList.of();
    if (wrappedStatement instanceof Execute) {
        parameters = ((Execute) wrappedStatement).getParameters();
    }
    validateParameters(statement, parameters);
    return new PreparedQuery(statement, parameters, prepareSql);
}
Also used : Execute(io.trino.sql.tree.Execute) ExplainAnalyze(io.trino.sql.tree.ExplainAnalyze) Expression(io.trino.sql.tree.Expression) Statement(io.trino.sql.tree.Statement) TrinoException(io.trino.spi.TrinoException) QueryType(io.trino.spi.resourcegroups.QueryType) StatementUtils.getQueryType(io.trino.util.StatementUtils.getQueryType)

Example 14 with Statement

use of io.trino.sql.tree.Statement in project trino by trinodb.

the class TestStatementBuilder method printStatement.

private static void printStatement(String sql) {
    println(sql.trim());
    println("");
    ParsingOptions parsingOptions = new ParsingOptions(AS_DOUBLE);
    Statement statement = SQL_PARSER.createStatement(sql, parsingOptions);
    println(statement.toString());
    println("");
    println(SqlFormatter.formatSql(statement));
    println("");
    assertFormattedSql(SQL_PARSER, statement);
    println(repeat("=", 60));
    println("");
}
Also used : Statement(io.trino.sql.tree.Statement)

Aggregations

Statement (io.trino.sql.tree.Statement)14 ParsingOptions (io.trino.sql.parser.ParsingOptions)6 Test (org.testng.annotations.Test)5 Parameter (io.trino.sql.tree.Parameter)4 Execute (io.trino.sql.tree.Execute)3 NodeLocation (io.trino.sql.tree.NodeLocation)3 TrinoException (io.trino.spi.TrinoException)2 Expression (io.trino.sql.tree.Expression)2 QualifiedName (io.trino.sql.tree.QualifiedName)2 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 FeaturesConfig (io.trino.FeaturesConfig)1 Session (io.trino.Session)1 WarningCollector (io.trino.execution.warnings.WarningCollector)1 Metadata (io.trino.metadata.Metadata)1 EXPRESSION_NOT_SCALAR (io.trino.spi.StandardErrorCode.EXPRESSION_NOT_SCALAR)1 QueryType (io.trino.spi.resourcegroups.QueryType)1 ExpressionTreeUtils.extractAggregateFunctions (io.trino.sql.analyzer.ExpressionTreeUtils.extractAggregateFunctions)1 ExpressionTreeUtils.extractExpressions (io.trino.sql.analyzer.ExpressionTreeUtils.extractExpressions)1 ExpressionTreeUtils.extractWindowExpressions (io.trino.sql.analyzer.ExpressionTreeUtils.extractWindowExpressions)1