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

tidb_query: fix CAST as decimal inconsistency between TiKV and TiDB #5468

Merged
merged 32 commits into from
Oct 12, 2019

Conversation

H-ZeX
Copy link
Contributor

@H-ZeX H-ZeX commented Sep 16, 2019

Signed-off-by: H-ZeX hzx20112012@gmail.com

What have you changed?

compare tidb and tikv's impl of cast and fix what they are inconsistent.

What is the type of the changes?

  • Improvement (a change which is an improvement to an existing feature)
  • Bugfix (a change which fixes an issue)
  • Engineering (engineering change which doesn't change any feature or fix any issue)

How is the PR tested?

unit test

Does this PR affect documentation (docs) or should it be mentioned in the release notes?

No.

Does this PR affect tidb-ansible?

No.

Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
@sre-bot
Copy link
Contributor

sre-bot commented Sep 16, 2019

Thanks for your PR.
This PR will be closed by bot because you already had 3 opened PRs, close or merge them before submitting a new one.
#5423 , #5431 , #5433

@sre-bot sre-bot closed this Sep 16, 2019
@H-ZeX H-ZeX added the sig/coprocessor SIG: Coprocessor label Sep 16, 2019
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
@H-ZeX H-ZeX reopened this Sep 24, 2019
…iff_part_5

Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
@lonng lonng changed the title check and fix inconsistency between TiKV and TiDB's cast (part 5)(* as decimal) tidb_query: fix CAST as decimal inconsistency between TiKV and TiDB Sep 27, 2019
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
…iff_part_5

Signed-off-by: H-ZeX <hzx20112012@gmail.com>
if let Error::Eval(_, d) = r.err().unwrap() {
assert_eq!(d, ERR_M_BIGGER_THAN_D, "{}", log);
} else {
panic!("unreachable path, {}", log);
Copy link
Member

Choose a reason for hiding this comment

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

Should we use unreachable!() instead?

panic!("unreachable path, {}", log);
}
}
_ => panic!("unreachable path, {}", log),
Copy link
Member

Choose a reason for hiding this comment

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

Ditto

@@ -1907,6 +1908,7 @@ impl ToString for Decimal {
impl Display for Decimal {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
let mut dec = self.clone();
// TODO: why there is rounding here?
Copy link
Member

Choose a reason for hiding this comment

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

Please fix this TODO note, make it more declarative.

@@ -779,6 +924,25 @@ mod tests {
EvalContext::new(cfg)
}

fn make_ctx_4(
Copy link
Member

Choose a reason for hiding this comment

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

Could we apply a builder pattern here? Create a configuration structure to describe such flags.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Different test need different flag, so if will create a config, it should had all flag as option. Then it will same as EvalContext.
This is test, and rust has no overload (if use trait, it is too troublesome). So it need to seperate these functions will _x

if truncate_as_warning {
flag |= Flag::TRUNCATE_AS_WARNING;
}
for i in flags {
Copy link
Member

Choose a reason for hiding this comment

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

flag |= flags.fold(Flag::new(), |acc, x| acc | x) looks better?

#[derive(Clone, Copy, Debug)]
#[allow(clippy::enum_variant_names)]
enum Cond {
TargetIntPartLenLessThanOriginIntPartLen,
Copy link
Member

Choose a reason for hiding this comment

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

Could we shorten these names?

Copy link
Contributor Author

@H-ZeX H-ZeX Oct 9, 2019

Choose a reason for hiding this comment

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

If shorten it, then I need to add doc for it. I think add doc is not a good idea.
There is many long function name in TiKV.

iosmanthus and others added 2 commits October 9, 2019 19:43
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
let res = if in_union(extra.implicit_args) && val.is_negative() {
Decimal::zero()
} else {
// FIXME: here TiDB may has bug, fix this after fix TiDB's
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

@@ -434,8 +434,9 @@ fn do_sub<'a>(mut lhs: &'a Decimal, mut rhs: &'a Decimal) -> Res<Decimal> {
res
}

// TODO: add check for prec and frac_cnt
Copy link
Member

Choose a reason for hiding this comment

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

Prefer to remove this comment and add an assert(prec > frac_cnt) (this TODO is meaningless because it's easy to fix it).

Comment on lines 629 to 630
// because uint's upper bound is smaller than signed decimal's upper bound
// so we can merge cast uint as signed/unsigned decimal in this function
Copy link
Member

Choose a reason for hiding this comment

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

I think to move the two lines comments to above L641 is appropriate.

Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Signed-off-by: H-ZeX <hzx20112012@gmail.com>
/// If `prec` > `frac_cnt`, panic.
/// The panic is because of `debug_assert`.

// TODO: what if prec-frac_cnt==0
Copy link
Member

@lonng lonng Oct 12, 2019

Choose a reason for hiding this comment

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

It's OK if the int_cnt is zero.

create table td (a decimal(2,2));
insert into td values ('.99');

@@ -435,7 +435,16 @@ fn do_sub<'a>(mut lhs: &'a Decimal, mut rhs: &'a Decimal) -> Res<Decimal> {
}

/// Get the max possible decimal with giving precision and fraction digit count.
fn max_decimal(prec: u8, frac_cnt: u8) -> Decimal {
/// The `prec` should <= `frac_cnt`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// The `prec` should <= `frac_cnt`.
/// The `prec` should >= `frac_cnt`.

@@ -465,6 +474,12 @@ fn max_decimal(prec: u8, frac_cnt: u8) -> Decimal {

/// `max_or_min_dec`(`NewMaxOrMinDec` in tidb) returns the max or min
/// value decimal for given precision and fraction.
/// The `prec` should <= `frac_cnt`.
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Signed-off-by: H-ZeX <hzx20112012@gmail.com>
///
/// # Panics
///
/// If `prec` > `frac_cnt`, panic.
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Signed-off-by: H-ZeX <hzx20112012@gmail.com>
Copy link
Member

@lonng lonng left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@andylokandy andylokandy left a comment

Choose a reason for hiding this comment

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

LGTM

@breezewish
Copy link
Member

/merge

@sre-bot sre-bot added the status/can-merge Indicates a PR has been approved by a committer. label Oct 12, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Oct 12, 2019

Your auto merge job has been accepted, waiting for 5635

@sre-bot
Copy link
Contributor

sre-bot commented Oct 12, 2019

/run-all-tests

@sre-bot
Copy link
Contributor

sre-bot commented Oct 12, 2019

@H-ZeX merge failed.

@lonng
Copy link
Member

lonng commented Oct 12, 2019

/test

@lonng lonng merged commit 0e355bf into tikv:master Oct 12, 2019
@lonng lonng deleted the fix_cast_diff_part_5 branch October 12, 2019 08:00
aknuds1 pushed a commit to aknuds1/tikv that referenced this pull request Oct 13, 2019
sticnarf pushed a commit to sticnarf/tikv that referenced this pull request Oct 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/coprocessor SIG: Coprocessor status/can-merge Indicates a PR has been approved by a committer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants