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

Add support for function mod #2402

Merged
merged 36 commits into from
Jul 22, 2021
Merged

Add support for function mod #2402

merged 36 commits into from
Jul 22, 2021

Conversation

riteme
Copy link
Contributor

@riteme riteme commented Jul 14, 2021

What problem does this PR solve?

Issue Number: close #2318

Problem Summary: enable pushing function mod down to TiFlash.

What is changed and how it works?

Proposal:

What's Changed:

  • Decimal.h: compatible with TiDB.
  • DataTypeDecimal.h: add a helper function to get decimal precision in unit test.
  • DAGUtils.cpp: add mappings from TiPB codes to scalar function name.
  • FunctionArithmetic.h: update implementation of FunctionModulo.
  • NumberTraits.h: fix modulo result type deduction.
  • types.h: add actual_type, which returns correct size of boost multiprecision integer.
  • mod.test, mod_extra.test: the integration test.
  • gtest_arithmetic_functions.cpp: the unit test.

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch:

Check List

  • Examine current implementation
    • Return type detection
    • Decimal precision and scale
    • Floating point: INF, NaN
  • Fix compiler warnings

Tests

  • Unit test
  • Integration test

Release note

  • Support pushing down MOD(N, M) to TiFlash.

@riteme riteme changed the title [DNM] Add support for function mod [DNM][WIP] Add support for function mod Jul 14, 2021
@riteme riteme changed the title [DNM][WIP] Add support for function mod [DNM] Add support for function mod Jul 15, 2021
@riteme
Copy link
Contributor Author

riteme commented Jul 15, 2021

/run-all-tests

@riteme
Copy link
Contributor Author

riteme commented Jul 16, 2021

/run-all-tests

@riteme riteme changed the title [DNM] Add support for function mod Add support for function mod Jul 16, 2021
@riteme
Copy link
Contributor Author

riteme commented Jul 19, 2021

/run-all-tests

dbms/src/DataTypes/NumberTraits.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsArithmetic.h Outdated Show resolved Hide resolved
}
else
{
return static_cast<Result>(a) % static_cast<Result>(b);
Copy link
Contributor

Choose a reason for hiding this comment

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

When both A and B are not decimal, the Result shouldn't be decimal. Suggest only leaving a static_assert(!IsDecimal<Result>) here.

dbms/src/Functions/FunctionsArithmetic.h Outdated Show resolved Hide resolved
}
else // both A and B are integrals.
{
using UnsignedResultType = std::make_unsigned_t<Result>;
Copy link
Contributor

Choose a reason for hiding this comment

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

suggest using tics version of make_unsigned_t and is_signed_v.

dbms/src/Functions/FunctionsArithmetic.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsArithmetic.h Outdated Show resolved Hide resolved
x = static_cast<UnsignedResultType>(a);

if (std::is_signed_v<B> && b < 0)
y = static_cast<UnsignedResultType>(-b);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

dbms/src/Functions/FunctionsArithmetic.h Outdated Show resolved Hide resolved
@riteme
Copy link
Contributor Author

riteme commented Jul 19, 2021

/run-all-tests

@riteme
Copy link
Contributor Author

riteme commented Jul 21, 2021

/run-all-tests

* TODO:
* * add 128-bit and 256-bit integers.
* * change Float32 to Float64.
* * all floating-point arithmetics should evaluate to Float64.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* * all floating-point arithmetics should evaluate to Float64.
* * all floating-point arithmetics should be evaluated to Float64.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Past tense may not be needed. Here are some examples: https://linggle.com/?q=evaluate+to

Copy link
Contributor

Choose a reason for hiding this comment

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

Guess they have different subjects. Anyway either style is okay.

dbms/src/DataTypes/NumberTraits.h Show resolved Hide resolved
dbms/src/DataTypes/NumberTraits.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsArithmetic.h Outdated Show resolved Hide resolved
auto & a_nullmap_data = a_nullmap->getData();
auto & b_nullmap_data = b_nullmap->getData();
for (size_t i = 0; i < size; ++i)
res_null[i] = a_nullmap_data[i] || b_nullmap_data[i];
Copy link
Contributor

Choose a reason for hiding this comment

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

https://godbolt.org/z/Kj7chc3xo seems | can generate simpler code than ||.

Anyway I found compiler won't automatically vectorize this for-loop.

dbms/src/Functions/FunctionsArithmetic.h Show resolved Hide resolved
}
else if constexpr (is_modulo)
{
if (scale_a != 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Got it, so would you like to paste your explanation into the code?

riteme and others added 2 commits July 22, 2021 09:43
Co-authored-by: Fu Zhe <fuzhe1989@gmail.com>
Co-authored-by: Fu Zhe <fuzhe1989@gmail.com>
@fuzhe1989
Copy link
Contributor

/run-all-tests

@ti-srebot
Copy link
Collaborator

@fuzhe1989, Thanks for your review. The bot only counts LGTMs from Reviewers and higher roles, but you're still welcome to leave your comments. See the corresponding SIG page for more information. Related SIG: tiflash(slack).

@fuzhe1989
Copy link
Contributor

/merge

@ti-srebot ti-srebot added the status/can-merge Indicates a PR has been approved by a committer. label Jul 22, 2021
@ti-srebot
Copy link
Collaborator

/run-all-tests

@ti-srebot ti-srebot merged commit 685880a into pingcap:master Jul 22, 2021
@zanmato1984 zanmato1984 added needs-cherry-pick-release-5.0 PR which needs to be cherry-picked to release-5.0 needs-cherry-pick-release-5.1 PR which needs to be cherry-picked to release-5.1 labels Aug 4, 2021
@zanmato1984
Copy link
Contributor

/run-cherry-picker

@zanmato1984 zanmato1984 added the type/enhancement Issue or PR for enhancement label Aug 4, 2021
@ti-srebot
Copy link
Collaborator

cherry pick to release-5.1 in PR #2560

@ti-srebot
Copy link
Collaborator

cherry pick to release-5.0 in PR #2561

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cherry-pick-release-5.0 PR which needs to be cherry-picked to release-5.0 needs-cherry-pick-release-5.1 PR which needs to be cherry-picked to release-5.1 status/can-merge Indicates a PR has been approved by a committer. type/enhancement Issue or PR for enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support function mod in TiFlash
5 participants