Search in sources :

Example 1 with Statement

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

the class PrepareTask method execute.

@Override
public ListenableFuture<Void> execute(Prepare prepare, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Statement statement = prepare.getStatement();
    if ((statement instanceof Prepare) || (statement instanceof Execute) || (statement instanceof Deallocate)) {
        String type = statement.getClass().getSimpleName().toUpperCase(ENGLISH);
        throw new TrinoException(NOT_SUPPORTED, "Invalid statement type for prepared statement: " + type);
    }
    String sql = getFormattedSql(statement, sqlParser);
    stateMachine.addPreparedStatement(prepare.getName().getValue(), sql);
    return immediateVoidFuture();
}
Also used : Execute(io.trino.sql.tree.Execute) Deallocate(io.trino.sql.tree.Deallocate) Statement(io.trino.sql.tree.Statement) Prepare(io.trino.sql.tree.Prepare) TrinoException(io.trino.spi.TrinoException)

Example 2 with Statement

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

the class Analyzer method analyze.

public Analysis analyze(Statement statement, QueryType queryType) {
    Statement rewrittenStatement = statementRewrite.rewrite(analyzerFactory, session, statement, parameters, parameterLookup, warningCollector);
    Analysis analysis = new Analysis(rewrittenStatement, parameterLookup, queryType);
    StatementAnalyzer analyzer = statementAnalyzerFactory.createStatementAnalyzer(analysis, session, warningCollector, CorrelationSupport.ALLOWED);
    analyzer.analyze(rewrittenStatement, Optional.empty());
    // check column access permissions for each table
    analysis.getTableColumnReferences().forEach((accessControlInfo, tableColumnReferences) -> tableColumnReferences.forEach((tableName, columns) -> accessControlInfo.getAccessControl().checkCanSelectFromColumns(accessControlInfo.getSecurityContext(session.getRequiredTransactionId(), session.getQueryId()), tableName, columns)));
    return analysis;
}
Also used : ExpressionTreeUtils.extractExpressions(io.trino.sql.analyzer.ExpressionTreeUtils.extractExpressions) ExpressionTreeUtils.extractWindowExpressions(io.trino.sql.analyzer.ExpressionTreeUtils.extractWindowExpressions) Iterables(com.google.common.collect.Iterables) StatementRewrite(io.trino.sql.rewrite.StatementRewrite) Statement(io.trino.sql.tree.Statement) EXPRESSION_NOT_SCALAR(io.trino.spi.StandardErrorCode.EXPRESSION_NOT_SCALAR) GroupingOperation(io.trino.sql.tree.GroupingOperation) ExpressionTreeUtils.extractAggregateFunctions(io.trino.sql.analyzer.ExpressionTreeUtils.extractAggregateFunctions) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Parameter(io.trino.sql.tree.Parameter) NodeRef(io.trino.sql.tree.NodeRef) Map(java.util.Map) OTHERS(io.trino.sql.analyzer.QueryType.OTHERS) Objects.requireNonNull(java.util.Objects.requireNonNull) WarningCollector(io.trino.execution.warnings.WarningCollector) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) FunctionCall(io.trino.sql.tree.FunctionCall) SemanticExceptions.semanticException(io.trino.sql.analyzer.SemanticExceptions.semanticException) Session(io.trino.Session) Statement(io.trino.sql.tree.Statement)

Example 3 with Statement

use of io.trino.sql.tree.Statement 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 4 with Statement

use of io.trino.sql.tree.Statement 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)

Example 5 with Statement

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

the class TestParameterExtractor method testParameterCount.

@Test
public void testParameterCount() {
    Statement statement = sqlParser.createStatement("SELECT c1, c2 FROM test_table WHERE c1 = ? AND c2 > ?", new ParsingOptions());
    assertThat(ParameterExtractor.getParameters(statement)).containsExactly(new Parameter(new NodeLocation(1, 41), 0), new Parameter(new NodeLocation(1, 52), 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

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