-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add support for SET DEFAULT and DROP DEFAULT statements
#26162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
core/trino-main/src/main/java/io/trino/execution/DropDefaultValueTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| 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(); | ||
| } | ||
| } | ||
124 changes: 124 additions & 0 deletions
124
core/trino-main/src/main/java/io/trino/execution/SetDefaultValueTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.