Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.execution;

import com.facebook.presto.common.QualifiedObjectName;
import com.facebook.presto.common.type.NamedTypeSignature;
import com.facebook.presto.common.type.RowFieldName;
import com.facebook.presto.common.type.TypeSignature;
import com.facebook.presto.common.type.TypeSignatureParameter;
import com.facebook.presto.common.type.UserDefinedType;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.security.AccessControl;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.sql.parser.SqlParser;
import com.facebook.presto.sql.tree.CreateType;
import com.facebook.presto.sql.tree.Expression;
import com.facebook.presto.transaction.TransactionManager;
import com.google.common.collect.Streams;
import com.google.common.util.concurrent.ListenableFuture;

import javax.inject.Inject;

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

import static com.facebook.presto.common.type.StandardTypes.ROW;
import static com.facebook.presto.common.type.TypeSignature.parseTypeSignature;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class CreateTypeTask
implements DataDefinitionTask<CreateType>
{
private final SqlParser sqlParser;

@Inject
public CreateTypeTask(SqlParser sqlParser)
{
this.sqlParser = requireNonNull(sqlParser, "sqlParser is null");
}

@Override
public String getName()
{
return "CREATE TYPE";
}

@Override
public String explain(CreateType statement, List<Expression> parameters)
{
return format("CREATE TYPE %s", statement.getTypeName());
}

@Override
public ListenableFuture<?> execute(CreateType statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters)
{
if (statement.getDistinctType().isPresent()) {
throw new PrestoException(NOT_SUPPORTED, "Creating distinct types is not yet supported");
}

List<TypeSignatureParameter> typeParameters = Streams.zip(
statement.getParameterNames().stream(),
statement.getParameterTypes().stream(),
(name, type) -> TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(name, false)), parseTypeSignature(type))))
.collect(toImmutableList());
TypeSignature signature = new TypeSignature(ROW, typeParameters);

UserDefinedType userDefinedType = new UserDefinedType(QualifiedObjectName.valueOf(statement.getTypeName().toString()), signature);
metadata.getFunctionAndTypeManager().addUserDefinedType(userDefinedType);

return immediateFuture(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.facebook.presto.execution.CreateRoleTask;
import com.facebook.presto.execution.CreateSchemaTask;
import com.facebook.presto.execution.CreateTableTask;
import com.facebook.presto.execution.CreateTypeTask;
import com.facebook.presto.execution.CreateViewTask;
import com.facebook.presto.execution.DataDefinitionTask;
import com.facebook.presto.execution.DeallocateTask;
Expand Down Expand Up @@ -123,6 +124,7 @@
import com.facebook.presto.sql.tree.CreateRole;
import com.facebook.presto.sql.tree.CreateSchema;
import com.facebook.presto.sql.tree.CreateTable;
import com.facebook.presto.sql.tree.CreateType;
import com.facebook.presto.sql.tree.CreateView;
import com.facebook.presto.sql.tree.Deallocate;
import com.facebook.presto.sql.tree.DropColumn;
Expand Down Expand Up @@ -336,6 +338,7 @@ protected void setup(Binder binder)
bindDataDefinitionTask(binder, executionBinder, CreateSchema.class, CreateSchemaTask.class);
bindDataDefinitionTask(binder, executionBinder, DropSchema.class, DropSchemaTask.class);
bindDataDefinitionTask(binder, executionBinder, RenameSchema.class, RenameSchemaTask.class);
bindDataDefinitionTask(binder, executionBinder, CreateType.class, CreateTypeTask.class);
bindDataDefinitionTask(binder, executionBinder, AddColumn.class, AddColumnTask.class);
bindDataDefinitionTask(binder, executionBinder, CreateTable.class, CreateTableTask.class);
bindDataDefinitionTask(binder, executionBinder, RenameTable.class, RenameTableTask.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.facebook.presto.execution.CreateFunctionTask;
import com.facebook.presto.execution.CreateMaterializedViewTask;
import com.facebook.presto.execution.CreateTableTask;
import com.facebook.presto.execution.CreateTypeTask;
import com.facebook.presto.execution.CreateViewTask;
import com.facebook.presto.execution.DataDefinitionTask;
import com.facebook.presto.execution.DeallocateTask;
Expand Down Expand Up @@ -170,6 +171,7 @@
import com.facebook.presto.sql.tree.CreateFunction;
import com.facebook.presto.sql.tree.CreateMaterializedView;
import com.facebook.presto.sql.tree.CreateTable;
import com.facebook.presto.sql.tree.CreateType;
import com.facebook.presto.sql.tree.CreateView;
import com.facebook.presto.sql.tree.Deallocate;
import com.facebook.presto.sql.tree.DropFunction;
Expand Down Expand Up @@ -471,6 +473,7 @@ private LocalQueryRunner(Session defaultSession, FeaturesConfig featuresConfig,
.put(CreateTable.class, new CreateTableTask())
.put(CreateView.class, new CreateViewTask(jsonCodec(ViewDefinition.class), sqlParser, new FeaturesConfig()))
.put(CreateMaterializedView.class, new CreateMaterializedViewTask(sqlParser))
.put(CreateType.class, new CreateTypeTask(sqlParser))
.put(CreateFunction.class, new CreateFunctionTask(sqlParser))
.put(AlterFunction.class, new AlterFunctionTask(sqlParser))
.put(DropFunction.class, new DropFunctionTask(sqlParser))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.facebook.presto.sql.tree.CreateSchema;
import com.facebook.presto.sql.tree.CreateTable;
import com.facebook.presto.sql.tree.CreateTableAsSelect;
import com.facebook.presto.sql.tree.CreateType;
import com.facebook.presto.sql.tree.CreateView;
import com.facebook.presto.sql.tree.Deallocate;
import com.facebook.presto.sql.tree.Delete;
Expand Down Expand Up @@ -110,6 +111,7 @@ private StatementUtils() {}
builder.put(CreateSchema.class, QueryType.DATA_DEFINITION);
builder.put(DropSchema.class, QueryType.DATA_DEFINITION);
builder.put(RenameSchema.class, QueryType.DATA_DEFINITION);
builder.put(CreateType.class, QueryType.DATA_DEFINITION);
builder.put(AddColumn.class, QueryType.DATA_DEFINITION);
builder.put(CreateTable.class, QueryType.DATA_DEFINITION);
builder.put(RenameTable.class, QueryType.DATA_DEFINITION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ statement
| ALTER TABLE (IF EXISTS)? tableName=qualifiedName
ADD COLUMN (IF NOT EXISTS)? column=columnDefinition #addColumn
| ANALYZE qualifiedName (WITH properties)? #analyze
| CREATE TYPE qualifiedName AS (
'(' sqlParameterDeclaration (',' sqlParameterDeclaration)* ')'
| type) #createType
| CREATE (OR REPLACE)? VIEW qualifiedName
(SECURITY (DEFINER | INVOKER))? AS query #createView
| DROP VIEW (IF EXISTS)? qualifiedName #dropView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.facebook.presto.sql.tree.CreateSchema;
import com.facebook.presto.sql.tree.CreateTable;
import com.facebook.presto.sql.tree.CreateTableAsSelect;
import com.facebook.presto.sql.tree.CreateType;
import com.facebook.presto.sql.tree.CreateView;
import com.facebook.presto.sql.tree.Cube;
import com.facebook.presto.sql.tree.CurrentTime;
Expand Down Expand Up @@ -328,6 +329,27 @@ public Node visitCreateTable(SqlBaseParser.CreateTableContext context)
comment);
}

@Override
public Node visitCreateType(SqlBaseParser.CreateTypeContext context)
{
if (context.type() == null) {
try {
return new CreateType(
getQualifiedName(context.qualifiedName()),
context.sqlParameterDeclaration().stream().map(p -> ((Identifier) visit(p.identifier())).getValue()).collect(toImmutableList()),
context.sqlParameterDeclaration().stream().map(p -> getType(p.type())).collect(toImmutableList()));
}
catch (IllegalArgumentException e) {
throw parseError(e.getMessage(), context);
}
}
else {
return new CreateType(
getQualifiedName(context.qualifiedName()),
getType(context.type()));
}
}

@Override
public Node visitShowCreateTable(SqlBaseParser.ShowCreateTableContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,11 @@ protected R visitCreateTable(CreateTable node, C context)
return visitStatement(node, context);
}

protected R visitCreateType(CreateType node, C context)
{
return visitStatement(node, context);
}

protected R visitCreateTableAsSelect(CreateTableAsSelect node, C context)
{
return visitStatement(node, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;

public class CreateType
extends Statement
{
private final QualifiedName typeName;
private final Optional<String> distinctType;
private final List<String> parameterNames;
private final List<String> parameterTypes;

public CreateType(QualifiedName typeName, String distinctType)
{
this(typeName, Optional.of(distinctType), ImmutableList.of(), ImmutableList.of());
}

public CreateType(QualifiedName typeName, ImmutableList<String> parameterNames, ImmutableList<String> parameterTypes)
{
this(typeName, Optional.empty(), parameterNames, parameterTypes);
}

private CreateType(QualifiedName typeName, Optional<String> distinctType, ImmutableList<String> parameterNames, ImmutableList<String> parameterTypes)
{
super(Optional.empty());

checkArgument(parameterNames.size() == parameterTypes.size(), "Expected one type for each parameter name");
checkArgument(distinctType.isPresent() || !parameterNames.isEmpty(), "Type can be either a distinct type or parameterized");

this.typeName = typeName;
this.distinctType = distinctType;
this.parameterNames = parameterNames;
this.parameterTypes = parameterTypes;

// Check for duplicate fields
HashSet<String> parameterNamesSet = new HashSet();
parameterNames.forEach(parameterName -> {
checkArgument(!parameterNamesSet.contains(parameterName), format("Duplicated member definition for %s in %s", parameterName, typeName));
parameterNamesSet.add(parameterName);
});
}

public QualifiedName getTypeName()
{
return typeName;
}

public Optional<String> getDistinctType()
{
return distinctType;
}

public List<String> getParameterNames()
{
return parameterNames;
}

public List<String> getParameterTypes()
{
return parameterTypes;
}

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

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

@Override
public int hashCode()
{
return Objects.hash(typeName, distinctType, parameterNames, parameterTypes);
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
CreateType o = (CreateType) obj;

return Objects.equals(typeName, o.typeName) &&
Objects.equals(distinctType, o.distinctType) &&
Objects.equals(parameterNames, o.parameterNames) &&
Objects.equals(parameterTypes, o.parameterTypes);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("typeName", typeName)
.add("distinctType", distinctType)
.add("parameterNames", parameterNames)
.add("parameterTypes", parameterTypes)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public Object[][] getStatements()
{"select foo(DISTINCT ,1)",
"line 1:21: mismatched input ','. Expecting: <expression>"},
{"CREATE TABLE foo () AS (VALUES 1)",
"line 1:19: mismatched input ')'. Expecting: 'FUNCTION', 'MATERIALIZED', 'OR', 'ROLE', 'SCHEMA', 'TABLE', 'TEMPORARY', 'VIEW'"},
"line 1:19: mismatched input ')'. Expecting: 'FUNCTION', 'MATERIALIZED', 'OR', 'ROLE', 'SCHEMA', 'TABLE', 'TEMPORARY', 'TYPE', 'VIEW'"},
{"CREATE TABLE foo (*) AS (VALUES 1)",
"line 1:19: mismatched input '*'. Expecting: 'FUNCTION', 'MATERIALIZED', 'OR', 'ROLE', 'SCHEMA', 'TABLE', 'TEMPORARY', 'VIEW'"},
"line 1:19: mismatched input '*'. Expecting: 'FUNCTION', 'MATERIALIZED', 'OR', 'ROLE', 'SCHEMA', 'TABLE', 'TEMPORARY', 'TYPE', 'VIEW'"},
{"SELECT grouping(a+2) FROM (VALUES (1)) AS t (a) GROUP BY a+2",
"line 1:18: mismatched input '+'. Expecting: ')', ','"},
{"SELECT x() over (ROWS select) FROM t",
Expand Down
Loading