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
Expand Up @@ -87,6 +87,10 @@ statement
RENAME COLUMN (IF EXISTS)? from=qualifiedName TO to=identifier #renameColumn
| ALTER TABLE (IF EXISTS)? tableName=qualifiedName
DROP COLUMN (IF EXISTS)? column=qualifiedName #dropColumn
| ALTER TABLE (IF EXISTS)? tableName=qualifiedName
ALTER COLUMN columnName=qualifiedName SET DEFAULT literal #setDefaultValue
| ALTER TABLE (IF EXISTS)? tableName=qualifiedName
ALTER COLUMN columnName=qualifiedName DROP DEFAULT #dropDefaultValue
| ALTER TABLE (IF EXISTS)? tableName=qualifiedName
ALTER COLUMN columnName=qualifiedName SET DATA TYPE type #setColumnType
| ALTER TABLE (IF EXISTS)? tableName=qualifiedName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.trino.execution;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Inject;
import io.trino.Session;
import io.trino.connector.CatalogHandle;
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.Metadata;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.RedirectionAwareTableHandle;
import io.trino.metadata.TableHandle;
import io.trino.security.AccessControl;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ColumnMetadata;
import io.trino.sql.tree.DropDefaultValue;
import io.trino.sql.tree.Expression;
import io.trino.sql.tree.QualifiedName;

import java.util.List;

import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static io.trino.metadata.MetadataUtil.createQualifiedObjectName;
import static io.trino.spi.StandardErrorCode.COLUMN_NOT_FOUND;
import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND;
import static io.trino.spi.connector.ConnectorCapabilities.DEFAULT_COLUMN_VALUE;
import static io.trino.sql.analyzer.SemanticExceptions.semanticException;
import static java.util.Objects.requireNonNull;

public class DropDefaultValueTask
implements DataDefinitionTask<DropDefaultValue>
{
private final Metadata metadata;
private final AccessControl accessControl;

@Inject
public DropDefaultValueTask(Metadata metadata, AccessControl accessControl)
{
this.metadata = requireNonNull(metadata, "metadata is null");
this.accessControl = requireNonNull(accessControl, "accessControl is null");
}

@Override
public String getName()
{
return "DROP DEFAULT";
}

@Override
public ListenableFuture<Void> execute(
DropDefaultValue statement,
QueryStateMachine stateMachine,
List<Expression> parameters,
WarningCollector warningCollector)
{
Session session = stateMachine.getSession();
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTableName());
RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, tableName);
if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
String exceptionMessage = "Table '%s' does not exist".formatted(tableName);
if (metadata.getMaterializedView(session, tableName).isPresent()) {
exceptionMessage += ", but a materialized view with that name exists.";
}
else if (metadata.isView(session, tableName)) {
exceptionMessage += ", but a view with that name exists.";
}
if (!statement.isTableExists()) {
throw semanticException(TABLE_NOT_FOUND, statement, "%s", exceptionMessage);
}
return immediateVoidFuture();
}
accessControl.checkCanAlterColumn(session.toSecurityContext(), tableName);

TableHandle tableHandle = redirectionAwareTableHandle.tableHandle().get();
CatalogHandle catalogHandle = tableHandle.catalogHandle();
QualifiedName field = statement.getColumnName();
if (field.getOriginalParts().size() != 1) {
throw semanticException(NOT_SUPPORTED, statement, "Cannot modify nested fields");
}
String columnName = field.getOriginalParts().getFirst().getValue();
ColumnHandle columnHandle = metadata.getColumnHandles(session, tableHandle).get(columnName);

if (columnHandle == null) {
throw semanticException(COLUMN_NOT_FOUND, statement, "Column '%s' does not exist", columnName);
}

ColumnMetadata columnMetadata = metadata.getColumnMetadata(session, tableHandle, columnHandle);
if (columnMetadata.isHidden()) {
throw semanticException(NOT_SUPPORTED, statement, "Cannot modify hidden column");
}
if (columnMetadata.getDefaultValue().isEmpty()) {
throw semanticException(GENERIC_USER_ERROR, statement, "Column '%s' does not have a default value", columnName);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add a new error code for this case.

}
if (!metadata.getConnectorCapabilities(session, catalogHandle).contains(DEFAULT_COLUMN_VALUE)) {
throw semanticException(NOT_SUPPORTED, statement, "Catalog '%s' does not support default value for column name '%s'", catalogHandle, columnName);
}

metadata.dropDefaultValue(session, tableHandle, columnHandle);
return immediateVoidFuture();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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.trino.execution;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Inject;
import io.trino.Session;
import io.trino.connector.CatalogHandle;
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.Metadata;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.RedirectionAwareTableHandle;
import io.trino.metadata.TableHandle;
import io.trino.security.AccessControl;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ColumnMetadata;
import io.trino.sql.PlannerContext;
import io.trino.sql.tree.Expression;
import io.trino.sql.tree.NodeRef;
import io.trino.sql.tree.Parameter;
import io.trino.sql.tree.QualifiedName;
import io.trino.sql.tree.SetDefaultValue;

import java.util.List;
import java.util.Map;

import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static io.trino.execution.ParameterExtractor.bindParameters;
import static io.trino.metadata.MetadataUtil.createQualifiedObjectName;
import static io.trino.spi.StandardErrorCode.COLUMN_NOT_FOUND;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND;
import static io.trino.spi.connector.ConnectorCapabilities.DEFAULT_COLUMN_VALUE;
import static io.trino.sql.analyzer.ExpressionAnalyzer.analyzeDefaultColumnValue;
import static io.trino.sql.analyzer.SemanticExceptions.semanticException;
import static java.util.Objects.requireNonNull;

public class SetDefaultValueTask
implements DataDefinitionTask<SetDefaultValue>
{
private final PlannerContext plannerContext;
private final Metadata metadata;
private final AccessControl accessControl;

@Inject
public SetDefaultValueTask(PlannerContext plannerContext, AccessControl accessControl)
{
this.plannerContext = requireNonNull(plannerContext, "plannerContext is null");
this.metadata = plannerContext.getMetadata();
this.accessControl = requireNonNull(accessControl, "accessControl is null");
}

@Override
public String getName()
{
return "SET DEFAULT";
}

@Override
public ListenableFuture<Void> execute(
SetDefaultValue statement,
QueryStateMachine stateMachine,
List<Expression> parameters,
WarningCollector warningCollector)
{
Session session = stateMachine.getSession();
Map<NodeRef<Parameter>, Expression> parameterLookup = bindParameters(statement, parameters);
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTableName());
RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, tableName);

if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
String exceptionMessage = "Table '%s' does not exist".formatted(tableName);
if (metadata.getMaterializedView(session, tableName).isPresent()) {
exceptionMessage += ", but a materialized view with that name exists.";
}
else if (metadata.isView(session, tableName)) {
exceptionMessage += ", but a view with that name exists.";
}
if (!statement.isTableExists()) {
throw semanticException(TABLE_NOT_FOUND, statement, "%s", exceptionMessage);
}
return immediateVoidFuture();
}
Comment on lines +82 to +94
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe as a follow-up - Do we need to abstract this out for all the DDL operations to be perfomed on a table ?

accessControl.checkCanAlterColumn(session.toSecurityContext(), tableName);

TableHandle tableHandle = redirectionAwareTableHandle.tableHandle().get();
CatalogHandle catalogHandle = tableHandle.catalogHandle();
QualifiedName field = statement.getColumnName();
if (field.getOriginalParts().size() != 1) {
throw semanticException(NOT_SUPPORTED, statement, "Cannot modify nested fields");
}
String columnName = field.getOriginalParts().getFirst().getValue();
ColumnHandle columnHandle = metadata.getColumnHandles(session, tableHandle).get(columnName);

if (columnHandle == null) {
throw semanticException(COLUMN_NOT_FOUND, statement, "Column '%s' does not exist", columnName);
}

ColumnMetadata columnMetadata = metadata.getColumnMetadata(session, tableHandle, columnHandle);
if (columnMetadata.isHidden()) {
throw semanticException(NOT_SUPPORTED, statement, "Cannot modify hidden column");
}

if (!metadata.getConnectorCapabilities(session, catalogHandle).contains(DEFAULT_COLUMN_VALUE)) {
throw semanticException(NOT_SUPPORTED, statement, "Catalog '%s' does not support default value for column name '%s'", catalogHandle, columnName);
}
Expression defaultValue = statement.getDefaultValue();
analyzeDefaultColumnValue(session, plannerContext, accessControl, parameterLookup, warningCollector, columnMetadata.getType(), defaultValue);

metadata.setDefaultValue(session, tableHandle, columnHandle, defaultValue.toString());
return immediateVoidFuture();
}
}
10 changes: 10 additions & 0 deletions core/trino-main/src/main/java/io/trino/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@ Optional<TableExecuteHandle> getTableHandleForExecute(
*/
void addField(Session session, TableHandle tableHandle, List<String> parentPath, String fieldName, Type type, boolean ignoreExisting);

/**
* Set the specified default value to the column.
*/
void setDefaultValue(Session session, TableHandle tableHandle, ColumnHandle column, String defaultValue);

/**
* Drop a default value on the specified column.
*/
void dropDefaultValue(Session session, TableHandle tableHandle, ColumnHandle column);

/**
* Set the specified type to the column.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,22 @@ public void dropField(Session session, TableHandle tableHandle, ColumnHandle col
metadata.dropField(session.toConnectorSession(catalogHandle), tableHandle.connectorHandle(), column, fieldPath);
}

@Override
public void setDefaultValue(Session session, TableHandle tableHandle, ColumnHandle column, String defaultValue)
{
CatalogHandle catalogHandle = tableHandle.catalogHandle();
ConnectorMetadata metadata = getMetadataForWrite(session, catalogHandle);
metadata.setDefaultValue(session.toConnectorSession(catalogHandle), tableHandle.connectorHandle(), column, defaultValue);
}

@Override
public void dropDefaultValue(Session session, TableHandle tableHandle, ColumnHandle column)
{
CatalogHandle catalogHandle = tableHandle.catalogHandle();
ConnectorMetadata metadata = getMetadataForWrite(session, catalogHandle);
metadata.dropDefaultValue(session.toConnectorSession(catalogHandle), tableHandle.connectorHandle(), column);
}

@Override
public void setColumnType(Session session, TableHandle tableHandle, ColumnHandle column, Type type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.trino.execution.DropBranchTask;
import io.trino.execution.DropCatalogTask;
import io.trino.execution.DropColumnTask;
import io.trino.execution.DropDefaultValueTask;
import io.trino.execution.DropFunctionTask;
import io.trino.execution.DropMaterializedViewTask;
import io.trino.execution.DropNotNullConstraintTask;
Expand All @@ -62,6 +63,7 @@
import io.trino.execution.RollbackTask;
import io.trino.execution.SetAuthorizationTask;
import io.trino.execution.SetColumnTypeTask;
import io.trino.execution.SetDefaultValueTask;
import io.trino.execution.SetPathTask;
import io.trino.execution.SetPropertiesTask;
import io.trino.execution.SetRoleTask;
Expand Down Expand Up @@ -89,6 +91,7 @@
import io.trino.sql.tree.DropBranch;
import io.trino.sql.tree.DropCatalog;
import io.trino.sql.tree.DropColumn;
import io.trino.sql.tree.DropDefaultValue;
import io.trino.sql.tree.DropFunction;
import io.trino.sql.tree.DropMaterializedView;
import io.trino.sql.tree.DropNotNullConstraint;
Expand All @@ -113,6 +116,7 @@
import io.trino.sql.tree.Rollback;
import io.trino.sql.tree.SetAuthorizationStatement;
import io.trino.sql.tree.SetColumnType;
import io.trino.sql.tree.SetDefaultValue;
import io.trino.sql.tree.SetPath;
import io.trino.sql.tree.SetProperties;
import io.trino.sql.tree.SetRole;
Expand Down Expand Up @@ -183,6 +187,8 @@ public void configure(Binder binder)
bindDataDefinitionTask(binder, executionBinder, Revoke.class, RevokeTask.class);
bindDataDefinitionTask(binder, executionBinder, RevokeRoles.class, RevokeRolesTask.class);
bindDataDefinitionTask(binder, executionBinder, Rollback.class, RollbackTask.class);
bindDataDefinitionTask(binder, executionBinder, SetDefaultValue.class, SetDefaultValueTask.class);
bindDataDefinitionTask(binder, executionBinder, DropDefaultValue.class, DropDefaultValueTask.class);
bindDataDefinitionTask(binder, executionBinder, SetColumnType.class, SetColumnTypeTask.class);
bindDataDefinitionTask(binder, executionBinder, DropNotNullConstraint.class, DropNotNullConstraintTask.class);
bindDataDefinitionTask(binder, executionBinder, SetPath.class, SetPathTask.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
import io.trino.sql.tree.DereferenceExpression;
import io.trino.sql.tree.DropCatalog;
import io.trino.sql.tree.DropColumn;
import io.trino.sql.tree.DropDefaultValue;
import io.trino.sql.tree.DropMaterializedView;
import io.trino.sql.tree.DropNotNullConstraint;
import io.trino.sql.tree.DropSchema;
Expand Down Expand Up @@ -234,6 +235,7 @@
import io.trino.sql.tree.SelectItem;
import io.trino.sql.tree.SetAuthorizationStatement;
import io.trino.sql.tree.SetColumnType;
import io.trino.sql.tree.SetDefaultValue;
import io.trino.sql.tree.SetOperation;
import io.trino.sql.tree.SetProperties;
import io.trino.sql.tree.SetSession;
Expand Down Expand Up @@ -1125,6 +1127,18 @@ protected Scope visitAddColumn(AddColumn node, Optional<Scope> scope)
return createAndAssignScope(node, scope);
}

@Override
protected Scope visitSetDefaultValue(SetDefaultValue node, Optional<Scope> scope)
{
return createAndAssignScope(node, scope);
}

@Override
protected Scope visitDropDefaultValue(DropDefaultValue node, Optional<Scope> scope)
{
return createAndAssignScope(node, scope);
}

@Override
protected Scope visitSetColumnType(SetColumnType node, Optional<Scope> scope)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,28 @@ public void addColumn(ConnectorSession session, ConnectorTableHandle tableHandle
tables.put(tableName, new ConnectorTableMetadata(tableName, columns.build(), tableMetadata.getProperties(), tableMetadata.getComment()));
}

@Override
public void setDefaultValue(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle column, String defaultValue)
{
ConnectorTableMetadata tableMetadata = getTableMetadata(session, tableHandle);
SchemaTableName tableName = getTableName(tableHandle);
List<ColumnMetadata> columns = new ArrayList<>(tableMetadata.getColumns());
ColumnMetadata columnMetadata = getColumnMetadata(session, tableHandle, column);
columns.set(columns.indexOf(columnMetadata), ColumnMetadata.builderFrom(columnMetadata).setDefaultValue(Optional.of(defaultValue)).build());
tables.put(tableName, new ConnectorTableMetadata(tableName, ImmutableList.copyOf(columns), tableMetadata.getProperties(), tableMetadata.getComment()));
}

@Override
public void dropDefaultValue(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle columnHandle)
{
ConnectorTableMetadata tableMetadata = getTableMetadata(session, tableHandle);
SchemaTableName tableName = getTableName(tableHandle);
List<ColumnMetadata> columns = new ArrayList<>(tableMetadata.getColumns());
ColumnMetadata columnMetadata = getColumnMetadata(session, tableHandle, columnHandle);
columns.set(columns.indexOf(columnMetadata), ColumnMetadata.builderFrom(columnMetadata).setDefaultValue(Optional.empty()).build());
tables.put(tableName, new ConnectorTableMetadata(tableName, ImmutableList.copyOf(columns), tableMetadata.getProperties(), tableMetadata.getComment()));
}

@Override
public void setColumnType(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle column, Type type)
{
Expand Down
Loading
Loading