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.
Add temporal interval data type and support interval clause as functi…
…on (#687) * add interval type * add interval type * added docs * update * update * corrected typo * update * added interval in sql parser * update
- Loading branch information
Showing
32 changed files
with
915 additions
and
122 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
50 changes: 50 additions & 0 deletions
50
core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/expression/Interval.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,50 @@ | ||
/* | ||
* 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.ast.expression; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@ToString | ||
@EqualsAndHashCode(callSuper = false) | ||
@RequiredArgsConstructor | ||
public class Interval extends UnresolvedExpression { | ||
|
||
private final UnresolvedExpression value; | ||
private final IntervalUnit unit; | ||
|
||
public Interval(UnresolvedExpression value, String unit) { | ||
this.value = value; | ||
this.unit = IntervalUnit.of(unit); | ||
} | ||
|
||
@Override | ||
public List<UnresolvedExpression> getChild() { | ||
return Collections.singletonList(value); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) { | ||
return nodeVisitor.visitInterval(this, context); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
.../src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/expression/IntervalUnit.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,65 @@ | ||
/* | ||
* 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.ast.expression; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum IntervalUnit { | ||
UNKNOWN, | ||
|
||
MICROSECOND, | ||
SECOND, | ||
MINUTE, | ||
HOUR, | ||
DAY, | ||
WEEK, | ||
MONTH, | ||
QUARTER, | ||
YEAR, | ||
SECOND_MICROSECOND, | ||
MINUTE_MICROSECOND, | ||
MINUTE_SECOND, | ||
HOUR_MICROSECOND, | ||
HOUR_SECOND, | ||
HOUR_MINUTE, | ||
DAY_MICROSECOND, | ||
DAY_SECOND, | ||
DAY_MINUTE, | ||
DAY_HOUR, | ||
YEAR_MONTH; | ||
|
||
private static final List<IntervalUnit> INTERVAL_UNITS; | ||
|
||
static { | ||
ImmutableList.Builder<IntervalUnit> builder = new ImmutableList.Builder<>(); | ||
INTERVAL_UNITS = builder.add(IntervalUnit.values()).build(); | ||
} | ||
|
||
/** | ||
* Util method to get interval unit given the unit name. | ||
*/ | ||
public static IntervalUnit of(String unit) { | ||
return INTERVAL_UNITS.stream() | ||
.filter(v -> unit.equalsIgnoreCase(v.name())) | ||
.findFirst() | ||
.orElse(IntervalUnit.UNKNOWN); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...src/main/java/com/amazon/opendistroforelasticsearch/sql/data/model/ExprIntervalValue.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,71 @@ | ||
/* | ||
* 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.data.model; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType; | ||
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType; | ||
import com.amazon.opendistroforelasticsearch.sql.exception.ExpressionEvaluationException; | ||
import java.time.temporal.TemporalAmount; | ||
import java.time.temporal.TemporalUnit; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class ExprIntervalValue extends AbstractExprValue { | ||
private final TemporalAmount interval; | ||
|
||
@Override | ||
public TemporalAmount intervalValue() { | ||
return interval; | ||
} | ||
|
||
@Override | ||
public int compare(ExprValue other) { | ||
TemporalAmount otherInterval = other.intervalValue(); | ||
if (!interval.getClass().equals(other.intervalValue().getClass())) { | ||
throw new ExpressionEvaluationException( | ||
String.format("invalid to compare intervals with units %s and %s", | ||
unit(), ((ExprIntervalValue) other).unit())); | ||
} | ||
return Long.compare( | ||
interval.get(unit()), otherInterval.get(((ExprIntervalValue) other).unit())); | ||
} | ||
|
||
@Override | ||
public boolean equal(ExprValue other) { | ||
return interval.equals(other.intervalValue()); | ||
} | ||
|
||
@Override | ||
public TemporalAmount value() { | ||
return interval; | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return ExprCoreType.INTERVAL; | ||
} | ||
|
||
/** | ||
* Util method to get temporal unit stored locally. | ||
*/ | ||
public TemporalUnit unit() { | ||
return interval.getUnits() | ||
.stream() | ||
.filter(v -> interval.get(v) != 0) | ||
.findAny() | ||
.orElse(interval.getUnits().get(0)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ public enum ExprCoreType implements ExprType { | |
TIMESTAMP, | ||
DATE, | ||
TIME, | ||
INTERVAL, | ||
|
||
/** | ||
* Struct. | ||
|
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.