Skip to content

Commit f608a2d

Browse files
authored
Adjust the exception message cause by the WHERE clause, which uses a range comparison on the same field, specifically when the left value of the range is greater than the right value of the range. (apache#16741)
1 parent 6e8748e commit f608a2d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBDeletionIT.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
package org.apache.iotdb.db.it;
2121

22+
import org.apache.iotdb.db.queryengine.plan.parser.ASTVisitor;
2223
import org.apache.iotdb.it.env.EnvFactory;
2324
import org.apache.iotdb.it.framework.IoTDBTestRunner;
2425
import org.apache.iotdb.itbase.category.ClusterIT;
2526
import org.apache.iotdb.itbase.category.LocalStandaloneIT;
27+
import org.apache.iotdb.jdbc.IoTDBSQLException;
2628
import org.apache.iotdb.rpc.TSStatusCode;
2729

2830
import org.junit.AfterClass;
@@ -467,6 +469,25 @@ public void testDelAfterUpdate() throws SQLException {
467469
}
468470
}
469471

472+
@Test
473+
public void testDeleteByRangeComparison() throws SQLException {
474+
try (Connection connection = EnvFactory.getEnv().getConnection();
475+
Statement statement = connection.createStatement()) {
476+
statement.execute("CREATE DATABASE root.test");
477+
statement.execute("CREATE ALIGNED TIMESERIES root.test.g_0.d2(s_0 int32)");
478+
statement.execute("INSERT INTO root.test.g_0.d_2(time,s_0) VALUES(1, 1)");
479+
480+
try {
481+
statement.execute("DELETE FROM root.test.g_0.d_2.s_0 WHERE time > 4 AND time < 0");
482+
} catch (IoTDBSQLException e) {
483+
Assert.assertEquals(
484+
e.getErrorCode() + ": " + ASTVisitor.DELETE_RANGE_COMPARISON_ERROR_MSG, e.getMessage());
485+
}
486+
487+
statement.execute("DROP DATABASE root.test");
488+
}
489+
}
490+
470491
private static void prepareSeries() {
471492
try (Connection connection = EnvFactory.getEnv().getConnection();
472493
Statement statement = connection.createStatement()) {

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/parser/ASTVisitor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
344344
private static final String LIMIT_CONFIGURATION_ENABLED_ERROR_MSG =
345345
"Limit configuration is not enabled, please enable it first.";
346346

347+
public static final String DELETE_RANGE_COMPARISON_ERROR_MSG =
348+
"For delete statement, where clause use a range comparison on the same field, the left value of the range cannot be greater than the right value of the range, it must be written like this : time > 5 and time < 10";
349+
347350
private static final String NODE_NAME_IN_INTO_PATH_MATCHER = "([a-zA-Z0-9_${}\\u2E80-\\u9FFF]+)";
348351
private static final Pattern NODE_NAME_IN_INTO_PATH_PATTERN =
349352
Pattern.compile(NODE_NAME_IN_INTO_PATH_MATCHER);
@@ -2993,9 +2996,12 @@ private TimeRange parseDeleteTimeRange(Expression predicate) {
29932996
parseDeleteTimeRange(((LogicAndExpression) predicate).getLeftExpression());
29942997
TimeRange rightTimeRange =
29952998
parseDeleteTimeRange(((LogicAndExpression) predicate).getRightExpression());
2996-
return new TimeRange(
2997-
Math.max(leftTimeRange.getMin(), rightTimeRange.getMin()),
2998-
Math.min(leftTimeRange.getMax(), rightTimeRange.getMax()));
2999+
long min = Math.max(leftTimeRange.getMin(), rightTimeRange.getMin());
3000+
long max = Math.min(leftTimeRange.getMax(), rightTimeRange.getMax());
3001+
if (min > max) {
3002+
throw new SemanticException(DELETE_RANGE_COMPARISON_ERROR_MSG);
3003+
}
3004+
return new TimeRange(min, max);
29993005
} else if (predicate instanceof CompareBinaryExpression) {
30003006
if (((CompareBinaryExpression) predicate).getLeftExpression() instanceof TimestampOperand) {
30013007
return parseTimeRangeForDeleteTimeRange(

0 commit comments

Comments
 (0)