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();
}
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;
}
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;
}
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);
}
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);
}
Aggregations