Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print the query statement. #466

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutionException;
Expand All @@ -54,6 +56,8 @@ public abstract class BaseMode {
Executors.newScheduledThreadPool(2, new NamedThreadFactory("ShowResultPeriodically"));

private static final double NANO_TO_SECOND = 1000000000.0d;
private static final String RESULT_ITEM = "%-35s";
private static final String LATENCY_ITEM = "%-80s";

protected ExecutorService schemaExecutorService =
Executors.newFixedThreadPool(
Expand Down Expand Up @@ -101,6 +105,7 @@ public void run() {
Thread.currentThread().interrupt();
}
postCheck();
printSqlStatements();
scheduler.shutdownNow();
}

Expand Down Expand Up @@ -275,4 +280,38 @@ private static void measure(
measurement.outputCSV();
}
}

private static Map<Operation, String> sqlMap = new HashMap<>();

/**
* Each type of query is recorded once.
*
* @param operation
* @param sql
*/
public static void logSqlIfNotCollect(Operation operation, String sql) {
sqlMap.computeIfAbsent(operation, k -> sql);
}

/** Output all query statements */
private void printSqlStatements() {
StringBuilder stringBuilder = new StringBuilder("\n");
stringBuilder
.append(
"----------------------------------------------------------------------------SQL Statements Example---------------------------------------------------------------------")
.append('\n');
stringBuilder
.append(String.format(RESULT_ITEM, "Operation"))
.append(String.format(LATENCY_ITEM, "SQL"))
.append("\n");
for (Map.Entry<Operation, String> entry : sqlMap.entrySet()) {
stringBuilder
.append(String.format(RESULT_ITEM, entry.getKey()))
.append(String.format(LATENCY_ITEM, entry.getValue()))
.append("\n");
}
stringBuilder.append(
"------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println(stringBuilder.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import cn.edu.tsinghua.iot.benchmark.iotdb200.ModelStrategy.TreeStrategy;
import cn.edu.tsinghua.iot.benchmark.iotdb200.utils.IoTDBUtils;
import cn.edu.tsinghua.iot.benchmark.measurement.Status;
import cn.edu.tsinghua.iot.benchmark.mode.BaseMode;
import cn.edu.tsinghua.iot.benchmark.schema.schemaImpl.DeviceSchema;
import cn.edu.tsinghua.iot.benchmark.tsdb.DBConfig;
import cn.edu.tsinghua.iot.benchmark.tsdb.IDatabase;
Expand Down Expand Up @@ -227,6 +228,7 @@ public Status preciseQuery(PreciseQuery preciseQuery) {
builder.append(getSimpleQuerySqlHead(preciseQuery.getDeviceSchema()));
modelStrategy.addPreciseQueryWhereClause(
String.valueOf(preciseQuery.getTimestamp()), preciseQuery.getDeviceSchema(), builder);
BaseMode.logSqlIfNotCollect(Operation.PRECISE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.PRECISE_QUERY);
}

Expand All @@ -250,6 +252,7 @@ public Status rangeQuery(RangeQuery rangeQuery) {
rangeQuery.getDeviceSchema(),
0,
builder);
BaseMode.logSqlIfNotCollect(Operation.RANGE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.RANGE_QUERY);
}

Expand All @@ -273,6 +276,7 @@ public Status valueRangeQuery(ValueRangeQuery valueRangeQuery) {
valueRangeQuery.getDeviceSchema(),
(int) valueRangeQuery.getValueThreshold(),
builder);
BaseMode.logSqlIfNotCollect(Operation.VALUE_RANGE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.VALUE_RANGE_QUERY);
}

Expand All @@ -299,6 +303,7 @@ public Status aggRangeQuery(AggRangeQuery aggRangeQuery) {
aggRangeQuery.getDeviceSchema(),
0,
builder);
BaseMode.logSqlIfNotCollect(Operation.AGG_RANGE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.AGG_RANGE_QUERY);
}

Expand All @@ -324,6 +329,7 @@ public Status aggValueQuery(AggValueQuery aggValueQuery) {
aggValueQuery.getDeviceSchema(),
(int) aggValueQuery.getValueThreshold(),
builder);
BaseMode.logSqlIfNotCollect(Operation.AGG_VALUE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.AGG_VALUE_QUERY);
}

Expand Down Expand Up @@ -351,6 +357,7 @@ public Status aggRangeValueQuery(AggRangeValueQuery aggRangeValueQuery) {
aggRangeValueQuery.getDeviceSchema(),
(int) aggRangeValueQuery.getValueThreshold(),
builder);
BaseMode.logSqlIfNotCollect(Operation.AGG_RANGE_VALUE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.AGG_RANGE_VALUE_QUERY);
}

Expand All @@ -363,6 +370,7 @@ public Status aggRangeValueQuery(AggRangeValueQuery aggRangeValueQuery) {
@Override
public Status groupByQuery(GroupByQuery groupByQuery) {
String sql = modelStrategy.getGroupByQuerySQL(groupByQuery, false);
BaseMode.logSqlIfNotCollect(Operation.GROUP_BY_QUERY, sql);
return executeQueryAndGetStatus(sql, Operation.GROUP_BY_QUERY);
}

Expand All @@ -376,6 +384,7 @@ public Status latestPointQuery(LatestPointQuery latestPointQuery) {
String latestPointSqlHead =
modelStrategy.getLatestPointQuerySql(latestPointQuery.getDeviceSchema());
String sql = modelStrategy.addGroupByClauseIfNecessary(latestPointSqlHead);
BaseMode.logSqlIfNotCollect(Operation.LATEST_POINT_QUERY, sql);
return executeQueryAndGetStatus(sql, Operation.LATEST_POINT_QUERY);
}

Expand All @@ -401,6 +410,7 @@ public Status rangeQueryOrderByDesc(RangeQuery rangeQuery) {
builder);
// ORDER BY
modelStrategy.addOrderByTimeDesc(builder);
BaseMode.logSqlIfNotCollect(Operation.RANGE_QUERY_ORDER_BY_TIME_DESC, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.RANGE_QUERY_ORDER_BY_TIME_DESC);
}

Expand All @@ -426,6 +436,7 @@ public Status valueRangeQueryOrderByDesc(ValueRangeQuery valueRangeQuery) {
builder);
// ORDER BY
modelStrategy.addOrderByTimeDesc(builder);
BaseMode.logSqlIfNotCollect(Operation.AGG_RANGE_VALUE_QUERY, builder.toString());
return executeQueryAndGetStatus(
builder.toString(), Operation.VALUE_RANGE_QUERY_ORDER_BY_TIME_DESC);
}
Expand All @@ -440,6 +451,7 @@ public Status valueRangeQueryOrderByDesc(ValueRangeQuery valueRangeQuery) {
@Override
public Status groupByQueryOrderByDesc(GroupByQuery groupByQuery) {
String sql = modelStrategy.getGroupByQuerySQL(groupByQuery, true);
BaseMode.logSqlIfNotCollect(Operation.GROUP_BY_QUERY_ORDER_BY_TIME_DESC, sql);
return executeQueryAndGetStatus(sql, Operation.GROUP_BY_QUERY_ORDER_BY_TIME_DESC);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ public void registerDatabases(Session metaSession, List<TimeseriesSchema> schema
private void registerTable(Session metaSession, List<TimeseriesSchema> timeseriesSchemas)
throws TsdbException {
try {
// get all tables
Set<String> tableNames = getAllTables(timeseriesSchemas);
// register tables
DeviceSchema deviceSchema = timeseriesSchemas.get(0).getDeviceSchema();
HashMap<String, List<String>> tables = new HashMap<>();
for (TimeseriesSchema timeseriesSchema : timeseriesSchemas) {
DeviceSchema deviceSchema = timeseriesSchema.getDeviceSchema();
for (String tableName : tableNames) {
StringBuilder builder = new StringBuilder();
builder.append("create table if not exists ").append(deviceSchema.getTable()).append("(");
builder.append("create table if not exists ").append(tableName).append("(");
for (int i = 0; i < deviceSchema.getSensors().size(); i++) {
if (i != 0) builder.append(", ");
builder
Expand Down Expand Up @@ -143,6 +146,15 @@ private void registerTable(Session metaSession, List<TimeseriesSchema> timeserie
}
}

public Set<String> getAllTables(List<TimeseriesSchema> schemaList) {
Set<String> tableNames = new HashSet<>();
for (TimeseriesSchema timeseriesSchema : schemaList) {
DeviceSchema schema = timeseriesSchema.getDeviceSchema();
tableNames.add(schema.getTable());
}
return tableNames;
}

// region select

@Override
Expand Down