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

GH-41011: [C++] Add an output type resolver for decimal types in CompareFunction so can be casted correctly #41012

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ZhangHuiGui
Copy link
Collaborator

@ZhangHuiGui ZhangHuiGui commented Apr 4, 2024

Rationale for this change

Fix the problem that decimal types array compare with different scales make error results.

What changes are included in this PR?

Add an output type resolver for decimal types in CompareFunction so can be casted correctly

Are these changes tested?

Yes

Are there any user-facing changes?

Yes, decimal type's comparison operations with different scale will be correct.

Co-authored-by ZhangHuiGui [email protected]
Co-authored-by laotan332 [email protected]

Copy link

github-actions bot commented Apr 4, 2024

⚠️ GitHub issue #41011 has been automatically assigned in GitHub to PR creator.

@ZhangHuiGui ZhangHuiGui force-pushed the fix-deciaml-compare-wrong-in-compute-expression branch from d837aeb to 8ca5f83 Compare April 10, 2024 14:46
@ZhangHuiGui
Copy link
Collaborator Author

ZhangHuiGui commented Apr 11, 2024

@bkietz Would you mind take a look at this? A simple fix for the decimal type's compare function:)

@pitrou pitrou requested a review from bkietz April 15, 2024 14:16
}
return boolean();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

It's an interesting approach to use the output-type resolver to assert on the input types, but what you really doing here is asserting on the inputs. I think you should write a custom matcher instead. Compare always returns boolean(), there is nothing to resolve.

That would also help (in the future) dispatching to kernels that can deal with different scales dynamically. Values can still be logically equal even though they are represented physically in memory with different scales.

Copy link
Contributor

@felipecrv felipecrv May 10, 2024

Choose a reason for hiding this comment

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

I've read the issue and it seems that the problem here is that we need a special cast when the scales don't match because the generic cast can't currently handle that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or maybe we want users to be explicit with casts before comparing. I still think this should be handled by the input matching machinery.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

but what you really doing here is asserting on the inputs.

Yes, this line of code DCHECK_EQ(left_type.id(), right_type.id()); in ResolveDecimalCompareOutputType is problematic because we might be comparing float and decimal or decimal128 and decimal256...

Or maybe we want users to be explicit with casts before comparing. I still think this should be handled by the input matching machinery.

The current matcher system only works on matching of builtin types, and what the compare kernel function needs is whether the dependencies between builtin types meet the requirements. This requires adding a bool Matches(const std::vector<TypeHolder>& types) const; interface to TypeMatcher.

That means the previous semantics of TypeMatcher was to check the validity of builtin types. Now it is necessary to check the legality of dependencies generated when functions use builtin types. Is this reasonable for the design of TypeMatcher?

But in fact, it is indeed more reasonable to use matcher to determine whether the comparison operation of decimal types is legal.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Besides, there is a similar case in IfElse related kernel functions : #41363.
Do the input types' check in the output resolver in IfElse case is reasonable?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this reasonable for the design of TypeMatcher?

As long as it checks only the types and the code is simple enough that we can reason about the set of combinations handled.

If some combinations are invalid, but you want to generate a message, I think you could have the type matcher route bad combinations to these fail kernels that produce an error instead of automatically casting inputs.

Copy link
Contributor

Choose a reason for hiding this comment

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

That way, the output type of "compare" remains boolean as it should be. No extra logic needed for output type resolution, but the function is not total — for some input types, it produces a bad Status.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As long as it checks only the types and the code is simple enough that we can reason about the set of combinations handled.

Thanks, it's more concise to use TypeMatcher check the input types.

@github-actions github-actions bot added awaiting changes Awaiting changes awaiting change review Awaiting change review and removed awaiting review Awaiting review awaiting changes Awaiting changes labels May 10, 2024
@@ -505,9 +510,14 @@ bool KernelSignature::Equals(const KernelSignature& other) const {
}

bool KernelSignature::MatchesInputs(const std::vector<TypeHolder>& types) const {
auto is_match_combination_types = [&](const InputType& in_type) {
Copy link
Collaborator Author

@ZhangHuiGui ZhangHuiGui May 17, 2024

Choose a reason for hiding this comment

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

Add type matcher acceptable rule for the combination types.

//
const auto& left_type = checked_cast<const DecimalType&>(*types[0]);
const auto& right_type = checked_cast<const DecimalType&>(*types[1]);
assert(is_decimal(left_type) && is_decimal(right_type));
Copy link
Member

Choose a reason for hiding this comment

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

Is this duplicate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, yeah. Thanks, the line is for test. :)

const int32_t s1 = left_type.scale();
const int32_t s2 = right_type.scale();
Copy link
Member

Choose a reason for hiding this comment

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

So same scale with different precision can be matched here?

Copy link
Collaborator Author

@ZhangHuiGui ZhangHuiGui May 17, 2024

Choose a reason for hiding this comment

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

Yes, kAdd promotion rule only care about the scale and the implicit cast with this rule only keep scales same.

Result<TypeHolder> ResolveDecimalAdditionOrSubtractionOutput(
KernelContext*, const std::vector<TypeHolder>& types) {
return ResolveDecimalBinaryOperationOutput(
types,
[](int32_t p1, int32_t s1, int32_t p2,
int32_t s2) -> Result<std::pair<int32_t, int32_t>> {
if (s1 != s2) {
return Status::Invalid("Addition or subtraction of two decimal ",
"types scale1 != scale2. (", s1, s2, ").");
}
DCHECK_EQ(s1, s2);
const int32_t scale = s1;
const int32_t precision = std::max(p1 - s1, p2 - s2) + scale + 1;
return std::make_pair(precision, scale);
});

if (is_varargs_) {
for (size_t i = 0; i < types.size(); ++i) {
if (!in_types_[std::min(i, in_types_.size() - 1)].Matches(*types[i])) {
auto in_type = in_types_[std::min(i, in_types_.size() - 1)];
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
auto in_type = in_types_[std::min(i, in_types_.size() - 1)];
const auto& in_type = in_types_[std::min(i, in_types_.size() - 1)];

@ZhangHuiGui ZhangHuiGui force-pushed the fix-deciaml-compare-wrong-in-compute-expression branch from cd69ab2 to a7703b9 Compare May 17, 2024 10:09
@ZhangHuiGui
Copy link
Collaborator Author

cc @felipecrv , the new changes include the TypeMatcher way to return failure for combination types.

@felipecrv
Copy link
Contributor

cc @felipecrv , the new changes include the TypeMatcher way to return failure for combination types.

I've been away. I will take a look soon.

Comment on lines +112 to +114
/// \brief Return true if this matcher accepts the combination types
virtual bool Matches(const std::vector<TypeHolder>& types) const { return true; }

Copy link
Contributor

Choose a reason for hiding this comment

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

This class is for matching a single matcher (this) against a single type.

I don't think it's reasonable to extend the type matching this way as it makes type checking Turing-complete (wouldn't be easy/possible to encode the typing rules in a provably-decidable type system).

I'm sorry for this back and forth because I was the one that said "type matching machinery". I still think this logic shouldn't go on the output type resolver so I have a more precise suggestion this time. Since functions already have a way of implementing custom type matching rules -- DispatchBest —— you can add this logic in

if (HasDecimal(*types)) {
RETURN_NOT_OK(CastBinaryDecimalArgs(DecimalPromotion::kAdd, types));
}

and return return arrow::compute::detail::NoMatchingKernel(this, *types); or a Status::NotImplemented with a message that describes that you can't compare two decimals with different scales.

Copy link
Collaborator Author

@ZhangHuiGui ZhangHuiGui Jun 6, 2024

Choose a reason for hiding this comment

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

Ah, the root cause of the problem that this PR is trying to solve is actually CompareFunction with two different decimal scales in BindNonRecursive , it is impossible to enter the logic of DispatchBest.

// First try and bind exactly
Result<const Kernel*> maybe_exact_match = call.function->DispatchExact(types);
if (maybe_exact_match.ok()) {
call.kernel = *maybe_exact_match;
if (FinishBind().ok()) {
return Expression(std::move(call));
}
}

Which means the DispatchExact will always return ok and can't go into DispatchBest when CompareFunction called by expression system with two different decimal scales.

This is why we did type judgment in the output type Resolver before, so that we can return not-ok in the first FinishBind and enter DispatchBest.

DispatchBest does not need to make additional judgments, because it will do cast according to the decimal rules, ensuring that different input scales are cast to the same scales according to DecimalPromotion::kAdd.

@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting change review Awaiting change review labels Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants