Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/refactor_stonedb' into refactor_…
Browse files Browse the repository at this point in the history
…stonedb
  • Loading branch information
[email protected] committed Jul 31, 2023
2 parents d7829c5 + 9cbfbcd commit 2d9abce
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ jobs:
docker exec $ContainerID bash
sleep 60s
docker exec $ContainerID sed -i "s/tianmu_insert_delayed=1/tianmu_insert_delayed=0/" /opt/stonedb57/install/my.cnf
sleep 60s
docker exec $ContainerID /opt/stonedb57/install/bin/mysql -uroot -p123456 -e "CREATE USER 'sqlancer'@'%' IDENTIFIED WITH mysql_native_password BY 'sqlancer'; GRANT ALL PRIVILEGES ON *.* TO 'sqlancer'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;"
- name: Set up JDK 11
uses: actions/[email protected]
Expand Down
4 changes: 4 additions & 0 deletions src/sqlancer/stonedb/StoneDBBugs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
public final class StoneDBBugs {
// https://github.com/stoneatom/stonedb/issues/1933
public static boolean bug1933 = true;
// https://github.com/stoneatom/stonedb/issues/1942
public static boolean bug1942 = true;
// https://github.com/stoneatom/stonedb/issues/1953
public static boolean bug1953 = true;

private StoneDBBugs() {
}
Expand Down
2 changes: 0 additions & 2 deletions src/sqlancer/stonedb/StoneDBErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ private StoneDBErrors() {
}

public static void addExpectedExpressionErrors(ExpectedErrors errors) {
// java.sql.SQLException: Incorrect DATETIME value: '292269055-12-02 16:47:04'
errors.add("Incorrect DATETIME value: ");
// java.sql.SQLException: Incorrect DATE value: '292269055-12-02'
errors.add("Incorrect DATE value: ");
// java.sql.SQLException: Incorrect string value: '\xBC\xE7\xC9\x91\x05R...' for column 'c1' at row 1
Expand Down
14 changes: 7 additions & 7 deletions src/sqlancer/stonedb/StoneDBSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static StoneDBDataType getRandom() {
return Randomly.fromOptions(values());
}

public static Node<StoneDBExpression> getRandomValue(StoneDBDataType dataType) {
public static Node<StoneDBExpression> getRandomValue(StoneDBDataType dataType, Randomly r) {
switch (dataType) {
case TINYINT:
break;
Expand Down Expand Up @@ -72,7 +72,7 @@ public static Node<StoneDBExpression> getRandomValue(StoneDBDataType dataType) {
break;
case TEXT:
StringGenerationStrategy strategy = StringGenerationStrategy.ALPHANUMERIC;
String str = strategy.getString(new Randomly());
String str = strategy.getString(r);
return StoneDBConstant.createTextConstant(str);
case MEDIUMTEXT:
break;
Expand All @@ -96,7 +96,7 @@ public static Node<StoneDBExpression> getRandomValue(StoneDBDataType dataType) {
return null;
}

public static String getTypeAndValue(StoneDBDataType dataType) {
public static String getTypeAndValue(StoneDBDataType dataType, Randomly r) {
StringBuilder sb = new StringBuilder();
switch (dataType) {
case TINYINT:
Expand Down Expand Up @@ -130,10 +130,10 @@ public static String getTypeAndValue(StoneDBDataType dataType) {
case TIMESTAMP:
return "TIMESTAMP";
case CHAR:
sb.append("CHAR").append(Randomly.fromOptions("", "(" + new Randomly().getInteger(0, 255) + ")"));
sb.append("CHAR").append(Randomly.fromOptions("", "(" + r.getInteger(0, 255) + ")"));
return sb.toString();
case VARCHAR:
sb.append("VARCHAR").append("(").append(new Randomly().getInteger(0, 65535)).append(")");
sb.append("VARCHAR").append("(").append(r.getInteger(0, 65535)).append(")");
return sb.toString();
case TINYTEXT:
return "TINYTEXT";
Expand All @@ -144,10 +144,10 @@ public static String getTypeAndValue(StoneDBDataType dataType) {
case LONGTEXT:
return "LONGTEXT";
case BINARY:
sb.append("BINARY").append("(").append(new Randomly().getInteger(0, 255)).append(")");
sb.append("BINARY").append("(").append(r.getInteger(0, 255)).append(")");
return sb.toString();
case VARBINARY:
sb.append("VARBINARY").append("(").append(new Randomly().getInteger(0, 65535)).append(")");
sb.append("VARBINARY").append("(").append(r.getInteger(0, 65535)).append(")");
return sb.toString();
case TINYBLOB:
return "TINYBLOB";
Expand Down
22 changes: 20 additions & 2 deletions src/sqlancer/stonedb/ast/StoneDBConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,16 @@ public static class StoneDBDateConstant extends StoneDBConstant {
public String textRepr;

public StoneDBDateConstant(long val) {
Timestamp timestamp = new Timestamp(val);
long validValue = val;
// 9999-12-31 23:59:59.000999
if (validValue > 253402271999999L) {
validValue = 253402271999999L;
}
// 1000-01-01 00:00:00.000000
if (validValue < -30609820800000L) {
validValue = -30609820800000L;
}
Timestamp timestamp = new Timestamp(validValue);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
textRepr = dateFormat.format(timestamp);
}
Expand All @@ -160,7 +169,16 @@ public static class StoneDBTimestampConstant extends StoneDBConstant {
public String textRepr;

public StoneDBTimestampConstant(long val) {
Timestamp timestamp = new Timestamp(val);
long validValue = val;
// 9999-12-31 23:59:59.000999
if (validValue > 253402271999999L) {
validValue = 253402271999999L;
}
// 1000-01-01 00:00:00.000000
if (validValue < -30609820800000L) {
validValue = -30609820800000L;
}
Timestamp timestamp = new Timestamp(validValue);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
textRepr = dateFormat.format(timestamp);
}
Expand Down
12 changes: 9 additions & 3 deletions src/sqlancer/stonedb/gen/StoneDBExpressionGenerator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sqlancer.stonedb.gen;

import static sqlancer.stonedb.StoneDBBugs.bug1942;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -87,7 +89,7 @@ public Node<StoneDBExpression> generateConstant(StoneDBDataType dataType) {
return StoneDBConstant.createTimestampConstant(globalState.getRandomly().getInteger());
case VARCHAR:
StringGenerationStrategy strategy = StringGenerationStrategy.ALPHANUMERIC;
String str = strategy.getString(new Randomly());
String str = strategy.getString(globalState.getRandomly());
return StoneDBConstant.createTextConstant(str);
case DOUBLE:
return StoneDBConstant.createDoubleConstant(globalState.getRandomly().getDouble());
Expand Down Expand Up @@ -122,7 +124,11 @@ protected Node<StoneDBExpression> generateExpression(int depth) {
op = StoneDBUnaryPrefixOperator.getRandom();
return new NewUnaryPrefixOperatorNode<>(generateExpression(depth + 1), op);
case UNARY_POSTFIX:
op = StoneDBUnaryPostfixOperator.getRandom();
if (!bug1942) {
op = StoneDBUnaryPostfixOperator.getRandom();
} else {
op = StoneDBUnaryPostfixOperator.IS_NULL;
}
return new NewUnaryPostfixOperatorNode<>(generateExpression(depth + 1), op);
case BINARY_COMPARISON:
op = StoneDBBinaryComparisonOperator.getRandom();
Expand Down Expand Up @@ -256,7 +262,7 @@ public String getTextRepresentation() {
*/
public enum StoneDBBinaryLogicalOperator implements Operator {

AND("AND"), OR("OR");
AND("AND"), OR("OR"), XOR("XOR");

private final String textRepr;

Expand Down
6 changes: 4 additions & 2 deletions src/sqlancer/stonedb/gen/StoneDBTableAlterGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ private void appendAlterOption(Action action) {
sb.append("ADD COLUMN ");
String columnName = table.getFreeColumnName();
sb.append(" ").append(columnName).append(" ");
sb.append(StoneDBDataType.getTypeAndValue(StoneDBDataType.getRandomWithoutNull()));
sb.append(
StoneDBDataType.getTypeAndValue(StoneDBDataType.getRandomWithoutNull(), globalState.getRandomly()));
// java.sql.SQLSyntaxErrorException: Column length too big for column 'c1' (max = 16383); use BLOB or TEXT
// instead
errors.addRegex(Pattern
Expand Down Expand Up @@ -105,7 +106,8 @@ private void appendAlterOption(Action action) {
String oldColumnName = table.getRandomColumn().getName();
String newColumnName = table.getFreeColumnName();
sb.append(oldColumnName).append(" ").append(newColumnName).append(" ");
sb.append(StoneDBDataType.getTypeAndValue(StoneDBDataType.getRandomWithoutNull()));
sb.append(
StoneDBDataType.getTypeAndValue(StoneDBDataType.getRandomWithoutNull(), globalState.getRandomly()));
// java.sql.SQLSyntaxErrorException: Column length too big for column 'c1' (max = 16383); use BLOB or TEXT
// instead
errors.addRegex(Pattern
Expand Down
2 changes: 1 addition & 1 deletion src/sqlancer/stonedb/gen/StoneDBTableCreateGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,6 @@ private void appendColumnOption(StoneDBDataType type) {
}

private void appendType(StoneDBDataType randomType) {
sb.append(StoneDBDataType.getTypeAndValue(randomType));
sb.append(StoneDBDataType.getTypeAndValue(randomType, r));
}
}
9 changes: 4 additions & 5 deletions src/sqlancer/stonedb/gen/StoneDBTableUpdateGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ public void appendAssignmentList() {
}
sb.append(table.getColumns().get(i));
sb.append(" = ");
sb.append(
Randomly.fromOptions(
StoneDBToStringVisitor.asString(StoneDBSchema.StoneDBDataType
.getRandomValue(table.getColumns().get(i).getType().getPrimitiveDataType())),
"DEFAULT"));
sb.append(Randomly.fromOptions(
StoneDBToStringVisitor.asString(StoneDBSchema.StoneDBDataType.getRandomValue(
table.getColumns().get(i).getType().getPrimitiveDataType(), globalState.getRandomly())),
"DEFAULT"));
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/sqlancer/stonedb/oracle/StoneDBNoRECOracle.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sqlancer.stonedb.oracle;

import static sqlancer.stonedb.StoneDBBugs.bug1953;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand Down Expand Up @@ -79,6 +81,9 @@ private int getUnoptimizedQueryCount(List<Node<StoneDBExpression>> tableList,
select.setFromList(tableList);
select.setJoinList(joins);
unoptimizedQueryString = "SELECT SUM(count) FROM (" + StoneDBToStringVisitor.asString(select) + ") as res;";
if (bug1953) {
unoptimizedQueryString = "SELECT * FROM (" + StoneDBToStringVisitor.asString(select) + ") as res;";
}
SQLQueryAdapter q = new SQLQueryAdapter(unoptimizedQueryString, errors);
SQLancerResultSet rs;
try {
Expand All @@ -90,8 +95,14 @@ private int getUnoptimizedQueryCount(List<Node<StoneDBExpression>> tableList,
return -1;
}
int secondCount = 0;
if (rs.next()) {
secondCount += rs.getLong(1);
if (bug1953) {
while (rs.next()) {
secondCount += rs.getInt(1);
}
} else {
if (rs.next()) {
secondCount += rs.getLong(1);
}
}
rs.close();
return secondCount;
Expand Down

0 comments on commit 2d9abce

Please sign in to comment.