|
| 1 | +package sqlancer.mysql.oracle; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +import sqlancer.IgnoreMeException; |
| 10 | +import sqlancer.Randomly; |
| 11 | +import sqlancer.SQLGlobalState; |
| 12 | +import sqlancer.common.DBMSCommon; |
| 13 | +import sqlancer.common.oracle.CERTOracleBase; |
| 14 | +import sqlancer.common.oracle.TestOracle; |
| 15 | +import sqlancer.common.query.SQLQueryAdapter; |
| 16 | +import sqlancer.common.query.SQLancerResultSet; |
| 17 | +import sqlancer.mysql.MySQLErrors; |
| 18 | +import sqlancer.mysql.MySQLGlobalState; |
| 19 | +import sqlancer.mysql.MySQLSchema.MySQLTables; |
| 20 | +import sqlancer.mysql.MySQLVisitor; |
| 21 | +import sqlancer.mysql.ast.MySQLBinaryLogicalOperation; |
| 22 | +import sqlancer.mysql.ast.MySQLBinaryLogicalOperation.MySQLBinaryLogicalOperator; |
| 23 | +import sqlancer.mysql.ast.MySQLColumnReference; |
| 24 | +import sqlancer.mysql.ast.MySQLExpression; |
| 25 | +import sqlancer.mysql.ast.MySQLSelect; |
| 26 | +import sqlancer.mysql.ast.MySQLTableReference; |
| 27 | +import sqlancer.mysql.gen.MySQLExpressionGenerator; |
| 28 | + |
| 29 | +public class MySQLCERTOracle extends CERTOracleBase<MySQLGlobalState> implements TestOracle<MySQLGlobalState> { |
| 30 | + private MySQLExpressionGenerator gen; |
| 31 | + private MySQLSelect select; |
| 32 | + |
| 33 | + public MySQLCERTOracle(MySQLGlobalState globalState) { |
| 34 | + super(globalState); |
| 35 | + MySQLErrors.addExpressionErrors(errors); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void check() throws SQLException { |
| 40 | + queryPlan1Sequences = new ArrayList<>(); |
| 41 | + queryPlan2Sequences = new ArrayList<>(); |
| 42 | + |
| 43 | + // Randomly generate a query |
| 44 | + MySQLTables tables = state.getSchema().getRandomTableNonEmptyTables(); |
| 45 | + gen = new MySQLExpressionGenerator(state).setColumns(tables.getColumns()); |
| 46 | + List<MySQLExpression> fetchColumns = new ArrayList<>(); |
| 47 | + fetchColumns.addAll(Randomly.nonEmptySubset(tables.getColumns()).stream() |
| 48 | + .map(c -> new MySQLColumnReference(c, null)).collect(Collectors.toList())); |
| 49 | + List<MySQLExpression> tableList = tables.getTables().stream().map(t -> new MySQLTableReference(t)) |
| 50 | + .collect(Collectors.toList()); |
| 51 | + |
| 52 | + select = new MySQLSelect(); |
| 53 | + select.setFetchColumns(fetchColumns); |
| 54 | + select.setFromList(tableList); |
| 55 | + |
| 56 | + select.setSelectType(Randomly.fromOptions(MySQLSelect.SelectType.values())); |
| 57 | + if (Randomly.getBoolean()) { |
| 58 | + select.setWhereClause(gen.generateExpression()); |
| 59 | + } |
| 60 | + if (Randomly.getBoolean()) { |
| 61 | + select.setGroupByExpressions(fetchColumns); |
| 62 | + if (Randomly.getBoolean()) { |
| 63 | + select.setHavingClause(gen.generateExpression()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Set the join. Todo: to make it random |
| 68 | + // List<MySQLExpression> joinExpressions = getJoins(tableList, state); |
| 69 | + // select.setJoinList(joinExpressions); |
| 70 | + |
| 71 | + // Get the result of the first query |
| 72 | + String queryString1 = MySQLVisitor.asString(select); |
| 73 | + int rowCount1 = getRow(state, queryString1, queryPlan1Sequences); |
| 74 | + |
| 75 | + boolean increase = mutate(Mutator.JOIN, Mutator.LIMIT); |
| 76 | + |
| 77 | + // Get the result of the second query |
| 78 | + String queryString2 = MySQLVisitor.asString(select); |
| 79 | + int rowCount2 = getRow(state, queryString2, queryPlan2Sequences); |
| 80 | + |
| 81 | + // Check structural equivalence |
| 82 | + if (DBMSCommon.editDistance(queryPlan1Sequences, queryPlan2Sequences) > 1) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Check the results |
| 87 | + if (increase && rowCount1 > rowCount2 || !increase && rowCount1 < rowCount2) { |
| 88 | + throw new AssertionError("Inconsistent result for query: EXPLAIN " + queryString1 + "; --" + rowCount1 |
| 89 | + + "\nEXPLAIN " + queryString2 + "; --" + rowCount2); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected boolean mutateDistinct() { |
| 95 | + MySQLSelect.SelectType selectType = select.getFromOptions(); |
| 96 | + if (selectType != MySQLSelect.SelectType.ALL) { |
| 97 | + select.setSelectType(MySQLSelect.SelectType.ALL); |
| 98 | + return true; |
| 99 | + } else { |
| 100 | + select.setSelectType(MySQLSelect.SelectType.DISTINCT); |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected boolean mutateWhere() { |
| 107 | + boolean increase = select.getWhereClause() != null; |
| 108 | + if (increase) { |
| 109 | + select.setWhereClause(null); |
| 110 | + } else { |
| 111 | + select.setWhereClause(gen.generateExpression()); |
| 112 | + } |
| 113 | + return increase; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected boolean mutateGroupBy() { |
| 118 | + boolean increase = select.getGroupByExpressions().size() > 0; |
| 119 | + if (increase) { |
| 120 | + select.clearGroupByExpressions(); |
| 121 | + } else { |
| 122 | + select.setGroupByExpressions(select.getFetchColumns()); |
| 123 | + } |
| 124 | + return increase; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected boolean mutateHaving() { |
| 129 | + if (select.getGroupByExpressions().size() == 0) { |
| 130 | + select.setGroupByExpressions(select.getFetchColumns()); |
| 131 | + select.setHavingClause(gen.generateExpression()); |
| 132 | + return false; |
| 133 | + } else { |
| 134 | + if (select.getHavingClause() == null) { |
| 135 | + select.setHavingClause(gen.generateExpression()); |
| 136 | + return false; |
| 137 | + } else { |
| 138 | + select.setHavingClause(null); |
| 139 | + return true; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + protected boolean mutateAnd() { |
| 146 | + if (select.getWhereClause() == null) { |
| 147 | + select.setWhereClause(gen.generateExpression()); |
| 148 | + } else { |
| 149 | + MySQLExpression newWhere = new MySQLBinaryLogicalOperation(select.getWhereClause(), |
| 150 | + gen.generateExpression(), MySQLBinaryLogicalOperator.AND); |
| 151 | + select.setWhereClause(newWhere); |
| 152 | + } |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected boolean mutateOr() { |
| 158 | + if (select.getWhereClause() == null) { |
| 159 | + select.setWhereClause(gen.generateExpression()); |
| 160 | + return false; |
| 161 | + } else { |
| 162 | + MySQLExpression newWhere = new MySQLBinaryLogicalOperation(select.getWhereClause(), |
| 163 | + gen.generateExpression(), MySQLBinaryLogicalOperator.OR); |
| 164 | + select.setWhereClause(newWhere); |
| 165 | + return true; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // The limit clause only accpets positive integers, which is not supported yet |
| 170 | + // private boolean mutateLimit() { |
| 171 | + // boolean increase = select.getLimitClause() != null; |
| 172 | + // if (increase) { |
| 173 | + // select.setLimitClause(null); |
| 174 | + // } else { |
| 175 | + // select.setLimitClause(gen.generateConstant()); |
| 176 | + // } |
| 177 | + // return increase; |
| 178 | + // } |
| 179 | + |
| 180 | + private int getRow(SQLGlobalState<?, ?> globalState, String selectStr, List<String> queryPlanSequences) |
| 181 | + throws AssertionError, SQLException { |
| 182 | + int row = -1; |
| 183 | + String explainQuery = "EXPLAIN " + selectStr; |
| 184 | + |
| 185 | + // Log the query |
| 186 | + if (globalState.getOptions().logEachSelect()) { |
| 187 | + globalState.getLogger().writeCurrent(explainQuery); |
| 188 | + try { |
| 189 | + globalState.getLogger().getCurrentFileWriter().flush(); |
| 190 | + } catch (IOException e) { |
| 191 | + e.printStackTrace(); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + // Get the row count |
| 196 | + SQLQueryAdapter q = new SQLQueryAdapter(explainQuery, errors); |
| 197 | + try (SQLancerResultSet rs = q.executeAndGet(globalState)) { |
| 198 | + if (rs != null) { |
| 199 | + while (rs.next()) { |
| 200 | + int estRows = rs.getInt(10); |
| 201 | + if (row == -1) { |
| 202 | + row = estRows; |
| 203 | + } |
| 204 | + String operation = rs.getString(2); |
| 205 | + queryPlanSequences.add(operation); |
| 206 | + } |
| 207 | + } |
| 208 | + } catch (Exception e) { |
| 209 | + throw new AssertionError(q.getQueryString(), e); |
| 210 | + } |
| 211 | + if (row == -1) { |
| 212 | + throw new IgnoreMeException(); |
| 213 | + } |
| 214 | + return row; |
| 215 | + } |
| 216 | + |
| 217 | +} |
0 commit comments