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(expr): support interval to_char() #14071

Merged
merged 7 commits into from Dec 21, 2023
Merged

feat(expr): support interval to_char() #14071

merged 7 commits into from Dec 21, 2023

Conversation

KeXiangWang
Copy link
Contributor

@KeXiangWang KeXiangWang commented Dec 19, 2023

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

To solve #13561
Postgres' corresponding doc here.

In Postgres, an interval is first converted to a timestamp, and then use timestamp's to_char() implementation to convert the interval to a char. But chrono doesn't support interval type, especially negative intervals.
To reuse the format parsing implementation of chrono, in this PR, we still first convert the template to chrono template, and then format the interval based on the the Item types and the corresponding behaviors in Postgres.

In the test file, you may see some output really wired. For example,

select to_char('-120hours'::interval, 'HH12');
----
012

I have to say the implementation of postgres is not prudent and unified among different Items. Here, I choose to strictly follow their behaviors.

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

to_char

Converts the input to string according to the given format. Both uppercase and lowercase formats are supported.

to_char ( timestamptz, format ) → *string*
to_char ( timestamp, format ) → *string*
to_char ( interval, format ) → *string*
to_char(timestamp '2002-04-20 17:31:12.66', 'HH12:MI:SS') → '05:31:12'

to_char('2023-07-11 20:01:00-07:00'::timestamptz, 'HH12:MI:SS TZH:TZM') → 03:01:00 +00:00

to_char('1year 2 month 3day 4hour 5minute 6second'::interval, 'YYYY MM DD PM HH12 HH24 MI SS MS US') → 0001 02 03 AM 04 04 05 06 000 000000

Copy link

codecov bot commented Dec 19, 2023

Codecov Report

Attention: 192 lines in your changes are missing coverage. Please review.

Comparison is base (640befd) 67.99% compared to head (78f584b) 67.88%.

Files Patch % Lines
src/expr/impl/src/scalar/to_char.rs 5.88% 192 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14071      +/-   ##
==========================================
- Coverage   67.99%   67.88%   -0.11%     
==========================================
  Files        1554     1554              
  Lines      268874   269076     +202     
==========================================
- Hits       182819   182670     -149     
- Misses      86055    86406     +351     
Flag Coverage Δ
rust 67.88% <5.88%> (-0.11%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@xxchan xxchan left a comment

Choose a reason for hiding this comment

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

Is there any pg regress tests for it? 🤔

@KeXiangWang
Copy link
Contributor Author

Is there any pg regress tests for it? 🤔

Good point! will check.

Copy link
Contributor

@wangrunji0408 wangrunji0408 left a comment

Choose a reason for hiding this comment

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

LGTM for the code. I don't check the format.

src/expr/impl/src/scalar/to_char.rs Outdated Show resolved Hide resolved
src/expr/impl/src/scalar/to_char.rs Outdated Show resolved Hide resolved
src/expr/impl/src/scalar/to_char.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@TennyZhuang TennyZhuang left a comment

Choose a reason for hiding this comment

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

Generally LGTM

@KeXiangWang
Copy link
Contributor Author

Is there any pg regress tests for it? 🤔

Unfortunately, no available regress tests for us, the tests are either for timestamp and timestamptz or composed of currently unsupported patterns.

@KeXiangWang KeXiangWang linked an issue Dec 21, 2023 that may be closed by this pull request
@KeXiangWang KeXiangWang added this pull request to the merge queue Dec 21, 2023
Merged via the queue into main with commit 5b36cb6 Dec 21, 2023
28 of 29 checks passed
@KeXiangWang KeXiangWang deleted the wkx/interval-to_char branch December 21, 2023 01:20
huangjw806 pushed a commit that referenced this pull request Dec 25, 2023
huangjw806 added a commit that referenced this pull request Dec 25, 2023
Co-authored-by: Kexiang Wang <kx.wang@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.

feat(expr): to_char on interval
4 participants