Repository navigation
Conversation
Add QueryContext interface to capture query metadata and provide to function implementations. Update FunctionBuilder to take QueryContext in addition to function arguments when evaluating a SQL function. Implement constant date time functions using QueryContext. Signed-off-by: MaxKsyunz <[email protected]>
454e986 to
041b701
Compare
|
What does the error message refer to? |
- Address checkstyle errors - Add comments to QueryContext. - Address PR feedback. Signed-off-by: MaxKsyunz <[email protected]>
041b701 to
8c8dca9
Compare
Codecov Report
@@ Coverage Diff @@
## 2.x #937 +/- ##
=============================================
- Coverage 98.26% 62.76% -35.50%
=============================================
Files 324 10 -314
Lines 8399 658 -7741
Branches 553 119 -434
=============================================
- Hits 8253 413 -7840
- Misses 142 192 +50
- Partials 4 53 +49
Flags with carried forward coverage won't be shown. Click here to find out more. Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
|
@MaxKsyunz is there a test to confirm that multiple calls to now function within the same context return the same value? |
|
How to access the brand new |
|
Another way to add this would be to make I like that better conceptually since "query execution start time" is related to plan execution, not function definition. It does seem more complex to implement -- the value would have to be passed from |
See #960 for |
| FunctionBuilder functionBuilder = | ||
| (queryContext, arguments) -> new FunctionExpression(functionName, arguments) { | ||
| @Override | ||
| public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | ||
| ExprValue value = arguments.get(0).valueOf(valueEnv); | ||
| return function.apply(queryContext, value); | ||
| } | ||
|
|
||
| @Override | ||
| public ExprType type() { | ||
| return returnType; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("%s(%s)", functionName, | ||
| arguments.stream() | ||
| .map(Object::toString) | ||
| .collect(Collectors.joining(", "))); | ||
| } | ||
| }; | ||
| return Pair.of(functionSignature, functionBuilder); |
| * Class to capture values that may be necessary to implement some functions. | ||
| * An example would be query execution start time to implement now(). | ||
| */ | ||
| public interface QueryContext { |
There was a problem hiding this comment.
I feel FunctionParameter is more concrete name.
There was a problem hiding this comment.
I feel "function" is overloaded and we don't want to limit this to just functions. There's no reason why the context couldn't be passed to non-function expressions, if for example, we want to save the storage mechanism in here (catalog vs SQL vs something else) and have that available to... say... identifiers or datatypes.
| QueryContext queryContext = new QueryContext() { | ||
| private final Clock currentTime = Clock.fixed(Instant.now(), | ||
| ZoneId.systemDefault()); | ||
| @Override | ||
| public Clock getQueryStartTime() { | ||
| return currentTime; | ||
| } | ||
| }; |
There was a problem hiding this comment.
The implication is BuiltinFunctionRepository and QueryContext is created per request. With current Spring configuration, it is true. I think we should add test to guarantee this.
There was a problem hiding this comment.
Good point -- I will add this test.
Thanks for push this PR. This is long pending issue. |
Thank you @penghuo! I will clean up this PR and re-submit it. As for #960, this is what I'm trying to get to:
It solves this problem but can also be used for other purposes. I'll write up a separate proposal with some examples and cleaner code. |
1. Rename QueryContext to FunctionProperties, renmae related parameter names. 2. Remove QueryContext interface. 3. Add FunctionProperties as a bean to ExpressionConfig. 4. Add unit tests for ExpressionConfig. Refactored unit tests in opensearch module to keep ExpressionCOnfig misuse in one place -- OpenSearchTestBase. Signed-off-by: MaxKsyunz <[email protected]>
Signed-off-by: MaxKsyunz <[email protected]>
Signed-off-by: MaxKsyunz <[email protected]>
Signed-off-by: MaxKsyunz <[email protected]>
# Conflicts: # core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java # core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTestBase.java
| { | ||
| ExpressionConfig config = new ExpressionConfig(); | ||
| dsl = config.dsl(config.functionRepository(config.functionExecutionContext())); | ||
| } |
There was a problem hiding this comment.
Is something missing?
|
|
||
| private final DSL dsl = new ExpressionConfig().dsl(new ExpressionConfig().functionRepository()); | ||
| @Autowired | ||
| private DSL dsl; |
There was a problem hiding this comment.
Can you name it in uppercase to avoid changing all depending tests?
| private DSL dsl; | |
| private DSL DSL; |
Make FunctionProperties serializable -- required toString override. Signed-off-by: MaxKsyunz <[email protected]>
Add unit tests for FunctionProperties, including ensuring it is serializable. Fix serializability of return value of FunctionDSL.implWithProperties Signed-off-by: MaxKsyunz <[email protected]>
Signed-off-by: MaxKsyunz <[email protected]>
Signed-off-by: MaxKsyunz <[email protected]>
Signed-off-by: MaxKsyunz <[email protected]>
Constant value caching is no longer necessary in ExpressionAnalyzer -- the same behavior is now implemented with FunctionProperties. Signed-off-by: MaxKsyunz <[email protected]>
On Windows, getQueryStartClock_differs_from_instantNow fails because Instant.now() in the test returns the same value as Instant.now() called for FunctionProperties construction. Signed-off-by: MaxKsyunz <[email protected]>
It's failing on Windows -- turns out the assumption about Spring it was testing does not hold true across platforms. Signed-off-by: MaxKsyunz <[email protected]>
Signed-off-by: MaxKsyunz <[email protected]>
Signed-off-by: MaxKsyunz <[email protected]>
Add FunctionDSL.nullMissingHandlingWithProperties to all for consistent null and missing value handling across all functions. Signed-off-by: MaxKsyunz <[email protected]>
527a735 to
5bed983
Compare
Description
These changes allow creating SQL functions that expose engine metadata.
As an example, now-family of functions is updated to report consistent time across multiple invocations.
Note:
sysdatewas unmodified. As per MySQL, it returns the time at invocation.Implementation Details
QueryContextinterface -- a way for SQL functions to access SQL plugin metadata.FunctionBuilder.applysignature expectsQueryContextas well as list of arguments.Expression.Config.functionRepositoryand passed toBuiltInFunctionRepository.FunctionDSL.queryContextFunctionmethod. It is similar toFunctionDSL.implbut passesQueryContextto the implementing lambda function.Issues Resolved
[List any issues this PR will resolve]
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.