-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathComparatorHelper.java
More file actions
196 lines (177 loc) · 8.87 KB
/
ComparatorHelper.java
File metadata and controls
196 lines (177 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package sqlancer;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.common.query.SQLancerResultSet;
public final class ComparatorHelper {
private ComparatorHelper() {
}
public static boolean isEqualDouble(String first, String second) {
try {
double val = Double.parseDouble(first);
double secVal = Double.parseDouble(second);
return equals(val, secVal);
} catch (Exception e) {
return false;
}
}
static boolean equals(double a, double b) {
if (a == b) {
return true;
}
// If the difference is less than epsilon, treat as equal.
return Math.abs(a - b) < 0.001 * Math.max(Math.abs(a), Math.abs(b)) + 0.001;
}
public static List<String> getResultSetFirstColumnAsString(String queryString, ExpectedErrors errors,
SQLGlobalState<?, ?> state) throws SQLException {
if (state.getOptions().logEachSelect()) {
// TODO: refactor me
state.getLogger().writeCurrent(queryString);
try {
state.getLogger().getCurrentFileWriter().flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
boolean canonicalizeString = state.getOptions().canonicalizeSqlString();
SQLQueryAdapter q = new SQLQueryAdapter(queryString, errors, true, canonicalizeString);
List<String> resultSet = new ArrayList<>();
SQLancerResultSet result = null;
try {
result = q.executeAndGet(state);
if (result == null) {
throw new IgnoreMeException();
}
while (result.next()) {
String resultTemp = result.getString(1);
if (resultTemp != null) {
resultTemp = resultTemp.replaceAll("[\\.]0+$", ""); // Remove the trailing zeros as many DBMS treat
// it as non-bugs
}
resultSet.add(resultTemp);
}
} catch (Exception e) {
if (e instanceof IgnoreMeException) {
throw e;
}
if (e.getMessage() == null) {
throw new AssertionError(queryString, e);
}
if (errors.errorIsExpected(e.getMessage())) {
throw new IgnoreMeException();
}
throw new AssertionError(queryString, e);
} finally {
if (result != null && !result.isClosed()) {
result.close();
}
}
return resultSet;
}
public static void assumeResultSetsAreEqual(List<String> resultSet, List<String> secondResultSet,
String originalQueryString, List<String> combinedString, SQLGlobalState<?, ?> state) {
if (resultSet.size() != secondResultSet.size()) {
String queryFormatString = "-- %s;" + System.lineSeparator() + "-- cardinality: %d"
+ System.lineSeparator();
String firstQueryString = String.format(queryFormatString, originalQueryString, resultSet.size());
String combinedQueryString = String.join(";", combinedString);
String secondQueryString = String.format(queryFormatString, combinedQueryString, secondResultSet.size());
state.getState().getLocalState()
.log(String.format("%s" + System.lineSeparator() + "%s", firstQueryString, secondQueryString));
String assertionMessage = String.format(
"The size of the result sets mismatch (%d and %d)!" + System.lineSeparator()
+ "First query: \"%s\", whose cardinality is: %d" + System.lineSeparator()
+ "Second query:\"%s\", whose cardinality is: %d",
resultSet.size(), secondResultSet.size(), originalQueryString, resultSet.size(),
combinedQueryString, secondResultSet.size());
throw new AssertionError(assertionMessage);
}
Set<String> firstHashSet = new HashSet<>(resultSet);
Set<String> secondHashSet = new HashSet<>(secondResultSet);
boolean validateResultSizeOnly = state.getOptions().validateResultSizeOnly();
if (!validateResultSizeOnly && !firstHashSet.equals(secondHashSet)) {
Set<String> firstResultSetMisses = new HashSet<>(firstHashSet);
firstResultSetMisses.removeAll(secondHashSet);
Set<String> secondResultSetMisses = new HashSet<>(secondHashSet);
secondResultSetMisses.removeAll(firstHashSet);
String queryFormatString = "-- Query: \"%s\"; It misses: \"%s\"";
String firstQueryString = String.format(queryFormatString, originalQueryString, firstResultSetMisses);
String secondQueryString = String.format(queryFormatString, String.join(";", combinedString),
secondResultSetMisses);
// update the SELECT queries to be logged at the bottom of the error log file
state.getState().getLocalState()
.log(String.format("%s" + System.lineSeparator() + "%s", firstQueryString, secondQueryString));
String assertionMessage = String.format("The content of the result sets mismatch!" + System.lineSeparator()
+ "First query : \"%s\"" + System.lineSeparator() + "Second query: \"%s\"", originalQueryString,
secondQueryString);
throw new AssertionError(assertionMessage);
}
}
public static void assumeResultSetsAreEqual(List<String> resultSet, List<String> secondResultSet,
String originalQueryString, List<String> combinedString, SQLGlobalState<?, ?> state,
UnaryOperator<String> canonicalizationRule) {
// Overloaded version of assumeResultSetsAreEqual that takes a canonicalization function which is applied to
// both result sets before their comparison.
List<String> canonicalizedResultSet = resultSet.stream().map(canonicalizationRule).collect(Collectors.toList());
List<String> canonicalizedSecondResultSet = secondResultSet.stream().map(canonicalizationRule)
.collect(Collectors.toList());
assumeResultSetsAreEqual(canonicalizedResultSet, canonicalizedSecondResultSet, originalQueryString,
combinedString, state);
}
public static List<String> getCombinedResultSet(String firstQueryString, String secondQueryString,
String thirdQueryString, List<String> combinedString, boolean asUnion, SQLGlobalState<?, ?> state,
ExpectedErrors errors) throws SQLException {
List<String> secondResultSet;
if (asUnion) {
String unionString = firstQueryString + " UNION ALL " + secondQueryString + " UNION ALL "
+ thirdQueryString;
combinedString.add(unionString);
secondResultSet = getResultSetFirstColumnAsString(unionString, errors, state);
} else {
secondResultSet = new ArrayList<>();
secondResultSet.addAll(getResultSetFirstColumnAsString(firstQueryString, errors, state));
secondResultSet.addAll(getResultSetFirstColumnAsString(secondQueryString, errors, state));
secondResultSet.addAll(getResultSetFirstColumnAsString(thirdQueryString, errors, state));
combinedString.add(firstQueryString);
combinedString.add(secondQueryString);
combinedString.add(thirdQueryString);
}
return secondResultSet;
}
public static List<String> getCombinedResultSetNoDuplicates(String firstQueryString, String secondQueryString,
String thirdQueryString, List<String> combinedString, boolean asUnion, SQLGlobalState<?, ?> state,
ExpectedErrors errors) throws SQLException {
String unionString;
if (asUnion) {
unionString = firstQueryString + " UNION " + secondQueryString + " UNION " + thirdQueryString;
} else {
unionString = "SELECT DISTINCT * FROM (" + firstQueryString + " UNION ALL " + secondQueryString
+ " UNION ALL " + thirdQueryString + ")";
}
List<String> secondResultSet;
combinedString.add(unionString);
secondResultSet = getResultSetFirstColumnAsString(unionString, errors, state);
return secondResultSet;
}
public static String canonicalizeResultValue(String value) {
if (value == null) {
return value;
}
switch (value) {
case "-0.0":
return "0.0";
case "-0":
return "0";
default:
}
return value;
}
}