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

Push down ROUND(x) on decimal types #2492

Merged
merged 16 commits into from
Aug 6, 2021
Merged

Push down ROUND(x) on decimal types #2492

merged 16 commits into from
Aug 6, 2021

Conversation

riteme
Copy link
Contributor

@riteme riteme commented Jul 26, 2021

What problem does this PR solve?

Issue Number: close #2426, part of #2550

Problem Summary: enable ROUND(x) on TiFlash.

What is changed and how it works?

What's Changed:

  • IDataType.h , DataTypesNumber.h: add isFloatingPoint.
  • DAGExpressionAnalyzer.cpp: construct tidbRound from tidbRoundWithFrac.
  • DAGUtils.cpp: map TiPB codes to function names.
  • FunctionsArithmetic.h, toUnsigned.h: move out toUnsigned for reusing in FunctionsRound.h.
  • FunctionsRound.cpp: register new function.
  • FunctionsRound.h: the implementation.

Related changes

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

Check List

Tests

  • Unit test
  • Integration test

Release note

  • Support pushing down ROUND(X) to TiFlash.

@riteme riteme changed the title [DNM] Push down ROUND on decimal types [DNM] Push down ROUND(x) on decimal types Aug 3, 2021
@riteme riteme changed the title [DNM] Push down ROUND(x) on decimal types [test] Push down ROUND(x) on decimal types Aug 3, 2021
@riteme riteme self-assigned this Aug 3, 2021
@riteme riteme requested a review from fuzhe1989 August 3, 2021 07:48
@riteme
Copy link
Contributor Author

riteme commented Aug 3, 2021

/run-all-tests

dbms/src/Functions/toUnsigned.h Outdated Show resolved Hide resolved
dbms/src/Functions/toUnsigned.h Outdated Show resolved Hide resolved
tests/fullstack-test/expr/round.test Show resolved Hide resolved
if (expr.children_size() != 1)
throw TiFlashException("Invalid arguments of ROUND function", Errors::Coprocessor::BadRequest);

Names argument_names;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you can construct argument_names after const_zero_arg_name is ready.

dbms/src/Functions/FunctionsRound.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsRound.h Outdated Show resolved Hide resolved
block.getByPosition(result).column = std::move(result_column);
}

bool checkInputType(const DataTypePtr & input_type, const DataTypePtr & return_type, const DataTypePtr & frac_type,
Copy link
Contributor

Choose a reason for hiding this comment

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

there's a mismatch in the parameter orders: for data type the order is [input, return, frac] while for column it is [input, frac, result]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some combinations of InputType and ReturnType are invalid. I want to stop the recursion as soon as possible to reduce compilation time.

dbms/src/Functions/FunctionsRound.h Show resolved Hide resolved
using ResultColumn = std::conditional_t<IsDecimal<ReturnType>, ColumnDecimal<ReturnType>, ColumnVector<ReturnType>>;

// TODO: RoundWithFrac
EXPECT(!input_column->isColumnConst());
Copy link
Contributor

Choose a reason for hiding this comment

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

guess I'm reviewing an unfinished pr...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah...It's unfinished because the code framework is prepared for round(x, d).

auto input_scale = getDecimalScale(*input_type, 0);
auto result_scale = getDecimalScale(*return_type, 0);

if (!checkInputType(input_type, return_type, frac_type, input_column, frac_column, result_column, input_scale, result_scale))
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. check sounds like a readonly function but it isn't.
  2. a boolean return value can't tell us anything, we should throw exception from where the check fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. Any suggestion on naming? What about checkInputTypeAndApply?
  2. I will rewrite the code that throws an exception in each checkXXX function.

@riteme riteme added component/expression type/enhancement The issue or PR belongs to an enhancement. labels Aug 4, 2021
Comment on lines +643 to +645
{tipb::ScalarFuncSig::RoundReal, "tidbRound"},
{tipb::ScalarFuncSig::RoundInt, "tidbRound"},
{tipb::ScalarFuncSig::RoundDec, "tidbRound"},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Original implementation of round from Clickhouse employed some SIMD computations, but tidbRoundWithFrac currently doesn't make use of them at all.

Maybe reset RoundReal and RoundInt to "round" in this PR for better performance?

@riteme riteme changed the title [test] Push down ROUND(x) on decimal types Push down ROUND(x) on decimal types Aug 4, 2021
@riteme
Copy link
Contributor Author

riteme commented Aug 4, 2021

/run-all-tests

1 similar comment
@riteme
Copy link
Contributor Author

riteme commented Aug 4, 2021

/run-all-tests

@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 5, 2021
@riteme
Copy link
Contributor Author

riteme commented Aug 5, 2021

/run-all-tests

@riteme riteme requested a review from fuzhe1989 August 5, 2021 07:16
dbms/src/Functions/tests/CMakeLists.txt Outdated Show resolved Hide resolved
// for DEBUG only, will be removed after RoundWithFrac is implemented.
#define EXPECT(expr) \
if (!(expr)) \
throw TiFlashException(fmt::format("\"{}\" failed", #expr), Errors::Coprocessor::Internal);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure it's good to put expr into error message. Suggest to provide readable error message since it may be seen by users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to replace them with assert so that they don't abort in Release mode.

dbms/src/Functions/FunctionsRound.h Show resolved Hide resolved
dbms/src/Functions/FunctionsRound.h Outdated Show resolved Hide resolved
dbms/src/Functions/FunctionsRound.h Show resolved Hide resolved
dbms/src/Functions/FunctionsRound.h Show resolved Hide resolved
dbms/src/Functions/FunctionsRound.h Show resolved Hide resolved
}

// in MySQL, frac is clamped to 30, which is identical to decimal_max_scale.
if (unsigned_frac > decimal_max_scale)
Copy link
Contributor

Choose a reason for hiding this comment

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

the result won't be clamped if the field is signed, is it expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's indeed a bug.

dbms/src/Functions/FunctionsRound.h Show resolved Hide resolved
@@ -874,7 +869,7 @@ struct TiDBIntegerRound
static constexpr InputType eval(const InputType & input, FracType frac)
{
// TODO: RoundWithFrac.
EXPECT(frac == 0);
assert(frac == 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

then how will you prevent round(x, frac) from being pushed down?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only tidbRound is mapped in DAGUtils.cpp.

dbms/src/Functions/FunctionsRound.h Show resolved Hide resolved
dbms/src/Functions/FunctionsRound.h Outdated Show resolved Hide resolved
Copy link
Contributor

@fuzhe1989 fuzhe1989 left a comment

Choose a reason for hiding this comment

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

rest LGTM

{
static_assert(is_integer_v<InputType>);

static constexpr InputType eval(const InputType & input, FracType frac)
Copy link
Contributor

Choose a reason for hiding this comment

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

here [[maybe_unused]]

using UnsignedNativeType = make_unsigned_t<typename InputType::NativeType>;
using Pow = ConstPowOf10<UnsignedNativeType, maxDecimalPrecision<InputType>()>;

static constexpr OutputType eval(const InputType & input, FracType frac, ScaleType input_scale)
Copy link
Contributor

Choose a reason for hiding this comment

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

here [[maybe_unused]]

@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).

1 similar comment
@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).

@riteme
Copy link
Contributor Author

riteme commented Aug 6, 2021

/merge

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

/run-all-tests

@ti-srebot ti-srebot merged commit 4997809 into pingcap:master Aug 6, 2021
@ti-srebot
Copy link
Collaborator

cherry pick to release-5.0 in PR #2610

@ti-srebot
Copy link
Collaborator

cherry pick to release-5.1 in PR #2611

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression 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 The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Push down ROUND() on Decimal types
4 participants