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

SQL: Extend DATE_TRUNC to also operate on intervals(elastic - #46632 ) #47720

Merged
merged 32 commits into from
Mar 23, 2020

Conversation

musteaf
Copy link

@musteaf musteaf commented Oct 8, 2019

The function is extended to operate on intervals according to the PostgreSQL: https://www.postgresql.org/docs/9.1/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC

Closes : #46632

@musteaf musteaf changed the title SQL: Extend DATE_TRUNC to also operate on intervals(elastic#46632) SQL: Extend DATE_TRUNC to also operate on intervals(https://github.com/elastic/elasticsearch/issues/46632) Oct 8, 2019
@musteaf musteaf changed the title SQL: Extend DATE_TRUNC to also operate on intervals(https://github.com/elastic/elasticsearch/issues/46632) SQL: Extend DATE_TRUNC to also operate on intervals(elastic - https://github.com/elastic/elasticsearch/issues/46632 ) Oct 8, 2019
@musteaf musteaf changed the title SQL: Extend DATE_TRUNC to also operate on intervals(elastic - https://github.com/elastic/elasticsearch/issues/46632 ) SQL: Extend DATE_TRUNC to also operate on intervals(elastic - #46632 ) Oct 8, 2019
@martijnvg martijnvg added the :Analytics/SQL SQL querying label Oct 8, 2019
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/SQL)

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

@musteaf,

First of all: thank you for your contribution!
Nice job so far, I left some comments.

Moreover, I'd like to ask you to update the documentation of the function here:
https://github.com/elastic/elasticsearch/master/docs/reference/sql/functions/date-time.asciidoc regarding the 2nd argument type (add the interval support) along with some example/integ tests
here: https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec#L2546
Please also add some integration tests here: https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec#L143 and if possible also some for filter/groupby/having/orderby below in the same file.

Thank you!

@musteaf
Copy link
Author

musteaf commented Oct 8, 2019

@matriv thank you for review. I will fix the problems soon as possible.

Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

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

Nice work, but it needs a serious formatting step before merging. I've added some comments, as well.

@musteaf
Copy link
Author

musteaf commented Oct 14, 2019

Hi @astefan Thank you very much for your kind review. I started to work on problems.

@musteaf
Copy link
Author

musteaf commented Oct 17, 2019

@matriv
I completed most of the tasks. For integration tests, I want to test manually first. I have a problem with importing test datas for integration tests. Could you please share with me if there is any way to import test datas to elasticsearch? I tried Kibana but v7.4 doesnt work with master version of elasticsearch. If there is any code like " ./gradlew run ......." could you share with me. I could not find any information related to test datas in the documents.

@matriv
Copy link
Contributor

matriv commented Oct 17, 2019

@musteaf currently your PR is changed to point to your master branch:

musteaf wants to merge 6 commits into elastic:master from musteaf:master

and we can see no changes anymore.

Our parent class which loads the test_emp data is SpecBaseIntegrationTestCase.

@musteaf
Copy link
Author

musteaf commented Oct 29, 2019

Hi @matriv,
I fixed all issues. I completed the desired things in the review. I committed. Can you see changes now?
But I have a problem with group by with interval type. I think it is related with #43072

This is the query:

dateTruncGroupByWithInterval
SELECT count(*) as count, birth_date + DATE_TRUNC('decade', INTERVAL '1 12:43:21' DAY TO SECONDS) AS dt FROM test_emp GROUP BY dt ORDER BY 2 LIMIT 3;

and error

Original type was [line 1:27: Cannot find grouping for 'birth_date + DATE_TRUNC('decade', INTERVAL '1 12:43:21' DAY TO SECONDS)']. [org.elasticsearch.xpack.sql.planner.FoldingException: line 1:27: Cannot find grouping for 'birth_date + DATE_TRUNC('decade', INTERVAL '1 12:43:21' DAY TO SECONDS)'

Do you have any suggestion?

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

@musteaf Thank you for addressing our comments, I left some new though, most of them minor.

Regarding the integ test for GROUP BY, how about not using an alias but repeat the function in the GROUP BY:

SELECT count(*) as count, birth_date + DATE_TRUNC('decade', INTERVAL '1 12:43:21' DAY TO SECONDS) AS dt FROM test_emp GROUP BY birth_date + DATE_TRUNC('decade', INTERVAL '1 12:43:21' DAY TO SECONDS) ORDER BY 2 LIMIT 3;

or even without the alias:

SELECT count(*) as count, birth_date + DATE_TRUNC('decade', INTERVAL '1 12:43:21' DAY TO SECONDS) FROM test_emp GROUP BY birth_date + DATE_TRUNC('decade', INTERVAL '1 12:43:21' DAY TO SECONDS) ORDER BY 2 LIMIT 3;

If neither work please add the test as is but with -Ignore as a suffix and right above it add a comment:

// AwaitsFix https://github.com/elastic/elasticsearch/issues/42041

so we can enable it once the bug is fixed.

@matriv
Copy link
Contributor

matriv commented Oct 29, 2019

@elasticmachine test this please

@matriv matriv requested a review from astefan October 29, 2019 22:24
@musteaf
Copy link
Author

musteaf commented Oct 30, 2019

@matriv Thank you for your kind review. I really learned a lot from this PR. Thank you for help. I fixed the issues. But, unfortunately GROUP BY doesn't work.

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

I left some more comments, please check.

@matriv
Copy link
Contributor

matriv commented Oct 30, 2019

@elasticmachine test this please

@matriv
Copy link
Contributor

matriv commented Oct 30, 2019

But, unfortunately GROUP BY doesn't work.

Don't worry, thx for adding the test as ignored, it will be enabled once the bug is fixed.

@matriv
Copy link
Contributor

matriv commented Oct 30, 2019

@elasticmachine run elasticsearch-ci/2

@matriv
Copy link
Contributor

matriv commented Oct 30, 2019

@musteaf Please check and fix the checkstyle errors:


:x-pack:plugin:sql:checkstyleTest FAILED
--
[ant:checkstyle] [ERROR] /dev/shm/elastic+elasticsearch+pull-request-2/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java:215: Line is longer than 140 characters (found 153). [LineLength]
[ant:checkstyle] [ERROR] /dev/shm/elastic+elasticsearch+pull-request-2/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessorTests.java:277: Line is longer than 140 characters (found 164). [LineLength]
[ant:checkstyle] [ERROR] /dev/shm/elastic+elasticsearch+pull-request-2/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessorTests.java:281: Line is longer than 140 characters (found 157). [LineLength]
[ant:checkstyle] [ERROR] /dev/shm/elastic+elasticsearch+pull-request-2/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessorTests.java:285: Line is longer than 140 characters (found 155). [LineLength]
[ant:checkstyle] [ERROR] /dev/shm/elastic+elasticsearch+pull-request-2/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessorTests.java:289: Line is longer than 140 characters (found 156). [LineLength]
[ant:checkstyle] [ERROR] /dev/shm/elastic+elasticsearch+pull-request-2/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessorTests.java:293: Line is longer than 140 characters (found 155). [LineLength]

@musteaf
Copy link
Author

musteaf commented Oct 31, 2019

@matriv Thank you for review. I think I fixed the issues. I left comment about asDateTime. Do you have any suggestion about that?

@matriv
Copy link
Contributor

matriv commented Oct 31, 2019

@elasticmachine test this please

@matriv
Copy link
Contributor

matriv commented Oct 31, 2019

@musteaf the PR #43072 has been merged, so please merge master and enable the group by integration test.

@musteaf
Copy link
Author

musteaf commented Oct 31, 2019

@matriv I merged the master and enabled the group by integration test.

@matriv
Copy link
Contributor

matriv commented Oct 31, 2019

@musteaf Please apply the change we discussed in the description here: #47720 (comment)

Please also add references to the 3 examples in the docs.csv-spec file you added to the ascii doc after the examples with Date.

@matriv
Copy link
Contributor

matriv commented Mar 16, 2020

@elasticmachine test this please.

@matriv
Copy link
Contributor

matriv commented Mar 16, 2020

:x-pack:plugin:sql:checkstyleMain FAILED	
[ant:checkstyle] [ERROR] /dev/shm/elastic+elasticsearch+pull-request-2/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTrunc.java:30: Using the '.*' form of import should be avoided - org.elasticsearch.xpack.ql.util.DateUtils.*. [AvoidStarImport]	

@matriv
Copy link
Contributor

matriv commented Mar 16, 2020

@elasticmachine test this please

@matriv
Copy link
Contributor

matriv commented Mar 16, 2020

@musteaf Please run ./gradlew --parallel -p x-pack/plugin/sql check locally on the root dir of the checked out elasticsearch and check the integ tests errors.

@musteaf
Copy link
Author

musteaf commented Mar 17, 2020

@matriv I fixed the issues. Also if there is a "gradlew" command to check styles, could you share with me for further PR? According to the CONTRIBUTING file, we can use Spotless Gradle plugin to check style but It only supports Eclipse. If there is not any "gradlew" command which works with Intellij, I will use Eclipse then. Thanks in advance.

@matriv
Copy link
Contributor

matriv commented Mar 18, 2020

@musteaf I've pasted it earlier: :x-pack:plugin:sql:checkstyleMain & for tests: :x-pack:plugin:sql:checkstyleTest but both are run with the check command I've sent.

@matriv
Copy link
Contributor

matriv commented Mar 18, 2020

@elasticmachine test this please

@musteaf
Copy link
Author

musteaf commented Mar 18, 2020

@matriv thank you so much for the code snippet. I examined elasticmachine's build errors but I could not find any related error with this PR.

@matriv
Copy link
Contributor

matriv commented Mar 18, 2020

@elasticmachine update branch

@matriv
Copy link
Contributor

matriv commented Mar 18, 2020

@elasticmachine test this please

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

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

LGTM. Left just few cosmetics related comments.

Comment on lines 68 to 70
int year = period.getYears();
int firstYearOfCentury = year - (year % 100);
return new IntervalYearMonth(Period.ZERO.plusYears(firstYearOfCentury), iym.dataType());
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong indentation.

import static org.elasticsearch.xpack.ql.util.DateUtils.SECONDS_PER_MINUTE;
import static org.elasticsearch.xpack.sql.expression.SqlTypeResolutions.isDateOrInterval;
import static org.elasticsearch.xpack.sql.type.SqlDataTypes.isInterval;

public class DateTrunc extends BinaryDateTimeFunction {

public enum Part implements DateTimeField {

MILLENNIUM(dt -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a personal preference, but I think the Part enum values are more easily distinguishable if they are indented in a similar style to this: https://gist.github.com/astefan/a4c27b9bfefb1b074156c86d01d507ff.
At the moment, this is a big block of code where, every few lines, there is a capital letters noun without any clear boundaries.

}
if (truncateDateField == Part.WEEK && (timestamp instanceof IntervalDayTime || timestamp instanceof IntervalYearMonth)) {
throw new SqlIllegalArgumentException("Truncating intervals is not supported for {} units",
truncateTo);
Copy link
Contributor

Choose a reason for hiding this comment

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

This truncateTo can sit on the previous line.

@matriv
Copy link
Contributor

matriv commented Mar 21, 2020

@musteaf Could you please address those last comments?

Mustafa Fırtına added 2 commits March 22, 2020 22:46
@matriv
Copy link
Contributor

matriv commented Mar 23, 2020

@elasticmachine test this please

@matriv matriv merged commit 2dc7950 into elastic:master Mar 23, 2020
@matriv
Copy link
Contributor

matriv commented Mar 23, 2020

@musteaf Thank you for contribution!

@musteaf
Copy link
Author

musteaf commented Mar 23, 2020

@matriv Thank you so much for your excellent guidance! That was so educative for me.

matriv added a commit that referenced this pull request Mar 23, 2020
#47720) (#53972)

The function is extended to operate on intervals according to the PostgreSQL: https://www.postgresql.org/docs/9.1/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC

Closes : #46632
(cherry picked from commit 2dc7950)

Co-authored-by: musteaf <gs_mustea@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQL: Extend DATE_TRUNC to also operate on intervals
6 participants