-
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
Changes from 6 commits
c88a5ce
7534d46
0fa818d
eec2e19
2ea3a09
2c3047a
b9542d8
ce2e8bf
4008c75
6ef778d
64ccf47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
import org.opensearch.sql.ast.expression.EqualTo; | ||
import org.opensearch.sql.ast.expression.Field; | ||
import org.opensearch.sql.ast.expression.Function; | ||
import org.opensearch.sql.ast.expression.FunctionLikeConstant; | ||
import org.opensearch.sql.ast.expression.HighlightFunction; | ||
import org.opensearch.sql.ast.expression.In; | ||
import org.opensearch.sql.ast.expression.Interval; | ||
|
@@ -58,6 +59,7 @@ | |
import org.opensearch.sql.expression.aggregation.Aggregator; | ||
import org.opensearch.sql.expression.conditional.cases.CaseClause; | ||
import org.opensearch.sql.expression.conditional.cases.WhenClause; | ||
import org.opensearch.sql.expression.datetime.DateTimeFunction; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionRepository; | ||
import org.opensearch.sql.expression.function.FunctionName; | ||
|
@@ -169,6 +171,19 @@ public Expression visitRelevanceFieldList(RelevanceFieldList node, AnalysisConte | |
ImmutableMap.copyOf(node.getFieldList()))); | ||
} | ||
|
||
@Override | ||
public Expression visitFunctionLikeConstant(FunctionLikeConstant node, AnalysisContext context) { | ||
var valueName = node.toString(); | ||
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 think it's sufficient to use
In the case of 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. Thank you, fixed in 4008c75. |
||
if (context.getFunctionLikeConstantValues().containsKey(valueName)) { | ||
return context.getFunctionLikeConstantValues().get(valueName); | ||
} | ||
|
||
var value = visitFunction(node, context); | ||
value = DSL.literal(value.valueOf(null)); | ||
context.getFunctionLikeConstantValues().put(valueName, value); | ||
return value; | ||
} | ||
|
||
@Override | ||
public Expression visitFunction(Function node, AnalysisContext context) { | ||
FunctionName functionName = FunctionName.of(node.getFuncName()); | ||
|
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 FunctionLikeConstant extends Function { | ||
|
||
public FunctionLikeConstant(String funcName, List<UnresolvedExpression> funcArgs) { | ||
super(funcName, funcArgs); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) { | ||
return nodeVisitor.visitFunctionLikeConstant(this, context); | ||
} | ||
} |
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,13 @@ 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) | ||
//impl((v) -> new ExprDatetimeValue(formatNow(v.integerValue())), DATETIME, INTEGER) | ||
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. Should remove the comment. |
||
); | ||
} | ||
|
||
|
@@ -135,21 +131,20 @@ 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) | ||
//impl((v) -> new ExprTimeValue(formatNow(v.integerValue()).toLocalTime()), TIME, INTEGER) | ||
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. Should probably remove the comment. |
||
); | ||
} | ||
|
||
|
@@ -163,7 +158,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,15 +824,13 @@ 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; | ||
} | ||
|
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.