|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.execution; |
| 15 | + |
| 16 | +import com.google.common.util.concurrent.ListenableFuture; |
| 17 | +import com.google.inject.Inject; |
| 18 | +import io.trino.Session; |
| 19 | +import io.trino.execution.warnings.WarningCollector; |
| 20 | +import io.trino.metadata.BranchPropertyManager; |
| 21 | +import io.trino.metadata.Metadata; |
| 22 | +import io.trino.metadata.QualifiedObjectName; |
| 23 | +import io.trino.metadata.TableHandle; |
| 24 | +import io.trino.security.AccessControl; |
| 25 | +import io.trino.spi.connector.CatalogHandle; |
| 26 | +import io.trino.sql.PlannerContext; |
| 27 | +import io.trino.sql.tree.CreateBranch; |
| 28 | +import io.trino.sql.tree.Expression; |
| 29 | +import io.trino.sql.tree.NodeRef; |
| 30 | +import io.trino.sql.tree.Parameter; |
| 31 | + |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +import static com.google.common.util.concurrent.Futures.immediateVoidFuture; |
| 37 | +import static io.trino.execution.ParameterExtractor.bindParameters; |
| 38 | +import static io.trino.metadata.MetadataUtil.createQualifiedObjectName; |
| 39 | +import static io.trino.metadata.MetadataUtil.getRequiredCatalogHandle; |
| 40 | +import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR; |
| 41 | +import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; |
| 42 | +import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; |
| 43 | +import static io.trino.sql.analyzer.SemanticExceptions.semanticException; |
| 44 | +import static io.trino.sql.tree.SaveMode.FAIL; |
| 45 | +import static io.trino.sql.tree.SaveMode.REPLACE; |
| 46 | +import static java.util.Objects.requireNonNull; |
| 47 | + |
| 48 | +public class CreateBranchTask |
| 49 | + implements DataDefinitionTask<CreateBranch> |
| 50 | +{ |
| 51 | + private final PlannerContext plannerContext; |
| 52 | + private final Metadata metadata; |
| 53 | + private final AccessControl accessControl; |
| 54 | + private final BranchPropertyManager branchPropertyManager; |
| 55 | + |
| 56 | + @Inject |
| 57 | + public CreateBranchTask(PlannerContext plannerContext, Metadata metadata, AccessControl accessControl, BranchPropertyManager branchPropertyManager) |
| 58 | + { |
| 59 | + this.plannerContext = requireNonNull(plannerContext, "plannerContext is null"); |
| 60 | + this.metadata = requireNonNull(metadata, "metadata is null"); |
| 61 | + this.accessControl = requireNonNull(accessControl, "accessControl is null"); |
| 62 | + this.branchPropertyManager = requireNonNull(branchPropertyManager, "branchPropertyManager is null"); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String getName() |
| 67 | + { |
| 68 | + return "CREATE BRANCH"; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public ListenableFuture<Void> execute(CreateBranch statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) |
| 73 | + { |
| 74 | + Session session = stateMachine.getSession(); |
| 75 | + |
| 76 | + QualifiedObjectName table = createQualifiedObjectName(session, statement, statement.getTableName()); |
| 77 | + String branch = statement.getBranchName().getValue(); |
| 78 | + |
| 79 | + if (metadata.isMaterializedView(session, table)) { |
| 80 | + throw semanticException(GENERIC_USER_ERROR, statement, "Dropping branch from materialized view is not supported"); |
| 81 | + } |
| 82 | + if (metadata.isView(session, table)) { |
| 83 | + throw semanticException(GENERIC_USER_ERROR, statement, "Dropping branch from view is not supported"); |
| 84 | + } |
| 85 | + Optional<TableHandle> tableHandle = metadata.getRedirectionAwareTableHandle(session, table).tableHandle(); |
| 86 | + if (tableHandle.isEmpty()) { |
| 87 | + throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", table); |
| 88 | + } |
| 89 | + |
| 90 | + if (metadata.branchExists(session, table, branch) && statement.getSaveMode() != REPLACE) { |
| 91 | + if (statement.getSaveMode() == FAIL) { |
| 92 | + throw semanticException(NOT_SUPPORTED, statement, "Branch '%s' already exists", branch); |
| 93 | + } |
| 94 | + return immediateVoidFuture(); |
| 95 | + } |
| 96 | + |
| 97 | + Map<NodeRef<Parameter>, Expression> parameterLookup = bindParameters(statement, parameters); |
| 98 | + CatalogHandle catalogHandle = getRequiredCatalogHandle(metadata, session, statement, table.catalogName()); |
| 99 | + Map<String, Object> properties = branchPropertyManager.getProperties( |
| 100 | + table.catalogName(), |
| 101 | + catalogHandle, |
| 102 | + statement.getProperties(), |
| 103 | + session, |
| 104 | + plannerContext, |
| 105 | + accessControl, |
| 106 | + parameterLookup, |
| 107 | + true); |
| 108 | + |
| 109 | + accessControl.checkCanCreateBranch(session.toSecurityContext(), table, branch); |
| 110 | + metadata.createBranch(session, tableHandle.get(), branch, properties); |
| 111 | + |
| 112 | + return immediateVoidFuture(); |
| 113 | + } |
| 114 | +} |
0 commit comments