Skip to content

Commit 1b47275

Browse files
Cover other Date x TimeUnit cases
1 parent c7af4b1 commit 1b47275

File tree

1 file changed

+322
-0
lines changed

1 file changed

+322
-0
lines changed

arrow-cast/src/cast/mod.rs

+322
Original file line numberDiff line numberDiff line change
@@ -5228,8 +5228,238 @@ mod tests {
52285228
}};
52295229
}
52305230

5231+
#[test]
5232+
fn test_cast_date32_to_timestamp_with_timezone() {
5233+
let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5234+
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5235+
let array = Arc::new(a) as ArrayRef;
5236+
let b = cast(
5237+
&array,
5238+
&DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5239+
)
5240+
.unwrap();
5241+
let c = b.as_primitive::<TimestampSecondType>();
5242+
assert_eq!(1609459200, c.value(0));
5243+
assert_eq!(1640995200, c.value(1));
5244+
assert!(c.is_null(2));
5245+
5246+
let expected = vec![
5247+
Some("2021-01-01 05:45:00.000000"),
5248+
Some("2022-01-01 05:45:00.000000"),
5249+
None,
5250+
];
5251+
5252+
let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
5253+
let cast_options = CastOptions {
5254+
safe: true,
5255+
format_options: FormatOptions::default()
5256+
.with_timestamp_format(Some(ts_format))
5257+
.with_timestamp_tz_format(Some(ts_format)),
5258+
};
5259+
5260+
assert_cast_timestamp_to_string!(
5261+
c,
5262+
DataType::Utf8View,
5263+
StringViewArray,
5264+
cast_options,
5265+
expected
5266+
);
5267+
assert_cast_timestamp_to_string!(c, DataType::Utf8, StringArray, cast_options, expected);
5268+
assert_cast_timestamp_to_string!(
5269+
c,
5270+
DataType::LargeUtf8,
5271+
LargeStringArray,
5272+
cast_options,
5273+
expected
5274+
);
5275+
}
5276+
5277+
#[test]
5278+
fn test_cast_date32_to_timestamp_with_timezone_ms() {
5279+
let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5280+
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5281+
let array = Arc::new(a) as ArrayRef;
5282+
let b = cast(
5283+
&array,
5284+
&DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5285+
)
5286+
.unwrap();
5287+
let c = b.as_primitive::<TimestampMillisecondType>();
5288+
assert_eq!(1609459200000, c.value(0));
5289+
assert_eq!(1640995200000, c.value(1));
5290+
assert!(c.is_null(2));
5291+
5292+
let expected = vec![
5293+
Some("2021-01-01 05:45:00.000000"),
5294+
Some("2022-01-01 05:45:00.000000"),
5295+
None,
5296+
];
5297+
5298+
let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
5299+
let cast_options = CastOptions {
5300+
safe: true,
5301+
format_options: FormatOptions::default()
5302+
.with_timestamp_format(Some(ts_format))
5303+
.with_timestamp_tz_format(Some(ts_format)),
5304+
};
5305+
5306+
assert_cast_timestamp_to_string!(
5307+
c,
5308+
DataType::Utf8View,
5309+
StringViewArray,
5310+
cast_options,
5311+
expected
5312+
);
5313+
assert_cast_timestamp_to_string!(c, DataType::Utf8, StringArray, cast_options, expected);
5314+
assert_cast_timestamp_to_string!(
5315+
c,
5316+
DataType::LargeUtf8,
5317+
LargeStringArray,
5318+
cast_options,
5319+
expected
5320+
);
5321+
}
5322+
5323+
#[test]
5324+
fn test_cast_date32_to_timestamp_with_timezone_us() {
5325+
let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5326+
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5327+
let array = Arc::new(a) as ArrayRef;
5328+
let b = cast(
5329+
&array,
5330+
&DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5331+
)
5332+
.unwrap();
5333+
let c = b.as_primitive::<TimestampMicrosecondType>();
5334+
assert_eq!(1609459200000000, c.value(0));
5335+
assert_eq!(1640995200000000, c.value(1));
5336+
assert!(c.is_null(2));
5337+
5338+
let expected = vec![
5339+
Some("2021-01-01 05:45:00.000000000"),
5340+
Some("2022-01-01 05:45:00.000000000"),
5341+
None,
5342+
];
5343+
5344+
let ts_format = "%Y-%m-%d %H:%M:%S%.9f";
5345+
let cast_options = CastOptions {
5346+
safe: true,
5347+
format_options: FormatOptions::default()
5348+
.with_timestamp_format(Some(ts_format))
5349+
.with_timestamp_tz_format(Some(ts_format)),
5350+
};
5351+
5352+
assert_cast_timestamp_to_string!(
5353+
c,
5354+
DataType::Utf8View,
5355+
StringViewArray,
5356+
cast_options,
5357+
expected
5358+
);
5359+
assert_cast_timestamp_to_string!(c, DataType::Utf8, StringArray, cast_options, expected);
5360+
assert_cast_timestamp_to_string!(
5361+
c,
5362+
DataType::LargeUtf8,
5363+
LargeStringArray,
5364+
cast_options,
5365+
expected
5366+
);
5367+
}
5368+
5369+
#[test]
5370+
fn test_cast_date32_to_timestamp_with_timezone_ns() {
5371+
let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5372+
let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5373+
let array = Arc::new(a) as ArrayRef;
5374+
let b = cast(
5375+
&array,
5376+
&DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5377+
)
5378+
.unwrap();
5379+
let c = b.as_primitive::<TimestampNanosecondType>();
5380+
assert_eq!(1609459200000000000, c.value(0));
5381+
assert_eq!(1640995200000000000, c.value(1));
5382+
assert!(c.is_null(2));
5383+
5384+
let expected = vec![
5385+
Some("2021-01-01 05:45:00.000000000"),
5386+
Some("2022-01-01 05:45:00.000000000"),
5387+
None,
5388+
];
5389+
5390+
let ts_format = "%Y-%m-%d %H:%M:%S%.9f";
5391+
let cast_options = CastOptions {
5392+
safe: true,
5393+
format_options: FormatOptions::default()
5394+
.with_timestamp_format(Some(ts_format))
5395+
.with_timestamp_tz_format(Some(ts_format)),
5396+
};
5397+
5398+
assert_cast_timestamp_to_string!(
5399+
c,
5400+
DataType::Utf8View,
5401+
StringViewArray,
5402+
cast_options,
5403+
expected
5404+
);
5405+
assert_cast_timestamp_to_string!(c, DataType::Utf8, StringArray, cast_options, expected);
5406+
assert_cast_timestamp_to_string!(
5407+
c,
5408+
DataType::LargeUtf8,
5409+
LargeStringArray,
5410+
cast_options,
5411+
expected
5412+
);
5413+
}
5414+
52315415
#[test]
52325416
fn test_cast_date64_to_timestamp_with_timezone() {
5417+
let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5418+
let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5419+
let b = cast(
5420+
&array,
5421+
&DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5422+
)
5423+
.unwrap();
5424+
5425+
let c = b.as_primitive::<TimestampSecondType>();
5426+
assert_eq!(864000000, c.value(0));
5427+
assert_eq!(1545696000, c.value(1));
5428+
assert!(c.is_null(2));
5429+
5430+
let expected = vec![
5431+
Some("1997-05-19 05:45:00.000000"),
5432+
Some("2018-12-25 05:45:00.000000"),
5433+
None,
5434+
];
5435+
5436+
let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
5437+
let cast_options = CastOptions {
5438+
safe: true,
5439+
format_options: FormatOptions::default()
5440+
.with_timestamp_format(Some(ts_format))
5441+
.with_timestamp_tz_format(Some(ts_format)),
5442+
};
5443+
5444+
assert_cast_timestamp_to_string!(
5445+
c,
5446+
DataType::Utf8View,
5447+
StringViewArray,
5448+
cast_options,
5449+
expected
5450+
);
5451+
assert_cast_timestamp_to_string!(c, DataType::Utf8, StringArray, cast_options, expected);
5452+
assert_cast_timestamp_to_string!(
5453+
c,
5454+
DataType::LargeUtf8,
5455+
LargeStringArray,
5456+
cast_options,
5457+
expected
5458+
);
5459+
}
5460+
5461+
#[test]
5462+
fn test_cast_date64_to_timestamp_with_timezone_ms() {
52335463
let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
52345464
let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
52355465
let b = cast(
@@ -5243,6 +5473,52 @@ mod tests {
52435473
assert_eq!(1545696000001, c.value(1));
52445474
assert!(c.is_null(2));
52455475

5476+
let expected = vec![
5477+
Some("1997-05-19 05:45:00.005"),
5478+
Some("2018-12-25 05:45:00.001"),
5479+
None,
5480+
];
5481+
5482+
let ts_format = "%Y-%m-%d %H:%M:%S%.3f";
5483+
let cast_options = CastOptions {
5484+
safe: true,
5485+
format_options: FormatOptions::default()
5486+
.with_timestamp_format(Some(ts_format))
5487+
.with_timestamp_tz_format(Some(ts_format)),
5488+
};
5489+
5490+
assert_cast_timestamp_to_string!(
5491+
c,
5492+
DataType::Utf8View,
5493+
StringViewArray,
5494+
cast_options,
5495+
expected
5496+
);
5497+
assert_cast_timestamp_to_string!(c, DataType::Utf8, StringArray, cast_options, expected);
5498+
assert_cast_timestamp_to_string!(
5499+
c,
5500+
DataType::LargeUtf8,
5501+
LargeStringArray,
5502+
cast_options,
5503+
expected
5504+
);
5505+
}
5506+
5507+
#[test]
5508+
fn test_cast_date64_to_timestamp_with_timezone_us() {
5509+
let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5510+
let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5511+
let b = cast(
5512+
&array,
5513+
&DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5514+
)
5515+
.unwrap();
5516+
5517+
let c = b.as_primitive::<TimestampMicrosecondType>();
5518+
assert_eq!(864000000005000, c.value(0));
5519+
assert_eq!(1545696000001000, c.value(1));
5520+
assert!(c.is_null(2));
5521+
52465522
let expected = vec![
52475523
Some("1997-05-19 05:45:00.005000"),
52485524
Some("2018-12-25 05:45:00.001000"),
@@ -5274,6 +5550,52 @@ mod tests {
52745550
);
52755551
}
52765552

5553+
#[test]
5554+
fn test_cast_date64_to_timestamp_with_timezone_ns() {
5555+
let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5556+
let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5557+
let b = cast(
5558+
&array,
5559+
&DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5560+
)
5561+
.unwrap();
5562+
5563+
let c = b.as_primitive::<TimestampNanosecondType>();
5564+
assert_eq!(864000000005000000, c.value(0));
5565+
assert_eq!(1545696000001000000, c.value(1));
5566+
assert!(c.is_null(2));
5567+
5568+
let expected = vec![
5569+
Some("1997-05-19 05:45:00.005000000"),
5570+
Some("2018-12-25 05:45:00.001000000"),
5571+
None,
5572+
];
5573+
5574+
let ts_format = "%Y-%m-%d %H:%M:%S%.9f";
5575+
let cast_options = CastOptions {
5576+
safe: true,
5577+
format_options: FormatOptions::default()
5578+
.with_timestamp_format(Some(ts_format))
5579+
.with_timestamp_tz_format(Some(ts_format)),
5580+
};
5581+
5582+
assert_cast_timestamp_to_string!(
5583+
c,
5584+
DataType::Utf8View,
5585+
StringViewArray,
5586+
cast_options,
5587+
expected
5588+
);
5589+
assert_cast_timestamp_to_string!(c, DataType::Utf8, StringArray, cast_options, expected);
5590+
assert_cast_timestamp_to_string!(
5591+
c,
5592+
DataType::LargeUtf8,
5593+
LargeStringArray,
5594+
cast_options,
5595+
expected
5596+
);
5597+
}
5598+
52775599
#[test]
52785600
fn test_cast_timestamp_to_strings() {
52795601
// "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None

0 commit comments

Comments
 (0)