Search in sources :

Example 1 with DropTable

use of com.facebook.presto.sql.tree.DropTable in project presto by prestodb.

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));
    assertEquals(createTableAs.getName().getParts().size(), 1);
    assertTrue(createTableAs.getName().getSuffix().startsWith("tmp_"));
    assertFalse(createTableAs.getName().getSuffix().contains("my_test_table"));
    assertEquals(PrestoVerifier.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"))))));
    Select select = new Select(false, ImmutableList.of(column1, column2));
    QuerySpecification querySpecification = new QuerySpecification(select, Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    assertEquals(parser.createStatement(rewrittenQuery.getQuery()), new com.facebook.presto.sql.tree.Query(Optional.empty(), querySpecification, Optional.empty(), Optional.empty()));
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0)), new DropTable(createTableAs.getName(), true));
}
Also used : Table(com.facebook.presto.sql.tree.Table) DropTable(com.facebook.presto.sql.tree.DropTable) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SqlParser(com.facebook.presto.sql.parser.SqlParser) Duration(io.airlift.units.Duration) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) DropTable(com.facebook.presto.sql.tree.DropTable) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Identifier(com.facebook.presto.sql.tree.Identifier) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) Select(com.facebook.presto.sql.tree.Select) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 2 with DropTable

use of com.facebook.presto.sql.tree.DropTable in project presto by prestodb.

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 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));
}
Also used : DropTable(com.facebook.presto.sql.tree.DropTable) Test(org.testng.annotations.Test)

Example 3 with DropTable

use of com.facebook.presto.sql.tree.DropTable in project presto by prestodb.

the class QueryRewriter method rewriteQuery.

public QueryObjectBundle rewriteQuery(@Language("SQL") String query, ClusterType clusterType) {
    checkState(prefixes.containsKey(clusterType), "Unsupported cluster type: %s", clusterType);
    Statement statement = sqlParser.createStatement(query, PARSING_OPTIONS);
    QualifiedName prefix = prefixes.get(clusterType);
    List<Property> properties = tableProperties.get(clusterType);
    if (statement instanceof CreateTableAsSelect) {
        CreateTableAsSelect createTableAsSelect = (CreateTableAsSelect) statement;
        QualifiedName temporaryTableName = generateTemporaryName(Optional.of(createTableAsSelect.getName()), prefix);
        return new QueryObjectBundle(temporaryTableName, ImmutableList.of(), new CreateTableAsSelect(temporaryTableName, createTableAsSelect.getQuery(), createTableAsSelect.isNotExists(), applyPropertyOverride(createTableAsSelect.getProperties(), properties), createTableAsSelect.isWithData(), createTableAsSelect.getColumnAliases(), createTableAsSelect.getComment()), ImmutableList.of(new DropTable(temporaryTableName, true)), clusterType);
    }
    if (statement instanceof Insert) {
        Insert insert = (Insert) statement;
        QualifiedName originalTableName = insert.getTarget();
        QualifiedName temporaryTableName = generateTemporaryName(Optional.of(originalTableName), prefix);
        return new QueryObjectBundle(temporaryTableName, ImmutableList.of(new CreateTable(temporaryTableName, ImmutableList.of(new LikeClause(originalTableName, Optional.of(INCLUDING))), false, properties, Optional.empty())), new Insert(temporaryTableName, insert.getColumns(), insert.getQuery()), ImmutableList.of(new DropTable(temporaryTableName, true)), clusterType);
    }
    if (statement instanceof Query) {
        QualifiedName temporaryTableName = generateTemporaryName(Optional.empty(), prefix);
        ResultSetMetaData metadata = getResultMetadata((Query) statement);
        List<Identifier> columnAliases = generateStorageColumnAliases(metadata);
        Query rewrite = rewriteNonStorableColumns((Query) statement, metadata);
        return new QueryObjectBundle(temporaryTableName, ImmutableList.of(), new CreateTableAsSelect(temporaryTableName, rewrite, false, properties, true, Optional.of(columnAliases), Optional.empty()), ImmutableList.of(new DropTable(temporaryTableName, true)), clusterType);
    }
    if (statement instanceof CreateView) {
        CreateView createView = (CreateView) statement;
        QualifiedName temporaryViewName = generateTemporaryName(Optional.empty(), prefix);
        ImmutableList.Builder<Statement> setupQueries = ImmutableList.builder();
        // Otherwise, do not pre-create temporary view.
        try {
            String createExistingViewQuery = getOnlyElement(prestoAction.execute(new ShowCreate(VIEW, createView.getName()), REWRITE, SHOW_CREATE_VIEW_CONVERTER).getResults());
            CreateView createExistingView = (CreateView) sqlParser.createStatement(createExistingViewQuery, PARSING_OPTIONS);
            setupQueries.add(new CreateView(temporaryViewName, createExistingView.getQuery(), false, createExistingView.getSecurity()));
        } catch (QueryException e) {
        // no-op
        }
        return new QueryObjectBundle(temporaryViewName, setupQueries.build(), new CreateView(temporaryViewName, createView.getQuery(), createView.isReplace(), createView.getSecurity()), ImmutableList.of(new DropView(temporaryViewName, true)), clusterType);
    }
    if (statement instanceof CreateTable) {
        CreateTable createTable = (CreateTable) statement;
        QualifiedName temporaryTableName = generateTemporaryName(Optional.empty(), prefix);
        return new QueryObjectBundle(temporaryTableName, ImmutableList.of(), new CreateTable(temporaryTableName, createTable.getElements(), createTable.isNotExists(), applyPropertyOverride(createTable.getProperties(), properties), createTable.getComment()), ImmutableList.of(new DropTable(temporaryTableName, true)), clusterType);
    }
    throw new IllegalStateException(format("Unsupported query type: %s", statement.getClass()));
}
Also used : LikeClause(com.facebook.presto.sql.tree.LikeClause) Query(com.facebook.presto.sql.tree.Query) Statement(com.facebook.presto.sql.tree.Statement) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) DropView(com.facebook.presto.sql.tree.DropView) CreateTable(com.facebook.presto.sql.tree.CreateTable) DropTable(com.facebook.presto.sql.tree.DropTable) Insert(com.facebook.presto.sql.tree.Insert) CreateView(com.facebook.presto.sql.tree.CreateView) ResultSetMetaData(java.sql.ResultSetMetaData) QueryException(com.facebook.presto.verifier.framework.QueryException) Identifier(com.facebook.presto.sql.tree.Identifier) ShowCreate(com.facebook.presto.sql.tree.ShowCreate) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) QueryObjectBundle(com.facebook.presto.verifier.framework.QueryObjectBundle) Property(com.facebook.presto.sql.tree.Property)

Aggregations

DropTable (com.facebook.presto.sql.tree.DropTable)3 CreateTableAsSelect (com.facebook.presto.sql.tree.CreateTableAsSelect)2 Identifier (com.facebook.presto.sql.tree.Identifier)2 Test (org.testng.annotations.Test)2 SqlParser (com.facebook.presto.sql.parser.SqlParser)1 CreateTable (com.facebook.presto.sql.tree.CreateTable)1 CreateView (com.facebook.presto.sql.tree.CreateView)1 DropView (com.facebook.presto.sql.tree.DropView)1 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)1 Insert (com.facebook.presto.sql.tree.Insert)1 LikeClause (com.facebook.presto.sql.tree.LikeClause)1 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)1 Property (com.facebook.presto.sql.tree.Property)1 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)1 Query (com.facebook.presto.sql.tree.Query)1 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)1 Select (com.facebook.presto.sql.tree.Select)1 ShowCreate (com.facebook.presto.sql.tree.ShowCreate)1 SingleColumn (com.facebook.presto.sql.tree.SingleColumn)1 Statement (com.facebook.presto.sql.tree.Statement)1