-
Notifications
You must be signed in to change notification settings - Fork 155
Add earliest and latest in aggregation and window function #3640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xinyual
wants to merge
18
commits into
opensearch-project:main
Choose a base branch
from
xinyual:addEarliest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7d2813d
add earliest and latest
xinyual 4a82bd5
add more ITs
xinyual 5589557
reuse max/min
xinyual b0246c4
revert useless change
xinyual 6fe9895
add doc
xinyual b6437e4
fix do
xinyual 7cbe0d0
fix doctest
xinyual e5d50f2
add earliest and latest as two condition function
xinyual fd3fcc7
fix relative string
xinyual 6b756e6
add more IT
xinyual 4971444
merge from main
xinyual 343cc4e
change doc
xinyual 8460647
update doc
xinyual 591e4a1
update doc
xinyual c71a9b6
update doc
xinyual a5309ec
add more doc
xinyual c280092
update to utc_timestamp
xinyual 4b9ad84
remove useless change
xinyual 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
64 changes: 64 additions & 0 deletions
64
.../src/main/java/org/opensearch/sql/expression/function/udf/condition/EarliestFunction.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,64 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.function.udf.condition; | ||
|
||
import static org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils.prependFunctionProperties; | ||
import static org.opensearch.sql.utils.DateTimeUtils.getRelativeZonedDateTime; | ||
|
||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
import org.apache.calcite.adapter.enumerable.NotNullImplementor; | ||
import org.apache.calcite.adapter.enumerable.NullPolicy; | ||
import org.apache.calcite.adapter.enumerable.RexToLixTranslator; | ||
import org.apache.calcite.linq4j.tree.Expression; | ||
import org.apache.calcite.linq4j.tree.Expressions; | ||
import org.apache.calcite.rel.type.RelDataType; | ||
import org.apache.calcite.rex.RexCall; | ||
import org.apache.calcite.rex.RexNode; | ||
import org.apache.calcite.sql.type.ReturnTypes; | ||
import org.apache.calcite.sql.type.SqlReturnTypeInference; | ||
import org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.expression.function.FunctionProperties; | ||
import org.opensearch.sql.expression.function.ImplementorUDF; | ||
|
||
public class EarliestFunction extends ImplementorUDF { | ||
public EarliestFunction() { | ||
super(new EarliestImplementor(), NullPolicy.ANY); | ||
} | ||
|
||
@Override | ||
public SqlReturnTypeInference getReturnTypeInference() { | ||
return ReturnTypes.BOOLEAN; | ||
} | ||
|
||
public static class EarliestImplementor implements NotNullImplementor { | ||
@Override | ||
public Expression implement( | ||
RexToLixTranslator rexToLixTranslator, RexCall rexCall, List<Expression> list) { | ||
List<RelDataType> types = rexCall.getOperands().stream().map(RexNode::getType).toList(); | ||
return Expressions.call( | ||
EarliestFunction.class, | ||
"earliest", | ||
prependFunctionProperties( | ||
UserDefinedFunctionUtils.convertToExprValues(list, types), rexToLixTranslator)); | ||
} | ||
} | ||
|
||
public static Boolean earliest(Object... inputs) { | ||
String expression = ((ExprValue) inputs[1]).stringValue(); | ||
Instant candidate = ((ExprValue) inputs[2]).timestampValue(); | ||
FunctionProperties functionProperties = (FunctionProperties) inputs[0]; | ||
Clock clock = functionProperties.getQueryStartClock(); | ||
ZonedDateTime candidateDatetime = ZonedDateTime.ofInstant(candidate, clock.getZone()); | ||
ZonedDateTime earliest = | ||
getRelativeZonedDateTime( | ||
expression, ZonedDateTime.ofInstant(clock.instant(), clock.getZone())); | ||
return earliest.isBefore(candidateDatetime); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
core/src/main/java/org/opensearch/sql/expression/function/udf/condition/LatestFunction.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,64 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.function.udf.condition; | ||
|
||
import static org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils.prependFunctionProperties; | ||
import static org.opensearch.sql.utils.DateTimeUtils.getRelativeZonedDateTime; | ||
|
||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
import org.apache.calcite.adapter.enumerable.NotNullImplementor; | ||
import org.apache.calcite.adapter.enumerable.NullPolicy; | ||
import org.apache.calcite.adapter.enumerable.RexToLixTranslator; | ||
import org.apache.calcite.linq4j.tree.Expression; | ||
import org.apache.calcite.linq4j.tree.Expressions; | ||
import org.apache.calcite.rel.type.RelDataType; | ||
import org.apache.calcite.rex.RexCall; | ||
import org.apache.calcite.rex.RexNode; | ||
import org.apache.calcite.sql.type.ReturnTypes; | ||
import org.apache.calcite.sql.type.SqlReturnTypeInference; | ||
import org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.expression.function.FunctionProperties; | ||
import org.opensearch.sql.expression.function.ImplementorUDF; | ||
|
||
public class LatestFunction extends ImplementorUDF { | ||
public LatestFunction() { | ||
super(new LatestFunction.LatestImplementor(), NullPolicy.ANY); | ||
} | ||
|
||
@Override | ||
public SqlReturnTypeInference getReturnTypeInference() { | ||
return ReturnTypes.BOOLEAN; | ||
} | ||
|
||
public static class LatestImplementor implements NotNullImplementor { | ||
@Override | ||
public Expression implement( | ||
RexToLixTranslator rexToLixTranslator, RexCall rexCall, List<Expression> list) { | ||
List<RelDataType> types = rexCall.getOperands().stream().map(RexNode::getType).toList(); | ||
return Expressions.call( | ||
LatestFunction.class, | ||
"latest", | ||
prependFunctionProperties( | ||
UserDefinedFunctionUtils.convertToExprValues(list, types), rexToLixTranslator)); | ||
} | ||
} | ||
|
||
public static Boolean latest(Object... inputs) { | ||
String expression = ((ExprValue) inputs[1]).stringValue(); | ||
Instant candidate = ((ExprValue) inputs[2]).timestampValue(); | ||
FunctionProperties functionProperties = (FunctionProperties) inputs[0]; | ||
Clock clock = functionProperties.getQueryStartClock(); | ||
ZonedDateTime candidateDatetime = ZonedDateTime.ofInstant(candidate, clock.getZone()); | ||
ZonedDateTime latest = | ||
getRelativeZonedDateTime( | ||
expression, ZonedDateTime.ofInstant(clock.instant(), clock.getZone())); | ||
return latest.isAfter(candidateDatetime); | ||
} | ||
} |
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.
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.