|
| 1 | +package sqlancer.datafusion; |
| 2 | + |
| 3 | +import static java.lang.System.exit; |
| 4 | +import static sqlancer.datafusion.DataFusionUtil.DataFusionLogger.DataFusionLogType.DML; |
| 5 | +import static sqlancer.datafusion.DataFusionUtil.displayTables; |
| 6 | + |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.DriverManager; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Properties; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +import com.google.auto.service.AutoService; |
| 15 | + |
| 16 | +import sqlancer.DatabaseProvider; |
| 17 | +import sqlancer.IgnoreMeException; |
| 18 | +import sqlancer.Randomly; |
| 19 | +import sqlancer.SQLConnection; |
| 20 | +import sqlancer.SQLGlobalState; |
| 21 | +import sqlancer.SQLProviderAdapter; |
| 22 | +import sqlancer.common.query.SQLQueryAdapter; |
| 23 | +import sqlancer.datafusion.DataFusionProvider.DataFusionGlobalState; |
| 24 | +import sqlancer.datafusion.DataFusionSchema.DataFusionTable; |
| 25 | +import sqlancer.datafusion.DataFusionUtil.DataFusionInstanceID; |
| 26 | +import sqlancer.datafusion.DataFusionUtil.DataFusionLogger; |
| 27 | +import sqlancer.datafusion.gen.DataFusionInsertGenerator; |
| 28 | +import sqlancer.datafusion.gen.DataFusionTableGenerator; |
| 29 | + |
| 30 | +@AutoService(DatabaseProvider.class) |
| 31 | +public class DataFusionProvider extends SQLProviderAdapter<DataFusionGlobalState, DataFusionOptions> { |
| 32 | + |
| 33 | + public DataFusionProvider() { |
| 34 | + super(DataFusionGlobalState.class, DataFusionOptions.class); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void generateDatabase(DataFusionGlobalState globalState) throws Exception { |
| 39 | + int tableCount = Randomly.fromOptions(1, 2, 3, 4, 5, 6, 7); |
| 40 | + for (int i = 0; i < tableCount; i++) { |
| 41 | + SQLQueryAdapter queryCreateRandomTable = new DataFusionTableGenerator().getQuery(globalState); |
| 42 | + queryCreateRandomTable.execute(globalState); |
| 43 | + globalState.updateSchema(); |
| 44 | + globalState.dfLogger.appendToLog(DML, queryCreateRandomTable.toString() + "\n"); |
| 45 | + } |
| 46 | + |
| 47 | + // Now only `INSERT` DML is supported |
| 48 | + // If more DMLs are added later, should use`StatementExecutor` instead |
| 49 | + // (see DuckDB's implementation for reference) |
| 50 | + |
| 51 | + globalState.updateSchema(); |
| 52 | + List<DataFusionTable> allTables = globalState.getSchema().getDatabaseTables(); |
| 53 | + List<String> allTablesName = allTables.stream().map(t -> t.getName()).collect(Collectors.toList()); |
| 54 | + if (allTablesName.isEmpty()) { |
| 55 | + System.out.println("Generate database failed"); |
| 56 | + exit(1); |
| 57 | + } |
| 58 | + |
| 59 | + // Randomly insert some data into existing tables |
| 60 | + for (DataFusionTable table : allTables) { |
| 61 | + int nInsertQuery = globalState.getRandomly().getInteger(0, 8); // [0, 10) |
| 62 | + |
| 63 | + for (int i = 0; i < nInsertQuery; i++) { |
| 64 | + SQLQueryAdapter insertQuery = null; |
| 65 | + try { |
| 66 | + insertQuery = DataFusionInsertGenerator.getQuery(globalState, table); |
| 67 | + } catch (IgnoreMeException e) { |
| 68 | + // Only for special case: table has 0 column |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + insertQuery.execute(globalState); |
| 73 | + globalState.dfLogger.appendToLog(DML, insertQuery.toString() + "\n"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // TODO(datafusion) add `DataFUsionLogType.STATE` for this whole db state log |
| 78 | + if (globalState.getDbmsSpecificOptions().showDebugInfo) { |
| 79 | + System.out.println(displayTables(globalState, allTablesName)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public SQLConnection createDatabase(DataFusionGlobalState globalState) throws SQLException { |
| 85 | + if (globalState.getDbmsSpecificOptions().showDebugInfo) { |
| 86 | + System.out.println("A new database get created!\n"); |
| 87 | + } |
| 88 | + Properties props = new Properties(); |
| 89 | + props.setProperty("UseEncryption", "false"); |
| 90 | + // must set 'user' and 'password' to trigger server 'do_handshake()' |
| 91 | + props.setProperty("user", "foo"); |
| 92 | + props.setProperty("password", "bar"); |
| 93 | + props.setProperty("create", globalState.getDatabaseName()); // Hack: use this property to let DataFusion server |
| 94 | + // clear the current context |
| 95 | + String url = "jdbc:arrow-flight-sql://127.0.0.1:50051"; |
| 96 | + Connection connection = DriverManager.getConnection(url, props); |
| 97 | + |
| 98 | + return new SQLConnection(connection); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String getDBMSName() { |
| 103 | + return "datafusion"; |
| 104 | + } |
| 105 | + |
| 106 | + // If run SQLancer with multiple thread |
| 107 | + // Each thread's instance will have its own `DataFusionGlobalState` |
| 108 | + // It will store global states including: |
| 109 | + // JDBC connection to DataFusion server |
| 110 | + // Logger for this thread |
| 111 | + public static class DataFusionGlobalState extends SQLGlobalState<DataFusionOptions, DataFusionSchema> { |
| 112 | + public DataFusionLogger dfLogger; |
| 113 | + DataFusionInstanceID id; |
| 114 | + |
| 115 | + public DataFusionGlobalState() { |
| 116 | + // HACK: test will only run in spawned thread, not main thread |
| 117 | + // this way redundant logger files won't be created |
| 118 | + if (Thread.currentThread().getName().equals("main")) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + id = new DataFusionInstanceID(Thread.currentThread().getName()); |
| 123 | + try { |
| 124 | + dfLogger = new DataFusionLogger(this, id); |
| 125 | + } catch (Exception e) { |
| 126 | + throw new IgnoreMeException(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected DataFusionSchema readSchema() throws SQLException { |
| 132 | + return DataFusionSchema.fromConnection(getConnection(), getDatabaseName()); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments