Search in sources :

Example 6 with Insert

use of io.prestosql.sql.tree.Insert in project hetu-core by openlookeng.

the class TestSqlParser method testInsertInto.

@Test
public void testInsertInto() {
    QualifiedName table = QualifiedName.of("a");
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    assertStatement("INSERT INTO a SELECT * FROM t", new Insert(table, Optional.empty(), query));
    assertStatement("INSERT INTO a (c1, c2) SELECT * FROM t", new Insert(table, Optional.of(ImmutableList.of(identifier("c1"), identifier("c2"))), query));
}
Also used : QueryUtil.simpleQuery(io.prestosql.sql.QueryUtil.simpleQuery) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) QualifiedName(io.prestosql.sql.tree.QualifiedName) AllColumns(io.prestosql.sql.tree.AllColumns) Insert(io.prestosql.sql.tree.Insert) Test(org.testng.annotations.Test)

Example 7 with Insert

use of io.prestosql.sql.tree.Insert in project hetu-core by openlookeng.

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));
    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));
    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"))));
    Select select = new Select(false, ImmutableList.of(columnA, columnB, columnC));
    QuerySpecification querySpecification = new QuerySpecification(select, Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    assertEquals(parser.createStatement(rewrittenQuery.getQuery()), new io.prestosql.sql.tree.Query(Optional.empty(), querySpecification, Optional.empty(), Optional.empty(), Optional.empty()));
    assertEquals(rewrittenQuery.getPostQueries().size(), 1);
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0)), new DropTable(createTable.getName(), true));
}
Also used : Table(io.prestosql.sql.tree.Table) DropTable(io.prestosql.sql.tree.DropTable) CreateTable(io.prestosql.sql.tree.CreateTable) LongLiteral(io.prestosql.sql.tree.LongLiteral) SqlParser(io.prestosql.sql.parser.SqlParser) CreateTable(io.prestosql.sql.tree.CreateTable) Duration(io.airlift.units.Duration) SingleColumn(io.prestosql.sql.tree.SingleColumn) Insert(io.prestosql.sql.tree.Insert) DropTable(io.prestosql.sql.tree.DropTable) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) Identifier(io.prestosql.sql.tree.Identifier) Select(io.prestosql.sql.tree.Select) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) FunctionCall(io.prestosql.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Aggregations

Insert (io.prestosql.sql.tree.Insert)7 QualifiedName (io.prestosql.sql.tree.QualifiedName)5 Statement (io.prestosql.sql.tree.Statement)4 TableHandle (io.prestosql.spi.metadata.TableHandle)3 Test (org.testng.annotations.Test)3 Preconditions.checkState (com.google.common.base.Preconditions.checkState)2 Verify.verify (com.google.common.base.Verify.verify)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Streams.zip (com.google.common.collect.Streams.zip)2 CubeFilter (io.hetu.core.spi.cube.CubeFilter)2 CubeMetadata (io.hetu.core.spi.cube.CubeMetadata)2 CubeStatus (io.hetu.core.spi.cube.CubeStatus)2 Session (io.prestosql.Session)2 SystemSessionProperties.isSkipAttachingStatsWithPlan (io.prestosql.SystemSessionProperties.isSkipAttachingStatsWithPlan)2 CachingCostProvider (io.prestosql.cost.CachingCostProvider)2 CachingStatsProvider (io.prestosql.cost.CachingStatsProvider)2