Skip to content

Commit b5a8b47

Browse files
authored
Support using long to compare timestamp type in expression
1 parent 8a84787 commit b5a8b47

File tree

3 files changed

+95
-15
lines changed

3 files changed

+95
-15
lines changed

integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/query/IoTDBInTableIT.java

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import static org.apache.iotdb.db.it.utils.TestUtils.defaultFormatDataTime;
4848
import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
4949
import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
50+
import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
5051
import static org.junit.Assert.fail;
5152

5253
@RunWith(IoTDBTestRunner.class)
@@ -57,19 +58,19 @@ public class IoTDBInTableIT {
5758
new String[] {
5859
"CREATE DATABASE " + DATABASE_NAME,
5960
"USE " + DATABASE_NAME,
60-
"CREATE TABLE sg(device1 STRING ID, device2 STRING ID, qrcode TEXT MEASUREMENT, date_v DATE MEASUREMENT, blob_v BLOB MEASUREMENT)",
61-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465600000,'d1','s1','qrcode001', '2024-08-01', X'abc0')",
62-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465660000,'d1','s1','qrcode002', '2024-08-02', X'abc1')",
63-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465720000,'d1','s1','qrcode003', '2024-08-03', X'abc2')",
64-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465780000,'d1','s1','qrcode004', '2024-08-04', X'abc3')",
65-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465720000,'d1','s2','qrcode002', '2024-08-05', X'abc4')",
66-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465780000,'d1','s2','qrcode003', '2024-08-06', X'abc5')",
67-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465840000,'d1','s2','qrcode004', '2024-08-07', X'abc6')",
68-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465900000,'d1','s2','qrcode005', '2024-08-08', X'abc7')",
69-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465780000,'d2','s1','qrcode002', '2024-08-09', X'abc8')",
70-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465840000,'d2','s1','qrcode003', '2024-08-10', X'abc9')",
71-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465900000,'d2','s1','qrcode004', '2024-08-11', X'abca')",
72-
"insert into sg(time,device1,device2,qrcode,date_v,blob_v) values(1509465960000,'d2','s1','qrcode005', '2024-08-12', X'abcb')",
61+
"CREATE TABLE sg(device1 STRING ID, device2 STRING ID, qrcode TEXT MEASUREMENT, date_v DATE MEASUREMENT, blob_v BLOB MEASUREMENT, timestamp_v TIMESTAMP MEASUREMENT)",
62+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465600000,'d1','s1','qrcode001', '2024-08-01', X'abc0',1509465600000)",
63+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465660000,'d1','s1','qrcode002', '2024-08-02', X'abc1',1509465660000)",
64+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465720000,'d1','s1','qrcode003', '2024-08-03', X'abc2',1509465720000)",
65+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465780000,'d1','s1','qrcode004', '2024-08-04', X'abc3',1509465780000)",
66+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465720000,'d1','s2','qrcode002', '2024-08-05', X'abc4',1509465720000)",
67+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465780000,'d1','s2','qrcode003', '2024-08-06', X'abc5',1509465780000)",
68+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465840000,'d1','s2','qrcode004', '2024-08-07', X'abc6',1509465840000)",
69+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465900000,'d1','s2','qrcode005', '2024-08-08', X'abc7',1509465900000)",
70+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465780000,'d2','s1','qrcode002', '2024-08-09', X'abc8',1509465780000)",
71+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465840000,'d2','s1','qrcode003', '2024-08-10', X'abc9',1509465840000)",
72+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465900000,'d2','s1','qrcode004', '2024-08-11', X'abca',1509465900000)",
73+
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v) values(1509465960000,'d2','s1','qrcode005', '2024-08-12', X'abcb',1509465960000)",
7374
"CREATE TABLE table1(device STRING ID, s1 INT32 MEASUREMENT, s2 INT64 MEASUREMENT, s3 FLOAT MEASUREMENT, s4 DOUBLE MEASUREMENT, s5 BOOLEAN MEASUREMENT)",
7475
};
7576

@@ -215,6 +216,68 @@ public void selectWithAlignByDeviceTest() {
215216
}
216217
}
217218

219+
@Test
220+
public void testTimestampIn() {
221+
// select time,device1,device2,timestamp_v from sg where timestamp_v in (1509465600000)
222+
// 1509465600000,'d1','s1',
223+
String[] expectedHeader = new String[] {"time", "device1", "device2", "timestamp_v"};
224+
String[] retArray =
225+
new String[] {
226+
"2017-10-31T16:00:00.000Z,d1,s1,2017-10-31T16:00:00.000Z,",
227+
};
228+
tableResultSetEqualTest(
229+
"select time,device1,device2,timestamp_v from sg where timestamp_v in (1509465600000)",
230+
expectedHeader,
231+
retArray,
232+
DATABASE_NAME);
233+
234+
tableResultSetEqualTest(
235+
"select time,device1,device2,timestamp_v from sg where timestamp_v=1509465600000",
236+
expectedHeader,
237+
retArray,
238+
DATABASE_NAME);
239+
240+
tableResultSetEqualTest(
241+
"select time,device1,device2,timestamp_v from sg where timestamp_v=2017-11-01T00:00:00.000+08:00",
242+
expectedHeader,
243+
retArray,
244+
DATABASE_NAME);
245+
tableResultSetEqualTest(
246+
"select time,device1,device2,timestamp_v from sg where timestamp_v=CAST('2017-11-01T00:00:00.000+08:00' AS TIMESTAMP)",
247+
expectedHeader,
248+
retArray,
249+
DATABASE_NAME);
250+
251+
tableResultSetEqualTest(
252+
"select time,device1,device2,timestamp_v from sg where timestamp_v in (1509465600000.0)",
253+
expectedHeader,
254+
retArray,
255+
DATABASE_NAME);
256+
257+
tableResultSetEqualTest(
258+
"select time,device1,device2,timestamp_v from sg where timestamp_v=1509465600000.0",
259+
expectedHeader,
260+
retArray,
261+
DATABASE_NAME);
262+
263+
retArray =
264+
new String[] {
265+
"2017-10-31T16:03:00.000Z,d1,s1,2017-10-31T16:03:00.000Z,",
266+
"2017-10-31T16:03:00.000Z,d1,s2,2017-10-31T16:03:00.000Z,",
267+
"2017-10-31T16:04:00.000Z,d1,s2,2017-10-31T16:04:00.000Z,",
268+
"2017-10-31T16:05:00.000Z,d1,s2,2017-10-31T16:05:00.000Z,",
269+
"2017-10-31T16:03:00.000Z,d2,s1,2017-10-31T16:03:00.000Z,",
270+
"2017-10-31T16:04:00.000Z,d2,s1,2017-10-31T16:04:00.000Z,",
271+
"2017-10-31T16:05:00.000Z,d2,s1,2017-10-31T16:05:00.000Z,",
272+
"2017-10-31T16:06:00.000Z,d2,s1,2017-10-31T16:06:00.000Z,",
273+
};
274+
tableResultSetEqualTest(
275+
"select time,device1,device2,timestamp_v from sg where timestamp_v not in (2017-11-01T00:00:00.000+08:00,1509465660000.0,CAST('2017-11-01T00:02:00.000+08:00' AS TIMESTAMP)) order by device1,device2,time",
276+
expectedHeader,
277+
retArray,
278+
DATABASE_NAME);
279+
}
280+
218281
private List<Integer> checkHeader(
219282
ResultSetMetaData resultSetMetaData, String expectedHeaderStrings, int[] expectedTypes)
220283
throws SQLException {

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/relational/ColumnTransformerBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,17 @@ private static InMultiColumnTransformer constructInColumnTransformer(
10731073
Set<Long> timestampSet = new HashSet<>();
10741074
for (Literal value : values) {
10751075
try {
1076-
timestampSet.add(Long.parseLong(((GenericLiteral) value).getValue()));
1076+
if (value instanceof LongLiteral) {
1077+
timestampSet.add(((LongLiteral) value).getParsedValue());
1078+
} else if (value instanceof DoubleLiteral) {
1079+
timestampSet.add((long) ((DoubleLiteral) value).getValue());
1080+
} else if (value instanceof GenericLiteral) {
1081+
timestampSet.add(Long.parseLong(((GenericLiteral) value).getValue()));
1082+
} else {
1083+
throw new SemanticException(
1084+
"InList Literal for TIMESTAMP can only be LongLiteral, DoubleLiteral and GenericLiteral, current is "
1085+
+ value.getClass().getSimpleName());
1086+
}
10771087
} catch (IllegalArgumentException e) {
10781088
throw new SemanticException(String.format(errorMsg, value, childType));
10791089
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.iotdb.db.queryengine.plan.relational.analyzer.predicate;
2121

2222
import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
23+
import org.apache.iotdb.db.exception.sql.SemanticException;
2324
import org.apache.iotdb.db.queryengine.plan.relational.metadata.ColumnSchema;
2425
import org.apache.iotdb.db.queryengine.plan.relational.planner.Symbol;
2526
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.BetweenPredicate;
@@ -374,8 +375,14 @@ public static Integer getDateValue(Expression expression) {
374375
public static Long getTimestampValue(Expression expression) {
375376
if (expression instanceof LongLiteral) {
376377
return ((LongLiteral) expression).getParsedValue();
377-
} else {
378+
} else if (expression instanceof DoubleLiteral) {
379+
return (long) ((DoubleLiteral) expression).getValue();
380+
} else if (expression instanceof GenericLiteral) {
378381
return Long.valueOf(((GenericLiteral) expression).getValue());
382+
} else {
383+
throw new SemanticException(
384+
"InList Literal for TIMESTAMP can only be LongLiteral, DoubleLiteral and GenericLiteral, current is "
385+
+ expression.getClass().getSimpleName());
379386
}
380387
}
381388

0 commit comments

Comments
 (0)