Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -81,15 +82,30 @@ public void registerTable(
public void generateData(final String tableName, final int number, final long timeGap)
throws IOException, WriteProcessException {
final List<IMeasurementSchema> schemas = table2MeasurementSchema.get(tableName);
final List<ColumnCategory> columnCategoryList = table2ColumnCategory.get(tableName);
int timeIndex = -1;
for (int i = 0; i < columnCategoryList.size(); ++i) {
if (columnCategoryList.get(i) == ColumnCategory.TIME) {
timeIndex = i;
break;
}
}
final List<IMeasurementSchema> schemaWithoutTime = new ArrayList<>(schemas);
final List<ColumnCategory> columnCategoriesWithoutTime = new ArrayList<>(columnCategoryList);
if (timeIndex > -1) {
schemaWithoutTime.remove(timeIndex);
columnCategoriesWithoutTime.remove(timeIndex);
}
final List<String> columnNameList =
schemas.stream().map(IMeasurementSchema::getMeasurementName).collect(Collectors.toList());
schemaWithoutTime.stream()
.map(IMeasurementSchema::getMeasurementName)
.collect(Collectors.toList());
final List<TSDataType> dataTypeList =
schemas.stream().map(IMeasurementSchema::getType).collect(Collectors.toList());
final List<ColumnCategory> columnCategoryList = table2ColumnCategory.get(tableName);
schemaWithoutTime.stream().map(IMeasurementSchema::getType).collect(Collectors.toList());
final TreeSet<Long> timeSet = table2TimeSet.get(tableName);
final Tablet tablet = new Tablet(tableName, columnNameList, dataTypeList, columnCategoryList);
final Object[] values = tablet.getValues();
final long sensorNum = schemas.size();
final Tablet tablet =
new Tablet(tableName, columnNameList, dataTypeList, columnCategoriesWithoutTime);
final long sensorNum = schemaWithoutTime.size();
long startTime = timeSet.isEmpty() ? 0L : timeSet.last();

for (long r = 0; r < number; r++) {
Expand All @@ -98,7 +114,7 @@ public void generateData(final String tableName, final int number, final long ti
tablet.addTimestamp(row, startTime);
timeSet.add(startTime);
for (int i = 0; i < sensorNum; i++) {
generateDataPoint(tablet, i, row, schemas.get(i));
generateDataPoint(tablet, i, row, schemaWithoutTime.get(i));
}
// write
if (tablet.getRowSize() == tablet.getMaxRowNumber()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,27 @@
*/
package org.apache.iotdb.db.it.query;

import org.apache.iotdb.isession.SessionConfig;
import org.apache.iotdb.it.env.EnvFactory;
import org.apache.iotdb.it.framework.IoTDBTestRunner;
import org.apache.iotdb.itbase.category.TableClusterIT;
import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
import org.apache.iotdb.itbase.env.BaseEnv;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

@RunWith(IoTDBTestRunner.class)
@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
Expand Down Expand Up @@ -103,25 +111,83 @@ public static void tearDown() throws Exception {
EnvFactory.getEnv().cleanClusterEnvironment();
}

@Test
public void TestPriority() {
String[] expectedHeader = {"_col0", "_col1", "_col2", "_col3", "_col4", "_col5"};
String[] retArray = {"1,1,1.0,1.0,true,1s,"};
tableResultSetEqualTest(
"select "
+ "first_by(s_int, y_criteria), "
+ "first_by(s_long, y_criteria), "
+ "first_by(s_float, y_criteria), "
+ "first_by(s_double, y_criteria), "
+ "first_by(s_bool, y_criteria), "
+ "first_by(s_string, y_criteria) "
+ "from table_a "
+ "where table_a.device='d1'",
expectedHeader,
retArray,
DATABASE_NAME);
}

@Test
public void testNoTimeStamp() {

String sql =
"select "
+ "first_by(s_int, y_criteria)"
+ " from ("
+ " select "
+ " s_int,"
+ " s_long,"
+ " s_float,"
+ " y_criteria"
+ " from table_a"
+ ") AS t";
try (Connection connection =
EnvFactory.getEnv()
.getConnection(
SessionConfig.DEFAULT_USER,
SessionConfig.DEFAULT_PASSWORD,
BaseEnv.TABLE_SQL_DIALECT)) {
connection.setClientInfo("time_zone", "+00:00");
try (Statement statement = connection.createStatement()) {
statement.execute("use " + DATABASE_NAME);
statement.executeQuery(sql);
}
fail("Missing valid time column, the query should fail");
} catch (SQLException e) {
assertEquals(
"701: Missing valid time column. The table must contain either a column with the TIME category or at least one TIMESTAMP column.",
e.getMessage());
}
}

@Test
public void testFirstBy_d1_NoNulls() {
String[] expectedHeader = {"_col0", "_col1", "_col2", "_col3", "_col4", "_col5"};
String[] retArray = {"5,5,5.0,5.0,false,5s,"};
runTest("d1", expectedHeader, retArray);
runTest2("d1", expectedHeader, retArray);
runTest3("d1", expectedHeader, retArray);
}

@Test
public void testFirstBy_d2_ForwardTracking() {
String[] expectedHeader = {"_col0", "_col1", "_col2", "_col3", "_col4", "_col5"};
String[] retArray = {"10,10,10.0,10.0,true,10s,"};
runTest("d2", expectedHeader, retArray);
runTest2("d2", expectedHeader, retArray);
runTest3("d2", expectedHeader, retArray);
}

@Test
public void testFirstBy_d3_TargetNull() {
String[] expectedHeader = {"_col0", "_col1", "_col2", "_col3", "_col4", "_col5"};
String[] retArray = {"5,5,null,null,null,null,"};
runTest("d3", expectedHeader, retArray);
runTest2("d3", expectedHeader, retArray);
runTest3("d3", expectedHeader, retArray);
}

@Test
Expand All @@ -130,6 +196,8 @@ public void testFirstBy_d4_AllNullCriteria() {
// Expected: No valid s2 found.
String[] retArray = {"null,null,null,null,null,null,"};
runTest("d4", expectedHeader, retArray);
runTest2("d4", expectedHeader, retArray);
runTest3("d4", expectedHeader, retArray);
}

@Test
Expand All @@ -138,6 +206,8 @@ public void testFirstBy_d5_AllTimeNull() {
// Expected: The row with y_criteria=NULL is skipped. The row with y_criteria=50 is picked.
String[] retArray = {"50,50,50.0,50.0,false,50s,"};
runTest("d5", expectedHeader, retArray);
runTest2("d5", expectedHeader, retArray);
runTest3("d5", expectedHeader, retArray);
}

private void runTest(String deviceId, String[] expectedHeader, String[] retArray) {
Expand All @@ -159,4 +229,43 @@ private void runTest(String deviceId, String[] expectedHeader, String[] retArray
retArray,
DATABASE_NAME);
}

private void runTest2(String deviceId, String[] expectedHeader, String[] retArray) {
tableResultSetEqualTest(
"select "
+ "first_by(s_int, y_criteria), "
+ "first_by(s_long, y_criteria), "
+ "first_by(s_float, y_criteria), "
+ "first_by(s_double, y_criteria), "
+ "first_by(s_bool, y_criteria), "
+ "first_by(s_string, y_criteria) "
+ "from "
+ "(select s_int, s_long, s_float, s_double, s_bool, s_string, y_criteria, time_type "
+ "from table_a left join table_b on table_a.time=table_b.time "
+ "where table_a.device='"
+ deviceId
+ "') ",
expectedHeader,
retArray,
DATABASE_NAME);
}

/** Test 3: Raw table query with explicit time_type column, 3 arguments */
private void runTest3(String deviceId, String[] expectedHeader, String[] retArray) {
tableResultSetEqualTest(
"select "
+ "first_by(s_int, y_criteria, time_type), "
+ "first_by(s_long, y_criteria, time_type), "
+ "first_by(s_float, y_criteria, time_type), "
+ "first_by(s_double, y_criteria, time_type), "
+ "first_by(s_bool, y_criteria, time_type), "
+ "first_by(s_string, y_criteria, time_type) "
+ "from table_a "
+ "where table_a.device='"
+ deviceId
+ "'",
expectedHeader,
retArray,
DATABASE_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void testGroupedFirstByAggregation() {
"p5,50,50,50.0,50.0,false,50s,"
};

// 1. eliminate the identity of time column
tableResultSetEqualTest(
"select "
+ "partition, "
Expand All @@ -135,5 +136,42 @@ public void testGroupedFirstByAggregation() {
expectedHeader,
retArray,
DATABASE_NAME);

// 2. lack of the third argument, supply with timestamp column
tableResultSetEqualTest(
"select "
+ "partition, "
+ "first_by(s_int, y_criteria), "
+ "first_by(s_long, y_criteria), "
+ "first_by(s_float, y_criteria), "
+ "first_by(s_double, y_criteria), "
+ "first_by(s_bool, y_criteria), "
+ "first_by(s_string, y_criteria) "
+ "from "
// SubQuery: Rename time_type to 'ts' to avoid ambiguity with physical 'time'
+ "(select s_int, s_long, s_float, s_double, s_bool, s_string, y_criteria, partition, time_type "
+ "from table_a left join table_b on table_a.time=table_b.time) "
+ "group by partition "
+ "order by partition",
expectedHeader,
retArray,
DATABASE_NAME);

// 3. base table query with column that with timestamp datatype
tableResultSetEqualTest(
"select "
+ "partition, "
+ "first_by(s_int, y_criteria, time_type), "
+ "first_by(s_long, y_criteria, time_type), "
+ "first_by(s_float, y_criteria, time_type), "
+ "first_by(s_double, y_criteria, time_type), "
+ "first_by(s_bool, y_criteria, time_type), "
+ "first_by(s_string, y_criteria, time_type) "
+ "from table_a "
+ "group by partition "
+ "order by partition",
expectedHeader,
retArray,
DATABASE_NAME);
}
}
Loading