imay commented on a change in pull request #3418:
URL: https://github.com/apache/incubator-doris/pull/3418#discussion_r426222831



##########
File path: fe/src/main/java/org/apache/doris/analysis/CreateResourceStmt.java
##########
@@ -0,0 +1,100 @@
+// 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.analysis;
+
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Resource.ResourceType;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.FeNameFormat;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.util.PrintableMap;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.qe.ConnectContext;
+
+import java.util.Map;
+
+// CREATE [EXTERNAL] RESOURCE resource_name
+// PROPERTIES (key1 = value1, ...)
+public class CreateResourceStmt extends DdlStmt {
+    private static final String TYPE = "type";
+
+    private final boolean isExternal;
+    private final String resourceName;
+    private final Map<String, String> properties;
+
+    public CreateResourceStmt(boolean isExternal, String resourceName, 
Map<String, String> properties) {
+        this.isExternal = isExternal;
+        this.resourceName = resourceName;
+        this.properties = properties;
+    }
+
+    public String getResourceName() {
+        return resourceName;
+    }
+
+    public Map<String, String> getProperties() {
+        return properties;
+    }
+
+    public ResourceType getResourceType() {
+        return ResourceType.fromString(properties.get(TYPE));
+    }
+
+    @Override
+    public void analyze(Analyzer analyzer) throws UserException {
+        super.analyze(analyzer);
+
+        // check auth
+        if 
(!Catalog.getCurrentCatalog().getAuth().checkGlobalPriv(ConnectContext.get(), 
PrivPredicate.ADMIN)) {
+            
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, 
"ADMIN");
+        }
+
+        // check name
+        FeNameFormat.checkResourceName(resourceName);
+
+        // check type in properties
+        if (properties == null || properties.isEmpty()) {
+            throw new AnalysisException("Resource properties can't be null");
+        }
+        String type = properties.get(TYPE);
+        if (type == null) {
+            throw new AnalysisException("Resource type can't be null");
+        }
+        ResourceType resourceType = ResourceType.fromString(type);
+        if (resourceType == null) {
+            throw new AnalysisException("Unsupported resource type: " + type);
+        }
+        if (resourceType == ResourceType.SPARK && !isExternal) {
+            throw new AnalysisException("Spark is external resource");
+        }

Review comment:
       If there are properties which are not valid, better to throw exception 
to let users know

##########
File path: fe/src/main/java/org/apache/doris/analysis/CreateResourceStmt.java
##########
@@ -0,0 +1,100 @@
+// 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.analysis;
+
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Resource.ResourceType;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.FeNameFormat;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.util.PrintableMap;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.qe.ConnectContext;
+
+import java.util.Map;
+
+// CREATE [EXTERNAL] RESOURCE resource_name
+// PROPERTIES (key1 = value1, ...)
+public class CreateResourceStmt extends DdlStmt {
+    private static final String TYPE = "type";
+
+    private final boolean isExternal;
+    private final String resourceName;
+    private final Map<String, String> properties;
+
+    public CreateResourceStmt(boolean isExternal, String resourceName, 
Map<String, String> properties) {
+        this.isExternal = isExternal;
+        this.resourceName = resourceName;
+        this.properties = properties;
+    }
+
+    public String getResourceName() {
+        return resourceName;
+    }
+
+    public Map<String, String> getProperties() {
+        return properties;
+    }
+
+    public ResourceType getResourceType() {
+        return ResourceType.fromString(properties.get(TYPE));
+    }
+
+    @Override
+    public void analyze(Analyzer analyzer) throws UserException {
+        super.analyze(analyzer);
+
+        // check auth
+        if 
(!Catalog.getCurrentCatalog().getAuth().checkGlobalPriv(ConnectContext.get(), 
PrivPredicate.ADMIN)) {
+            
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, 
"ADMIN");
+        }
+
+        // check name
+        FeNameFormat.checkResourceName(resourceName);
+
+        // check type in properties
+        if (properties == null || properties.isEmpty()) {
+            throw new AnalysisException("Resource properties can't be null");
+        }
+        String type = properties.get(TYPE);
+        if (type == null) {
+            throw new AnalysisException("Resource type can't be null");
+        }
+        ResourceType resourceType = ResourceType.fromString(type);

Review comment:
       If the resource type has been resolved, better to save it as a class 
member.

##########
File path: fe/src/main/cup/sql_parser.cup
##########
@@ -1083,6 +1084,11 @@ create_stmt ::=
     {:
         RESULT = new AlterTableStmt(tableName, Lists.newArrayList(new 
CreateIndexClause(tableName, new IndexDef(indexName, cols, indexType, comment), 
false)));
     :}
+    /* resource */
+    | KW_CREATE opt_external:isExternal KW_RESOURCE ident_or_text:resourceName 
opt_properties:properties
+    {:
+        RESULT = new CreateResourceStmt(isExternal, resourceName, properties);

Review comment:
       Should update documents for all changed SQL reference.

##########
File path: fe/src/main/java/org/apache/doris/catalog/Resource.java
##########
@@ -0,0 +1,100 @@
+// 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.catalog;
+
+import org.apache.doris.analysis.CreateResourceStmt;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.io.Text;
+import org.apache.doris.common.io.Writable;
+import org.apache.doris.common.proc.BaseProcResult;
+import org.apache.doris.persist.gson.GsonUtils;
+
+import com.google.gson.annotations.SerializedName;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.Map;
+
+public abstract class Resource implements Writable {
+    public enum ResourceType {
+        SPARK;
+
+        public static ResourceType fromString(String resourceType) {
+            for (ResourceType type : ResourceType.values()) {
+                if (type.name().equalsIgnoreCase(resourceType)) {
+                    return type;
+                }
+            }
+            return null;
+        }
+    }
+
+    @SerializedName(value = "name")
+    protected String name;
+    @SerializedName(value = "type")
+    protected ResourceType type;
+
+    public Resource(String name, ResourceType type) {
+        this.name = name;
+        this.type = type;
+    }
+
+    public static Resource fromStmt(CreateResourceStmt stmt) throws 
DdlException {
+        Resource resource = null;
+        ResourceType type = stmt.getResourceType();
+        switch (type) {
+            case SPARK:
+                resource = new SparkResource(stmt.getResourceName());
+                break;
+            default:
+                throw new DdlException("Only support Spark resource.");
+        }
+
+        resource.setProperties(stmt.getProperties());
+        return resource;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public ResourceType getType() {
+        return type;
+    }
+
+    protected abstract void setProperties(Map<String, String> properties) 
throws DdlException;
+    protected abstract void getProcNodeData(BaseProcResult result);

Review comment:
       should give comments for these functions to help others know how to 
implement them

##########
File path: fe/src/main/java/org/apache/doris/catalog/Resource.java
##########
@@ -0,0 +1,100 @@
+// 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.catalog;
+
+import org.apache.doris.analysis.CreateResourceStmt;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.io.Text;
+import org.apache.doris.common.io.Writable;
+import org.apache.doris.common.proc.BaseProcResult;
+import org.apache.doris.persist.gson.GsonUtils;
+
+import com.google.gson.annotations.SerializedName;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.Map;
+
+public abstract class Resource implements Writable {
+    public enum ResourceType {
+        SPARK;

Review comment:
       ```suggestion
           UNKNOWN,
           SPARK;
   ```

##########
File path: fe/src/main/java/org/apache/doris/catalog/Resource.java
##########
@@ -0,0 +1,100 @@
+// 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.catalog;
+
+import org.apache.doris.analysis.CreateResourceStmt;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.io.Text;
+import org.apache.doris.common.io.Writable;
+import org.apache.doris.common.proc.BaseProcResult;
+import org.apache.doris.persist.gson.GsonUtils;
+
+import com.google.gson.annotations.SerializedName;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.Map;
+
+public abstract class Resource implements Writable {
+    public enum ResourceType {
+        SPARK;
+
+        public static ResourceType fromString(String resourceType) {
+            for (ResourceType type : ResourceType.values()) {
+                if (type.name().equalsIgnoreCase(resourceType)) {
+                    return type;
+                }
+            }
+            return null;

Review comment:
       ```suggestion
               return UNKNOWN;
   ```




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to