-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathSQLite3NoRECOracle.java
More file actions
158 lines (145 loc) · 6.53 KB
/
SQLite3NoRECOracle.java
File metadata and controls
158 lines (145 loc) · 6.53 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
package sqlancer.sqlite3.oracle;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.common.oracle.NoRECBase;
import sqlancer.common.oracle.TestOracle;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.common.query.SQLancerResultSet;
import sqlancer.sqlite3.SQLite3Errors;
import sqlancer.sqlite3.SQLite3Provider.SQLite3GlobalState;
import sqlancer.sqlite3.SQLite3Visitor;
import sqlancer.sqlite3.ast.SQLite3Aggregate;
import sqlancer.sqlite3.ast.SQLite3Expression;
import sqlancer.sqlite3.ast.SQLite3Expression.Join;
import sqlancer.sqlite3.ast.SQLite3Expression.SQLite3ColumnName;
import sqlancer.sqlite3.ast.SQLite3Expression.SQLite3PostfixText;
import sqlancer.sqlite3.ast.SQLite3Expression.SQLite3PostfixUnaryOperation;
import sqlancer.sqlite3.ast.SQLite3Expression.SQLite3PostfixUnaryOperation.PostfixUnaryOperator;
import sqlancer.sqlite3.ast.SQLite3Select;
import sqlancer.sqlite3.gen.SQLite3Common;
import sqlancer.sqlite3.gen.SQLite3ExpressionGenerator;
import sqlancer.sqlite3.schema.SQLite3Schema;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Table;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Tables;
public class SQLite3NoRECOracle extends NoRECBase<SQLite3GlobalState> implements TestOracle {
private static final int NO_VALID_RESULT = -1;
private final SQLite3Schema s;
private SQLite3ExpressionGenerator gen;
public SQLite3NoRECOracle(SQLite3GlobalState globalState) {
super(globalState);
this.s = globalState.getSchema();
SQLite3Errors.addExpectedExpressionErrors(errors);
SQLite3Errors.addMatchQueryErrors(errors);
SQLite3Errors.addQueryErrors(errors);
errors.add("misuse of aggregate");
errors.add("misuse of window function");
errors.add("second argument to nth_value must be a positive integer");
errors.add("no such table");
errors.add("no query solution");
errors.add("unable to use function MATCH in the requested context");
}
@Override
public void check() throws SQLException {
SQLite3Tables randomTables = s.getRandomTableNonEmptyTables();
List<SQLite3Column> columns = randomTables.getColumns();
gen = new SQLite3ExpressionGenerator(state).setColumns(columns);
SQLite3Expression randomWhereCondition = gen.generateExpression();
List<SQLite3Table> tables = randomTables.getTables();
List<Join> joinStatements = gen.getRandomJoinClauses(tables);
List<SQLite3Expression> tableRefs = SQLite3Common.getTableRefs(tables, s);
SQLite3Select select = new SQLite3Select();
select.setFromTables(tableRefs);
select.setJoinClauses(joinStatements);
int optimizedCount = getOptimizedQuery(select, randomWhereCondition);
int unoptimizedCount = getUnoptimizedQuery(select, randomWhereCondition);
if (optimizedCount == NO_VALID_RESULT || unoptimizedCount == NO_VALID_RESULT) {
throw new IgnoreMeException();
}
if (optimizedCount != unoptimizedCount) {
state.getState().getLocalState().log(optimizedQueryString + ";\n" + unoptimizedQueryString + ";");
throw new AssertionError(optimizedCount + " " + unoptimizedCount);
}
}
private int getUnoptimizedQuery(SQLite3Select select, SQLite3Expression randomWhereCondition) throws SQLException {
SQLite3PostfixUnaryOperation isTrue = new SQLite3PostfixUnaryOperation(PostfixUnaryOperator.IS_TRUE,
randomWhereCondition);
SQLite3PostfixText asText = new SQLite3PostfixText(isTrue, " as count", null);
select.setFetchColumns(Arrays.asList(asText));
select.setWhereClause(null);
unoptimizedQueryString = "SELECT SUM(count) FROM (" + SQLite3Visitor.asString(select) + ")";
if (options.logEachSelect()) {
logger.writeCurrent(unoptimizedQueryString);
}
SQLQueryAdapter q = new SQLQueryAdapter(unoptimizedQueryString, errors);
return extractCounts(q);
}
private int getOptimizedQuery(SQLite3Select select, SQLite3Expression randomWhereCondition) throws SQLException {
boolean useAggregate = Randomly.getBoolean();
if (Randomly.getBoolean()) {
select.setOrderByExpressions(gen.generateOrderBys());
}
if (useAggregate) {
select.setFetchColumns(Arrays.asList(new SQLite3Aggregate(Collections.emptyList(),
SQLite3Aggregate.SQLite3AggregateFunction.COUNT_ALL)));
} else {
SQLite3ColumnName aggr = new SQLite3ColumnName(SQLite3Column.createDummy("*"), null);
select.setFetchColumns(Arrays.asList(aggr));
}
select.setWhereClause(randomWhereCondition);
optimizedQueryString = SQLite3Visitor.asString(select);
if (options.logEachSelect()) {
logger.writeCurrent(optimizedQueryString);
}
SQLQueryAdapter q = new SQLQueryAdapter(optimizedQueryString, errors);
return useAggregate ? extractCounts(q) : countRows(q);
}
private int countRows(SQLQueryAdapter q) {
int count = 0;
try (SQLancerResultSet rs = q.executeAndGet(state)) {
if (rs == null) {
return NO_VALID_RESULT;
} else {
try {
while (rs.next()) {
count++;
}
} catch (SQLException e) {
count = NO_VALID_RESULT;
}
}
} catch (Exception e) {
if (e instanceof IgnoreMeException) {
throw (IgnoreMeException) e;
}
throw new AssertionError(unoptimizedQueryString, e);
}
return count;
}
private int extractCounts(SQLQueryAdapter q) {
int count = 0;
try (SQLancerResultSet rs = q.executeAndGet(state)) {
if (rs == null) {
return NO_VALID_RESULT;
} else {
try {
while (rs.next()) {
count += rs.getInt(1);
}
} catch (SQLException e) {
count = NO_VALID_RESULT;
}
}
} catch (Exception e) {
if (e instanceof IgnoreMeException) {
throw (IgnoreMeException) e;
}
throw new AssertionError(unoptimizedQueryString, e);
}
return count;
}
}