This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support aggregate window functions (#946)
* Change grammar and AST builder * Add tostring and getchild for window function AST node * Add aggregate window function class * Add peer window frame and refactor * Name window function and optimize it when analysis * Refactor peer frame by peeking iterator * Comparison test * Add doctest * Add more comparison tests * Add java doc * Rename and fix broken UT * Add more IT * Add design doc * Prepare PR * Fix no sort key bug * Change README
- Loading branch information
Showing
44 changed files
with
1,308 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...com/amazon/opendistroforelasticsearch/sql/expression/window/WindowFunctionExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.sql.expression.window; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.expression.Expression; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.window.frame.WindowFrame; | ||
|
||
/** | ||
* Window function abstraction. | ||
*/ | ||
public interface WindowFunctionExpression extends Expression { | ||
|
||
/** | ||
* Create specific window frame based on window definition and what's current window function. | ||
* For now two types of cumulative window frame is returned: | ||
* 1. Ranking window functions: ignore frame definition and always operates on | ||
* previous and current row. | ||
* 2. Aggregate window functions: frame partition into peers and sliding window is not supported. | ||
* | ||
* @param definition window definition | ||
* @return window frame | ||
*/ | ||
WindowFrame createWindowFrame(WindowDefinition definition); | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
...opendistroforelasticsearch/sql/expression/window/aggregation/AggregateWindowFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.sql.expression.window.aggregation; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue; | ||
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.Expression; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.ExpressionNodeVisitor; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.aggregation.AggregationState; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.aggregation.Aggregator; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.env.Environment; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.window.WindowDefinition; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.window.WindowFunctionExpression; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.window.frame.PeerRowsWindowFrame; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.window.frame.WindowFrame; | ||
import java.util.List; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* Aggregate function adapter that adapts Aggregator for window operator use. | ||
*/ | ||
@EqualsAndHashCode | ||
@RequiredArgsConstructor | ||
public class AggregateWindowFunction implements WindowFunctionExpression { | ||
|
||
private final Aggregator<AggregationState> aggregator; | ||
private AggregationState state; | ||
|
||
@Override | ||
public WindowFrame createWindowFrame(WindowDefinition definition) { | ||
return new PeerRowsWindowFrame(definition); | ||
} | ||
|
||
@Override | ||
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | ||
PeerRowsWindowFrame frame = (PeerRowsWindowFrame) valueEnv; | ||
if (frame.isNewPartition()) { | ||
state = aggregator.create(); | ||
} | ||
|
||
List<ExprValue> peers = frame.next(); | ||
for (ExprValue peer : peers) { | ||
state = aggregator.iterate(peer.bindingTuples(), state); | ||
} | ||
return state.result(); | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return aggregator.type(); | ||
} | ||
|
||
@Override | ||
public <T, C> T accept(ExpressionNodeVisitor<T, C> visitor, C context) { | ||
return aggregator.accept(visitor, context); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return aggregator.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.