Skip to content

Commit 36154e5

Browse files
authored
Optimized the error message when the column is not tag/attribute in device related SQLs (#16750)
* logic * may-fi * fix * fix * Update IoTDBDeviceIT.java * fixfix * fix * fix * h * coding
1 parent 9121208 commit 36154e5

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBDeviceIT.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Collections;
3939

4040
import static org.junit.Assert.assertEquals;
41+
import static org.junit.Assert.assertTrue;
4142
import static org.junit.Assert.fail;
4243

4344
@RunWith(IoTDBTestRunner.class)
@@ -163,20 +164,11 @@ public void testDevice() throws SQLException {
163164
assertEquals("550: Table 'test.table2' does not exist.", e.getMessage());
164165
}
165166

166-
try {
167-
statement.executeQuery("show devices from table0 where temperature = 37.6");
168-
fail("Show devices shall fail for measurement predicate");
169-
} catch (final Exception e) {
170-
assertEquals(
171-
"701: The TIME/FIELD columns are currently not allowed in devices related operations",
172-
e.getMessage());
173-
}
174-
175167
try {
176168
statement.executeQuery("count devices from table0 where a = 1");
177169
fail("Count devices shall fail for non-exist column");
178170
} catch (final Exception e) {
179-
assertEquals("616: Column 'a' cannot be resolved", e.getMessage());
171+
assertEquals("701: Column 'a' is not an attribute or tag column", e.getMessage());
180172
}
181173

182174
// Test fully qualified name
@@ -224,6 +216,34 @@ public void testDevice() throws SQLException {
224216
assertEquals("701: Update's attribute value must be STRING, TEXT or null.", e.getMessage());
225217
}
226218

219+
try {
220+
statement.execute("show devices from table0 where humidity = 1");
221+
fail("Update shall fail for non-tag/attribute columns");
222+
} catch (final Exception e) {
223+
assertEquals("701: Column 'humidity' is not an attribute or tag column", e.getMessage());
224+
}
225+
226+
try {
227+
statement.execute("count devices from table0 where humidity = 1");
228+
fail("Update shall fail for non-tag/attribute columns");
229+
} catch (final Exception e) {
230+
assertEquals("701: Column 'humidity' is not an attribute or tag column", e.getMessage());
231+
}
232+
233+
try {
234+
statement.execute("update table0 set model = '1' where humidity = 1");
235+
fail("Update shall fail for non-tag/attribute columns");
236+
} catch (final Exception e) {
237+
assertEquals("701: Column 'humidity' is not an attribute or tag column", e.getMessage());
238+
}
239+
240+
try {
241+
statement.execute("update table0 set model = humidity");
242+
fail("Update shall fail for non-tag/attribute columns");
243+
} catch (final Exception e) {
244+
assertTrue(e.getMessage().contains("Column 'humidity' is not an attribute or tag column"));
245+
}
246+
227247
// Test filter with no effect
228248
statement.execute("update table0 set model = null where model = 'A' and model = 'B'");
229249

@@ -270,9 +290,7 @@ public void testDevice() throws SQLException {
270290
statement.executeQuery("delete devices from table0 where time = 1");
271291
fail("Delete devices shall fail when specifies non tag column");
272292
} catch (final Exception e) {
273-
assertEquals(
274-
"701: The TIME/FIELD columns are currently not allowed in devices related operations",
275-
e.getMessage());
293+
assertEquals("701: Column 'time' is not an attribute or tag column", e.getMessage());
276294
}
277295
}
278296
}

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.apache.iotdb.db.queryengine.plan.relational.analyzer;
2121

22+
import org.apache.iotdb.commons.exception.IoTDBException;
2223
import org.apache.iotdb.commons.schema.table.TsTable;
2324
import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
2425
import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
@@ -550,9 +551,24 @@ protected Scope visitUpdate(final Update node, final Optional<Scope> context) {
550551
}
551552
attributeNames.add((SymbolReference) parsedColumn);
552553

553-
final Pair<Type, Expression> expressionPair =
554-
analyzeAndRewriteExpression(
555-
translationMap, translationMap.getScope(), assignment.getValue());
554+
final Pair<Type, Expression> expressionPair;
555+
try {
556+
expressionPair =
557+
analyzeAndRewriteExpression(
558+
translationMap, translationMap.getScope(), assignment.getValue());
559+
} catch (final Exception e) {
560+
if (e.getMessage().contains("cannot be resolved")) {
561+
throw new SemanticException(
562+
new IoTDBException(
563+
e.getCause()
564+
.getMessage()
565+
.replace(
566+
"cannot be resolved",
567+
"is not an attribute or tag column"),
568+
TSStatusCode.SEMANTIC_ERROR.getStatusCode()));
569+
}
570+
throw e;
571+
}
556572
if (!expressionPair.getLeft().equals(StringType.STRING)
557573
&& !expressionPair.getLeft().equals(BinaryType.TEXT)
558574
&& !expressionPair.getLeft().equals(UnknownType.UNKNOWN)) {
@@ -4545,11 +4561,24 @@ private TranslationMap analyzeTraverseDevice(
45454561

45464562
final TableSchema originalSchema = tableSchema.get();
45474563
final ImmutableList.Builder<Field> fields = ImmutableList.builder();
4564+
// We only leave attribute & tag here because the others are not allowed in device related
4565+
// SQLs
45484566
fields.addAll(
45494567
analyzeTableOutputFields(
45504568
node.getTable(),
45514569
name,
4552-
new TableSchema(originalSchema.getTableName(), originalSchema.getColumns())));
4570+
new TableSchema(
4571+
originalSchema.getTableName(),
4572+
originalSchema.getColumns().stream()
4573+
.filter(
4574+
columnSchema ->
4575+
columnSchema
4576+
.getColumnCategory()
4577+
.equals(TsTableColumnCategory.ATTRIBUTE)
4578+
|| columnSchema
4579+
.getColumnCategory()
4580+
.equals(TsTableColumnCategory.TAG))
4581+
.collect(Collectors.toList()))));
45534582
final List<Field> fieldList = fields.build();
45544583
final Scope scope = createAndAssignScope(node, context, fieldList);
45554584
translationMap =
@@ -4563,7 +4592,19 @@ private TranslationMap analyzeTraverseDevice(
45634592
new PlannerContext(metadata, null));
45644593

45654594
if (node.getWhere().isPresent()) {
4566-
analyzeWhere(node, translationMap.getScope(), node.getWhere().get());
4595+
try {
4596+
analyzeWhere(node, translationMap.getScope(), node.getWhere().get());
4597+
} catch (final Throwable e) {
4598+
if (e instanceof SemanticException && e.getMessage().contains("cannot be resolved")) {
4599+
throw new SemanticException(
4600+
new IoTDBException(
4601+
e.getCause()
4602+
.getMessage()
4603+
.replace("cannot be resolved", "is not an attribute or tag column"),
4604+
TSStatusCode.SEMANTIC_ERROR.getStatusCode()));
4605+
}
4606+
throw e;
4607+
}
45674608
node.setWhere(translationMap.rewrite(analysis.getWhere(node)));
45684609
}
45694610
}

0 commit comments

Comments
 (0)