LakshSingla opened a new pull request, #14981:
URL: https://github.com/apache/druid/pull/14981

   ### Description
   MSQ currently doesn't support UNION queries. However, in the query stack, 
there are two types of UNIONs:
   1. UnionDataSource - Very limited. MSQ detects this and throws a 
`QueryNotSupported` fault, which is the expected behavior
   2. Top-level union - Works around the shortcomings of 1.
   
   However 2) is executed sequentially by the SQL layer and the results are 
appended sequentially. For a simple query like 
   ```sql
   SELECT * FROM foo
   UNION ALL
   SELECT * FROM foo2 
   ```
   SQL would execute `SELECT * FROM foo` and `SELECT * FROM foo2` and concat 
the results together.
   This works fine for working with engines producing results synchronously 
like `sql-native` where we return the results, however, for MSQ, which produces 
results asynchronously, the concatenation logic doesn't work as expected since 
don't wait for the query to finish, fetch the results and submit the second 
query.
   
   To make matters worse, the SQL layer submits the first query, gets the query 
ID back as the result, and then executes the second query (that fails). 
Therefore we only submit the partial query successfully and we might even get 
the incorrect results back.
   
   This PR introduces the engine feature `ALLOW_TOP_LEVEL_UNION_ALL` that 
dictates whether the planner can plan the query using top-level union alls. MSQ 
disallows this, so the queries are forced to plan using the union data source, 
which will return query not supported exception.
   
   This flag will also be useful once we start supporting unions in MSQ, which 
we'd want to exclusively execute using `UnionDataSource`, and the flag would 
seamlessly tie in with the query paths we'd wanna take when planning unions 
then.
   
   With the change, the following query:
   <img width="912" alt="Screenshot 2023-09-14 at 2 07 34 AM" 
src="https://github.com/apache/druid/assets/30999375/dc09b94f-a112-49b6-9c27-7811bc7a92b3";>
   
   native tasks plan query as before (top-level union all)
   
   <img width="470" alt="Screenshot 2023-09-14 at 2 07 39 AM" 
src="https://github.com/apache/druid/assets/30999375/f598aff4-2a10-4b16-b227-3cb332660f59";>
   
   
   MSQ tasks plan can't plan query with top-level union all, therefore use the 
`UnionDataSource` to plan the query, which then ultimately fails with 
`QueryNotSupported` in MSQ 
   
   <img width="384" alt="Screenshot 2023-09-14 at 2 07 48 AM" 
src="https://github.com/apache/druid/assets/30999375/145031d1-4961-41ad-9626-38523fcdacf9";>
   
   
   #### Release note
   Fixes the bug where top-level unions partially execute with MSQ engine, and 
can give incorrect results.
   
   <hr>
   
   
   <!-- Check the items by putting "x" in the brackets for the done things. Not 
all of these items apply to every PR. Remove the items which are not done or 
not relevant to the PR. None of the items from the checklist below are strictly 
necessary, but it would be very helpful if you at least self-review the PR. -->
   
   This PR has:
   
   - [x] been self-reviewed.
      - [ ] using the [concurrency 
checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md)
 (Remove this item if the PR doesn't have any relation to concurrency.)
   - [x] added documentation for new or modified features or behaviors.
   - [x] a release note entry in the PR description.
   - [x] added Javadocs for most classes and all non-trivial methods. Linked 
related entities via Javadoc links.
   - [ ] added or updated version, license, or notice information in 
[licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
   - [x] added comments explaining the "why" and the intent of the code 
wherever would not be obvious for an unfamiliar reader.
   - [x] added unit tests or modified existing tests to cover new code paths, 
ensuring the threshold for [code 
coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md)
 is met.
   - [ ] added integration tests.
   - [x] been tested in a test Druid cluster.
   


-- 
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]

Reply via email to