You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "rishvin (via GitHub)" <gi...@apache.org> on 2025/09/05 05:36:21 UTC

[PR] chore: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

rishvin opened a new pull request, #2312:
URL: https://github.com/apache/datafusion-comet/pull/2312

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Addresses Part of #1941 
   
   ## Rationale for this change
   
   <!--
    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.  
   -->
   Introduces `map_to_list` which converts a `MapArray` to `ListArray<Struct<K, V>`. 
   
   This PR has been split from original PR https://github.com/apache/datafusion-comet/pull/2221. Please see that for details.
   ## What changes are included in this PR?
   
   <!--
   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.
   -->
   A new `map_to_list` physical function.
   
   ## How are these changes tested?
   
   <!--
   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)?
   -->
   Added unit tests.
   


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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

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

   Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.


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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on PR #2312:
URL: https://github.com/apache/datafusion-comet/pull/2312#issuecomment-3285609973

   Thanks @rishvin appreciate if you can take #2388  fill up the context and we can go through PRs once again 


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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on PR #2312:
URL: https://github.com/apache/datafusion-comet/pull/2312#issuecomment-3271689317

   Sorry @rishvin it looks like some conflicts in there


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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

Posted by "rishvin (via GitHub)" <gi...@apache.org>.
rishvin commented on PR #2312:
URL: https://github.com/apache/datafusion-comet/pull/2312#issuecomment-3283141180

   > Thanks @rishvin what exactly spark sql function this PR addresses? I checked https://spark.apache.org/docs/latest/api/sql/search.html?q=map I dont see map_to_list
   > 
   > Are you referring to https://spark.apache.org/docs/latest/api/sql/#map_entries ? If so this function already supported
   
   Hey @comphead : This is not spark sql function. We are using it internally to support grouping on map type. 
   
   I know there has been some confusion around `MapSort` lately, hence providing the full context here regarding the original PR: https://github.com/apache/datafusion-comet/pull/2221. There is also subtle difference between `map_to_list` and `map_entries`, mentioned later.
   
   Spark4.0+ when doing grouping on `Map` type applies a logical rule to wrap the grouping column with `MapSort` expression. This expression class sorts the Map elements by keys, such that the output performed after evaluation is an ordered map. This ordered map can then be supplied for grouping because it will produce the correct result. For eg. these 2 map elements - `{a: 1, b: 2}` and `{b:2, a:1}`, regardless of ordering of keys in the map are essentially the same, hence should be part of the same group. The `MapSort` will sort them as `{a: 1, b: 2}` and  `groupby` will put them in the same group `{a: 1, b: 2}`.
   
   Today, writing only the equivalent `MapSort` expression in Comet along will not work. This is because DataFusion does not directly support grouping on `Map` type. If we attempt to pass a `Map` type to DataFusion, it will throw with `Not implemented ...` error. However, DataFusion does support grouping on a more canonical form like `List`. So, if we change the `Map` type to `List` type, then Datafusion will  work and give the correct result. To do so, I added this internal function `map_to_list` that will convert the `Map` type to the `List` type before passing it to the grouping expression. This translation from map to list type is done [here](https://github.com/rishvin/datafusion-comet/blob/4c580f829fae74c4b5a9b3feb277a32cd94937dc/native/core/src/execution/utils.rs#L160), when building the grouping expressions.
   
   But now passing `map_to_list` to the grouping expression has one problem, the output grouping keys produced by the `HashAggregate` will be a `List` instead of `Map`. The result produced cannot be directly consumed by the downstream operators because they expect the grouping key type to be `Map`, however, we are passing them `List` type. We will hit schema-mismatch exception. So, we must convert the `List` type back to `Map` again. This translation is done by `map_from_list` internal function, which convert the output grouping keys back to `Map` type. This function is implemented in part3 [here](https://github.com/apache/datafusion-comet/pull/2328). And the conversion of `List` to `Map` type happens [here](https://github.com/rishvin/datafusion-comet/blob/4c580f829fae74c4b5a9b3feb277a32cd94937dc/native/core/src/execution/utils.rs#L209) by applying a projection on top of `HashAggregate`.
   
   
   These conversation are  totally done in native code, localized to `HashAggregate`. Since, we are maintain the schema requirements, the result produced by the `HashAggregate` can be consumed downstream Spark operators also.
   
   --- 
   Regarding the second part of using `map_entries`:
   
   I checked the implementation in Datafusion, [here](https://github.com/rishvin/datafusion/blob/407a965d3740634d582376aa22c4b2b57da6f005/datafusion/functions-nested/src/map_entries.rs#L137-L137), which has one difference. It doesn't carry forward the field names. It is creating key field with name `key` and value field with name `value`. This means, if the original map has field names as `keys` and `values` (plural), they will not be retained when doing this conversion. Also, it does not carry forward `sorted` flag which is very specific to `Map` type. 
   
   The `map_to_list` addresses this by cloning the incoming Map's field and piggybacking the `sorted` flag in the metadata hashmap of the field. This is being done [here](https://github.com/rishvin/datafusion-comet/blob/da1a397395b5deadc99408b6b8325f6cdef20bb9/native/spark-expr/src/map_funcs/map_to_list.rs#L72).
   
   And when converting the list back to map, the original field and metadata is consumed to construct the map type back. This is done [here](https://github.com/rishvin/datafusion-comet/blob/ded06b5c7548560761f21569035e7b4345fb3ca2/native/spark-expr/src/map_funcs/map_from_list.rs#L90).
   
   Retaining these metadata information is important, otherwise, we may run into schema mismatch issues. I ran into some schema related issue when implementing this.
   
   So, I think we may be able to use `map_entries` but we should retain the field and map specific metadata.
   
   Let me know if you have any further questions and your thoughts around 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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] chore: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #2312:
URL: https://github.com/apache/datafusion-comet/pull/2312#issuecomment-3259420300

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion-comet/pull/2312?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :white_check_mark: All modified and coverable lines are covered by tests.
   :white_check_mark: Project coverage is 33.02%. Comparing base ([`f09f8af`](https://app.codecov.io/gh/apache/datafusion-comet/commit/f09f8af64c6599255e116a376f4f008f2fd63b43?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`d90c182`](https://app.codecov.io/gh/apache/datafusion-comet/commit/d90c182a49ff41bd1a3199fcdd65f23b88833968?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 462 commits behind head on main.
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@              Coverage Diff              @@
   ##               main    #2312       +/-   ##
   =============================================
   - Coverage     56.12%   33.02%   -23.10%     
   + Complexity      976      732      -244     
   =============================================
     Files           119      146       +27     
     Lines         11743    13364     +1621     
     Branches       2251     2358      +107     
   =============================================
   - Hits           6591     4414     -2177     
   - Misses         4012     8148     +4136     
   + Partials       1140      802      -338     
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/datafusion-comet/pull/2312?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   </details>


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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

Posted by "rishvin (via GitHub)" <gi...@apache.org>.
rishvin commented on PR #2312:
URL: https://github.com/apache/datafusion-comet/pull/2312#issuecomment-3276977428

   >[Spark SQL Tests / spark-sql-sql_core-1/ubuntu-24.04/spark-3.5.6/java-11 (pull_request)](https://github.com/apache/datafusion-comet/actions/runs/17602201824/job/50068284122?pr=2312)
   
   This failure looks unrelated to the PR changes.


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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] chore: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

Posted by "rishvin (via GitHub)" <gi...@apache.org>.
rishvin commented on PR #2312:
URL: https://github.com/apache/datafusion-comet/pull/2312#issuecomment-3257130644

   @comphead / @andygrove split the second function.


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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

Posted by "rishvin (via GitHub)" <gi...@apache.org>.
rishvin commented on PR #2312:
URL: https://github.com/apache/datafusion-comet/pull/2312#issuecomment-3273115056

   > Sorry @rishvin it looks like some conflicts in there
   
   Thanks @comphead , resolved the conflict. I forced pushed to change my commit message from  `chore`  to `feat`.


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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: [1941-Part2]: Introduce map_to_list scalar function [datafusion-comet]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #2312: feat: [1941-Part2]: Introduce map_to_list scalar function
URL: https://github.com/apache/datafusion-comet/pull/2312


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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org