Search in sources :

Example 1 with DropTable

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

the class TestSqlParser method testDropTable.

@Test
public void testDropTable() {
    assertStatement("DROP TABLE a", new DropTable(QualifiedName.of("a"), false));
    assertStatement("DROP TABLE a.b", new DropTable(QualifiedName.of("a", "b"), false));
    assertStatement("DROP TABLE a.b.c", new DropTable(QualifiedName.of("a", "b", "c"), false));
    assertStatement("DROP TABLE a.\"b/y\".c", new DropTable(QualifiedName.of("a", "b/y", "c"), false));
    assertStatement("DROP TABLE IF EXISTS a", new DropTable(QualifiedName.of("a"), true));
    assertStatement("DROP TABLE IF EXISTS a.b", new DropTable(QualifiedName.of("a", "b"), true));
    assertStatement("DROP TABLE IF EXISTS a.b.c", new DropTable(QualifiedName.of("a", "b", "c"), true));
    assertStatement("DROP TABLE IF EXISTS a.\"b/y\".c", new DropTable(QualifiedName.of("a", "b/y", "c"), true));
}
Also used : DropTable(io.trino.sql.tree.DropTable) Test(org.junit.jupiter.api.Test)

Example 2 with DropTable

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

Example 3 with DropTable

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

the class TestShadowing method testInsert.

@Test
public void testInsert() throws Exception {
    handle.execute("CREATE TABLE \"test_insert_table\" (a BIGINT, b DOUBLE, c VARCHAR)");
    SqlParser parser = new SqlParser();
    Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "INSERT INTO test_insert_table (b, a, c) values (1.1, 1, 'a'), (2.0, 2, 'b'), (3.1, 3, 'c')", ImmutableList.of(), null, null, ImmutableMap.of());
    QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("other_catalog", "other_schema", "tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
    Query rewrittenQuery = rewriter.shadowQuery(query);
    assertEquals(rewrittenQuery.getPreQueries().size(), 2);
    CreateTable createTable = (CreateTable) parser.createStatement(rewrittenQuery.getPreQueries().get(0), PARSING_OPTIONS);
    assertEquals(createTable.getName().getParts().size(), 3);
    assertEquals(createTable.getName().getPrefix().get(), QualifiedName.of("other_catalog", "other_schema"));
    assertTrue(createTable.getName().getSuffix().startsWith("tmp_"));
    assertFalse(createTable.getName().getSuffix().contains("test_insert_table"));
    Insert insert = (Insert) parser.createStatement(rewrittenQuery.getPreQueries().get(1), PARSING_OPTIONS);
    assertEquals(insert.getTarget(), createTable.getName());
    assertEquals(insert.getColumns(), Optional.of(ImmutableList.of(identifier("b"), identifier("a"), identifier("c"))));
    Table table = new Table(createTable.getName());
    SingleColumn columnA = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("A"))));
    SingleColumn columnB = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("B"), new LongLiteral("1"))))));
    SingleColumn columnC = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("C"))));
    assertEquals(parser.createStatement(rewrittenQuery.getQuery(), PARSING_OPTIONS), simpleQuery(selectList(columnA, columnB, columnC), table));
    assertEquals(rewrittenQuery.getPostQueries().size(), 1);
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0), PARSING_OPTIONS), new DropTable(createTable.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) SqlParser(io.trino.sql.parser.SqlParser) CreateTable(io.trino.sql.tree.CreateTable) Duration(io.airlift.units.Duration) SingleColumn(io.trino.sql.tree.SingleColumn) FunctionCall(io.trino.sql.tree.FunctionCall) Insert(io.trino.sql.tree.Insert) DropTable(io.trino.sql.tree.DropTable) Test(org.testng.annotations.Test)

Aggregations

DropTable (io.trino.sql.tree.DropTable)3 Duration (io.airlift.units.Duration)2 QueryUtil.simpleQuery (io.trino.sql.QueryUtil.simpleQuery)2 SqlParser (io.trino.sql.parser.SqlParser)2 CreateTable (io.trino.sql.tree.CreateTable)2 FunctionCall (io.trino.sql.tree.FunctionCall)2 Identifier (io.trino.sql.tree.Identifier)2 LongLiteral (io.trino.sql.tree.LongLiteral)2 SingleColumn (io.trino.sql.tree.SingleColumn)2 Table (io.trino.sql.tree.Table)2 Test (org.testng.annotations.Test)2 CreateTableAsSelect (io.trino.sql.tree.CreateTableAsSelect)1 Insert (io.trino.sql.tree.Insert)1 Test (org.junit.jupiter.api.Test)1