forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresTLPBase.java
More file actions
140 lines (128 loc) · 6.32 KB
/
PostgresTLPBase.java
File metadata and controls
140 lines (128 loc) · 6.32 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
package sqlancer.postgres.oracle.tlp;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import sqlancer.Randomly;
import sqlancer.common.gen.ExpressionGenerator;
import sqlancer.common.oracle.TernaryLogicPartitioningOracleBase;
import sqlancer.common.oracle.TestOracle;
import sqlancer.postgres.PostgresGlobalState;
import sqlancer.postgres.PostgresSchema;
import sqlancer.postgres.PostgresSchema.PostgresColumn;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
import sqlancer.postgres.PostgresSchema.PostgresTable;
import sqlancer.postgres.PostgresSchema.PostgresTables;
import sqlancer.postgres.ast.PostgresColumnValue;
import sqlancer.postgres.ast.PostgresConstant;
import sqlancer.postgres.ast.PostgresExpression;
import sqlancer.postgres.ast.PostgresJoin;
import sqlancer.postgres.ast.PostgresJoin.PostgresJoinType;
import sqlancer.postgres.ast.PostgresSelect;
import sqlancer.postgres.ast.PostgresSelect.ForClause;
import sqlancer.postgres.ast.PostgresSelect.PostgresFromTable;
import sqlancer.postgres.ast.PostgresSelect.PostgresSubquery;
import sqlancer.postgres.gen.PostgresCommon;
import sqlancer.postgres.gen.PostgresExpressionGenerator;
public class PostgresTLPBase extends TernaryLogicPartitioningOracleBase<PostgresExpression, PostgresGlobalState>
implements TestOracle<PostgresGlobalState> {
protected PostgresSchema s;
protected PostgresTables targetTables;
protected PostgresExpressionGenerator gen;
protected PostgresSelect select;
public PostgresTLPBase(PostgresGlobalState state) {
super(state);
PostgresCommon.addCommonExpressionErrors(errors);
PostgresCommon.addCommonFetchErrors(errors);
}
@Override
public void check() throws SQLException {
s = state.getSchema();
targetTables = s.getRandomTableNonEmptyTables();
List<PostgresTable> tables = targetTables.getTables();
List<PostgresJoin> joins = getJoinStatements(state, targetTables.getColumns(), tables);
generateSelectBase(tables, joins);
}
protected List<PostgresJoin> getJoinStatements(PostgresGlobalState globalState, List<PostgresColumn> columns,
List<PostgresTable> tables) {
List<PostgresJoin> joinStatements = new ArrayList<>();
PostgresExpressionGenerator gen = new PostgresExpressionGenerator(globalState).setColumns(columns);
for (int i = 1; i < tables.size(); i++) {
PostgresExpression joinClause = gen.generateExpression(PostgresDataType.BOOLEAN);
PostgresTable table = Randomly.fromList(tables);
tables.remove(table);
PostgresJoinType options = PostgresJoinType.getRandom();
PostgresJoin j = new PostgresJoin(new PostgresFromTable(table, Randomly.getBoolean()), joinClause, options);
joinStatements.add(j);
}
// JOIN subqueries
for (int i = 0; i < Randomly.smallNumber(); i++) {
PostgresTables subqueryTables = globalState.getSchema().getRandomTableNonEmptyTables();
PostgresSubquery subquery = PostgresTLPBase.createSubquery(globalState, String.format("sub%d", i),
subqueryTables);
PostgresExpression joinClause = gen.generateExpression(PostgresDataType.BOOLEAN);
PostgresJoinType options = PostgresJoinType.getRandom();
PostgresJoin j = new PostgresJoin(subquery, joinClause, options);
joinStatements.add(j);
}
return joinStatements;
}
protected void generateSelectBase(List<PostgresTable> tables, List<PostgresJoin> joins) {
List<PostgresExpression> tableList = tables.stream().map(t -> new PostgresFromTable(t, Randomly.getBoolean()))
.collect(Collectors.toList());
gen = new PostgresExpressionGenerator(state).setColumns(targetTables.getColumns());
initializeTernaryPredicateVariants();
select = new PostgresSelect();
select.setFetchColumns(generateFetchColumns());
select.setFromList(tableList);
select.setWhereClause(null);
select.setJoinClauses(joins);
if (Randomly.getBoolean()) {
select.setForClause(ForClause.getRandom());
}
}
List<PostgresExpression> generateFetchColumns() {
if (Randomly.getBooleanWithRatherLowProbability()) {
return Arrays.asList(new PostgresColumnValue(PostgresColumn.createDummy("*"), null));
}
List<PostgresExpression> fetchColumns = new ArrayList<>();
List<PostgresColumn> targetColumns = Randomly.nonEmptySubset(targetTables.getColumns());
for (PostgresColumn c : targetColumns) {
fetchColumns.add(new PostgresColumnValue(c, null));
}
return fetchColumns;
}
@Override
protected ExpressionGenerator<PostgresExpression> getGen() {
return gen;
}
public static PostgresSubquery createSubquery(PostgresGlobalState globalState, String name, PostgresTables tables) {
List<PostgresExpression> columns = new ArrayList<>();
PostgresExpressionGenerator gen = new PostgresExpressionGenerator(globalState).setColumns(tables.getColumns());
for (int i = 0; i < Randomly.smallNumber() + 1; i++) {
columns.add(gen.generateExpression(0));
}
PostgresSelect select = new PostgresSelect();
select.setFromList(tables.getTables().stream().map(t -> new PostgresFromTable(t, Randomly.getBoolean()))
.collect(Collectors.toList()));
select.setFetchColumns(columns);
if (Randomly.getBoolean()) {
select.setWhereClause(gen.generateExpression(0, PostgresDataType.BOOLEAN));
}
if (Randomly.getBooleanWithRatherLowProbability()) {
select.setOrderByClauses(gen.generateOrderBys());
}
if (Randomly.getBoolean()) {
select.setLimitClause(PostgresConstant.createIntConstant(Randomly.getPositiveOrZeroNonCachedInteger()));
if (Randomly.getBoolean()) {
select.setOffsetClause(
PostgresConstant.createIntConstant(Randomly.getPositiveOrZeroNonCachedInteger()));
}
}
if (Randomly.getBooleanWithRatherLowProbability()) {
select.setForClause(ForClause.getRandom());
}
return new PostgresSubquery(select, name);
}
}