Skip to content
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

Use query execution start time as the value of now-like functions. #149

Merged
merged 18 commits into from
Nov 8, 2022

Conversation

MaxKsyunz
Copy link

Description

Implement now-family of functions using FunctionProperties object.
This object is created at query execution start.
This ensures that each call to now in SELECT now(), now(), now() will return the same value..

Issues Resolved

[List any issues this PR will resolve]

Check List

  • New functionality includes testing.
    • All tests pass, including unit test, integration test and doctest
  • New functionality has been documented.
    • New functionality has javadoc added
    • New functionality has user manual doc added
  • Commits are signed per the DCO using --signoff

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.

MaxKsyunz added 8 commits October 19, 2022 22:33
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]>
- Address checkstyle errors
- Add comments to QueryContext.
- Address PR feedback.

Signed-off-by: MaxKsyunz <[email protected]>
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]>
# Conflicts:
#	core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java
#	core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTestBase.java
Make FunctionProperties serializable -- required toString override.

Signed-off-by: MaxKsyunz <[email protected]>
@Yury-Fridlyand
Copy link

Are you planning to partially revert changes done in #113? You can't prove that now() returns the same value, because it is cached in ExpressionAnalyzer.

Copy link

@Yury-Fridlyand Yury-Fridlyand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to update UNIX_TIMESTAMP function and its tests - opensearch-project#996.

@codecov
Copy link

codecov bot commented Nov 1, 2022

Codecov Report

Merging #149 (5bed983) into integ-constant-now (be4512e) will decrease coverage by 2.58%.
The diff coverage is 100.00%.

@@                   Coverage Diff                    @@
##             integ-constant-now     #149      +/-   ##
========================================================
- Coverage                 98.26%   95.68%   -2.59%     
- Complexity                 3325     3329       +4     
========================================================
  Files                       324      335      +11     
  Lines                      8399     9053     +654     
  Branches                    553      671     +118     
========================================================
+ Hits                       8253     8662     +409     
- Misses                      142      334     +192     
- Partials                      4       57      +53     
Flag Coverage Δ
query-workbench 62.76% <ø> (?)
sql-engine 98.26% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...a/org/opensearch/sql/analysis/AnalysisContext.java 100.00% <ø> (ø)
...rg/opensearch/sql/analysis/ExpressionAnalyzer.java 100.00% <ø> (ø)
...c/main/java/org/opensearch/sql/expression/DSL.java 100.00% <ø> (ø)
...sql/expression/aggregation/AggregatorFunction.java 100.00% <100.00%> (ø)
...search/sql/expression/config/ExpressionConfig.java 100.00% <100.00%> (ø)
...arch/sql/expression/datetime/DateTimeFunction.java 100.00% <100.00%> (ø)
...expression/function/BuiltinFunctionRepository.java 100.00% <100.00%> (ø)
...pensearch/sql/expression/function/FunctionDSL.java 100.00% <100.00%> (ø)
...ch/sql/expression/function/FunctionProperties.java 100.00% <100.00%> (ø)
...expression/function/RelevanceFunctionResolver.java 100.00% <100.00%> (ø)
... and 15 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

MaxKsyunz added 3 commits November 1, 2022 13:07
Add unit tests for FunctionProperties, including ensuring it is
serializable.

Fix serializability of return value of FunctionDSL.implWithProperties

Signed-off-by: MaxKsyunz <[email protected]>
MaxKsyunz added 3 commits November 1, 2022 14:56
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]>
@MaxKsyunz MaxKsyunz marked this pull request as ready for review November 2, 2022 20:13
@Yury-Fridlyand Yury-Fridlyand self-requested a review November 2, 2022 21:26
@Yury-Fridlyand Yury-Fridlyand self-requested a review November 2, 2022 21:27
Copy link

@Yury-Fridlyand Yury-Fridlyand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad to see major test grooming here. Can you add brief what and why was changed? Thanks!

}
};
return Pair.of(functionSignature, functionBuilder);
};
}

/**
* Unary Function Implementation.
* Implementation of no args function that uses FunctionProperties.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you swap no-args-function and one-arg-function back? Just to smaller diff.


private LocalDateTime getExpectedNow() {
return LocalDateTime.now(
functionProperties.getQueryStartClock().withZone(ZoneId.of("UTC")))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use ExprTimestampValue.ZONE here instead?

Comment on lines 15 to 18
{
ExpressionConfig config = new ExpressionConfig();
dsl = config.dsl(config.functionRepository(config.functionExecutionContext()));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make dsl Autowired?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you name variable DSL (uppercase) you can avoid hundreds changes in inherited test classes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you name variable DSL (uppercase) you can avoid hundreds changes in inherited test classes.

namedArgument is a static method -- it works fine without an instance of DSL class. With this updated it's clearer where DSL instance is required and therefore which methods need Spring.

@Yury-Fridlyand
Copy link

Is it supposed to fix conversion ExprTimeValue -> LocalDate, LocalDateTime and Instant in scope of this PR?

@MaxKsyunz
Copy link
Author

Is it supposed to fix conversion ExprTimeValue -> LocalDate, LocalDateTime and Instant in scope of this PR?

Don't think so?I only made changes sufficient to fix failing tests.

@Yury-Fridlyand
Copy link

Is it possible to use implWithProperties with nullMissingHandling? Something like implWithProperties(nullMissingHandling(λ), ...)

* `fsp` argument support is removed until refactoring to avoid bug where `now()`, `now(x)` and
* `now(y) return different values.
*/
private FunctionResolver now(FunctionName functionName) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to return FunctionResolver, but not DefaultFunctionResolver? Does this matter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FunctionResolver is an interface and DefaultFunctionResolver is an implementation.
Standard practice is to return the most generic type possible.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we groom then to return FunctionResolver everywhere in DateTimeFunction?

Comment on lines +34 to +35
"localtime",
"now",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't checkstyle blame indents there?

@MaxKsyunz
Copy link
Author

Is it possible to use implWithProperties with nullMissingHandling? Something like implWithProperties(nullMissingHandling(λ), ...)

Good point! I'll add that.

Add FunctionDSL.nullMissingHandlingWithProperties to all for consistent
null and missing value handling across all functions.

Signed-off-by: MaxKsyunz <[email protected]>
@MaxKsyunz MaxKsyunz merged commit 6614306 into integ-constant-now Nov 8, 2022
MaxKsyunz pushed a commit that referenced this pull request Nov 8, 2022


- Add FunctionProperties interface to capture query metadata and
 provide to function implementations.
- Update FunctionBuilder to take FunctionProperties in addition to
 function arguments when evaluating a SQL function.
- Implement now-like functions using FunctionProperties.
- Add FunctionDSL.nullMissingHandlingWithProperties to allow for
 consistent null and missing value handling across all functions.
- Remove constant value caching from ExpressionAnalyzer
 -- the same behavior is now implemented with FunctionProperties.

### Unit Tests
- Adjust getQueryStartClock_differs_from_instantNow unit test.
  On Windows, getQueryStartClock_differs_from_instantNow fails
  because Instant.now() in the test returns the same value as
  Instant.now() called for FunctionProperties construction.
- Add unit tests for FunctionDSL.
- Use Spring to instantiate dsl in OpenSearchTestBase.

Signed-off-by: MaxKsyunz <[email protected]>
MaxKsyunz pushed a commit that referenced this pull request Nov 10, 2022


- Add FunctionProperties interface to capture query metadata and
 provide to function implementations.
- Update FunctionBuilder to take FunctionProperties in addition to
 function arguments when evaluating a SQL function.
- Implement now-like functions using FunctionProperties.
- Add FunctionDSL.nullMissingHandlingWithProperties to allow for
 consistent null and missing value handling across all functions.
- Remove constant value caching from ExpressionAnalyzer
 -- the same behavior is now implemented with FunctionProperties.

### Unit Tests
- Adjust getQueryStartClock_differs_from_instantNow unit test.
  On Windows, getQueryStartClock_differs_from_instantNow fails
  because Instant.now() in the test returns the same value as
  Instant.now() called for FunctionProperties construction.
- Add unit tests for FunctionDSL.
- Use Spring to instantiate dsl in OpenSearchTestBase.

Signed-off-by: MaxKsyunz <[email protected]>
@MitchellGale
Copy link

Validated ✅ LGTM

@MaxKsyunz MaxKsyunz deleted the dev-constant-now branch February 8, 2023 17:59
andy-k-improving pushed a commit that referenced this pull request Nov 16, 2024
…notes for 2.3.0 version. (#149)

Signed-off-by: Navneet Verma <[email protected]>

Signed-off-by: Navneet Verma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants