提交 0ef8cba8 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not sort window data if ORDER BY is not specified

上级 a6c409c9
...@@ -3049,40 +3049,7 @@ public class Parser { ...@@ -3049,40 +3049,7 @@ public class Parser {
} }
Window over = null; Window over = null;
if (readIf("OVER")) { if (readIf("OVER")) {
read(OPEN_PAREN); over = readWindowSpecification(aggregate);
ArrayList<Expression> partitionBy = null;
if (readIf("PARTITION")) {
read("BY");
partitionBy = Utils.newSmallArrayList();
do {
Expression expr = readExpression();
partitionBy.add(expr);
} while (readIf(COMMA));
}
ArrayList<SelectOrderBy> orderBy = null;
if (readIf(ORDER)) {
read("BY");
orderBy = parseSimpleOrderList();
} else if (!isAggregate) {
orderBy = new ArrayList<>(0);
}
WindowFrame frame;
if (aggregate instanceof WindowFunction) {
WindowFunction w = (WindowFunction) aggregate;
switch (w.getFunctionType()) {
case FIRST_VALUE:
case LAST_VALUE:
case NTH_VALUE:
frame = readWindowFrame();
break;
default:
frame = null;
}
} else {
frame = readWindowFrame();
}
read(CLOSE_PAREN);
over = new Window(partitionBy, orderBy, frame);
aggregate.setOverCondition(over); aggregate.setOverCondition(over);
currentSelect.setWindowQuery(); currentSelect.setWindowQuery();
} else if (!isAggregate) { } else if (!isAggregate) {
...@@ -3092,6 +3059,41 @@ public class Parser { ...@@ -3092,6 +3059,41 @@ public class Parser {
} }
} }
private Window readWindowSpecification(AbstractAggregate aggregate) {
read(OPEN_PAREN);
ArrayList<Expression> partitionBy = null;
if (readIf("PARTITION")) {
read("BY");
partitionBy = Utils.newSmallArrayList();
do {
Expression expr = readExpression();
partitionBy.add(expr);
} while (readIf(COMMA));
}
ArrayList<SelectOrderBy> orderBy = null;
if (readIf(ORDER)) {
read("BY");
orderBy = parseSimpleOrderList();
}
WindowFrame frame;
if (aggregate instanceof WindowFunction) {
WindowFunction w = (WindowFunction) aggregate;
switch (w.getFunctionType()) {
case FIRST_VALUE:
case LAST_VALUE:
case NTH_VALUE:
frame = readWindowFrame();
break;
default:
frame = null;
}
} else {
frame = readWindowFrame();
}
read(CLOSE_PAREN);
return new Window(partitionBy, orderBy, frame);
}
private WindowFrame readWindowFrame() { private WindowFrame readWindowFrame() {
WindowFrameUnits units; WindowFrameUnits units;
if (readIf("ROWS")) { if (readIf("ROWS")) {
......
...@@ -118,6 +118,8 @@ public abstract class AbstractAggregate extends Expression { ...@@ -118,6 +118,8 @@ public abstract class AbstractAggregate extends Expression {
ArrayList<SelectOrderBy> orderBy = over.getOrderBy(); ArrayList<SelectOrderBy> orderBy = over.getOrderBy();
if (orderBy != null) { if (orderBy != null) {
overOrderBySort = createOrder(session, orderBy, getNumExpressions()); overOrderBySort = createOrder(session, orderBy, getNumExpressions());
} else if (!isAggregate()) {
overOrderBySort = new SortOrder(session.getDatabase(), new int[getNumExpressions()], new int[0], null);
} }
} }
return this; return this;
...@@ -176,7 +178,7 @@ public abstract class AbstractAggregate extends Expression { ...@@ -176,7 +178,7 @@ public abstract class AbstractAggregate extends Expression {
} }
if (over != null) { if (over != null) {
ArrayList<SelectOrderBy> orderBy = over.getOrderBy(); ArrayList<SelectOrderBy> orderBy = over.getOrderBy();
if (orderBy != null) { if (orderBy != null || !isAggregate()) {
updateOrderedAggregate(session, groupData, groupRowId, orderBy); updateOrderedAggregate(session, groupData, groupRowId, orderBy);
return; return;
} }
...@@ -359,7 +361,7 @@ public abstract class AbstractAggregate extends Expression { ...@@ -359,7 +361,7 @@ public abstract class AbstractAggregate extends Expression {
data = partition.getData(); data = partition.getData();
} }
} }
if (over.getOrderBy() != null) { if (over.getOrderBy() != null || !isAggregate()) {
return getOrderedResult(session, groupData, partition, data); return getOrderedResult(session, groupData, partition, data);
} }
Value result = partition.getResult(); Value result = partition.getResult();
...@@ -384,10 +386,11 @@ public abstract class AbstractAggregate extends Expression { ...@@ -384,10 +386,11 @@ public abstract class AbstractAggregate extends Expression {
private void updateOrderedAggregate(Session session, SelectGroups groupData, int groupRowId, private void updateOrderedAggregate(Session session, SelectGroups groupData, int groupRowId,
ArrayList<SelectOrderBy> orderBy) { ArrayList<SelectOrderBy> orderBy) {
int ne = getNumExpressions(); int ne = getNumExpressions();
int size = orderBy.size(); int size = orderBy != null ? orderBy.size() : 0;
Value[] array = new Value[ne + size + 1]; Value[] array = new Value[ne + size + 1];
rememberExpressions(session, array); rememberExpressions(session, array);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@SuppressWarnings("null")
SelectOrderBy o = orderBy.get(i); SelectOrderBy o = orderBy.get(i);
array[ne++] = o.expression.getValue(session); array[ne++] = o.expression.getValue(session);
} }
...@@ -395,7 +398,6 @@ public abstract class AbstractAggregate extends Expression { ...@@ -395,7 +398,6 @@ public abstract class AbstractAggregate extends Expression {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ArrayList<Value[]> data = (ArrayList<Value[]>) getData(session, groupData, false, true); ArrayList<Value[]> data = (ArrayList<Value[]>) getData(session, groupData, false, true);
data.add(array); data.add(array);
return;
} }
private Value getOrderedResult(Session session, SelectGroups groupData, PartitionData partition, Object data) { private Value getOrderedResult(Session session, SelectGroups groupData, PartitionData partition, Object data) {
...@@ -404,9 +406,12 @@ public abstract class AbstractAggregate extends Expression { ...@@ -404,9 +406,12 @@ public abstract class AbstractAggregate extends Expression {
result = new HashMap<>(); result = new HashMap<>();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ArrayList<Value[]> orderedData = (ArrayList<Value[]>) data; ArrayList<Value[]> orderedData = (ArrayList<Value[]>) data;
int ne = getNumExpressions(); int rowIdColumn = getNumExpressions();
int rowIdColumn = ne + over.getOrderBy().size(); ArrayList<SelectOrderBy> orderBy = over.getOrderBy();
Collections.sort(orderedData, overOrderBySort); if (orderBy != null) {
rowIdColumn += orderBy.size();
Collections.sort(orderedData, overOrderBySort);
}
getOrderedResultLoop(session, result, orderedData, rowIdColumn); getOrderedResultLoop(session, result, orderedData, rowIdColumn);
partition.setOrderedResult(result); partition.setOrderedResult(result);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论