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(query): function about convert_timezone #16177 #16181

Open
wants to merge 71 commits into
base: main
Choose a base branch
from

Conversation

florann
Copy link

@florann florann commented Aug 4, 2024

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

Summary

The goal was to add a function that allow to convert a timestamp, with a timezone or not, to a specific timezone

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test

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

@florann florann changed the title Feature: function about convert_timezone #16177 feat(query): function about convert_timezone #16177 Aug 4, 2024
@github-actions github-actions bot added the pr-feature this PR introduces a new feature to the codebase label Aug 4, 2024
@b41sh
Copy link
Member

b41sh commented Aug 5, 2024

Hi @florann
Thanks for your contribution, please add the convert_timezone function at this file https://github.com/datafuselabs/databend/blob/main/src/query/functions/src/scalars/datetime.rs
and add some unit tests for it https://github.com/datafuselabs/databend/tree/main/src/query/functions/tests/it/scalars/datetime.rs

src/query/functions/src/scalars/datetime.rs Outdated Show resolved Hide resolved
src/query/functions/src/scalars/datetime.rs Outdated Show resolved Hide resolved
src/query/functions/src/scalars/datetime.rs Outdated Show resolved Hide resolved
@florann
Copy link
Author

florann commented Aug 8, 2024

Hello !

@b41sh, I correct all the points you previously mentioned.

:^)

I'll try to watch a bit in depth how unit test configuration is working otherwise I'll ask for your help

@florann
Copy link
Author

florann commented Aug 9, 2024

Okay, so I really don't get how I need to set up my unit test.
Even refering to the function to_timestamp isn't helping me

@@ -108,6 +109,9 @@ pub fn register(registry: &mut FunctionRegistry) {

// [date | timestamp] +/- number
register_timestamp_add_sub(registry);

// convert_timezone( target_timezone, date, src_timezone)
Copy link
Member

Choose a reason for hiding this comment

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

The arguments are in the wrong order. They should be src_timezone, target_timezone, source_timestamp

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes.

Need support

CONVERT_TIMEZONE( <source_tz> , <target_tz> , <source_timestamp_ntz> )

and

CONVERT_TIMEZONE( <target_tz> , <source_timestamp> )

eval_convert_timezone,
);

// 3 arguments function [target_timezone, src_timestamp, src_timezone]
Copy link
Member

Choose a reason for hiding this comment

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

ditto

"convert_timezone",
|_, _, _, _| FunctionDomain::MayThrow,
vectorize_with_builder_3_arg::<StringType, StringType, TimestampType, TimestampType>(
|src_tz, target_tz, src_timestamp, output, ctx| {
Copy link
Member

Choose a reason for hiding this comment

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

check validity here to handle NULL value

            if let Some(validity) = &ctx.validity {
                if !validity.get_bit(output.len()) {
                    output.push_default();
                    return;
                }
            }

@b41sh b41sh requested a review from TCeason August 9, 2024 13:45
@TCeason
Copy link
Collaborator

TCeason commented Aug 9, 2024



// Create dummy timezone
let utc_now: DateTime<Utc> = Utc::now();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think no need to create dummy timezone. You can use this https://docs.rs/chrono/latest/chrono/offset/trait.TimeZone.html#method.timestamp_opt to convert ts to source timezone.

Then convert the ts with src tz to target tz.

Copy link
Collaborator

Choose a reason for hiding this comment

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


// Convert the UTC time to the specified target timezone
let target_datetime: DateTime<Tz> = datetime.with_timezone(&t_tz);
let result_timestamp = target_datetime.timestamp();
Copy link
Collaborator

Choose a reason for hiding this comment

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

timestamp_micros()

test_convert_timezone(file);
}

fn test_convert_timezone(file: &mut impl Write) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Cool.

SELECT CONVERT_TIMEZONE(
'America/Los_Angeles',
'America/New_York',
to_timestamp('2024-01-01 14:00:00')
) AS conv;

SELECT CONVERT_TIMEZONE(
'Europe/Warsaw',
'UTC',
'2024-01-01 00:00:00'::TIMESTAMP
) AS conv;

SELECT CONVERT_TIMEZONE(
'America/Los_Angeles',
'2024-04-05 12:00:00 +02:00'::TIMESTAMP
) AS time_in_la;

SELECT
CURRENT_TIMESTAMP() AS now_in_la,
CONVERT_TIMEZONE('America/New_York', CURRENT_TIMESTAMP()) AS now_in_nyc,
CONVERT_TIMEZONE('Europe/Paris', CURRENT_TIMESTAMP()) AS now_in_paris,
CONVERT_TIMEZONE('Asia/Tokyo', CURRENT_TIMESTAMP()) AS now_in_tokyo;

Need to add these tests in logical test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-feature this PR introduces a new feature to the codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: function about convert_timezone
3 participants