2010YOUY01 opened a new pull request, #27393: URL: https://github.com/apache/datafusion/pull/27393
> [!IMPORTANT] > > **Fork CI setup:** For pull requests from a fork to `main`, enable workflows in your fork's **Actions** tab before you push. > See the [fork CI setup guide](https://datafusion.apache.org/contributor-guide/#pull-requests-from-forks). ## 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 #. ## 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. Please explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the implementation. "COUNT(DISTINCT) returns wrong results when the column contains nulls" is the user-visible problem. --> For the bug reproducer, see the rationale in https://github.com/apache/datafusion/pull/27170, and this PR's slt testcase. In https://github.com/apache/datafusion/pull/16785, we added support for preserving the output ordering of `COPY TO`: ```sql COPY (SELECT * FROM t ORDER BY c1) TO 'ordered.parquet'; ``` The output must preserve the specified ordering. The key implementation is here: https://github.com/apache/datafusion/blob/55de8615567c58403010273b057de962ab3c214e/datafusion/core/src/physical_planner.rs#L1045 The idea is: 1. During physical planning, infer the existing output ordering of the subplan. 2. Set the inferred ordering as the required input ordering of `DataSinkExec`, so subsequent physical optimizations preserve it. Otherwise, the optimizer might eliminate the ordering. ```text DataSinkExec (subplan) ``` However, this approach has a bug: during initial physical planning, the subplan has not yet been processed by `EnsureRequirements`, so some partitioning and ordering requirements may still be unsatisfied. As a result, the inferred output ordering can be incorrect at this stage. It only becomes reliable after `EnsureRequirements` has enforced those requirements and finalized the plan shape. https://github.com/apache/datafusion/pull/27170 explains a query triggers this bug in detail. ## 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. --> Change the input-ordering derivation of DataSourceExec, see above rationale: it previously use the inferred input ordering, this PR changes it to explicit ordering inside a child SortExec. ## What is the testing strategy for this PR? <!-- 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 Briefly describe how this PR is tested, and point to the specific tests you added. For example: 'This new feature is covered by the `sqllogictest` cases added in `foo.slt`'. If this PR does not add tests, explain why. For example, if the change is already covered by existing tests, please mention it. You should also check the `codecov` bot reply on this PR to confirm the changed code is exercised. --> slt ## 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. --> Yes, we previously provided a stronger guarantee than the SQL spec/PostgreSQL, but we can no longer maintain it without compromising correctness. This PR tightens the behavior to align with the SQL spec. Previously the behavior is ``` # If `t` has existing order, the output file keeps this order COPY (SELECT * FROM t) TO 'ordered.parquet'; ``` This is stronger than SQL spec and postgres behavior https://www.postgresql.org/docs/current/sql-copy.html Now we have to declare the order, but if there is existing order, no extra sorting is required in execution ``` COPY (SELECT * FROM t ORDER BY c1) TO 'ordered.parquet'; ``` -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
