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 aggregations min, max (#541)
* Support aggregations min, max * update * support string in max and min * update * update * added min/max support for date, datetime, time, timestamp types * address comments * update * update * address comment * add comparison tests * update * added doc
- Loading branch information
Showing
23 changed files
with
710 additions
and
36 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
72 changes: 72 additions & 0 deletions
72
.../java/com/amazon/opendistroforelasticsearch/sql/expression/aggregation/MaxAggregator.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,72 @@ | ||
/* | ||
* 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.aggregation; | ||
|
||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.LITERAL_NULL; | ||
import static com.amazon.opendistroforelasticsearch.sql.utils.ExpressionUtils.format; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue; | ||
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.Expression; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName; | ||
import com.amazon.opendistroforelasticsearch.sql.storage.bindingtuple.BindingTuple; | ||
import java.util.List; | ||
|
||
public class MaxAggregator extends Aggregator<MaxAggregator.MaxState> { | ||
|
||
public MaxAggregator(List<Expression> arguments, ExprCoreType returnType) { | ||
super(BuiltinFunctionName.MAX.getName(), arguments, returnType); | ||
} | ||
|
||
@Override | ||
public MaxState create() { | ||
return new MaxState(); | ||
} | ||
|
||
@Override | ||
public MaxState iterate(BindingTuple tuple, MaxState state) { | ||
Expression expression = getArguments().get(0); | ||
ExprValue value = expression.valueOf(tuple); | ||
if (!(value.isNull() || value.isMissing())) { | ||
state.max(value); | ||
} | ||
return state; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("max(%s)", format(getArguments())); | ||
} | ||
|
||
protected static class MaxState implements AggregationState { | ||
private ExprValue maxResult; | ||
|
||
MaxState() { | ||
maxResult = LITERAL_NULL; | ||
} | ||
|
||
public void max(ExprValue value) { | ||
maxResult = maxResult.isNull() ? value | ||
: (maxResult.compareTo(value) > 0) | ||
? maxResult : value; | ||
} | ||
|
||
@Override | ||
public ExprValue result() { | ||
return maxResult; | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
.../java/com/amazon/opendistroforelasticsearch/sql/expression/aggregation/MinAggregator.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,77 @@ | ||
/* | ||
* 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.aggregation; | ||
|
||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.LITERAL_NULL; | ||
import static com.amazon.opendistroforelasticsearch.sql.utils.ExpressionUtils.format; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue; | ||
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.Expression; | ||
import com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName; | ||
import com.amazon.opendistroforelasticsearch.sql.storage.bindingtuple.BindingTuple; | ||
import java.util.List; | ||
|
||
/** | ||
* The minimum aggregator aggregate the value evaluated by the expression. | ||
* If the expression evaluated result is NULL or MISSING, then the result is NULL. | ||
*/ | ||
public class MinAggregator extends Aggregator<MinAggregator.MinState> { | ||
|
||
public MinAggregator(List<Expression> arguments, ExprCoreType returnType) { | ||
super(BuiltinFunctionName.MIN.getName(), arguments, returnType); | ||
} | ||
|
||
|
||
@Override | ||
public MinState create() { | ||
return new MinState(); | ||
} | ||
|
||
@Override | ||
public MinState iterate(BindingTuple tuple, MinState state) { | ||
Expression expression = getArguments().get(0); | ||
ExprValue value = expression.valueOf(tuple); | ||
if (!(value.isNull() || value.isMissing())) { | ||
state.min(value); | ||
} | ||
return state; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("min(%s)", format(getArguments())); | ||
} | ||
|
||
protected static class MinState implements AggregationState { | ||
private ExprValue minResult; | ||
|
||
MinState() { | ||
minResult = LITERAL_NULL; | ||
} | ||
|
||
public void min(ExprValue value) { | ||
minResult = minResult.isNull() ? value | ||
: (minResult.compareTo(value) < 0) | ||
? minResult : value; | ||
} | ||
|
||
@Override | ||
public ExprValue result() { | ||
return minResult; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.