use std::collections::HashMap;
use uni_common::{Value, unival};
use uni_query::query::expr_eval::eval_scalar_function;
fn make_map(entries: Vec<(&str, Value)>) -> Value {
Value::Map(
entries
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect::<HashMap<_, _>>(),
)
}
#[test]
fn test_date_function() {
let res = eval_scalar_function("DATE", &[unival!("2023-01-15")], None).unwrap();
assert_eq!(res.to_string(), "2023-01-15");
let res = eval_scalar_function("DATE", &[unival!("2023-01-15 10:30:00")], None).unwrap();
assert_eq!(res.to_string(), "2023-01-15");
let res = eval_scalar_function("DATE", &[], None).unwrap();
assert!(res.to_string().len() == 10);
}
#[test]
fn test_time_function() {
let res = eval_scalar_function("TIME", &[unival!("10:30:00")], None).unwrap();
assert_eq!(res.to_string(), "10:30Z");
let res = eval_scalar_function("TIME", &[unival!("10:30:45")], None).unwrap();
assert_eq!(res.to_string(), "10:30:45Z");
let res = eval_scalar_function("TIME", &[unival!("10:30:45+01:00")], None).unwrap();
assert_eq!(res.to_string(), "10:30:45+01:00");
}
#[test]
fn test_datetime_function() {
let res = eval_scalar_function("DATETIME", &[unival!("2023-01-15T10:30:00Z")], None).unwrap();
assert_eq!(res.to_string(), "2023-01-15T10:30Z");
let res =
eval_scalar_function("DATETIME", &[unival!("2023-01-15T10:30:00+05:00")], None).unwrap();
assert_eq!(res.to_string(), "2023-01-15T10:30+05:00");
}
#[test]
fn test_extract_functions() {
let dt = unival!("2023-01-15 10:30:45");
assert_eq!(
eval_scalar_function("YEAR", std::slice::from_ref(&dt), None)
.unwrap()
.as_i64()
.unwrap(),
2023
);
assert_eq!(
eval_scalar_function("MONTH", std::slice::from_ref(&dt), None)
.unwrap()
.as_i64()
.unwrap(),
1
);
assert_eq!(
eval_scalar_function("DAY", std::slice::from_ref(&dt), None)
.unwrap()
.as_i64()
.unwrap(),
15
);
assert_eq!(
eval_scalar_function("HOUR", std::slice::from_ref(&dt), None)
.unwrap()
.as_i64()
.unwrap(),
10
);
assert_eq!(
eval_scalar_function("MINUTE", std::slice::from_ref(&dt), None)
.unwrap()
.as_i64()
.unwrap(),
30
);
assert_eq!(
eval_scalar_function("SECOND", std::slice::from_ref(&dt), None)
.unwrap()
.as_i64()
.unwrap(),
45
);
}
#[test]
fn test_localdatetime_function() {
let res = eval_scalar_function("LOCALDATETIME", &[], None).unwrap();
let s = res.to_string();
assert!(s.contains("T"), "Expected format with T separator");
assert!(s.len() >= 16, "Expected at least YYYY-MM-DDTHH:MM");
let res =
eval_scalar_function("LOCALDATETIME", &[unival!("2023-01-15T10:30:00")], None).unwrap();
assert_eq!(res.to_string(), "2023-01-15T10:30");
}
#[test]
fn test_localtime_function() {
let res = eval_scalar_function("LOCALTIME", &[], None).unwrap();
let s = res.to_string();
assert!(s.contains(":"), "Expected time format with colons");
assert!(s.len() >= 5, "Expected at least HH:MM");
let res = eval_scalar_function("LOCALTIME", &[unival!("10:30:00")], None).unwrap();
assert_eq!(res.to_string(), "10:30");
}
#[test]
fn test_date_all_string_formats() {
let cases = [
("2015-07-21", "2015-07-21"),
("20150721", "2015-07-21"),
("2015-07", "2015-07-01"),
("201507", "2015-07-01"),
("2015-W30-2", "2015-07-21"),
("2015W302", "2015-07-21"),
("2015-W30", "2015-07-20"),
("2015W30", "2015-07-20"),
("2015-202", "2015-07-21"),
("2015202", "2015-07-21"),
("2015", "2015-01-01"),
];
for (input, expected) in &cases {
let res = eval_scalar_function("DATE", &[unival!(*input)], None)
.unwrap_or_else(|e| panic!("DATE({:?}) failed: {}", input, e));
assert_eq!(
res.to_string(),
*expected,
"DATE({:?}) => {:?}, expected {:?}",
input,
res.to_string(),
expected
);
}
}
#[test]
fn test_localtime_all_string_formats() {
let cases = [
("21:40:32.142", "21:40:32.142"),
("214032.142", "21:40:32.142"),
("21:40:32", "21:40:32"),
("214032", "21:40:32"),
("21:40", "21:40"),
("2140", "21:40"),
("21", "21:00"),
];
for (input, expected) in &cases {
let res = eval_scalar_function("LOCALTIME", &[unival!(*input)], None)
.unwrap_or_else(|e| panic!("LOCALTIME({:?}) failed: {}", input, e));
assert_eq!(
res.to_string(),
*expected,
"LOCALTIME({:?}) => {:?}, expected {:?}",
input,
res.to_string(),
expected
);
}
}
#[test]
fn test_time_all_string_formats() {
let cases = [
("21:40:32.142+0100", "21:40:32.142+01:00"),
("214032.142Z", "21:40:32.142Z"),
("21:40:32+01:00", "21:40:32+01:00"),
("214032-0100", "21:40:32-01:00"),
("21:40-01:30", "21:40-01:30"),
("2140-00:00", "21:40Z"),
("2140-02", "21:40-02:00"),
("22+18:00", "22:00+18:00"),
];
for (input, expected) in &cases {
let res = eval_scalar_function("TIME", &[unival!(*input)], None)
.unwrap_or_else(|e| panic!("TIME({:?}) failed: {}", input, e));
assert_eq!(
res.to_string(),
*expected,
"TIME({:?}) => {:?}, expected {:?}",
input,
res.to_string(),
expected
);
}
}
#[test]
fn test_localdatetime_all_string_formats() {
let cases = [
("2015-07-21T21:40:32.142", "2015-07-21T21:40:32.142"),
("2015-W30-2T214032.142", "2015-07-21T21:40:32.142"),
("2015-202T21:40:32", "2015-07-21T21:40:32"),
("2015T214032", "2015-01-01T21:40:32"),
("20150721T21:40", "2015-07-21T21:40"),
("2015-W30T2140", "2015-07-20T21:40"),
("2015202T21", "2015-07-21T21:00"),
];
for (input, expected) in &cases {
let res = eval_scalar_function("LOCALDATETIME", &[unival!(*input)], None)
.unwrap_or_else(|e| panic!("LOCALDATETIME({:?}) failed: {}", input, e));
assert_eq!(
res.to_string(),
*expected,
"LOCALDATETIME({:?}) => {:?}, expected {:?}",
input,
res.to_string(),
expected
);
}
}
#[test]
fn test_datetime_all_string_formats() {
let cases = [
(
"2015-07-21T21:40:32.142+0100",
"2015-07-21T21:40:32.142+01:00",
),
("2015-W30-2T214032.142Z", "2015-07-21T21:40:32.142Z"),
("2015-202T21:40:32+01:00", "2015-07-21T21:40:32+01:00"),
("2015T214032-0100", "2015-01-01T21:40:32-01:00"),
("20150721T21:40-01:30", "2015-07-21T21:40-01:30"),
("2015-W30T2140-00:00", "2015-07-20T21:40Z"),
("2015-W30T2140-02", "2015-07-20T21:40-02:00"),
("2015202T21+18:00", "2015-07-21T21:00+18:00"),
];
for (input, expected) in &cases {
let res = eval_scalar_function("DATETIME", &[unival!(*input)], None)
.unwrap_or_else(|e| panic!("DATETIME({:?}) failed: {}", input, e));
assert_eq!(
res.to_string(),
*expected,
"DATETIME({:?}) => {:?}, expected {:?}",
input,
res.to_string(),
expected
);
}
}
#[test]
fn test_datetime_named_timezone_formats() {
let cases = [
(
"2015-07-21T21:40:32.142+02:00[Europe/Stockholm]",
"2015-07-21T21:40:32.142+02:00[Europe/Stockholm]",
),
(
"2015-07-21T21:40:32.142+0845[Australia/Eucla]",
"2015-07-21T21:40:32.142+08:45[Australia/Eucla]",
),
(
"2015-07-21T21:40:32.142-04[America/New_York]",
"2015-07-21T21:40:32.142-04:00[America/New_York]",
),
(
"2015-07-21T21:40:32.142[Europe/London]",
"2015-07-21T21:40:32.142+01:00[Europe/London]",
),
(
"1818-07-21T21:40:32.142[Europe/Stockholm]",
"1818-07-21T21:40:32.142+00:53:28[Europe/Stockholm]",
),
];
for (input, expected) in &cases {
let res = eval_scalar_function("DATETIME", &[unival!(*input)], None)
.unwrap_or_else(|e| panic!("DATETIME({:?}) failed: {}", input, e));
assert_eq!(
res.to_string(),
*expected,
"DATETIME({:?}) => {:?}, expected {:?}",
input,
res.to_string(),
expected
);
}
}
#[test]
fn test_duration_all_string_formats() {
let cases = [
("P14DT16H12M", "P14DT16H12M"),
("P5M1.5D", "P5M1DT12H"),
("P0.75M", "P22DT19H51M49.5S"),
("PT0.75M", "PT45S"),
("P2.5W", "P17DT12H"),
("P12Y5M14DT16H12M70S", "P12Y5M14DT16H13M10S"),
("P2012-02-02T14:37:21.545", "P2012Y2M2DT14H37M21.545S"),
];
for (input, expected) in &cases {
let res = eval_scalar_function("DURATION", &[unival!(*input)], None)
.unwrap_or_else(|e| panic!("DURATION({:?}) failed: {}", input, e));
assert_eq!(
res.to_string(),
*expected,
"DURATION({:?}) => {:?}, expected {:?}",
input,
res.to_string(),
expected
);
}
}
#[test]
fn test_project_date_from_date() {
let source = eval_scalar_function(
"DATE",
&[unival!({"year": 1984, "month": 11, "day": 11})],
None,
)
.unwrap();
let res = eval_scalar_function("DATE", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "1984-11-11");
let res =
eval_scalar_function("DATE", &[make_map(vec![("date", source.clone())])], None).unwrap();
assert_eq!(res.to_string(), "1984-11-11");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("year", Value::from(28)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "0028-11-11");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("day", Value::from(28)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-11-28");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("week", Value::from(1)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-01-08");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("ordinalDay", Value::from(28)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-01-28");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("quarter", Value::from(3)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-08-11");
}
#[test]
fn test_project_date_from_localdatetime() {
let source = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "month": 11, "day": 11, "hour": 12, "minute": 31, "second": 14, "nanosecond": 645876123})],
None,
)
.unwrap();
let res = eval_scalar_function("DATE", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "1984-11-11");
let res =
eval_scalar_function("DATE", &[make_map(vec![("date", source.clone())])], None).unwrap();
assert_eq!(res.to_string(), "1984-11-11");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("year", Value::from(28)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "0028-11-11");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("day", Value::from(28)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-11-28");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("week", Value::from(1)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-01-08");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("ordinalDay", Value::from(28)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-01-28");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("quarter", Value::from(3)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-08-11");
}
#[test]
fn test_project_date_from_datetime() {
let source = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 11, "day": 11, "hour": 12, "timezone": "+01:00"})],
None,
)
.unwrap();
let res = eval_scalar_function("DATE", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "1984-11-11");
let res =
eval_scalar_function("DATE", &[make_map(vec![("date", source.clone())])], None).unwrap();
assert_eq!(res.to_string(), "1984-11-11");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("year", Value::from(28)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "0028-11-11");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("day", Value::from(28)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-11-28");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("week", Value::from(1)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-01-08");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("ordinalDay", Value::from(28)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-01-28");
let res = eval_scalar_function(
"DATE",
&[make_map(vec![
("date", source.clone()),
("quarter", Value::from(3)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-08-11");
}
#[test]
fn test_project_localtime_from_localtime() {
let source = eval_scalar_function(
"LOCALTIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "nanosecond": 645876123})],
None,
)
.unwrap();
let res = eval_scalar_function("LOCALTIME", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "12:31:14.645876123");
let res = eval_scalar_function(
"LOCALTIME",
&[make_map(vec![("time", source.clone())])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:14.645876123");
let res = eval_scalar_function(
"LOCALTIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:42.645876123");
}
#[test]
fn test_project_localtime_from_time() {
let source = eval_scalar_function(
"TIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "microsecond": 645876, "timezone": "+01:00"})],
None,
)
.unwrap();
let res = eval_scalar_function("LOCALTIME", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "12:31:14.645876");
let res = eval_scalar_function(
"LOCALTIME",
&[make_map(vec![("time", source.clone())])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:14.645876");
let res = eval_scalar_function(
"LOCALTIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:42.645876");
}
#[test]
fn test_project_localtime_from_localdatetime() {
let source = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let res = eval_scalar_function("LOCALTIME", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "12:31:14.645");
let res = eval_scalar_function(
"LOCALTIME",
&[make_map(vec![("time", source.clone())])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:14.645");
let res = eval_scalar_function(
"LOCALTIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:42.645");
}
#[test]
fn test_project_localtime_from_datetime() {
let source = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "+01:00"})],
None,
)
.unwrap();
let res = eval_scalar_function("LOCALTIME", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "12:00");
let res = eval_scalar_function(
"LOCALTIME",
&[make_map(vec![("time", source.clone())])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:00");
let res = eval_scalar_function(
"LOCALTIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:00:42");
}
#[test]
fn test_project_time_from_localtime() {
let source = eval_scalar_function(
"LOCALTIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "nanosecond": 645876123})],
None,
)
.unwrap();
let res = eval_scalar_function("TIME", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "12:31:14.645876123Z");
let res =
eval_scalar_function("TIME", &[make_map(vec![("time", source.clone())])], None).unwrap();
assert_eq!(res.to_string(), "12:31:14.645876123Z");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:14.645876123+05:00");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:42.645876123Z");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:42.645876123+05:00");
}
#[test]
fn test_project_time_from_time() {
let source = eval_scalar_function(
"TIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "microsecond": 645876, "timezone": "+01:00"})],
None,
)
.unwrap();
let res = eval_scalar_function("TIME", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "12:31:14.645876+01:00");
let res =
eval_scalar_function("TIME", &[make_map(vec![("time", source.clone())])], None).unwrap();
assert_eq!(res.to_string(), "12:31:14.645876+01:00");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "16:31:14.645876+05:00");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:42.645876+01:00");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "16:31:42.645876+05:00");
}
#[test]
fn test_project_time_from_localdatetime() {
let source = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let res = eval_scalar_function("TIME", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "12:31:14.645Z");
let res =
eval_scalar_function("TIME", &[make_map(vec![("time", source.clone())])], None).unwrap();
assert_eq!(res.to_string(), "12:31:14.645Z");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:14.645+05:00");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:42.645Z");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:31:42.645+05:00");
}
#[test]
fn test_project_time_from_datetime() {
let source = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "Europe/Stockholm"})],
None,
)
.unwrap();
let res = eval_scalar_function("TIME", std::slice::from_ref(&source), None).unwrap();
assert_eq!(res.to_string(), "12:00+01:00");
let res =
eval_scalar_function("TIME", &[make_map(vec![("time", source.clone())])], None).unwrap();
assert_eq!(res.to_string(), "12:00+01:00");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "16:00+05:00");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "12:00:42+01:00");
let res = eval_scalar_function(
"TIME",
&[make_map(vec![
("time", source.clone()),
("second", Value::from(42)),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "16:00:42+05:00");
}
#[test]
fn test_project_localdatetime_from_date() {
let date_source = eval_scalar_function(
"DATE",
&[unival!({"year": 1984, "month": 10, "day": 11})],
None,
)
.unwrap();
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("date", date_source.clone()),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T10:10:10");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("date", date_source.clone()),
("day", Value::from(28)),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-28T10:10:10");
let ldt_source = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("date", ldt_source.clone()),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-07T10:10:10");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("date", ldt_source.clone()),
("day", Value::from(28)),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-28T10:10:10");
let dt_source = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "+01:00"})],
None,
)
.unwrap();
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("date", dt_source.clone()),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T10:10:10");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("date", dt_source.clone()),
("day", Value::from(28)),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-28T10:10:10");
}
#[test]
fn test_project_localdatetime_from_time() {
let lt_source = eval_scalar_function(
"LOCALTIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "nanosecond": 645876123})],
None,
)
.unwrap();
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", lt_source.clone()),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:14.645876123");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", lt_source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:42.645876123");
let t_source = eval_scalar_function(
"TIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "microsecond": 645876, "timezone": "+01:00"})],
None,
)
.unwrap();
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", t_source.clone()),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:14.645876");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", t_source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:42.645876");
let ldt_source = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", ldt_source.clone()),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:14.645");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", ldt_source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:42.645");
let dt_source = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "+01:00"})],
None,
)
.unwrap();
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", dt_source.clone()),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:00");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", dt_source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:00:42");
}
#[test]
#[allow(clippy::type_complexity)]
fn test_project_localdatetime_date_and_time() {
let date1 = eval_scalar_function(
"DATE",
&[unival!({"year": 1984, "month": 10, "day": 11})],
None,
)
.unwrap();
let date_ldt = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let date_dt = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "+01:00"})],
None,
)
.unwrap();
let time_lt = eval_scalar_function(
"LOCALTIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "nanosecond": 645876123})],
None,
)
.unwrap();
let time_t = eval_scalar_function(
"TIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "microsecond": 645876, "timezone": "+01:00"})],
None,
)
.unwrap();
let time_ldt = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let time_dt = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "+01:00"})],
None,
)
.unwrap();
let cases_date1: Vec<(&Value, &str, Option<(i64, i64)>, &str)> = vec![
(&time_lt, "localtime", None, "1984-10-11T12:31:14.645876123"),
(
&time_lt,
"localtime+overrides",
Some((28, 42)),
"1984-10-28T12:31:42.645876123",
),
(&time_t, "time", None, "1984-10-11T12:31:14.645876"),
(
&time_t,
"time+overrides",
Some((28, 42)),
"1984-10-28T12:31:42.645876",
),
(&time_ldt, "localdatetime", None, "1984-10-11T12:31:14.645"),
(
&time_ldt,
"localdatetime+overrides",
Some((28, 42)),
"1984-10-28T12:31:42.645",
),
(&time_dt, "datetime", None, "1984-10-11T12:00"),
(
&time_dt,
"datetime+overrides",
Some((28, 42)),
"1984-10-28T12:00:42",
),
];
for (time_src, label, overrides, expected) in &cases_date1 {
let mut entries: Vec<(&str, Value)> =
vec![("date", date1.clone()), ("time", (*time_src).clone())];
if let Some((day, sec)) = overrides {
entries.push(("day", Value::from(*day)));
entries.push(("second", Value::from(*sec)));
}
let res = eval_scalar_function("LOCALDATETIME", &[make_map(entries)], None)
.unwrap_or_else(|e| panic!("localdatetime(date1+{}) failed: {}", label, e));
assert_eq!(res.to_string(), *expected, "date1+{}", label);
}
let cases_ldt: Vec<(&Value, &str, Option<(i64, i64)>, &str)> = vec![
(&time_lt, "localtime", None, "1984-03-07T12:31:14.645876123"),
(
&time_lt,
"localtime+overrides",
Some((28, 42)),
"1984-03-28T12:31:42.645876123",
),
(&time_t, "time", None, "1984-03-07T12:31:14.645876"),
(
&time_t,
"time+overrides",
Some((28, 42)),
"1984-03-28T12:31:42.645876",
),
(&time_ldt, "localdatetime", None, "1984-03-07T12:31:14.645"),
(
&time_ldt,
"localdatetime+overrides",
Some((28, 42)),
"1984-03-28T12:31:42.645",
),
(&time_dt, "datetime", None, "1984-03-07T12:00"),
(
&time_dt,
"datetime+overrides",
Some((28, 42)),
"1984-03-28T12:00:42",
),
];
for (time_src, label, overrides, expected) in &cases_ldt {
let mut entries: Vec<(&str, Value)> =
vec![("date", date_ldt.clone()), ("time", (*time_src).clone())];
if let Some((day, sec)) = overrides {
entries.push(("day", Value::from(*day)));
entries.push(("second", Value::from(*sec)));
}
let res = eval_scalar_function("LOCALDATETIME", &[make_map(entries)], None)
.unwrap_or_else(|e| panic!("localdatetime(ldt+{}) failed: {}", label, e));
assert_eq!(res.to_string(), *expected, "ldt+{}", label);
}
let cases_dt: Vec<(&Value, &str, Option<(i64, i64)>, &str)> = vec![
(&time_lt, "localtime", None, "1984-10-11T12:31:14.645876123"),
(
&time_lt,
"localtime+overrides",
Some((28, 42)),
"1984-10-28T12:31:42.645876123",
),
(&time_t, "time", None, "1984-10-11T12:31:14.645876"),
(
&time_t,
"time+overrides",
Some((28, 42)),
"1984-10-28T12:31:42.645876",
),
(&time_ldt, "localdatetime", None, "1984-10-11T12:31:14.645"),
(
&time_ldt,
"localdatetime+overrides",
Some((28, 42)),
"1984-10-28T12:31:42.645",
),
(&time_dt, "datetime", None, "1984-10-11T12:00"),
(
&time_dt,
"datetime+overrides",
Some((28, 42)),
"1984-10-28T12:00:42",
),
];
for (time_src, label, overrides, expected) in &cases_dt {
let mut entries: Vec<(&str, Value)> =
vec![("date", date_dt.clone()), ("time", (*time_src).clone())];
if let Some((day, sec)) = overrides {
entries.push(("day", Value::from(*day)));
entries.push(("second", Value::from(*sec)));
}
let res = eval_scalar_function("LOCALDATETIME", &[make_map(entries)], None)
.unwrap_or_else(|e| panic!("localdatetime(dt+{}) failed: {}", label, e));
assert_eq!(res.to_string(), *expected, "dt+{}", label);
}
}
#[test]
fn test_project_localdatetime_from_datetime() {
let ldt_source = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let res =
eval_scalar_function("LOCALDATETIME", std::slice::from_ref(&ldt_source), None).unwrap();
assert_eq!(res.to_string(), "1984-03-07T12:31:14.645");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![("datetime", ldt_source.clone())])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-07T12:31:14.645");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("datetime", ldt_source.clone()),
("day", Value::from(28)),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-28T12:31:42.645");
let dt_source = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "+01:00"})],
None,
)
.unwrap();
let res =
eval_scalar_function("LOCALDATETIME", std::slice::from_ref(&dt_source), None).unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:00");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![("datetime", dt_source.clone())])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:00");
let res = eval_scalar_function(
"LOCALDATETIME",
&[make_map(vec![
("datetime", dt_source.clone()),
("day", Value::from(28)),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-28T12:00:42");
}
#[test]
fn test_project_datetime_from_date() {
let date_source = eval_scalar_function(
"DATE",
&[unival!({"year": 1984, "month": 10, "day": 11})],
None,
)
.unwrap();
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", date_source.clone()),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T10:10:10Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", date_source.clone()),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T10:10:10+05:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", date_source.clone()),
("day", Value::from(28)),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-28T10:10:10Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", date_source.clone()),
("day", Value::from(28)),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
("timezone", Value::from("Pacific/Honolulu")),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-10-28T10:10:10-10:00[Pacific/Honolulu]"
);
let ldt_source = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", ldt_source.clone()),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-07T10:10:10Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", ldt_source.clone()),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-07T10:10:10+05:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", ldt_source.clone()),
("day", Value::from(28)),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-28T10:10:10Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", ldt_source.clone()),
("day", Value::from(28)),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
("timezone", Value::from("Pacific/Honolulu")),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-03-28T10:10:10-10:00[Pacific/Honolulu]"
);
let dt_source = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "+01:00"})],
None,
)
.unwrap();
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", dt_source.clone()),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T10:10:10Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", dt_source.clone()),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T10:10:10+05:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", dt_source.clone()),
("day", Value::from(28)),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-28T10:10:10Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("date", dt_source.clone()),
("day", Value::from(28)),
("hour", Value::from(10)),
("minute", Value::from(10)),
("second", Value::from(10)),
("timezone", Value::from("Pacific/Honolulu")),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-10-28T10:10:10-10:00[Pacific/Honolulu]"
);
}
#[test]
fn test_project_datetime_from_time() {
let lt_source = eval_scalar_function(
"LOCALTIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "nanosecond": 645876123})],
None,
)
.unwrap();
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", lt_source.clone()),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:14.645876123Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", lt_source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:14.645876123+05:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", lt_source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:42.645876123Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", lt_source.clone()),
("second", Value::from(42)),
("timezone", Value::from("Pacific/Honolulu")),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-10-11T12:31:42.645876123-10:00[Pacific/Honolulu]"
);
let t_source = eval_scalar_function(
"TIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "microsecond": 645876, "timezone": "+01:00"})],
None,
)
.unwrap();
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", t_source.clone()),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:14.645876+01:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", t_source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T16:31:14.645876+05:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", t_source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:42.645876+01:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", t_source.clone()),
("second", Value::from(42)),
("timezone", Value::from("Pacific/Honolulu")),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-10-11T01:31:42.645876-10:00[Pacific/Honolulu]"
);
let ldt_source = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", ldt_source.clone()),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:14.645Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", ldt_source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:14.645+05:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", ldt_source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:31:42.645Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", ldt_source.clone()),
("second", Value::from(42)),
("timezone", Value::from("Pacific/Honolulu")),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-10-11T12:31:42.645-10:00[Pacific/Honolulu]"
);
let dt_source = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "Europe/Stockholm"})],
None,
)
.unwrap();
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", dt_source.clone()),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:00+01:00[Europe/Stockholm]");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", dt_source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T16:00+05:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", dt_source.clone()),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-10-11T12:00:42+01:00[Europe/Stockholm]"
);
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("year", Value::from(1984)),
("month", Value::from(10)),
("day", Value::from(11)),
("time", dt_source.clone()),
("second", Value::from(42)),
("timezone", Value::from("Pacific/Honolulu")),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-10-11T01:00:42-10:00[Pacific/Honolulu]"
);
}
#[test]
fn test_project_datetime_date_and_time() {
let date1 = eval_scalar_function(
"DATE",
&[unival!({"year": 1984, "month": 10, "day": 11})],
None,
)
.unwrap();
let date_ldt = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let date_dt = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "+01:00"})],
None,
)
.unwrap();
let time_lt = eval_scalar_function(
"LOCALTIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "nanosecond": 645876123})],
None,
)
.unwrap();
let time_t = eval_scalar_function(
"TIME",
&[unival!({"hour": 12, "minute": 31, "second": 14, "microsecond": 645876, "timezone": "+01:00"})],
None,
)
.unwrap();
let time_ldt = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let time_dt = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "Europe/Stockholm"})],
None,
)
.unwrap();
struct Case {
date: Value,
time: Value,
tz: Option<&'static str>,
day_sec: Option<(i64, i64)>,
expected: &'static str,
label: &'static str,
}
let cases = vec![
Case {
date: date1.clone(),
time: time_lt.clone(),
tz: None,
day_sec: None,
expected: "1984-10-11T12:31:14.645876123Z",
label: "d1+lt",
},
Case {
date: date1.clone(),
time: time_lt.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-10-11T12:31:14.645876123+05:00",
label: "d1+lt+tz",
},
Case {
date: date1.clone(),
time: time_lt.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645876123Z",
label: "d1+lt+ds",
},
Case {
date: date1.clone(),
time: time_lt.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645876123-10:00[Pacific/Honolulu]",
label: "d1+lt+ds+tz",
},
Case {
date: date1.clone(),
time: time_t.clone(),
tz: None,
day_sec: None,
expected: "1984-10-11T12:31:14.645876+01:00",
label: "d1+t",
},
Case {
date: date1.clone(),
time: time_t.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-10-11T16:31:14.645876+05:00",
label: "d1+t+tz",
},
Case {
date: date1.clone(),
time: time_t.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645876+01:00",
label: "d1+t+ds",
},
Case {
date: date1.clone(),
time: time_t.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-10-28T01:31:42.645876-10:00[Pacific/Honolulu]",
label: "d1+t+ds+tz",
},
Case {
date: date1.clone(),
time: time_ldt.clone(),
tz: None,
day_sec: None,
expected: "1984-10-11T12:31:14.645Z",
label: "d1+ldt",
},
Case {
date: date1.clone(),
time: time_ldt.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-10-11T12:31:14.645+05:00",
label: "d1+ldt+tz",
},
Case {
date: date1.clone(),
time: time_ldt.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645Z",
label: "d1+ldt+ds",
},
Case {
date: date1.clone(),
time: time_ldt.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645-10:00[Pacific/Honolulu]",
label: "d1+ldt+ds+tz",
},
Case {
date: date1.clone(),
time: time_dt.clone(),
tz: None,
day_sec: None,
expected: "1984-10-11T12:00+01:00[Europe/Stockholm]",
label: "d1+dt",
},
Case {
date: date1.clone(),
time: time_dt.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-10-11T16:00+05:00",
label: "d1+dt+tz",
},
Case {
date: date1.clone(),
time: time_dt.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-10-28T12:00:42+01:00[Europe/Stockholm]",
label: "d1+dt+ds",
},
Case {
date: date1.clone(),
time: time_dt.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-10-28T01:00:42-10:00[Pacific/Honolulu]",
label: "d1+dt+ds+tz",
},
Case {
date: date_ldt.clone(),
time: time_lt.clone(),
tz: None,
day_sec: None,
expected: "1984-03-07T12:31:14.645876123Z",
label: "ldt+lt",
},
Case {
date: date_ldt.clone(),
time: time_lt.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-03-07T12:31:14.645876123+05:00",
label: "ldt+lt+tz",
},
Case {
date: date_ldt.clone(),
time: time_lt.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-03-28T12:31:42.645876123Z",
label: "ldt+lt+ds",
},
Case {
date: date_ldt.clone(),
time: time_lt.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-03-28T12:31:42.645876123-10:00[Pacific/Honolulu]",
label: "ldt+lt+ds+tz",
},
Case {
date: date_ldt.clone(),
time: time_t.clone(),
tz: None,
day_sec: None,
expected: "1984-03-07T12:31:14.645876+01:00",
label: "ldt+t",
},
Case {
date: date_ldt.clone(),
time: time_t.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-03-07T16:31:14.645876+05:00",
label: "ldt+t+tz",
},
Case {
date: date_ldt.clone(),
time: time_t.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-03-28T12:31:42.645876+01:00",
label: "ldt+t+ds",
},
Case {
date: date_ldt.clone(),
time: time_t.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-03-28T01:31:42.645876-10:00[Pacific/Honolulu]",
label: "ldt+t+ds+tz",
},
Case {
date: date_ldt.clone(),
time: time_ldt.clone(),
tz: None,
day_sec: None,
expected: "1984-03-07T12:31:14.645Z",
label: "ldt+ldt",
},
Case {
date: date_ldt.clone(),
time: time_ldt.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-03-07T12:31:14.645+05:00",
label: "ldt+ldt+tz",
},
Case {
date: date_ldt.clone(),
time: time_ldt.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-03-28T12:31:42.645Z",
label: "ldt+ldt+ds",
},
Case {
date: date_ldt.clone(),
time: time_ldt.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-03-28T12:31:42.645-10:00[Pacific/Honolulu]",
label: "ldt+ldt+ds+tz",
},
Case {
date: date_ldt.clone(),
time: time_dt.clone(),
tz: None,
day_sec: None,
expected: "1984-03-07T12:00+01:00[Europe/Stockholm]",
label: "ldt+dt",
},
Case {
date: date_ldt.clone(),
time: time_dt.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-03-07T16:00+05:00",
label: "ldt+dt+tz",
},
Case {
date: date_ldt.clone(),
time: time_dt.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-03-28T12:00:42+02:00[Europe/Stockholm]",
label: "ldt+dt+ds",
},
Case {
date: date_ldt.clone(),
time: time_dt.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-03-28T00:00:42-10:00[Pacific/Honolulu]",
label: "ldt+dt+ds+tz",
},
Case {
date: date_dt.clone(),
time: time_lt.clone(),
tz: None,
day_sec: None,
expected: "1984-10-11T12:31:14.645876123Z",
label: "dt+lt",
},
Case {
date: date_dt.clone(),
time: time_lt.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-10-11T12:31:14.645876123+05:00",
label: "dt+lt+tz",
},
Case {
date: date_dt.clone(),
time: time_lt.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645876123Z",
label: "dt+lt+ds",
},
Case {
date: date_dt.clone(),
time: time_lt.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645876123-10:00[Pacific/Honolulu]",
label: "dt+lt+ds+tz",
},
Case {
date: date_dt.clone(),
time: time_t.clone(),
tz: None,
day_sec: None,
expected: "1984-10-11T12:31:14.645876+01:00",
label: "dt+t",
},
Case {
date: date_dt.clone(),
time: time_t.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-10-11T16:31:14.645876+05:00",
label: "dt+t+tz",
},
Case {
date: date_dt.clone(),
time: time_t.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645876+01:00",
label: "dt+t+ds",
},
Case {
date: date_dt.clone(),
time: time_t.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-10-28T01:31:42.645876-10:00[Pacific/Honolulu]",
label: "dt+t+ds+tz",
},
Case {
date: date_dt.clone(),
time: time_ldt.clone(),
tz: None,
day_sec: None,
expected: "1984-10-11T12:31:14.645Z",
label: "dt+ldt",
},
Case {
date: date_dt.clone(),
time: time_ldt.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-10-11T12:31:14.645+05:00",
label: "dt+ldt+tz",
},
Case {
date: date_dt.clone(),
time: time_ldt.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645Z",
label: "dt+ldt+ds",
},
Case {
date: date_dt.clone(),
time: time_ldt.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-10-28T12:31:42.645-10:00[Pacific/Honolulu]",
label: "dt+ldt+ds+tz",
},
Case {
date: date_dt.clone(),
time: time_dt.clone(),
tz: None,
day_sec: None,
expected: "1984-10-11T12:00+01:00[Europe/Stockholm]",
label: "dt+dt",
},
Case {
date: date_dt.clone(),
time: time_dt.clone(),
tz: Some("+05:00"),
day_sec: None,
expected: "1984-10-11T16:00+05:00",
label: "dt+dt+tz",
},
Case {
date: date_dt.clone(),
time: time_dt.clone(),
tz: None,
day_sec: Some((28, 42)),
expected: "1984-10-28T12:00:42+01:00[Europe/Stockholm]",
label: "dt+dt+ds",
},
Case {
date: date_dt.clone(),
time: time_dt.clone(),
tz: Some("Pacific/Honolulu"),
day_sec: Some((28, 42)),
expected: "1984-10-28T01:00:42-10:00[Pacific/Honolulu]",
label: "dt+dt+ds+tz",
},
];
for case in &cases {
let mut entries: Vec<(&str, Value)> =
vec![("date", case.date.clone()), ("time", case.time.clone())];
if let Some((day, sec)) = case.day_sec {
entries.push(("day", Value::from(day)));
entries.push(("second", Value::from(sec)));
}
if let Some(tz) = case.tz {
entries.push(("timezone", Value::from(tz)));
}
let res = eval_scalar_function("DATETIME", &[make_map(entries)], None)
.unwrap_or_else(|e| panic!("datetime({}) failed: {}", case.label, e));
assert_eq!(res.to_string(), case.expected, "case: {}", case.label);
}
}
#[test]
fn test_project_datetime_from_datetime() {
let ldt_source = eval_scalar_function(
"LOCALDATETIME",
&[unival!({"year": 1984, "week": 10, "dayOfWeek": 3, "hour": 12, "minute": 31, "second": 14, "millisecond": 645})],
None,
)
.unwrap();
let res = eval_scalar_function("DATETIME", std::slice::from_ref(&ldt_source), None).unwrap();
assert_eq!(res.to_string(), "1984-03-07T12:31:14.645Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![("datetime", ldt_source.clone())])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-07T12:31:14.645Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("datetime", ldt_source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-07T12:31:14.645+05:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("datetime", ldt_source.clone()),
("day", Value::from(28)),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-03-28T12:31:42.645Z");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("datetime", ldt_source.clone()),
("day", Value::from(28)),
("second", Value::from(42)),
("timezone", Value::from("Pacific/Honolulu")),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-03-28T12:31:42.645-10:00[Pacific/Honolulu]"
);
let dt_source = eval_scalar_function(
"DATETIME",
&[unival!({"year": 1984, "month": 10, "day": 11, "hour": 12, "timezone": "Europe/Stockholm"})],
None,
)
.unwrap();
let res = eval_scalar_function("DATETIME", std::slice::from_ref(&dt_source), None).unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:00+01:00[Europe/Stockholm]");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![("datetime", dt_source.clone())])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T12:00+01:00[Europe/Stockholm]");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("datetime", dt_source.clone()),
("timezone", Value::from("+05:00")),
])],
None,
)
.unwrap();
assert_eq!(res.to_string(), "1984-10-11T16:00+05:00");
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("datetime", dt_source.clone()),
("day", Value::from(28)),
("second", Value::from(42)),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-10-28T12:00:42+01:00[Europe/Stockholm]"
);
let res = eval_scalar_function(
"DATETIME",
&[make_map(vec![
("datetime", dt_source.clone()),
("day", Value::from(28)),
("second", Value::from(42)),
("timezone", Value::from("Pacific/Honolulu")),
])],
None,
)
.unwrap();
assert_eq!(
res.to_string(),
"1984-10-28T01:00:42-10:00[Pacific/Honolulu]"
);
}