mustafasrepo opened a new pull request, #7040: URL: https://github.com/apache/arrow-datafusion/pull/7040
# 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. --> Closes [#6190](https://github.com/apache/arrow-datafusion/issues/6190). # Rationale for this change Primary key information specifies that some of the columns in the table, would consist of unique values. This contract enables us to support additional queries, currently cannot be run by datafusiuon. Consider the query below ```sql SELECT sn, amount FROM sales_global GROUP BY sn ``` When `sn` is `PRIMARY KEY` (each `sn` will have unique values) we know that, all the columns the table `sales_global` can be emitted after aggregation (since for each group all rows would have same value). In terms of functionality, above query and following query ```sql SELECT sn, amount FROM sales_global GROUP BY sn, amount ``` are same. The reason is that, since column `sn` already consists of unique values; both `GROUP BY sn, amount` and `GROUP BY sn` will produce groups that consist of single row. ```sql SELECT sn, amount FROM sales_global GROUP BY sn ``` Above query can run in Postgre. However, datafusion can only emit `sn` after aggregation from the original table. With this PR above query also supported by Datafusion. As part of this PR, we keep track of identifier key (where primary key is a special case) to accomplish this. To illusturate what an identifier key is Consider table below | a | b | | ---- | --- | | 1 | USD | | 2 | USD | | 3 | TRY | In table above, `column a` consists of unique values, it is primary key. We store this information as ```rust IdentifierKeyGroup{ identifier_key_indices: vec![0], is_unique: true, associated_indices: vec![0, 1]) } ``` this means that, when we know the value of `0th` column, we will know the values of `0th and 1st` columns deterministically. And each row will be unique. As an another example, consider table below, | a | b | | ----- | ----- | | 1 | USD | | 1 | USD | | 2 | USD | | 2 | USD | | 3 | TRY | | 3 | TRY | In the example above, `column a` is not primary key, however, still knowing value of `a` enables us to know value of `b` deterministically. We encode this information as ```rust IdentifierKeyGroup{ identifier_key_indices: vec![0], is_unique: false, associated_indices: vec![0, 1]) } ``` All of these information are propagated from the primary key information at the source. These analysis enables us to support complex queries. As an example, while we can support query below ```sql SELECT r.sn, SUM(l.amount), r.amount FROM sales_global_with_pk AS l JOIN sales_global_with_pk AS r ON l.sn >= r.sn GROUP BY r.sn ORDER BY r.sn ``` we reject, following query ```sql SELECT l.sn, r.sn, SUM(l.amount), r.amount FROM sales_global_with_pk AS l JOIN sales_global_with_pk AS r ON l.sn >= r.sn GROUP BY l.sn ORDER BY l.sn ``` because second query, uses an unassociated column(`r.sn`) with `l.sn` after group by, whereas, first query uses only associated columns(`r.sn`, `r.amount`) with `r.sn` during projection. <!-- 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? <!-- 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, new tests that show supported functionality are added to the `groupby.slt` file. <!-- 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? <!-- 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 add the `api change` label. --> -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
