Skip to content

Commit

Permalink
tidb_query: reset the hms after validating date (#6447)
Browse files Browse the repository at this point in the history
Signed-off-by: Lonng <heng@lonng.org>
  • Loading branch information
lonng authored Jan 9, 2020
1 parent 837299f commit 8dacc18
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions components/tidb_query/src/codec/mysql/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,19 +921,19 @@ impl Time {
time
}

fn new(ctx: &mut EvalContext, mut config: TimeArgs) -> Result<Time> {
if config.time_type == TimeType::Date {
config.hour = 0;
config.minute = 0;
config.second = 0;
config.micro = 0;
config.fsp = 0;
}

fn new(ctx: &mut EvalContext, config: TimeArgs) -> Result<Time> {
let unchecked_time = Self::unchecked_new(config.clone());
Ok(Self::unchecked_new(config.check(ctx).ok_or_else(|| {
Error::incorrect_datetime_value(unchecked_time)
})?))
let mut checked = config
.check(ctx)
.ok_or_else(|| Error::incorrect_datetime_value(unchecked_time))?;
if checked.time_type == TimeType::Date {
checked.hour = 0;
checked.minute = 0;
checked.second = 0;
checked.micro = 0;
checked.fsp = 0;
}
Ok(Self::unchecked_new(checked))
}

fn check_month_and_day(
Expand Down Expand Up @@ -1882,24 +1882,26 @@ mod tests {
}

let should_fail = vec![
("11-12-13 T 12:34:56", 0),
("11:12:13 T12:34:56", 0),
("11:12:13 T12:34:56.12", 7),
("11:12:13T25:34:56.12", 7),
("11:12:13T23:61:56.12", 7),
("11:12:13T23:59:89.12", 7),
("11121311121.1", 2),
("111213111.1", 2),
("11121311.1", 2),
("1112131.1", 2),
("111213.1", 2),
("111213.1", 2),
("11121.1", 2),
("1112", 2),
("11-12-13 T 12:34:56"),
("11:12:13 T12:34:56"),
("11:12:13 T12:34:56.12"),
("11:12:13T25:34:56.12"),
("11:12:13T23:61:56.12"),
("11:12:13T23:59:89.12"),
("11121311121.1"),
("1201012736"),
("1201012736.0"),
("111213111.1"),
("11121311.1"),
("1112131.1"),
("111213.1"),
("111213.1"),
("11121.1"),
("1112"),
];

for (case, fsp) in should_fail {
assert!(Time::parse_datetime(&mut ctx, case, fsp, false).is_err());
for case in should_fail {
assert!(Time::parse_date(&mut ctx, case).is_err());
}
Ok(())
}
Expand Down Expand Up @@ -1994,6 +1996,8 @@ mod tests {
("11:12:13T23:61:56.12", 7),
("11:12:13T23:59:89.12", 7),
("11121311121.1", 2),
("1201012736", 2),
("1201012736.0", 2),
("111213111.1", 2),
("11121311.1", 2),
("1112131.1", 2),
Expand Down

0 comments on commit 8dacc18

Please sign in to comment.