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

feat(sql): improve interval expression, support shortened version #4182

Merged

Conversation

etolbakov
Copy link
Collaborator

@etolbakov etolbakov commented Jun 20, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link

#4168

What's changed and what's your intention?

improve interval expression by handling shortened version

Checklist

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.

Summary by CodeRabbit

  • New Features

    • Introduced transformation to expand short interval names (e.g., 'h' to 'hours') in SQL expressions.
  • Documentation

    • Updated comments related to the Int8 alias for Postgres Bigint.
  • Tests

    • Added new SQL queries to test interval operations like addition and subtraction.

@etolbakov etolbakov requested a review from a team as a code owner June 20, 2024 12:41
@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Jun 20, 2024
Copy link

codecov bot commented Jun 20, 2024

Codecov Report

Attention: Patch coverage is 97.29730% with 4 lines in your changes missing coverage. Please review.

Project coverage is 84.76%. Comparing base (4b42c7b) to head (a8435dc).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4182      +/-   ##
==========================================
- Coverage   85.03%   84.76%   -0.28%     
==========================================
  Files        1030     1031       +1     
  Lines      181129   181276     +147     
==========================================
- Hits       154020   153653     -367     
- Misses      27109    27623     +514     

@etolbakov
Copy link
Collaborator Author

I see that one sqlness test is failing

Result unexpected, path:"./tests/cases/standalone/common/types/interval/interval.sql"
[
    ... skip(287) ...,
~   Error: 3000(PlanQuery), Failed to plan SQL: Error during planning: No function matches the given name and argument types 'SUM(Interval(MonthDayNano))'. You might need to add explicit type casts. -> Error: 3000(PlanQuery), Failed to plan SQL: Error during planning: Execution error: User-defined coercion failed with Execution("Sum not supported for Interval(MonthDayNano)") and No function matches the given name and argument types 'SUM(Interval(MonthDayNano))'. You might need to add explicit type casts.,
    	Candidate functions:,
~   	SUM(Int8/Int16/Int32/Int64/UInt8/UInt16/UInt32/UInt64/Float32/Float64) -> 	SUM(UserDefined),
    ... skip(11) ...
]

strangely enough I don't see the same behaviour running those tests locally. Keep looking though

@killme2008
Copy link
Contributor

@etolbakov Could you rebase the branch with the main branch? Looks like it's an issue after upgrading DataFusion and fixed.

@etolbakov
Copy link
Collaborator Author

etolbakov commented Jun 20, 2024

🤔
I've just synced with the latest upstream in my branch and run sqlness tests locally, interval is passing

Test case "/Users/eugenet/Documents/projects/other/rust/greptimedb/tests/cases/standalone/common/types/float/nan_ordering" finished, cost: 221ms
Test case "/Users/eugenet/Documents/projects/other/rust/greptimedb/tests/cases/standalone/common/types/float/nan_window" finished, cost: 148ms
Test case "/Users/eugenet/Documents/projects/other/rust/greptimedb/tests/cases/standalone/common/types/interval/interval" finished, cost: 165ms
Test case "/Users/eugenet/Documents/projects/other/rust/greptimedb/tests/cases/standalone/common/types/string/bigstring" finished, cost: 109ms
standalone log file at /var/folders/7l/7j6kdl01359dqfh_wz6sztgm0000gp/T/sqlnessMgeUNN/greptime-sqlness-standalone.log

Looks like it's an issue after upgrading DataFusion and fixed.

seems that the error message format became different.

Error: 3000(PlanQuery), Failed to plan SQL: Error during planning: Execution error: User-defined coercion failed with Execution("Sum not supported for Interval(MonthDayNano)") and No function matches the given name and argument types 'SUM(Interval(MonthDayNano))'. You might need to add explicit type casts.
	Candidate functions:
	SUM(UserDefined)

@killme2008
Copy link
Contributor

@etolbakov Let me try it on my machine, thank you 👍

@killme2008
Copy link
Contributor

Did you forget to commit interval.result?

Yes, the error message is different after upgrading DataFusion, you have to commit and push it too.

@killme2008
Copy link
Contributor

killme2008 commented Jun 20, 2024

My result is,

Error: 3000(PlanQuery), Failed to plan SQL: Error during planning: Execution error: User-defined coercion failed with Execution("Sum not supported for Interval(MonthDayNano)") and No function matches the given name and argument types 'SUM(Interval(MonthDayNano))'. You might need to add explicit type casts.
	Candidate functions:
	SUM(UserDefined)

It's different from your branch result.

@etolbakov
Copy link
Collaborator Author

Thank you very much for validating! let me double check now (maybe it's me being inattentive)

Copy link
Contributor

@killme2008 killme2008 left a comment

Choose a reason for hiding this comment

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

What a great job! Thanks, @etolbakov
I have a few suggestions, please take a look.

src/sql/src/statements/transform/expand_interval.rs Outdated Show resolved Hide resolved
src/sql/src/statements/transform/expand_interval.rs Outdated Show resolved Hide resolved
src/sql/src/statements/transform/expand_interval.rs Outdated Show resolved Hide resolved
src/sql/src/statements/transform/expand_interval.rs Outdated Show resolved Hide resolved
src/sql/src/statements/transform/expand_interval.rs Outdated Show resolved Hide resolved
tests/cases/standalone/common/types/interval/interval.sql Outdated Show resolved Hide resolved
Copy link
Contributor

coderabbitai bot commented Jun 22, 2024

Walkthrough

This update introduces a new transformer rule for SQL statements to expand short interval names to their full names, such as converting 'h' to 'hours'. Additionally, it includes new SQL test cases to validate these changes, and a minor comment update regarding type aliases in a different module.

Changes

File/Directory Change Summary
src/sql/src/statements/transform.rs Introduced expand_interval module and added ExpandIntervalTransformRule to the RULES vector.
src/sql/src/statements/transform/expand_interval.rs Created new module to expand short interval names to full names. Implemented ExpandIntervalTransformRule.
src/sql/src/statements/transform/type_alias.rs Updated comments related to the Int8 alias for Postgres Bigint in the replace_type_alias function.
tests/cases/standalone/common/types/interval/interval.result Added SQL queries demonstrating various interval operations.
tests/cases/standalone/common/types/interval/interval.sql Added SQL queries selecting various intervals.

Sequence Diagram(s)

sequenceDiagram
    participant SQLParser
    participant TransformEngine
    participant ExpandIntervalTransformRule
    participant SQLExecutor

    SQLParser->>TransformEngine: Parse query
    TransformEngine->>ExpandIntervalTransformRule: Apply expansion rule
    ExpandIntervalTransformRule-->>TransformEngine: Expanded intervals
    TransformEngine->>SQLExecutor: Transformed query
    SQLExecutor->>SQLExecutor: Execute query
Loading

Poem

In the land of SQL statements,
New intervals take their place.
No more just 'h' or 'm' or 's',
Full names now fill the space.
Expanding intervals clear and bright,
Bringing queries to full light. 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5566dd7 and 3e7e59d.

Files selected for processing (5)
  • src/sql/src/statements/transform.rs (1 hunks)
  • src/sql/src/statements/transform/expand_interval.rs (1 hunks)
  • src/sql/src/statements/transform/type_alias.rs (1 hunks)
  • tests/cases/standalone/common/types/interval/interval.result (1 hunks)
  • tests/cases/standalone/common/types/interval/interval.sql (1 hunks)
Files skipped from review due to trivial changes (3)
  • src/sql/src/statements/transform/type_alias.rs
  • tests/cases/standalone/common/types/interval/interval.result
  • tests/cases/standalone/common/types/interval/interval.sql
Additional comments not posted (9)
src/sql/src/statements/transform.rs (4)

23-26: Ensure consistent module usage.

The expand_interval module is correctly declared and used. However, ensure that any functionality from this module is correctly integrated and tested elsewhere in the codebase.


33-33: Addition of ExpandIntervalTransformRule to rules vector.

This addition is crucial for integrating the new interval expansion functionality. Ensure that the ExpandIntervalTransformRule is thoroughly tested to maintain the robustness of the transformation logic.


Line range hint 37-46: Review the TransformRule trait.

The trait provides a framework for implementing transformation rules but does not contain specific logic. Ensure that all implementations of this trait handle their specific logic correctly and efficiently.


Line range hint 52-67: Review the transform_statements function.

The function systematically applies transformation rules to each statement and expression. Ensure that this does not introduce performance issues with large numbers of statements or complex nested expressions.

src/sql/src/statements/transform/expand_interval.rs (5)

24-37: Review the interval short name mappings.

The mappings are comprehensive and cover a wide range of interval units. Ensure that these mappings are kept up-to-date with any changes in the SQL standards or user requirements.


54-54: Struct declaration for interval transformation.

The ExpandIntervalTransformRule struct is well-defined. Ensure that its implementation in the visit_expr method correctly handles all edge cases for interval expressions.


56-81: Review the implementation of interval expression transformation.

The method visit_expr effectively handles interval expressions by expanding short names. Ensure that this method handles all possible input variations and correctly reports any errors or invalid inputs.


84-94: Review the utility function for getting interval short names.

The function get_interval_short_name correctly extracts the unit from the interval string. Ensure that this function is robust against malformed inputs and performs necessary validations.


96-105: Review the utility function for expanding interval names.

The function expand_interval_name uses the mapping to expand short names to their full equivalents. Ensure that this function correctly handles cases where no mapping exists and does not alter the input inappropriately.

Copy link
Contributor

@killme2008 killme2008 left a comment

Choose a reason for hiding this comment

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

LGTM

@killme2008
Copy link
Contributor

@waynexia @fengjiachun Please take a look.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3e7e59d and 044d4c4.

Files selected for processing (3)
  • src/sql/src/statements/transform/expand_interval.rs (1 hunks)
  • tests/cases/standalone/common/types/interval/interval.result (1 hunks)
  • tests/cases/standalone/common/types/interval/interval.sql (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/sql/src/statements/transform/expand_interval.rs
  • tests/cases/standalone/common/types/interval/interval.result
  • tests/cases/standalone/common/types/interval/interval.sql

Copy link
Contributor

@tisonkun tisonkun left a comment

Choose a reason for hiding this comment

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

This is a good patch! I ever introduced a difference between InfluxQL and SQL that SQL has a standard interval literal syntax, but this patch supports the convenient syntax for interval literal :D

@tisonkun tisonkun enabled auto-merge June 24, 2024 20:17
@tisonkun tisonkun added this pull request to the merge queue Jun 24, 2024
Merged via the queue into GreptimeTeam:main with commit cdd4baf Jun 24, 2024
49 checks passed
@etolbakov etolbakov deleted the feat-improve-interval-expression branch June 27, 2024 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required This change does not impact docs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants