Skip to content

Add back Distinct for where-exists if subquery is a DISTINCT #5344

@ygf11

Description

@ygf11

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

After #5264, we support rewriting more where-exists to join, but do not add back the Distinct.
For query:

SELECT t1.t1_id
FROM   t1
WHERE  EXISTS(SELECT DISTINCT t2_int
              FROM   t2
              WHERE  t2.t2_id > t1.t1_id); 

# current logical plan:
Projection: t1.t1_id                                                                                                                
  LeftSemi Join:  Filter: t2.t2_id > t1.t1_id                                                                                                               
    TableScan: t1 projection=[t1_id]                                                                                                       
    Projection: t2.t2_id # DISTINCT is not added back                                                                                                                                    
      TableScan: t2 projection=[t2_id]

Although the execution result is correct, we should add back the DISTINCT.
In some cases, DISTINCT can filter a lot rows before join, we should let user choose.

Describe the solution you'd like
Add back Distinct if the subquery is a DISTINCT.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions