Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline trivial subquery #4231

Closed
findepi opened this issue Jun 25, 2020 · 4 comments
Closed

Inline trivial subquery #4231

findepi opened this issue Jun 25, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@findepi
Copy link
Member

findepi commented Jun 25, 2020

Consider:

presto> EXPLAIN (TYPE LOGICAL) SELECT * FROM tpch.tiny.nation WHERE nationkey = (SELECT 3);
                                                               Query Plan
----------------------------------------------------------------------------------------------------------------------------------------
 Output[nationkey, name, regionkey, comment]
 │   Layout: [nationkey:bigint, name:varchar(25), regionkey:bigint, comment:varchar(152)]
 │   Estimates: {rows: 1 (109B), cpu: 5.55k, memory: 0B, network: 109B}
 └─ RemoteExchange[GATHER]
    │   Layout: [nationkey:bigint, name:varchar(25), regionkey:bigint, comment:varchar(152)]
    │   Estimates: {rows: 1 (109B), cpu: 5.55k, memory: 0B, network: 109B}
    └─ CrossJoin
       │   Layout: [nationkey:bigint, name:varchar(25), regionkey:bigint, comment:varchar(152)]
       │   Estimates: {rows: 1 (109B), cpu: 5.55k, memory: 0B, network: 0B}
       │   Distribution: REPLICATED
       ├─ ScanFilter[table = tpch:nation:sf0.01, filterPredicate = ("nationkey" = BIGINT '3')]
       │      Layout: [nationkey:bigint, name:varchar(25), regionkey:bigint, comment:varchar(152)]
       │      Estimates: {rows: 25 (2.67kB), cpu: 2.67k, memory: 0B, network: 0B}/{rows: 1 (109B), cpu: 5.34k, memory: 0B, network: 0B}
       │      nationkey := tpch:nationkey
       │      name := tpch:name
       │      regionkey := tpch:regionkey
       │      comment := tpch:comment
       └─ LocalExchange[SINGLE] ()
          │   Layout: []
          │   Estimates: {rows: 1 (0B), cpu: 0, memory: 0B, network: 0B}
          └─ RemoteExchange[REPLICATE]
             │   Layout: []
             │   Estimates: {rows: 1 (0B), cpu: 0, memory: 0B, network: 0B}
             └─ Values
                    Layout: []
                    Estimates: {rows: 1 (0B), cpu: 0, memory: 0B, network: 0B}
                    ()

there should be no join, the subquery should get inlined.

@findepi findepi added the enhancement New feature or request label Jun 25, 2020
@martint
Copy link
Member

martint commented Jun 26, 2020

The RemoveRedundantCrossJoin rule should take care of that, but it's probably not firing in the right place.

@findepi
Copy link
Member Author

findepi commented Jun 26, 2020

RemoveRedundantCrossJoin can remove scalar subquery when it has no projections.

@martint
Copy link
Member

martint commented Jun 26, 2020

Exactly. That's the case in the example above. I tried adding the rule somewhere after

                new IterativeOptimizer(
                        ruleStats,
                        statsCalculator,
                        estimatedExchangesCostCalculator,
                        ImmutableSet.<Rule<?>>builder()
                                // add UnaliasSymbolReferences when it's ported
                                .add(new RemoveRedundantIdentityProjections())
                                .addAll(GatherAndMergeWindows.rules())
                                .build()),

and it does get optimized.

@findepi
Copy link
Member Author

findepi commented May 21, 2021

With 356

trino:tiny> EXPLAIN (TYPE LOGICAL) SELECT * FROM tpch.tiny.nation WHERE nationkey = (SELECT 3);
                                                             Query Plan
-------------------------------------------------------------------------------------------------------------------------------------
 Output[nationkey, name, regionkey, comment]
 │   Layout: [nationkey:bigint, name:varchar(25), regionkey:bigint, comment:varchar(152)]
 │   Estimates: {rows: 1 (109B), cpu: 5.34k, memory: 0B, network: 109B}
 └─ RemoteExchange[GATHER]
    │   Layout: [nationkey:bigint, name:varchar(25), regionkey:bigint, comment:varchar(152)]
    │   Estimates: {rows: 1 (109B), cpu: 5.34k, memory: 0B, network: 109B}
    └─ ScanFilter[table = tpch:nation:sf0.01, filterPredicate = ("nationkey" = BIGINT '3')]
           Layout: [nationkey:bigint, name:varchar(25), regionkey:bigint, comment:varchar(152)]
           Estimates: {rows: 25 (2.67kB), cpu: 2.67k, memory: 0B, network: 0B}/{rows: 1 (109B), cpu: 5.34k, memory: 0B, network: 0B}
           nationkey := tpch:nationkey
           regionkey := tpch:regionkey
           name := tpch:name
           comment := tpch:comment

cc @kasiafi

@findepi findepi closed this as completed May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants