Skip to content

Commit

Permalink
support refresh database command
Browse files Browse the repository at this point in the history
  • Loading branch information
vinlee19 committed Nov 14, 2024
1 parent 854512f commit 0874499
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ statementBase
| supportedDropStatement #supportedDropStatementAlias
| supportedSetStatement #supportedSetStatementAlias
| supportedUnsetStatement #supportedUnsetStatementAlias
| supportedRefreshStatement #supportedRefreshStatementAlias
| supportedShowStatement #supportedShowStatementAlias
| unsupportedStatement #unsupported
;
Expand Down Expand Up @@ -411,11 +412,12 @@ channelDescription
: FROM source=multipartIdentifier INTO destination=multipartIdentifier
partitionSpec? columnList=identifierList?
;

supportedRefreshStatement
: REFRESH CATALOG name=identifier propertyClause? #refreshCatalog
| REFRESH DATABASE name=multipartIdentifier propertyClause? #refreshDatabase
;
unsupportedRefreshStatement
: REFRESH TABLE name=multipartIdentifier #refreshTable
| REFRESH DATABASE name=multipartIdentifier propertyClause? #refreshDatabase
| REFRESH CATALOG name=identifier propertyClause? #refreshCatalog
| REFRESH LDAP (ALL | (FOR user=identifierOrText)) #refreshLdap
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
import org.apache.doris.nereids.DorisParser.QueryContext;
import org.apache.doris.nereids.DorisParser.QueryOrganizationContext;
import org.apache.doris.nereids.DorisParser.QueryTermContext;
import org.apache.doris.nereids.DorisParser.RefreshDatabaseContext;
import org.apache.doris.nereids.DorisParser.RefreshMTMVContext;
import org.apache.doris.nereids.DorisParser.RefreshMethodContext;
import org.apache.doris.nereids.DorisParser.RefreshScheduleContext;
Expand Down Expand Up @@ -481,6 +482,7 @@
import org.apache.doris.nereids.trees.plans.commands.insert.BatchInsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertOverwriteTableCommand;
import org.apache.doris.nereids.trees.plans.commands.refresh.RefreshDatabaseCommand;
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
import org.apache.doris.nereids.trees.plans.logical.LogicalCTE;
import org.apache.doris.nereids.trees.plans.logical.LogicalExcept;
Expand Down Expand Up @@ -522,6 +524,7 @@
import org.apache.doris.qe.SqlModeHelper;
import org.apache.doris.system.NodeType;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
Expand Down Expand Up @@ -4046,4 +4049,22 @@ public ShowViewCommand visitShowView(ShowViewContext ctx) {
}
return new ShowViewCommand(databaseName, new TableNameInfo(tableNameParts));
}

@Override
public RefreshDatabaseCommand visitRefreshDatabase(RefreshDatabaseContext ctx) {
Map<String, String> properties = visitPropertyClause(ctx.propertyClause()) == null ? Maps.newHashMap()
: visitPropertyClause(ctx.propertyClause());
List<String> parts = visitMultipartIdentifier(ctx.name);
int size = parts.size();
Preconditions.checkArgument(size > 0, "database name can't be empty");
String dbName = parts.get(size - 1);

if (size == 1) {
return new RefreshDatabaseCommand(dbName, properties);
} else if (parts.size() == 2) { // [ctl,db] or [db]
return new RefreshDatabaseCommand(parts.get(0), dbName, properties);
}
throw new IllegalArgumentException("Only one dot can be in the name:{}" + ctx.name);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ public enum PlanType {
SET_TRANSACTION_COMMAND,
SET_USER_PROPERTIES_COMMAND,
SET_DEFAULT_STORAGE_VAULT_COMMAND,
REFRESH_CATALOG_COMMAND,
REFRESH_DATABASE_COMMAND,

PREPARED_COMMAND,
EXECUTE_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// 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.refresh;

import org.apache.doris.analysis.StmtType;
import org.apache.doris.catalog.DatabaseIf;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.InfoSchemaDb;
import org.apache.doris.catalog.MysqlDb;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.DdlException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.datasource.CatalogIf;
import org.apache.doris.datasource.ExternalCatalog;
import org.apache.doris.datasource.ExternalDatabase;
import org.apache.doris.datasource.ExternalObjectLog;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.commands.Command;
import org.apache.doris.nereids.trees.plans.commands.ForwardWithSync;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.StmtExecutor;

import com.google.common.base.Strings;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

/**
* Refresh database.
*/
public class RefreshDatabaseCommand extends Command implements ForwardWithSync {
private static final Logger LOG = LogManager.getLogger(RefreshDatabaseCommand.class);
private static final String INVALID_CACHE = "invalid_cache";

private String catalogName;
private String dbName;
private Map<String, String> properties;
private boolean invalidCache = false;

public RefreshDatabaseCommand(String dbName, Map<String, String> properties) {
super(PlanType.REFRESH_DATABASE_COMMAND);
this.dbName = dbName;
this.properties = properties;
}

public RefreshDatabaseCommand(String catalogName, String dbName, Map<String, String> properties) {
super(PlanType.REFRESH_DATABASE_COMMAND);
this.catalogName = catalogName;
this.dbName = dbName;
this.properties = properties;
}

private void validate(ConnectContext ctx) throws AnalysisException {
if (Strings.isNullOrEmpty(catalogName)) {
catalogName = ConnectContext.get().getCurrentCatalog().getName();
}
if (Strings.isNullOrEmpty(dbName)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_DB_NAME, dbName);
}

// Don't allow dropping 'information_schema' database
if (dbName.equalsIgnoreCase(InfoSchemaDb.DATABASE_NAME)) {

ErrorReport.reportAnalysisException(
ErrorCode.ERR_DBACCESS_DENIED_ERROR, ctx.getQualifiedUser(), dbName);
}
// Don't allow dropping 'mysql' database
if (dbName.equalsIgnoreCase(MysqlDb.DATABASE_NAME)) {
ErrorReport.reportAnalysisException(
ErrorCode.ERR_DBACCESS_DENIED_ERROR, ctx.getQualifiedUser(), dbName);
}
// check access
if (!Env.getCurrentEnv().getAccessManager().checkDbPriv(ConnectContext.get(), catalogName,
dbName, PrivPredicate.SHOW)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_DB_ACCESS_DENIED_ERROR,
PrivPredicate.SHOW.getPrivs().toString(), dbName);
}
String invalidConfig = properties == null ? null : properties.get(INVALID_CACHE);
// Default is to invalid cache.
invalidCache = invalidConfig == null || invalidConfig.equalsIgnoreCase("true");
}

/**
* Refresh database
*/
public void handleRefreshDb() throws DdlException {
Env env = Env.getCurrentEnv();
CatalogIf catalog = catalogName != null ? env.getCatalogMgr().getCatalog(catalogName) : env.getCurrentCatalog();
if (catalog == null) {
throw new DdlException("Catalog " + catalogName + " doesn't exist.");
}
if (!(catalog instanceof ExternalCatalog)) {
throw new DdlException("Only support refresh database in external catalog");
}
DatabaseIf db = catalog.getDbOrDdlException(dbName);
((ExternalDatabase<?>) db).setUnInitialized(invalidCache);

ExternalObjectLog log = new ExternalObjectLog();
log.setCatalogId(catalog.getId());
log.setDbId(db.getId());
log.setInvalidCache(invalidCache);
Env.getCurrentEnv().getEditLog().logRefreshExternalDb(log);
}

@Override
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
validate(ctx);
handleRefreshDb();
}

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

/**
* refresh database statement.
*/
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("REFRESH DATABASE ");
if (catalogName != null) {
sb.append("`").append(catalogName).append("`.");
}
sb.append("`").append(dbName).append("`");
return sb.toString();
}

@Override
public StmtType stmtType() {
return StmtType.REFRESH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.apache.doris.nereids.trees.plans.commands.insert.BatchInsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertOverwriteTableCommand;
import org.apache.doris.nereids.trees.plans.commands.refresh.RefreshDatabaseCommand;

/** CommandVisitor. */
public interface CommandVisitor<R, C> {
Expand Down Expand Up @@ -242,4 +243,8 @@ default R visitShowVariablesCommand(ShowVariablesCommand showVariablesCommand, C
default R visitShowViewCommand(ShowViewCommand showViewCommand, C context) {
return visitCommand(showViewCommand, context);
}

default R visitRefreshDatabaseCommand(RefreshDatabaseCommand refreshDatabaseCommand, C context) {
return visitCommand(refreshDatabaseCommand, context);
}
}

0 comments on commit 0874499

Please sign in to comment.