Skip to content

Commit e774155

Browse files
committed
[MySQL] Support float constants
1 parent 8aa3682 commit e774155

2 files changed

Lines changed: 68 additions & 7 deletions

File tree

src/sqlancer/mysql/ast/MySQLConstant.java

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,65 @@ public boolean isNull() {
1717
return false;
1818
}
1919

20+
public abstract static class MySQLNoPQSConstant extends MySQLConstant {
21+
22+
@Override
23+
public boolean asBooleanNotNull() {
24+
throw throwException();
25+
}
26+
27+
private RuntimeException throwException() {
28+
throw new UnsupportedOperationException("not applicable for PQS evaluation!");
29+
}
30+
31+
@Override
32+
public MySQLConstant isEquals(MySQLConstant rightVal) {
33+
throw throwException();
34+
35+
}
36+
37+
@Override
38+
public MySQLConstant castAs(CastType type) {
39+
throw throwException();
40+
}
41+
42+
@Override
43+
public String castAsString() {
44+
throw throwException();
45+
46+
}
47+
48+
@Override
49+
public MySQLDataType getType() {
50+
throw throwException();
51+
}
52+
53+
@Override
54+
protected MySQLConstant isLessThan(MySQLConstant rightVal) {
55+
throw throwException();
56+
}
57+
58+
}
59+
60+
public static class MySQLDoubleConstant extends MySQLNoPQSConstant {
61+
62+
private double val;
63+
64+
public MySQLDoubleConstant(double val) {
65+
this.val = val;
66+
if (Double.isInfinite(val) || Double.isNaN(val)) {
67+
// seems to not be supported by MySQL
68+
throw new IgnoreMeException();
69+
}
70+
}
71+
72+
@Override
73+
public String getTextRepresentation() {
74+
return String.valueOf(val);
75+
}
76+
77+
}
78+
2079
public static class MySQLTextConstant extends MySQLConstant {
2180

2281
private final String value;
@@ -29,12 +88,11 @@ public MySQLTextConstant(String value) {
2988
}
3089

3190
private void checkIfSmallFloatingPointText() {
32-
boolean isSmallFloatingPointText = isString()
33-
&& asBooleanNotNull()
91+
boolean isSmallFloatingPointText = isString() && asBooleanNotNull()
3492
&& castAs(CastType.SIGNED).getInt() == 0;
35-
if (isSmallFloatingPointText) {
36-
throw new IgnoreMeException();
37-
}
93+
if (isSmallFloatingPointText) {
94+
throw new IgnoreMeException();
95+
}
3896
}
3997

4098
@Override
@@ -245,7 +303,7 @@ public MySQLDataType getType() {
245303
public boolean isSigned() {
246304
return isSigned;
247305
}
248-
306+
249307
private final String getStringRepr() {
250308
if (isSigned) {
251309
return String.valueOf(value);

src/sqlancer/mysql/gen/MySQLExpressionGenerator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import sqlancer.mysql.ast.MySQLComputableFunction;
2323
import sqlancer.mysql.ast.MySQLComputableFunction.MySQLFunction;
2424
import sqlancer.mysql.ast.MySQLConstant;
25+
import sqlancer.mysql.ast.MySQLConstant.MySQLDoubleConstant;
2526
import sqlancer.mysql.ast.MySQLExists;
2627
import sqlancer.mysql.ast.MySQLExpression;
2728
import sqlancer.mysql.ast.MySQLInOperation;
@@ -127,7 +128,7 @@ private MySQLExpression getFunction(int depth) {
127128
}
128129

129130
private enum ConstantType {
130-
INT, NULL, STRING;
131+
INT, NULL, STRING, DOUBLE;
131132
}
132133

133134
@Override
@@ -151,6 +152,8 @@ public MySQLExpression generateConstant() {
151152
return new MySQLCollate(createStringConstant, Randomly.fromOptions("ascii_bin", "binary"));
152153
}
153154
return createStringConstant;
155+
case DOUBLE:
156+
return new MySQLDoubleConstant(state.getRandomly().getDouble());
154157
default:
155158
throw new AssertionError();
156159
}

0 commit comments

Comments
 (0)