morningman commented on code in PR #9786:
URL: https://github.com/apache/incubator-doris/pull/9786#discussion_r884050084
##########
fe/fe-core/src/main/java/org/apache/doris/policy/Policy.java:
##########
@@ -18,79 +18,61 @@
package org.apache.doris.policy;
import org.apache.doris.analysis.CreatePolicyStmt;
-import org.apache.doris.analysis.Expr;
-import org.apache.doris.analysis.SqlParser;
-import org.apache.doris.analysis.SqlScanner;
import org.apache.doris.analysis.UserIdentity;
import org.apache.doris.catalog.Catalog;
import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.Table;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.io.Text;
import org.apache.doris.common.io.Writable;
-import org.apache.doris.common.util.SqlParserUtils;
import org.apache.doris.persist.gson.GsonPostProcessable;
import org.apache.doris.persist.gson.GsonUtils;
import org.apache.doris.qe.ConnectContext;
-import com.google.common.collect.Lists;
import com.google.gson.annotations.SerializedName;
-import lombok.AllArgsConstructor;
import lombok.Data;
+import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
-import java.io.StringReader;
import java.util.List;
/**
* Save policy for filtering data.
**/
@Data
-@AllArgsConstructor
-public class Policy implements Writable, GsonPostProcessable {
-
- public static final String ROW_POLICY = "ROW";
+public abstract class Policy implements Writable, GsonPostProcessable {
Review Comment:
Modify the comment of this class
##########
fe/fe-core/src/main/cup/sql_parser.cup:
##########
@@ -1354,7 +1354,7 @@ create_stmt ::=
{:
RESULT = new CreateSqlBlockRuleStmt(ruleName, properties);
:}
- /* row policy */
+ /* table policy */
Review Comment:
This is indeed a row policy, why change to table policy in comment?
##########
fe/fe-core/src/main/java/org/apache/doris/policy/TablePolicy.java:
##########
@@ -0,0 +1,166 @@
+// 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.policy;
+
+import org.apache.doris.analysis.CreatePolicyStmt;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.SqlParser;
+import org.apache.doris.analysis.SqlScanner;
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.io.Text;
+import org.apache.doris.common.util.SqlParserUtils;
+import org.apache.doris.persist.gson.GsonUtils;
+
+import com.google.common.collect.Lists;
+import com.google.gson.annotations.SerializedName;
+import lombok.Data;
+import org.apache.commons.lang.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.DataInput;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.List;
+
+/**
+ * Save policy for filtering data.
+ **/
+@Data
+public class TablePolicy extends Policy {
+
+ private static final Logger LOG = LogManager.getLogger(TablePolicy.class);
+
+ /**
+ * Policy bind user.
+ **/
+ @SerializedName(value = "user")
+ private UserIdentity user = null;
+
+ @SerializedName(value = "dbId")
+ private long dbId = -1;
+
+ @SerializedName(value = "tableId")
+ private long tableId = -1;
+
+ /**
+ * PERMISSIVE | RESTRICTIVE, If multiple types exist, the last type
prevails.
+ **/
+ @SerializedName(value = "filterType")
+ private FilterType filterType = null;
+
+ private Expr wherePredicate = null;
+
+ public TablePolicy() {}
+
+ /**
+ * Policy for Table. Policy of ROW or others.
+ *
+ * @param type PolicyType
+ * @param policyName policy name
+ * @param dbId database i
+ * @param user username
+ * @param originStmt origin stmt
+ * @param tableId table id
+ * @param filterType filter type
+ * @param wherePredicate where predicate
+ */
+ public TablePolicy(final PolicyTypeEnum type, final String policyName,
long dbId,
+ UserIdentity user, String originStmt, final long tableId,
+ final FilterType filterType, final Expr wherePredicate) {
+ super(type, policyName, originStmt);
+ this.user = user;
+ this.dbId = dbId;
+ this.tableId = tableId;
+ this.filterType = filterType;
+ this.wherePredicate = wherePredicate;
+ }
+
+ /**
+ * Use for SHOW POLICY.
+ **/
+ public List<String> getShowInfo() throws AnalysisException {
+ Database database =
Catalog.getCurrentCatalog().getDbOrAnalysisException(this.dbId);
+ Table table = database.getTableOrAnalysisException(this.tableId);
+ return Lists.newArrayList(this.policyName, database.getFullName(),
table.getName(), this.type.name(),
+ this.filterType.name(), this.wherePredicate.toSql(),
this.user.getQualifiedUser(), this.originStmt);
+ }
+
+ /**
+ * Read Table Policy from file.
+ **/
+ public static TablePolicy read(DataInput in) throws IOException {
+ String json = Text.readString(in);
+ return GsonUtils.GSON.fromJson(json, TablePolicy.class);
+ }
+
+ @Override
+ public void gsonPostProcess() throws IOException {
+ if (wherePredicate != null) {
+ return;
+ }
+ try {
+ SqlScanner input = new SqlScanner(new StringReader(originStmt),
0L);
+ SqlParser parser = new SqlParser(input);
+ CreatePolicyStmt stmt = (CreatePolicyStmt)
SqlParserUtils.getFirstStmt(parser);
+ wherePredicate = stmt.getWherePredicate();
+ } catch (Exception e) {
+ throw new IOException("table policy parse originStmt error", e);
+ }
+ }
+
+ @Override
+ public TablePolicy clone() {
+ return new TablePolicy(this.type, this.policyName, this.dbId,
this.user, this.originStmt, this.tableId,
+ this.filterType, this.wherePredicate);
+ }
+
+ private boolean checkMatched(long dbId, long tableId, PolicyTypeEnum type,
+ String policyName, UserIdentity user) {
+ return super.checkMatched(type, policyName)
+ && (dbId == -1 || dbId == this.dbId)
+ && (tableId == -1 || tableId == this.tableId)
+ && (user == null || this.user == null
+ || StringUtils.equals(user.getQualifiedUser(),
this.user.getQualifiedUser()));
+ }
+
+ @Override
+ public boolean matchPolicy(Policy policy) {
+ if (!(policy instanceof TablePolicy)) {
+ return false;
+ }
+ TablePolicy tablePolicy = (TablePolicy) policy;
+ return checkMatched(tablePolicy.getDbId(), tablePolicy.getTableId(),
tablePolicy.getType(),
+ tablePolicy.getPolicyName(),
tablePolicy.getUser());
+ }
+
+ @Override
+ public boolean matchPolicy(DropPolicyLog dropPolicyLog) {
Review Comment:
This method name is strange
##########
fe/fe-core/src/main/java/org/apache/doris/policy/Policy.java:
##########
@@ -18,79 +18,61 @@
package org.apache.doris.policy;
import org.apache.doris.analysis.CreatePolicyStmt;
-import org.apache.doris.analysis.Expr;
-import org.apache.doris.analysis.SqlParser;
-import org.apache.doris.analysis.SqlScanner;
import org.apache.doris.analysis.UserIdentity;
import org.apache.doris.catalog.Catalog;
import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.Table;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.io.Text;
import org.apache.doris.common.io.Writable;
-import org.apache.doris.common.util.SqlParserUtils;
import org.apache.doris.persist.gson.GsonPostProcessable;
import org.apache.doris.persist.gson.GsonUtils;
import org.apache.doris.qe.ConnectContext;
-import com.google.common.collect.Lists;
import com.google.gson.annotations.SerializedName;
-import lombok.AllArgsConstructor;
import lombok.Data;
+import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
-import java.io.StringReader;
import java.util.List;
/**
* Save policy for filtering data.
**/
@Data
-@AllArgsConstructor
-public class Policy implements Writable, GsonPostProcessable {
-
- public static final String ROW_POLICY = "ROW";
+public abstract class Policy implements Writable, GsonPostProcessable {
private static final Logger LOG = LogManager.getLogger(Policy.class);
- @SerializedName(value = "dbId")
- private long dbId;
-
- @SerializedName(value = "tableId")
- private long tableId;
+ @SerializedName(value = "type")
+ protected PolicyTypeEnum type = null;
@SerializedName(value = "policyName")
- private String policyName;
-
- /**
- * ROW.
- **/
- @SerializedName(value = "type")
- private PolicyTypeEnum type;
+ protected String policyName = null;
/**
- * PERMISSIVE | RESTRICTIVE, If multiple types exist, the last type
prevails.
+ * Use for Serialization/deserialization.
**/
- @SerializedName(value = "filterType")
- private final FilterType filterType;
-
- private Expr wherePredicate;
+ @SerializedName(value = "originStmt")
+ protected String originStmt;
Review Comment:
Not all policy need to save origin stmt. I think we can move this field to
TablePolicy.
For row policy, we save origin stmt only because we can not serialize the
filter expr directly.
But for storage policy, there is only key value properties that can be
serialized directly.
##########
fe/fe-core/src/main/java/org/apache/doris/policy/Policy.java:
##########
@@ -101,54 +83,42 @@ public static Policy fromCreateStmt(CreatePolicyStmt stmt)
throws AnalysisExcept
curDb = ConnectContext.get().getDatabase();
}
Database db =
Catalog.getCurrentCatalog().getDbOrAnalysisException(curDb);
- Table table =
db.getTableOrAnalysisException(stmt.getTableName().getTbl());
UserIdentity userIdent = stmt.getUser();
userIdent.analyze(ConnectContext.get().getClusterName());
- return new Policy(db.getId(), table.getId(), stmt.getPolicyName(),
stmt.getType(), stmt.getFilterType(),
- stmt.getWherePredicate(), userIdent,
stmt.getOrigStmt().originStmt);
+ switch (stmt.getType()) {
+ case ROW:
+ default:
+ Table table =
db.getTableOrAnalysisException(stmt.getTableName().getTbl());
+ return new TablePolicy(stmt.getType(), stmt.getPolicyName(),
db.getId(), userIdent,
+ stmt.getOrigStmt().originStmt, table.getId(),
stmt.getFilterType(),
+ stmt.getWherePredicate());
+ }
}
/**
* Use for SHOW POLICY.
**/
- public List<String> getShowInfo() throws AnalysisException {
- Database database =
Catalog.getCurrentCatalog().getDbOrAnalysisException(this.dbId);
- Table table = database.getTableOrAnalysisException(this.tableId);
- return Lists.newArrayList(this.policyName, database.getFullName(),
table.getName(), this.type.name(),
- this.filterType.name(), this.wherePredicate.toSql(),
this.user.getQualifiedUser(), this.originStmt);
- }
+ public abstract List<String> getShowInfo() throws AnalysisException;
@Override
public void write(DataOutput out) throws IOException {
Text.writeString(out, GsonUtils.GSON.toJson(this));
}
- /**
- * Read policy from file.
- **/
- public static Policy read(DataInput in) throws IOException {
- String json = Text.readString(in);
- return GsonUtils.GSON.fromJson(json, Policy.class);
+ protected boolean checkMatched(PolicyTypeEnum type, String policyName) {
+ return (type == null || type.equals(this.type))
Review Comment:
the `type` and `policyname` should not be null.
--
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]