Skip to content

Commit

Permalink
Add parsing for DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Jun 4, 2015
1 parent 177a812 commit e3f3b37
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 1 deletion.
Expand Up @@ -35,6 +35,7 @@ statement
'(' tableElement (',' tableElement)* ')' #createTable
| DROP TABLE (IF EXISTS)? qualifiedName #dropTable
| INSERT INTO qualifiedName query #insertInto
| DELETE FROM qualifiedName (WHERE booleanExpression)? #delete
| ALTER TABLE from=qualifiedName RENAME TO to=qualifiedName #renameTable
| ALTER TABLE tableName=qualifiedName
RENAME COLUMN from=identifier TO to=identifier #renameColumn
Expand Down Expand Up @@ -418,6 +419,7 @@ TABLE: 'TABLE';
VIEW: 'VIEW';
REPLACE: 'REPLACE';
INSERT: 'INSERT';
DELETE: 'DELETE';
INTO: 'INTO';
CONSTRAINT: 'CONSTRAINT';
DESCRIBE: 'DESCRIBE';
Expand Down
Expand Up @@ -85,7 +85,7 @@ public static Select selectAll(List<SelectItem> items)
return new Select(false, items);
}

public static Relation table(QualifiedName name)
public static Table table(QualifiedName name)
{
return new Table(name);
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.facebook.presto.sql.tree.CreateTable;
import com.facebook.presto.sql.tree.CreateTableAsSelect;
import com.facebook.presto.sql.tree.CreateView;
import com.facebook.presto.sql.tree.Delete;
import com.facebook.presto.sql.tree.DropTable;
import com.facebook.presto.sql.tree.DropView;
import com.facebook.presto.sql.tree.Except;
Expand Down Expand Up @@ -575,6 +576,20 @@ protected Void visitShowSession(ShowSession node, Integer context)
return null;
}

@Override
protected Void visitDelete(Delete node, Integer context)
{
builder.append("DELETE FROM ")
.append(node.getTable().getName());

if (node.getWhere().isPresent()) {
builder.append(" WHERE ")
.append(formatExpression(node.getWhere().get()));
}

return null;
}

@Override
protected Void visitCreateTableAsSelect(CreateTableAsSelect node, Integer indent)
{
Expand Down
Expand Up @@ -28,6 +28,7 @@
import com.facebook.presto.sql.tree.CreateTableAsSelect;
import com.facebook.presto.sql.tree.CreateView;
import com.facebook.presto.sql.tree.CurrentTime;
import com.facebook.presto.sql.tree.Delete;
import com.facebook.presto.sql.tree.DoubleLiteral;
import com.facebook.presto.sql.tree.DropTable;
import com.facebook.presto.sql.tree.DropView;
Expand Down Expand Up @@ -172,6 +173,14 @@ public Node visitInsertInto(@NotNull SqlBaseParser.InsertIntoContext context)
return new Insert(getQualifiedName(context.qualifiedName()), (Query) visit(context.query()));
}

@Override
public Node visitDelete(@NotNull SqlBaseParser.DeleteContext context)
{
return new Delete(
new Table(getQualifiedName(context.qualifiedName())),
visitIfPresent(context.booleanExpression(), Expression.class));
}

@Override
public Node visitRenameTable(@NotNull SqlBaseParser.RenameTableContext context)
{
Expand Down
Expand Up @@ -461,4 +461,9 @@ protected R visitInsert(Insert node, C context)
{
return visitNode(node, context);
}

protected R visitDelete(Delete node, C context)
{
return visitStatement(node, context);
}
}
@@ -0,0 +1,78 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.sql.tree;

import java.util.Objects;
import java.util.Optional;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;

public class Delete
extends Statement
{
private final Table table;
private final Optional<Expression> where;

public Delete(Table table, Optional<Expression> where)
{
this.table = checkNotNull(table, "table is null");
this.where = checkNotNull(where, "where is null");
}

public Table getTable()
{
return table;
}

public Optional<Expression> getWhere()
{
return where;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
{
return visitor.visitDelete(this, context);
}

@Override
public int hashCode()
{
return Objects.hash(table, where);
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
Delete o = (Delete) obj;
return Objects.equals(table, o.table) &&
Objects.equals(where, o.where);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("table", table.getName())
.add("where", where)
.toString();
}
}
Expand Up @@ -24,6 +24,7 @@
import com.facebook.presto.sql.tree.CreateTableAsSelect;
import com.facebook.presto.sql.tree.CreateView;
import com.facebook.presto.sql.tree.CurrentTime;
import com.facebook.presto.sql.tree.Delete;
import com.facebook.presto.sql.tree.DoubleLiteral;
import com.facebook.presto.sql.tree.DropTable;
import com.facebook.presto.sql.tree.DropView;
Expand Down Expand Up @@ -698,6 +699,17 @@ public void testInsertInto()
new Insert(QualifiedName.of("a"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")))));
}

@Test
public void testDelete()
{
assertStatement("DELETE FROM t", new Delete(table(QualifiedName.of("t")), Optional.empty()));

assertStatement("DELETE FROM t WHERE a = b", new Delete(table(QualifiedName.of("t")), Optional.of(
new ComparisonExpression(ComparisonExpression.Type.EQUAL,
new QualifiedNameReference(QualifiedName.of("a")),
new QualifiedNameReference(QualifiedName.of("b"))))));
}

@Test
public void testRenameTable()
throws Exception
Expand Down
Expand Up @@ -129,6 +129,9 @@ public void testStatementBuilder()

printStatement("insert into foo select * from abc");

printStatement("delete from foo");
printStatement("delete from foo where a = b");

printStatement("values ('a', 1, 2.2), ('b', 2, 3.3)");

printStatement("table foo");
Expand Down

0 comments on commit e3f3b37

Please sign in to comment.