Skip to content

Common subexpression elimination #566

Description

@Dandandan

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
I like to support eliminating common subexpressions from a query plan.

For example, this example from TCP-H query 1:

select *
    sum(l_extendedprice * (1 - l_discount)) as sum_disc_price,
    sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge
from T

could be rewritten to reuse the result of l_extendedprice * (1 - l_discount) by moving it into another projection.
This looks in SQL as something like:

with temp as (
   select
       l_extendedprice * (1 - l_discount) as cs
   from t
)
select 
    sum(cs) as sum_disc_price,
    sum(cs * (1 + l_tax)) as sum_charge
from temp

Describe the solution you'd like
Detect common subexpression in projections / group by, join conditions etc. and remove them by moving doing them once in a projection step. Note: this only makes sense when the expression includes any calculation (e.g. not a column reference with alias).

Describe alternatives you've considered
n/a
Additional context
n/a

Activity

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