@@ -7,7 +7,7 @@ create table t3(t3v2 int, t3v4 int);
7
7
8
8
*/
9
9
10
- -- Test whether the optimizer can unnest correlated subqueries.
10
+ -- Test whether the optimizer can unnest correlated subqueries with (scalar op agg)
11
11
select * from t1 where (select sum (t2v3) from t2 where t2v1 = t1v1) > 100 ;
12
12
13
13
/*
@@ -74,6 +74,142 @@ PhysicalProjection { exprs: [ #2, #3 ], cost: {compute=18005,io=3000}, stat: {ro
74
74
└── PhysicalScan { table: t1, cost: {compute=0,io=1000}, stat: {row_cnt=1000} }
75
75
*/
76
76
77
+ -- Test whether the optimizer can unnest correlated subqueries with (scalar op group agg)
78
+ select * from t1 where (select sum (sumt2v3) from (select t2v1, sum (t2v3) as sumt2v3 from t2 where t2v1 = t1v1 group by t2v1)) > 100 ;
79
+
80
+ /*
81
+ LogicalProjection { exprs: [ #0, #1 ] }
82
+ └── LogicalFilter
83
+ ├── cond:Gt
84
+ │ ├── #2
85
+ │ └── 100(i64)
86
+ └── RawDependentJoin { join_type: Cross, cond: true, extern_cols: [ Extern(#0) ] }
87
+ ├── LogicalScan { table: t1 }
88
+ └── LogicalProjection { exprs: [ #0 ] }
89
+ └── LogicalAgg
90
+ ├── exprs:Agg(Sum)
91
+ │ └── [ #1 ]
92
+ ├── groups: []
93
+ └── LogicalProjection { exprs: [ #0, #1 ] }
94
+ └── LogicalAgg
95
+ ├── exprs:Agg(Sum)
96
+ │ └── [ Cast { cast_to: Int64, child: #1 } ]
97
+ ├── groups: [ #0 ]
98
+ └── LogicalFilter
99
+ ├── cond:Eq
100
+ │ ├── #0
101
+ │ └── Extern(#0)
102
+ └── LogicalScan { table: t2 }
103
+ LogicalProjection { exprs: [ #0, #1 ] }
104
+ └── LogicalFilter
105
+ ├── cond:Gt
106
+ │ ├── #2
107
+ │ └── 100(i64)
108
+ └── LogicalProjection { exprs: [ #0, #1, #3 ] }
109
+ └── LogicalJoin
110
+ ├── join_type: Inner
111
+ ├── cond:Eq
112
+ │ ├── #0
113
+ │ └── #2
114
+ ├── LogicalScan { table: t1 }
115
+ └── LogicalProjection { exprs: [ #0, #1 ] }
116
+ └── LogicalAgg
117
+ ├── exprs:Agg(Sum)
118
+ │ └── [ #2 ]
119
+ ├── groups: [ #0 ]
120
+ └── LogicalProjection { exprs: [ #0, #1, #2 ] }
121
+ └── LogicalAgg
122
+ ├── exprs:Agg(Sum)
123
+ │ └── [ Cast { cast_to: Int64, child: #2 } ]
124
+ ├── groups: [ #0, #1 ]
125
+ └── LogicalFilter
126
+ ├── cond:Eq
127
+ │ ├── #1
128
+ │ └── #0
129
+ └── LogicalJoin { join_type: Inner, cond: true }
130
+ ├── LogicalAgg { exprs: [], groups: [ #0 ] }
131
+ │ └── LogicalScan { table: t1 }
132
+ └── LogicalScan { table: t2 }
133
+ PhysicalProjection { exprs: [ #2, #3 ], cost: {compute=25005,io=3000}, stat: {row_cnt=1} }
134
+ └── PhysicalHashJoin { join_type: Inner, left_keys: [ #0 ], right_keys: [ #0 ], cost: {compute=25002,io=3000}, stat: {row_cnt=1} }
135
+ ├── PhysicalFilter
136
+ │ ├── cond:Gt
137
+ │ │ ├── #1
138
+ │ │ └── 100(i64)
139
+ │ ├── cost: {compute=24000,io=2000}
140
+ │ ├── stat: {row_cnt=1}
141
+ │ └── PhysicalAgg
142
+ │ ├── aggrs:Agg(Sum)
143
+ │ │ └── [ #2 ]
144
+ │ ├── groups: [ #0 ]
145
+ │ ├── cost: {compute=21000,io=2000}
146
+ │ ├── stat: {row_cnt=1000}
147
+ │ └── PhysicalAgg
148
+ │ ├── aggrs:Agg(Sum)
149
+ │ │ └── [ Cast { cast_to: Int64, child: #2 } ]
150
+ │ ├── groups: [ #0, #1 ]
151
+ │ ├── cost: {compute=15000,io=2000}
152
+ │ ├── stat: {row_cnt=1000}
153
+ │ └── PhysicalHashJoin { join_type: Inner, left_keys: [ #0 ], right_keys: [ #0 ], cost: {compute=6000,io=2000}, stat: {row_cnt=1000} }
154
+ │ ├── PhysicalAgg { aggrs: [], groups: [ #0 ], cost: {compute=3000,io=1000}, stat: {row_cnt=1000} }
155
+ │ │ └── PhysicalScan { table: t1, cost: {compute=0,io=1000}, stat: {row_cnt=1000} }
156
+ │ └── PhysicalScan { table: t2, cost: {compute=0,io=1000}, stat: {row_cnt=1000} }
157
+ └── PhysicalScan { table: t1, cost: {compute=0,io=1000}, stat: {row_cnt=1000} }
158
+ */
159
+
160
+ -- Test whether the optimizer can unnest correlated subqueries with scalar agg in select list
161
+ select t1v1, (select sum (t2v3) from t2 where t2v1 = t1v1) as sum from t1;
162
+
163
+ /*
164
+ LogicalProjection { exprs: [ #0, #2 ] }
165
+ └── RawDependentJoin { join_type: Cross, cond: true, extern_cols: [ Extern(#0) ] }
166
+ ├── LogicalScan { table: t1 }
167
+ └── LogicalProjection { exprs: [ #0 ] }
168
+ └── LogicalAgg
169
+ ├── exprs:Agg(Sum)
170
+ │ └── [ Cast { cast_to: Int64, child: #1 } ]
171
+ ├── groups: []
172
+ └── LogicalFilter
173
+ ├── cond:Eq
174
+ │ ├── #0
175
+ │ └── Extern(#0)
176
+ └── LogicalScan { table: t2 }
177
+ LogicalProjection { exprs: [ #0, #2 ] }
178
+ └── LogicalProjection { exprs: [ #0, #1, #3 ] }
179
+ └── LogicalJoin
180
+ ├── join_type: Inner
181
+ ├── cond:Eq
182
+ │ ├── #0
183
+ │ └── #2
184
+ ├── LogicalScan { table: t1 }
185
+ └── LogicalProjection { exprs: [ #0, #1 ] }
186
+ └── LogicalAgg
187
+ ├── exprs:Agg(Sum)
188
+ │ └── [ Cast { cast_to: Int64, child: #2 } ]
189
+ ├── groups: [ #0 ]
190
+ └── LogicalFilter
191
+ ├── cond:Eq
192
+ │ ├── #1
193
+ │ └── #0
194
+ └── LogicalJoin { join_type: Inner, cond: true }
195
+ ├── LogicalAgg { exprs: [], groups: [ #0 ] }
196
+ │ └── LogicalScan { table: t1 }
197
+ └── LogicalScan { table: t2 }
198
+ PhysicalProjection { exprs: [ #0, #3 ], cost: {compute=20000,io=3000}, stat: {row_cnt=1000} }
199
+ └── PhysicalHashJoin { join_type: Inner, left_keys: [ #0 ], right_keys: [ #0 ], cost: {compute=17000,io=3000}, stat: {row_cnt=1000} }
200
+ ├── PhysicalScan { table: t1, cost: {compute=0,io=1000}, stat: {row_cnt=1000} }
201
+ └── PhysicalAgg
202
+ ├── aggrs:Agg(Sum)
203
+ │ └── [ Cast { cast_to: Int64, child: #2 } ]
204
+ ├── groups: [ #0 ]
205
+ ├── cost: {compute=14000,io=2000}
206
+ ├── stat: {row_cnt=1000}
207
+ └── PhysicalHashJoin { join_type: Inner, left_keys: [ #0 ], right_keys: [ #0 ], cost: {compute=6000,io=2000}, stat: {row_cnt=1000} }
208
+ ├── PhysicalAgg { aggrs: [], groups: [ #0 ], cost: {compute=3000,io=1000}, stat: {row_cnt=1000} }
209
+ │ └── PhysicalScan { table: t1, cost: {compute=0,io=1000}, stat: {row_cnt=1000} }
210
+ └── PhysicalScan { table: t2, cost: {compute=0,io=1000}, stat: {row_cnt=1000} }
211
+ */
212
+
77
213
-- Test whether the optimizer can unnest correlated subqueries.
78
214
select * from t1 where (select sum (t2v3) from (select * from t2, t3 where t2v1 = t1v1 and t2v3 = t3v2)) > 100 ;
79
215
0 commit comments