Skip to content

Commit

Permalink
[Enhancement] (nereids)implement CreateFileCommand in nereids
Browse files Browse the repository at this point in the history
  • Loading branch information
Vallishp committed Dec 2, 2024
1 parent 54fe0b4 commit 248fd1c
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ supportedCreateStatement
| CREATE (OR REPLACE)? VIEW (IF NOT EXISTS)? name=multipartIdentifier
(LEFT_PAREN cols=simpleColumnDefs RIGHT_PAREN)?
(COMMENT STRING_LITERAL)? AS query #createView
| CREATE FILE name=STRING_LITERAL
((FROM | IN) database=identifier)? properties=propertyClause #createFile
| CREATE (EXTERNAL)? TABLE (IF NOT EXISTS)? name=multipartIdentifier
LIKE existedTable=multipartIdentifier
(WITH ROLLUP (rollupNames=identifierList)?)? #createTableLike
Expand Down Expand Up @@ -752,8 +754,6 @@ unsupportedCreateStatement
(SUPERUSER | DEFAULT ROLE role=STRING_LITERAL)?
passwordOption (COMMENT STRING_LITERAL)? #createUser
| CREATE (READ ONLY)? REPOSITORY name=identifier WITH storageBackend #createRepository
| CREATE FILE name=STRING_LITERAL
((FROM | IN) database=identifier)? properties=propertyClause #createFile
| CREATE INDEX (IF NOT EXISTS)? name=identifier
ON tableName=multipartIdentifier identifierList
(USING (BITMAP | NGRAM_BF | INVERTED))?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ public void createFile(CreateFileStmt stmt) throws DdlException {
stmt.getDownloadUrl(), stmt.getChecksum(), stmt.isSaveContent());
}

public void createFile(String dbName, String catalog, String fileName, String downloadUrl, String md5sum,
boolean saveContent) throws DdlException {
Database db = Env.getCurrentInternalCatalog().getDbOrDdlException(dbName);
downloadAndAddFile(db.getId(), catalog, fileName, downloadUrl, md5sum, saveContent);
}

public void dropFile(DropFileStmt stmt) throws DdlException {
String dbName = stmt.getDbName();
Database db = Env.getCurrentInternalCatalog().getDbOrDdlException(dbName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import org.apache.doris.nereids.DorisParser.ComplexDataTypeContext;
import org.apache.doris.nereids.DorisParser.ConstantContext;
import org.apache.doris.nereids.DorisParser.CreateEncryptkeyContext;
import org.apache.doris.nereids.DorisParser.CreateFileContext;
import org.apache.doris.nereids.DorisParser.CreateMTMVContext;
import org.apache.doris.nereids.DorisParser.CreateProcedureContext;
import org.apache.doris.nereids.DorisParser.CreateRoleContext;
Expand Down Expand Up @@ -479,6 +480,7 @@
import org.apache.doris.nereids.trees.plans.commands.Command;
import org.apache.doris.nereids.trees.plans.commands.Constraint;
import org.apache.doris.nereids.trees.plans.commands.CreateEncryptkeyCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateFileCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateJobCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.CreatePolicyCommand;
Expand Down Expand Up @@ -4680,6 +4682,17 @@ public LogicalPlan visitCreateRole(CreateRoleContext ctx) {
return new CreateRoleCommand(ctx.EXISTS() != null, ctx.name.getText(), comment);
}

@Override
public LogicalPlan visitCreateFile(CreateFileContext ctx) {
String dbName = null;
if (ctx.database != null) {
dbName = ctx.database.getText();
}
Map<String, String> properties = ctx.propertyClause() != null
? Maps.newHashMap(visitPropertyClause(ctx.propertyClause())) : Maps.newHashMap();
return new CreateFileCommand(stripQuotes(ctx.name.getText()), dbName, properties);
}

@Override
public LogicalPlan visitShowFrontends(ShowFrontendsContext ctx) {
String detail = (ctx.name != null) ? ctx.name.getText() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,6 @@ public enum PlanType {
RECOVER_PARTITION_COMMAND,
REPLAY_COMMAND,
CREATE_ENCRYPTKEY_COMMAND,
CREATE_FILE_COMMAND,
CREATE_ROUTINE_LOAD_COMMAND
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// 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;

import org.apache.doris.analysis.StmtType;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.trees.plans.PlanType;
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 com.google.common.collect.ImmutableSet;

import java.util.Map;
import java.util.Optional;

/** CreateFileCommand */
public class CreateFileCommand extends Command implements ForwardWithSync {
public static final String PROP_CATALOG_DEFAULT = "DEFAULT";
private static final String PROP_CATALOG = "catalog";
private static final String PROP_URL = "url";
private static final String PROP_MD5 = "md5";
private static final String PROP_SAVE_CONTENT = "save_content";

private static final ImmutableSet<String> PROPERTIES_SET = new ImmutableSet.Builder<String>()
.add(PROP_CATALOG).add(PROP_URL).add(PROP_MD5).build();
private final String fileName;
private String dbName;
private final Map<String, String> properties;

// analyze properties will fill these.
// needed item set after analyzed
private String catalogName;
private String downloadUrl;
private String checksum;
// if saveContent is true, the file content will be saved in FE memory, so the file size will be limited
// by the configuration. If it is false, only URL will be saved.
private boolean saveContent = true;

public CreateFileCommand(String fileName, String dbName, Map<String, String> properties) {
super(PlanType.CREATE_FILE_COMMAND);
this.fileName = fileName;
this.dbName = dbName;
this.properties = properties;
}

private void analyzeProperties() throws Exception {
Optional<String> optional = properties.keySet().stream().filter(
entity -> !PROPERTIES_SET.contains(entity)).findFirst();
if (optional.isPresent()) {
throw new AnalysisException(optional.get() + " is invalid property");
}

catalogName = properties.get(PROP_CATALOG);
if (Strings.isNullOrEmpty(catalogName)) {
catalogName = PROP_CATALOG_DEFAULT;
}

downloadUrl = properties.get(PROP_URL);
if (Strings.isNullOrEmpty(downloadUrl)) {
throw new AnalysisException("download url is missing");
}

if (properties.containsKey(PROP_MD5)) {
checksum = properties.get(PROP_MD5);
}

if (properties.containsKey(PROP_SAVE_CONTENT)) {
throw new AnalysisException("'save_content' property is not supported yet");
}
}

@Override
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
// check operation privilege
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN");
}

if (dbName == null) {
dbName = ctx.getDatabase();
}

if (Strings.isNullOrEmpty(dbName)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR);
}

if (Strings.isNullOrEmpty(fileName)) {
throw new AnalysisException("File name is not specified");
}

analyzeProperties();
Env.getCurrentEnv().getSmallFileMgr().createFile(dbName, catalogName, fileName,
downloadUrl, checksum, saveContent);
}

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

@Override
public StmtType stmtType() {
return StmtType.CREATE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.doris.nereids.trees.plans.commands.CleanAllProfileCommand;
import org.apache.doris.nereids.trees.plans.commands.Command;
import org.apache.doris.nereids.trees.plans.commands.CreateEncryptkeyCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateFileCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateJobCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.CreatePolicyCommand;
Expand Down Expand Up @@ -193,6 +194,10 @@ default R visitCreateJobCommand(CreateJobCommand createJobCommand, C context) {
return visitCommand(createJobCommand, context);
}

default R visitCreateFileCommand(CreateFileCommand createFileCommand, C context) {
return visitCommand(createFileCommand, context);
}

default R visitAlterMTMVCommand(AlterMTMVCommand alterMTMVCommand, C context) {
return visitCommand(alterMTMVCommand, context);
}
Expand Down
4 changes: 2 additions & 2 deletions regression-test/suites/auth_call/test_ddl_file_auth.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ suite("test_ddl_file_auth","p0,auth_call") {

sql """grant admin_priv on *.*.* to ${user}"""
connect(user=user, password="${pwd}", url=context.config.jdbcUrl) {
sql """CREATE FILE "${fileName}" IN ${dbName}
checkNereidsExecute("""CREATE FILE "${fileName}" IN ${dbName}
PROPERTIES
(
"url" = "${dataFilePath}",
"catalog" = "internal"
);"""
);""")
sql """use ${dbName}"""
checkNereidsExecute("SHOW FILE;")
checkNereidsExecute("SHOW FILE FROM ${dbName};")
Expand Down

0 comments on commit 248fd1c

Please sign in to comment.