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

Add The TO_SECONDS Function To The SQL Plugin #232

Merged

Conversation

GabeFernandez310
Copy link

@GabeFernandez310 GabeFernandez310 commented Feb 23, 2023

Description

Adds the to_seconds function to the SQL plugin. Takes a DATE/TIME/DATETIME/TIMESTAMP/STRING/INTEGER representing a point in time, and converts it to seconds since the year 0. This function is based off of MySQL. For arguments of type TIME, the function takes the current date at the given time. For INTEGER, the integer is parsed as a date (e.g. 950501 == 1995-05-01).

Examples:
SELECT TO_SECONDS(950501); -> 62966505600
SELECT TO_SECONDS('2009-11-29'); -> 63426672000
SELECT TO_SECONDS('2009-11-29 13:43:32'); -> 63426721412

Issues Resolved

#722

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.

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
@codecov
Copy link

codecov bot commented Feb 23, 2023

Codecov Report

Merging #232 (8d2587e) into integ-add-to_seconds-function (259b001) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@                       Coverage Diff                        @@
##             integ-add-to_seconds-function     #232   +/-   ##
================================================================
  Coverage                            98.38%   98.39%           
- Complexity                            3693     3706   +13     
================================================================
  Files                                  343      343           
  Lines                                 9107     9148   +41     
  Branches                               585      587    +2     
================================================================
+ Hits                                  8960     9001   +41     
  Misses                                 142      142           
  Partials                                 5        5           
Flag Coverage Δ
sql-engine 98.39% <100.00%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
...c/main/java/org/opensearch/sql/expression/DSL.java 100.00% <100.00%> (ø)
...arch/sql/expression/datetime/DateTimeFunction.java 100.00% <100.00%> (ø)
...h/sql/expression/function/BuiltinFunctionName.java 100.00% <100.00%> (ø)
...a/org/opensearch/sql/utils/DateTimeFormatters.java 100.00% <100.00%> (ø)

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

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
@GabeFernandez310 GabeFernandez310 marked this pull request as ready for review March 1, 2023 23:36
Comment on lines 1662 to 1720
private ExprValue exprToSecondsForIntType(ExprValue dateExpr) {
try {
if (dateExpr.longValue() < 0 || dateExpr.longValue() > 99999999) {
throw new DateTimeException("Integer argument was out of range");
}

try {
LocalDate date = LocalDate.parse(String.valueOf(dateExpr.longValue()),
DATE_FORMATTER_LONG_YEAR);

return new ExprLongValue(
date.toEpochSecond(LocalTime.MIN, ZoneOffset.UTC)
+ DAYS_0000_TO_1970 * SECONDS_PER_DAY);
} catch (DateTimeParseException ignored) {
//ignore parse exception and try next format
}

try {
LocalDate date = LocalDate.parse(String.valueOf(dateExpr.longValue()),
DATE_FORMATTER_SHORT_YEAR);

return new ExprLongValue(
date.toEpochSecond(LocalTime.MIN, ZoneOffset.UTC)
+ DAYS_0000_TO_1970 * SECONDS_PER_DAY);
} catch (DateTimeParseException ignored) {
//ignore parse exception and try next format
}

try {
LocalDate date = LocalDate.parse(String.valueOf(dateExpr.longValue()),
DATE_FORMATTER_SINGLE_DIGIT_YEAR);

return new ExprLongValue(
date.toEpochSecond(LocalTime.MIN, ZoneOffset.UTC)
+ DAYS_0000_TO_1970 * SECONDS_PER_DAY);
} catch (DateTimeParseException ignored) {
//ignore parse exception and try next format
}

try {
LocalDate date = LocalDate.parse(String.valueOf(dateExpr.longValue()),
DATE_FORMATTER_NO_YEAR);

return new ExprLongValue(
date.toEpochSecond(LocalTime.MIN, ZoneOffset.UTC)
+ DAYS_0000_TO_1970 * SECONDS_PER_DAY);
} catch (DateTimeParseException ignored) {
//ignore parse exception and try next format
}

LocalDate date = LocalDate.parse(String.valueOf(dateExpr.longValue()),
DATE_FORMATTER_SINGLE_DIGIT_MONTH);

return new ExprLongValue(
date.toEpochSecond(LocalTime.MIN, ZoneOffset.UTC)
+ DAYS_0000_TO_1970 * SECONDS_PER_DAY);
} catch (DateTimeException e) {
return ExprNullValue.of();
}
Copy link

Choose a reason for hiding this comment

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

I think this can be refactored to reduce duplication of try catches

Copy link
Author

@GabeFernandez310 GabeFernandez310 Mar 2, 2023

Choose a reason for hiding this comment

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

Addressed in ec908ad. Github formatted the diff strangely so I would suggest just looking at the file.

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
* @return is a DateTimeFormatter that can parse the input.
*/
private DateTimeFormatter getFormatter(int dateAsInt) {
if (dateAsInt < 0 || dateAsInt > 99999999) {

Choose a reason for hiding this comment

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

99999999 is a magic number. Make this a static constant.

Choose a reason for hiding this comment

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

nit: that being said, you're trying to length of the number. Why not convert it to a string and get the string length? I think that makes more sense - although less efficient

Copy link
Author

Choose a reason for hiding this comment

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

This is the main reason I did it this way, for efficiency. Actually, using the length of a string might not be too bad though because it enables me to change the multiple if-statements below into a switch/case block. It would also probably be clearer for people who come back to this and read it later... I'll just try to change it.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed in 2b37b0d

}

//Check if dateAsInt is at least 6 digits long
if (dateAsInt - 99999 > 0) {

Choose a reason for hiding this comment

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

99999 is a magic number

Copy link
Author

Choose a reason for hiding this comment

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

Fixed in 2b37b0d

}

//Check if dateAsInt is at least 4 digits long
if (dateAsInt - 999 > 0) {

Choose a reason for hiding this comment

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

999 magic number

Copy link
Author

Choose a reason for hiding this comment

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

Fixed in 2b37b0d

}

//Check if dateAsInt is at least 3 digits long
if (dateAsInt - 99 > 0) {

Choose a reason for hiding this comment

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

magic number

Copy link
Author

Choose a reason for hiding this comment

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

Fixed in 2b37b0d

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
@GabeFernandez310 GabeFernandez310 merged commit b366b76 into integ-add-to_seconds-function Mar 9, 2023
GabeFernandez310 added a commit that referenced this pull request Mar 16, 2023
)

* Add The `TO_SECONDS` Function To The SQL Plugin (#232)

* Added Basic Tests

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added IT Test

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Implementation

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Changed Integration Tests

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Test For Time Type

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Documentation

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Addressed PR Comments

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Fixed Docs and Implementation

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Fixed Checkstyle

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Changed DateTimeFormatter Priority

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added More Formatters

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Updated Docs

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Reworked Implementation For Formatters

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Cleanup

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Test

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Fixed Implementation And Code Coverage

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Changed getFormatter Function

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Comments

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

---------

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Removed Unneeded Code

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

---------

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
(cherry picked from commit d38a6ec)
GabeFernandez310 added a commit that referenced this pull request Mar 16, 2023
)

* Add The `TO_SECONDS` Function To The SQL Plugin (#232)

* Added Basic Tests

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added IT Test

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Implementation

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Changed Integration Tests

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Test For Time Type

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Documentation

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Addressed PR Comments

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Fixed Docs and Implementation

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Fixed Checkstyle

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Changed DateTimeFormatter Priority

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added More Formatters

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Updated Docs

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Reworked Implementation For Formatters

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Cleanup

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Test

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Fixed Implementation And Code Coverage

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Changed getFormatter Function

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Comments

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

---------

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Removed Unneeded Code

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

---------

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
MaxKsyunz pushed a commit that referenced this pull request Mar 29, 2023
) (opensearch-project#1447)

* Add The `TO_SECONDS` Function To The SQL Plugin (#232)

* Added Basic Tests

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added IT Test

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Implementation

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Changed Integration Tests

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Test For Time Type

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Documentation

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Addressed PR Comments

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Fixed Docs and Implementation

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Fixed Checkstyle

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Changed DateTimeFormatter Priority

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added More Formatters

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Updated Docs

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Reworked Implementation For Formatters

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Cleanup

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Test

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Fixed Implementation And Code Coverage

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Changed getFormatter Function

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Added Comments

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

---------

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

* Removed Unneeded Code

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>

---------

Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
(cherry picked from commit d38a6ec)
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