|
| 1 | +package sqlancer.hive; |
| 2 | + |
| 3 | +import sqlancer.AbstractAction; |
| 4 | +import sqlancer.DatabaseProvider; |
| 5 | +import sqlancer.IgnoreMeException; |
| 6 | +import sqlancer.MainOptions; |
| 7 | +import sqlancer.Randomly; |
| 8 | +import sqlancer.SQLConnection; |
| 9 | +import sqlancer.SQLProviderAdapter; |
| 10 | +import sqlancer.StatementExecutor; |
| 11 | +import sqlancer.common.query.SQLQueryAdapter; |
| 12 | +import sqlancer.common.query.SQLQueryProvider; |
| 13 | +import sqlancer.hive.gen.HiveInsertGenerator; |
| 14 | +import sqlancer.hive.gen.HiveTableGenerator; |
| 15 | + |
| 16 | +import java.sql.Connection; |
| 17 | +import java.sql.DriverManager; |
| 18 | +import java.sql.SQLException; |
| 19 | +import java.sql.Statement; |
| 20 | + |
| 21 | +import com.google.auto.service.AutoService; |
| 22 | + |
| 23 | +@AutoService(DatabaseProvider.class) |
| 24 | +public class HiveProvider extends SQLProviderAdapter<HiveGlobalState, HiveOptions> { |
| 25 | + |
| 26 | + public HiveProvider() { |
| 27 | + super(HiveGlobalState.class, HiveOptions.class); |
| 28 | + } |
| 29 | + |
| 30 | + public enum Action implements AbstractAction<HiveGlobalState> { |
| 31 | + |
| 32 | + INSERT(HiveInsertGenerator::getQuery); |
| 33 | + |
| 34 | + private final SQLQueryProvider<HiveGlobalState> sqlQueryProvider; |
| 35 | + |
| 36 | + Action(SQLQueryProvider<HiveGlobalState> sqlQueryProvider) { |
| 37 | + this.sqlQueryProvider = sqlQueryProvider; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public SQLQueryAdapter getQuery(HiveGlobalState state) throws Exception { |
| 42 | + return sqlQueryProvider.getQuery(state); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static int mapActions(HiveGlobalState globalState, Action a) { |
| 47 | + Randomly r = globalState.getRandomly(); |
| 48 | + switch (a) { |
| 49 | + case INSERT: |
| 50 | + return r.getInteger(0, globalState.getOptions().getMaxNumberInserts()); |
| 51 | + default: |
| 52 | + throw new AssertionError(a); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void generateDatabase(HiveGlobalState globalState) throws Exception { |
| 58 | + for (int i = 0; i < Randomly.fromOptions(1, 2); i++) { |
| 59 | + boolean success; |
| 60 | + do { |
| 61 | + String tableName = globalState.getSchema().getFreeTableName(); |
| 62 | + SQLQueryAdapter qt = HiveTableGenerator.generate(globalState, tableName); |
| 63 | + success = globalState.executeStatement(qt); |
| 64 | + } while(!success); |
| 65 | + } |
| 66 | + if (globalState.getSchema().getDatabaseTables().isEmpty()) { |
| 67 | + throw new IgnoreMeException(); // TODO |
| 68 | + } |
| 69 | + |
| 70 | + StatementExecutor<HiveGlobalState, Action> se = new StatementExecutor<HiveGlobalState, Action>( |
| 71 | + globalState, Action.values(), |
| 72 | + HiveProvider::mapActions, (q) -> { |
| 73 | + if (globalState.getSchema().getDatabaseTables().isEmpty()) { |
| 74 | + throw new IgnoreMeException(); |
| 75 | + } |
| 76 | + }); |
| 77 | + se.executeStatements(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public SQLConnection createDatabase(HiveGlobalState globalState) throws SQLException { |
| 82 | + String username = globalState.getOptions().getUserName(); |
| 83 | + String password = globalState.getOptions().getPassword(); |
| 84 | + String host = globalState.getOptions().getHost(); |
| 85 | + int port = globalState.getOptions().getPort(); |
| 86 | + if (host == null) { |
| 87 | + host = HiveOptions.DEFAULT_HOST; |
| 88 | + } |
| 89 | + if (port == MainOptions.NO_SET_PORT) { |
| 90 | + port = HiveOptions.DEFAULT_PORT; |
| 91 | + } |
| 92 | + |
| 93 | + String databaseName = globalState.getDatabaseName(); |
| 94 | + |
| 95 | + String url = String.format("jdbc:hive2://%s:%d/%s", host, port, "default"); |
| 96 | + Connection con = DriverManager.getConnection(url, username, password); |
| 97 | + globalState.getState().logStatement("DROP DATABASE IF EXISTS " + databaseName + " CASCADE"); |
| 98 | + globalState.getState().logStatement("CREATE DATABASE " + databaseName); |
| 99 | + globalState.getState().logStatement("USE " + databaseName); |
| 100 | + try (Statement s = con.createStatement()) { |
| 101 | + s.execute("DROP DATABASE IF EXISTS " + databaseName + " CASCADE"); |
| 102 | + } |
| 103 | + try (Statement s = con.createStatement()) { |
| 104 | + s.execute("CREATE DATABASE " + databaseName); |
| 105 | + } |
| 106 | + try (Statement s = con.createStatement()) { |
| 107 | + s.execute("USE " + databaseName); |
| 108 | + } |
| 109 | + con.close(); |
| 110 | + con = DriverManager.getConnection( |
| 111 | + String.format("jdbc:hive2://%s:%d/%s", host, port, databaseName, |
| 112 | + username, password)); |
| 113 | + |
| 114 | + return new SQLConnection(con); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String getDBMSName() { |
| 119 | + return "hive"; |
| 120 | + } |
| 121 | +} |
0 commit comments