forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Rework on now
function implementation
#113
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c88a5ce
Try to process `now` in `ExpressionAnalyzer`.
Yury-Fridlyand 7534d46
Optimize `FunctionLikeConstant`.
Yury-Fridlyand 0fa818d
Code cleanup.
Yury-Fridlyand eec2e19
Test coverage.
Yury-Fridlyand 2ea3a09
Fix tests.
Yury-Fridlyand 2c3047a
Fix & update tests, style and docs.
Yury-Fridlyand b9542d8
Address PR feedback by @MitchellGale-BitQuill.
Yury-Fridlyand ce2e8bf
Rename `FunctionLikeConstant` to `FunctionWithCachedValue`.
Yury-Fridlyand 4008c75
Minor cleanup.
Yury-Fridlyand 6ef778d
More thorough renaming of `FunctionLikeConstant` and `FunctionWithCac…
Yury-Fridlyand 64ccf47
Change default precision to match MySQL.
Yury-Fridlyand 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 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
28 changes: 28 additions & 0 deletions
28
core/src/main/java/org/opensearch/sql/ast/expression/ConstantFunction.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,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 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 |
---|---|---|
|
@@ -28,10 +28,8 @@ | |
import java.time.format.TextStyle; | ||
import java.util.Locale; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
import javax.annotation.Nullable; | ||
import lombok.experimental.UtilityClass; | ||
import org.opensearch.sql.common.utils.QueryContext; | ||
import org.opensearch.sql.data.model.ExprDateValue; | ||
import org.opensearch.sql.data.model.ExprDatetimeValue; | ||
import org.opensearch.sql.data.model.ExprIntegerValue; | ||
|
@@ -104,15 +102,12 @@ public void register(BuiltinFunctionRepository repository) { | |
|
||
/** | ||
* 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 LocalDateTime now(@Nullable Integer fsp) { | ||
return formatLocalDateTime(QueryContext::getProcessingStartedTime, fsp); | ||
} | ||
|
||
private FunctionResolver now(FunctionName functionName) { | ||
return define(functionName, | ||
impl(() -> new ExprDatetimeValue(now((Integer)null)), DATETIME), | ||
impl((v) -> new ExprDatetimeValue(now(v.integerValue())), DATETIME, INTEGER) | ||
impl(() -> new ExprDatetimeValue(formatNow(null)), DATETIME) | ||
); | ||
} | ||
|
||
|
@@ -135,21 +130,19 @@ private FunctionResolver localtime() { | |
/** | ||
* SYSDATE() returns the time at which it executes. | ||
*/ | ||
private LocalDateTime sysDate(@Nullable Integer fsp) { | ||
return formatLocalDateTime(LocalDateTime::now, fsp); | ||
} | ||
|
||
private FunctionResolver sysdate() { | ||
return define(BuiltinFunctionName.SYSDATE.getName(), | ||
impl(() -> new ExprDatetimeValue(sysDate(null)), DATETIME), | ||
impl((v) -> new ExprDatetimeValue(sysDate(v.integerValue())), DATETIME, INTEGER) | ||
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(sysDate(null).toLocalTime()), TIME), | ||
impl((v) -> new ExprTimeValue(sysDate(v.integerValue()).toLocalTime()), TIME, INTEGER) | ||
impl(() -> new ExprTimeValue(formatNow(null).toLocalTime()), TIME) | ||
); | ||
} | ||
|
||
|
@@ -163,7 +156,7 @@ private FunctionResolver current_time() { | |
|
||
private FunctionResolver curdate(FunctionName functionName) { | ||
return define(functionName, | ||
impl(() -> new ExprDateValue(sysDate(null).toLocalDate()), DATE) | ||
impl(() -> new ExprDateValue(formatNow(null).toLocalDate()), DATE) | ||
); | ||
} | ||
|
||
|
@@ -829,17 +822,15 @@ private ExprValue exprYear(ExprValue date) { | |
} | ||
|
||
/** | ||
* Prepare LocalDateTime value. | ||
* @param supplier A function which returns LocalDateTime to format. | ||
* 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 formatLocalDateTime(Supplier<LocalDateTime> supplier, | ||
@Nullable Integer fsp) { | ||
var res = supplier.get(); | ||
private LocalDateTime formatNow(@Nullable Integer fsp) { | ||
var res = LocalDateTime.now(); | ||
if (fsp == null) { | ||
return res; | ||
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] | ||
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. Do you need this if you are not having the values in now be handled? 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.
select sysdate(), sysdate(0), sysdate(6); |
||
|
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Constant" suggests that the return value is hard-coded.
I would re-word this as a cache for function return values.
Maybe something like
functionToExpressionCache
.Note: It would be more useful if we could include arguments in the key, then we could use this cache to calculate and cache function calls with different arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to rename it.
Regarding arguments - they are already included, but
now
function (and all synonyms) doesn't accept argument anymore. Otherwise there is bug, whennow()
andnow(6)
have different values, because were calculated twice, because they can't cross-use cached value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. No arguments on
now
is fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renaming done in ce2e8bf.