Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.2k
ARROW-13390: [C++] Implement coalesce for remaining types#11080
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
882e86746912eef744fca754e0ceb51c251c11c4bd56a975c305855990c6a103a519e463b2fa6File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1279,14 +1279,17 @@ void ReplaceNullWithOtherType(std::vector<ValueDescr>* descrs); | ||
| ARROW_EXPORT | ||
| void ReplaceTypes(const std::shared_ptr<DataType>&, std::vector<ValueDescr>* descrs); | ||
| ARROW_EXPORT | ||
| void ReplaceTypes(const std::shared_ptr<DataType>&, ValueDescr* descrs, size_t count); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a reminder that we'd like a Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. | ||
| ARROW_EXPORT | ||
| std::shared_ptr<DataType> CommonNumeric(const std::vector<ValueDescr>& descrs); | ||
| ARROW_EXPORT | ||
| std::shared_ptr<DataType> CommonNumeric(const ValueDescr* begin, size_t count); | ||
| ARROW_EXPORT | ||
| std::shared_ptr<DataType> CommonTimestamp(const std::vector<ValueDescr>& descrs); | ||
| std::shared_ptr<DataType> CommonTemporal(const std::vector<ValueDescr>& descrs); | ||
| ARROW_EXPORT | ||
| std::shared_ptr<DataType> CommonBinary(const std::vector<ValueDescr>& descrs); | ||
| @@ -1298,9 +1301,17 @@ enum class DecimalPromotion : uint8_t { | ||
| kDivide, | ||
| }; | ||
| /// Given two arguments, at least one of which is decimal, promote all | ||
| /// to not necessarily identical types, but types which are compatible | ||
| /// for the given operator (add/multiply/divide). | ||
| ARROW_EXPORT | ||
| Status CastBinaryDecimalArgs(DecimalPromotion promotion, std::vector<ValueDescr>* descrs); | ||
| /// Given one or more arguments, at least one of which is decimal, | ||
| /// promote all to an identical type. | ||
| ARROW_EXPORT | ||
| Status CastDecimalArgs(ValueDescr* begin, size_t count); | ||
| ARROW_EXPORT | ||
| bool HasDecimal(const std::vector<ValueDescr>& descrs); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
| #include "arrow/compute/kernels/codegen_internal.h" | ||
| #include "arrow/testing/gtest_util.h" | ||
| #include "arrow/type.h" | ||
| #include "arrow/type_fwd.h" | ||
| namespace arrow { | ||
| namespace compute { | ||
| namespace internal { | ||
| TEST(TestDispatchBest, CastBinaryDecimalArgs) { | ||
| std::vector<ValueDescr> args; | ||
| std::vector<DecimalPromotion> modes = { | ||
| DecimalPromotion::kAdd, DecimalPromotion::kMultiply, DecimalPromotion::kDivide}; | ||
| // Any float -> all float | ||
| for (auto mode : modes) { | ||
| args = {decimal128(3, 2), float64()}; | ||
| ASSERT_OK(CastBinaryDecimalArgs(mode, &args)); | ||
| AssertTypeEqual(args[0].type, float64()); | ||
| AssertTypeEqual(args[1].type, float64()); | ||
| } | ||
| // Integer -> decimal with common scale | ||
| args = {decimal128(1, 0), int64()}; | ||
| ASSERT_OK(CastBinaryDecimalArgs(DecimalPromotion::kAdd, &args)); | ||
| AssertTypeEqual(args[0].type, decimal128(1, 0)); | ||
| AssertTypeEqual(args[1].type, decimal128(19, 0)); | ||
| // Add: rescale so all have common scale | ||
| args = {decimal128(3, 2), decimal128(3, -2)}; | ||
| EXPECT_RAISES_WITH_MESSAGE_THAT( | ||
| NotImplemented, ::testing::HasSubstr("Decimals with negative scales not supported"), | ||
| CastBinaryDecimalArgs(DecimalPromotion::kAdd, &args)); | ||
| } | ||
| TEST(TestDispatchBest, CastDecimalArgs) { | ||
pitrou marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| std::vector<ValueDescr> args; | ||
| // Any float -> all float | ||
| args = {decimal128(3, 2), float64()}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, float64()); | ||
| AssertTypeEqual(args[1].type, float64()); | ||
| args = {float32(), float64(), decimal128(3, 2)}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, float64()); | ||
| AssertTypeEqual(args[1].type, float64()); | ||
| AssertTypeEqual(args[2].type, float64()); | ||
| // Promote to common decimal width | ||
| args = {decimal128(3, 2), decimal256(3, 2)}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, decimal256(3, 2)); | ||
| AssertTypeEqual(args[1].type, decimal256(3, 2)); | ||
| // Rescale so all have common scale/precision | ||
| args = {decimal128(3, 2), decimal128(3, 0)}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, decimal128(5, 2)); | ||
| AssertTypeEqual(args[1].type, decimal128(5, 2)); | ||
| args = {decimal128(3, 2), decimal128(3, -2)}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, decimal128(7, 2)); | ||
| AssertTypeEqual(args[1].type, decimal128(7, 2)); | ||
| args = {decimal128(3, 0), decimal128(3, 1), decimal128(3, 2)}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, decimal128(5, 2)); | ||
| AssertTypeEqual(args[1].type, decimal128(5, 2)); | ||
| AssertTypeEqual(args[2].type, decimal128(5, 2)); | ||
| // Integer -> decimal with appropriate precision | ||
| args = {decimal128(3, 0), int64()}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, decimal128(19, 0)); | ||
| AssertTypeEqual(args[1].type, decimal128(19, 0)); | ||
| args = {decimal128(3, 1), int64()}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, decimal128(20, 1)); | ||
| AssertTypeEqual(args[1].type, decimal128(20, 1)); | ||
| args = {decimal128(3, -1), int64()}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, decimal128(19, 0)); | ||
| AssertTypeEqual(args[1].type, decimal128(19, 0)); | ||
| // Overflow decimal128 max precision -> promote to decimal256 | ||
| args = {decimal128(38, 0), decimal128(37, 2)}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, decimal256(40, 2)); | ||
| AssertTypeEqual(args[1].type, decimal256(40, 2)); | ||
| // Overflow decimal256 max precision | ||
| args = {decimal256(76, 0), decimal256(75, 1)}; | ||
| EXPECT_RAISES_WITH_MESSAGE_THAT( | ||
| Invalid, | ||
| ::testing::HasSubstr( | ||
| "Result precision (77) exceeds max precision of Decimal256 (76)"), | ||
| CastDecimalArgs(args.data(), args.size())); | ||
| // Incompatible, no cast | ||
| args = {decimal256(3, 2), float64(), utf8()}; | ||
| ASSERT_OK(CastDecimalArgs(args.data(), args.size())); | ||
| AssertTypeEqual(args[0].type, decimal256(3, 2)); | ||
| AssertTypeEqual(args[1].type, float64()); | ||
| AssertTypeEqual(args[2].type, utf8()); | ||
| } | ||
| TEST(TestDispatchBest, CommonTemporal) { | ||
| AssertTypeEqual(timestamp(TimeUnit::NANO), CommonTemporal({timestamp(TimeUnit::SECOND), | ||
| timestamp(TimeUnit::NANO)})); | ||
| AssertTypeEqual(timestamp(TimeUnit::NANO, "UTC"), | ||
| CommonTemporal({timestamp(TimeUnit::SECOND, "UTC"), | ||
| timestamp(TimeUnit::NANO, "UTC")})); | ||
| AssertTypeEqual(timestamp(TimeUnit::NANO), | ||
| CommonTemporal({date32(), timestamp(TimeUnit::NANO)})); | ||
| AssertTypeEqual(timestamp(TimeUnit::MILLI), | ||
| CommonTemporal({date64(), timestamp(TimeUnit::SECOND)})); | ||
| AssertTypeEqual(date32(), CommonTemporal({date32(), date32()})); | ||
| AssertTypeEqual(date64(), CommonTemporal({date64(), date64()})); | ||
| AssertTypeEqual(date64(), CommonTemporal({date32(), date64()})); | ||
| ASSERT_EQ(nullptr, CommonTemporal({})); | ||
| ASSERT_EQ(nullptr, CommonTemporal({float64(), int32()})); | ||
| ASSERT_EQ(nullptr, CommonTemporal({timestamp(TimeUnit::SECOND), | ||
| timestamp(TimeUnit::SECOND, "UTC")})); | ||
| ASSERT_EQ(nullptr, CommonTemporal({timestamp(TimeUnit::SECOND, "America/Phoenix"), | ||
| timestamp(TimeUnit::SECOND, "UTC")})); | ||
| } | ||
| } // namespace internal | ||
| } // namespace compute | ||
| } // namespace arrow | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.