kszucs commented on a change in pull request #7519: URL: https://github.com/apache/arrow/pull/7519#discussion_r450317608
########## File path: cpp/src/arrow/scalar_test.cc ########## @@ -451,6 +565,127 @@ TEST(TestStructScalar, FieldAccess) { ASSERT_RAISES(Invalid, abc.field(5).status()); ASSERT_RAISES(Invalid, abc.field("c").status()); + + ASSERT_OK_AND_ASSIGN(auto d, abc.field("d")); + ASSERT_TRUE(d->Equals(MakeNullScalar(int64()))); +} + +TEST(TestDictionaryScalar, Basics) { + auto ty = dictionary(int8(), utf8()); + auto dict = ArrayFromJSON(utf8(), "[\"alpha\", \"beta\", \"gamma\"]"); + + DictionaryScalar::ValueType alpha; + ASSERT_OK_AND_ASSIGN(alpha.index, MakeScalar(int8(), 0)); + alpha.dictionary = dict; + + DictionaryScalar::ValueType gamma; + ASSERT_OK_AND_ASSIGN(gamma.index, MakeScalar(int8(), 2)); + gamma.dictionary = dict; + + auto scalar_null = MakeNullScalar(ty); + auto scalar_alpha = DictionaryScalar(alpha, ty); + auto scalar_gamma = DictionaryScalar(gamma, ty); + + ASSERT_OK_AND_ASSIGN( + auto encoded_null, + checked_cast<const DictionaryScalar&>(*scalar_null).GetEncodedValue()); + ASSERT_TRUE(encoded_null->Equals(MakeNullScalar(utf8()))); + + ASSERT_OK_AND_ASSIGN( + auto encoded_alpha, + checked_cast<const DictionaryScalar&>(scalar_alpha).GetEncodedValue()); + ASSERT_TRUE(encoded_alpha->Equals(MakeScalar("alpha"))); + + ASSERT_OK_AND_ASSIGN( + auto encoded_gamma, + checked_cast<const DictionaryScalar&>(scalar_gamma).GetEncodedValue()); + ASSERT_TRUE(encoded_gamma->Equals(MakeScalar("gamma"))); + + // test Array.GetScalar + DictionaryArray arr(ty, ArrayFromJSON(int8(), "[2, 0, 1, null]"), dict); + ASSERT_OK_AND_ASSIGN(auto first, arr.GetScalar(0)); + ASSERT_OK_AND_ASSIGN(auto second, arr.GetScalar(1)); + ASSERT_OK_AND_ASSIGN(auto last, arr.GetScalar(3)); + + ASSERT_TRUE(first->Equals(scalar_gamma)); + ASSERT_TRUE(second->Equals(scalar_alpha)); + ASSERT_TRUE(last->Equals(scalar_null)); +} + +TEST(TestSparseUnionScalar, Basics) { + auto ty = sparse_union({field("string", utf8()), field("number", uint64())}); + + auto alpha = MakeScalar("alpha"); + auto beta = MakeScalar("beta"); + ASSERT_OK_AND_ASSIGN(auto two, MakeScalar(uint64(), 2)); + + auto scalar_alpha = SparseUnionScalar(alpha, ty); + auto scalar_beta = SparseUnionScalar(beta, ty); + auto scalar_two = SparseUnionScalar(two, ty); + + // test Array.GetScalar + std::vector<std::shared_ptr<Array>> children{ + ArrayFromJSON(utf8(), "[\"alpha\", \"\", \"beta\", null, \"gamma\"]"), + ArrayFromJSON(uint64(), "[1, 2, 11, 22, null]")}; + + auto type_ids = ArrayFromJSON(int8(), "[0, 1, 0, 0, 1]"); + SparseUnionArray arr(ty, 5, children, type_ids->data()->buffers[1]); + ASSERT_OK(arr.ValidateFull()); + + ASSERT_OK_AND_ASSIGN(auto first, arr.GetScalar(0)); + ASSERT_TRUE(first->Equals(scalar_alpha)); + + ASSERT_OK_AND_ASSIGN(auto second, arr.GetScalar(1)); + ASSERT_TRUE(second->Equals(scalar_two)); + + ASSERT_OK_AND_ASSIGN(auto third, arr.GetScalar(2)); + ASSERT_TRUE(third->Equals(scalar_beta)); + + ASSERT_OK_AND_ASSIGN(auto fourth, arr.GetScalar(3)); + ASSERT_TRUE(fourth->Equals(MakeNullScalar(ty))); Review comment: I think `GetScalar()` should return a Scalar with the same type as the Array, so in case of unions with the union type rather than the type selected by the union type id (the union scalar's value is another scalar with the type id). I don't have a strong opinion on this though. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org