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/24 07:21:54 UTC

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

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

   <!--
   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 Dictionary 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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -77,17 +85,23 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   return Status::OK();
 }
 
-std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
-  auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
-
-  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get());
-  ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastToDictionary);
+template <typename SrcType>
+void AddDictionaryCast(CastFunction* func) {
+  ScalarKernel kernel;
+  kernel.exec = CastToDictionary;
+  kernel.signature =
+      KernelSignature::Make({InputType(SrcType::type_id)}, kOutputTargetType);
   kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE;
-  kernel.mem_allocation = MemAllocation::NO_PREALLOCATE;

Review Comment:
   Can we keep this for `DictionaryType`?



##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1479,11 +1479,12 @@ TEST(TestDictionaryScalar, Cast) {
       auto alpha =
           dict->IsValid(i) ? MakeScalar(dict->GetString(i)) : MakeNullScalar(utf8());
       // Cast string to dict(..., string)
-      ASSERT_OK_AND_ASSIGN(auto cast_alpha, alpha->CastTo(ty));
-      ASSERT_OK(cast_alpha->ValidateFull());
+      ASSERT_OK_AND_ASSIGN(auto cast_alpha, Cast(alpha, ty));
+      const auto& scalar = cast_alpha.scalar();

Review Comment:
   How about keeping `cast_alpha` name? It's more meaningful than `scalar`.
   
   ```suggestion
         ASSERT_OK_AND_ASSIGN(auto cast_alpha_datum, Cast(alpha, ty));
         const auto& cast_alpha = cast_alpha_datum.scalar();
   ```



##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -44,6 +44,14 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   }
 
   std::shared_ptr<ArrayData> in_array = batch[0].array.ToArrayData();
+
+  // If the input type is STRING, it is first encoded as a dictionary to facilitate
+  // processing. This approach allows the subsequent code to uniformly handle STRING
+  // inputs as if they were originally provided in dictionary format. Encoding as a
+  // dictionary helps in reusing the same logic for dictionary operations.
+  if (batch[0].type()->id() == Type::STRING) {
+    in_array = DictionaryEncode(batch[0].array.ToArrayData())->array();

Review Comment:
   Can we use `in_array` here?
   
   ```suggestion
       in_array = DictionaryEncode(in_array)->array();
   ```



##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -77,17 +85,23 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   return Status::OK();
 }
 
-std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
-  auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
-
-  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get());
-  ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastToDictionary);
+template <typename SrcType>
+void AddDictionaryCast(CastFunction* func) {
+  ScalarKernel kernel;
+  kernel.exec = CastToDictionary;
+  kernel.signature =
+      KernelSignature::Make({InputType(SrcType::type_id)}, kOutputTargetType);
   kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE;
-  kernel.mem_allocation = MemAllocation::NO_PREALLOCATE;
+  DCHECK_OK(func->AddKernel(SrcType::type_id, std::move(kernel)));
+}
 
-  DCHECK_OK(func->AddKernel(Type::DICTIONARY, std::move(kernel)));
+std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
+  auto cast_dict = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
+  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, cast_dict.get());
+  AddDictionaryCast<DictionaryType>(cast_dict.get());
+  AddDictionaryCast<StringType>(cast_dict.get());

Review Comment:
   Can we use `arrow::StringTypes()` for all string types (`StringType` and `LargeStringType`)?
   
   (We may want to add support for `BinaryType` and `LargeBinaryType` as a separated PR.)



-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -77,17 +85,23 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   return Status::OK();
 }
 
-std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
-  auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
-
-  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get());
-  ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastToDictionary);
+template <typename SrcType>
+void AddDictionaryCast(CastFunction* func) {
+  ScalarKernel kernel;
+  kernel.exec = CastToDictionary;
+  kernel.signature =
+      KernelSignature::Make({InputType(SrcType::type_id)}, kOutputTargetType);
   kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE;
-  kernel.mem_allocation = MemAllocation::NO_PREALLOCATE;
+  DCHECK_OK(func->AddKernel(SrcType::type_id, std::move(kernel)));
+}
 
-  DCHECK_OK(func->AddKernel(Type::DICTIONARY, std::move(kernel)));
+std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
+  auto cast_dict = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
+  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, cast_dict.get());
+  AddDictionaryCast<DictionaryType>(cast_dict.get());
+  AddDictionaryCast<StringType>(cast_dict.get());

Review Comment:
   I understand. You're right. Can we address that part as a known issue in a new PR? I would like to first finish the work on removing the Legacy Cast.



-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -77,17 +85,24 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   return Status::OK();
 }
 
-std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
-  auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
-
-  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get());
-  ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastToDictionary);
+template <typename SrcType>
+void AddDictionaryCast(CastFunction* func) {
+  ScalarKernel kernel;
+  kernel.exec = CastToDictionary;
+  kernel.signature =
+      KernelSignature::Make({InputType(SrcType::type_id)}, kOutputTargetType);

Review Comment:
   How about using the `ScalarKernel()` constructor like the original code?
   
   ```cpp
     ScalarKernel kernel({InputType(SrcType::type_id)}, kOutputTargetType, CastToDictionary);
   ```



-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -77,17 +85,24 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   return Status::OK();
 }
 
-std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
-  auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
-
-  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get());
-  ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastToDictionary);
+template <typename SrcType>
+void AddDictionaryCast(CastFunction* func) {
+  ScalarKernel kernel;
+  kernel.exec = CastToDictionary;
+  kernel.signature =
+      KernelSignature::Make({InputType(SrcType::type_id)}, kOutputTargetType);

Review Comment:
   Oh. I didn't notice it.
   The constructor is a shortcut of these existing codes: https://github.com/apache/arrow/blob/1c1fa746f8d1266f9210d9c0e1bbab074c5dd4e0/cpp/src/arrow/compute/kernel.h#L496-L498
   So I think the constructor is recommended.
   
   > Would it be advisable to open a new issue for these changes if they are recommended?
   
   Yes, please. We can discuss there what API is recommended.



-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on PR #39362:
URL: https://github.com/apache/arrow/pull/39362#issuecomment-1874257206

   @kou Would it be possible for you to review this PR? I anticipate that once this PR and the GLib PR are merged, we will be able to proceed with the task of deprecating `CastTo`. #39182 
   
   Thank you as always.


-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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

   After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 37a8bf04bc713858a5b247d4424c1e8505e61947.
   
   There were no benchmark performance regressions. 🎉
   
   The [full Conbench report](https://github.com/apache/arrow/runs/20242572738) has more details. It also includes information about 5 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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -77,17 +85,24 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   return Status::OK();
 }
 
-std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
-  auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
-
-  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get());
-  ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastToDictionary);
+template <typename SrcType>
+void AddDictionaryCast(CastFunction* func) {
+  ScalarKernel kernel;
+  kernel.exec = CastToDictionary;
+  kernel.signature =
+      KernelSignature::Make({InputType(SrcType::type_id)}, kOutputTargetType);

Review Comment:
   I have made changes and identified the following code snippets. Is it recommended to use the `ScalarKernel()` constructor?
   
   https://github.com/apache/arrow/blob/1c1fa746f8d1266f9210d9c0e1bbab074c5dd4e0/cpp/src/arrow/compute/kernels/scalar_cast_nested.cc#L148-L151
   
   https://github.com/apache/arrow/blob/1c1fa746f8d1266f9210d9c0e1bbab074c5dd4e0/cpp/src/arrow/compute/kernels/scalar_cast_temporal.cc#L495-L497
   
   ...
   
   Would it be advisable to open a new issue for these changes if they are recommended?



-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -77,17 +85,23 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   return Status::OK();
 }
 
-std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
-  auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
-
-  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get());
-  ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastToDictionary);
+template <typename SrcType>
+void AddDictionaryCast(CastFunction* func) {
+  ScalarKernel kernel;
+  kernel.exec = CastToDictionary;
+  kernel.signature =
+      KernelSignature::Make({InputType(SrcType::type_id)}, kOutputTargetType);
   kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE;
-  kernel.mem_allocation = MemAllocation::NO_PREALLOCATE;
+  DCHECK_OK(func->AddKernel(SrcType::type_id, std::move(kernel)));
+}
 
-  DCHECK_OK(func->AddKernel(Type::DICTIONARY, std::move(kernel)));
+std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
+  auto cast_dict = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
+  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, cast_dict.get());
+  AddDictionaryCast<DictionaryType>(cast_dict.get());
+  AddDictionaryCast<StringType>(cast_dict.get());

Review Comment:
   It makes sense.



-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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

   :warning: GitHub issue #39049 **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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -77,17 +85,23 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   return Status::OK();
 }
 
-std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
-  auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
-
-  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get());
-  ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastToDictionary);
+template <typename SrcType>
+void AddDictionaryCast(CastFunction* func) {
+  ScalarKernel kernel;
+  kernel.exec = CastToDictionary;
+  kernel.signature =
+      KernelSignature::Make({InputType(SrcType::type_id)}, kOutputTargetType);
   kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE;
-  kernel.mem_allocation = MemAllocation::NO_PREALLOCATE;
+  DCHECK_OK(func->AddKernel(SrcType::type_id, std::move(kernel)));
+}
 
-  DCHECK_OK(func->AddKernel(Type::DICTIONARY, std::move(kernel)));
+std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
+  auto cast_dict = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
+  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, cast_dict.get());
+  AddDictionaryCast<DictionaryType>(cast_dict.get());
+  AddDictionaryCast<StringType>(cast_dict.get());

Review Comment:
   I created the issue.
   
   - #39463



-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -77,17 +85,23 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   return Status::OK();
 }
 
-std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() {
-  auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY);
-
-  AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get());
-  ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastToDictionary);
+template <typename SrcType>
+void AddDictionaryCast(CastFunction* func) {
+  ScalarKernel kernel;
+  kernel.exec = CastToDictionary;
+  kernel.signature =
+      KernelSignature::Make({InputType(SrcType::type_id)}, kOutputTargetType);
   kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE;
-  kernel.mem_allocation = MemAllocation::NO_PREALLOCATE;

Review Comment:
   too.



##########
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:
##########
@@ -44,6 +44,14 @@ Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* o
   }
 
   std::shared_ptr<ArrayData> in_array = batch[0].array.ToArrayData();
+
+  // If the input type is STRING, it is first encoded as a dictionary to facilitate
+  // processing. This approach allows the subsequent code to uniformly handle STRING
+  // inputs as if they were originally provided in dictionary format. Encoding as a
+  // dictionary helps in reusing the same logic for dictionary operations.
+  if (batch[0].type()->id() == Type::STRING) {
+    in_array = DictionaryEncode(batch[0].array.ToArrayData())->array();

Review Comment:
   I agree.



-- 
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-39049: [C++] Use Cast() instead of CastTo() for Dictionary Scalar in test [arrow]

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


##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1479,11 +1479,12 @@ TEST(TestDictionaryScalar, Cast) {
       auto alpha =
           dict->IsValid(i) ? MakeScalar(dict->GetString(i)) : MakeNullScalar(utf8());
       // Cast string to dict(..., string)
-      ASSERT_OK_AND_ASSIGN(auto cast_alpha, alpha->CastTo(ty));
-      ASSERT_OK(cast_alpha->ValidateFull());
+      ASSERT_OK_AND_ASSIGN(auto cast_alpha, Cast(alpha, ty));
+      const auto& scalar = cast_alpha.scalar();

Review Comment:
   Fix 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