-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPrestoProvider.java
More file actions
195 lines (175 loc) · 8 KB
/
PrestoProvider.java
File metadata and controls
195 lines (175 loc) · 8 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
package sqlancer.presto;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
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.SQLProviderAdapter;
import sqlancer.StatementExecutor;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.common.query.SQLQueryProvider;
import sqlancer.presto.gen.PrestoInsertGenerator;
import sqlancer.presto.gen.PrestoTableGenerator;
@AutoService(DatabaseProvider.class)
public class PrestoProvider extends SQLProviderAdapter<PrestoGlobalState, PrestoOptions> {
public PrestoProvider() {
super(PrestoGlobalState.class, PrestoOptions.class);
}
// TODO : check actions based on connector
// returns number of actions
private static int mapActions(PrestoGlobalState globalState, Action a) {
Randomly r = globalState.getRandomly();
if (Objects.requireNonNull(a) == Action.INSERT) {
return r.getInteger(0, globalState.getOptions().getMaxNumberInserts());
// case UPDATE:
// return r.getInteger(0, globalState.getDbmsSpecificOptions().maxNumUpdates + 1);
// case EXPLAIN:
// return r.getInteger(0, 2);
// case DELETE:
// return r.getInteger(0, globalState.getDbmsSpecificOptions().maxNumDeletes + 1);
// case CREATE_VIEW:
// return r.getInteger(0, globalState.getDbmsSpecificOptions().maxNumViews + 1);
}
throw new AssertionError(a);
}
@Override
public void generateDatabase(PrestoGlobalState globalState) throws Exception {
for (int i = 0; i < Randomly.fromOptions(1, 2); i++) {
boolean success;
do {
SQLQueryAdapter qt = new PrestoTableGenerator().getQuery(globalState);
success = globalState.executeStatement(qt);
} while (!success);
}
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException(); // TODO
}
StatementExecutor<PrestoGlobalState, Action> se = new StatementExecutor<>(globalState, Action.values(),
PrestoProvider::mapActions, (q) -> {
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException();
}
});
se.executeStatements();
}
@Override
public SQLConnection createDatabase(PrestoGlobalState globalState) throws SQLException {
String username = globalState.getOptions().getUserName();
String password = globalState.getOptions().getPassword();
boolean useSSl = true;
if (globalState.getOptions().isDefaultUsername() && globalState.getOptions().isDefaultPassword()) {
username = "presto";
password = null;
useSSl = false;
}
String host = globalState.getOptions().getHost();
int port = globalState.getOptions().getPort();
if (host == null) {
host = PrestoOptions.DEFAULT_HOST;
}
if (port == MainOptions.NO_SET_PORT) {
port = PrestoOptions.DEFAULT_PORT;
}
String catalogName = globalState.getDbmsSpecificOptions().catalog;
String databaseName = globalState.getDatabaseName();
String url = String.format("jdbc:presto://%s:%d/%s?SSL=%b", host, port, catalogName, useSSl);
Connection con = DriverManager.getConnection(url, username, password);
List<String> schemaNames = getSchemaNames(con, catalogName, databaseName);
dropExistingTables(con, catalogName, databaseName, schemaNames);
dropSchema(globalState, con, catalogName, databaseName);
createSchema(globalState, con, catalogName, databaseName);
useSchema(globalState, con, catalogName, databaseName);
return new SQLConnection(con);
}
private static void useSchema(PrestoGlobalState globalState, Connection con, String catalogName,
String databaseName) throws SQLException {
globalState.getState().logStatement("USE " + catalogName + "." + databaseName);
try (Statement s = con.createStatement()) {
s.execute("USE " + catalogName + "." + databaseName);
}
}
private static void createSchema(PrestoGlobalState globalState, Connection con, String catalogName,
String databaseName) throws SQLException {
globalState.getState().logStatement("CREATE SCHEMA IF NOT EXISTS " + catalogName + "." + databaseName);
try (Statement s = con.createStatement()) {
s.execute("CREATE SCHEMA IF NOT EXISTS " + catalogName + "." + databaseName);
}
}
private static void dropSchema(PrestoGlobalState globalState, Connection con, String catalogName,
String databaseName) throws SQLException {
globalState.getState().logStatement("DROP SCHEMA IF EXISTS " + catalogName + "." + databaseName);
try (Statement s = con.createStatement()) {
s.execute("DROP SCHEMA IF EXISTS " + catalogName + "." + databaseName);
}
}
private static List<String> getSchemaNames(Connection con, String catalogName, String databaseName)
throws SQLException {
List<String> schemaNames = new ArrayList<>();
final String showSchemasSql = "SHOW SCHEMAS FROM " + catalogName + " LIKE '" + databaseName + "'";
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery(showSchemasSql)) {
while (rs.next()) {
schemaNames.add(rs.getString("Schema"));
}
}
}
return schemaNames;
}
private static void dropExistingTables(Connection con, String catalogName, String databaseName,
List<String> schemaNames) throws SQLException {
if (!schemaNames.isEmpty()) {
List<String> tableNames = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("SHOW TABLES FROM " + catalogName + "." + databaseName)) {
while (rs.next()) {
tableNames.add(rs.getString("Table"));
}
}
}
try (Statement s = con.createStatement()) {
for (String tableName : tableNames) {
s.execute("DROP TABLE IF EXISTS " + catalogName + "." + databaseName + "." + tableName);
}
}
}
}
@Override
public String getDBMSName() {
return "presto";
}
public enum Action implements AbstractAction<PrestoGlobalState> {
// SHOW_TABLES((g) -> new SQLQueryAdapter("SHOW TABLES", new ExpectedErrors(), false, false)), //
INSERT(PrestoInsertGenerator::getQuery);
// TODO : check actions based on connector
// DELETE(PrestoDeleteGenerator::generate), //
// UPDATE(PrestoUpdateGenerator::getQuery), //
// CREATE_VIEW(PrestoViewGenerator::generate), //
// EXPLAIN((g) -> {
// ExpectedErrors errors = new ExpectedErrors();
// PrestoErrors.addExpressionErrors(errors);
// PrestoErrors.addGroupByErrors(errors);
// return new SQLQueryAdapter(
// "EXPLAIN " + PrestoToStringVisitor
// .asString(PrestoRandomQuerySynthesizer.generateSelect(g, Randomly.smallNumber() + 1)),
// errors);
// });
private final SQLQueryProvider<PrestoGlobalState> sqlQueryProvider;
Action(SQLQueryProvider<PrestoGlobalState> sqlQueryProvider) {
this.sqlQueryProvider = sqlQueryProvider;
}
@Override
public SQLQueryAdapter getQuery(PrestoGlobalState state) throws Exception {
return sqlQueryProvider.getQuery(state);
}
}
}