This is an automated email from the ASF dual-hosted git repository.
jackietien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 0422d48006 [IOTDB-3470] [IOTDB-3471] Add some semantic checks for
InsertStatement (#6298)
0422d48006 is described below
commit 0422d480064697355495aa18c749d296744d4dd4
Author: liuminghui233 <[email protected]>
AuthorDate: Thu Jun 16 09:43:18 2022 +0800
[IOTDB-3470] [IOTDB-3471] Add some semantic checks for InsertStatement
(#6298)
---
.../db/it/aligned/IoTDBInsertAlignedValuesIT.java | 25 ++++++++++++-------
.../apache/iotdb/db/mpp/plan/analyze/Analyzer.java | 2 +-
.../mpp/plan/statement/crud/InsertStatement.java | 29 ++++++++++++++++++++++
3 files changed, 46 insertions(+), 10 deletions(-)
diff --git
a/integration-test/src/test/java/org/apache/iotdb/db/it/aligned/IoTDBInsertAlignedValuesIT.java
b/integration-test/src/test/java/org/apache/iotdb/db/it/aligned/IoTDBInsertAlignedValuesIT.java
index 0c6894a21c..a9268b0d17 100644
---
a/integration-test/src/test/java/org/apache/iotdb/db/it/aligned/IoTDBInsertAlignedValuesIT.java
+++
b/integration-test/src/test/java/org/apache/iotdb/db/it/aligned/IoTDBInsertAlignedValuesIT.java
@@ -27,7 +27,6 @@ import org.apache.iotdb.itbase.category.LocalStandaloneIT;
import org.junit.After;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
@@ -223,23 +222,33 @@ public class IoTDBInsertAlignedValuesIT {
}
}
- @Test(expected = Exception.class)
+ @Test
public void testInsertWithWrongMeasurementNum1() throws SQLException {
try (Connection connection = EnvFactory.getEnv().getConnection();
Statement statement = connection.createStatement()) {
statement.execute(
"insert into root.t1.wf01.wt01(time, status, temperature) aligned
values(11000, 100)");
+ fail();
+ } catch (SQLException e) {
+ assertTrue(
+ e.getMessage()
+ .contains(
+ "the measurementList's size 2 is not consistent with the
valueList's size 1"));
}
}
- // TODO remove Ignore annotation while fixing this bug
- @Ignore
- @Test(expected = Exception.class)
- public void testInsertWithWrongMeasurementNum2() throws SQLException {
+ @Test
+ public void testInsertWithWrongMeasurementNum2() {
try (Connection connection = EnvFactory.getEnv().getConnection();
Statement statement = connection.createStatement()) {
statement.execute(
"insert into root.t1.wf01.wt01(time, status, temperature) aligned
values(11000, 100, 300, 400)");
+ fail();
+ } catch (SQLException e) {
+ assertTrue(
+ e.getMessage()
+ .contains(
+ "the measurementList's size 2 is not consistent with the
valueList's size 3"));
}
}
@@ -268,8 +277,6 @@ public class IoTDBInsertAlignedValuesIT {
}
}
- // TODO remove Ignore annotation while fixing this bug
- @Ignore
@Test
public void testInsertWithDuplicatedMeasurements() {
try (Connection connection = EnvFactory.getEnv().getConnection();
@@ -278,7 +285,7 @@ public class IoTDBInsertAlignedValuesIT {
"insert into root.t1.wf01.wt01(time, s3, status, status) aligned
values(100, true, 20.1, 20.2)");
fail();
} catch (SQLException e) {
- assertEquals("411: Insertion contains duplicated measurement: status",
e.getMessage());
+ assertTrue(e.getMessage().contains("Insertion contains duplicated
measurement: status"));
}
}
}
diff --git
a/server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/Analyzer.java
b/server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/Analyzer.java
index 834ee25932..9cf383cd8a 100644
--- a/server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/Analyzer.java
+++ b/server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/Analyzer.java
@@ -880,7 +880,7 @@ public class Analyzer {
@Override
public Analysis visitInsert(InsertStatement insertStatement,
MPPQueryContext context) {
context.setQueryType(QueryType.WRITE);
-
+ insertStatement.semanticCheck();
long[] timeArray = insertStatement.getTimes();
PartialPath devicePath = insertStatement.getDevice();
String[] measurements = insertStatement.getMeasurementList();
diff --git
a/server/src/main/java/org/apache/iotdb/db/mpp/plan/statement/crud/InsertStatement.java
b/server/src/main/java/org/apache/iotdb/db/mpp/plan/statement/crud/InsertStatement.java
index 0f4efab632..464e5ff7c2 100644
---
a/server/src/main/java/org/apache/iotdb/db/mpp/plan/statement/crud/InsertStatement.java
+++
b/server/src/main/java/org/apache/iotdb/db/mpp/plan/statement/crud/InsertStatement.java
@@ -20,12 +20,16 @@
package org.apache.iotdb.db.mpp.plan.statement.crud;
import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.db.exception.sql.SemanticException;
import org.apache.iotdb.db.mpp.plan.constant.StatementType;
import org.apache.iotdb.db.mpp.plan.statement.Statement;
import org.apache.iotdb.db.mpp.plan.statement.StatementVisitor;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
/** this class extends {@code Statement} and process insert statement. */
public class InsertStatement extends Statement {
@@ -98,4 +102,29 @@ public class InsertStatement extends Statement {
public <R, C> R accept(StatementVisitor<R, C> visitor, C context) {
return visitor.visitInsert(this, context);
}
+
+ public void semanticCheck() {
+ Set<String> deduplicatedMeasurements = new HashSet<>();
+ for (String measurement : measurementList) {
+ if (measurement == null || measurement.isEmpty()) {
+ throw new SemanticException(
+ "Measurement contains null or empty string: " +
Arrays.toString(measurementList));
+ }
+ if (deduplicatedMeasurements.contains(measurement)) {
+ throw new SemanticException("Insertion contains duplicated
measurement: " + measurement);
+ } else {
+ deduplicatedMeasurements.add(measurement);
+ }
+ }
+
+ int measurementsNum = measurementList.length;
+ for (int i = 0; i < times.length; i++) {
+ if (measurementsNum != valuesList.get(i).length) {
+ throw new SemanticException(
+ String.format(
+ "the measurementList's size %d is not consistent with the
valueList's size %d",
+ measurementsNum, valuesList.get(i).length));
+ }
+ }
+ }
}