-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
882e867
ARROW-13390: [C++] Implement ToString for union scalars
lidavidm 46912ee
ARROW-13390: [C++] Implement Coalesce for remaining types
lidavidm f744fca
ARROW-13390: [Ruby] Update scalar to_s tests
lidavidm 754e0ce
ARROW-13390: [C++] Try to satisfy MinGW
lidavidm b51c251
ARROW-13390: [C++] Address feedback, add dispatch tests
lidavidm c11c4bd
ARROW-13390: [C++] Add/fix more dispatch tests
lidavidm 56a975c
ARROW-13390: [C++] Fix lint error
lidavidm 3058559
ARROW-13390: [C++] Address review suggestions
lidavidm 90c6a10
Update cpp/src/arrow/compute/kernels/scalar_arithmetic_test.cc
lidavidm 3a519e4
ARROW-13390: [C++] Add one last test case
lidavidm 63b2fa6
ARROW-13390: [C++] Reconcile with ARROW-13358
lidavidm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
||
|
|
||
| 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); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Show resolved
Hide resolved
|
||
| 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.