Skip to content

Add optimizer rule to replace inlist with or chain for small list #799

Closed

Description

@Dandandan

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
For TCP-H query 12, according to the profier about 16% (including IO) of the time is spent in the code in InListExpr.

The full query is:

select
    l_shipmode,
    sum(case
            when o_orderpriority = '1-URGENT'
                or o_orderpriority = '2-HIGH'
                then 1
            else 0
        end) as high_line_count,
    sum(case
            when o_orderpriority <> '1-URGENT'
                and o_orderpriority <> '2-HIGH'
                then 1
            else 0
        end) as low_line_count
from
    lineitem
        join
    orders
    on
            l_orderkey = o_orderkey
where
        l_shipmode in ('MAIL', 'SHIP')
  and l_commitdate < l_receiptdate
  and l_shipdate < l_commitdate
  and l_receiptdate >= date '1994-01-01'
  and l_receiptdate < date '1995-01-01'
group by
    l_shipmode
order by
    l_shipmode;

We can replace:

x in (a, b)
with the equivalent of

(x==a or x==b)

for "small enough" lists for some performance improvement.

Describe the solution you'd like

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.

Activity

  1. alamb commented on Jan 10, 2025

    @alamb
    Contributor

    This is done now from what I can tell:

    > create table foo(x int, a int, b int) as values (1,2,3);
    0 row(s) fetched.
    Elapsed 0.006 seconds.
    
    > explain select * from foo where x in (a, b);
    +---------------+---------------------------------------------------+
    | plan_type     | plan                                              |
    +---------------+---------------------------------------------------+
    | logical_plan  | Filter: foo.x = foo.a OR foo.x = foo.b            |
    |               |   TableScan: foo projection=[x, a, b]             |
    | physical_plan | CoalesceBatchesExec: target_batch_size=8192       |
    |               |   FilterExec: x@0 = a@1 OR x@0 = b@2              |
    |               |     MemoryExec: partitions=1, partition_sizes=[1] |
    |               |                                                   |
    +---------------+---------------------------------------------------+
    2 row(s) fetched.
    Elapsed 0.018 seconds.
    
    >

    Note the

    FilterExec: x@0 = a@1 OR x@0 = b@2   
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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