-
Notifications
You must be signed in to change notification settings - Fork 0
Rework on now function implementation
#113
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
Changes from 8 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.FunctionWithCachedValue; | ||
| import org.opensearch.sql.ast.expression.HighlightFunction; | ||
| import org.opensearch.sql.ast.expression.In; | ||
| import org.opensearch.sql.ast.expression.Interval; | ||
|
|
@@ -169,6 +170,20 @@ public Expression visitRelevanceFieldList(RelevanceFieldList node, AnalysisConte | |
| ImmutableMap.copyOf(node.getFieldList()))); | ||
| } | ||
|
|
||
| @Override | ||
| public Expression visitFunctionWithCachedValue(FunctionWithCachedValue 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
Author
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.getCachedFunctionValues().containsKey(valueName)) { | ||
| return context.getCachedFunctionValues().get(valueName); | ||
| } | ||
|
|
||
| var value = visitFunction(node, context); | ||
| value = DSL.literal(value.valueOf(null)); | ||
| context.getCachedFunctionValues().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 FunctionWithCachedValue extends Function { | ||
|
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. These are called constant functions in math.
Author
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, renamed in 6ef778d. |
||
|
|
||
| public FunctionWithCachedValue(String funcName, List<UnresolvedExpression> funcArgs) { | ||
| super(funcName, funcArgs); | ||
| } | ||
|
|
||
| @Override | ||
| public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) { | ||
| return nodeVisitor.visitFunctionWithCachedValue(this, context); | ||
| } | ||
| } | ||
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
nowfunction (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
nowis 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.