-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathSQLite3WindowFunction.java
More file actions
143 lines (123 loc) · 4.13 KB
/
SQLite3WindowFunction.java
File metadata and controls
143 lines (123 loc) · 4.13 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
package sqlancer.sqlite3.ast;
import java.util.List;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.sqlite3.SQLite3Provider;
import sqlancer.sqlite3.SQLite3Provider.SQLite3GlobalState;
import sqlancer.sqlite3.gen.SQLite3ExpressionGenerator;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column.SQLite3CollateSequence;
public class SQLite3WindowFunction extends SQLite3Expression {
private WindowFunction func;
private SQLite3Expression[] args;
public static SQLite3WindowFunction getRandom(List<SQLite3Column> columns, SQLite3GlobalState globalState) {
WindowFunction func = Randomly.fromOptions(WindowFunction.values());
SQLite3Expression[] args = new SQLite3Expression[func.nrArgs];
for (int i = 0; i < args.length; i++) {
args[i] = new SQLite3ExpressionGenerator(globalState).setColumns(columns).generateExpression();
}
return new SQLite3WindowFunction(func, args);
}
public enum WindowFunction {
ROW_NUMBER {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return SQLite3Constant.createIntConstant(1);
}
},
RANK {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return SQLite3Constant.createIntConstant(1);
}
},
DENSE_RANK {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return SQLite3Constant.createIntConstant(1);
}
},
PERCENT_RANK {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return SQLite3Constant.createRealConstant(0.0);
}
},
CUME_DIST {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return SQLite3Constant.createRealConstant(1.0);
}
},
NTILE(1), //
LAG(3), //
LEAD(3), //
FIRST_VALUE(1) {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return args[0];
}
},
LAST_VALUE(1) {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return args[0];
}
},
NTH_VALUE(2) {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
SQLite3Constant n = SQLite3Cast.castToInt(args[1]);
if (!n.isNull() && n.asInt() == 1) {
return args[0];
} else {
return SQLite3Constant.createNullConstant();
}
}
};
int nrArgs;
WindowFunction(int nrArgs) {
this.nrArgs = nrArgs;
};
WindowFunction() {
this(0);
}
public SQLite3Constant apply(SQLite3Constant... args) {
if (SQLite3Provider.mustKnowResult) {
throw new AssertionError();
}
return null;
}
public int getNrArgs() {
return nrArgs;
}
}
public SQLite3WindowFunction(WindowFunction func, SQLite3Expression... args) {
this.func = func;
this.args = args.clone();
}
public WindowFunction getFunc() {
return func;
}
public SQLite3Expression[] getArgs() {
return args.clone();
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
return null;
}
@Override
public SQLite3Constant getExpectedValue() {
if (!SQLite3Provider.mustKnowResult) {
return null;
}
SQLite3Constant[] evaluatedConst = new SQLite3Constant[args.length];
for (int i = 0; i < evaluatedConst.length; i++) {
evaluatedConst[i] = args[i].getExpectedValue();
if (evaluatedConst[i] == null) {
throw new IgnoreMeException();
}
}
return func.apply(evaluatedConst);
}
}