diff --git a/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/IoTDB.java b/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/IoTDB.java index 71d78987e..9c79e3623 100644 --- a/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/IoTDB.java +++ b/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/IoTDB.java @@ -223,10 +223,11 @@ public Status insertOneBatch(IBatch batch) throws DBConnectException { */ @Override public Status preciseQuery(PreciseQuery preciseQuery) { - String strTime = preciseQuery.getTimestamp() + ""; - String sql = getSimpleQuerySqlHead(preciseQuery.getDeviceSchema()) + " WHERE time = " + strTime; - sql = modelStrategy.addDeviceIDColumnIfNecessary(preciseQuery.getDeviceSchema(), sql); - return executeQueryAndGetStatus(sql, Operation.PRECISE_QUERY); + StringBuilder builder = new StringBuilder(); + builder.append(getSimpleQuerySqlHead(preciseQuery.getDeviceSchema())); + modelStrategy.addPreciseQueryWhereClause( + String.valueOf(preciseQuery.getTimestamp()), preciseQuery.getDeviceSchema(), builder); + return executeQueryAndGetStatus(builder.toString(), Operation.PRECISE_QUERY); } /** @@ -237,13 +238,19 @@ public Status preciseQuery(PreciseQuery preciseQuery) { */ @Override public Status rangeQuery(RangeQuery rangeQuery) { - String sql = - getRangeQuerySql( - rangeQuery.getDeviceSchema(), - rangeQuery.getStartTimestamp(), - rangeQuery.getEndTimestamp()); - sql = modelStrategy.addDeviceIDColumnIfNecessary(rangeQuery.getDeviceSchema(), sql); - return executeQueryAndGetStatus(sql, Operation.RANGE_QUERY); + StringBuilder builder = new StringBuilder(); + // SELECT + FROM + builder.append(getSimpleQuerySqlHead(rangeQuery.getDeviceSchema())); + // WHERE + modelStrategy.addWhereClause( + true, + false, + rangeQuery.getStartTimestamp(), + rangeQuery.getEndTimestamp(), + rangeQuery.getDeviceSchema(), + 0, + builder); + return executeQueryAndGetStatus(builder.toString(), Operation.RANGE_QUERY); } /** @@ -254,9 +261,19 @@ public Status rangeQuery(RangeQuery rangeQuery) { */ @Override public Status valueRangeQuery(ValueRangeQuery valueRangeQuery) { - String sql = getValueRangeQuerySql(valueRangeQuery); - sql = modelStrategy.addDeviceIDColumnIfNecessary(valueRangeQuery.getDeviceSchema(), sql); - return executeQueryAndGetStatus(sql, Operation.VALUE_RANGE_QUERY); + StringBuilder builder = new StringBuilder(); + // SELECT + FROM + builder.append(getSimpleQuerySqlHead(valueRangeQuery.getDeviceSchema())); + // WHERE + modelStrategy.addWhereClause( + true, + true, + valueRangeQuery.getStartTimestamp(), + valueRangeQuery.getEndTimestamp(), + valueRangeQuery.getDeviceSchema(), + (int) valueRangeQuery.getValueThreshold(), + builder); + return executeQueryAndGetStatus(builder.toString(), Operation.VALUE_RANGE_QUERY); } /** @@ -267,13 +284,22 @@ public Status valueRangeQuery(ValueRangeQuery valueRangeQuery) { */ @Override public Status aggRangeQuery(AggRangeQuery aggRangeQuery) { + StringBuilder builder = new StringBuilder(); + // SELECT + FROM String aggQuerySqlHead = - getAggQuerySqlHead(aggRangeQuery.getDeviceSchema(), aggRangeQuery.getAggFun()); - String sql = - addWhereTimeClause( - aggQuerySqlHead, aggRangeQuery.getStartTimestamp(), aggRangeQuery.getEndTimestamp()); - sql = modelStrategy.addDeviceIDColumnIfNecessary(aggRangeQuery.getDeviceSchema(), sql); - return executeQueryAndGetStatus(sql, Operation.AGG_RANGE_QUERY); + modelStrategy.getAggQuerySqlHead( + aggRangeQuery.getDeviceSchema(), aggRangeQuery.getAggFun()); + builder.append(aggQuerySqlHead); + // WHERE + modelStrategy.addAggWhereClause( + true, + false, + aggRangeQuery.getStartTimestamp(), + aggRangeQuery.getEndTimestamp(), + aggRangeQuery.getDeviceSchema(), + 0, + builder); + return executeQueryAndGetStatus(builder.toString(), Operation.AGG_RANGE_QUERY); } /** @@ -283,16 +309,22 @@ public Status aggRangeQuery(AggRangeQuery aggRangeQuery) { */ @Override public Status aggValueQuery(AggValueQuery aggValueQuery) { + StringBuilder builder = new StringBuilder(); + // SELECT + FROM String aggQuerySqlHead = - getAggQuerySqlHead(aggValueQuery.getDeviceSchema(), aggValueQuery.getAggFun()); - String sql = - aggQuerySqlHead - + " WHERE " - + getValueFilterClause( - aggValueQuery.getDeviceSchema(), (int) aggValueQuery.getValueThreshold()) - .substring(4); - sql = modelStrategy.addDeviceIDColumnIfNecessary(aggValueQuery.getDeviceSchema(), sql); - return executeQueryAndGetStatus(sql, Operation.AGG_VALUE_QUERY); + modelStrategy.getAggQuerySqlHead( + aggValueQuery.getDeviceSchema(), aggValueQuery.getAggFun()); + builder.append(aggQuerySqlHead); + // WHERE + modelStrategy.addAggWhereClause( + false, + true, + aggValueQuery.getStartTimestamp(), + aggValueQuery.getEndTimestamp(), + aggValueQuery.getDeviceSchema(), + (int) aggValueQuery.getValueThreshold(), + builder); + return executeQueryAndGetStatus(builder.toString(), Operation.AGG_VALUE_QUERY); } /** @@ -304,18 +336,22 @@ public Status aggValueQuery(AggValueQuery aggValueQuery) { */ @Override public Status aggRangeValueQuery(AggRangeValueQuery aggRangeValueQuery) { + StringBuilder builder = new StringBuilder(); + // SELECT + FROM String aggQuerySqlHead = - getAggQuerySqlHead(aggRangeValueQuery.getDeviceSchema(), aggRangeValueQuery.getAggFun()); - String sql = - addWhereTimeClause( - aggQuerySqlHead, - aggRangeValueQuery.getStartTimestamp(), - aggRangeValueQuery.getEndTimestamp()); - sql += - getValueFilterClause( - aggRangeValueQuery.getDeviceSchema(), (int) aggRangeValueQuery.getValueThreshold()); - sql = modelStrategy.addDeviceIDColumnIfNecessary(aggRangeValueQuery.getDeviceSchema(), sql); - return executeQueryAndGetStatus(sql, Operation.AGG_RANGE_VALUE_QUERY); + modelStrategy.getAggQuerySqlHead( + aggRangeValueQuery.getDeviceSchema(), aggRangeValueQuery.getAggFun()); + builder.append(aggQuerySqlHead); + // WHERE + modelStrategy.addAggWhereClause( + true, + true, + aggRangeValueQuery.getStartTimestamp(), + aggRangeValueQuery.getEndTimestamp(), + aggRangeValueQuery.getDeviceSchema(), + (int) aggRangeValueQuery.getValueThreshold(), + builder); + return executeQueryAndGetStatus(builder.toString(), Operation.AGG_RANGE_VALUE_QUERY); } /** @@ -337,8 +373,10 @@ public Status groupByQuery(GroupByQuery groupByQuery) { */ @Override public Status latestPointQuery(LatestPointQuery latestPointQuery) { - String latestPointSqlHead = getLatestPointQuerySql(latestPointQuery.getDeviceSchema()); - return executeQueryAndGetStatus(latestPointSqlHead, Operation.LATEST_POINT_QUERY); + String latestPointSqlHead = + modelStrategy.getLatestPointQuerySql(latestPointQuery.getDeviceSchema()); + String sql = modelStrategy.addGroupByClauseIfNecessary(latestPointSqlHead); + return executeQueryAndGetStatus(sql, Operation.LATEST_POINT_QUERY); } /** @@ -349,15 +387,21 @@ public Status latestPointQuery(LatestPointQuery latestPointQuery) { */ @Override public Status rangeQueryOrderByDesc(RangeQuery rangeQuery) { - String sql = - getRangeQuerySql( - rangeQuery.getDeviceSchema(), - rangeQuery.getStartTimestamp(), - rangeQuery.getEndTimestamp()); - sql = - modelStrategy.addDeviceIDColumnIfNecessary(rangeQuery.getDeviceSchema(), sql) - + " order by time desc"; - return executeQueryAndGetStatus(sql, Operation.RANGE_QUERY_ORDER_BY_TIME_DESC); + StringBuilder builder = new StringBuilder(); + // SELECT + FROM + builder.append(getSimpleQuerySqlHead(rangeQuery.getDeviceSchema())); + // WHERE + modelStrategy.addWhereClause( + true, + false, + rangeQuery.getStartTimestamp(), + rangeQuery.getEndTimestamp(), + rangeQuery.getDeviceSchema(), + 0, + builder); + // ORDER BY + builder.append(" ORDER BY time desc"); + return executeQueryAndGetStatus(builder.toString(), Operation.RANGE_QUERY_ORDER_BY_TIME_DESC); } /** @@ -368,11 +412,22 @@ public Status rangeQueryOrderByDesc(RangeQuery rangeQuery) { */ @Override public Status valueRangeQueryOrderByDesc(ValueRangeQuery valueRangeQuery) { - String sql = getValueRangeQuerySql(valueRangeQuery); - sql = - modelStrategy.addDeviceIDColumnIfNecessary(valueRangeQuery.getDeviceSchema(), sql) - + " order by time desc"; - return executeQueryAndGetStatus(sql, Operation.VALUE_RANGE_QUERY_ORDER_BY_TIME_DESC); + StringBuilder builder = new StringBuilder(); + // SELECT + FROM + builder.append(getSimpleQuerySqlHead(valueRangeQuery.getDeviceSchema())); + // WHERE + modelStrategy.addWhereClause( + true, + true, + valueRangeQuery.getStartTimestamp(), + valueRangeQuery.getEndTimestamp(), + valueRangeQuery.getDeviceSchema(), + (int) valueRangeQuery.getValueThreshold(), + builder); + // ORDER BY + builder.append(" ORDER BY time desc"); + return executeQueryAndGetStatus( + builder.toString(), Operation.VALUE_RANGE_QUERY_ORDER_BY_TIME_DESC); } /** @@ -385,7 +440,6 @@ public Status valueRangeQueryOrderByDesc(ValueRangeQuery valueRangeQuery) { @Override public Status groupByQueryOrderByDesc(GroupByQuery groupByQuery) { String sql = modelStrategy.getGroupByQuerySQL(groupByQuery); - sql += ORDER_BY_TIME_DESC; return executeQueryAndGetStatus(sql, Operation.GROUP_BY_QUERY_ORDER_BY_TIME_DESC); } @@ -404,75 +458,10 @@ protected String getSimpleQuerySqlHead(List devices) { for (int i = 1; i < querySensors.size(); i++) { builder.append(", ").append(querySensors.get(i).getName()); } - return addFromClause(devices, builder); - } - - private String getAggQuerySqlHead(List devices, String aggFun) { - StringBuilder builder = new StringBuilder(); - builder.append("SELECT "); - List querySensors = devices.get(0).getSensors(); - builder.append(aggFun).append("(").append(querySensors.get(0).getName()).append(")"); - for (int i = 1; i < querySensors.size(); i++) { - builder - .append(", ") - .append(aggFun) - .append("(") - .append(querySensors.get(i).getName()) - .append(")"); - } - return addFromClause(devices, builder); - } - - /** - * Add from Clause - * - * @return From clause, e.g. FROM devices - */ - private String addFromClause(List devices, StringBuilder builder) { - return modelStrategy.addFromClause(devices, builder); - } - - private String getValueRangeQuerySql(ValueRangeQuery valueRangeQuery) { - String rangeQuerySql = - getRangeQuerySql( - valueRangeQuery.getDeviceSchema(), - valueRangeQuery.getStartTimestamp(), - valueRangeQuery.getEndTimestamp()); - String valueFilterClause = - getValueFilterClause( - valueRangeQuery.getDeviceSchema(), (int) valueRangeQuery.getValueThreshold()); - return rangeQuerySql + valueFilterClause; - } - - private String getValueFilterClause(List deviceSchemas, int valueThreshold) { - StringBuilder builder = new StringBuilder(); - modelStrategy.getValueFilterClause(deviceSchemas, valueThreshold, builder); + modelStrategy.addFromClause(devices, builder); return builder.toString(); } - private String getLatestPointQuerySql(List devices) { - StringBuilder builder = new StringBuilder(); - builder.append("SELECT last "); - List querySensors = devices.get(0).getSensors(); - builder.append(querySensors.get(0).getName()); - for (int i = 1; i < querySensors.size(); i++) { - builder.append(", ").append(querySensors.get(i).getName()); - } - String sql = addFromClause(devices, builder); - sql = modelStrategy.addWhereValueClauseIfNecessary(devices, sql); - return sql; - } - - private String getRangeQuerySql(List deviceSchemas, long start, long end) { - return addWhereTimeClause(getSimpleQuerySqlHead(deviceSchemas), start, end); - } - - public static String addWhereTimeClause(String prefix, long start, long end) { - String startTime = start + ""; - String endTime = end + ""; - return prefix + " WHERE time >= " + startTime + " AND time <= " + endTime; - } - protected Status executeQueryAndGetStatus(String sql, Operation operation) { String executeSQL; if (config.isIOTDB_USE_DEBUG() && random.nextDouble() < config.getIOTDB_USE_DEBUG_RATIO()) { diff --git a/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/IoTDBModelStrategy.java b/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/IoTDBModelStrategy.java index 036b5ed42..2503c21ed 100644 --- a/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/IoTDBModelStrategy.java +++ b/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/IoTDBModelStrategy.java @@ -63,15 +63,41 @@ public abstract void registerSchema( throws TsdbException; // region select - public abstract String selectTimeColumnIfNecessary(); - public abstract String addFromClause(List devices, StringBuilder builder); + public abstract String getAggQuerySqlHead(List devices, String aggFun); - public abstract String addDeviceIDColumnIfNecessary(List deviceSchemas, String sql); + public abstract String getGroupByQuerySQL(GroupByQuery groupByQuery); - public abstract void deleteIDColumnIfNecessary( - List columnTypes, List sensors, IBatch batch); + public abstract String getLatestPointQuerySql(List devices); + + public abstract void addFromClause(List devices, StringBuilder builder); + + public abstract void addPreciseQueryWhereClause( + String strTime, List deviceSchemas, StringBuilder builder); + + public abstract void addWhereClause( + boolean addTime, + boolean addValue, + long start, + long end, + List deviceSchemas, + int valueThreshold, + StringBuilder builder); + + public abstract void addAggWhereClause( + boolean addTime, + boolean addValue, + long start, + long end, + List deviceSchemas, + int valueThreshold, + StringBuilder builder); + + public abstract void addWhereValueClauseIfNecessary( + List devices, StringBuilder builder); + + public abstract String addGroupByClauseIfNecessary(String sql); public abstract void addVerificationQueryWhereClause( StringBuffer sql, @@ -79,8 +105,10 @@ public abstract void addVerificationQueryWhereClause( Map> recordMap, DeviceSchema deviceSchema); - public abstract void getValueFilterClause( - List deviceSchemas, int valueThreshold, StringBuilder builder); + public abstract void deleteIDColumnIfNecessary( + List columnTypes, List sensors, IBatch batch); + + public abstract String getValueFilterClause(List deviceSchemas, int valueThreshold); public abstract long getTimestamp(RowRecord rowRecord); @@ -132,11 +160,7 @@ public abstract void sessionCleanupImpl(Session session) // endregion - // region query - - public abstract String getGroupByQuerySQL(GroupByQuery groupByQuery); - - public abstract String addWhereValueClauseIfNecessary(List devices, String prefix); + public abstract Logger getLogger(); protected String getAggFunForGroupByQuery(List querySensors, String aggFunction) { StringBuilder builder = new StringBuilder(); @@ -152,9 +176,15 @@ protected String getAggFunForGroupByQuery(List querySensors, String aggF return builder.toString(); } - // endregion - - public abstract Logger getLogger(); + protected String getTimeWhereClause(long start, long end) { + StringBuilder builder = new StringBuilder(); + builder + .append(" time >= ") + .append(String.valueOf(start)) + .append(" AND time <= ") + .append(String.valueOf(end)); + return builder.toString(); + } public void handleRegisterException(Exception e) throws TsdbException { // ignore if already has the time series diff --git a/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/TableStrategy.java b/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/TableStrategy.java index e91027351..42bb0d1ef 100644 --- a/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/TableStrategy.java +++ b/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/TableStrategy.java @@ -148,7 +148,7 @@ public String selectTimeColumnIfNecessary() { } @Override - public String addFromClause(List devices, StringBuilder builder) { + public void addFromClause(List devices, StringBuilder builder) { // "root.test.g_0.d_0.s_0" // tree mode: select ... from root.test.g_0.d_0 // table mode: select ... from test_g_0.table_0 @@ -159,12 +159,141 @@ public String addFromClause(List devices, StringBuilder builder) { .append(devices.get(0).getGroup()) .append(".") .append(devices.get(0).getTable()); + } + + @Override + public String getAggQuerySqlHead(List devices, String aggFun) { + StringBuilder builder = new StringBuilder(); + builder.append("SELECT device_id"); + List querySensors = devices.get(0).getSensors(); + for (int i = 1; i < querySensors.size(); i++) { + builder + .append(", ") + .append(aggFun) + .append("(") + .append(querySensors.get(i).getName()) + .append(")"); + } + addFromClause(devices, builder); + return builder.toString(); + } + + /** + * eg. SELECT device_id, data_bin(20000ms, time), count(s_3), count(s_2), count(s_1) FROM + * test_g_1.table_4 WHERE time >= 1640966400000 AND time < 1640966650000 AND (device_id = 'd_0' OR + * device_id = 'd_1') group by device_id, data_bin(20000ms, time) + * + *

getAggForGroupByQuery + */ + @Override + public String getGroupByQuerySQL(GroupByQuery groupByQuery) { + StringBuilder builder = new StringBuilder(); + // SELECT + builder + .append("SELECT") + .append(" device_id,") + .append(" date_bin(") + .append(groupByQuery.getGranularity()) + .append("ms, ") + .append("time), ") + .append( + getAggFunForGroupByQuery( + groupByQuery.getDeviceSchema().get(0).getSensors(), groupByQuery.getAggFun())); + // FROM + addFromClause(groupByQuery.getDeviceSchema(), builder); + // WHERE + builder + .append(" WHERE") + .append( + getTimeWhereClause(groupByQuery.getStartTimestamp(), groupByQuery.getEndTimestamp())); + addDeviceIDColumnIfNecessary(groupByQuery.getDeviceSchema(), builder); + // GROUP BY + builder + .append(" group by device_id, date_bin(") + .append(groupByQuery.getGranularity()) + .append("ms, time)"); + // ORDER BY + builder + .append(" order by device_id, date_bin(") + .append(groupByQuery.getGranularity()) + .append("ms, time) desc"); return builder.toString(); } + private void addDeviceIDColumnIfNecessary( + List deviceSchemas, StringBuilder builder) { + builder.append(" AND").append(getDeviceIDColumn(deviceSchemas)); + } + + @Override + public void addPreciseQueryWhereClause( + String strTime, List deviceSchemas, StringBuilder builder) { + builder + .append(" WHERE time = ") + .append(strTime) + .append(" AND ") + .append(getDeviceIDColumn(deviceSchemas)); + } + @Override - public String addDeviceIDColumnIfNecessary(List deviceSchemas, String sql) { - return sql + " AND" + getDeviceIDColumn(deviceSchemas); + public void addWhereClause( + boolean addTime, + boolean addValue, + long start, + long end, + List deviceSchemas, + int valueThreshold, + StringBuilder builder) { + builder.append(" WHERE"); + if (addTime) { + builder.append(getTimeWhereClause(start, end)); + } + if (addValue) { + builder.append(getValueFilterClause(deviceSchemas, valueThreshold)); + } + builder.append(" AND ").append(getDeviceIDColumn(deviceSchemas)); + } + + @Override + public void addAggWhereClause( + boolean addTime, + boolean addValue, + long start, + long end, + List deviceSchemas, + int valueThreshold, + StringBuilder builder) { + builder.append(" WHERE"); + if (addTime) { + builder.append(getTimeWhereClause(start, end)); + } + if (addValue) { + String valueFilterClause = getValueFilterClause(deviceSchemas, valueThreshold); + if (!addTime) { + valueFilterClause = valueFilterClause.substring(4); + } + builder.append(valueFilterClause); + } + builder.append(" AND ").append(getDeviceIDColumn(deviceSchemas)).append(" GROUP BY device_id "); + } + + @Override + public String addGroupByClauseIfNecessary(String sql) { + sql = sql + " GROUP BY device_id"; + return sql; + } + + @Override + public String getLatestPointQuerySql(List devices) { + StringBuilder builder = new StringBuilder(); + builder.append("SELECT device_id, last(time)"); + List querySensors = devices.get(0).getSensors(); + for (int i = 0; i < querySensors.size(); i++) { + builder.append(", last_by(").append(querySensors.get(i).getName()).append(", time)"); + } + addFromClause(devices, builder); + addWhereValueClauseIfNecessary(devices, builder); + return builder.toString(); } public String getDeviceIDColumn(List deviceSchemas) { @@ -218,9 +347,9 @@ public void addVerificationQueryWhereClause( } @Override - public void getValueFilterClause( - List deviceSchemas, int valueThreshold, StringBuilder builder) { + public String getValueFilterClause(List deviceSchemas, int valueThreshold) { DeviceSchema deviceSchema = deviceSchemas.get(0); + StringBuilder builder = new StringBuilder(); for (Sensor sensor : deviceSchema.getSensors()) { builder.append(" AND ").append(sensor.getName()).append(" > "); if (sensor.getSensorType() == SensorType.DATE) { @@ -234,6 +363,7 @@ public void getValueFilterClause( builder.append(valueThreshold); } } + return builder.toString(); } @Override @@ -247,6 +377,7 @@ public int getQueryOffset() { } // TODO 用 count + @Override public String getTotalLineNumberSql(DeviceSchema deviceSchema) { return "select * from " @@ -361,46 +492,9 @@ public void sessionCleanupImpl(Session session) } } - /** - * eg. SELECT data_bin(20000ms, time), count(s_1), count(s_3), count(s_4) FROM test_g_0.table_0 - * WHERE time >= 1640966400000 AND time < 1640966650000 AND (device_id='d_0' OR device_id='d_3') - * group by time - * - *

getAggForGroupByQuery - */ @Override - public String getGroupByQuerySQL(GroupByQuery groupByQuery) { - StringBuilder builder = new StringBuilder(); - // SELECT - builder - .append("SELECT ") - .append("data_bin(") - .append(groupByQuery.getGranularity()) - .append("ms, ") - .append("time), ") - .append( - getAggFunForGroupByQuery( - groupByQuery.getDeviceSchema().get(0).getSensors(), groupByQuery.getAggFun())); - // FROM - String sql = addFromClause(groupByQuery.getDeviceSchema(), builder); - // WHERE - sql = - IoTDB.addWhereTimeClause( - sql, groupByQuery.getStartTimestamp(), groupByQuery.getEndTimestamp()); - sql = addDeviceIDColumnIfNecessary(groupByQuery.getDeviceSchema(), sql); - // GROUP BY - sql = addGroupByClause(sql); - return sql; - } - - @Override - public String addWhereValueClauseIfNecessary(List devices, String prefix) { - String sql = prefix + " WHERE" + getDeviceIDColumn(devices); - return sql; - } - - private String addGroupByClause(String prefix) { - return prefix + " group by time"; + public void addWhereValueClauseIfNecessary(List devices, StringBuilder builder) { + builder.append(" WHERE").append(getDeviceIDColumn(devices)); } // endregion diff --git a/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/TreeStrategy.java b/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/TreeStrategy.java index 158a2f3da..febb342b2 100644 --- a/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/TreeStrategy.java +++ b/iotdb-2.0/src/main/java/cn/edu/tsinghua/iot/benchmark/iotdb200/ModelStrategy/TreeStrategy.java @@ -211,24 +211,137 @@ private void registerTimeSeries(Session metaSession, List time } // region select - @Override public String selectTimeColumnIfNecessary() { return ""; } @Override - public String addFromClause(List devices, StringBuilder builder) { + public String getAggQuerySqlHead(List devices, String aggFun) { + StringBuilder builder = new StringBuilder(); + builder.append("SELECT "); + List querySensors = devices.get(0).getSensors(); + builder.append(aggFun).append("(").append(querySensors.get(0).getName()).append(")"); + for (int i = 1; i < querySensors.size(); i++) { + builder + .append(", ") + .append(aggFun) + .append("(") + .append(querySensors.get(i).getName()) + .append(")"); + } + addFromClause(devices, builder); + return builder.toString(); + } + + @Override + public void addFromClause(List devices, StringBuilder builder) { builder.append(" FROM ").append(IoTDBUtils.getDevicePath(devices.get(0), ROOT_SERIES_NAME)); for (int i = 1; i < devices.size(); i++) { builder.append(", ").append(IoTDBUtils.getDevicePath(devices.get(i), ROOT_SERIES_NAME)); } + } + + @Override + public String getGroupByQuerySQL(GroupByQuery groupByQuery) { + StringBuilder builder = new StringBuilder(); + // SELECT + builder + .append("SELECT ") + .append( + getAggFunForGroupByQuery( + groupByQuery.getDeviceSchema().get(0).getSensors(), groupByQuery.getAggFun())); + // FROM + addFromClause(groupByQuery.getDeviceSchema(), builder); + // GROUP BY + addGroupByClause( + builder, + groupByQuery.getStartTimestamp(), + groupByQuery.getEndTimestamp(), + groupByQuery.getGranularity()); + // ORDER BY + builder.append(" ORDER BY time desc"); + return builder.toString(); + } + + private void addGroupByClause(StringBuilder builder, long start, long end, long granularity) { + builder + .append(" group by ([") + .append(start) + .append(", ") + .append(end) + .append("),") + .append(granularity) + .append("ms) "); + } + + @Override + public String getLatestPointQuerySql(List devices) { + StringBuilder builder = new StringBuilder(); + builder.append("SELECT last "); + List querySensors = devices.get(0).getSensors(); + builder.append(querySensors.get(0).getName()); + for (int i = 1; i < querySensors.size(); i++) { + builder.append(", ").append(querySensors.get(i).getName()); + } + addFromClause(devices, builder); + addWhereValueClauseIfNecessary(devices, builder); return builder.toString(); } @Override - public String addDeviceIDColumnIfNecessary(List deviceSchemas, String sql) { + public void addWhereValueClauseIfNecessary(List devices, StringBuilder builder) { // do nothing + } + + @Override + public void addPreciseQueryWhereClause( + String strTime, List deviceSchemas, StringBuilder builder) { + builder.append(" WHERE time = ").append(strTime); + } + + @Override + public void addWhereClause( + boolean addTime, + boolean addValue, + long start, + long end, + List deviceSchemas, + int valueThreshold, + StringBuilder builder) { + builder.append(" WHERE"); + if (addTime) { + builder.append(getTimeWhereClause(start, end)); + } + if (addValue) { + builder.append(getValueFilterClause(deviceSchemas, valueThreshold)); + } + } + + @Override + public void addAggWhereClause( + boolean addTime, + boolean addValue, + long start, + long end, + List deviceSchemas, + int valueThreshold, + StringBuilder builder) { + builder.append(" WHERE"); + if (addTime) { + builder.append(getTimeWhereClause(start, end)); + } + if (addValue) { + String valueFilterClause = getValueFilterClause(deviceSchemas, valueThreshold); + if (!addTime) { + valueFilterClause = valueFilterClause.substring(4); + } + builder.append(valueFilterClause); + } + } + + @Override + public String addGroupByClauseIfNecessary(String sql) { return sql; } @@ -248,8 +361,8 @@ public void addVerificationQueryWhereClause( } @Override - public void getValueFilterClause( - List deviceSchemas, int valueThreshold, StringBuilder builder) { + public String getValueFilterClause(List deviceSchemas, int valueThreshold) { + StringBuilder builder = new StringBuilder(); for (DeviceSchema deviceSchema : deviceSchemas) { for (Sensor sensor : deviceSchema.getSensors()) { builder @@ -265,6 +378,7 @@ public void getValueFilterClause( } } } + return builder.toString(); } @Override @@ -300,6 +414,7 @@ public String getMinTimeStampSql(DeviceSchema deviceSchema) { // endregion // region insert + @Override public Tablet createTablet( String insertTargetName, @@ -356,36 +471,6 @@ public void sessionCleanupImpl(Session session) { } } - @Override - public String getGroupByQuerySQL(GroupByQuery groupByQuery) { - StringBuilder builder = new StringBuilder(); - // SELECT - builder - .append("SELECT ") - .append( - getAggFunForGroupByQuery( - groupByQuery.getDeviceSchema().get(0).getSensors(), groupByQuery.getAggFun())); - // FROM - String sql = addFromClause(groupByQuery.getDeviceSchema(), builder); - // GROUP BY - sql = - addGroupByClause( - sql, - groupByQuery.getStartTimestamp(), - groupByQuery.getEndTimestamp(), - groupByQuery.getGranularity()); - return sql; - } - - @Override - public String addWhereValueClauseIfNecessary(List devices, String prefix) { - return prefix; - } - - private String addGroupByClause(String prefix, long start, long end, long granularity) { - return prefix + " group by ([" + start + "," + end + ")," + granularity + "ms) "; - } - // endregion @Override