-
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
Conversation
Signed-off-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
@penghuo, please, have a look on the proposed changes. If everything is OK, it will be merged into PR opensearch-project#754's branch and update it. |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Should remove the comment.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably remove the comment.
docs/user/dql/functions.rst
Outdated
+----------------------------+---------------------+ | ||
+----------------------------+----------------------------+ | ||
| value_1 | value_2 | | ||
|----------------------------+----------------------------+ |
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.
Should be a vertical pipe (|) at the end of this line instead of +
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.
Oh right! We don't have doctests running for these functions to validate.
| TIMESTAMP | TO_DAYS | YEAR | WEEK | DATE_FORMAT | NOW | CURDATE | CURRENT_DATE | CURTIME | CURRENT_TIME | ||
| LOCALTIME | CURRENT_TIMESTAMP | LOCALTIMESTAMP | SYSDATE | UTC_TIMESTAMP | UTC_DATE | UTC_TIME | ||
| MAKETIME | MAKEDATE | ||
| TIMESTAMP | TO_DAYS | YEAR | WEEK | DATE_FORMAT | SYSDATE | MAKETIME | MAKEDATE |
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.
Please add these functions in alphabetical order to remain consistent.
| TIMESTAMP | TO_DAYS | YEAR | WEEK | DATE_FORMAT | NOW | CURDATE | CURRENT_DATE | CURTIME | CURRENT_TIME | ||
| LOCALTIME | CURRENT_TIMESTAMP | LOCALTIMESTAMP | SYSDATE | UTC_TIMESTAMP | UTC_DATE | UTC_TIME | ||
| MAKETIME | MAKEDATE | ||
| TIMESTAMP | TO_DAYS | YEAR | WEEK | DATE_FORMAT | SYSDATE | MAKETIME | MAKEDATE |
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.
Please put in alphabetical order.
now
function implementation
Signed-off-by: Yury-Fridlyand <[email protected]>
/** | ||
* Storage for values of functions which return a constant value. | ||
* We are storing the values there to use it in sequential calls to those functions. | ||
* For example, `now` function should the same value during processing a query. |
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, when now()
and now(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.
Signed-off-by: Yury-Fridlyand <[email protected]>
This comment was marked as spam.
This comment was marked as spam.
@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 comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's sufficient to use node.getFuncName
here.
toString
is not a great option to generate a hashmap key. All it guarantees is that the result is a string -- intended for logging and debugging. There is no requirement that it's unique for different object.
In the case of Function
object, it happens to include name and parameters making it ok as a key but it would be just as valid for Function.toString
to return "An unresolved function expression"
or "Function object 0xDEADBEEF"
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.
Thank you, fixed in 4008c75.
* [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 comment
The reason will be displayed to describe this comment to others. Learn more.
These are called constant functions in math.
ConstantFunction
or ConstantValueFunction
better explain what's special in this case.
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.
Thank you, renamed in 6ef778d.
@@ -307,6 +307,11 @@ functionCall | |||
| aggregateFunction (orderByClause)? filterClause #filteredAggregationFunctionCall | |||
| relevanceFunction #relevanceFunctionCall | |||
| highlightFunction #highlightFunctionCall | |||
| functionWithCachedValue #functionLikeConstantCall |
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.
It'd be cleaner if #functionLikeConstantCall
matched the rule name.
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.
Thank you, renamed in 6ef778d.
| functionWithCachedValue #functionLikeConstantCall | ||
; | ||
|
||
functionWithCachedValue |
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.
constantValuedFunction
or constantFunction
are more appropriate names for this rule.
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.
Thank you, renamed in 6ef778d.
Signed-off-by: Yury-Fridlyand <[email protected]>
…hedValue` to `ConstantFunction`. Signed-off-by: Yury-Fridlyand <[email protected]>
Unexpected result for now with number up to 6. Unsure what the standard is for warning messages for invalid inputs, but could also give a warning for values too high.
Invalid argument on now gives warning in MySQL. Should OpenSearchSQL match?
|
Results differ between current_timestamp in MySQL and OpenSearchSQL. OpenSearchSQL:
MySQL:
Results differ between localtimestamp in MySQL and OpenSearchSQL. OpenSearchSQL:
MySQL:
|
New implementation creates a cache entry for function and its arguments. There is no common cache for |
Signed-off-by: Yury-Fridlyand <[email protected]>
Nice catch! Fixed in 64ccf47. |
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
sysdate()
uses the same function under the hood and it has an argument.
Try
select sysdate(), sysdate(0), sysdate(6);
Signed-off-by: Yury-Fridlyand <[email protected]>
…ions (opensearch-project#754) * Add implementation of `now`, `sysdate`, `localtime` and similar functions (#92) Signed-off-by: Yury-Fridlyand <[email protected]> * Rework on `now` function implementation (#113) Signed-off-by: Yury-Fridlyand <[email protected]> * Minor SQL ANTLR clean-up. Signed-off-by: Yury-Fridlyand <[email protected]> Signed-off-by: Yury-Fridlyand <[email protected]>
Description
Address opensearch-project#754 feedback link
Issues Resolved
now
function value is calculated on AST analysis stage inExpressionAnalyzer
and value is cached inAnalysisContext
. This happens with all synonyms ofnow
too.Limitations
Value of
now
was calculated in 2 stages: (1) get value, (2) format it.ExpressionAnalyzer
is not capable of caching intermediate value and it shouldn't be overloaded bynow
function logic, so I have to remove formatting stage.now
(and all synonyms) don't accept anymorefsp
argument which describes formatting. OtherwiseAnalysisContext
stores different resulting values fornow()
,now(6)
andnow(0)
, which are different in scope of a query - that is incorrect.Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.