提交 85b4aa10 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Reorganize WindowFunction.getOrderedResultLoop()

上级 8145005a
......@@ -183,62 +183,58 @@ public class WindowFunction extends DataAnalysisOperation {
protected void getOrderedResultLoop(Session session, HashMap<Integer, Value> result, ArrayList<Value[]> ordered,
int rowIdColumn) {
switch (type) {
case ROW_NUMBER:
for (int i = 0, size = ordered.size(); i < size;) {
result.put(ordered.get(i)[rowIdColumn].getInt(), ValueInt.get(++i));
}
break;
case RANK:
case DENSE_RANK:
case PERCENT_RANK:
getRank(result, ordered, rowIdColumn);
break;
case CUME_DIST:
getCumeDist(session, result, ordered, rowIdColumn);
return;
break;
case NTILE:
getNtile(session, result, ordered, rowIdColumn);
return;
break;
case LEAD:
case LAG:
getLeadLag(session, result, ordered, rowIdColumn);
return;
break;
case FIRST_VALUE:
case LAST_VALUE:
case NTH_VALUE:
getNth(session, result, ordered, rowIdColumn);
return;
break;
default:
throw DbException.throwInternalError("type=" + type);
}
}
private void getRank(HashMap<Integer, Value> result, ArrayList<Value[]> ordered, int rowIdColumn) {
int size = ordered.size();
int number = 0;
for (int i = 0; i < size; i++) {
Value[] row = ordered.get(i);
int rowId = row[rowIdColumn].getInt();
Value v;
switch (type) {
case ROW_NUMBER:
v = ValueInt.get(i + 1);
break;
case RANK:
case DENSE_RANK:
case PERCENT_RANK: {
if (i == 0) {
number = 1;
} else {
if (getOverOrderBySort().compare(ordered.get(i - 1), row) != 0) {
switch (type) {
case RANK:
case PERCENT_RANK:
number = i + 1;
break;
default: // DENSE_RANK
number++;
}
}
}
if (type == WindowFunctionType.PERCENT_RANK) {
int nm = number - 1;
v = nm == 0 ? ValueDouble.ZERO : ValueDouble.get((double) nm / (size - 1));
if (i == 0) {
number = 1;
} else if (getOverOrderBySort().compare(ordered.get(i - 1), row) != 0) {
if (type == WindowFunctionType.DENSE_RANK) {
number++;
} else {
v = ValueInt.get(number);
number = i + 1;
}
break;
}
default:
throw DbException.throwInternalError("type=" + type);
Value v;
if (type == WindowFunctionType.PERCENT_RANK) {
int nm = number - 1;
v = nm == 0 ? ValueDouble.ZERO : ValueDouble.get((double) nm / (size - 1));
} else {
v = ValueInt.get(number);
}
result.put(rowId, v);
result.put(row[rowIdColumn].getInt(), v);
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论