Copilot commented on code in PR #47297: URL: https://github.com/apache/arrow/pull/47297#discussion_r2263675377
########## cpp/src/arrow/compute/kernel.cc: ########## @@ -475,23 +475,78 @@ std::string OutputType::ToString() const { return "computed"; } +// ---------------------------------------------------------------------- +// MatchConstraint + +std::shared_ptr<MatchConstraint> DecimalsHaveSameScale() { + class DecimalsHaveSameScaleConstraint : public MatchConstraint { + public: + bool Validate(const std::vector<TypeHolder>& types) const override { + DCHECK_GE(types.size(), 2); + DCHECK(std::all_of(types.begin(), types.end(), + [](const TypeHolder& type) { return is_decimal(type.id()); })); + auto ty0 = dynamic_cast<const DecimalType*>(types[0].type); + DCHECK_NE(ty0, nullptr); + auto s0 = ty0->scale(); + for (size_t i = 1; i < types.size(); ++i) { + auto ty = dynamic_cast<const DecimalType*>(types[i].type); + DCHECK_NE(ty, nullptr); + if (ty->scale() != s0) { + return false; + } + } + return true; + } + }; + static auto instance = std::make_shared<DecimalsHaveSameScaleConstraint>(); + return instance; +} + +namespace { + +template <typename Op> +class BinaryDecimalScaleComparisonConstraint : public MatchConstraint { + public: + bool Validate(const std::vector<TypeHolder>& types) const override { + DCHECK_EQ(types.size(), 2); + DCHECK(is_decimal(types[0].id())); + DCHECK(is_decimal(types[1].id())); + auto ty0 = dynamic_cast<const DecimalType*>(types[0].type); + DCHECK_NE(ty0, nullptr); + auto ty1 = dynamic_cast<const DecimalType*>(types[1].type); + DCHECK_NE(ty1, nullptr); + return Op{}(ty0->scale(), ty1->scale()); + } +}; + +} // namespace + +std::shared_ptr<MatchConstraint> BinaryDecimalScaleComparisonGE() { + using BinaryDecimalScaleComparisonGEConstraint = + BinaryDecimalScaleComparisonConstraint<std::greater_equal<>>; + static auto instance = std::make_shared<BinaryDecimalScaleComparisonGEConstraint>(); Review Comment: The static instance may not be thread-safe during initialization. Consider using std::call_once or similar synchronization mechanism to ensure thread-safe initialization. ```suggestion static std::shared_ptr<BinaryDecimalScaleComparisonGEConstraint> instance; static std::once_flag flag; std::call_once(flag, []() { instance = std::make_shared<BinaryDecimalScaleComparisonGEConstraint>(); }); ``` ########## cpp/src/arrow/compute/kernel.cc: ########## @@ -475,23 +475,78 @@ std::string OutputType::ToString() const { return "computed"; } +// ---------------------------------------------------------------------- +// MatchConstraint + +std::shared_ptr<MatchConstraint> DecimalsHaveSameScale() { + class DecimalsHaveSameScaleConstraint : public MatchConstraint { + public: + bool Validate(const std::vector<TypeHolder>& types) const override { + DCHECK_GE(types.size(), 2); + DCHECK(std::all_of(types.begin(), types.end(), + [](const TypeHolder& type) { return is_decimal(type.id()); })); + auto ty0 = dynamic_cast<const DecimalType*>(types[0].type); + DCHECK_NE(ty0, nullptr); + auto s0 = ty0->scale(); + for (size_t i = 1; i < types.size(); ++i) { + auto ty = dynamic_cast<const DecimalType*>(types[i].type); + DCHECK_NE(ty, nullptr); + if (ty->scale() != s0) { + return false; + } + } + return true; + } + }; + static auto instance = std::make_shared<DecimalsHaveSameScaleConstraint>(); Review Comment: The static instance may not be thread-safe during initialization. Consider using std::call_once or similar synchronization mechanism to ensure thread-safe initialization. ```suggestion static std::once_flag once; static std::shared_ptr<DecimalsHaveSameScaleConstraint> instance; std::call_once(once, []() { instance = std::make_shared<DecimalsHaveSameScaleConstraint>(); }); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org