use of com.facebook.presto.sql.tree.DropView in project presto by prestodb.
the class TestSqlParser method testDropView.
@Test
public void testDropView() {
assertStatement("DROP VIEW a", new DropView(QualifiedName.of("a"), false));
assertStatement("DROP VIEW a.b", new DropView(QualifiedName.of("a", "b"), false));
assertStatement("DROP VIEW a.b.c", new DropView(QualifiedName.of("a", "b", "c"), false));
assertStatement("DROP VIEW IF EXISTS a", new DropView(QualifiedName.of("a"), true));
assertStatement("DROP VIEW IF EXISTS a.b", new DropView(QualifiedName.of("a", "b"), true));
assertStatement("DROP VIEW IF EXISTS a.b.c", new DropView(QualifiedName.of("a", "b", "c"), true));
}
use of com.facebook.presto.sql.tree.DropView 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()));
}
Aggregations