forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFelderaSchema.java
More file actions
207 lines (170 loc) · 6.46 KB
/
FelderaSchema.java
File metadata and controls
207 lines (170 loc) · 6.46 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
197
198
199
200
201
202
203
204
205
206
207
package sqlancer.feldera;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import sqlancer.Randomly;
import sqlancer.common.schema.*;
import sqlancer.feldera.ast.FelderaColumnReference;
import sqlancer.feldera.ast.FelderaConstant;
import sqlancer.feldera.ast.FelderaExpression;
public class FelderaSchema extends AbstractSchema<FelderaGlobalState, FelderaSchema.FelderaTable> {
private final String pipelineName;
public FelderaSchema(List<FelderaTable> databaseTables, String pipelineName) {
super(databaseTables);
this.pipelineName = pipelineName;
}
public FelderaSchema(String pipelineName) {
super(new ArrayList<>());
this.pipelineName = pipelineName;
}
public FelderaSchema addTable(FelderaTable table) {
List<FelderaTable> tables = new ArrayList<>(this.getDatabaseTables());
tables.add(table);
return new FelderaSchema(tables, this.pipelineName);
}
public static FelderaDataType getColumnType(String typeString) {
switch (typeString.toUpperCase()) {
case "BOOLEAN":
return FelderaDataType.BOOLEAN;
case "TINYINT":
return FelderaDataType.TINYINT;
case "SMALLINT":
return FelderaDataType.SMALLINT;
case "INT":
return FelderaDataType.INT;
case "BIGINT":
return FelderaDataType.BIGINT;
case "VARCHAR":
return FelderaDataType.VARCHAR;
case "CHAR":
return FelderaDataType.CHAR;
case "NULL":
return FelderaDataType.NULL;
case "TIME":
return FelderaDataType.TIME;
case "DATE":
return FelderaDataType.DATE;
case "TIMESTAMP":
return FelderaDataType.TIMESTAMP;
case "REAL":
return FelderaDataType.REAL;
case "DOUBLE":
return FelderaDataType.DOUBLE;
default:
throw new AssertionError(typeString);
}
}
public static FelderaSchema fromConnection(FelderaConnection con) throws Exception {
return new FelderaSchema(new ArrayList<>(), con.getPipelineName());
}
protected List<FelderaColumn> getTableColumns(String tableName) throws Exception {
return this.getDatabaseTable(tableName).getColumns();
}
public FelderaTables getRandomTableNonEmptyTables() {
return new FelderaTables(Randomly.nonEmptySubset(getDatabaseTables()));
}
public String getPipelineName() {
return pipelineName;
}
public enum FelderaDataType {
BOOLEAN, TINYINT, SMALLINT, INT, BIGINT, VARCHAR, CHAR, NULL, TIME, DATE, TIMESTAMP,
// DECIMAL,
// VARBINARY,
// INTERVAL,
// GEOMETRY,
// ROW,
// ARRAY,
// MAP,
// VARIANT,
REAL, DOUBLE;
public static FelderaDataType getRandomNumericType() {
return Randomly
.fromList(Arrays.stream(values()).filter(FelderaDataType::isNumeric).collect(Collectors.toList()));
}
public boolean isNumeric() {
switch (this) {
case REAL:
case DOUBLE:
case TINYINT:
case SMALLINT:
case INT:
case BIGINT:
return true;
default:
return false;
}
}
public static FelderaDataType getRandomNonNullType() {
return Randomly.fromList(
Arrays.stream(values()).filter(t -> t != FelderaDataType.NULL).collect(Collectors.toList()));
}
public static FelderaDataType getRandomType() {
return Randomly.fromOptions(values());
}
public FelderaExpression getRandomConstant(FelderaGlobalState globalState) {
if (Randomly.getBooleanWithSmallProbability()) {
return FelderaConstant.createNullConstant();
}
return FelderaConstant.getRandomConstant(globalState, this);
}
}
public static class FelderaFieldColumn extends FelderaColumn {
public FelderaFieldColumn(String name, FelderaDataType columnType) {
super(name, columnType);
}
public FelderaFieldColumn(String name, FelderaDataType columnType, boolean isNullable) {
super(name, columnType, isNullable);
// Note to self: later, assert that the Field column isn't something like INTERVAL
}
}
public static class FelderaColumn extends AbstractTableColumn<FelderaTable, FelderaDataType> {
private final boolean isNullable;
public FelderaColumn(String name, FelderaDataType columnType) {
super(name, null, columnType);
this.isNullable = false;
}
public FelderaColumn(String name, FelderaDataType columnType, boolean isNullable) {
super(name, null, columnType);
this.isNullable = isNullable;
}
public FelderaColumnReference asColumnReference() {
return new FelderaColumnReference(this);
}
public static FelderaColumn createDummy(String name) {
return new FelderaColumn(name, FelderaDataType.getRandomType());
}
public boolean isNullable() {
return isNullable;
}
}
public static class FelderaTables extends AbstractTables<FelderaTable, FelderaColumn> {
public FelderaTables(List<FelderaTable> tables) {
super(tables);
}
}
public static class FelderaTable extends AbstractTable<FelderaColumn, TableIndex, FelderaGlobalState> {
public FelderaTable(String tableName, List<FelderaColumn> columns) {
super(tableName, columns, null, false);
}
// SELECT COUNT(*) FROM table;
@Override
public long getNrRows(FelderaGlobalState globalState) {
// TODO
return 0;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FelderaTable) {
FelderaTable other = (FelderaTable) obj;
return Objects.equals(this.name, other.name) && this.getColumns() == other.getColumns();
} else {
return false;
}
}
public static List<FelderaColumn> getAllColumns(List<FelderaTable> tables) {
return tables.stream().map(AbstractTable::getColumns).flatMap(List::stream).collect(Collectors.toList());
}
}
}