Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
ARROW-15545: [Python][C++] Support casting to extension type#14106
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
e4db00bb9d8cb689aa16c444fd026c05a1d296b4fb120204b34c999820be1fec7e76c74cd9b652d189047e8d795069bfd03e46cbd9a5a51429db94b2ea1e11e6ab97a02013e8da66bf4374301c44cef448e5063f2bc8a832cef2ae135c8a0File 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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // 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. | ||
| // Implementation of casting to extension types | ||
| #include "arrow/compute/kernels/common.h" | ||
| #include "arrow/compute/kernels/scalar_cast_internal.h" | ||
| #include "arrow/scalar.h" | ||
| namespace arrow { | ||
| namespace compute { | ||
| namespace internal { | ||
| namespace { | ||
| Status CastToExtension(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { | ||
| const CastOptions& options = checked_cast<const CastState*>(ctx->state())->options; | ||
| const auto& ext_ty = static_cast<const ExtensionType&>(*options.to_type.type); | ||
| auto out_ty = ext_ty.storage_type(); | ||
| DCHECK(batch[0].is_array()); | ||
| std::shared_ptr<Array> array = batch[0].array.ToArray(); | ||
| // Try to prevent user errors by preventing casting between extensions w/ | ||
| // different storage types. Provide a tip on how to accomplish same outcome. | ||
| std::shared_ptr<Array> result; | ||
| if (array->type()->id() == Type::EXTENSION) { | ||
| if (!array->type()->Equals(out_ty)) { | ||
| return Status::TypeError("Casting from '" + array->type()->ToString() + | ||
| "' to different extension type '" + ext_ty.ToString() + | ||
| "' not permitted. One can first cast to the storage " | ||
| "type, then to the extension type."); | ||
| } | ||
| result = array; | ||
milesgranger marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } else { | ||
| ARROW_ASSIGN_OR_RAISE(result, Cast(*array, out_ty, options, ctx->exec_context())); | ||
| } | ||
| ExtensionArray extension(options.to_type.GetSharedPtr(), result); | ||
| out->value = std::move(extension.data()); | ||
| return Status::OK(); | ||
| } | ||
| std::shared_ptr<CastFunction> GetCastToExtension(std::string name) { | ||
| auto func = std::make_shared<CastFunction>(std::move(name), Type::EXTENSION); | ||
| for (Type::type in_ty : AllTypeIds()) { | ||
| DCHECK_OK( | ||
| func->AddKernel(in_ty, {InputType(in_ty)}, kOutputTargetType, CastToExtension)); | ||
| } | ||
| return func; | ||
| } | ||
| }; // namespace | ||
| std::vector<std::shared_ptr<CastFunction>> GetExtensionCasts() { | ||
milesgranger marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| auto func = GetCastToExtension("cast_extension"); | ||
jorisvandenbossche marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return {func}; | ||
| } | ||
| } // namespace internal | ||
| } // namespace compute | ||
| } // namespace arrow | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -225,7 +225,8 @@ TEST(Cast, CanCast) { | ||
| ExpectCanCast(smallint(), {int16()}); // cast storage | ||
| ExpectCanCast(smallint(), | ||
| kNumericTypes); // any cast which is valid for storage is supported | ||
| ExpectCannotCast(null(), {smallint()}); // FIXME missing common cast from null | ||
| ExpectCanCast(null(), {smallint()}); | ||
| ExpectCanCast(tinyint(), {smallint()}); // cast between compatible storage types | ||
| ExpectCanCast(date32(), {utf8(), large_utf8()}); | ||
| ExpectCanCast(date64(), {utf8(), large_utf8()}); | ||
| @@ -2728,6 +2729,13 @@ std::shared_ptr<Array> SmallintArrayFromJSON(const std::string& json_data) { | ||
| return MakeArray(ext_data); | ||
| } | ||
| std::shared_ptr<Array> TinyintArrayFromJSON(const std::string& json_data) { | ||
| auto arr = ArrayFromJSON(int8(), json_data); | ||
| auto ext_data = arr->data()->Copy(); | ||
| ext_data->type = tinyint(); | ||
| return MakeArray(ext_data); | ||
| } | ||
| TEST(Cast, ExtensionTypeToIntDowncast) { | ||
| auto smallint = std::make_shared<SmallintType>(); | ||
| ExtensionTypeGuard smallint_guard(smallint); | ||
| @@ -2765,6 +2773,68 @@ TEST(Cast, ExtensionTypeToIntDowncast) { | ||
| } | ||
| } | ||
| TEST(Cast, PrimitiveToExtension) { | ||
| { | ||
| auto primitive_array = ArrayFromJSON(uint8(), "[0, 1, 3]"); | ||
| auto extension_array = SmallintArrayFromJSON("[0, 1, 3]"); | ||
| CastOptions options; | ||
| options.to_type = smallint(); | ||
| CheckCast(primitive_array, extension_array, options); | ||
| } | ||
| { | ||
| CastOptions options; | ||
| options.to_type = smallint(); | ||
| CheckCastFails(ArrayFromJSON(utf8(), "[\"hello\"]"), options); | ||
| } | ||
| } | ||
milesgranger marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| TEST(Cast, ExtensionDictToExtension) { | ||
| auto extension_array = SmallintArrayFromJSON("[1, 2, 1]"); | ||
| auto indices_array = ArrayFromJSON(int32(), "[0, 1, 0]"); | ||
| ASSERT_OK_AND_ASSIGN(auto dict_array, | ||
| DictionaryArray::FromArrays(indices_array, extension_array)); | ||
| CastOptions options; | ||
| options.to_type = smallint(); | ||
| CheckCast(dict_array, extension_array, options); | ||
| } | ||
milesgranger marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| TEST(Cast, IntToExtensionTypeDowncast) { | ||
| CheckCast(ArrayFromJSON(uint8(), "[0, 100, 200, 1, 2]"), | ||
| SmallintArrayFromJSON("[0, 100, 200, 1, 2]")); | ||
| // int32 to Smallint(int16), with overflow | ||
| { | ||
| CastOptions options; | ||
| options.to_type = smallint(); | ||
| CheckCastFails(ArrayFromJSON(int32(), "[0, null, 32768, 1, 3]"), options); | ||
| options.allow_int_overflow = true; | ||
| CheckCast(ArrayFromJSON(int32(), "[0, null, 32768, 1, 3]"), | ||
| SmallintArrayFromJSON("[0, null, -32768, 1, 3]"), options); | ||
| } | ||
| // int32 to Smallint(int16), with underflow | ||
| { | ||
| CastOptions options; | ||
| options.to_type = smallint(); | ||
| CheckCastFails(ArrayFromJSON(int32(), "[0, null, -32769, 1, 3]"), options); | ||
| options.allow_int_overflow = true; | ||
| CheckCast(ArrayFromJSON(int32(), "[0, null, -32769, 1, 3]"), | ||
| SmallintArrayFromJSON("[0, null, 32767, 1, 3]"), options); | ||
| } | ||
| // Cannot cast between extension types when storage types differ | ||
| { | ||
| CastOptions options; | ||
| options.to_type = smallint(); | ||
| auto tiny_array = TinyintArrayFromJSON("[0, 1, 3]"); | ||
| ASSERT_NOT_OK(Cast(tiny_array, smallint(), options)); | ||
| } | ||
| } | ||
| TEST(Cast, DictTypeToAnotherDict) { | ||
| auto check_cast = [&](const std::shared_ptr<DataType>& in_type, | ||
| const std::shared_ptr<DataType>& out_type, | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.