This repository was archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
Support aggregations min, max #541
Merged
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2a82d3a
Support aggregations min, max
chloe-zh 3006190
update
chloe-zh 58a5082
support string in max and min
chloe-zh 0828ef1
Merge remote-tracking branch 'upstream/develop' into agg
chloe-zh 68556f8
update
chloe-zh 9796b09
Merge remote-tracking branch 'upstream/develop' into agg
chloe-zh 0cb7af2
update
chloe-zh 6ff06f2
Merge remote-tracking branch 'upstream/develop' into agg
chloe-zh 6ec824f
added min/max support for date, datetime, time, timestamp types
chloe-zh fcb3d24
address comments
chloe-zh d01d6d6
update
chloe-zh a2586b0
Merge remote-tracking branch 'upstream/develop' into agg
chloe-zh ccad2cc
update
chloe-zh a3b0b43
address comment
chloe-zh 4ea7ad2
add comparison tests
chloe-zh ec1423b
Merge remote-tracking branch 'upstream/develop' into agg
chloe-zh 2fa0af8
update
chloe-zh 4de4f14
added doc
chloe-zh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
85 changes: 85 additions & 0 deletions
85
.../java/com/amazon/opendistroforelasticsearch/sql/expression/aggregation/MaxAggregator.java
This file contains hidden or 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,85 @@ | ||
/* | ||
* 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.data.model.ExprValueUtils.doubleValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.DOUBLE; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.FLOAT; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.INTEGER; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.LONG; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.SHORT; | ||
import static com.amazon.opendistroforelasticsearch.sql.utils.ExpressionUtils.format; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprNullValue; | ||
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.Arrays; | ||
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(returnType); | ||
} | ||
|
||
@Override | ||
public MaxState iterate(BindingTuple tuple, MaxState state) { | ||
Expression expression = getArguments().get(0); | ||
ExprValue value = expression.valueOf(tuple); | ||
if (!(value.isNull() || value.isMissing())) { | ||
state.isEmptyCollection = false; | ||
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; | ||
private boolean isEmptyCollection; | ||
|
||
MaxState(ExprCoreType type) { | ||
maxResult = isNumber(type) ? doubleValue(Double.MIN_VALUE) : LITERAL_NULL; | ||
isEmptyCollection = true; | ||
} | ||
|
||
public void max(ExprValue value) { | ||
maxResult = maxResult.isNull() ? value : maxResult.compareTo(value) > 0 ? maxResult : value; | ||
} | ||
|
||
@Override | ||
public ExprValue result() { | ||
return isEmptyCollection ? ExprNullValue.of() : maxResult; | ||
} | ||
|
||
private boolean isNumber(ExprCoreType type) { | ||
return Arrays.asList(SHORT, INTEGER, LONG, FLOAT, DOUBLE).contains(type); | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
.../java/com/amazon/opendistroforelasticsearch/sql/expression/aggregation/MinAggregator.java
This file contains hidden or 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,100 @@ | ||
/* | ||
* 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.data.model.ExprValueUtils.doubleValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.floatValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.getDoubleValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.getFloatValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.getIntegerValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.getLongValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.getStringValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.integerValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.longValue; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.DOUBLE; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.FLOAT; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.INTEGER; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.LONG; | ||
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.SHORT; | ||
import static com.amazon.opendistroforelasticsearch.sql.utils.ExpressionUtils.format; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprNullValue; | ||
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue; | ||
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType; | ||
import com.amazon.opendistroforelasticsearch.sql.exception.ExpressionEvaluationException; | ||
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.Arrays; | ||
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(returnType); | ||
} | ||
|
||
@Override | ||
public MinState iterate(BindingTuple tuple, MinState state) { | ||
Expression expression = getArguments().get(0); | ||
ExprValue value = expression.valueOf(tuple); | ||
if (!(value.isNull() || value.isMissing())) { | ||
state.isEmptyCollection = false; | ||
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; | ||
private boolean isEmptyCollection; | ||
|
||
MinState(ExprCoreType type) { | ||
minResult = isNumber(type) ? doubleValue(Double.MAX_VALUE) : LITERAL_NULL; | ||
chloe-zh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
isEmptyCollection = true; | ||
} | ||
|
||
public void min(ExprValue value) { | ||
minResult = minResult.isNull() ? value : minResult.compareTo(value) < 0 ? minResult : value; | ||
chloe-zh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public ExprValue result() { | ||
return isEmptyCollection ? ExprNullValue.of() : minResult; | ||
chloe-zh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
||
private boolean isNumber(ExprCoreType type) { | ||
return Arrays.asList(SHORT, INTEGER, LONG, FLOAT, DOUBLE).contains(type); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.