Skip to content

Commit 8487db4

Browse files
committed
[Postgres] Reuse the abstract table and column classes
1 parent ac452f2 commit 8487db4

6 files changed

Lines changed: 20 additions & 144 deletions

src/sqlancer/postgres/PostgresSchema.java

Lines changed: 11 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
import sqlancer.StateToReproduce.PostgresStateToReproduce;
2020
import sqlancer.postgres.PostgresSchema.PostgresTable.TableType;
2121
import sqlancer.postgres.ast.PostgresConstant;
22+
import sqlancer.schema.AbstractTable;
23+
import sqlancer.schema.AbstractTableColumn;
2224
import sqlancer.schema.TableIndex;
23-
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column;
2425

2526
public class PostgresSchema {
2627

@@ -38,75 +39,12 @@ public static PostgresDataType getRandomType() {
3839
return Randomly.fromList(dataTypes);
3940
}
4041
}
41-
42-
public static class PostgresColumn implements Comparable<PostgresColumn> {
43-
44-
private final String name;
45-
private final PostgresDataType columnType;
46-
private PostgresTable table;
47-
private int precision;
48-
42+
43+
44+
public static class PostgresColumn extends AbstractTableColumn<PostgresTable, PostgresDataType> {
4945

5046
public PostgresColumn(String name, PostgresDataType columnType) {
51-
this.name = name;
52-
this.columnType = columnType;
53-
}
54-
55-
@Override
56-
public String toString() {
57-
return String.format("%s.%s: %s", table.getName(), name, columnType);
58-
}
59-
60-
@Override
61-
public int hashCode() {
62-
return name.hashCode() + 11 * columnType.hashCode();
63-
}
64-
65-
@Override
66-
public boolean equals(Object obj) {
67-
if (!(obj instanceof SQLite3Column)) {
68-
return false;
69-
} else {
70-
PostgresColumn c = (PostgresColumn) obj;
71-
return table.getName().contentEquals(getName()) && name.equals(c.name);
72-
}
73-
}
74-
75-
public String getName() {
76-
return name;
77-
}
78-
79-
public String getFullQualifiedName() {
80-
if (table == null) {
81-
return getName();
82-
} else {
83-
return table.getName() + "." + getName();
84-
}
85-
}
86-
87-
public PostgresDataType getColumnType() {
88-
return columnType;
89-
}
90-
91-
public int getPrecision() {
92-
return precision;
93-
}
94-
95-
public void setTable(PostgresTable t) {
96-
this.table = t;
97-
}
98-
99-
public PostgresTable getTable() {
100-
return table;
101-
}
102-
103-
@Override
104-
public int compareTo(PostgresColumn o) {
105-
if (o.getTable().equals(this.getTable())) {
106-
return name.compareTo(o.getName());
107-
} else {
108-
return o.getTable().compareTo(table);
109-
}
47+
super(name, null, columnType);
11048
}
11149

11250
}
@@ -164,7 +102,7 @@ public PostgresRowValue getRandomRowValue(Connection con, PostgresStateToReprodu
164102
if (randomRowValues.getString(columnIndex) == null) {
165103
constant = PostgresConstant.createNullConstant();
166104
} else {
167-
switch (column.getColumnType()) {
105+
switch (column.getType()) {
168106
case INT:
169107
constant = PostgresConstant.createIntConstant(randomRowValues.getLong(columnIndex));
170108
break;
@@ -175,7 +113,7 @@ public PostgresRowValue getRandomRowValue(Connection con, PostgresStateToReprodu
175113
constant = PostgresConstant.createTextConstant(randomRowValues.getString(columnIndex));
176114
break;
177115
default:
178-
throw new AssertionError(column.getColumnType());
116+
throw new AssertionError(column.getType());
179117
}
180118
}
181119
values.put(column, constant);
@@ -274,83 +212,25 @@ public String getRowValuesAsString(List<PostgresColumn> columnsToCheck) {
274212
}
275213

276214
}
277-
278-
public static class PostgresTable implements Comparable<PostgresTable> {
215+
216+
public static class PostgresTable extends AbstractTable<PostgresColumn, PostgresIndex> {
279217

280218
public enum TableType {
281219
STANDARD, TEMPORARY
282220
}
283221

284-
private final String tableName;
285-
private final List<PostgresColumn> columns;
286-
private final List<PostgresIndex> indexes;
287222
private final TableType tableType;
288223
private List<PostgresStatisticsObject> statistics;
289-
private final boolean isView;
290224
private boolean isInsertable;
291225

292226
public PostgresTable(String tableName, List<PostgresColumn> columns, List<PostgresIndex> indexes,
293227
TableType tableType, List<PostgresStatisticsObject> statistics, boolean isView, boolean isInsertable) {
294-
this.tableName = tableName;
295-
this.indexes = indexes;
228+
super(tableName, columns, indexes, isView);
296229
this.statistics = statistics;
297-
this.isView = isView;
298230
this.isInsertable = isInsertable;
299-
this.columns = Collections.unmodifiableList(columns);
300231
this.tableType = tableType;
301232
}
302233

303-
@Override
304-
public String toString() {
305-
StringBuffer sb = new StringBuffer();
306-
sb.append(tableName + "\n");
307-
for (PostgresColumn c : columns) {
308-
sb.append("\t" + c + "\n");
309-
}
310-
return sb.toString();
311-
}
312-
313-
public List<PostgresIndex> getIndexes() {
314-
return indexes;
315-
}
316-
317-
public String getName() {
318-
return tableName;
319-
}
320-
321-
public List<PostgresColumn> getColumns() {
322-
return columns;
323-
}
324-
325-
public String getColumnsAsString() {
326-
return columns.stream().map(c -> c.getName()).collect(Collectors.joining(", "));
327-
}
328-
329-
public String getColumnsAsString(Function<PostgresColumn, String> function) {
330-
return columns.stream().map(function).collect(Collectors.joining(", "));
331-
}
332-
333-
public PostgresColumn getRandomColumn() {
334-
return Randomly.fromList(columns);
335-
}
336-
337-
public boolean hasIndexes() {
338-
return !indexes.isEmpty();
339-
}
340-
341-
public PostgresIndex getRandomIndex() {
342-
return Randomly.fromList(indexes);
343-
}
344-
345-
@Override
346-
public int compareTo(PostgresTable o) {
347-
return o.getName().compareTo(tableName);
348-
}
349-
350-
public List<PostgresColumn> getRandomNonEmptyColumnSubset() {
351-
return Randomly.nonEmptySubset(getColumns());
352-
}
353-
354234
public List<PostgresStatisticsObject> getStatistics() {
355235
return statistics;
356236
}
@@ -369,10 +249,6 @@ public TableType getTableType() {
369249
return tableType;
370250
}
371251

372-
public boolean isView() {
373-
return isView;
374-
}
375-
376252
public boolean isInsertable() {
377253
return isInsertable;
378254
}

src/sqlancer/postgres/ast/PostgresColumnValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public PostgresColumnValue(PostgresColumn c, PostgresConstant expectedValue) {
1616

1717
@Override
1818
public PostgresDataType getExpressionType() {
19-
return c.getColumnType();
19+
return c.getType();
2020
}
2121

2222
@Override

src/sqlancer/postgres/gen/PostgresAlterTableGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public Query generate() {
182182
} else {
183183
sb.append("SET DEFAULT ");
184184
sb.append(PostgresVisitor.asString(
185-
PostgresExpressionGenerator.generateExpression(globalState, randomColumn.getColumnType())));
185+
PostgresExpressionGenerator.generateExpression(globalState, randomColumn.getType())));
186186
errors.add("is out of range");
187187
errors.add("but default expression is of type");
188188
errors.add("cannot cast");

src/sqlancer/postgres/gen/PostgresExpressionGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private PostgresDataType getMeaningfulType() {
205205
if (Randomly.getBooleanWithSmallProbability() || columns == null || columns.isEmpty()) {
206206
return PostgresDataType.getRandomType();
207207
} else {
208-
return Randomly.fromList(columns).getColumnType();
208+
return Randomly.fromList(columns).getType();
209209
}
210210
}
211211

@@ -433,7 +433,7 @@ private final List<PostgresColumn> filterColumns(PostgresDataType type) {
433433
if (columns == null) {
434434
return Collections.emptyList();
435435
} else {
436-
return columns.stream().filter(c -> c.getColumnType() == type).collect(Collectors.toList());
436+
return columns.stream().filter(c -> c.getType() == type).collect(Collectors.toList());
437437
}
438438
}
439439

src/sqlancer/postgres/gen/PostgresInsertGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static Query insert(PostgresGlobalState globalState) {
5656
sbRowValue.append(", ");
5757
}
5858
sbRowValue.append(PostgresVisitor.asString(PostgresExpressionGenerator
59-
.generateConstant(globalState.getRandomly(), columns.get(i).getColumnType())));
59+
.generateConstant(globalState.getRandomly(), columns.get(i).getType())));
6060
}
6161
sbRowValue.append(")");
6262

@@ -109,10 +109,10 @@ private static void insertRow(PostgresGlobalState globalState, StringBuilder sb,
109109
PostgresExpression generateConstant;
110110
if (Randomly.getBoolean()) {
111111
generateConstant = PostgresExpressionGenerator.generateConstant(globalState.getRandomly(),
112-
columns.get(i).getColumnType());
112+
columns.get(i).getType());
113113
} else {
114114
generateConstant = new PostgresExpressionGenerator(globalState)
115-
.generateExpression(columns.get(i).getColumnType());
115+
.generateExpression(columns.get(i).getType());
116116
}
117117
sb.append(PostgresVisitor.asString(generateConstant));
118118
} else {

src/sqlancer/postgres/gen/PostgresUpdateGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public static Query create(PostgresGlobalState globalState) {
3737
sb.append(column.getName());
3838
sb.append(" = ");
3939
if (!Randomly.getBoolean()) {
40-
PostgresExpression constant = PostgresExpressionGenerator.generateConstant(globalState.getRandomly(), column.getColumnType());
40+
PostgresExpression constant = PostgresExpressionGenerator.generateConstant(globalState.getRandomly(), column.getType());
4141
sb.append(PostgresVisitor.asString(constant));
4242
} else if (Randomly.getBoolean()) {
4343
sb.append("DEFAULT");
4444
} else {
4545
sb.append("(");
4646
PostgresExpression expr = PostgresExpressionGenerator.generateExpression(globalState, randomTable.getColumns(),
47-
column.getColumnType());
47+
column.getType());
4848
// caused by casts
4949
sb.append(PostgresVisitor.asString(expr));
5050
sb.append(")");

0 commit comments

Comments
 (0)