Search in sources :

Example 26 with Table

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

Example 27 with Table

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

the class QueryRewriter method checksumSql.

private String checksumSql(List<Column> columns, QualifiedName table) throws QueryRewriteException {
    if (columns.isEmpty()) {
        throw new QueryRewriteException("Table " + table + " has no columns");
    }
    ImmutableList.Builder<SelectItem> selectItems = ImmutableList.builder();
    for (Column column : columns) {
        Expression expression = new Identifier(column.getName());
        if (column.isApproximateType()) {
            expression = new FunctionCall(QualifiedName.of("round"), ImmutableList.of(expression, new LongLiteral(Integer.toString(doublePrecision))));
        }
        selectItems.add(new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(expression))));
    }
    Select select = new Select(false, selectItems.build());
    return formatSql(new QuerySpecification(select, Optional.of(new Table(table)), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : DropTable(io.trino.sql.tree.DropTable) CreateTable(io.trino.sql.tree.CreateTable) Table(io.trino.sql.tree.Table) LongLiteral(io.trino.sql.tree.LongLiteral) ImmutableList(com.google.common.collect.ImmutableList) SingleColumn(io.trino.sql.tree.SingleColumn) QuerySpecification(io.trino.sql.tree.QuerySpecification) Identifier(io.trino.sql.tree.Identifier) SingleColumn(io.trino.sql.tree.SingleColumn) Expression(io.trino.sql.tree.Expression) SelectItem(io.trino.sql.tree.SelectItem) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) Select(io.trino.sql.tree.Select) FunctionCall(io.trino.sql.tree.FunctionCall)

Aggregations

Table (io.trino.sql.tree.Table)27 CreateTable (io.trino.sql.tree.CreateTable)22 DropTable (io.trino.sql.tree.DropTable)22 RenameTable (io.trino.sql.tree.RenameTable)18 TruncateTable (io.trino.sql.tree.TruncateTable)18 Test (org.junit.jupiter.api.Test)18 LongLiteral (io.trino.sql.tree.LongLiteral)14 AllColumns (io.trino.sql.tree.AllColumns)13 Identifier (io.trino.sql.tree.Identifier)12 Query (io.trino.sql.tree.Query)12 QueryUtil.simpleQuery (io.trino.sql.QueryUtil.simpleQuery)11 StringLiteral (io.trino.sql.tree.StringLiteral)9 WithQuery (io.trino.sql.tree.WithQuery)9 QueryUtil.quotedIdentifier (io.trino.sql.QueryUtil.quotedIdentifier)8 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)7 QualifiedName (io.trino.sql.tree.QualifiedName)7 CreateTableAsSelect (io.trino.sql.tree.CreateTableAsSelect)6 Expression (io.trino.sql.tree.Expression)6 QuerySpecification (io.trino.sql.tree.QuerySpecification)5 TableHandle (io.trino.metadata.TableHandle)4