You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2023/06/19 14:20:46 UTC

[arrow-datafusion] branch main updated: Move alias generator to per-query execution props (#6706)

This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new cae8cc2764 Move alias generator to per-query execution props (#6706)
cae8cc2764 is described below

commit cae8cc2764616407c250954917a5af89e78465d7
Author: Armin Primadi <ap...@gmail.com>
AuthorDate: Mon Jun 19 21:20:40 2023 +0700

    Move alias generator to per-query execution props (#6706)
    
    * Add alias generator to execution props
    
    * Fix error
    
    * Fix test
    
    * Fix test
    
    * Fix test
    
    * Use alias generator in exec props for scalar_subquery_to_join
    
    * Fix error
    
    * Fix tests
    
    * Update sql tests
    
    ---------
    
    Co-authored-by: Andrew Lamb <an...@nerdnetworks.org>
---
 datafusion/{optimizer => common}/src/alias.rs      |   1 +
 datafusion/common/src/lib.rs                       |   1 +
 datafusion/core/src/execution/context.rs           |   5 +
 .../core/tests/sqllogictests/test_files/joins.slt  |  64 ++++----
 .../tests/sqllogictests/test_files/subquery.slt    | 171 ++++++++++-----------
 .../sqllogictests/test_files/tpch/q15.slt.part     |   4 +-
 .../sqllogictests/test_files/tpch/q17.slt.part     |   4 +-
 .../sqllogictests/test_files/tpch/q18.slt.part     |   4 +-
 .../sqllogictests/test_files/tpch/q2.slt.part      |   6 +-
 .../sqllogictests/test_files/tpch/q20.slt.part     |  12 +-
 .../sqllogictests/test_files/tpch/q21.slt.part     |   8 +-
 .../sqllogictests/test_files/tpch/q22.slt.part     |   8 +-
 .../sqllogictests/test_files/tpch/q4.slt.part      |   4 +-
 .../src/decorrelate_predicate_subquery.rs          |  12 +-
 datafusion/optimizer/src/lib.rs                    |   1 -
 datafusion/optimizer/src/optimizer.rs              |  12 ++
 .../optimizer/src/scalar_subquery_to_join.rs       |  16 +-
 .../src/simplify_expressions/expr_simplifier.rs    |   2 +
 datafusion/physical-expr/src/execution_props.rs    |  10 +-
 19 files changed, 183 insertions(+), 162 deletions(-)

diff --git a/datafusion/optimizer/src/alias.rs b/datafusion/common/src/alias.rs
similarity index 98%
rename from datafusion/optimizer/src/alias.rs
rename to datafusion/common/src/alias.rs
index 6420cc685e..2ee2cb4dc7 100644
--- a/datafusion/optimizer/src/alias.rs
+++ b/datafusion/common/src/alias.rs
@@ -18,6 +18,7 @@
 use std::sync::atomic::{AtomicUsize, Ordering};
 
 /// A utility struct that can be used to generate unique aliases when optimizing queries
+#[derive(Debug)]
 pub struct AliasGenerator {
     next_id: AtomicUsize,
 }
diff --git a/datafusion/common/src/lib.rs b/datafusion/common/src/lib.rs
index 80b8a85977..e941e443df 100644
--- a/datafusion/common/src/lib.rs
+++ b/datafusion/common/src/lib.rs
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
+pub mod alias;
 pub mod cast;
 mod column;
 pub mod config;
diff --git a/datafusion/core/src/execution/context.rs b/datafusion/core/src/execution/context.rs
index 3fa30af0ce..8d9fdc2e53 100644
--- a/datafusion/core/src/execution/context.rs
+++ b/datafusion/core/src/execution/context.rs
@@ -28,6 +28,7 @@ use crate::{
     optimizer::optimizer::Optimizer,
     physical_optimizer::optimizer::{PhysicalOptimizer, PhysicalOptimizerRule},
 };
+use datafusion_common::alias::AliasGenerator;
 use datafusion_execution::registry::SerializerRegistry;
 use datafusion_expr::{
     logical_plan::{DdlStatement, Statement},
@@ -1985,6 +1986,10 @@ impl OptimizerConfig for SessionState {
         self.execution_props.query_execution_start_time
     }
 
+    fn alias_generator(&self) -> Arc<AliasGenerator> {
+        self.execution_props.alias_generator.clone()
+    }
+
     fn options(&self) -> &ConfigOptions {
         self.config_options()
     }
diff --git a/datafusion/core/tests/sqllogictests/test_files/joins.slt b/datafusion/core/tests/sqllogictests/test_files/joins.slt
index 3ab1c4ed60..72de12b036 100644
--- a/datafusion/core/tests/sqllogictests/test_files/joins.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/joins.slt
@@ -1807,9 +1807,9 @@ from join_t1
 where join_t1.t1_id + 12 in (select join_t2.t2_id + 1 from join_t2)
 ----
 logical_plan
-LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_5.join_t2.t2_id + Int64(1)
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.join_t2.t2_id + Int64(1)
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_5
+--SubqueryAlias: __correlated_sq_1
 ----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1)
 ------TableScan: join_t2 projection=[t2_id]
 
@@ -1836,9 +1836,9 @@ where join_t1.t1_id + 12 in
     )
 ----
 logical_plan
-LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_7.join_t2.t2_id + Int64(1) Filter: join_t1.t1_int <= __correlated_sq_7.t2_int
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.join_t2.t2_id + Int64(1) Filter: join_t1.t1_int <= __correlated_sq_1.t2_int
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_7
+--SubqueryAlias: __correlated_sq_1
 ----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1), join_t2.t2_int
 ------Filter: join_t2.t2_int > UInt32(0)
 --------TableScan: join_t2 projection=[t2_id, t2_int]
@@ -1872,9 +1872,9 @@ where join_t1.t1_id + 12 in
     )
 ----
 logical_plan
-LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_9.join_t2.t2_id + Int64(1) Filter: join_t1.t1_int <= __correlated_sq_9.t2_int AND join_t1.t1_name != __correlated_sq_9.t2_name
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.join_t2.t2_id + Int64(1) Filter: join_t1.t1_int <= __correlated_sq_1.t2_int AND join_t1.t1_name != __correlated_sq_1.t2_name
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_9
+--SubqueryAlias: __correlated_sq_1
 ----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1), join_t2.t2_int, join_t2.t2_name
 ------Filter: join_t2.t2_int > UInt32(0)
 --------TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
@@ -1904,10 +1904,10 @@ where join_t1.t1_id + 12 in
     (select join_t2.t2_id + 1 from join_t2 where join_t1.t1_int > 0)
 ----
 logical_plan
-LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_11.join_t2.t2_id + Int64(1)
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.join_t2.t2_id + Int64(1)
 --Filter: join_t1.t1_int > UInt32(0)
 ----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_11
+--SubqueryAlias: __correlated_sq_1
 ----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1)
 ------TableScan: join_t2 projection=[t2_id]
 
@@ -1921,9 +1921,9 @@ where join_t1.t1_id + 12 not in
     (select join_t2.t2_id + 1 from join_t2 where join_t1.t1_int > 0)
 ----
 logical_plan
-LeftAnti Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_12.join_t2.t2_id + Int64(1) Filter: join_t1.t1_int > UInt32(0)
+LeftAnti Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.join_t2.t2_id + Int64(1) Filter: join_t1.t1_int > UInt32(0)
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_12
+--SubqueryAlias: __correlated_sq_1
 ----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1)
 ------TableScan: join_t2 projection=[t2_id]
 
@@ -1943,10 +1943,10 @@ where join_t1.t1_id + 12 in
   and join_t1.t1_id > 0
 ----
 logical_plan
-LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_13.join_t2.t2_id + Int64(1) Filter: join_t1.t1_int <= __correlated_sq_13.t2_int AND join_t1.t1_name != __correlated_sq_13.t2_name
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.join_t2.t2_id + Int64(1) Filter: join_t1.t1_int <= __correlated_sq_1.t2_int AND join_t1.t1_name != __correlated_sq_1.t2_name
 --Filter: join_t1.t1_id > UInt32(0)
 ----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_13
+--SubqueryAlias: __correlated_sq_1
 ----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1), join_t2.t2_int, join_t2.t2_name
 ------TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
 
@@ -1976,14 +1976,14 @@ where join_t1.t1_id + 12 in (select join_t2.t2_id + 1 from join_t2)
   and join_t1.t1_id > 0
 ----
 logical_plan
-LeftSemi Join: CAST(join_t1.t1_int AS Int64) = __correlated_sq_16.join_t2.t2_int + Int64(1)
---LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_15.join_t2.t2_id + Int64(1)
+LeftSemi Join: CAST(join_t1.t1_int AS Int64) = __correlated_sq_2.join_t2.t2_int + Int64(1)
+--LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.join_t2.t2_id + Int64(1)
 ----Filter: join_t1.t1_id > UInt32(0)
 ------TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
-----SubqueryAlias: __correlated_sq_15
+----SubqueryAlias: __correlated_sq_1
 ------Projection: CAST(join_t2.t2_id AS Int64) + Int64(1)
 --------TableScan: join_t2 projection=[t2_id]
---SubqueryAlias: __correlated_sq_16
+--SubqueryAlias: __correlated_sq_2
 ----Projection: CAST(join_t2.t2_int AS Int64) + Int64(1)
 ------TableScan: join_t2 projection=[t2_int]
 
@@ -2110,9 +2110,9 @@ WHERE EXISTS (
 )
 ----
 logical_plan
-LeftSemi Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_19.t2_id AS Int64) * Int64(2)
+LeftSemi Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2)
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_19
+--SubqueryAlias: __correlated_sq_1
 ----TableScan: join_t2 projection=[t2_id]
 
 statement ok
@@ -2160,9 +2160,9 @@ WHERE EXISTS (
 )
 ----
 logical_plan
-LeftSemi Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_22.t2_id AS Int64) * Int64(2)
+LeftSemi Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2)
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_22
+--SubqueryAlias: __correlated_sq_1
 ----Projection: join_t2.t2_id
 ------Filter: join_t2.t2_int < UInt32(3)
 --------TableScan: join_t2 projection=[t2_id, t2_int]
@@ -2208,10 +2208,10 @@ WHERE EXISTS (
 )
 ----
 logical_plan
-LeftSemi Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_25.t2_id AS Int64) * Int64(2)
+LeftSemi Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2)
 --Filter: join_t1.t1_int < UInt32(3)
 ----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_25
+--SubqueryAlias: __correlated_sq_1
 ----TableScan: join_t2 projection=[t2_id]
 
 statement ok
@@ -2255,9 +2255,9 @@ WHERE NOT EXISTS (
 )
 ----
 logical_plan
-LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_28.t2_id AS Int64) * Int64(2)
+LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2)
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_28
+--SubqueryAlias: __correlated_sq_1
 ----TableScan: join_t2 projection=[t2_id]
 
 statement ok
@@ -2301,9 +2301,9 @@ WHERE NOT EXISTS (
 )
 ----
 logical_plan
-LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_31.t2_id AS Int64) * Int64(2)
+LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2)
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_31
+--SubqueryAlias: __correlated_sq_1
 ----Projection: join_t2.t2_id
 ------Aggregate: groupBy=[[join_t2.t2_int, join_t2.t2_id]], aggr=[[]]
 --------Projection: join_t2.t2_int, join_t2.t2_id
@@ -2350,9 +2350,9 @@ WHERE NOT EXISTS(
 )
 ----
 logical_plan
-LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_34.t2_id AS Int64) * Int64(2)
+LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2)
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_34
+--SubqueryAlias: __correlated_sq_1
 ----Projection: join_t2.t2_id
 ------Aggregate: groupBy=[[join_t2.t2_id + join_t2.t2_int, join_t2.t2_int, join_t2.t2_id]], aggr=[[]]
 --------Projection: join_t2.t2_id + join_t2.t2_int, join_t2.t2_int, join_t2.t2_id
@@ -2401,9 +2401,9 @@ WHERE NOT EXISTS(
 )
 ----
 logical_plan
-LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_37.t2_id AS Int64) * Int64(2)
+LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2)
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_37
+--SubqueryAlias: __correlated_sq_1
 ----Projection: join_t2.t2_id
 ------Aggregate: groupBy=[[Int64(1), join_t2.t2_int, join_t2.t2_id]], aggr=[[]]
 --------Projection: Int64(1), join_t2.t2_int, join_t2.t2_id
@@ -2432,9 +2432,9 @@ WHERE NOT EXISTS(
 )
 ----
 logical_plan
-LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_39.t2_id AS Int64) * Int64(2)
+LeftAnti Join:  Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2)
 --TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_39
+--SubqueryAlias: __correlated_sq_1
 ----Projection: join_t2.t2_id
 ------Aggregate: groupBy=[[Int64(1), join_t2.t2_int, join_t2.t2_id]], aggr=[[]]
 --------Projection: Int64(1), join_t2.t2_int, join_t2.t2_id
diff --git a/datafusion/core/tests/sqllogictests/test_files/subquery.slt b/datafusion/core/tests/sqllogictests/test_files/subquery.slt
index 1961a5840d..b668891e99 100644
--- a/datafusion/core/tests/sqllogictests/test_files/subquery.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/subquery.slt
@@ -168,10 +168,10 @@ query TT
 explain SELECT t1_id, (SELECT sum(t2_int) FROM t2 WHERE t2.t2_id = t1.t1_id) as t2_sum from t1
 ----
 logical_plan
-Projection: t1.t1_id, __scalar_sq_2.SUM(t2.t2_int) AS t2_sum
---Left Join: t1.t1_id = __scalar_sq_2.t2_id
+Projection: t1.t1_id, __scalar_sq_1.SUM(t2.t2_int) AS t2_sum
+--Left Join: t1.t1_id = __scalar_sq_1.t2_id
 ----TableScan: t1 projection=[t1_id]
-----SubqueryAlias: __scalar_sq_2
+----SubqueryAlias: __scalar_sq_1
 ------Projection: SUM(t2.t2_int), t2.t2_id
 --------Aggregate: groupBy=[[t2.t2_id]], aggr=[[SUM(t2.t2_int)]]
 ----------TableScan: t2 projection=[t2_id, t2_int]
@@ -202,10 +202,10 @@ query TT
 explain SELECT t1_id, (SELECT sum(t2_int * 1.0) + 1 FROM t2 WHERE t2.t2_id = t1.t1_id) as t2_sum from t1
 ----
 logical_plan
-Projection: t1.t1_id, __scalar_sq_4.SUM(t2.t2_int * Float64(1)) + Int64(1) AS t2_sum
---Left Join: t1.t1_id = __scalar_sq_4.t2_id
+Projection: t1.t1_id, __scalar_sq_1.SUM(t2.t2_int * Float64(1)) + Int64(1) AS t2_sum
+--Left Join: t1.t1_id = __scalar_sq_1.t2_id
 ----TableScan: t1 projection=[t1_id]
-----SubqueryAlias: __scalar_sq_4
+----SubqueryAlias: __scalar_sq_1
 ------Projection: SUM(t2.t2_int * Float64(1)) + Float64(1) AS SUM(t2.t2_int * Float64(1)) + Int64(1), t2.t2_id
 --------Aggregate: groupBy=[[t2.t2_id]], aggr=[[SUM(CAST(t2.t2_int AS Float64)) AS SUM(t2.t2_int * Float64(1))]]
 ----------TableScan: t2 projection=[t2_id, t2_int]
@@ -236,10 +236,10 @@ query TT
 explain SELECT t1_id, (SELECT sum(t2_int) FROM t2 WHERE t2.t2_id = t1.t1_id group by t2_id, 'a') as t2_sum from t1
 ----
 logical_plan
-Projection: t1.t1_id, __scalar_sq_6.SUM(t2.t2_int) AS t2_sum
---Left Join: t1.t1_id = __scalar_sq_6.t2_id
+Projection: t1.t1_id, __scalar_sq_1.SUM(t2.t2_int) AS t2_sum
+--Left Join: t1.t1_id = __scalar_sq_1.t2_id
 ----TableScan: t1 projection=[t1_id]
-----SubqueryAlias: __scalar_sq_6
+----SubqueryAlias: __scalar_sq_1
 ------Projection: SUM(t2.t2_int), t2.t2_id
 --------Aggregate: groupBy=[[t2.t2_id, Utf8("a")]], aggr=[[SUM(t2.t2_int)]]
 ----------TableScan: t2 projection=[t2_id, t2_int]
@@ -272,10 +272,10 @@ query TT
 explain SELECT t1_id, (SELECT sum(t2_int) FROM t2 WHERE t2.t2_id = t1.t1_id having sum(t2_int) < 3) as t2_sum from t1
 ----
 logical_plan
-Projection: t1.t1_id, __scalar_sq_8.SUM(t2.t2_int) AS t2_sum
---Left Join: t1.t1_id = __scalar_sq_8.t2_id
+Projection: t1.t1_id, __scalar_sq_1.SUM(t2.t2_int) AS t2_sum
+--Left Join: t1.t1_id = __scalar_sq_1.t2_id
 ----TableScan: t1 projection=[t1_id]
-----SubqueryAlias: __scalar_sq_8
+----SubqueryAlias: __scalar_sq_1
 ------Projection: SUM(t2.t2_int), t2.t2_id
 --------Filter: SUM(t2.t2_int) < Int64(3)
 ----------Aggregate: groupBy=[[t2.t2_id]], aggr=[[SUM(t2.t2_int)]]
@@ -322,15 +322,15 @@ where c_acctbal < (
 logical_plan
 Sort: customer.c_custkey ASC NULLS LAST
 --Projection: customer.c_custkey
-----Inner Join: customer.c_custkey = __scalar_sq_10.o_custkey Filter: CAST(customer.c_acctbal AS Decimal128(25, 2)) < __scalar_sq_10.SUM(orders.o_totalprice)
+----Inner Join: customer.c_custkey = __scalar_sq_1.o_custkey Filter: CAST(customer.c_acctbal AS Decimal128(25, 2)) < __scalar_sq_1.SUM(orders.o_totalprice)
 ------TableScan: customer projection=[c_custkey, c_acctbal]
-------SubqueryAlias: __scalar_sq_10
+------SubqueryAlias: __scalar_sq_1
 --------Projection: SUM(orders.o_totalprice), orders.o_custkey
 ----------Aggregate: groupBy=[[orders.o_custkey]], aggr=[[SUM(orders.o_totalprice)]]
 ------------Projection: orders.o_custkey, orders.o_totalprice
---------------Inner Join: orders.o_orderkey = __scalar_sq_11.l_orderkey Filter: CAST(orders.o_totalprice AS Decimal128(25, 2)) < __scalar_sq_11.price
+--------------Inner Join: orders.o_orderkey = __scalar_sq_2.l_orderkey Filter: CAST(orders.o_totalprice AS Decimal128(25, 2)) < __scalar_sq_2.price
 ----------------TableScan: orders projection=[o_orderkey, o_custkey, o_totalprice]
-----------------SubqueryAlias: __scalar_sq_11
+----------------SubqueryAlias: __scalar_sq_2
 ------------------Projection: SUM(lineitem.l_extendedprice) AS price, lineitem.l_orderkey
 --------------------Aggregate: groupBy=[[lineitem.l_orderkey]], aggr=[[SUM(lineitem.l_extendedprice)]]
 ----------------------TableScan: lineitem projection=[l_orderkey, l_extendedprice]
@@ -344,9 +344,9 @@ where o_orderstatus in (
 ----
 logical_plan
 Projection: orders.o_orderkey
---LeftSemi Join: orders.o_orderstatus = __correlated_sq_6.l_linestatus, orders.o_orderkey = __correlated_sq_6.l_orderkey
+--LeftSemi Join: orders.o_orderstatus = __correlated_sq_1.l_linestatus, orders.o_orderkey = __correlated_sq_1.l_orderkey
 ----TableScan: orders projection=[o_orderkey, o_orderstatus]
-----SubqueryAlias: __correlated_sq_6
+----SubqueryAlias: __correlated_sq_1
 ------Projection: lineitem.l_linestatus, lineitem.l_orderkey
 --------TableScan: lineitem projection=[l_orderkey, l_linestatus]
 
@@ -381,9 +381,9 @@ query TT
 explain SELECT t1_id, t1_name, t1_int FROM t1 WHERE t1_id IN(SELECT t1_int FROM t1 WHERE t1.t1_id > t1.t1_int)
 ----
 logical_plan
-LeftSemi Join: t1.t1_id = __correlated_sq_10.t1_int
+LeftSemi Join: t1.t1_id = __correlated_sq_1.t1_int
 --TableScan: t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_10
+--SubqueryAlias: __correlated_sq_1
 ----Projection: t1.t1_int
 ------Filter: t1.t1_id > t1.t1_int
 --------TableScan: t1 projection=[t1_id, t1_int]
@@ -393,13 +393,13 @@ query TT
 explain SELECT t1_id, t1_name, t1_int FROM t1 WHERE t1_id IN(SELECT t2_id FROM t2 WHERE EXISTS(select * from t1 WHERE t1.t1_int > t2.t2_int))
 ----
 logical_plan
-LeftSemi Join: t1.t1_id = __correlated_sq_11.t2_id
+LeftSemi Join: t1.t1_id = __correlated_sq_1.t2_id
 --TableScan: t1 projection=[t1_id, t1_name, t1_int]
---SubqueryAlias: __correlated_sq_11
+--SubqueryAlias: __correlated_sq_1
 ----Projection: t2.t2_id
-------LeftSemi Join:  Filter: __correlated_sq_12.t1_int > t2.t2_int
+------LeftSemi Join:  Filter: __correlated_sq_2.t1_int > t2.t2_int
 --------TableScan: t2 projection=[t2_id, t2_int]
---------SubqueryAlias: __correlated_sq_12
+--------SubqueryAlias: __correlated_sq_2
 ----------TableScan: t1 projection=[t1_int]
 
 #invalid_scalar_subquery
@@ -452,10 +452,10 @@ query TT
 explain SELECT t1_id, (SELECT a FROM (select 1 as a) WHERE a = t1.t1_int) as t2_int from t1
 ----
 logical_plan
-Projection: t1.t1_id, __scalar_sq_16.a AS t2_int
---Left Join: CAST(t1.t1_int AS Int64) = __scalar_sq_16.a
+Projection: t1.t1_id, __scalar_sq_1.a AS t2_int
+--Left Join: CAST(t1.t1_int AS Int64) = __scalar_sq_1.a
 ----TableScan: t1 projection=[t1_id, t1_int]
-----SubqueryAlias: __scalar_sq_16
+----SubqueryAlias: __scalar_sq_1
 ------Projection: Int64(1) AS a
 --------EmptyRelation
 
@@ -520,9 +520,9 @@ query TT
 explain SELECT t0_id, t0_name FROM t0 WHERE EXISTS (SELECT 1 FROM t1 INNER JOIN (select * from t2 where t2.t2_name = t0.t0_name) as t2 ON(t1.t1_id = t2.t2_id ))
 ----
 logical_plan
-LeftSemi Join: t0.t0_name = __correlated_sq_19.t2_name
+LeftSemi Join: t0.t0_name = __correlated_sq_1.t2_name
 --TableScan: t0 projection=[t0_id, t0_name]
---SubqueryAlias: __correlated_sq_19
+--SubqueryAlias: __correlated_sq_1
 ----Projection: t2.t2_name
 ------Inner Join: t1.t1_id = t2.t2_id
 --------TableScan: t1 projection=[t1_id]
@@ -534,9 +534,9 @@ query TT
 explain SELECT t0_id, t0_name FROM t0 WHERE EXISTS (select 1 from (SELECT * FROM t1 where t1.t1_id = t0.t0_id) as x INNER JOIN (select * from t2 where t2.t2_name = t0.t0_name) as y ON(x.t1_id = y.t2_id))
 ----
 logical_plan
-LeftSemi Join: t0.t0_id = __correlated_sq_20.t1_id, t0.t0_name = __correlated_sq_20.t2_name
+LeftSemi Join: t0.t0_id = __correlated_sq_1.t1_id, t0.t0_name = __correlated_sq_1.t2_name
 --TableScan: t0 projection=[t0_id, t0_name]
---SubqueryAlias: __correlated_sq_20
+--SubqueryAlias: __correlated_sq_1
 ----Projection: x.t1_id, y.t2_name
 ------Inner Join: x.t1_id = y.t2_id
 --------SubqueryAlias: x
@@ -574,9 +574,9 @@ query TT
 explain SELECT t1_id, t1_name FROM t1 WHERE EXISTS (SELECT * FROM t2 WHERE t2_id = t1_id limit 1)
 ----
 logical_plan
-LeftSemi Join: t1.t1_id = __correlated_sq_25.t2_id
+LeftSemi Join: t1.t1_id = __correlated_sq_1.t2_id
 --TableScan: t1 projection=[t1_id, t1_name]
---SubqueryAlias: __correlated_sq_25
+--SubqueryAlias: __correlated_sq_1
 ----TableScan: t2 projection=[t2_id]
 
 query IT rowsort
@@ -592,7 +592,7 @@ query TT
 explain SELECT t1_id, t1_name FROM t1 WHERE EXISTS (SELECT * FROM t2 WHERE t2_id = t1_id limit 0)
 ----
 logical_plan
-LeftSemi Join: t1.t1_id = __correlated_sq_27.t2_id
+LeftSemi Join: t1.t1_id = __correlated_sq_1.t2_id
 --TableScan: t1 projection=[t1_id, t1_name]
 --EmptyRelation
 
@@ -607,7 +607,7 @@ query TT
 explain SELECT t1_id, t1_name FROM t1 WHERE NOT EXISTS (SELECT * FROM t2 WHERE t2_id = t1_id limit 0)
 ----
 logical_plan
-LeftAnti Join: t1.t1_id = __correlated_sq_29.t2_id
+LeftAnti Join: t1.t1_id = __correlated_sq_1.t2_id
 --TableScan: t1 projection=[t1_id, t1_name]
 --EmptyRelation
 
@@ -639,9 +639,9 @@ query TT
 explain SELECT t1_id, t1_name FROM t1 WHERE t1_id in (SELECT t2_id FROM t2 limit 10)
 ----
 logical_plan
-LeftSemi Join: t1.t1_id = __correlated_sq_33.t2_id
+LeftSemi Join: t1.t1_id = __correlated_sq_1.t2_id
 --TableScan: t1 projection=[t1_id, t1_name]
---SubqueryAlias: __correlated_sq_33
+--SubqueryAlias: __correlated_sq_1
 ----Limit: skip=0, fetch=10
 ------TableScan: t2 projection=[t2_id], fetch=10
 
@@ -651,8 +651,8 @@ query TT
 explain SELECT t1_id, (SELECT t2_id FROM t2 limit 0) FROM t1
 ----
 logical_plan
-Projection: t1.t1_id, __scalar_sq_18.t2_id AS t2_id
---Left Join:
+Projection: t1.t1_id, __scalar_sq_1.t2_id AS t2_id
+--Left Join: 
 ----TableScan: t1 projection=[t1_id]
 ----EmptyRelation
 
@@ -685,8 +685,8 @@ query TT
 explain select (select count(*) from t1) as b
 ----
 logical_plan
-Projection: __scalar_sq_20.COUNT(UInt8(1)) AS b
---SubqueryAlias: __scalar_sq_20
+Projection: __scalar_sq_1.COUNT(UInt8(1)) AS b
+--SubqueryAlias: __scalar_sq_1
 ----Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]
 ------TableScan: t1 projection=[t1_id]
 
@@ -695,12 +695,12 @@ query TT
 explain select (select count(*) from t1) as b, (select count(1) from t2)
 ----
 logical_plan
-Projection: __scalar_sq_21.COUNT(UInt8(1)) AS b, __scalar_sq_22.COUNT(Int64(1)) AS COUNT(Int64(1))
---Left Join:
-----SubqueryAlias: __scalar_sq_21
+Projection: __scalar_sq_1.COUNT(UInt8(1)) AS b, __scalar_sq_2.COUNT(Int64(1)) AS COUNT(Int64(1))
+--Left Join: 
+----SubqueryAlias: __scalar_sq_1
 ------Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]
 --------TableScan: t1 projection=[t1_id]
-----SubqueryAlias: __scalar_sq_22
+----SubqueryAlias: __scalar_sq_2
 ------Aggregate: groupBy=[[]], aggr=[[COUNT(Int64(1))]]
 --------TableScan: t2 projection=[t2_id]
 
@@ -714,10 +714,10 @@ query TT
 explain SELECT t1_id, (SELECT count(*) FROM t2 WHERE t2.t2_int = t1.t1_int) from t1
 ----
 logical_plan
-Projection: t1.t1_id, CASE WHEN __scalar_sq_25.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_25.COUNT(UInt8(1)) END AS COUNT(UInt8(1))
---Left Join: t1.t1_int = __scalar_sq_25.t2_int
+Projection: t1.t1_id, CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_1.COUNT(UInt8(1)) END AS COUNT(UInt8(1))
+--Left Join: t1.t1_int = __scalar_sq_1.t2_int
 ----TableScan: t1 projection=[t1_id, t1_int]
-----SubqueryAlias: __scalar_sq_25
+----SubqueryAlias: __scalar_sq_1
 ------Projection: COUNT(UInt8(1)), t2.t2_int, __always_true
 --------Aggregate: groupBy=[[t2.t2_int, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 ----------TableScan: t2 projection=[t2_int]
@@ -736,10 +736,10 @@ query TT
 explain SELECT t1_id, (SELECT count(*) FROM t2 WHERE t2.t2_int = t1.t1_int) as cnt from t1
 ----
 logical_plan
-Projection: t1.t1_id, CASE WHEN __scalar_sq_27.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_27.COUNT(UInt8(1)) END AS cnt
---Left Join: t1.t1_int = __scalar_sq_27.t2_int
+Projection: t1.t1_id, CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_1.COUNT(UInt8(1)) END AS cnt
+--Left Join: t1.t1_int = __scalar_sq_1.t2_int
 ----TableScan: t1 projection=[t1_id, t1_int]
-----SubqueryAlias: __scalar_sq_27
+----SubqueryAlias: __scalar_sq_1
 ------Projection: COUNT(UInt8(1)), t2.t2_int, __always_true
 --------Aggregate: groupBy=[[t2.t2_int, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 ----------TableScan: t2 projection=[t2_int]
@@ -757,10 +757,10 @@ query TT
 explain SELECT t1_id, (SELECT count(*) as _cnt FROM t2 WHERE t2.t2_int = t1.t1_int) as cnt from t1
 ----
 logical_plan
-Projection: t1.t1_id, CASE WHEN __scalar_sq_29.__always_true IS NULL THEN Int64(0) AS _cnt ELSE __scalar_sq_29._cnt END AS cnt
---Left Join: t1.t1_int = __scalar_sq_29.t2_int
+Projection: t1.t1_id, CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(0) AS _cnt ELSE __scalar_sq_1._cnt END AS cnt
+--Left Join: t1.t1_int = __scalar_sq_1.t2_int
 ----TableScan: t1 projection=[t1_id, t1_int]
-----SubqueryAlias: __scalar_sq_29
+----SubqueryAlias: __scalar_sq_1
 ------Projection: COUNT(UInt8(1)) AS _cnt, t2.t2_int, __always_true
 --------Aggregate: groupBy=[[t2.t2_int, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 ----------TableScan: t2 projection=[t2_int]
@@ -778,10 +778,10 @@ query TT
 explain SELECT t1_id, (SELECT count(*) + 2 as _cnt FROM t2 WHERE t2.t2_int = t1.t1_int) from t1
 ----
 logical_plan
-Projection: t1.t1_id, CASE WHEN __scalar_sq_31.__always_true IS NULL THEN Int64(2) AS _cnt ELSE __scalar_sq_31._cnt END AS _cnt
---Left Join: t1.t1_int = __scalar_sq_31.t2_int
+Projection: t1.t1_id, CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(2) AS _cnt ELSE __scalar_sq_1._cnt END AS _cnt
+--Left Join: t1.t1_int = __scalar_sq_1.t2_int
 ----TableScan: t1 projection=[t1_id, t1_int]
-----SubqueryAlias: __scalar_sq_31
+----SubqueryAlias: __scalar_sq_1
 ------Projection: COUNT(UInt8(1)) + Int64(2) AS _cnt, t2.t2_int, __always_true
 --------Aggregate: groupBy=[[t2.t2_int, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 ----------TableScan: t2 projection=[t2_int]
@@ -800,11 +800,11 @@ explain select t1.t1_int from t1 where (select count(*) from t2 where t1.t1_id =
 ----
 logical_plan
 Projection: t1.t1_int
---Filter: CASE WHEN __scalar_sq_33.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_33.COUNT(UInt8(1)) END < CAST(t1.t1_int AS Int64)
-----Projection: t1.t1_int, __scalar_sq_33.COUNT(UInt8(1)), __scalar_sq_33.__always_true
-------Left Join: t1.t1_id = __scalar_sq_33.t2_id
+--Filter: CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_1.COUNT(UInt8(1)) END < CAST(t1.t1_int AS Int64)
+----Projection: t1.t1_int, __scalar_sq_1.COUNT(UInt8(1)), __scalar_sq_1.__always_true
+------Left Join: t1.t1_id = __scalar_sq_1.t2_id
 --------TableScan: t1 projection=[t1_id, t1_int]
---------SubqueryAlias: __scalar_sq_33
+--------SubqueryAlias: __scalar_sq_1
 ----------Projection: COUNT(UInt8(1)), t2.t2_id, __always_true
 ------------Aggregate: groupBy=[[t2.t2_id, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 --------------TableScan: t2 projection=[t2_id]
@@ -822,10 +822,10 @@ query TT
 explain SELECT t1_id, (SELECT count(*) + 2 as cnt_plus_2 FROM t2 WHERE t2.t2_int = t1.t1_int having count(*) >1) from t1
 ----
 logical_plan
-Projection: t1.t1_id, __scalar_sq_35.cnt_plus_2 AS cnt_plus_2
---Left Join: t1.t1_int = __scalar_sq_35.t2_int
+Projection: t1.t1_id, __scalar_sq_1.cnt_plus_2 AS cnt_plus_2
+--Left Join: t1.t1_int = __scalar_sq_1.t2_int
 ----TableScan: t1 projection=[t1_id, t1_int]
-----SubqueryAlias: __scalar_sq_35
+----SubqueryAlias: __scalar_sq_1
 ------Projection: COUNT(UInt8(1)) + Int64(2) AS cnt_plus_2, t2.t2_int
 --------Filter: COUNT(UInt8(1)) > Int64(1)
 ----------Projection: t2.t2_int, COUNT(UInt8(1))
@@ -846,10 +846,10 @@ query TT
 explain SELECT t1_id, (SELECT count(*) + 2 as cnt_plus_2 FROM t2 WHERE t2.t2_int = t1.t1_int having count(*) = 0) from t1
 ----
 logical_plan
-Projection: t1.t1_id, CASE WHEN __scalar_sq_37.__always_true IS NULL THEN Int64(2) AS cnt_plus_2 WHEN __scalar_sq_37.COUNT(UInt8(1)) != Int64(0) THEN NULL ELSE __scalar_sq_37.cnt_plus_2 END AS cnt_plus_2
---Left Join: t1.t1_int = __scalar_sq_37.t2_int
+Projection: t1.t1_id, CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(2) AS cnt_plus_2 WHEN __scalar_sq_1.COUNT(UInt8(1)) != Int64(0) THEN NULL ELSE __scalar_sq_1.cnt_plus_2 END AS cnt_plus_2
+--Left Join: t1.t1_int = __scalar_sq_1.t2_int
 ----TableScan: t1 projection=[t1_id, t1_int]
-----SubqueryAlias: __scalar_sq_37
+----SubqueryAlias: __scalar_sq_1
 ------Projection: COUNT(UInt8(1)) + Int64(2) AS cnt_plus_2, t2.t2_int, COUNT(UInt8(1)), __always_true
 --------Aggregate: groupBy=[[t2.t2_int, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 ----------TableScan: t2 projection=[t2_int]
@@ -868,12 +868,12 @@ explain select t1.t1_int from t1 group by t1.t1_int having (select count(*) from
 ----
 logical_plan
 Projection: t1.t1_int
---Filter: CASE WHEN __scalar_sq_39.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_39.COUNT(UInt8(1)) END = Int64(0)
-----Projection: t1.t1_int, __scalar_sq_39.COUNT(UInt8(1)), __scalar_sq_39.__always_true
-------Left Join: t1.t1_int = __scalar_sq_39.t2_int
+--Filter: CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_1.COUNT(UInt8(1)) END = Int64(0)
+----Projection: t1.t1_int, __scalar_sq_1.COUNT(UInt8(1)), __scalar_sq_1.__always_true
+------Left Join: t1.t1_int = __scalar_sq_1.t2_int
 --------Aggregate: groupBy=[[t1.t1_int]], aggr=[[]]
 ----------TableScan: t1 projection=[t1_int]
---------SubqueryAlias: __scalar_sq_39
+--------SubqueryAlias: __scalar_sq_1
 ----------Projection: COUNT(UInt8(1)), t2.t2_int, __always_true
 ------------Aggregate: groupBy=[[t2.t2_int, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 --------------TableScan: t2 projection=[t2_int]
@@ -890,11 +890,11 @@ explain select t1.t1_int from t1 where (select cnt from (select count(*) as cnt,
 ----
 logical_plan
 Projection: t1.t1_int
---Filter: CASE WHEN __scalar_sq_41.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_41.cnt END = Int64(0)
-----Projection: t1.t1_int, __scalar_sq_41.cnt, __scalar_sq_41.__always_true
-------Left Join: t1.t1_int = __scalar_sq_41.t2_int
+--Filter: CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_1.cnt END = Int64(0)
+----Projection: t1.t1_int, __scalar_sq_1.cnt, __scalar_sq_1.__always_true
+------Left Join: t1.t1_int = __scalar_sq_1.t2_int
 --------TableScan: t1 projection=[t1_int]
---------SubqueryAlias: __scalar_sq_41
+--------SubqueryAlias: __scalar_sq_1
 ----------Projection: COUNT(UInt8(1)) AS cnt, t2.t2_int, __always_true
 ------------Aggregate: groupBy=[[t2.t2_int, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 --------------TableScan: t2 projection=[t2_int]
@@ -920,11 +920,11 @@ select t1.t1_int from t1 where (
 ----
 logical_plan
 Projection: t1.t1_int
---Filter: CASE WHEN __scalar_sq_43.__always_true IS NULL THEN Int64(2) WHEN __scalar_sq_43.COUNT(UInt8(1)) != Int64(0) THEN NULL ELSE __scalar_sq_43.cnt_plus_two END = Int64(2)
-----Projection: t1.t1_int, __scalar_sq_43.cnt_plus_two, __scalar_sq_43.COUNT(UInt8(1)), __scalar_sq_43.__always_true
-------Left Join: t1.t1_int = __scalar_sq_43.t2_int
+--Filter: CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(2) WHEN __scalar_sq_1.COUNT(UInt8(1)) != Int64(0) THEN NULL ELSE __scalar_sq_1.cnt_plus_two END = Int64(2)
+----Projection: t1.t1_int, __scalar_sq_1.cnt_plus_two, __scalar_sq_1.COUNT(UInt8(1)), __scalar_sq_1.__always_true
+------Left Join: t1.t1_int = __scalar_sq_1.t2_int
 --------TableScan: t1 projection=[t1_int]
---------SubqueryAlias: __scalar_sq_43
+--------SubqueryAlias: __scalar_sq_1
 ----------Projection: COUNT(UInt8(1)) + Int64(1) + Int64(1) AS cnt_plus_two, t2.t2_int, COUNT(UInt8(1)), __always_true
 ------------Aggregate: groupBy=[[t2.t2_int, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 --------------TableScan: t2 projection=[t2_int]
@@ -949,11 +949,11 @@ select t1.t1_int from t1 where
 ----
 logical_plan
 Projection: t1.t1_int
---Filter: CASE WHEN __scalar_sq_45.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_45.cnt END = Int64(0)
-----Projection: t1.t1_int, __scalar_sq_45.cnt, __scalar_sq_45.__always_true
-------Left Join: t1.t1_int = __scalar_sq_45.t2_int
+--Filter: CASE WHEN __scalar_sq_1.__always_true IS NULL THEN Int64(0) ELSE __scalar_sq_1.cnt END = Int64(0)
+----Projection: t1.t1_int, __scalar_sq_1.cnt, __scalar_sq_1.__always_true
+------Left Join: t1.t1_int = __scalar_sq_1.t2_int
 --------TableScan: t1 projection=[t1_int]
---------SubqueryAlias: __scalar_sq_45
+--------SubqueryAlias: __scalar_sq_1
 ----------Projection: CASE WHEN COUNT(UInt8(1)) = Int64(1) THEN Int64(NULL) ELSE COUNT(UInt8(1)) END AS cnt, t2.t2_int, __always_true
 ------------Aggregate: groupBy=[[t2.t2_int, Boolean(true) AS __always_true]], aggr=[[COUNT(UInt8(1))]]
 --------------TableScan: t2 projection=[t2_int]
@@ -965,8 +965,3 @@ select t1.t1_int from t1 where
 ----
 2
 4
-
-
-
-
-
diff --git a/datafusion/core/tests/sqllogictests/test_files/tpch/q15.slt.part b/datafusion/core/tests/sqllogictests/test_files/tpch/q15.slt.part
index 0406b4f70f..1e76e63cd3 100644
--- a/datafusion/core/tests/sqllogictests/test_files/tpch/q15.slt.part
+++ b/datafusion/core/tests/sqllogictests/test_files/tpch/q15.slt.part
@@ -52,7 +52,7 @@ order by
 logical_plan
 Sort: supplier.s_suppkey ASC NULLS LAST
 --Projection: supplier.s_suppkey, supplier.s_name, supplier.s_address, supplier.s_phone, total_revenue
-----Inner Join: total_revenue = __scalar_sq_3.MAX(total_revenue)
+----Inner Join: total_revenue = __scalar_sq_1.MAX(total_revenue)
 ------Projection: supplier.s_suppkey, supplier.s_name, supplier.s_address, supplier.s_phone, total_revenue
 --------Inner Join: supplier.s_suppkey = supplier_no
 ----------TableScan: supplier projection=[s_suppkey, s_name, s_address, s_phone]
@@ -62,7 +62,7 @@ Sort: supplier.s_suppkey ASC NULLS LAST
 ----------------Projection: lineitem.l_suppkey, lineitem.l_extendedprice, lineitem.l_discount
 ------------------Filter: lineitem.l_shipdate >= Date32("9496") AND lineitem.l_shipdate < Date32("9587")
 --------------------TableScan: lineitem projection=[l_suppkey, l_extendedprice, l_discount, l_shipdate], partial_filters=[lineitem.l_shipdate >= Date32("9496"), lineitem.l_shipdate < Date32("9587")]
-------SubqueryAlias: __scalar_sq_3
+------SubqueryAlias: __scalar_sq_1
 --------Aggregate: groupBy=[[]], aggr=[[MAX(total_revenue)]]
 ----------Projection: revenue0.SUM(lineitem.l_extendedprice * Int64(1) - lineitem.l_discount) AS total_revenue
 ------------SubqueryAlias: revenue0
diff --git a/datafusion/core/tests/sqllogictests/test_files/tpch/q17.slt.part b/datafusion/core/tests/sqllogictests/test_files/tpch/q17.slt.part
index 4f52711f29..364369016b 100644
--- a/datafusion/core/tests/sqllogictests/test_files/tpch/q17.slt.part
+++ b/datafusion/core/tests/sqllogictests/test_files/tpch/q17.slt.part
@@ -39,14 +39,14 @@ logical_plan
 Projection: CAST(SUM(lineitem.l_extendedprice) AS Float64) / Float64(7) AS avg_yearly
 --Aggregate: groupBy=[[]], aggr=[[SUM(lineitem.l_extendedprice)]]
 ----Projection: lineitem.l_extendedprice
-------Inner Join: part.p_partkey = __scalar_sq_5.l_partkey Filter: CAST(lineitem.l_quantity AS Decimal128(30, 15)) < __scalar_sq_5.Float64(0.2) * AVG(lineitem.l_quantity)
+------Inner Join: part.p_partkey = __scalar_sq_1.l_partkey Filter: CAST(lineitem.l_quantity AS Decimal128(30, 15)) < __scalar_sq_1.Float64(0.2) * AVG(lineitem.l_quantity)
 --------Projection: lineitem.l_quantity, lineitem.l_extendedprice, part.p_partkey
 ----------Inner Join: lineitem.l_partkey = part.p_partkey
 ------------TableScan: lineitem projection=[l_partkey, l_quantity, l_extendedprice]
 ------------Projection: part.p_partkey
 --------------Filter: part.p_brand = Utf8("Brand#23") AND part.p_container = Utf8("MED BOX")
 ----------------TableScan: part projection=[p_partkey, p_brand, p_container], partial_filters=[part.p_brand = Utf8("Brand#23"), part.p_container = Utf8("MED BOX")]
---------SubqueryAlias: __scalar_sq_5
+--------SubqueryAlias: __scalar_sq_1
 ----------Projection: CAST(Float64(0.2) * CAST(AVG(lineitem.l_quantity) AS Float64) AS Decimal128(30, 15)), lineitem.l_partkey
 ------------Aggregate: groupBy=[[lineitem.l_partkey]], aggr=[[AVG(lineitem.l_quantity)]]
 --------------TableScan: lineitem projection=[l_partkey, l_quantity]
diff --git a/datafusion/core/tests/sqllogictests/test_files/tpch/q18.slt.part b/datafusion/core/tests/sqllogictests/test_files/tpch/q18.slt.part
index f7a96b8b64..01d683ffe4 100644
--- a/datafusion/core/tests/sqllogictests/test_files/tpch/q18.slt.part
+++ b/datafusion/core/tests/sqllogictests/test_files/tpch/q18.slt.part
@@ -53,7 +53,7 @@ order by
 logical_plan
 Sort: orders.o_totalprice DESC NULLS FIRST, orders.o_orderdate ASC NULLS LAST
 --Aggregate: groupBy=[[customer.c_name, customer.c_custkey, orders.o_orderkey, orders.o_orderdate, orders.o_totalprice]], aggr=[[SUM(lineitem.l_quantity)]]
-----LeftSemi Join: orders.o_orderkey = __correlated_sq_3.l_orderkey
+----LeftSemi Join: orders.o_orderkey = __correlated_sq_1.l_orderkey
 ------Projection: customer.c_custkey, customer.c_name, orders.o_orderkey, orders.o_totalprice, orders.o_orderdate, lineitem.l_quantity
 --------Inner Join: orders.o_orderkey = lineitem.l_orderkey
 ----------Projection: customer.c_custkey, customer.c_name, orders.o_orderkey, orders.o_totalprice, orders.o_orderdate
@@ -61,7 +61,7 @@ Sort: orders.o_totalprice DESC NULLS FIRST, orders.o_orderdate ASC NULLS LAST
 --------------TableScan: customer projection=[c_custkey, c_name]
 --------------TableScan: orders projection=[o_orderkey, o_custkey, o_totalprice, o_orderdate]
 ----------TableScan: lineitem projection=[l_orderkey, l_quantity]
-------SubqueryAlias: __correlated_sq_3
+------SubqueryAlias: __correlated_sq_1
 --------Projection: lineitem.l_orderkey
 ----------Filter: SUM(lineitem.l_quantity) > Decimal128(Some(30000),25,2)
 ------------Aggregate: groupBy=[[lineitem.l_orderkey]], aggr=[[SUM(lineitem.l_quantity)]]
diff --git a/datafusion/core/tests/sqllogictests/test_files/tpch/q2.slt.part b/datafusion/core/tests/sqllogictests/test_files/tpch/q2.slt.part
index 8203642869..ab9faeeb76 100644
--- a/datafusion/core/tests/sqllogictests/test_files/tpch/q2.slt.part
+++ b/datafusion/core/tests/sqllogictests/test_files/tpch/q2.slt.part
@@ -66,7 +66,7 @@ logical_plan
 Limit: skip=0, fetch=10
 --Sort: supplier.s_acctbal DESC NULLS FIRST, nation.n_name ASC NULLS LAST, supplier.s_name ASC NULLS LAST, part.p_partkey ASC NULLS LAST, fetch=10
 ----Projection: supplier.s_acctbal, supplier.s_name, nation.n_name, part.p_partkey, part.p_mfgr, supplier.s_address, supplier.s_phone, supplier.s_comment
-------Inner Join: part.p_partkey = __scalar_sq_7.ps_partkey, partsupp.ps_supplycost = __scalar_sq_7.MIN(partsupp.ps_supplycost)
+------Inner Join: part.p_partkey = __scalar_sq_1.ps_partkey, partsupp.ps_supplycost = __scalar_sq_1.MIN(partsupp.ps_supplycost)
 --------Projection: part.p_partkey, part.p_mfgr, supplier.s_name, supplier.s_address, supplier.s_phone, supplier.s_acctbal, supplier.s_comment, partsupp.ps_supplycost, nation.n_name
 ----------Inner Join: nation.n_regionkey = region.r_regionkey
 ------------Projection: part.p_partkey, part.p_mfgr, supplier.s_name, supplier.s_address, supplier.s_phone, supplier.s_acctbal, supplier.s_comment, partsupp.ps_supplycost, nation.n_name, nation.n_regionkey
@@ -84,7 +84,7 @@ Limit: skip=0, fetch=10
 ------------Projection: region.r_regionkey
 --------------Filter: region.r_name = Utf8("EUROPE")
 ----------------TableScan: region projection=[r_regionkey, r_name], partial_filters=[region.r_name = Utf8("EUROPE")]
---------SubqueryAlias: __scalar_sq_7
+--------SubqueryAlias: __scalar_sq_1
 ----------Projection: MIN(partsupp.ps_supplycost), partsupp.ps_partkey
 ------------Aggregate: groupBy=[[partsupp.ps_partkey]], aggr=[[MIN(partsupp.ps_supplycost)]]
 --------------Projection: partsupp.ps_partkey, partsupp.ps_supplycost
@@ -240,7 +240,7 @@ order by
     p_partkey
 limit 10;
 ----
-9828.21 Supplier#000000647 UNITED KINGDOM 13120 Manufacturer#5 x5U7MBZmwfG9 33-258-202-4782 s the slyly even ideas poach fluffily 
+9828.21 Supplier#000000647 UNITED KINGDOM 13120 Manufacturer#5 x5U7MBZmwfG9 33-258-202-4782 s the slyly even ideas poach fluffily
 9508.37 Supplier#000000070 FRANCE 3563 Manufacturer#1 INWNH2w,OOWgNDq0BRCcBwOMQc6PdFDc4 16-821-608-1166 ests sleep quickly express ideas. ironic ideas haggle about the final T
 9508.37 Supplier#000000070 FRANCE 17268 Manufacturer#4 INWNH2w,OOWgNDq0BRCcBwOMQc6PdFDc4 16-821-608-1166 ests sleep quickly express ideas. ironic ideas haggle about the final T
 9453.01 Supplier#000000802 ROMANIA 10021 Manufacturer#5 ,6HYXb4uaHITmtMBj4Ak57Pd 29-342-882-6463 gular frets. permanently special multipliers believe blithely alongs
diff --git a/datafusion/core/tests/sqllogictests/test_files/tpch/q20.slt.part b/datafusion/core/tests/sqllogictests/test_files/tpch/q20.slt.part
index 8e2af2c340..ef0ec5dfb3 100644
--- a/datafusion/core/tests/sqllogictests/test_files/tpch/q20.slt.part
+++ b/datafusion/core/tests/sqllogictests/test_files/tpch/q20.slt.part
@@ -58,23 +58,23 @@ order by
 logical_plan
 Sort: supplier.s_name ASC NULLS LAST
 --Projection: supplier.s_name, supplier.s_address
-----LeftSemi Join: supplier.s_suppkey = __correlated_sq_5.ps_suppkey
+----LeftSemi Join: supplier.s_suppkey = __correlated_sq_1.ps_suppkey
 ------Projection: supplier.s_suppkey, supplier.s_name, supplier.s_address
 --------Inner Join: supplier.s_nationkey = nation.n_nationkey
 ----------TableScan: supplier projection=[s_suppkey, s_name, s_address, s_nationkey]
 ----------Projection: nation.n_nationkey
 ------------Filter: nation.n_name = Utf8("CANADA")
 --------------TableScan: nation projection=[n_nationkey, n_name], partial_filters=[nation.n_name = Utf8("CANADA")]
-------SubqueryAlias: __correlated_sq_5
+------SubqueryAlias: __correlated_sq_1
 --------Projection: partsupp.ps_suppkey
-----------Inner Join: partsupp.ps_partkey = __scalar_sq_9.l_partkey, partsupp.ps_suppkey = __scalar_sq_9.l_suppkey Filter: CAST(partsupp.ps_availqty AS Float64) > __scalar_sq_9.Float64(0.5) * SUM(lineitem.l_quantity)
-------------LeftSemi Join: partsupp.ps_partkey = __correlated_sq_6.p_partkey
+----------Inner Join: partsupp.ps_partkey = __scalar_sq_3.l_partkey, partsupp.ps_suppkey = __scalar_sq_3.l_suppkey Filter: CAST(partsupp.ps_availqty AS Float64) > __scalar_sq_3.Float64(0.5) * SUM(lineitem.l_quantity)
+------------LeftSemi Join: partsupp.ps_partkey = __correlated_sq_2.p_partkey
 --------------TableScan: partsupp projection=[ps_partkey, ps_suppkey, ps_availqty]
---------------SubqueryAlias: __correlated_sq_6
+--------------SubqueryAlias: __correlated_sq_2
 ----------------Projection: part.p_partkey
 ------------------Filter: part.p_name LIKE Utf8("forest%")
 --------------------TableScan: part projection=[p_partkey, p_name], partial_filters=[part.p_name LIKE Utf8("forest%")]
-------------SubqueryAlias: __scalar_sq_9
+------------SubqueryAlias: __scalar_sq_3
 --------------Projection: Float64(0.5) * CAST(SUM(lineitem.l_quantity) AS Float64), lineitem.l_partkey, lineitem.l_suppkey
 ----------------Aggregate: groupBy=[[lineitem.l_partkey, lineitem.l_suppkey]], aggr=[[SUM(lineitem.l_quantity)]]
 ------------------Projection: lineitem.l_partkey, lineitem.l_suppkey, lineitem.l_quantity
diff --git a/datafusion/core/tests/sqllogictests/test_files/tpch/q21.slt.part b/datafusion/core/tests/sqllogictests/test_files/tpch/q21.slt.part
index bdc8e2076b..33bff25609 100644
--- a/datafusion/core/tests/sqllogictests/test_files/tpch/q21.slt.part
+++ b/datafusion/core/tests/sqllogictests/test_files/tpch/q21.slt.part
@@ -62,8 +62,8 @@ Sort: numwait DESC NULLS FIRST, supplier.s_name ASC NULLS LAST
 --Projection: supplier.s_name, COUNT(UInt8(1)) AS numwait
 ----Aggregate: groupBy=[[supplier.s_name]], aggr=[[COUNT(UInt8(1))]]
 ------Projection: supplier.s_name
---------LeftAnti Join: l1.l_orderkey = __correlated_sq_10.l_orderkey Filter: __correlated_sq_10.l_suppkey != l1.l_suppkey
-----------LeftSemi Join: l1.l_orderkey = __correlated_sq_9.l_orderkey Filter: __correlated_sq_9.l_suppkey != l1.l_suppkey
+--------LeftAnti Join: l1.l_orderkey = __correlated_sq_2.l_orderkey Filter: __correlated_sq_2.l_suppkey != l1.l_suppkey
+----------LeftSemi Join: l1.l_orderkey = __correlated_sq_1.l_orderkey Filter: __correlated_sq_1.l_suppkey != l1.l_suppkey
 ------------Projection: supplier.s_name, l1.l_orderkey, l1.l_suppkey
 --------------Inner Join: supplier.s_nationkey = nation.n_nationkey
 ----------------Projection: supplier.s_name, supplier.s_nationkey, l1.l_orderkey, l1.l_suppkey
@@ -81,10 +81,10 @@ Sort: numwait DESC NULLS FIRST, supplier.s_name ASC NULLS LAST
 ----------------Projection: nation.n_nationkey
 ------------------Filter: nation.n_name = Utf8("SAUDI ARABIA")
 --------------------TableScan: nation projection=[n_nationkey, n_name], partial_filters=[nation.n_name = Utf8("SAUDI ARABIA")]
-------------SubqueryAlias: __correlated_sq_9
+------------SubqueryAlias: __correlated_sq_1
 --------------SubqueryAlias: l2
 ----------------TableScan: lineitem projection=[l_orderkey, l_suppkey]
-----------SubqueryAlias: __correlated_sq_10
+----------SubqueryAlias: __correlated_sq_2
 ------------SubqueryAlias: l3
 --------------Projection: lineitem.l_orderkey, lineitem.l_suppkey
 ----------------Filter: lineitem.l_receiptdate > lineitem.l_commitdate
diff --git a/datafusion/core/tests/sqllogictests/test_files/tpch/q22.slt.part b/datafusion/core/tests/sqllogictests/test_files/tpch/q22.slt.part
index 9f8b651f53..35024c4b0d 100644
--- a/datafusion/core/tests/sqllogictests/test_files/tpch/q22.slt.part
+++ b/datafusion/core/tests/sqllogictests/test_files/tpch/q22.slt.part
@@ -61,14 +61,14 @@ Sort: custsale.cntrycode ASC NULLS LAST
 ----Aggregate: groupBy=[[custsale.cntrycode]], aggr=[[COUNT(UInt8(1)), SUM(custsale.c_acctbal)]]
 ------SubqueryAlias: custsale
 --------Projection: substr(customer.c_phone, Int64(1), Int64(2)) AS cntrycode, customer.c_acctbal
-----------Inner Join:  Filter: CAST(customer.c_acctbal AS Decimal128(19, 6)) > __scalar_sq_11.AVG(customer.c_acctbal)
+----------Inner Join:  Filter: CAST(customer.c_acctbal AS Decimal128(19, 6)) > __scalar_sq_2.AVG(customer.c_acctbal)
 ------------Projection: customer.c_phone, customer.c_acctbal
---------------LeftAnti Join: customer.c_custkey = __correlated_sq_13.o_custkey
+--------------LeftAnti Join: customer.c_custkey = __correlated_sq_1.o_custkey
 ----------------Filter: substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8("13"), Utf8("31"), Utf8("23"), Utf8("29"), Utf8("30"), Utf8("18"), Utf8("17")])
 ------------------TableScan: customer projection=[c_custkey, c_phone, c_acctbal], partial_filters=[substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8("13"), Utf8("31"), Utf8("23"), Utf8("29"), Utf8("30"), Utf8("18"), Utf8("17")])]
-----------------SubqueryAlias: __correlated_sq_13
+----------------SubqueryAlias: __correlated_sq_1
 ------------------TableScan: orders projection=[o_custkey]
-------------SubqueryAlias: __scalar_sq_11
+------------SubqueryAlias: __scalar_sq_2
 --------------Aggregate: groupBy=[[]], aggr=[[AVG(customer.c_acctbal)]]
 ----------------Projection: customer.c_acctbal
 ------------------Filter: customer.c_acctbal > Decimal128(Some(0),15,2) AND substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8("13"), Utf8("31"), Utf8("23"), Utf8("29"), Utf8("30"), Utf8("18"), Utf8("17")])
diff --git a/datafusion/core/tests/sqllogictests/test_files/tpch/q4.slt.part b/datafusion/core/tests/sqllogictests/test_files/tpch/q4.slt.part
index 109bcd6d5c..f3c428ae78 100644
--- a/datafusion/core/tests/sqllogictests/test_files/tpch/q4.slt.part
+++ b/datafusion/core/tests/sqllogictests/test_files/tpch/q4.slt.part
@@ -44,11 +44,11 @@ Sort: orders.o_orderpriority ASC NULLS LAST
 --Projection: orders.o_orderpriority, COUNT(UInt8(1)) AS order_count
 ----Aggregate: groupBy=[[orders.o_orderpriority]], aggr=[[COUNT(UInt8(1))]]
 ------Projection: orders.o_orderpriority
---------LeftSemi Join: orders.o_orderkey = __correlated_sq_15.l_orderkey
+--------LeftSemi Join: orders.o_orderkey = __correlated_sq_1.l_orderkey
 ----------Projection: orders.o_orderkey, orders.o_orderpriority
 ------------Filter: orders.o_orderdate >= Date32("8582") AND orders.o_orderdate < Date32("8674")
 --------------TableScan: orders projection=[o_orderkey, o_orderdate, o_orderpriority], partial_filters=[orders.o_orderdate >= Date32("8582"), orders.o_orderdate < Date32("8674")]
-----------SubqueryAlias: __correlated_sq_15
+----------SubqueryAlias: __correlated_sq_1
 ------------Projection: lineitem.l_orderkey
 --------------Filter: lineitem.l_commitdate < lineitem.l_receiptdate
 ----------------TableScan: lineitem projection=[l_orderkey, l_commitdate, l_receiptdate], partial_filters=[lineitem.l_commitdate < lineitem.l_receiptdate]
diff --git a/datafusion/optimizer/src/decorrelate_predicate_subquery.rs b/datafusion/optimizer/src/decorrelate_predicate_subquery.rs
index 80ceeb11e2..d3500304af 100644
--- a/datafusion/optimizer/src/decorrelate_predicate_subquery.rs
+++ b/datafusion/optimizer/src/decorrelate_predicate_subquery.rs
@@ -15,11 +15,11 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use crate::alias::AliasGenerator;
 use crate::decorrelate::PullUpCorrelatedExpr;
 use crate::optimizer::ApplyOrder;
 use crate::utils::{conjunction, replace_qualified_name, split_conjunction};
 use crate::{OptimizerConfig, OptimizerRule};
+use datafusion_common::alias::AliasGenerator;
 use datafusion_common::tree_node::TreeNode;
 use datafusion_common::{Column, DataFusionError, Result};
 use datafusion_expr::expr::{Exists, InSubquery};
@@ -36,9 +36,7 @@ use std::sync::Arc;
 
 /// Optimizer rule for rewriting predicate(IN/EXISTS) subquery to left semi/anti joins
 #[derive(Default)]
-pub struct DecorrelatePredicateSubquery {
-    alias: AliasGenerator,
-}
+pub struct DecorrelatePredicateSubquery {}
 
 impl DecorrelatePredicateSubquery {
     #[allow(missing_docs)]
@@ -115,7 +113,9 @@ impl OptimizerRule for DecorrelatePredicateSubquery {
                 // iterate through all exists clauses in predicate, turning each into a join
                 let mut cur_input = filter.input.as_ref().clone();
                 for subquery in subqueries {
-                    if let Some(plan) = build_join(&subquery, &cur_input, &self.alias)? {
+                    if let Some(plan) =
+                        build_join(&subquery, &cur_input, config.alias_generator())?
+                    {
                         cur_input = plan;
                     } else {
                         // If the subquery can not be converted to a Join, reconstruct the subquery expression and add it to the Filter
@@ -198,7 +198,7 @@ impl OptimizerRule for DecorrelatePredicateSubquery {
 fn build_join(
     query_info: &SubqueryInfo,
     left: &LogicalPlan,
-    alias: &AliasGenerator,
+    alias: Arc<AliasGenerator>,
 ) -> Result<Option<LogicalPlan>> {
     let where_in_expr_opt = &query_info.where_in_expr;
     let in_predicate_opt = where_in_expr_opt
diff --git a/datafusion/optimizer/src/lib.rs b/datafusion/optimizer/src/lib.rs
index 9144042bed..1d12ca7e39 100644
--- a/datafusion/optimizer/src/lib.rs
+++ b/datafusion/optimizer/src/lib.rs
@@ -15,7 +15,6 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub mod alias;
 pub mod analyzer;
 pub mod common_subexpr_eliminate;
 pub mod decorrelate;
diff --git a/datafusion/optimizer/src/optimizer.rs b/datafusion/optimizer/src/optimizer.rs
index a933acdffd..2306593d42 100644
--- a/datafusion/optimizer/src/optimizer.rs
+++ b/datafusion/optimizer/src/optimizer.rs
@@ -42,6 +42,7 @@ use crate::single_distinct_to_groupby::SingleDistinctToGroupBy;
 use crate::unwrap_cast_in_comparison::UnwrapCastInComparison;
 use crate::utils::log_plan;
 use chrono::{DateTime, Utc};
+use datafusion_common::alias::AliasGenerator;
 use datafusion_common::config::ConfigOptions;
 use datafusion_common::{DataFusionError, Result};
 use datafusion_expr::logical_plan::LogicalPlan;
@@ -80,6 +81,9 @@ pub trait OptimizerConfig {
     /// time is used as the value for now()
     fn query_execution_start_time(&self) -> DateTime<Utc>;
 
+    /// Return alias generator used to generate unique aliases for subqueries
+    fn alias_generator(&self) -> Arc<AliasGenerator>;
+
     fn options(&self) -> &ConfigOptions;
 }
 
@@ -91,6 +95,9 @@ pub struct OptimizerContext {
     /// expressions such as `now()` to use a literal value instead
     query_execution_start_time: DateTime<Utc>,
 
+    /// Alias generator used to generate unique aliases for subqueries
+    alias_generator: Arc<AliasGenerator>,
+
     options: ConfigOptions,
 }
 
@@ -102,6 +109,7 @@ impl OptimizerContext {
 
         Self {
             query_execution_start_time: Utc::now(),
+            alias_generator: Arc::new(AliasGenerator::new()),
             options,
         }
     }
@@ -148,6 +156,10 @@ impl OptimizerConfig for OptimizerContext {
         self.query_execution_start_time
     }
 
+    fn alias_generator(&self) -> Arc<AliasGenerator> {
+        self.alias_generator.clone()
+    }
+
     fn options(&self) -> &ConfigOptions {
         &self.options
     }
diff --git a/datafusion/optimizer/src/scalar_subquery_to_join.rs b/datafusion/optimizer/src/scalar_subquery_to_join.rs
index e5f491b2c8..357a2775cc 100644
--- a/datafusion/optimizer/src/scalar_subquery_to_join.rs
+++ b/datafusion/optimizer/src/scalar_subquery_to_join.rs
@@ -15,11 +15,11 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use crate::alias::AliasGenerator;
 use crate::decorrelate::{PullUpCorrelatedExpr, UN_MATCHED_ROW_INDICATOR};
 use crate::optimizer::ApplyOrder;
 use crate::utils::{conjunction, replace_qualified_name};
 use crate::{OptimizerConfig, OptimizerRule};
+use datafusion_common::alias::AliasGenerator;
 use datafusion_common::tree_node::{
     RewriteRecursion, Transformed, TreeNode, TreeNodeRewriter,
 };
@@ -32,9 +32,7 @@ use std::sync::Arc;
 
 /// Optimizer rule for rewriting subquery filters to joins
 #[derive(Default)]
-pub struct ScalarSubqueryToJoin {
-    alias: Arc<AliasGenerator>,
-}
+pub struct ScalarSubqueryToJoin {}
 
 impl ScalarSubqueryToJoin {
     #[allow(missing_docs)]
@@ -66,12 +64,14 @@ impl OptimizerRule for ScalarSubqueryToJoin {
     fn try_optimize(
         &self,
         plan: &LogicalPlan,
-        _config: &dyn OptimizerConfig,
+        config: &dyn OptimizerConfig,
     ) -> Result<Option<LogicalPlan>> {
         match plan {
             LogicalPlan::Filter(filter) => {
-                let (subqueries, mut rewrite_expr) =
-                    self.extract_subquery_exprs(&filter.predicate, self.alias.clone())?;
+                let (subqueries, mut rewrite_expr) = self.extract_subquery_exprs(
+                    &filter.predicate,
+                    config.alias_generator(),
+                )?;
 
                 if subqueries.is_empty() {
                     // regular filter, no subquery exists clause here
@@ -117,7 +117,7 @@ impl OptimizerRule for ScalarSubqueryToJoin {
                 let mut subquery_to_expr_map = HashMap::new();
                 for expr in projection.expr.iter() {
                     let (subqueries, rewrite_exprs) =
-                        self.extract_subquery_exprs(expr, self.alias.clone())?;
+                        self.extract_subquery_exprs(expr, config.alias_generator())?;
                     for (subquery, _) in &subqueries {
                         subquery_to_expr_map.insert(subquery.clone(), expr.clone());
                     }
diff --git a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
index 8aebae18c1..212b8b516d 100644
--- a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
+++ b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
@@ -1220,6 +1220,7 @@ mod tests {
         datatypes::{DataType, Field, Schema},
     };
     use chrono::{DateTime, TimeZone, Utc};
+    use datafusion_common::alias::AliasGenerator;
     use datafusion_common::{assert_contains, cast::as_int32_array, DFField, ToDFSchema};
     use datafusion_expr::*;
     use datafusion_physical_expr::{
@@ -1315,6 +1316,7 @@ mod tests {
     ) {
         let execution_props = ExecutionProps {
             query_execution_start_time: *date_time,
+            alias_generator: Arc::new(AliasGenerator::new()),
             var_providers: None,
         };
 
diff --git a/datafusion/physical-expr/src/execution_props.rs b/datafusion/physical-expr/src/execution_props.rs
index 5849850031..f20d7f1855 100644
--- a/datafusion/physical-expr/src/execution_props.rs
+++ b/datafusion/physical-expr/src/execution_props.rs
@@ -17,6 +17,7 @@
 
 use crate::var_provider::{VarProvider, VarType};
 use chrono::{DateTime, TimeZone, Utc};
+use datafusion_common::alias::AliasGenerator;
 use std::collections::HashMap;
 use std::sync::Arc;
 
@@ -34,6 +35,8 @@ use std::sync::Arc;
 #[derive(Clone, Debug)]
 pub struct ExecutionProps {
     pub query_execution_start_time: DateTime<Utc>,
+    /// Alias generator used by subquery optimizer rules
+    pub alias_generator: Arc<AliasGenerator>,
     /// Providers for scalar variables
     pub var_providers: Option<HashMap<VarType, Arc<dyn VarProvider + Send + Sync>>>,
 }
@@ -51,13 +54,16 @@ impl ExecutionProps {
             // Set this to a fixed sentinel to make it obvious if this is
             // not being updated / propagated correctly
             query_execution_start_time: Utc.timestamp_nanos(0),
+            alias_generator: Arc::new(AliasGenerator::new()),
             var_providers: None,
         }
     }
 
-    /// Marks the execution of query started timestamp
+    /// Marks the execution of query started timestamp.
+    /// This also instantiates a new alias generator.
     pub fn start_execution(&mut self) -> &Self {
         self.query_execution_start_time = Utc::now();
+        self.alias_generator = Arc::new(AliasGenerator::new());
         &*self
     }
 
@@ -94,6 +100,6 @@ mod test {
     #[test]
     fn debug() {
         let props = ExecutionProps::new();
-        assert_eq!("ExecutionProps { query_execution_start_time: 1970-01-01T00:00:00Z, var_providers: None }", format!("{props:?}"));
+        assert_eq!("ExecutionProps { query_execution_start_time: 1970-01-01T00:00:00Z, alias_generator: AliasGenerator { next_id: 1 }, var_providers: None }", format!("{props:?}"));
     }
 }