Skip to content

Commit 45a58cd

Browse files
b41shsundy-li
andauthored
fix(query): fix distinct set-returning function (#16883)
* fix(query): fix distinct set-returning function * add tests * fix * fix tests --------- Co-authored-by: sundyli <[email protected]>
1 parent 008485a commit 45a58cd

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

src/query/sql/src/planner/binder/bind_query/bind_select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl Binder {
225225
if stmt.distinct {
226226
s_expr = self.bind_distinct(
227227
stmt.span,
228-
&from_context,
228+
&mut from_context,
229229
&projections,
230230
&mut scalar_items,
231231
s_expr,

src/query/sql/src/planner/binder/distinct.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::sync::Arc;
1818
use databend_common_ast::Span;
1919
use databend_common_exception::Result;
2020

21+
use crate::binder::project_set::SetReturningRewriter;
2122
use crate::binder::Binder;
2223
use crate::binder::ColumnBinding;
2324
use crate::optimizer::SExpr;
@@ -37,11 +38,19 @@ impl Binder {
3738
pub fn bind_distinct(
3839
&self,
3940
span: Span,
40-
bind_context: &BindContext,
41+
bind_context: &mut BindContext,
4142
projections: &[ColumnBinding],
4243
scalar_items: &mut HashMap<IndexType, ScalarItem>,
4344
child: SExpr,
4445
) -> Result<SExpr> {
46+
if !bind_context.srf_info.srfs.is_empty() {
47+
// Rewrite the Set-returning functions as columns.
48+
let mut srf_rewriter = SetReturningRewriter::new(bind_context, false);
49+
for (_, item) in scalar_items.iter_mut() {
50+
srf_rewriter.visit(&mut item.scalar)?;
51+
}
52+
}
53+
4554
let scalar_items: Vec<ScalarItem> = scalar_items
4655
.drain()
4756
.map(|(_, item)| {

src/query/sql/src/planner/binder/select.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,11 @@ impl Binder {
296296
);
297297

298298
if distinct {
299+
let columns = new_bind_context.all_column_bindings().to_vec();
299300
new_expr = self.bind_distinct(
300301
left_span,
301-
&new_bind_context,
302-
new_bind_context.all_column_bindings(),
302+
&mut new_bind_context,
303+
&columns,
303304
&mut HashMap::new(),
304305
new_expr,
305306
)?;
@@ -359,10 +360,11 @@ impl Binder {
359360
right_expr: SExpr,
360361
join_type: JoinType,
361362
) -> Result<(SExpr, BindContext)> {
363+
let columns = left_context.all_column_bindings().to_vec();
362364
let left_expr = self.bind_distinct(
363365
left_span,
364-
&left_context,
365-
left_context.all_column_bindings(),
366+
&mut left_context,
367+
&columns,
366368
&mut HashMap::new(),
367369
left_expr,
368370
)?;

src/query/sql/src/planner/dataframe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl Dataframe {
313313
)?;
314314
self.s_expr = self.binder.bind_distinct(
315315
None,
316-
&self.bind_context,
316+
&mut self.bind_context,
317317
&projections,
318318
&mut scalar_items,
319319
self.s_expr.clone(),

tests/sqllogictests/suites/base/03_common/03_0003_select_group_by.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ a1
268268
a2
269269
a3
270270

271+
query T
272+
SELECT distinct unnest(split(col2, ',')) AS col3 FROM t_str ORDER BY col3;
273+
----
274+
a1
275+
a2
276+
a3
277+
271278
statement ok
272279
DROP TABLE t_str
273280

tests/sqllogictests/suites/query/functions/02_0062_function_unnest.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,12 @@ select max(unnest([11,12]))
503503
----
504504
12
505505

506+
query T
507+
SELECT distinct unnest(split(coalesce(NULL, 'a,b'), ',')) AS c1
508+
----
509+
a
510+
b
511+
506512
statement error 1065
507513
select unnest(first_value('aa') OVER (PARTITION BY 'bb'))
508514

0 commit comments

Comments
 (0)