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): add iso-8601 format support for intervals #4291

Merged
merged 5 commits into from
Jul 5, 2024

Conversation

etolbakov
Copy link
Collaborator

@etolbakov etolbakov commented Jul 4, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link

#4215

What's changed and what's your intention?

Add support for the format of ISO 8601 for the interval type.

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

    • Added support for ISO 8601 format interval parsing and normalization.
  • Improvements

    • Enhanced interval transformation by distinguishing between interval abbreviations and ISO 8601 formats.
    • Improved interval string processing capabilities.
  • Tests

    • Added new test cases demonstrating SQL queries with intervals in ISO 8601 format and interval arithmetic operations.

@etolbakov etolbakov requested a review from a team as a code owner July 4, 2024 22:02
@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Jul 4, 2024
Copy link
Contributor

coderabbitai bot commented Jul 4, 2024

Walkthrough

The changes introduce new capabilities for handling intervals within the SQL module, notably adding support for ISO 8601 format strings. Functions have been renamed, new ones added, and the entire interval transformation logic refactored to handle both interval abbreviations and ISO 8601 formats effectively. These enhancements improve the clarity and functionality of interval processing in SQL operations.

Changes

Files Change Summary
Cargo.toml Added the dependency iso8601 version 0.6.1.
src/.../expand_interval.rs Refactoring of interval normalization logic, added support for ISO 8601 formats, and introduced new functions for expanded capabilities.
tests/.../interval.result, tests/.../interval.sql Introduced new test cases for handling and calculating intervals in ISO 8601 format.

Poem

In code where time now spans and flows,
The intervals dance, in ISO clothes.
New paths we carve, through formats old,
With clearer names, our logic's bold.
As tests ensure, our strides hold true,
This rabbit hops, with joy for you.
🐇💻✨


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 b5c6c72 and 14e5f3a.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (4)
  • src/sql/Cargo.toml (1 hunks)
  • src/sql/src/statements/transform/expand_interval.rs (8 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 (2)
  • src/sql/Cargo.toml
  • tests/cases/standalone/common/types/interval/interval.sql
Additional comments not posted (12)
src/sql/src/statements/transform/expand_interval.rs (7)

81-83: Ensure correct handling of binary operations.

The normalize_interval_name function is used to handle the left operand of binary operations. Ensure that it correctly processes both abbreviated and ISO 8601 format strings.

Verification successful

Verified: The normalize_interval_name function correctly handles binary operations.

The function processes both abbreviated and ISO 8601 format strings, as evidenced by the comprehensive test cases covering these scenarios.

  • src/sql/src/statements/transform/expand_interval.rs: Implementation and tests for normalize_interval_name.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the normalize_interval_name function correctly processes the left operand of binary operations.

# Test: Search for the function usage. Expect: Correct handling of both formats.
rg --type rust $'normalize_interval_name'

Length of output: 1524


Script:

#!/bin/bash
# Extract the implementation of the normalize_interval_name function
ast-grep --lang rust --pattern 'fn normalize_interval_name($_) { $$$ }'

# List the test cases related to normalize_interval_name
rg 'normalize_interval_name' --type rust --context 5

Length of output: 9737


104-104: Ensure correct handling of cast expressions.

The normalize_interval_name function is used to handle cast expressions. Ensure that it correctly processes both abbreviated and ISO 8601 format strings.

Verification successful

Ensure correct handling of cast expressions.

The normalize_interval_name function is thoroughly tested and correctly processes both abbreviated and ISO 8601 format strings.

  • The test cases in src/sql/src/statements/transform/expand_interval.rs cover various scenarios including valid and invalid inputs for both formats.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the normalize_interval_name function correctly processes cast expressions.

# Test: Search for the function usage. Expect: Correct handling of both formats.
rg --type rust $'normalize_interval_name'

Length of output: 1524


Script:

#!/bin/bash
# Extract test cases for the normalize_interval_name function to verify if they cover both abbreviated and ISO 8601 format strings.

# Search for test functions that call normalize_interval_name
rg --type rust 'normalize_interval_name' --context 10

Length of output: 14870


71-74: Ensure correct handling of normalized interval names.

The normalize_interval_name function is used to handle the interval value. Ensure that it correctly processes both abbreviated and ISO 8601 format strings.

Verification successful

Ensure correct handling of normalized interval names.

The normalize_interval_name function correctly processes both abbreviated and ISO 8601 format strings, as confirmed by the implementation and extensive test cases.

  • normalize_interval_name function handles:
    • Abbreviated interval strings (e.g., "1y2mo3d")
    • ISO 8601 format strings (e.g., "P1Y2M3D")
  • Test cases verify both valid and invalid inputs.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the normalize_interval_name function correctly processes both abbreviated and ISO 8601 format strings.

# Test: Search for the function usage. Expect: Correct handling of both formats.
rg --type rust $'normalize_interval_name'

Length of output: 1524


Script:

#!/bin/bash
# Extract the implementation of the `normalize_interval_name` function
ast-grep --lang rust --pattern $'fn normalize_interval_name($_) {
    $$$
}' --json

# Extract the test cases related to `normalize_interval_name`
rg --type rust 'normalize_interval_name' --context 10 --json

Length of output: 36402


161-176: Ensure correct parsing of ISO 8601 format strings.

The parse_iso8601_interval function is a new addition to handle ISO 8601 format strings. Ensure that it correctly parses these strings and converts them to milliseconds.

Verification successful

The parse_iso8601_interval function is correctly tested.

The normalize_interval_name function, which utilizes parse_iso8601_interval, is comprehensively tested, including cases with ISO 8601 format strings. This ensures the correctness of parse_iso8601_interval.

  • src/sql/src/statements/transform/expand_interval.rs: Tests for normalize_interval_name cover various ISO 8601 format strings.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the parse_iso8601_interval function correctly parses ISO 8601 format strings.

# Test: Search for the function usage. Expect: Correct parsing of ISO 8601 format strings.
rg --type rust $'parse_iso8601_interval'

Length of output: 260


Script:

#!/bin/bash
# Extracting the context around the function call to understand its usage and testing.

# Display lines around the function call to understand its context
rg --type rust -C 10 'parse_iso8601_interval' src/sql/src/statements/transform/expand_interval.rs

Length of output: 1140


Script:

#!/bin/bash
# Searching for tests related to `normalize_interval_name` to verify if it covers ISO 8601 format strings.

# Find test files and look for `normalize_interval_name` usage
rg --type rust 'normalize_interval_name'

Length of output: 1524


192-196: Ensure correct identification of ISO 8601 format strings.

The is_iso8601_format function is a new addition to check if a given interval string is in ISO 8601 format. Ensure that it correctly identifies these strings.

Verification successful

Ensure correct identification of ISO 8601 format strings.

The is_iso8601_format function correctly identifies ISO 8601 format strings, and the parse_iso8601_interval function processes these strings accurately.

  • The is_iso8601_format function checks for strings starting with 'P', 'p', '-P', or '-p', which are valid indicators of ISO 8601 intervals.
  • The parse_iso8601_interval function correctly parses these strings and converts them to milliseconds.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the is_iso8601_format function correctly identifies ISO 8601 format strings.

# Test: Search for the function usage. Expect: Correct identification of ISO 8601 format strings.
rg --type rust $'is_iso8601_format'

Length of output: 230


Script:

#!/bin/bash
# Extract the surrounding lines of the usage of is_iso8601_format to understand its context and usage.
rg --type rust -A 10 -B 10 'is_iso8601_format\(interval_str\)' src/sql/src/statements/transform/expand_interval.rs

Length of output: 1033


Script:

#!/bin/bash
# Extract the full implementation of parse_iso8601_interval to verify its correctness.
rg --type rust -A 20 -B 0 'fn parse_iso8601_interval' src/sql/src/statements/transform/expand_interval.rs

Length of output: 810


141-159: Ensure correct handling of ISO 8601 format strings.

The normalize_interval_name function has been updated to handle ISO 8601 format strings. Ensure that it correctly processes these strings and maintains existing functionality.

Verification successful

Verified handling of ISO 8601 format strings.

The normalize_interval_name function correctly processes ISO 8601 format strings, as evidenced by the comprehensive test cases and their expected outcomes.

  • Valid ISO 8601 strings are correctly converted to milliseconds.
  • Negative interval strings are correctly processed.
  • Invalid ISO 8601 strings return None.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the normalize_interval_name function correctly processes ISO 8601 format strings.

# Test: Search for the function usage. Expect: Correct handling of ISO 8601 format strings.
rg --type rust $'normalize_interval_name'

Length of output: 1524


Script:

#!/bin/bash
# Description: Extract and review test cases for the normalize_interval_name function to verify ISO 8601 format handling.

# Search for test cases in the expand_interval.rs file.
rg --type rust --pcre2 'normalize_interval_name\("P\d+Y\d+M\d+D.*\)' src/sql/src/statements/transform/expand_interval.rs

Length of output: 177


Script:

#!/bin/bash
# Description: Extract all test cases for the normalize_interval_name function to verify ISO 8601 format handling.

# Search for all test cases in the expand_interval.rs file.
rg --type rust --pcre2 'normalize_interval_name\(".*"\)' src/sql/src/statements/transform/expand_interval.rs -A 3

Length of output: 629


178-190: Ensure correct expansion of interval abbreviation strings.

The expand_interval_abbreviation function is a new addition to handle interval abbreviation strings. Ensure that it correctly expands these strings to full interval names.

tests/cases/standalone/common/types/interval/interval.result (5)

341-347: Ensure correct conversion of ISO 8601 format interval.

The test case demonstrates the conversion of an ISO 8601 format interval to the internal representation. Ensure that it correctly covers the new functionality and maintains existing functionality.


349-355: Ensure correct conversion of negative ISO 8601 format interval.

The test case demonstrates the conversion of a negative ISO 8601 format interval to the internal representation. Ensure that it correctly covers the new functionality and maintains existing functionality.


357-363: Ensure correct casting of ISO 8601 format interval.

The test case demonstrates casting an ISO 8601 format interval to the internal representation. Ensure that it correctly covers the new functionality and maintains existing functionality.


365-371: Ensure correct addition of ISO 8601 format interval.

The test case demonstrates the addition of an ISO 8601 format interval to another interval. Ensure that it correctly covers the new functionality and maintains existing functionality.


Line range hint 1-372:
Ensure correct interval calculations and conversions.

The remaining test cases demonstrate various interval calculations and conversions. Ensure that they correctly cover the new functionality and maintain existing functionality.

Copy link

codecov bot commented Jul 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.96%. Comparing base (bc398cf) to head (b1ef887).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4291      +/-   ##
==========================================
- Coverage   85.22%   84.96%   -0.27%     
==========================================
  Files        1059     1059              
  Lines      187054   187111      +57     
==========================================
- Hits       159412   158973     -439     
- Misses      27642    28138     +496     

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 14e5f3a and 3836dd8.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (1)
  • src/sql/src/statements/transform/expand_interval.rs (8 hunks)
Additional comments not posted (5)
src/sql/src/statements/transform/expand_interval.rs (5)

149-159: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to normalize_interval_name are updated from the old name expand_interval_name.

Verification successful

Function usage verification complete.

All references to the old function name expand_interval_name have been successfully updated to normalize_interval_name.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `normalize_interval_name` are updated from the old name `expand_interval_name`.

# Test: Search for the function usage. Expect: Only occurrences of the new function name.
rg --type rust $'normalize_interval_name'

Length of output: 1524


Script:

#!/bin/bash
# Description: Verify that there are no remaining calls to the old function name `expand_interval_name`.

# Test: Search for the old function name. Expect: No occurrences of the old function name.
rg --type rust $'expand_interval_name'

Length of output: 37


192-196: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to is_iso8601_format are correct.

Verification successful

Let's inspect the surrounding code where is_iso8601_format is used to ensure it is being used correctly.


The function usage is correct.

The function is_iso8601_format is used appropriately within the normalize_interval_name function to check for ISO 8601 formatted interval strings.

  • src/sql/src/statements/transform/expand_interval.rs: Lines 192-196
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `is_iso8601_format` are correct.

# Test: Search for the function usage. Expect: Correct usage of the function.
rg --type rust $'is_iso8601_format'

Length of output: 230


Script:

#!/bin/bash
# Extract lines around the usage of `is_iso8601_format` to verify the context.
rg --type rust -A 10 -B 10 'is_iso8601_format' src/sql/src/statements/transform/expand_interval.rs

Length of output: 1527


178-190: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to expand_interval_abbreviation are correct.

Verification successful

Function usage verified successfully.

The function expand_interval_abbreviation is used correctly within the same file as a fallback when the interval string is not in ISO8601 format.

  • src/sql/src/statements/transform/expand_interval.rs: Verified usage in the conditional block.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `expand_interval_abbreviation` are correct.

# Test: Search for the function usage. Expect: Correct usage of the function.
rg --type rust $'expand_interval_abbreviation'

Length of output: 268


Script:

#!/bin/bash
# Extract and review the context around the function call to ensure it is used correctly.
rg -A 5 -B 5 'expand_interval_abbreviation' src/sql/src/statements/transform/expand_interval.rs

Length of output: 701


Line range hint 71-104:
LGTM! But verify the struct usage in the codebase.

The code changes are approved.

However, ensure that all struct usages of ExpandIntervalTransformRule are correct.

Verification successful

Verified struct usage of ExpandIntervalTransformRule.

The usage of ExpandIntervalTransformRule in the codebase is correct. The struct is properly integrated and tested for various interval expressions, ensuring the correctness of the changes made.

  • src/sql/src/statements/transform.rs
  • src/sql/src/statements/transform/expand_interval.rs
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all struct usages of `ExpandIntervalTransformRule` are correct.

# Test: Search for the struct usage. Expect: Correct usage of the struct.
rg --type rust $'ExpandIntervalTransformRule'

Length of output: 1016


Script:

#!/bin/bash
# Extract lines around usages of ExpandIntervalTransformRule to verify correctness.

# Extract lines around usage in transform.rs
rg --type rust --context 5 'ExpandIntervalTransformRule' src/sql/src/statements/transform.rs

# Extract lines around usage in expand_interval.rs
rg --type rust --context 5 'ExpandIntervalTransformRule' src/sql/src/statements/transform/expand_interval.rs

Length of output: 3438


161-176: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to parse_iso8601_interval are correct.

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.

Great job!

src/sql/src/statements/transform/expand_interval.rs Outdated Show resolved Hide resolved
@killme2008 killme2008 enabled auto-merge July 5, 2024 22:06
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 3836dd8 and b1ef887.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (1)
  • src/sql/src/statements/transform/expand_interval.rs (10 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/sql/src/statements/transform/expand_interval.rs

@killme2008 killme2008 added this pull request to the merge queue Jul 5, 2024
Merged via the queue into GreptimeTeam:main with commit 3f4928e Jul 5, 2024
54 checks passed
@etolbakov etolbakov deleted the iso-8601-interval-support branch July 6, 2024 05:52
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.

3 participants