提交 77ab3a41 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add some comments and reuse variable

上级 3d50985a
...@@ -76,21 +76,26 @@ public abstract class AbstractAggregate extends DataAnalysisOperation { ...@@ -76,21 +76,26 @@ public abstract class AbstractAggregate extends DataAnalysisOperation {
int rowIdColumn) { int rowIdColumn) {
WindowFrame frame = over.getWindowFrame(); WindowFrame frame = over.getWindowFrame();
if (frame == null || frame.isDefault()) { if (frame == null || frame.isDefault()) {
// Aggregate all values before the current row (including)
Object aggregateData = createAggregateData(); Object aggregateData = createAggregateData();
for (Value[] row : ordered) { for (Value[] row : ordered) {
// Collect values one by one
updateFromExpressions(session, aggregateData, row); updateFromExpressions(session, aggregateData, row);
result.put(row[rowIdColumn].getInt(), getAggregatedValue(session, aggregateData)); result.put(row[rowIdColumn].getInt(), getAggregatedValue(session, aggregateData));
} }
} else if (frame.isFullPartition()) { } else if (frame.isFullPartition()) {
// Aggregate values from the whole partition
Object aggregateData = createAggregateData(); Object aggregateData = createAggregateData();
for (Value[] row : ordered) { for (Value[] row : ordered) {
updateFromExpressions(session, aggregateData, row); updateFromExpressions(session, aggregateData, row);
} }
// All rows have the same value
Value value = getAggregatedValue(session, aggregateData); Value value = getAggregatedValue(session, aggregateData);
for (Value[] row : ordered) { for (Value[] row : ordered) {
result.put(row[rowIdColumn].getInt(), value); result.put(row[rowIdColumn].getInt(), value);
} }
} else { } else {
// All other types of frames (slow)
int size = ordered.size(); int size = ordered.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
Object aggregateData = createAggregateData(); Object aggregateData = createAggregateData();
......
...@@ -244,6 +244,16 @@ public abstract class DataAnalysisOperation extends Expression { ...@@ -244,6 +244,16 @@ public abstract class DataAnalysisOperation extends Expression {
: getWindowResult(session, groupData); : getWindowResult(session, groupData);
} }
/**
* Returns result of this window function or window aggregate. This method
* is not used for plain aggregates.
*
* @param session
* the session
* @param groupData
* the group data
* @return result of this function
*/
private Value getWindowResult(Session session, SelectGroups groupData) { private Value getWindowResult(Session session, SelectGroups groupData) {
PartitionData partition; PartitionData partition;
Object data; Object data;
...@@ -251,15 +261,17 @@ public abstract class DataAnalysisOperation extends Expression { ...@@ -251,15 +261,17 @@ public abstract class DataAnalysisOperation extends Expression {
ValueArray key = over.getCurrentKey(session); ValueArray key = over.getCurrentKey(session);
partition = groupData.getWindowExprData(this, key); partition = groupData.getWindowExprData(this, key);
if (partition == null) { if (partition == null) {
// Window aggregates with FILTER clause may have no collected values
data = forOrderBy ? new ArrayList<>() : createAggregateData(); data = forOrderBy ? new ArrayList<>() : createAggregateData();
partition = new PartitionData(data); partition = new PartitionData(data);
groupData.setWindowExprData(this, key, partition); groupData.setWindowExprData(this, key, partition);
} else { } else {
data = partition.getData(); data = partition.getData();
} }
if (over.getOrderBy() != null || !isAggregate()) { if (forOrderBy || !isAggregate()) {
return getOrderedResult(session, groupData, partition, data); return getOrderedResult(session, groupData, partition, data);
} }
// Window aggregate without ORDER BY clause in window specification
Value result = partition.getResult(); Value result = partition.getResult();
if (result == null) { if (result == null) {
result = getAggregatedValue(session, data); result = getAggregatedValue(session, data);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论