-
Notifications
You must be signed in to change notification settings - Fork 158
Add implementation of now
, sysdate
, localtime
and similar functions
#754
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
Merged
dai-chen
merged 3 commits into
opensearch-project:2.x
from
Bit-Quill:integ-datetime-now
Sep 23, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
28 changes: 28 additions & 0 deletions
28
core/src/main/java/org/opensearch/sql/ast/expression/ConstantFunction.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,28 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
package org.opensearch.sql.ast.expression; | ||
|
||
import java.util.List; | ||
import lombok.EqualsAndHashCode; | ||
import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
|
||
/** | ||
* Expression node that holds a function which should be replaced by its constant[1] value. | ||
* [1] Constant at execution time. | ||
*/ | ||
@EqualsAndHashCode(callSuper = false) | ||
public class ConstantFunction extends Function { | ||
|
||
public ConstantFunction(String funcName, List<UnresolvedExpression> funcArgs) { | ||
super(funcName, funcArgs); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) { | ||
return nodeVisitor.visitConstantFunction(this, context); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,16 @@ | |
import static org.opensearch.sql.expression.function.FunctionDSL.impl; | ||
import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandling; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.TextStyle; | ||
import java.util.Locale; | ||
import java.util.concurrent.TimeUnit; | ||
import javax.annotation.Nullable; | ||
import lombok.experimental.UtilityClass; | ||
import org.opensearch.sql.data.model.ExprDateValue; | ||
import org.opensearch.sql.data.model.ExprDatetimeValue; | ||
|
@@ -85,6 +89,84 @@ public void register(BuiltinFunctionRepository repository) { | |
repository.register(to_days()); | ||
repository.register(week()); | ||
repository.register(year()); | ||
|
||
repository.register(now()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: should have inserted these alphabetically into the above list There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll update in next PR |
||
repository.register(current_timestamp()); | ||
repository.register(localtimestamp()); | ||
repository.register(localtime()); | ||
repository.register(sysdate()); | ||
repository.register(curtime()); | ||
repository.register(current_time()); | ||
repository.register(curdate()); | ||
repository.register(current_date()); | ||
} | ||
|
||
/** | ||
* NOW() returns a constant time that indicates the time at which the statement began to execute. | ||
* `fsp` argument support is removed until refactoring to avoid bug where `now()`, `now(x)` and | ||
* `now(y) return different values. | ||
*/ | ||
private FunctionResolver now(FunctionName functionName) { | ||
return define(functionName, | ||
impl(() -> new ExprDatetimeValue(formatNow(null)), DATETIME) | ||
); | ||
} | ||
|
||
private FunctionResolver now() { | ||
return now(BuiltinFunctionName.NOW.getName()); | ||
} | ||
|
||
private FunctionResolver current_timestamp() { | ||
return now(BuiltinFunctionName.CURRENT_TIMESTAMP.getName()); | ||
} | ||
|
||
private FunctionResolver localtimestamp() { | ||
return now(BuiltinFunctionName.LOCALTIMESTAMP.getName()); | ||
} | ||
|
||
private FunctionResolver localtime() { | ||
return now(BuiltinFunctionName.LOCALTIME.getName()); | ||
} | ||
|
||
/** | ||
* SYSDATE() returns the time at which it executes. | ||
*/ | ||
private FunctionResolver sysdate() { | ||
return define(BuiltinFunctionName.SYSDATE.getName(), | ||
impl(() -> new ExprDatetimeValue(formatNow(null)), DATETIME), | ||
impl((v) -> new ExprDatetimeValue(formatNow(v.integerValue())), DATETIME, INTEGER) | ||
); | ||
} | ||
|
||
/** | ||
* Synonym for @see `now`. | ||
*/ | ||
private FunctionResolver curtime(FunctionName functionName) { | ||
return define(functionName, | ||
impl(() -> new ExprTimeValue(formatNow(null).toLocalTime()), TIME) | ||
); | ||
} | ||
|
||
private FunctionResolver curtime() { | ||
return curtime(BuiltinFunctionName.CURTIME.getName()); | ||
} | ||
|
||
private FunctionResolver current_time() { | ||
return curtime(BuiltinFunctionName.CURRENT_TIME.getName()); | ||
} | ||
|
||
private FunctionResolver curdate(FunctionName functionName) { | ||
return define(functionName, | ||
impl(() -> new ExprDateValue(formatNow(null).toLocalDate()), DATE) | ||
); | ||
} | ||
|
||
private FunctionResolver curdate() { | ||
return curdate(BuiltinFunctionName.CURDATE.getName()); | ||
} | ||
|
||
private FunctionResolver current_date() { | ||
return curdate(BuiltinFunctionName.CURRENT_DATE.getName()); | ||
} | ||
|
||
/** | ||
|
@@ -742,4 +824,24 @@ private ExprValue exprYear(ExprValue date) { | |
return new ExprIntegerValue(date.dateValue().getYear()); | ||
} | ||
|
||
/** | ||
* Prepare LocalDateTime value. Truncate fractional second part according to the argument. | ||
* @param fsp argument is given to specify a fractional seconds precision from 0 to 6, | ||
* the return value includes a fractional seconds part of that many digits. | ||
* @return LocalDateTime object. | ||
*/ | ||
private LocalDateTime formatNow(@Nullable Integer fsp) { | ||
var res = LocalDateTime.now(); | ||
if (fsp == null) { | ||
fsp = 0; | ||
} | ||
var defaultPrecision = 9; // There are 10^9 nanoseconds in one second | ||
if (fsp < 0 || fsp > 6) { // Check that the argument is in the allowed range [0, 6] | ||
throw new IllegalArgumentException( | ||
String.format("Invalid `fsp` value: %d, allowed 0 to 6", fsp)); | ||
} | ||
var nano = new BigDecimal(res.getNano()) | ||
.setScale(fsp - defaultPrecision, RoundingMode.DOWN).intValue(); | ||
return res.withNano(nano); | ||
} | ||
} |
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.