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): add support for make_date/time/timestamp #14827

Merged
merged 3 commits into from
Feb 1, 2024
Merged

Conversation

KeXiangWang
Copy link
Contributor

@KeXiangWang KeXiangWang commented Jan 26, 2024

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

What's changed and what's your intention?

As title
https://www.postgresql.org/docs/current/functions-datetime.html

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.

make_date ( year int, month int, day int ) → date
Create date from year, month and day fields (negative years signify BC)
make_date(2024, 1, 31) → 2024-01-31

make_time ( hour int, min int, sec double precision ) → time
Create time from hour, minute and seconds fields
make_time(1, 45, 30.2) → 01:45:30.2

make_timestamp ( year int, month int, day int, hour int, min int, sec double precision ) → timestamp
Create timestamp from year, month, day, hour, minute and seconds fields (negative years signify BC)
make_timestamp(2024, 1, 31, 1, 45, 30.2) → 2024-01-31 01:45:30.2

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.

Generally LGTM.

@xiangjinwu please double check. 🥹

Copy link
Contributor

@xiangjinwu xiangjinwu left a comment

Choose a reason for hiding this comment

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

Rest LGTM!

Comment on lines 29 to 34
let adjusted_year = if year < 0 && !for_timestamp {
// For a year `-X`, The `Date` type is printed as `X+1 BC`, while `Timestamp` is printed as `-X`.
year + 1
} else {
year
};
Copy link
Contributor

@xiangjinwu xiangjinwu Jan 30, 2024

Choose a reason for hiding this comment

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

Did not quite get this part. Date / timestamp / timestamptz shall all follow the same rule:

  • In PG SQL, the year before 1 is -1. There is no year 0. It is printed as 1 BC.
  • In ISO 8601 (chrono crate), the year before 1 is 0. It can be printed as 1 BC (year_ce instead of year)

Practically it is okay to be incompatible with PG for BC years. But the logic here is both wrong and more complicated

In short, year + 1 as long as year < 0 when passing a PG SQL input to the chrono crate. This yields the correct value in memory during input. Whether showing X+1 BC or -X shall be adjusted on the output side. (same issue discovered multiple times recently #14832 (comment))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After discussion with xiangjin, we conduct +1 for both Date and Timestamp/Timestamptz. However, as our ToText for Date and Timestamp/Timestamptz are different, the output of make_date and make_timestamp/timestamptz are different.

SELECT make_timestamp(-5, 02, 29, 08, 15, 55.33);
----
-0004-02-29 08:15:55.330

SELECT make_date(-5, 2, 29);
----
0005-02-29 BC

We'll fix it later in another PR by modify the impl of ToText forTimestamp/Timestamptz.

Copy link
Contributor

@xiangjinwu xiangjinwu left a comment

Choose a reason for hiding this comment

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

LGTM

@KeXiangWang KeXiangWang enabled auto-merge January 31, 2024 05:05
auto-merge was automatically disabled January 31, 2024 06:35

Merge queue setting changed


query error Invalid parameter sec: invalid sec
query error Invalid parameter year, month, day: invalid date: -3-2-29
Copy link
Member

Choose a reason for hiding this comment

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

In postgres we have date field value out of range: -4-02-29. Note the -4 here. Are we behaving correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

> SELECT make_date(-4, 02, 29);
ERROR:  date field value out of range: -3-02-29
> SELECT make_timestamp(-4, 02, 29, 08, 15, 55.33);
ERROR:  date field value out of range: -4-02-29

Pg behaves differently on make_date and make_timestamp. Seems to be a misaligned design. So I think it's OK if we don't follow it strictly.

Copy link
Member

@BugenZhao BugenZhao left a comment

Choose a reason for hiding this comment

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

Rest LGTM

@KeXiangWang KeXiangWang added this pull request to the merge queue Feb 1, 2024
Merged via the queue into main with commit 82d1277 Feb 1, 2024
38 of 39 checks passed
@KeXiangWang KeXiangWang deleted the wkx/make_date branch February 1, 2024 05:46
StrikeW added a commit that referenced this pull request Feb 4, 2024
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.

4 participants