milesgranger commented on code in PR #14106: URL: https://github.com/apache/arrow/pull/14106#discussion_r977319194
########## cpp/src/arrow/compute/kernels/scalar_cast_test.cc: ########## @@ -2765,6 +2772,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); + } +} + +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); +} + +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 + { + CastOptions options; + options.to_type = smallint(); + auto tiny_array = TinyintArrayFromJSON("[0, 1, 3]"); + ASSERT_NOT_OK(Cast(tiny_array, smallint(), options)); + } Review Comment: Updated now, both C++ and Python tests for casting between compatible extension storage types. -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org