forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBMSCommon.java
More file actions
69 lines (56 loc) · 2.06 KB
/
DBMSCommon.java
File metadata and controls
69 lines (56 loc) · 2.06 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
package sqlancer.common;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class DBMSCommon {
private static final Pattern SQLANCER_INDEX_PATTERN = Pattern.compile("^i\\d+");
private DBMSCommon() {
}
public static String createTableName(int nr) {
return String.format("t%d", nr);
}
public static String createColumnName(int nr) {
return String.format("c%d", nr);
}
public static String createIndexName(int nr) {
return String.format("i%d", nr);
}
public static boolean matchesIndexName(String indexName) {
Matcher matcher = SQLANCER_INDEX_PATTERN.matcher(indexName);
return matcher.matches();
}
public static int getMaxIndexInDoubleArray(double... doubleArray) {
int maxIndex = 0;
double maxValue = 0.0;
for (int j = 0; j < doubleArray.length; j++) {
double curReward = doubleArray[j];
if (curReward > maxValue) {
maxIndex = j;
maxValue = curReward;
}
}
return maxIndex;
}
public static boolean areQueryPlanSequencesSimilar(List<String> list1, List<String> list2) {
return editDistance(list1, list2) <= 1;
}
public static int editDistance(List<String> list1, List<String> list2) {
int[][] dp = new int[list1.size() + 1][list2.size() + 1];
for (int i = 0; i <= list1.size(); i++) {
for (int j = 0; j <= list2.size(); j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else {
dp[i][j] = Math.min(dp[i - 1][j - 1] + costOfSubstitution(list1.get(i - 1), list2.get(j - 1)),
Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1));
}
}
}
return dp[list1.size()][list2.size()];
}
private static int costOfSubstitution(String string, String string2) {
return string.equals(string2) ? 0 : 1;
}
}