Skip to content

Commit

Permalink
ANALYZE statement: Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jessesleeping committed Feb 1, 2019
1 parent aa0ddb9 commit dbb82b5
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ statement
DROP COLUMN column=qualifiedName #dropColumn
| ALTER TABLE tableName=qualifiedName
ADD COLUMN column=columnDefinition #addColumn
| ANALYZE qualifiedName (WITH properties)? #analyze
| CREATE (OR REPLACE)? VIEW qualifiedName AS query #createView
| DROP VIEW (IF EXISTS)? qualifiedName #dropView
| CALL qualifiedName '(' (callArgument (',' callArgument)*)? ')' #call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.facebook.presto.sql.tree.AddColumn;
import com.facebook.presto.sql.tree.AliasedRelation;
import com.facebook.presto.sql.tree.AllColumns;
import com.facebook.presto.sql.tree.Analyze;
import com.facebook.presto.sql.tree.AstVisitor;
import com.facebook.presto.sql.tree.Call;
import com.facebook.presto.sql.tree.CallArgument;
Expand Down Expand Up @@ -914,6 +915,15 @@ protected Void visitDropColumn(DropColumn node, Integer context)
return null;
}

@Override
protected Void visitAnalyze(Analyze node, Integer context)
{
builder.append("ANALYZE ")
.append(formatName(node.getTableName()));
builder.append(formatPropertiesMultiLine(node.getProperties()));
return null;
}

@Override
protected Void visitAddColumn(AddColumn node, Integer indent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.facebook.presto.sql.tree.AddColumn;
import com.facebook.presto.sql.tree.AliasedRelation;
import com.facebook.presto.sql.tree.AllColumns;
import com.facebook.presto.sql.tree.Analyze;
import com.facebook.presto.sql.tree.ArithmeticBinaryExpression;
import com.facebook.presto.sql.tree.ArithmeticUnaryExpression;
import com.facebook.presto.sql.tree.ArrayConstructor;
Expand Down Expand Up @@ -354,6 +355,19 @@ public Node visitRenameColumn(SqlBaseParser.RenameColumnContext context)
(Identifier) visit(context.to));
}

@Override
public Node visitAnalyze(SqlBaseParser.AnalyzeContext context)
{
List<Property> properties = ImmutableList.of();
if (context.properties() != null) {
properties = visit(context.properties().property(), Property.class);
}
return new Analyze(
getLocation(context),
getQualifiedName(context.qualifiedName()),
properties);
}

@Override
public Node visitAddColumn(SqlBaseParser.AddColumnContext context)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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 com.google.common.collect.ImmutableList;

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

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

public class Analyze
extends Statement
{
private final QualifiedName tableName;
private final List<Property> properties;

public Analyze(QualifiedName tableName, List<Property> properties)
{
this(Optional.empty(), tableName, properties);
}

public Analyze(NodeLocation location, QualifiedName tableName, List<Property> properties)
{
this(Optional.of(location), tableName, properties);
}

private Analyze(Optional<NodeLocation> location, QualifiedName tableName, List<Property> properties)
{
super(location);
this.tableName = requireNonNull(tableName, "table is null");
this.properties = ImmutableList.copyOf(requireNonNull(properties, "properties is null"));
}

public QualifiedName getTableName()
{
return tableName;
}

public List<Property> getProperties()
{
return properties;
}

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

@Override
public List<? extends Node> getChildren()
{
return properties;
}

@Override
public int hashCode()
{
return Objects.hash(tableName, properties);
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}

if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
Analyze o = (Analyze) obj;
return Objects.equals(tableName, o.tableName) &&
Objects.equals(properties, o.properties);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("tableName", tableName)
.add("properties", properties)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ protected R visitAddColumn(AddColumn node, C context)
return visitStatement(node, context);
}

protected R visitAnalyze(Analyze node, C context)
{
return visitStatement(node, context);
}

protected R visitCreateView(CreateView node, C context)
{
return visitStatement(node, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,15 @@ protected R visitProperty(Property node, C context)
return null;
}

@Override
protected R visitAnalyze(Analyze node, C context)
{
for (Property property : node.getProperties()) {
process(property, context);
}
return null;
}

@Override
protected R visitCreateView(CreateView node, C context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.facebook.presto.sql.tree.AddColumn;
import com.facebook.presto.sql.tree.AliasedRelation;
import com.facebook.presto.sql.tree.AllColumns;
import com.facebook.presto.sql.tree.Analyze;
import com.facebook.presto.sql.tree.ArithmeticBinaryExpression;
import com.facebook.presto.sql.tree.ArrayConstructor;
import com.facebook.presto.sql.tree.AtTimeZone;
Expand Down Expand Up @@ -1328,6 +1329,25 @@ public void testRenameColumn()
assertStatement("ALTER TABLE foo.t RENAME COLUMN a TO b", new RenameColumn(QualifiedName.of("foo", "t"), identifier("a"), identifier("b")));
}

@Test
public void testAnalyze()
{
QualifiedName table = QualifiedName.of("foo");
assertStatement("ANALYZE foo", new Analyze(table, ImmutableList.of()));

assertStatement("ANALYZE foo WITH ( \"string\" = 'bar', \"long\" = 42, computed = concat('ban', 'ana'), a = ARRAY[ 'v1', 'v2' ] )",
new Analyze(table, ImmutableList.of(
new Property(new Identifier("string"), new StringLiteral("bar")),
new Property(new Identifier("long"), new LongLiteral("42")),
new Property(
new Identifier("computed"),
new FunctionCall(QualifiedName.of("concat"), ImmutableList.of(new StringLiteral("ban"), new StringLiteral("ana")))),
new Property(new Identifier("a"), new ArrayConstructor(ImmutableList.of(new StringLiteral("v1"), new StringLiteral("v2")))))));

assertStatement("EXPLAIN ANALYZE foo", new Explain(new Analyze(table, ImmutableList.of()), false, false, ImmutableList.of()));
assertStatement("EXPLAIN ANALYZE ANALYZE foo", new Explain(new Analyze(table, ImmutableList.of()), true, false, ImmutableList.of()));
}

@Test
public void testAddColumn()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public Object[][] getStatements()
{
return new Object[][] {
{"",
"line 1:1: mismatched input '<EOF>'. Expecting: 'ALTER', 'CALL', 'COMMIT', 'CREATE', 'DEALLOCATE', 'DELETE', 'DESC', 'DESCRIBE', 'DROP', 'EXECUTE', 'EXPLAIN', 'GRANT', " +
"line 1:1: mismatched input '<EOF>'. Expecting: 'ALTER', 'ANALYZE', 'CALL', 'COMMIT', 'CREATE', 'DEALLOCATE', 'DELETE', 'DESC', 'DESCRIBE', 'DROP', 'EXECUTE', 'EXPLAIN', 'GRANT', " +
"'INSERT', 'PREPARE', 'RESET', 'REVOKE', 'ROLLBACK', 'SET', 'SHOW', 'START', 'USE', <query>"},
{"@select",
"line 1:1: mismatched input '@'. Expecting: 'ALTER', 'CALL', 'COMMIT', 'CREATE', 'DEALLOCATE', 'DELETE', 'DESC', 'DESCRIBE', 'DROP', 'EXECUTE', 'EXPLAIN', 'GRANT', " +
"line 1:1: mismatched input '@'. Expecting: 'ALTER', 'ANALYZE', 'CALL', 'COMMIT', 'CREATE', 'DEALLOCATE', 'DELETE', 'DESC', 'DESCRIBE', 'DROP', 'EXECUTE', 'EXPLAIN', 'GRANT', " +
"'INSERT', 'PREPARE', 'RESET', 'REVOKE', 'ROLLBACK', 'SET', 'SHOW', 'START', 'USE', <query>"},
{"select * from foo where @what",
"line 1:25: mismatched input '@'. Expecting: <expression>"},
Expand Down

0 comments on commit dbb82b5

Please sign in to comment.