This is an automated email from the ASF dual-hosted git repository. leirui pushed a commit to branch research/M4-visualization in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 48bfd3d54d11d0a126806baf3fba182f45ede0c4 Author: Lei Rui <[email protected]> AuthorDate: Sat Jun 17 14:59:33 2023 +0800 M4 udf add test --- .../iotdb/db/query/udf/builtin/UDTFM4MAC.java | 199 +++++++++++---------- .../apache/iotdb/db/integration/m4/MyTest3.java | 58 ++++++ .../apache/iotdb/db/integration/m4/MyTmpTest.java | 42 ++--- 3 files changed, 181 insertions(+), 118 deletions(-) diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFM4MAC.java b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFM4MAC.java index 4c3fd5b5690..7b2388af2d6 100644 --- a/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFM4MAC.java +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFM4MAC.java @@ -123,7 +123,7 @@ public class UDTFM4MAC implements UDTF { .setAccessStrategy(new RowByRowAccessStrategy()) .setOutputDataType(TSDataType.TEXT); init(); - this.idx = 0; + this.idx = -1; result = new String[w]; for (int i = 0; i < w; i++) { result[i] = "empty"; @@ -134,6 +134,9 @@ public class UDTFM4MAC implements UDTF { @Override public void transform(Row row, PointCollector collector) throws QueryProcessException, IOException { + if (idx < 0) { + idx = 0; // means at least not all empty + } switch (dataType) { case INT32: transformInt(row.getTime(), row.getInt(0)); @@ -358,102 +361,104 @@ public class UDTFM4MAC implements UDTF { @Override public void terminate(PointCollector collector) throws IOException, QueryProcessException { - // record the last interval (not necessarily idx=w-1) in the transform stage - switch (dataType) { - case INT32: - result[idx] = - "FirstPoint=(" - + minTime - + "," - + intFirstV - + "), " - + "LastPoint=(" - + maxTime - + "," - + intLastV - + "), " - + "BottomPoint=(" - + bottomTime - + "," - + intMinV - + "), " - + "TopPoint=(" - + topTime - + "," - + intMaxV - + ")"; - break; - case INT64: - result[idx] = - "FirstPoint=(" - + minTime - + "," - + longFirstV - + "), " - + "LastPoint=(" - + maxTime - + "," - + longLastV - + "), " - + "BottomPoint=(" - + bottomTime - + "," - + longMinV - + "), " - + "TopPoint=(" - + topTime - + "," - + longMaxV - + ")"; - break; - case FLOAT: - result[idx] = - "FirstPoint=(" - + minTime - + "," - + floatFirstV - + "), " - + "LastPoint=(" - + maxTime - + "," - + floatLastV - + "), " - + "BottomPoint=(" - + bottomTime - + "," - + floatMinV - + "), " - + "TopPoint=(" - + topTime - + "," - + floatMaxV - + ")"; - break; - case DOUBLE: - result[idx] = - "FirstPoint=(" - + minTime - + "," - + doubleFirstV - + "), " - + "LastPoint=(" - + maxTime - + "," - + doubleLastV - + "), " - + "BottomPoint=(" - + bottomTime - + "," - + doubleMinV - + "), " - + "TopPoint=(" - + topTime - + "," - + doubleMaxV - + ")"; - break; - default: - break; + if (idx >= 0) { // means at least not all empty + // record the last interval (not necessarily idx=w-1) in the transform stage + switch (dataType) { + case INT32: + result[idx] = + "FirstPoint=(" + + minTime + + "," + + intFirstV + + "), " + + "LastPoint=(" + + maxTime + + "," + + intLastV + + "), " + + "BottomPoint=(" + + bottomTime + + "," + + intMinV + + "), " + + "TopPoint=(" + + topTime + + "," + + intMaxV + + ")"; + break; + case INT64: + result[idx] = + "FirstPoint=(" + + minTime + + "," + + longFirstV + + "), " + + "LastPoint=(" + + maxTime + + "," + + longLastV + + "), " + + "BottomPoint=(" + + bottomTime + + "," + + longMinV + + "), " + + "TopPoint=(" + + topTime + + "," + + longMaxV + + ")"; + break; + case FLOAT: + result[idx] = + "FirstPoint=(" + + minTime + + "," + + floatFirstV + + "), " + + "LastPoint=(" + + maxTime + + "," + + floatLastV + + "), " + + "BottomPoint=(" + + bottomTime + + "," + + floatMinV + + "), " + + "TopPoint=(" + + topTime + + "," + + floatMaxV + + ")"; + break; + case DOUBLE: + result[idx] = + "FirstPoint=(" + + minTime + + "," + + doubleFirstV + + "), " + + "LastPoint=(" + + maxTime + + "," + + doubleLastV + + "), " + + "BottomPoint=(" + + bottomTime + + "," + + doubleMinV + + "), " + + "TopPoint=(" + + topTime + + "," + + doubleMaxV + + ")"; + break; + default: + break; + } } // collect result for (int i = 0; i < w; i++) { diff --git a/server/src/test/java/org/apache/iotdb/db/integration/m4/MyTest3.java b/server/src/test/java/org/apache/iotdb/db/integration/m4/MyTest3.java index ad3f99f9a78..74923aed64a 100644 --- a/server/src/test/java/org/apache/iotdb/db/integration/m4/MyTest3.java +++ b/server/src/test/java/org/apache/iotdb/db/integration/m4/MyTest3.java @@ -217,4 +217,62 @@ public class MyTest3 { fail(e.getMessage()); } } + + @Test + public void test2() { // test UDF MAC extreme case: empty from the beginning + prepareData2(); + + String[] res = new String[] {"0,empty", "50,empty"}; + try (Connection connection = + DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root"); + Statement statement = connection.createStatement()) { + long tqs = 0L; + long tqe = 100L; + int w = 2; + boolean hasResultSet = + statement.execute( + String.format( + "select M4(s0,'tqs'='%1$d','tqe'='%2$d','w'='%3$d') from root.vehicle.d0 where " + + "time>=%1$d and time<%2$d", + tqs, tqe, w)); + + String columnName = "M4(root.vehicle.d0.s0, \"tqs\"=\"%d\", \"tqe\"=\"%d\", \"w\"=\"%d\")"; + Assert.assertTrue(hasResultSet); + try (ResultSet resultSet = statement.getResultSet()) { + int i = 0; + while (resultSet.next()) { + String ans = + resultSet.getString(TIMESTAMP_STR) + + "," + + resultSet.getString(String.format(columnName, tqs, tqe, w)); + System.out.println(ans); + Assert.assertEquals(res[i++], ans); + } + } + } catch (Exception e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + private static void prepareData2() { + try (Connection connection = + DriverManager.getConnection( + Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root", "root"); + Statement statement = connection.createStatement()) { + + for (String sql : creationSqls) { + statement.execute(sql); + } + + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 200, 5)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 300, 15)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 400, 1)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 500, 8)); + statement.execute("FLUSH"); + + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/server/src/test/java/org/apache/iotdb/db/integration/m4/MyTmpTest.java b/server/src/test/java/org/apache/iotdb/db/integration/m4/MyTmpTest.java index 677c79ab535..1c8a47cd729 100644 --- a/server/src/test/java/org/apache/iotdb/db/integration/m4/MyTmpTest.java +++ b/server/src/test/java/org/apache/iotdb/db/integration/m4/MyTmpTest.java @@ -72,7 +72,7 @@ public class MyTmpTest { config.setEnableCPV(true); // CPV originalUseChunkIndex = TSFileDescriptor.getInstance().getConfig().isUseChunkIndex(); - TSFileDescriptor.getInstance().getConfig().setUseChunkIndex(false); + TSFileDescriptor.getInstance().getConfig().setUseChunkIndex(true); originalUseMad = TSFileDescriptor.getInstance().getConfig().isUseMad(); TSFileDescriptor.getInstance().getConfig().setUseMad(true); @@ -107,14 +107,14 @@ public class MyTmpTest { Statement statement = connection.createStatement()) { boolean hasResultSet = statement.execute( - "SELECT M4(s0,'tqs'='0','tqe'='100','w'='4')" + "SELECT M4(s0,'tqs'='0','tqe'='100','w'='10')" + " FROM root.vehicle.d0 where time>=0 and time<100"); Assert.assertTrue(hasResultSet); try (ResultSet resultSet = statement.getResultSet()) { int i = 0; while (resultSet.next()) { - String ans = resultSet.getString(TIMESTAMP_STR) + "," + resultSet.getString(1); + String ans = resultSet.getString(TIMESTAMP_STR) + "," + resultSet.getString(2); System.out.println(ans); // Assert.assertEquals(res[i++], ans); } @@ -184,30 +184,30 @@ public class MyTmpTest { statement.execute(sql); } - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 1, 5)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 2, 15)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 20, 1)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 25, 8)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 54, 3)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 120, 8)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 1000, 5)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 2000, 15)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 20000, 1)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 25000, 8)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 54000, 3)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 120000, 8)); statement.execute("FLUSH"); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 5, 10)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 8, 8)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 10, 30)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 20, 20)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 5000, 10)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 8000, 8)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 10000, 30)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 20000, 20)); statement.execute("FLUSH"); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 27, 20)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 30, 40)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 35, 10)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 40, 20)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 27000, 20)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 30000, 40)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 35000, 10)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 40000, 20)); statement.execute("FLUSH"); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 33, 9)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 45, 30)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 52, 8)); - statement.execute(String.format(Locale.ENGLISH, insertTemplate, 54, 18)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 33000, 9)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 45000, 30)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 52000, 8)); + statement.execute(String.format(Locale.ENGLISH, insertTemplate, 54000, 18)); statement.execute("FLUSH"); } catch (Exception e) {
