Skip to content

Commit

Permalink
ANALYZE statement: Parser
Browse files Browse the repository at this point in the history
Extracted-From: prestodb/presto#11376
  • Loading branch information
jessesleeping authored and sopel39 committed Jan 29, 2019
1 parent 7c6b846 commit 618b4d0
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
(SECURITY (DEFINER | INVOKER))? AS query #createView
| DROP VIEW (IF EXISTS)? qualifiedName #dropView
Expand Down
10 changes: 10 additions & 0 deletions presto-parser/src/main/java/io/prestosql/sql/SqlFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.prestosql.sql.tree.AddColumn;
import io.prestosql.sql.tree.AliasedRelation;
import io.prestosql.sql.tree.AllColumns;
import io.prestosql.sql.tree.Analyze;
import io.prestosql.sql.tree.AstVisitor;
import io.prestosql.sql.tree.Call;
import io.prestosql.sql.tree.CallArgument;
Expand Down Expand Up @@ -956,6 +957,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 @@ -20,6 +20,7 @@
import io.prestosql.sql.tree.AddColumn;
import io.prestosql.sql.tree.AliasedRelation;
import io.prestosql.sql.tree.AllColumns;
import io.prestosql.sql.tree.Analyze;
import io.prestosql.sql.tree.ArithmeticBinaryExpression;
import io.prestosql.sql.tree.ArithmeticUnaryExpression;
import io.prestosql.sql.tree.ArrayConstructor;
Expand Down Expand Up @@ -365,6 +366,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
99 changes: 99 additions & 0 deletions presto-parser/src/main/java/io/prestosql/sql/tree/Analyze.java
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 io.prestosql.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 @@ -19,6 +19,7 @@
import io.prestosql.sql.tree.AddColumn;
import io.prestosql.sql.tree.AliasedRelation;
import io.prestosql.sql.tree.AllColumns;
import io.prestosql.sql.tree.Analyze;
import io.prestosql.sql.tree.ArithmeticBinaryExpression;
import io.prestosql.sql.tree.ArrayConstructor;
import io.prestosql.sql.tree.AtTimeZone;
Expand Down Expand Up @@ -1338,6 +1339,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 618b4d0

Please sign in to comment.