starocean999 commented on code in PR #22673:
URL: https://github.com/apache/doris/pull/22673#discussion_r1285779604


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/DeleteStmt.java:
##########
@@ -281,6 +288,123 @@ void analyzePredicate(Expr predicate, Analyzer analyzer) 
throws AnalysisExceptio
         }
     }
 
+    private void checkDeleteConditions() throws AnalysisException {
+        // check condition column is key column and condition value
+        // Here we use "getFullSchema()" to get all columns including VISIBLE 
and SHADOW columns
+
+        // we ensure the db and table exists.
+        Database db = (Database) 
Env.getCurrentEnv().getCurrentCatalog().getDb(getDbName()).get();
+        OlapTable table = ((OlapTable) db.getTable(getTableName()).get());
+
+        Map<String, Column> nameToColumn = 
Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER);
+        for (Column column : table.getFullSchema()) {
+            nameToColumn.put(column.getName(), column);
+        }
+
+        for (Predicate condition : deleteConditions) {
+            SlotRef slotRef = getSlotRef(condition);
+            String columnName = slotRef.getColumnName();
+            if (!nameToColumn.containsKey(columnName)) {
+                throw new AnalysisException(String.format("Unknown column '%s' 
in '%s'", columnName, table.getName()));
+            }
+
+            if (Column.isShadowColumn(columnName)) {
+                throw new AnalysisException("Can not apply delete condition to 
shadow column");
+            }
+
+            // Check if this column is under schema change, if yes, there will 
be a shadow column related to it.
+            // And we don't allow doing delete operation when a condition 
column is under schema change.
+            String shadowColName = Column.getShadowName(columnName);
+            if (nameToColumn.containsKey(shadowColName)) {
+                throw new AnalysisException(String.format("Column '%s' is 
under"
+                        + " schema change operation. Do not allow delete 
operation", columnName));
+            }
+
+            Column column = nameToColumn.get(columnName);
+            // Due to rounding errors, most floating-point numbers end up 
being slightly imprecise,
+            // it also means that numbers expected to be equal often differ 
slightly, so we do not allow compare with
+            // floating-point numbers, floating-point number not allowed in 
where clause
+            if (!column.isKey() && table.getKeysType() != KeysType.DUP_KEYS
+                    || column.getDataType().isFloatingPointType()) {
+                throw new AnalysisException("Column[" + columnName + "] is not 
key column or storage model "
+                        + "is not duplicate or column type is float or 
double.");
+            }
+
+            if (condition instanceof BinaryPredicate) {
+                String value = null;
+                try {
+                    BinaryPredicate binaryPredicate = (BinaryPredicate) 
condition;
+                    // if a bool cond passed to be, be's zone_map cannot 
handle bool correctly,
+                    // change it to a tinyint type here;
+                    value = binaryPredicate.getChild(1).getStringValue();
+                    if (column.getDataType() == PrimitiveType.BOOLEAN) {
+                        if (value.equalsIgnoreCase("true")) {
+                            binaryPredicate.setChild(1, 
LiteralExpr.create("1", Type.TINYINT));
+                        } else if (value.equalsIgnoreCase("false")) {
+                            binaryPredicate.setChild(1, 
LiteralExpr.create("0", Type.TINYINT));
+                        }
+                    } else if (column.getDataType() == PrimitiveType.DATE
+                            || column.getDataType() == PrimitiveType.DATETIME
+                            || column.getDataType() == PrimitiveType.DATEV2) {
+                        DateLiteral dateLiteral = new DateLiteral(value, 
Type.fromPrimitiveType(column.getDataType()));
+                        value = dateLiteral.getStringValue();
+                        binaryPredicate.setChild(1, LiteralExpr.create(value,
+                                Type.fromPrimitiveType(column.getDataType())));
+                    } else if (column.getDataType() == 
PrimitiveType.DATETIMEV2) {
+                        DateLiteral dateLiteral = new DateLiteral(value,
+                                
ScalarType.createDatetimeV2Type(ScalarType.MAX_DATETIMEV2_SCALE));
+                        value = dateLiteral.getStringValue();
+                        binaryPredicate.setChild(1, LiteralExpr.create(value,
+                                
ScalarType.createDatetimeV2Type(ScalarType.MAX_DATETIMEV2_SCALE)));
+                    }
+                    LiteralExpr.create(value, column.getType());
+                } catch (AnalysisException e) {
+                    throw new AnalysisException("Invalid column value[" + 
value + "] for column " + columnName);
+                }
+            } else if (condition instanceof InPredicate) {
+                String value = null;
+                try {
+                    InPredicate inPredicate = (InPredicate) condition;
+                    for (int i = 1; i <= inPredicate.getInElementNum(); i++) {
+                        value = inPredicate.getChild(i).getStringValue();
+                        if (column.getDataType() == PrimitiveType.DATE
+                                || column.getDataType() == 
PrimitiveType.DATETIME
+                                || column.getDataType() == PrimitiveType.DATEV2
+                                || column.getDataType() == 
PrimitiveType.DATETIMEV2) {
+                            DateLiteral dateLiteral = new DateLiteral(value,
+                                    column.getType());
+                            value = dateLiteral.getStringValue();
+                            inPredicate.setChild(i, LiteralExpr.create(value,
+                                    column.getType()));
+                        } else {
+                            LiteralExpr.create(value, 
Type.fromPrimitiveType(column.getDataType()));
+                        }
+                    }
+                } catch (AnalysisException e) {
+                    throw new AnalysisException("Invalid column value[" + 
value + "] for column " + columnName);
+                }
+            }
+
+            // set schema column name
+            slotRef.setCol(column.getName());
+        }
+    }
+
+    private SlotRef getSlotRef(Predicate condition) {

Review Comment:
   The checkDeleteConditions only handle BinaryPredicate and InPredicate, how 
about the IsNullPredicate here, or other predicate like "delete from test1 
where NOT length(id) >= 2"



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

Reply via email to