-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Change array agg result from empty list to null if no row qualifed #11299
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
|
||
# TODO: Expect NULL, got empty list | ||
query ? | ||
select array_agg(distinct a) from t where a > 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only query that has unexpected result.
I thought this should be equivalent to select array_agg(a) from t where a > 3 group by a;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jayzhan211 for calling this out!
Given there are no rows with a > 3
, the aggregation input relation from t where a > 3
is empty, so this should produce same result as array_agg from empty table, ie NULL
. currently it shows []
as a result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is related to a multi-stage grouping (where the first phase outputs a NULL and then the second phase gets that NULL and interprets it as []
🤔
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
query III | ||
WITH indices AS ( | ||
SELECT 1 AS idx UNION ALL | ||
SELECT 2 AS idx UNION ALL | ||
SELECT 3 AS idx UNION ALL | ||
SELECT 4 AS idx UNION ALL | ||
SELECT 5 AS idx | ||
) | ||
SELECT data.arr[indices.idx] as element, array_length(data.arr) as array_len, dummy | ||
FROM ( | ||
SELECT array_agg(distinct c2) as arr, count(1) as dummy FROM aggregate_test_100 | ||
) data | ||
CROSS JOIN indices | ||
ORDER BY 1 | ||
---- | ||
1 5 100 | ||
2 5 100 | ||
3 5 100 | ||
4 5 100 | ||
5 5 100 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rewrite it to the simpler one!
|
||
# TODO: Expect NULL, got empty list | ||
query ? | ||
select array_agg(distinct a) from t where a > 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jayzhan211 for calling this out!
Given there are no rows with a > 3
, the aggregation input relation from t where a > 3
is empty, so this should produce same result as array_agg from empty table, ie NULL
. currently it shows []
as a result.
statement ok | ||
drop table t; | ||
|
||
# test with no values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add array_agg(distinct
case on empty table
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
Which issue does this PR close?
As @findepi pointed out in #11274 (comment) that most of the aggregate function does not return non-null result if no row qualified. I double check the result in Postgres and Duckdb and find out they does not return empty list for
array_agg
. I think we can follow the behaviour as they did.I also hope this can make dealing with nullability simpler
Closes #.
Rationale for this change
What changes are included in this PR?
Are these changes tested?
Are there any user-facing changes?
The result of array agg is changed