Search in sources :

Example 6 with Statement

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

the class TreeAssertions method assertFormattedSql.

public static void assertFormattedSql(SqlParser sqlParser, ParsingOptions parsingOptions, Node expected) {
    String formatted = formatSql(expected);
    // verify round-trip of formatting already-formatted SQL
    Statement actual = parseFormatted(sqlParser, parsingOptions, formatted, expected);
    assertEquals(formatSql(actual), formatted);
    // compare parsed tree with parsed tree of formatted SQL
    if (!actual.equals(expected)) {
        // simplify finding the non-equal part of the tree
        assertListEquals(linearizeTree(actual), linearizeTree(expected));
    }
    assertEquals(actual, expected);
}
Also used : Statement(io.trino.sql.tree.Statement)

Example 7 with Statement

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

the class TestAnalyzer method analyze.

private Analysis analyze(Session clientSession, @Language("SQL") String query, AccessControl accessControl) {
    return transaction(transactionManager, accessControl).singleStatement().readUncommitted().execute(clientSession, session -> {
        Analyzer analyzer = createAnalyzer(session, accessControl);
        Statement statement = SQL_PARSER.createStatement(query, new ParsingOptions(new FeaturesConfig().isParseDecimalLiteralsAsDouble() ? AS_DOUBLE : AS_DECIMAL));
        return analyzer.analyze(statement);
    });
}
Also used : ParsingOptions(io.trino.sql.parser.ParsingOptions) Statement(io.trino.sql.tree.Statement) FeaturesConfig(io.trino.FeaturesConfig)

Example 8 with Statement

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

the class QueryRewriter method rewriteCreateTableAsSelect.

private Query rewriteCreateTableAsSelect(Connection connection, Query query, CreateTableAsSelect statement) throws SQLException, QueryRewriteException {
    QualifiedName temporaryTableName = generateTemporaryTableName(statement.getName());
    Statement rewritten = new CreateTableAsSelect(temporaryTableName, statement.getQuery(), statement.isNotExists(), statement.getProperties(), statement.isWithData(), statement.getColumnAliases(), Optional.empty());
    String createTableAsSql = formatSql(rewritten);
    String checksumSql = checksumSql(getColumns(connection, statement), temporaryTableName);
    String dropTableSql = dropTableSql(temporaryTableName);
    return new Query(query.getCatalog(), query.getSchema(), ImmutableList.of(createTableAsSql), checksumSql, ImmutableList.of(dropTableSql), query.getUsername(), query.getPassword(), query.getSessionProperties());
}
Also used : Statement(io.trino.sql.tree.Statement) QualifiedName(io.trino.sql.tree.QualifiedName) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect)

Example 9 with Statement

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

the class QueryRewriter method rewriteInsertQuery.

private Query rewriteInsertQuery(Connection connection, Query query, Insert statement) throws SQLException, QueryRewriteException {
    QualifiedName temporaryTableName = generateTemporaryTableName(statement.getTarget());
    Statement createTemporaryTable = new CreateTable(temporaryTableName, ImmutableList.of(new LikeClause(statement.getTarget(), Optional.of(INCLUDING))), true, ImmutableList.of(), Optional.empty());
    String createTemporaryTableSql = formatSql(createTemporaryTable);
    String insertSql = formatSql(new Insert(new Table(temporaryTableName), statement.getColumns(), statement.getQuery()));
    String checksumSql = checksumSql(getColumnsForTable(connection, query.getCatalog(), query.getSchema(), statement.getTarget().toString()), temporaryTableName);
    String dropTableSql = dropTableSql(temporaryTableName);
    return new Query(query.getCatalog(), query.getSchema(), ImmutableList.of(createTemporaryTableSql, insertSql), checksumSql, ImmutableList.of(dropTableSql), query.getUsername(), query.getPassword(), query.getSessionProperties());
}
Also used : LikeClause(io.trino.sql.tree.LikeClause) DropTable(io.trino.sql.tree.DropTable) CreateTable(io.trino.sql.tree.CreateTable) Table(io.trino.sql.tree.Table) Statement(io.trino.sql.tree.Statement) QualifiedName(io.trino.sql.tree.QualifiedName) CreateTable(io.trino.sql.tree.CreateTable) Insert(io.trino.sql.tree.Insert)

Example 10 with Statement

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

the class TestPrepareTask method testPrepareInvalidStatement.

@Test
public void testPrepareInvalidStatement() {
    Statement statement = new Execute(identifier("foo"), emptyList());
    String sqlString = "PREPARE my_query FROM EXECUTE foo";
    assertTrinoExceptionThrownBy(() -> executePrepare("my_query", statement, sqlString, TEST_SESSION)).hasErrorCode(NOT_SUPPORTED).hasMessage("Invalid statement type for prepared statement: EXECUTE");
}
Also used : Execute(io.trino.sql.tree.Execute) Statement(io.trino.sql.tree.Statement) 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