-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathClickHouseProvider.java
More file actions
156 lines (133 loc) · 5.96 KB
/
ClickHouseProvider.java
File metadata and controls
156 lines (133 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package sqlancer.clickhouse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.stream.Collectors;
import com.google.auto.service.AutoService;
import sqlancer.AbstractAction;
import sqlancer.DatabaseProvider;
import sqlancer.IgnoreMeException;
import sqlancer.MainOptions;
import sqlancer.Randomly;
import sqlancer.SQLConnection;
import sqlancer.SQLGlobalState;
import sqlancer.SQLProviderAdapter;
import sqlancer.StatementExecutor;
import sqlancer.clickhouse.ClickHouseProvider.ClickHouseGlobalState;
import sqlancer.clickhouse.gen.ClickHouseCommon;
import sqlancer.clickhouse.gen.ClickHouseInsertGenerator;
import sqlancer.clickhouse.gen.ClickHouseTableGenerator;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.common.query.SQLQueryProvider;
@AutoService(DatabaseProvider.class)
public class ClickHouseProvider extends SQLProviderAdapter<ClickHouseGlobalState, ClickHouseOptions> {
public ClickHouseProvider() {
super(ClickHouseGlobalState.class, ClickHouseOptions.class);
}
public enum Action implements AbstractAction<ClickHouseGlobalState> {
INSERT(ClickHouseInsertGenerator::getQuery);
private final SQLQueryProvider<ClickHouseGlobalState> sqlQueryProvider;
Action(SQLQueryProvider<ClickHouseGlobalState> sqlQueryProvider) {
this.sqlQueryProvider = sqlQueryProvider;
}
@Override
public SQLQueryAdapter getQuery(ClickHouseGlobalState state) throws Exception {
return sqlQueryProvider.getQuery(state);
}
}
private static int mapActions(ClickHouseGlobalState globalState, Action a) {
Randomly r = globalState.getRandomly();
switch (a) {
case INSERT:
return r.getInteger(0, globalState.getOptions().getMaxNumberInserts());
default:
throw new AssertionError(a);
}
}
public static class ClickHouseGlobalState extends SQLGlobalState<ClickHouseOptions, ClickHouseSchema> {
private ClickHouseOptions clickHouseOptions;
public void setClickHouseOptions(ClickHouseOptions clickHouseOptions) {
this.clickHouseOptions = clickHouseOptions;
}
public ClickHouseOptions getClickHouseOptions() {
return this.clickHouseOptions;
}
public String getOracleName() {
return String.join("_",
this.clickHouseOptions.oracle.stream().map(o -> o.toString()).collect(Collectors.toList()));
}
@Override
public String getDatabaseName() {
return super.getDatabaseName() + this.getOracleName();
}
@Override
protected ClickHouseSchema readSchema() throws SQLException {
return ClickHouseSchema.fromConnection(getConnection(), getDatabaseName());
}
}
@Override
public void generateDatabase(ClickHouseGlobalState globalState) throws Exception {
for (int i = 0; i < Randomly.fromOptions(1, 2, 3, 4, 5); i++) {
boolean success;
do {
String tableName = ClickHouseCommon.createTableName(i);
SQLQueryAdapter qt = ClickHouseTableGenerator.createTableStatement(tableName, globalState);
success = globalState.executeStatement(qt);
} while (!success);
}
// TODO: add more Actions to populate table
StatementExecutor<ClickHouseGlobalState, Action> se = new StatementExecutor<>(globalState, Action.values(),
ClickHouseProvider::mapActions, (q) -> {
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException();
}
});
se.executeStatements();
}
@Override
public SQLConnection createDatabase(ClickHouseGlobalState globalState) throws SQLException {
String host = globalState.getOptions().getHost();
int port = globalState.getOptions().getPort();
if (host == null) {
host = ClickHouseOptions.DEFAULT_HOST;
}
if (port == MainOptions.NO_SET_PORT) {
port = ClickHouseOptions.DEFAULT_PORT;
}
ClickHouseOptions clickHouseOptions = globalState.getDbmsSpecificOptions();
globalState.setClickHouseOptions(clickHouseOptions);
String url = String.format("jdbc:clickhouse://%s:%d/%s", host, port, "default");
String databaseName = globalState.getDatabaseName();
Connection con = DriverManager.getConnection(url, globalState.getOptions().getUserName(),
globalState.getOptions().getPassword());
String dropDatabaseCommand = "DROP DATABASE IF EXISTS " + databaseName;
globalState.getState().logStatement(dropDatabaseCommand);
String createDatabaseCommand = "CREATE DATABASE IF NOT EXISTS " + databaseName;
globalState.getState().logStatement(createDatabaseCommand);
String useDatabaseCommand = "USE " + databaseName; // Noop. To reproduce easier.
globalState.getState().logStatement(useDatabaseCommand);
try (Statement s = con.createStatement()) {
s.execute(dropDatabaseCommand);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try (Statement s = con.createStatement()) {
s.execute(createDatabaseCommand);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
con.close();
con = DriverManager.getConnection(
String.format("jdbc:clickhouse://%s:%d/%s?socket_timeout=300000%s", host, port, databaseName,
clickHouseOptions.enableAnalyzer ? "&allow_experimental_analyzer=1" : ""),
globalState.getOptions().getUserName(), globalState.getOptions().getPassword());
return new SQLConnection(con);
}
@Override
public String getDBMSName() {
return "clickhouse";
}
}