forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLite3Aggregate.java
More file actions
132 lines (114 loc) · 3.92 KB
/
SQLite3Aggregate.java
File metadata and controls
132 lines (114 loc) · 3.92 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
package sqlancer.sqlite3.ast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.sqlite3.SQLite3Provider;
import sqlancer.sqlite3.schema.SQLite3DataType;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column.SQLite3CollateSequence;
/**
* @see <a href="https://www.sqlite.org/lang_aggfunc.html">Built-in Aggregate Functions</a>
*/
public class SQLite3Aggregate extends SQLite3Expression {
private final SQLite3AggregateFunction func;
private final List<SQLite3Expression> expr;
public enum SQLite3AggregateFunction {
AVG() {
@Override
public SQLite3Constant apply(SQLite3Constant exprVal) {
return SQLite3Cast.castToReal(exprVal);
}
},
COUNT() {
@Override
public SQLite3Constant apply(SQLite3Constant exprVal) {
int count;
if (exprVal.isNull()) {
count = 0;
} else {
count = 1;
}
return SQLite3Constant.createIntConstant(count);
}
},
COUNT_ALL() {
@Override
public SQLite3Constant apply(SQLite3Constant exprVal) {
return SQLite3Constant.createIntConstant(1);
}
},
GROUP_CONCAT() {
@Override
public SQLite3Constant apply(SQLite3Constant exprVal) {
SQLite3Constant castToText = SQLite3Cast.castToText(exprVal);
if (castToText == null && SQLite3Provider.mustKnowResult) {
throw new IgnoreMeException();
}
return castToText;
}
},
MAX {
@Override
public SQLite3Constant apply(SQLite3Constant exprVal) {
return exprVal;
}
},
MIN {
@Override
public SQLite3Constant apply(SQLite3Constant exprVal) {
return exprVal;
}
},
SUM() {
@Override
public SQLite3Constant apply(SQLite3Constant exprVal) {
return SQLite3Cast.castToReal(exprVal);
}
},
TOTAL() {
@Override
public SQLite3Constant apply(SQLite3Constant exprVal) {
if (exprVal.isNull()) {
return SQLite3Constant.createRealConstant(0);
} else {
return SQLite3Cast.castToReal(exprVal);
}
}
};
public abstract SQLite3Constant apply(SQLite3Constant exprVal);
public static SQLite3AggregateFunction getRandom() {
List<SQLite3AggregateFunction> functions = new ArrayList<>(Arrays.asList(values()));
if (SQLite3Provider.mustKnowResult) {
functions.remove(SQLite3AggregateFunction.SUM);
functions.remove(SQLite3AggregateFunction.TOTAL);
functions.remove(SQLite3AggregateFunction.GROUP_CONCAT);
}
return Randomly.fromOptions(values());
}
public static SQLite3AggregateFunction getRandom(SQLite3DataType type) {
return Randomly.fromOptions(values());
}
}
public SQLite3Aggregate(List<SQLite3Expression> expr, SQLite3AggregateFunction func) {
this.expr = expr;
this.func = func;
}
public SQLite3AggregateFunction getFunc() {
return func;
}
public List<SQLite3Expression> getExpr() {
return expr;
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return null;
// return expr.getExplicitCollateSequence();
}
@Override
public SQLite3Constant getExpectedValue() {
assert !SQLite3Provider.mustKnowResult;
return null;
// return func.apply(expr.getExpectedValue());
}
}