You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "llama90 (via GitHub)" <gi...@apache.org> on 2023/12/22 13:18:03 UTC

[PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

llama90 opened a new pull request, #39353:
URL: https://github.com/apache/arrow/pull/39353

   <!--
   Thanks for opening a pull request!
   If this is your first pull request you can find detailed information on how 
   to contribute here:
     * [New Contributor's Guide](https://arrow.apache.org/docs/dev/developers/guide/step_by_step/pr_lifecycle.html#reviews-and-merge-of-the-pull-request)
     * [Contributing Overview](https://arrow.apache.org/docs/dev/developers/overview.html)
   
   
   If this is not a [minor PR](https://github.com/apache/arrow/blob/main/CONTRIBUTING.md#Minor-Fixes). Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose
   
   Opening GitHub issues ahead of time contributes to the [Openness](http://theapacheway.com/open/#:~:text=Openness%20allows%20new%20users%20the,must%20happen%20in%20the%20open.) of the Apache Arrow project.
   
   Then could you also rename the pull request title in the following format?
   
       GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   or
   
       MINOR: [${COMPONENT}] ${SUMMARY}
   
   In the case of PARQUET issues on JIRA the title also supports:
   
       PARQUET-${JIRA_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   -->
   
   ### Rationale for this change
   
   Remove legacy code
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ### What changes are included in this PR?
   
   Replace the legacy scalar CastTo implementation for List Scalar in test.
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ### Are these changes tested?
   
   Yes. It is passed by existing test cases.
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ### Are there any user-facing changes?
   
   No.
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please uncomment the line below and explain which changes are breaking.
   -->
   <!-- **This PR includes breaking changes to public APIs.** -->
   
   <!--
   Please uncomment the line below (and provide explanation) if the changes fix either (a) a security vulnerability, (b) a bug that caused incorrect or invalid data to be produced, or (c) a bug that causes a crash (even when the API contract is upheld). We use this to highlight fixes to issues that may affect users without their knowledge. For this reason, fixing bugs that cause errors don't count, since those are usually obvious.
   -->
   <!-- **This PR contains a "Critical Fix".** -->


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1438879932


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1178,10 +1196,10 @@ class TestListLikeScalar : public ::testing::Test {
     CheckListCast(
         scalar, fixed_size_list(value_->type(), static_cast<int32_t>(value_->length())));
 
-    CheckInvalidListCast(scalar, fixed_size_list(value_->type(), 5),
-                         "Cannot cast " + scalar.type->ToString() + " of length " +
-                             std::to_string(value_->length()) +
-                             " to fixed size list of length 5");
+    auto invalidCastType = fixed_size_list(value_->type(), 5);

Review Comment:
   Could you use `snake_case`?
   
   ```suggestion
       auto invalid_cast_type = fixed_size_list(value_->type(), 5);
   ```



##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1238,10 +1256,10 @@ TEST(TestMapScalar, Cast) {
   CheckListCast(scalar, large_list(key_value_type));
   CheckListCast(scalar, fixed_size_list(key_value_type, 2));
 
-  CheckInvalidListCast(scalar, fixed_size_list(key_value_type, 5),
-                       "Cannot cast " + scalar.type->ToString() + " of length " +
-                           std::to_string(value->length()) +
-                           " to fixed size list of length 5");
+  auto invalidCastType = fixed_size_list(key_value_type, 5);

Review Comment:
   ditto.



##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1087,11 +1088,28 @@ void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to
                       *checked_cast<const BaseListScalar&>(*cast_scalar).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
-                          const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}

Review Comment:
   Both of them just pass the returned value of `GetExpectedError()` to `CheckListCastError()`.
   I think that we don't need to split `GetExpectedError()` and `CheckListCastError()` (I think that we don't have code duplication without splitting to them):
   
   ```diff
   diff --git a/cpp/src/arrow/scalar_test.cc b/cpp/src/arrow/scalar_test.cc
   index 3170a00e34..fefa8b8b52 100644
   --- a/cpp/src/arrow/scalar_test.cc
   +++ b/cpp/src/arrow/scalar_test.cc
   @@ -1088,26 +1088,23 @@ void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to
                          *checked_cast<const BaseListScalar&>(*cast_scalar).value);
    }
    
   -std::tuple<StatusCode, std::string> GetExpectedError(
   -    const std::shared_ptr<DataType>& type,
   -    const std::shared_ptr<DataType>& invalidCastType) {
   -  if (type->id() == Type::FIXED_SIZE_LIST) {
   -    return std::make_tuple(
   -        StatusCode::TypeError,
   -        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
   -            " output list: " + invalidCastType->ToString());
   -  } else {
   -    return std::make_tuple(
   -        StatusCode::Invalid,
   -        "ListType can only be casted to FixedSizeListType if the lists are all the "
   -        "expected size.");
   -  }
   -}
   -
    template <typename ScalarType>
    void CheckListCastError(const ScalarType& scalar,
   -                        const std::shared_ptr<DataType>& to_type, const StatusCode code,
   -                        const std::string& expected_message) {
   +                        const std::shared_ptr<DataType>& from_type,
   +                        const std::shared_ptr<DataType>& to_type) {
   +  StatusCode code;
   +  std::string expected_message;
   +  if (from_type->id() == Type::FIXED_SIZE_LIST) {
   +    code = StatusCode::TypeError;
   +    expected_message =
   +        "Size of FixedSizeList is not the same. input list: " + from_type->ToString() +
   +        " output list: " + to_type->ToString();
   +  } else {
   +    code = StatusCode::Invalid;
   +    expected_message =
   +        "ListType can only be casted to FixedSizeListType if the lists are all the "
   +        "expected size.";
   +  }
      EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(code, ::testing::HasSubstr(expected_message),
                                               Cast(scalar, to_type));
    }
   @@ -1197,9 +1194,7 @@ class TestListLikeScalar : public ::testing::Test {
            scalar, fixed_size_list(value_->type(), static_cast<int32_t>(value_->length())));
    
        auto invalidCastType = fixed_size_list(value_->type(), 5);
   -    auto [expectedCode, expectedMessage] = GetExpectedError(type_, invalidCastType);
   -
   -    CheckListCastError(scalar, invalidCastType, expectedCode, expectedMessage);
   +    CheckListCastError(scalar, type_, invalidCastType);
      }
    
     protected:
   @@ -1257,9 +1252,7 @@ TEST(TestMapScalar, Cast) {
      CheckListCast(scalar, fixed_size_list(key_value_type, 2));
    
      auto invalidCastType = fixed_size_list(key_value_type, 5);
   -  auto [expectedCode, expectedMessage] = GetExpectedError(scalar.type, invalidCastType);
   -
   -  CheckListCastError(scalar, invalidCastType, expectedCode, expectedMessage);
   +  CheckListCastError(scalar, scalar.type, invalidCastType);
    }
    
    TEST(TestStructScalar, FieldAccess) {
   ```



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1436210085


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1077,21 +1077,38 @@ std::shared_ptr<DataType> MakeListType<FixedSizeListType>(
 
 template <typename ScalarType>
 void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to_type) {
-  EXPECT_OK_AND_ASSIGN(auto cast_scalar, scalar.CastTo(to_type));
-  ASSERT_OK(cast_scalar->ValidateFull());
-  ASSERT_EQ(*cast_scalar->type, *to_type);
+  EXPECT_OK_AND_ASSIGN(auto cast_scalar, Cast(scalar, to_type));
+  ASSERT_OK(cast_scalar.scalar()->ValidateFull());
+  ASSERT_EQ(*cast_scalar.scalar()->type, *to_type);
 
-  ASSERT_EQ(scalar.is_valid, cast_scalar->is_valid);
+  ASSERT_EQ(scalar.is_valid, cast_scalar.scalar()->is_valid);
   ASSERT_TRUE(scalar.is_valid);
   ASSERT_ARRAYS_EQUAL(*scalar.value,
-                      *checked_cast<const BaseListScalar&>(*cast_scalar).value);
+                      *checked_cast<const BaseListScalar&>(*cast_scalar.scalar()).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}
+
+template <typename ScalarType>
+void CheckInvalidListCast(const ScalarType& scalar,
+                          const std::shared_ptr<DataType>& to_type, const StatusCode code,
                           const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(code, ::testing::HasSubstr(expected_message),

Review Comment:
   Yes. You are correct. In the current situation, as you mentioned, both `Invalid` and `TypeError` can occur. I think `CheckListCastError` or `VerifyListCastFailure` would be more appropriate. What do you think? 



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1439008535


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1087,11 +1088,28 @@ void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to
                       *checked_cast<const BaseListScalar&>(*cast_scalar).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
-                          const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}

Review Comment:
   Yes, that argument is redundant. Removed it. 



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1438789787


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1087,11 +1088,28 @@ void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to
                       *checked_cast<const BaseListScalar&>(*cast_scalar).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
-                          const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}

Review Comment:
   This function(`CheckListCastError`) called from two cases as `TEST(TestMapScalar, Cast)` and  `void TestCast()` for `TYPED_TEST(TestListLikeScalar, Cast)`. That's why I added the function to avoid code duplication.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1438454366


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1077,21 +1077,38 @@ std::shared_ptr<DataType> MakeListType<FixedSizeListType>(
 
 template <typename ScalarType>
 void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to_type) {
-  EXPECT_OK_AND_ASSIGN(auto cast_scalar, scalar.CastTo(to_type));
-  ASSERT_OK(cast_scalar->ValidateFull());
-  ASSERT_EQ(*cast_scalar->type, *to_type);
+  EXPECT_OK_AND_ASSIGN(auto cast_scalar, Cast(scalar, to_type));
+  ASSERT_OK(cast_scalar.scalar()->ValidateFull());
+  ASSERT_EQ(*cast_scalar.scalar()->type, *to_type);
 
-  ASSERT_EQ(scalar.is_valid, cast_scalar->is_valid);
+  ASSERT_EQ(scalar.is_valid, cast_scalar.scalar()->is_valid);
   ASSERT_TRUE(scalar.is_valid);
   ASSERT_ARRAYS_EQUAL(*scalar.value,
-                      *checked_cast<const BaseListScalar&>(*cast_scalar).value);
+                      *checked_cast<const BaseListScalar&>(*cast_scalar.scalar()).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}
+
+template <typename ScalarType>
+void CheckInvalidListCast(const ScalarType& scalar,
+                          const std::shared_ptr<DataType>& to_type, const StatusCode code,
                           const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(code, ::testing::HasSubstr(expected_message),

Review Comment:
   @kou Hello. When you have free time, could you please review it? Thank you :)



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #39353:
URL: https://github.com/apache/arrow/pull/39353#issuecomment-1867678699

   :warning: GitHub issue #39051 **has been automatically assigned in GitHub** to PR creator.


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou merged PR #39353:
URL: https://github.com/apache/arrow/pull/39353


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1438743978


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1087,11 +1088,28 @@ void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to
                       *checked_cast<const BaseListScalar&>(*cast_scalar).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
-                          const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}

Review Comment:
   How about moving this code to `CheckListCastError()`?
   Why do you want to split this function and `CheckListCastError()`?



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1438744065


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1077,21 +1077,38 @@ std::shared_ptr<DataType> MakeListType<FixedSizeListType>(
 
 template <typename ScalarType>
 void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to_type) {
-  EXPECT_OK_AND_ASSIGN(auto cast_scalar, scalar.CastTo(to_type));
-  ASSERT_OK(cast_scalar->ValidateFull());
-  ASSERT_EQ(*cast_scalar->type, *to_type);
+  EXPECT_OK_AND_ASSIGN(auto cast_scalar, Cast(scalar, to_type));
+  ASSERT_OK(cast_scalar.scalar()->ValidateFull());
+  ASSERT_EQ(*cast_scalar.scalar()->type, *to_type);
 
-  ASSERT_EQ(scalar.is_valid, cast_scalar->is_valid);
+  ASSERT_EQ(scalar.is_valid, cast_scalar.scalar()->is_valid);
   ASSERT_TRUE(scalar.is_valid);
   ASSERT_ARRAYS_EQUAL(*scalar.value,
-                      *checked_cast<const BaseListScalar&>(*cast_scalar).value);
+                      *checked_cast<const BaseListScalar&>(*cast_scalar.scalar()).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}
+
+template <typename ScalarType>
+void CheckInvalidListCast(const ScalarType& scalar,
+                          const std::shared_ptr<DataType>& to_type, const StatusCode code,
                           const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(code, ::testing::HasSubstr(expected_message),

Review Comment:
   Sorry! I reviewed this!



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1438901186


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1178,10 +1196,10 @@ class TestListLikeScalar : public ::testing::Test {
     CheckListCast(
         scalar, fixed_size_list(value_->type(), static_cast<int32_t>(value_->length())));
 
-    CheckInvalidListCast(scalar, fixed_size_list(value_->type(), 5),
-                         "Cannot cast " + scalar.type->ToString() + " of length " +
-                             std::to_string(value_->length()) +
-                             " to fixed size list of length 5");
+    auto invalidCastType = fixed_size_list(value_->type(), 5);

Review Comment:
   Fixed it.



##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1238,10 +1256,10 @@ TEST(TestMapScalar, Cast) {
   CheckListCast(scalar, large_list(key_value_type));
   CheckListCast(scalar, fixed_size_list(key_value_type, 2));
 
-  CheckInvalidListCast(scalar, fixed_size_list(key_value_type, 5),
-                       "Cannot cast " + scalar.type->ToString() + " of length " +
-                           std::to_string(value->length()) +
-                           " to fixed size list of length 5");
+  auto invalidCastType = fixed_size_list(key_value_type, 5);

Review Comment:
   too.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #39353:
URL: https://github.com/apache/arrow/pull/39353#issuecomment-1873398899

   After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 8a9f877896644ef1629136e8428a2c21bce64ae3.
   
   There were no benchmark performance regressions. 🎉
   
   The [full Conbench report](https://github.com/apache/arrow/runs/20074166667) has more details. It also includes information about 2 possible false positives for unstable benchmarks that are known to sometimes produce them.


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1439006759


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1087,11 +1088,28 @@ void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to
                       *checked_cast<const BaseListScalar&>(*cast_scalar).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
-                          const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}

Review Comment:
   Ah, sorry. Can we use `Scalar::type` instead of passing `from_type`?
   
   ```diff
   diff --git a/cpp/src/arrow/scalar_test.cc b/cpp/src/arrow/scalar_test.cc
   index 654e0510c6..8592765adc 100644
   --- a/cpp/src/arrow/scalar_test.cc
   +++ b/cpp/src/arrow/scalar_test.cc
   @@ -1090,14 +1090,13 @@ void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to
    
    template <typename ScalarType>
    void CheckListCastError(const ScalarType& scalar,
   -                        const std::shared_ptr<DataType>& from_type,
                            const std::shared_ptr<DataType>& to_type) {
      StatusCode code;
      std::string expected_message;
   -  if (from_type->id() == Type::FIXED_SIZE_LIST) {
   +  if (scalar.type->id() == Type::FIXED_SIZE_LIST) {
        code = StatusCode::TypeError;
        expected_message =
   -        "Size of FixedSizeList is not the same. input list: " + from_type->ToString() +
   +        "Size of FixedSizeList is not the same. input list: " + scalar.type->ToString() +
            " output list: " + to_type->ToString();
      } else {
        code = StatusCode::Invalid;
   @@ -1195,7 +1194,7 @@ class TestListLikeScalar : public ::testing::Test {
            scalar, fixed_size_list(value_->type(), static_cast<int32_t>(value_->length())));
    
        auto invalid_cast_type = fixed_size_list(value_->type(), 5);
   -    CheckListCastError(scalar, type_, invalid_cast_type);
   +    CheckListCastError(scalar, invalid_cast_type);
      }
    
     protected:
   @@ -1253,7 +1252,7 @@ TEST(TestMapScalar, Cast) {
      CheckListCast(scalar, fixed_size_list(key_value_type, 2));
    
      auto invalid_cast_type = fixed_size_list(key_value_type, 5);
   -  CheckListCastError(scalar, scalar.type, invalid_cast_type);
   +  CheckListCastError(scalar, invalid_cast_type);
    }
    
    TEST(TestStructScalar, FieldAccess) {
   ```



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1438901160


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1087,11 +1088,28 @@ void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to
                       *checked_cast<const BaseListScalar&>(*cast_scalar).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
-                          const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}

Review Comment:
   You are right. I changed.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1436210085


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1077,21 +1077,38 @@ std::shared_ptr<DataType> MakeListType<FixedSizeListType>(
 
 template <typename ScalarType>
 void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to_type) {
-  EXPECT_OK_AND_ASSIGN(auto cast_scalar, scalar.CastTo(to_type));
-  ASSERT_OK(cast_scalar->ValidateFull());
-  ASSERT_EQ(*cast_scalar->type, *to_type);
+  EXPECT_OK_AND_ASSIGN(auto cast_scalar, Cast(scalar, to_type));
+  ASSERT_OK(cast_scalar.scalar()->ValidateFull());
+  ASSERT_EQ(*cast_scalar.scalar()->type, *to_type);
 
-  ASSERT_EQ(scalar.is_valid, cast_scalar->is_valid);
+  ASSERT_EQ(scalar.is_valid, cast_scalar.scalar()->is_valid);
   ASSERT_TRUE(scalar.is_valid);
   ASSERT_ARRAYS_EQUAL(*scalar.value,
-                      *checked_cast<const BaseListScalar&>(*cast_scalar).value);
+                      *checked_cast<const BaseListScalar&>(*cast_scalar.scalar()).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}
+
+template <typename ScalarType>
+void CheckInvalidListCast(const ScalarType& scalar,
+                          const std::shared_ptr<DataType>& to_type, const StatusCode code,
                           const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(code, ::testing::HasSubstr(expected_message),

Review Comment:
   Yes. You are correct. In the current situation, as you mentioned, both `Invalid` and `TypeError` can occur. I think `CheckListCastError` or `VerifyListCastFailure` would be more appropriate. What do you think? (First, I chose `CheckListCastError`.)



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1436197539


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1077,21 +1077,38 @@ std::shared_ptr<DataType> MakeListType<FixedSizeListType>(
 
 template <typename ScalarType>
 void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to_type) {
-  EXPECT_OK_AND_ASSIGN(auto cast_scalar, scalar.CastTo(to_type));
-  ASSERT_OK(cast_scalar->ValidateFull());
-  ASSERT_EQ(*cast_scalar->type, *to_type);
+  EXPECT_OK_AND_ASSIGN(auto cast_scalar, Cast(scalar, to_type));
+  ASSERT_OK(cast_scalar.scalar()->ValidateFull());
+  ASSERT_EQ(*cast_scalar.scalar()->type, *to_type);
 
-  ASSERT_EQ(scalar.is_valid, cast_scalar->is_valid);
+  ASSERT_EQ(scalar.is_valid, cast_scalar.scalar()->is_valid);
   ASSERT_TRUE(scalar.is_valid);
   ASSERT_ARRAYS_EQUAL(*scalar.value,
-                      *checked_cast<const BaseListScalar&>(*cast_scalar).value);
+                      *checked_cast<const BaseListScalar&>(*cast_scalar.scalar()).value);
 }
 
-void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
+std::tuple<StatusCode, std::string> GetExpectedError(
+    const std::shared_ptr<DataType>& type,
+    const std::shared_ptr<DataType>& invalidCastType) {
+  if (type->id() == Type::FIXED_SIZE_LIST) {
+    return std::make_tuple(
+        StatusCode::TypeError,
+        "Size of FixedSizeList is not the same. input list: " + type->ToString() +
+            " output list: " + invalidCastType->ToString());
+  } else {
+    return std::make_tuple(
+        StatusCode::Invalid,
+        "ListType can only be casted to FixedSizeListType if the lists are all the "
+        "expected size.");
+  }
+}
+
+template <typename ScalarType>
+void CheckInvalidListCast(const ScalarType& scalar,
+                          const std::shared_ptr<DataType>& to_type, const StatusCode code,
                           const std::string& expected_message) {
-  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
-                                           ::testing::HasSubstr(expected_message),
-                                           scalar.CastTo(to_type));
+  EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(code, ::testing::HasSubstr(expected_message),

Review Comment:
   It's strange that `CheckInvalidListCast()` may not expect `StatusCode::Invalid`.
   Can we use better function name?



##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1077,21 +1077,38 @@ std::shared_ptr<DataType> MakeListType<FixedSizeListType>(
 
 template <typename ScalarType>
 void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to_type) {
-  EXPECT_OK_AND_ASSIGN(auto cast_scalar, scalar.CastTo(to_type));
-  ASSERT_OK(cast_scalar->ValidateFull());
-  ASSERT_EQ(*cast_scalar->type, *to_type);
+  EXPECT_OK_AND_ASSIGN(auto cast_scalar, Cast(scalar, to_type));

Review Comment:
   How about using `cast_scalar_datum` for this variable? `cast_scalar.scalar()` is strange.
   
   ```suggestion
     EXPECT_OK_AND_ASSIGN(auto cast_scalar_datum, Cast(scalar, to_type));
     auto cast_scalar = cast_scalar_datum.scalar();
   ```



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test [arrow]

Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on code in PR #39353:
URL: https://github.com/apache/arrow/pull/39353#discussion_r1436207418


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1077,21 +1077,38 @@ std::shared_ptr<DataType> MakeListType<FixedSizeListType>(
 
 template <typename ScalarType>
 void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to_type) {
-  EXPECT_OK_AND_ASSIGN(auto cast_scalar, scalar.CastTo(to_type));
-  ASSERT_OK(cast_scalar->ValidateFull());
-  ASSERT_EQ(*cast_scalar->type, *to_type);
+  EXPECT_OK_AND_ASSIGN(auto cast_scalar, Cast(scalar, to_type));

Review Comment:
   I changed it.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org