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

[C++] Support casting from storage type to extension type #30057

Closed
asfimport opened this issue Oct 28, 2021 · 4 comments
Closed

[C++] Support casting from storage type to extension type #30057

asfimport opened this issue Oct 28, 2021 · 4 comments

Comments

@asfimport
Copy link

asfimport commented Oct 28, 2021

We already support casting an extension array to its storage type (ARROW-7858):

>>> from pyarrow.tests.test_extension_type import IntegerType
>>> arr = pa.array([1, 2, 3], pa.int64())
>>> arr_ext = pa.ExtensionArray.from_storage(IntegerType(), arr)
>>> arr_ext.cast(arr.type)
Out[29]: 
<pyarrow.lib.Int64Array object at 0x7f7b0d4739a0>
[
  1,
  2,
  3
]

but we don't support the cast the other way around from storage type to extension type:

>>> arr.cast(arr_ext.type)
...
ArrowNotImplementedError: Unsupported cast from int64 to extension<arrow.py_extension_type<IntegerType>> (no available cast function for target type)
../src/arrow/compute/cast.cc:119  GetCastFunctionInternal(cast_options->to_type, args[0].type().get())

Reporter: Joris Van den Bossche / @jorisvandenbossche
Assignee: Miles Granger / @milesgranger

Related issues:

PRs and other links:

Note: This issue was originally created as ARROW-14500. Please see the migration documentation for further details.

@asfimport
Copy link
Author

Will Ayd / @WillAyd:
Did a little research here. I see the casting from an extension type is handled here:

Status CastFromExtension(KernelContext* ctx, const ExecBatch& batch, Datum* out) {

So the other way around could be pretty similar. However, I'm wondering if a cast to an extension type is truly possible? When coming from an extension type the built-in type can be cast from the extension's physical storage. But would going the other way around would require a creation of a new ExtensionArray instance that populates some of the additional methods?

@asfimport
Copy link
Author

Joris Van den Bossche / @jorisvandenbossche:

require a creation of a new ExtensionArray instance that populates some of the additional methods?

The ExtensionType instance can create such an array, see eg

/// \brief Wrap the given storage array as an extension array
static std::shared_ptr<Array> WrapArray(const std::shared_ptr<DataType>& ext_type,
const std::shared_ptr<Array>& storage);
(ExtensionType::MakeArray is a required method for extension authors to implement)

@asfimport
Copy link
Author

Joris Van den Bossche / @jorisvandenbossche:
This is being addressed in #14106

@asfimport
Copy link
Author

Joris Van den Bossche / @jorisvandenbossche:
Resolved by #14106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants