Skip to content

feat(query): enhance datetime functions #18027

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

Merged
merged 2 commits into from
May 29, 2025
Merged

Conversation

TCeason
Copy link
Collaborator

@TCeason TCeason commented May 28, 2025

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

PR Summary: Enhancing Date and Time Functionality and Formatting Capabilities

This PR introduces several enhancements to improve the system's flexibility and compatibility in date and time processing and formatting:

  1. New Settings Introduced:
    • date_format_style: Allows users to select the date format style, with available values "MySQL" or "Oracle". The default is "MySQL".
    • week_start: Specifies the first day of the week, accepting values 0 or 1. The default is 0.
  2. trunc Function Support: The trunc(expr, datetime_interval_type) function has been added, allowing truncation of date or time expressions to a specified interval type.
  3. Extended date_trunc Function: The date_trunc function now supports WEEK as a datetime interval type, enabling truncation of dates by week.
  4. Enhanced Oracle Date Format Compatibility: When date_format_style is set to "Oracle", the system now supports and correctly parses Oracle-style date format expressions, mapping them to corresponding internal formatting symbols.
  5. use to_char as alias of to_string
  6. delete TZM map

Detailed Information:

1. New Settings (with their definitions):

("date_format_style", DefaultSettingValue {
    value: UserSettingValue::String("MySQL".to_owned()),
    desc: "Sets the date format style(Used by datetime functions). Available values include \"MySQL\",  \"Oracle\".",
    mode: SettingMode::Both,
    scope: SettingScope::Both,
    range: Some(SettingRange::String(vec!["Oracle".into(), "MySQL".into()])),
}),
                
("week_start", DefaultSettingValue {
    value: UserSettingValue::UInt64(1),
    desc: "Specifies the first day of the week.(Used by week-related date functions)",
    mode: SettingMode::Both,
    scope: SettingScope::Both,
    range: Some(SettingRange::Numeric(0..=1)),
}),

4. Oracle Date Format Mappings (when date_format_style is Oracle):

let mut mappings = vec![
      ("YYYY", "%Y"),
      ("YY", "%y"),
      ("MMMM", "%B"),
      ("MON", "%b"),
      ("MM", "%m"),
      ("DD", "%d"),
      ("DY", "%a"),
      ("HH24", "%H"),
      ("HH12", "%I"),
      ("AM", "%p"),
      ("PM", "%p"),
      ("MI", "%M"),
      ("SS", "%S"),
      ("FF", "%f"),
      ("UUUU", "%G"),
      ("TZHTZM", "%z"),
      ("TZH:TZM", "%z"),
      ("TZH", "%:::z"),
  ];

in sf:

select TO_CHAR('2024-04-05'::DATE, 'MMM'), TO_CHAR('2024-04-05'::DATE, 'MMMON') , TO_CHAR('2024-04-05'::DATE, 'MMMM')
----
04m | April | April

In Databend

settings (date_format_style='Oracle') select to_string('2024-04-05'::DATE, 'MMM') , to_string('2024-04-05'::DATE, 'MMMON') , to_string('2024-04-05'::DATE, 'MMMM')
----
04M 04MON April


settings (date_format_style = 'Oracle') select to_string('2024-04-05'::DATE, 'MM MON')
----
04 Apr

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

This change is Reviewable

@github-actions github-actions bot added the pr-feature this PR introduces a new feature to the codebase label May 28, 2025
@TCeason TCeason marked this pull request as draft May 28, 2025 09:57
@TCeason TCeason force-pushed the trunc branch 2 times, most recently from 1ea44e9 to 0aad80b Compare May 28, 2025 10:53
@TCeason TCeason requested review from sundy-li and zhang2014 May 28, 2025 14:15
@TCeason TCeason marked this pull request as ready for review May 28, 2025 15:34
@TCeason TCeason force-pushed the trunc branch 2 times, most recently from 0f3d331 to b2d63cb Compare May 29, 2025 05:01
@wubx
Copy link
Member

wubx commented May 29, 2025

#v1.2.743-nightly-70290f1a01

 select to_date('20250124', 'yyyymmdd') dt, to_char(dt,'yyyymmdd'), date_trunc('year',dt), trunc(dt,'year'),last_day(dt);

select to_date('20250124', 'yyyymmdd') dt, to_char(dt,'yyyymmdd'), date_trunc('year',dt), trunc(dt,'year'),last_day(dt)

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│     dt     │ to_char(dt, 'yyyymmdd') │ DATE_TRUNC(YEAR, dt) │ DATE_TRUNC(YEAR, dt) │ LAST_DAY(dt, MONTH) │
│    Date    │          String         │         Date         │         Date         │         Date        │
├────────────┼─────────────────────────┼──────────────────────┼──────────────────────┼─────────────────────┤
│ 2025-01-24 │ 20250124                │ 2025-01-01           │ 2025-01-01           │ 2025-01-31          │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘
1 row read in 0.121 sec. Processed 1 row, 1 B (8.26 rows/s, 8 B/s)

LGTM

@sundy-li sundy-li requested a review from forsaken628 May 29, 2025 06:29
@wubx wubx added the ci-cloud Build docker image for cloud test label May 29, 2025
Copy link
Contributor

Docker Image for PR

  • tag: pr-18027-ea94694-1748505505

note: this image tag is only available for internal use.

@BohuTANG BohuTANG merged commit af7ac4c into databendlabs:main May 29, 2025
224 of 236 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci-cloud Build docker image for cloud test pr-feature this PR introduces a new feature to the codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Enhance time functions
5 participants