morrySnow commented on code in PR #46105: URL: https://github.com/apache/doris/pull/46105#discussion_r1908314751
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/BuildIndexOp.java: ########## @@ -0,0 +1,104 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 org.apache.doris.nereids.trees.plans.commands.info; + +import org.apache.doris.alter.AlterOpType; +import org.apache.doris.analysis.AlterTableClause; +import org.apache.doris.analysis.BuildIndexClause; +import org.apache.doris.catalog.Index; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.UserException; +import org.apache.doris.qe.ConnectContext; + +import com.google.common.collect.Maps; + +import java.util.Map; + +/** + * BuildIndexOp + */ +public class BuildIndexOp extends AlterTableOp { + // in which table the index on, only used when alter = false + private TableNameInfo tableName; + // index definition class + private IndexDefinition indexDef; + // when alter = true, clause like: alter table add index xxxx + // when alter = false, clause like: create index xx on table xxxx + private boolean alter; + // index internal class + private Index index; + + public BuildIndexOp(TableNameInfo tableName, IndexDefinition indexDef, boolean alter) { + super(AlterOpType.SCHEMA_CHANGE); + this.tableName = tableName; + this.indexDef = indexDef; + this.alter = alter; + } + + @Override + public Map<String, String> getProperties() { + return Maps.newHashMap(); + } + + public Index getIndex() { + return index; + } + + public IndexDefinition getIndexDef() { + return indexDef; + } + + public boolean isAlter() { + return alter; + } + + @Override + public void validate(ConnectContext ctx) throws UserException { + if (indexDef == null) { + throw new AnalysisException("index definition expected."); + } + indexDef.validate(); Review Comment: could we translate index in ctor? and make all field final? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreatePolicyCommand.java: ########## @@ -80,11 +97,86 @@ public <R, C> R accept(PlanVisitor<R, C> visitor, C context) { @Override public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { - throw new MustFallbackException("Not support create policy command in Nereids now"); + validate(ctx); + Policy policy = createPolicy(ctx, executor); + Env.getCurrentEnv().getPolicyMgr().createPolicy(policy, ifNotExists); } @Override public StmtType stmtType() { return StmtType.CREATE; } + + private void validate(ConnectContext ctx) throws AnalysisException { + switch (policyType) { + case STORAGE: + if (!Config.enable_storage_policy) { + throw new AnalysisException("storage policy feature is disabled by default. " + + "Enable it by setting 'enable_storage_policy=true' in fe.conf"); + } + // check auth + // check if can create policy and use storage_resource + if (!Env.getCurrentEnv().getAccessManager() + .checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, + PrivPredicate.ADMIN.getPrivs().toString()); + } + break; + case ROW: + default: + tableNameInfo.analyze(ctx); + if (user != null) { + user.analyze(); + if (user.isRootUser() || user.isAdminUser()) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLEACCESS_DENIED_ERROR, "CreatePolicyStmt", + user.getQualifiedUser(), user.getHost(), tableNameInfo.getTbl()); + } + } + // check auth + if (!Env.getCurrentEnv().getAccessManager() + .checkGlobalPriv(ConnectContext.get(), PrivPredicate.GRANT)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, + PrivPredicate.GRANT.getPrivs().toString()); + } + } + } + + private Policy createPolicy(ConnectContext ctx, StmtExecutor executor) throws AnalysisException { + long policyId = Env.getCurrentEnv().getNextId(); + switch (policyType) { + case STORAGE: + StoragePolicy storagePolicy = new StoragePolicy(policyId, policyName); + storagePolicy.init(properties, ifNotExists); + return storagePolicy; + case ROW: + return new RowPolicy(policyId, policyName, tableNameInfo.getCtl(), + tableNameInfo.getDb(), tableNameInfo.getTbl(), user, roleName, + executor.getOriginStmt().originStmt, executor.getOriginStmt().idx, filterType.get(), + translateToLegacyExpr(wherePredicate.get(), ctx)); + default: + throw new AnalysisException("Unknown policy type: " + policyType); + } + } + + /** + * translate to legacy expr, which do not need complex expression and table columns + */ + private Expr translateToLegacyExpr(Expression expression, ConnectContext ctx) { Review Comment: shoule more than one place need this function, move it into ExpressionUtils and reuse it in any place need it. such as ``` org.apache.doris.nereids.trees.plans.commands.info.CreateRoutineLoadInfo#translateToLegacyExpr ``` and ``` org.apache.doris.nereids.trees.plans.commands.CancelCommand#translateToLegacyExpr ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
