Search in sources :

Example 6 with CreateTableAsSelect

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

the class TestShadowing method testCreateTableAsSelect.

@Test
public void testCreateTableAsSelect() throws Exception {
    handle.execute("CREATE TABLE \"my_test_table\" (column1 BIGINT, column2 DOUBLE)");
    SqlParser parser = new SqlParser();
    Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "CREATE TABLE my_test_table AS SELECT 1 column1, CAST('2.0' AS DOUBLE) column2 LIMIT 1", ImmutableList.of(), null, null, ImmutableMap.of());
    QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
    Query rewrittenQuery = rewriter.shadowQuery(query);
    assertEquals(rewrittenQuery.getPreQueries().size(), 1);
    assertEquals(rewrittenQuery.getPostQueries().size(), 1);
    CreateTableAsSelect createTableAs = (CreateTableAsSelect) parser.createStatement(rewrittenQuery.getPreQueries().get(0), PARSING_OPTIONS);
    assertEquals(createTableAs.getName().getParts().size(), 1);
    assertTrue(createTableAs.getName().getSuffix().startsWith("tmp_"));
    assertFalse(createTableAs.getName().getSuffix().contains("my_test_table"));
    assertEquals(statementToQueryType(parser, rewrittenQuery.getQuery()), READ);
    Table table = new Table(createTableAs.getName());
    SingleColumn column1 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("COLUMN1"))));
    SingleColumn column2 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("COLUMN2"), new LongLiteral("1"))))));
    assertEquals(parser.createStatement(rewrittenQuery.getQuery(), PARSING_OPTIONS), simpleQuery(selectList(column1, column2), table));
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0), PARSING_OPTIONS), new DropTable(createTableAs.getName(), true));
}
Also used : DropTable(io.trino.sql.tree.DropTable) CreateTable(io.trino.sql.tree.CreateTable) Table(io.trino.sql.tree.Table) Identifier(io.trino.sql.tree.Identifier) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) LongLiteral(io.trino.sql.tree.LongLiteral) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) SqlParser(io.trino.sql.parser.SqlParser) Duration(io.airlift.units.Duration) SingleColumn(io.trino.sql.tree.SingleColumn) FunctionCall(io.trino.sql.tree.FunctionCall) DropTable(io.trino.sql.tree.DropTable) Test(org.testng.annotations.Test)

Aggregations

CreateTableAsSelect (io.trino.sql.tree.CreateTableAsSelect)6 QueryUtil.simpleQuery (io.trino.sql.QueryUtil.simpleQuery)4 Identifier (io.trino.sql.tree.Identifier)3 LongLiteral (io.trino.sql.tree.LongLiteral)3 QualifiedName (io.trino.sql.tree.QualifiedName)3 Duration (io.airlift.units.Duration)2 QueryUtil.quotedIdentifier (io.trino.sql.QueryUtil.quotedIdentifier)2 SqlParser (io.trino.sql.parser.SqlParser)2 CreateTable (io.trino.sql.tree.CreateTable)2 DropTable (io.trino.sql.tree.DropTable)2 FunctionCall (io.trino.sql.tree.FunctionCall)2 Query (io.trino.sql.tree.Query)2 Table (io.trino.sql.tree.Table)2 WithQuery (io.trino.sql.tree.WithQuery)2 Test (org.junit.jupiter.api.Test)2 Test (org.testng.annotations.Test)2 OutputNode (io.trino.sql.planner.plan.OutputNode)1 PlanNode (io.trino.sql.planner.plan.PlanNode)1 ValuesNode (io.trino.sql.planner.plan.ValuesNode)1 AllColumns (io.trino.sql.tree.AllColumns)1