Skip to content

Commit 7a26fea

Browse files
committed
[MySQL] When using PQS, only use the supported types
1 parent 3054a8e commit 7a26fea

5 files changed

Lines changed: 35 additions & 13 deletions

File tree

src/sqlancer/mysql/MySQLGlobalState.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.sql.SQLException;
55

66
import sqlancer.GlobalState;
7+
import sqlancer.mysql.MySQLOptions.MySQLOracleFactory;
78

89
public class MySQLGlobalState extends GlobalState<MySQLOptions, MySQLSchema> {
910

@@ -12,4 +13,8 @@ protected void updateSchema() throws SQLException {
1213
setSchema(MySQLSchema.fromConnection(getConnection(), getDatabaseName()));
1314
}
1415

16+
public boolean usesPQS() {
17+
return getDmbsSpecificOptions().oracles.stream().anyMatch(o -> o == MySQLOracleFactory.PQS);
18+
}
19+
1520
}

src/sqlancer/mysql/MySQLProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ enum Action implements AbstractAction<MySQLGlobalState> {
5656
CREATE_TABLE((g) -> {
5757
// TODO refactor
5858
String tableName = SQLite3Common.createTableName(g.getSchema().getDatabaseTables().size());
59-
return MySQLTableGenerator.generate(tableName, g.getRandomly(), g.getSchema());
59+
return MySQLTableGenerator.generate(g, tableName);
6060
}), //
6161
DELETE(MySQLDeleteGenerator::delete), //
6262
DROP_INDEX(MySQLDropIndex::generate);
@@ -134,10 +134,9 @@ private static int mapActions(MySQLGlobalState globalState, Action a) {
134134

135135
@Override
136136
public void generateDatabase(MySQLGlobalState globalState) throws SQLException {
137-
Randomly r = globalState.getRandomly();
138137
while (globalState.getSchema().getDatabaseTables().size() < Randomly.smallNumber() + 1) {
139138
String tableName = SQLite3Common.createTableName(globalState.getSchema().getDatabaseTables().size());
140-
Query createTable = MySQLTableGenerator.generate(tableName, r, globalState.getSchema());
139+
Query createTable = MySQLTableGenerator.generate(globalState, tableName);
141140
globalState.executeStatement(createTable);
142141
}
143142

src/sqlancer/mysql/MySQLSchema.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ public class MySQLSchema extends AbstractSchema<MySQLTable> {
2929
public enum MySQLDataType {
3030
INT, VARCHAR, FLOAT, DOUBLE, DECIMAL;
3131

32-
public static MySQLDataType getRandom() {
33-
return Randomly.fromOptions(values());
32+
public static MySQLDataType getRandom(MySQLGlobalState globalState) {
33+
if (globalState.usesPQS()) {
34+
return Randomly.fromOptions(MySQLDataType.INT, MySQLDataType.VARCHAR);
35+
} else {
36+
return Randomly.fromOptions(values());
37+
}
3438
}
3539

3640
public boolean isNumeric() {

src/sqlancer/mysql/gen/MySQLExpressionGenerator.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,21 @@ private MySQLExpression getComputableFunction(int depth) {
134134

135135
private enum ConstantType {
136136
INT, NULL, STRING, DOUBLE;
137+
138+
public static ConstantType[] valuesPQS() {
139+
return new ConstantType[] { INT, NULL, STRING };
140+
}
137141
}
138142

139143
@Override
140144
public MySQLExpression generateConstant() {
141-
switch (Randomly.fromOptions(ConstantType.values())) {
145+
ConstantType[] values;
146+
if (state.usesPQS()) {
147+
values = ConstantType.valuesPQS();
148+
} else {
149+
values = ConstantType.values();
150+
}
151+
switch (Randomly.fromOptions(values)) {
142152
case INT:
143153
return MySQLConstant.createIntConstant((int) state.getRandomly().getInteger());
144154
case NULL:
@@ -155,7 +165,8 @@ public MySQLExpression generateConstant() {
155165
}
156166
MySQLConstant createStringConstant = MySQLConstant.createStringConstant(string);
157167
// if (Randomly.getBoolean()) {
158-
// return new MySQLCollate(createStringConstant, Randomly.fromOptions("ascii_bin", "binary"));
168+
// return new MySQLCollate(createStringConstant,
169+
// Randomly.fromOptions("ascii_bin", "binary"));
159170
// }
160171
if (string.startsWith("1e")) {
161172
// https://bugs.mysql.com/bug.php?id=99146

src/sqlancer/mysql/gen/MySQLTableGenerator.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import sqlancer.common.query.Query;
1313
import sqlancer.common.query.QueryAdapter;
1414
import sqlancer.mysql.MySQLBugs;
15+
import sqlancer.mysql.MySQLGlobalState;
1516
import sqlancer.mysql.MySQLSchema;
1617
import sqlancer.mysql.MySQLSchema.MySQLDataType;
1718
import sqlancer.mysql.MySQLSchema.MySQLTable.MySQLEngine;
@@ -30,16 +31,18 @@ public class MySQLTableGenerator {
3031
private int keysSpecified;
3132
private final List<String> columns = new ArrayList<>();
3233
private final MySQLSchema schema;
34+
private final MySQLGlobalState globalState;
3335

34-
public MySQLTableGenerator(String tableName, Randomly r, MySQLSchema schema) {
36+
public MySQLTableGenerator(MySQLGlobalState globalState, String tableName) {
3537
this.tableName = tableName;
36-
this.r = r;
37-
this.schema = schema;
38+
this.r = globalState.getRandomly();
39+
this.schema = globalState.getSchema();
3840
allowPrimaryKey = Randomly.getBoolean();
41+
this.globalState = globalState;
3942
}
4043

41-
public static Query generate(String tableName, Randomly r, MySQLSchema schema) {
42-
return new MySQLTableGenerator(tableName, r, schema).create();
44+
public static Query generate(MySQLGlobalState globalState, String tableName) {
45+
return new MySQLTableGenerator(globalState, tableName).create();
4346
}
4447

4548
private Query create() {
@@ -257,7 +260,7 @@ private enum ColumnOptions {
257260

258261
private void appendColumnDefinition() {
259262
sb.append(" ");
260-
MySQLDataType randomType = MySQLDataType.getRandom();
263+
MySQLDataType randomType = MySQLDataType.getRandom(globalState);
261264
boolean isTextType = randomType == MySQLDataType.VARCHAR;
262265
appendTypeString(randomType);
263266
sb.append(" ");

0 commit comments

Comments
 (0)