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

Reorganize WindowFunction.getOrderedResultLoop()

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