-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPostgresAggregate.java
More file actions
59 lines (46 loc) · 2.28 KB
/
PostgresAggregate.java
File metadata and controls
59 lines (46 loc) · 2.28 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
package sqlancer.postgres.ast;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import sqlancer.Randomly;
import sqlancer.common.ast.FunctionNode;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
import sqlancer.postgres.ast.PostgresAggregate.PostgresAggregateFunction;
/**
* @see <a href="https://www.sqlite.org/lang_aggfunc.html">Built-in Aggregate Functions</a>
*/
public class PostgresAggregate extends FunctionNode<PostgresAggregateFunction, PostgresExpression>
implements PostgresExpression {
public enum PostgresAggregateFunction {
AVG(PostgresDataType.INT, PostgresDataType.FLOAT, PostgresDataType.REAL, PostgresDataType.DECIMAL),
BIT_AND(PostgresDataType.INT), BIT_OR(PostgresDataType.INT), BOOL_AND(PostgresDataType.BOOLEAN),
BOOL_OR(PostgresDataType.BOOLEAN), COUNT(PostgresDataType.INT), EVERY(PostgresDataType.BOOLEAN), MAX, MIN,
// STRING_AGG
SUM(PostgresDataType.INT, PostgresDataType.FLOAT, PostgresDataType.REAL, PostgresDataType.DECIMAL);
private PostgresDataType[] supportedReturnTypes;
PostgresAggregateFunction(PostgresDataType... supportedReturnTypes) {
this.supportedReturnTypes = supportedReturnTypes.clone();
}
public List<PostgresDataType> getTypes(PostgresDataType returnType) {
return Arrays.asList(returnType);
}
public boolean supportsReturnType(PostgresDataType returnType) {
return Arrays.asList(supportedReturnTypes).stream().anyMatch(t -> t == returnType)
|| supportedReturnTypes.length == 0;
}
public static List<PostgresAggregateFunction> getAggregates(PostgresDataType type) {
return Arrays.asList(values()).stream().filter(p -> p.supportsReturnType(type))
.collect(Collectors.toList());
}
public PostgresDataType getRandomReturnType() {
if (supportedReturnTypes.length == 0) {
return Randomly.fromOptions(PostgresDataType.getRandomType());
} else {
return Randomly.fromOptions(supportedReturnTypes);
}
}
}
public PostgresAggregate(List<PostgresExpression> args, PostgresAggregateFunction func) {
super(func, args);
}
}