forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClickHouseProvider.java
More file actions
245 lines (203 loc) · 9.6 KB
/
Copy pathClickHouseProvider.java
File metadata and controls
245 lines (203 loc) · 9.6 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package sqlancer.clickhouse;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;
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.ClickHouseAlterGenerator;
import sqlancer.clickhouse.gen.ClickHouseCommon;
import sqlancer.clickhouse.gen.ClickHouseInsertGenerator;
import sqlancer.clickhouse.gen.ClickHouseMutationGenerator;
import sqlancer.clickhouse.gen.ClickHouseTableGenerator;
import sqlancer.clickhouse.gen.ClickHouseViewGenerator;
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),
ALTER(ClickHouseAlterGenerator::getQuery),
MUTATION(ClickHouseMutationGenerator::getQuery),
VIEW(ClickHouseViewGenerator::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());
case ALTER:
return Randomly.fromOptions(0, 0, 0, 0, 1);
case MUTATION:
return Randomly.fromOptions(0, 0, 0, 0, 1, 1, 1, 2);
case VIEW:
return Randomly.fromOptions(0, 0, 0, 1);
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() {
String base = super.getDatabaseName();
String suffix = this.getOracleName();
int maxSuffix = 200 - base.length();
if (suffix.length() <= maxSuffix) {
return base + suffix;
}
return base + "o" + Integer.toHexString(suffix.hashCode());
}
@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);
}
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 databaseName = globalState.getDatabaseName();
return createDatabaseClient(globalState, host, port, databaseName, clickHouseOptions);
}
private static void runSetupCommandsWithTolerance(sqlancer.clickhouse.transport.ClickHouseTransport transport,
String dropDatabaseCommand, String createDatabaseCommand, String useDatabaseCommand) throws SQLException {
try {
transport.executeUpdate(dropDatabaseCommand);
transport.executeUpdate(createDatabaseCommand);
transport.executeUpdate(useDatabaseCommand);
} catch (SQLException e) {
sqlancer.common.query.ExpectedErrors tolerated = sqlancer.common.query.ExpectedErrors.newErrors()
.with(ClickHouseErrors.getExpectedExpressionErrors()).build();
if (tolerated.errorIsExpected(e.getMessage())) {
throw new IgnoreMeException();
}
throw e;
}
}
private SQLConnection createDatabaseClient(ClickHouseGlobalState globalState, String host, int port,
String databaseName, ClickHouseOptions clickHouseOptions) throws SQLException {
java.util.LinkedHashMap<String, String> settings = new java.util.LinkedHashMap<>();
settings.put("max_execution_time", "30");
settings.put("wait_end_of_query", "1");
settings.put("http_response_buffer_size", "104857600");
settings.put("max_result_rows", "1000000");
settings.put("result_overflow_mode", "throw");
settings.put("allow_experimental_analyzer", "1");
settings.put("allow_experimental_variant_type", "1");
settings.put("allow_experimental_dynamic_type", "1");
settings.put("allow_experimental_json_type", "1");
settings.put("allow_experimental_vector_similarity_index", "1");
if (clickHouseOptions.enableLowCardinality) {
settings.put("allow_suspicious_low_cardinality_types", "1");
}
sqlancer.clickhouse.transport.ClickHouseClientV2Transport transport = new sqlancer.clickhouse.transport.ClickHouseClientV2Transport(
host, port, globalState.getOptions().getUserName(), globalState.getOptions().getPassword(), "default",
settings, 5_000L, 60_000L);
String dropDatabaseCommand = "DROP DATABASE IF EXISTS " + databaseName + " SYNC";
String createDatabaseCommand = "CREATE DATABASE IF NOT EXISTS " + databaseName;
String useDatabaseCommand = "USE " + databaseName;
globalState.getState().logStatement(dropDatabaseCommand);
globalState.getState().logStatement(createDatabaseCommand);
globalState.getState().logStatement(useDatabaseCommand);
runSetupCommandsWithTolerance(transport, dropDatabaseCommand, createDatabaseCommand, useDatabaseCommand);
Connection con = new sqlancer.clickhouse.transport.ClickHouseTransportConnection(transport);
if (clickHouseOptions.randomSessionSettings) {
applyRandomSessionSettings(globalState, clickHouseOptions, con);
}
return new SQLConnection(con);
}
private static void applyRandomSessionSettings(ClickHouseGlobalState globalState,
ClickHouseOptions clickHouseOptions, Connection con) throws SQLException {
LinkedHashMap<String, String> profile = ClickHouseSessionSettings.pickRandomProfile(globalState.getRandomly(),
clickHouseOptions.randomSessionSettingsBudget);
int attempted = 0;
int accepted = 0;
for (Map.Entry<String, String> entry : profile.entrySet()) {
String stmt = "SET " + entry.getKey() + " = " + entry.getValue();
globalState.getState().logStatement(stmt);
attempted++;
try (Statement s = con.createStatement()) {
s.execute(stmt);
accepted++;
} catch (SQLException e) {
String msg = e.getMessage();
if (msg == null || !isExpectedSessionSettingError(msg)) {
throw e;
}
}
}
globalState.getState()
.logStatement(String.format("-- session-settings applied: %d of %d", accepted, attempted));
}
private static boolean isExpectedSessionSettingError(String msg) {
for (String pattern : ClickHouseErrors.getSessionSettingsErrors()) {
if (msg.contains(pattern)) {
return true;
}
}
return false;
}
@Override
public String getDBMSName() {
return "clickhouse";
}
}