xiedeyantu opened a new issue, #21310:
URL: https://github.com/apache/datafusion/issues/21310

   ### Is your feature request related to a problem or challenge?
   
   The optimizer currently does not have a configurable rewrite for eligible 
`UNION DISTINCT` queries that could be collapsed into a single filtered scan 
when all branches read from the same source and differ only by filter 
predicates. As a result, some queries may scan the same input multiple times 
even though an equivalent and more efficient plan is possible.
   
   
   ### Describe the solution you'd like
   
   Add a new optimizer configuration option, 
`datafusion.optimizer.enable_unions_to_filter`, to control whether the 
`UnionsToFilter` rewrite rule is enabled.
   
   When this option is enabled, the optimizer should rewrite eligible `UNION 
DISTINCT` queries into an equivalent plan that scans the shared source once and 
combines the branch predicates into a single filter when it is semantically 
safe to do so. When the option is disabled, the existing `UNION DISTINCT` plan 
shape should remain unchanged.
   
   **Example rewrite**
   When the rule is enabled, a query such as:
   
   ```
   SELECT id, name FROM t1 WHERE id = 1
   UNION
   SELECT id, name FROM t1 WHERE id = 2
   ```
   
   may be rewritten into an equivalent plan that scans t1 once and applies a 
combined filter such as:
   
   ```
   SELECT id, name FROM t1 WHERE id = 1 OR id = 2
   ```
   
   This keeps the results unchanged while avoiding repeated reads from the same 
source.
   
   ### Describe alternatives you've considered
   
   _No response_
   
   ### Additional context
   
   _No response_


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