Skip to content

Commit

Permalink
Add IF EXISTS support to DROP TABLE and DROP VIEW
Browse files Browse the repository at this point in the history
  • Loading branch information
yuananf authored and dain committed Apr 21, 2015
1 parent 97eedad commit 224433d
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 26 deletions.
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/release/release-0.101.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ General Changes
---------------

* Add support for :doc:`/sql/create-table` (in addition to :doc:`/sql/create-table-as`).
* Add `IF EXISTS` support to :doc:`/sql/drop-table` and :doc:`/sql/drop-view`.
* Add :func:`array_agg` function.
* Add :func:`array_intersect` function.
* Add :func:`regexp_split` function.
Expand Down
13 changes: 10 additions & 3 deletions presto-docs/src/main/sphinx/sql/drop-table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ Synopsis

.. code-block:: none
DROP TABLE table_name
DROP TABLE [ IF EXISTS ] table_name
Description
-----------

Drops an existing table
Drops an existing table.

The optional ``IF EXISTS`` clause causes the error to be suppressed if
the table does not exist.

Examples
--------

Drop a table ``orders_by_date``::
Drop the table ``orders_by_date``::

DROP TABLE orders_by_date

Drop the table ``orders_by_date`` if it exists::

DROP TABLE IF EXISTS orders_by_date

9 changes: 8 additions & 1 deletion presto-docs/src/main/sphinx/sql/drop-view.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ Synopsis

.. code-block:: none
DROP VIEW view_name
DROP VIEW [ IF EXISTS ] view_name
Description
-----------

Drop an existing view.

The optional ``IF EXISTS`` clause causes the error to be suppressed if
the view does not exist.

Examples
--------

Drop the view ``orders_by_date``::

DROP VIEW orders_by_date

Drop the view ``orders_by_date`` if it exists::

DROP VIEW IF EXISTS orders_by_date
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public void execute(DropTable statement, Session session, Metadata metadata, Que

Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
if (!tableHandle.isPresent()) {
throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
if (!statement.isExists()) {
throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
}
return;
}

metadata.dropTable(tableHandle.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public void execute(DropView statement, Session session, Metadata metadata, Quer

Optional<ViewDefinition> view = metadata.getView(session, name);
if (!view.isPresent()) {
throw new SemanticException(MISSING_TABLE, statement, "View '%s' does not exist", name);
if (!statement.isExists()) {
throw new SemanticException(MISSING_TABLE, statement, "View '%s' does not exist", name);
}
return;
}

metadata.dropView(session, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ statement
| CREATE TABLE qualifiedName AS query #createTableAsSelect
| CREATE TABLE qualifiedName
'(' tableElement (',' tableElement)* ')' #createTable
| DROP TABLE qualifiedName #dropTable
| DROP TABLE (IF EXISTS)? qualifiedName #dropTable
| INSERT INTO qualifiedName query #insertInto
| ALTER TABLE from=qualifiedName RENAME TO to=qualifiedName #renameTable
| CREATE (OR REPLACE)? VIEW qualifiedName AS query #createView
| DROP VIEW qualifiedName #dropView
| DROP VIEW (IF EXISTS)? qualifiedName #dropView
| EXPLAIN ('(' explainOption (',' explainOption)* ')')? statement #explain
| SHOW TABLES ((FROM | IN) qualifiedName)? (LIKE pattern=STRING)? #showTables
| SHOW SCHEMAS ((FROM | IN) identifier)? #showSchemas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,11 @@ protected Void visitCreateView(CreateView node, Integer indent)
@Override
protected Void visitDropView(DropView node, Integer context)
{
builder.append("DROP VIEW ")
.append(node.getName());
builder.append("DROP VIEW ");
if (node.isExists()) {
builder.append("IF EXISTS ");
}
builder.append(node.getName());

return null;
}
Expand Down Expand Up @@ -600,8 +603,11 @@ protected Void visitCreateTable(CreateTable node, Integer indent)
@Override
protected Void visitDropTable(DropTable node, Integer context)
{
builder.append("DROP TABLE ")
.append(node.getTableName());
builder.append("DROP TABLE ");
if (node.isExists()) {
builder.append("IF EXISTS ");
}
builder.append(node.getTableName());

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ public Node visitCreateTable(@NotNull SqlBaseParser.CreateTableContext context)
@Override
public Node visitDropTable(@NotNull SqlBaseParser.DropTableContext context)
{
return new DropTable(getQualifiedName(context.qualifiedName()));
return new DropTable(getQualifiedName(context.qualifiedName()), context.EXISTS() != null);
}

@Override
public Node visitDropView(@NotNull SqlBaseParser.DropViewContext context)
{
return new DropView(getQualifiedName(context.qualifiedName()));
return new DropView(getQualifiedName(context.qualifiedName()), context.EXISTS() != null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ public class DropTable
extends Statement
{
private final QualifiedName tableName;
private final boolean exists;

public DropTable(QualifiedName tableName)
public DropTable(QualifiedName tableName, boolean exists)
{
this.tableName = tableName;
this.exists = exists;
}

public QualifiedName getTableName()
{
return tableName;
}

public boolean isExists()
{
return exists;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
{
Expand All @@ -41,7 +48,7 @@ public <R, C> R accept(AstVisitor<R, C> visitor, C context)
@Override
public int hashCode()
{
return Objects.hash(tableName);
return Objects.hash(tableName, exists);
}

@Override
Expand All @@ -54,14 +61,16 @@ public boolean equals(Object obj)
return false;
}
DropTable o = (DropTable) obj;
return Objects.equals(tableName, o.tableName);
return Objects.equals(tableName, o.tableName)
&& (exists == o.exists);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("tableName", tableName)
.add("exists", exists)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ public class DropView
extends Statement
{
private final QualifiedName name;
private final boolean exists;

public DropView(QualifiedName name)
public DropView(QualifiedName name, boolean exists)
{
this.name = name;
this.exists = exists;
}

public QualifiedName getName()
{
return name;
}

public boolean isExists()
{
return exists;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
{
Expand All @@ -41,7 +48,7 @@ public <R, C> R accept(AstVisitor<R, C> visitor, C context)
@Override
public int hashCode()
{
return Objects.hash(name);
return Objects.hash(name, exists);
}

@Override
Expand All @@ -54,14 +61,16 @@ public boolean equals(Object obj)
return false;
}
DropView o = (DropView) obj;
return Objects.equals(name, o.name);
return Objects.equals(name, o.name)
&& (exists == o.exists);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("name", name)
.add("exists", exists)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -667,18 +667,26 @@ public void testCreateTableAsSelect()
public void testDropTable()
throws Exception
{
assertStatement("DROP TABLE a", new DropTable(QualifiedName.of("a")));
assertStatement("DROP TABLE a.b", new DropTable(QualifiedName.of("a", "b")));
assertStatement("DROP TABLE a.b.c", new DropTable(QualifiedName.of("a", "b", "c")));
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));
}

@Test
public void testDropView()
throws Exception
{
assertStatement("DROP VIEW a", new DropView(QualifiedName.of("a")));
assertStatement("DROP VIEW a.b", new DropView(QualifiedName.of("a", "b")));
assertStatement("DROP VIEW a.b.c", new DropView(QualifiedName.of("a", "b", "c")));
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));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ public void testInsert()
assertQueryTrue("DROP TABLE test_insert");
}

@Test
public void testDropTableIfExists()
throws Exception
{
assertFalse(queryRunner.tableExists(getSession(), "test_drop_if_exists"));
assertQueryTrue("DROP TABLE IF EXISTS test_create");
assertFalse(queryRunner.tableExists(getSession(), "test_drop_if_exists"));
}

@Test
public void testView()
throws Exception
Expand Down

0 comments on commit 224433d

Please sign in to comment.