| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.iotdb.db.integration.aligned; |
| |
| import org.apache.iotdb.db.conf.IoTDBDescriptor; |
| import org.apache.iotdb.db.utils.EnvironmentUtils; |
| import org.apache.iotdb.itbase.category.LocalStandaloneTest; |
| import org.apache.iotdb.jdbc.Config; |
| |
| import org.junit.AfterClass; |
| import org.junit.Assert; |
| import org.junit.BeforeClass; |
| import org.junit.Test; |
| import org.junit.experimental.categories.Category; |
| |
| import java.sql.Connection; |
| import java.sql.DriverManager; |
| import java.sql.ResultSet; |
| import java.sql.ResultSetMetaData; |
| import java.sql.SQLException; |
| import java.sql.Statement; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import static org.junit.Assert.assertArrayEquals; |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.fail; |
| |
| @Category({LocalStandaloneTest.class}) |
| public class IoTDBAggregationWithValueFilterIT { |
| |
| private static final double DELTA = 1e-6; |
| protected static boolean enableSeqSpaceCompaction; |
| protected static boolean enableUnseqSpaceCompaction; |
| protected static boolean enableCrossSpaceCompaction; |
| |
| @BeforeClass |
| public static void setUp() throws Exception { |
| EnvironmentUtils.envSetUp(); |
| // TODO When the aligned time series support compaction, we need to set compaction to true |
| enableSeqSpaceCompaction = |
| IoTDBDescriptor.getInstance().getConfig().isEnableSeqSpaceCompaction(); |
| enableUnseqSpaceCompaction = |
| IoTDBDescriptor.getInstance().getConfig().isEnableUnseqSpaceCompaction(); |
| enableCrossSpaceCompaction = |
| IoTDBDescriptor.getInstance().getConfig().isEnableCrossSpaceCompaction(); |
| IoTDBDescriptor.getInstance().getConfig().setEnableSeqSpaceCompaction(false); |
| IoTDBDescriptor.getInstance().getConfig().setEnableUnseqSpaceCompaction(false); |
| IoTDBDescriptor.getInstance().getConfig().setEnableCrossSpaceCompaction(false); |
| AlignedWriteUtil.insertData(); |
| } |
| |
| @AfterClass |
| public static void tearDown() throws Exception { |
| IoTDBDescriptor.getInstance().getConfig().setEnableSeqSpaceCompaction(enableSeqSpaceCompaction); |
| IoTDBDescriptor.getInstance() |
| .getConfig() |
| .setEnableUnseqSpaceCompaction(enableUnseqSpaceCompaction); |
| IoTDBDescriptor.getInstance() |
| .getConfig() |
| .setEnableCrossSpaceCompaction(enableCrossSpaceCompaction); |
| EnvironmentUtils.cleanEnv(); |
| } |
| |
| @Test |
| public void countAlignedWithValueFilterTest() throws ClassNotFoundException { |
| String[] retArray = new String[] {"11"}; |
| String[] columnNames = {"count(root.sg1.d1.s4)"}; |
| Class.forName(Config.JDBC_DRIVER_NAME); |
| try (Connection connection = |
| DriverManager.getConnection( |
| Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root", "root"); |
| Statement statement = connection.createStatement()) { |
| |
| boolean hasResultSet = statement.execute("select count(s4) from root.sg1.d1 where s4 = true"); |
| Assert.assertTrue(hasResultSet); |
| try (ResultSet resultSet = statement.getResultSet()) { |
| ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); |
| Map<String, Integer> map = new HashMap<>(); // used to adjust result sequence |
| for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { |
| map.put(resultSetMetaData.getColumnName(i), i); |
| } |
| assertEquals(columnNames.length, resultSetMetaData.getColumnCount()); |
| int cnt = 0; |
| while (resultSet.next()) { |
| String[] ans = new String[columnNames.length]; |
| // No need to add time column for aggregation query |
| for (int i = 0; i < columnNames.length; i++) { |
| String columnName = columnNames[i]; |
| int index = map.get(columnName); |
| ans[i] = resultSet.getString(index); |
| } |
| assertArrayEquals(retArray, ans); |
| cnt++; |
| } |
| assertEquals(1, cnt); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| fail(e.getMessage()); |
| } |
| } |
| |
| @Test |
| public void aggregationFuncAlignedWithValueFilterTest() throws ClassNotFoundException { |
| String[] retArray = new String[] {"8", "42.0", "5.25", "1.0", "9.0", "1", "9", "1.0", "9.0"}; |
| String[] columnNames = { |
| "count(root.sg1.d1.s1)", |
| "sum(root.sg1.d1.s1)", |
| "avg(root.sg1.d1.s1)", |
| "first_value(root.sg1.d1.s1)", |
| "last_value(root.sg1.d1.s1)", |
| "min_time(root.sg1.d1.s1)", |
| "max_time(root.sg1.d1.s1)", |
| "min_value(root.sg1.d1.s1)", |
| "max_value(root.sg1.d1.s1)", |
| }; |
| Class.forName(Config.JDBC_DRIVER_NAME); |
| try (Connection connection = |
| DriverManager.getConnection( |
| Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root", "root"); |
| Statement statement = connection.createStatement()) { |
| |
| boolean hasResultSet = |
| statement.execute( |
| "select count(s1), sum(s1), avg(s1), " |
| + "first_value(s1), last_value(s1), " |
| + "min_time(s1), max_time(s1)," |
| + "max_value(s1), min_value(s1) from root.sg1.d1 where s1 < 10"); |
| Assert.assertTrue(hasResultSet); |
| try (ResultSet resultSet = statement.getResultSet()) { |
| ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); |
| Map<String, Integer> map = new HashMap<>(); // used to adjust result sequence |
| for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { |
| map.put(resultSetMetaData.getColumnName(i), i); |
| } |
| assertEquals(columnNames.length, resultSetMetaData.getColumnCount()); |
| int cnt = 0; |
| while (resultSet.next()) { |
| String[] ans = new String[columnNames.length]; |
| // No need to add time column for aggregation query |
| for (int i = 0; i < columnNames.length; i++) { |
| String columnName = columnNames[i]; |
| int index = map.get(columnName); |
| ans[i] = resultSet.getString(index); |
| } |
| assertArrayEquals(retArray, ans); |
| cnt++; |
| } |
| assertEquals(1, cnt); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| fail(e.getMessage()); |
| } |
| } |
| |
| @Test |
| public void countAllAlignedWithValueFilterTest() throws ClassNotFoundException { |
| String[] retArray = new String[] {"6", "6", "9", "11", "6"}; |
| String[] columnNames = { |
| "count(root.sg1.d1.s1)", |
| "count(root.sg1.d1.s2)", |
| "count(root.sg1.d1.s3)", |
| "count(root.sg1.d1.s4)", |
| "count(root.sg1.d1.s5)" |
| }; |
| Class.forName(Config.JDBC_DRIVER_NAME); |
| try (Connection connection = |
| DriverManager.getConnection( |
| Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root", "root"); |
| Statement statement = connection.createStatement()) { |
| |
| boolean hasResultSet = statement.execute("select count(*) from root.sg1.d1 where s4 = true"); |
| Assert.assertTrue(hasResultSet); |
| try (ResultSet resultSet = statement.getResultSet()) { |
| ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); |
| Map<String, Integer> map = new HashMap<>(); // used to adjust result sequence |
| for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { |
| map.put(resultSetMetaData.getColumnName(i), i); |
| } |
| assertEquals(columnNames.length, resultSetMetaData.getColumnCount()); |
| int cnt = 0; |
| while (resultSet.next()) { |
| String[] ans = new String[columnNames.length]; |
| // No need to add time column for aggregation query |
| for (int i = 0; i < columnNames.length; i++) { |
| String columnName = columnNames[i]; |
| int index = map.get(columnName); |
| ans[i] = resultSet.getString(index); |
| } |
| assertArrayEquals(retArray, ans); |
| cnt++; |
| } |
| assertEquals(1, cnt); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| fail(e.getMessage()); |
| } |
| } |
| |
| @Test |
| public void aggregationAllAlignedWithValueFilterTest() throws ClassNotFoundException { |
| String[] retArray = new String[] {"160016.0", "11", "1", "13"}; |
| String[] columnNames = { |
| "sum(root.sg1.d1.s1)", |
| "count(root.sg1.d1.s4)", |
| "min_value(root.sg1.d1.s3)", |
| "max_time(root.sg1.d1.s2)", |
| }; |
| Class.forName(Config.JDBC_DRIVER_NAME); |
| try (Connection connection = |
| DriverManager.getConnection( |
| Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root", "root"); |
| Statement statement = connection.createStatement()) { |
| |
| boolean hasResultSet = |
| statement.execute( |
| "select sum(s1), count(s4), min_value(s3), max_time(s2) from root.sg1.d1 where s4 = true"); |
| Assert.assertTrue(hasResultSet); |
| try (ResultSet resultSet = statement.getResultSet()) { |
| ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); |
| Map<String, Integer> map = new HashMap<>(); // used to adjust result sequence |
| for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { |
| map.put(resultSetMetaData.getColumnName(i), i); |
| } |
| assertEquals(columnNames.length, resultSetMetaData.getColumnCount()); |
| int cnt = 0; |
| while (resultSet.next()) { |
| String[] ans = new String[columnNames.length]; |
| // No need to add time column for aggregation query |
| for (int i = 0; i < columnNames.length; i++) { |
| String columnName = columnNames[i]; |
| int index = map.get(columnName); |
| ans[i] = resultSet.getString(index); |
| } |
| assertArrayEquals(retArray, ans); |
| cnt++; |
| } |
| assertEquals(1, cnt); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| fail(e.getMessage()); |
| } |
| } |
| } |