pitrou commented on code in PR #14106: URL: https://github.com/apache/arrow/pull/14106#discussion_r974401297
########## cpp/src/arrow/compute/kernels/scalar_cast_extension.cc: ########## @@ -0,0 +1,66 @@ +// 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 { + +Status CastToExtension(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { + const CastOptions& options = checked_cast<const CastState*>(ctx->state())->options; + auto out_ty = static_cast<const ExtensionType&>(*options.to_type.type).storage_type(); + + DCHECK(batch[0].is_array()); + std::shared_ptr<Array> array = batch[0].array.ToArray(); + std::shared_ptr<Array> result; + + RETURN_NOT_OK(Cast(*array, out_ty, options, + ctx->exec_context()) + .Value(&result)); + 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); + // TODO(milesgranger): Better way to add all types? `AllTypeIds` exists in tests... Review Comment: Instead of passing an explicit type or type id, you should probably instead define a `TypeMatcher` that allows non-extension types. You can find some examples below: https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernel.cc#L82 ########## cpp/src/arrow/compute/kernels/scalar_cast_extension.cc: ########## @@ -0,0 +1,66 @@ +// 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 { + +Status CastToExtension(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { + const CastOptions& options = checked_cast<const CastState*>(ctx->state())->options; + auto out_ty = static_cast<const ExtensionType&>(*options.to_type.type).storage_type(); + + DCHECK(batch[0].is_array()); + std::shared_ptr<Array> array = batch[0].array.ToArray(); + std::shared_ptr<Array> result; + + RETURN_NOT_OK(Cast(*array, out_ty, options, + ctx->exec_context()) + .Value(&result)); + 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); + // TODO(milesgranger): Better way to add all types? `AllTypeIds` exists in tests... + for (auto types : {IntTypes(), FloatingPointTypes(), StringTypes(), BinaryTypes()}) { + for (auto in_ty : types) { + DCHECK_OK(func->AddKernel(in_ty->id(), {in_ty}, + kOutputTargetType, CastToExtension)); + } + } + DCHECK_OK(func->AddKernel(Type::DICTIONARY, {InputType(Type::DICTIONARY)}, + kOutputTargetType, CastToExtension)); + return func; +} + +std::vector<std::shared_ptr<CastFunction>> GetExtensionCasts() { Review Comment: Everything except this function should probably be in the "anonymous namespace" (`namespace {`). The anonymous namespace makes everything inside it (including classes, methods, etc.) static, therefore not exposed globally by the linker. ########## cpp/src/arrow/compute/kernels/scalar_cast_test.cc: ########## @@ -2765,6 +2765,26 @@ 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); +} Review Comment: Also add a test similar to `ExtensionTypeToIntDowncast`, but downcasting from primitive to extension? (to check forwarding of cast options) ########## cpp/src/arrow/compute/kernels/scalar_cast_internal.h: ########## @@ -43,6 +43,7 @@ struct CastFunctor< }; Status CastFromExtension(KernelContext* ctx, const ExecSpan& batch, ExecResult* out); +Status CastToExtension(KernelContext* ctx, const ExecSpan& batch, ExecResult* out); Review Comment: I'm not sure it's useful to expose this function as it's not called directly anywhere. ########## cpp/src/arrow/compute/kernels/scalar_cast_test.cc: ########## @@ -2765,6 +2765,26 @@ 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); +} + +TEST(Cast, DictTypeToExtension) { + 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); +} + Review Comment: Should probably add a test of failed casts? For example an extension to another extension, and a case where it's impossible to cast from the source to the extension's storage type. ########## cpp/src/arrow/compute/kernels/scalar_cast_test.cc: ########## @@ -2765,6 +2765,26 @@ 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); +} + +TEST(Cast, DictTypeToExtension) { Review Comment: ```suggestion TEST(Cast, ExtensionDictToExtension) { ``` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
