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

release-2.1: sql: Fix the handling of NULL arguments in string_agg #30076

Merged
merged 1 commit into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/sql/distsqlrun/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetAggregateInfo(
datumTypes[i] = inputTypes[i].ToDatumType()
}

_, builtins := builtins.GetBuiltinProperties(strings.ToLower(fn.String()))
props, builtins := builtins.GetBuiltinProperties(strings.ToLower(fn.String()))
for _, b := range builtins {
types := b.Types.Types()
if len(types) != len(inputTypes) {
Expand All @@ -66,6 +66,9 @@ func GetAggregateInfo(
match := true
for i, t := range types {
if !datumTypes[i].Equivalent(t) {
if props.NullableArgs && datumTypes[i].IsAmbiguous() {
continue
}
match = false
break
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/distsqlrun/windower.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func GetWindowFunctionInfo(
"function is neither an aggregate nor a window function",
)
}
_, builtins := builtins.GetBuiltinProperties(strings.ToLower(funcStr))
props, builtins := builtins.GetBuiltinProperties(strings.ToLower(funcStr))
for _, b := range builtins {
types := b.Types.Types()
if len(types) != len(inputTypes) {
Expand All @@ -76,6 +76,9 @@ func GetWindowFunctionInfo(
match := true
for i, t := range types {
if !datumTypes[i].Equivalent(t) {
if props.NullableArgs && datumTypes[i].IsAmbiguous() {
continue
}
match = false
break
}
Expand Down
272 changes: 272 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/aggregate
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,22 @@ ORDER BY company_id;
----
company_id string_agg

query IT colnames
SELECT company_id, string_agg(employee, NULL)
FROM string_agg_test
GROUP BY company_id
ORDER BY company_id;
----
company_id string_agg

query IT colnames
SELECT company_id, string_agg(employee::BYTES, NULL)
FROM string_agg_test
GROUP BY company_id
ORDER BY company_id;
----
company_id string_agg

statement OK
INSERT INTO string_agg_test VALUES
(1, 1, 'A'),
Expand Down Expand Up @@ -1261,6 +1277,86 @@ company_id string_agg
3 CCC
4 DDDD

query IT colnames
SELECT company_id, string_agg(employee, NULL)
FROM string_agg_test
GROUP BY company_id
ORDER BY company_id;
----
company_id string_agg
1 A
2 BB
3 CCC
4 DDDD

query IT colnames
SELECT company_id, string_agg(employee::BYTES, NULL)
FROM string_agg_test
GROUP BY company_id
ORDER BY company_id;
----
company_id string_agg
1 A
2 BB
3 CCC
4 DDDD

query IT colnames
SELECT company_id, string_agg(NULL::STRING, ',')
FROM string_agg_test
GROUP BY company_id
ORDER BY company_id;
----
company_id string_agg
1 NULL
2 NULL
3 NULL
4 NULL

query IT colnames
SELECT company_id, string_agg(NULL::BYTES, b',')
FROM string_agg_test
GROUP BY company_id
ORDER BY company_id;
----
company_id string_agg
1 NULL
2 NULL
3 NULL
4 NULL

query IT colnames
SELECT company_id, string_agg(NULL::STRING, NULL)
FROM string_agg_test
GROUP BY company_id
ORDER BY company_id;
----
company_id string_agg
1 NULL
2 NULL
3 NULL
4 NULL

query IT colnames
SELECT company_id, string_agg(NULL::BYTES, NULL)
FROM string_agg_test
GROUP BY company_id
ORDER BY company_id;
----
company_id string_agg
1 NULL
2 NULL
3 NULL
4 NULL

query error pq: ambiguous call: string_agg\(unknown, unknown\)
SELECT company_id, string_agg(NULL, NULL)
FROM string_agg_test
GROUP BY company_id
ORDER BY company_id;

# Now test the window function version of string_agg.

query IT colnames
SELECT company_id, string_agg(employee, ',')
OVER (PARTITION BY company_id ORDER BY id)
Expand Down Expand Up @@ -1333,6 +1429,156 @@ company_id string_agg
4 DDD
4 DDDD

query IT colnames
SELECT company_id, string_agg(employee, NULL)
OVER (PARTITION BY company_id ORDER BY id)
FROM string_agg_test
ORDER BY company_id, id;
----
company_id string_agg
1 A
2 B
2 BB
3 C
3 CC
3 CCC
4 D
4 DD
4 DDD
4 DDDD

query IT colnames
SELECT company_id, string_agg(employee::BYTES, NULL)
OVER (PARTITION BY company_id ORDER BY id)
FROM string_agg_test
ORDER BY company_id, id;
----
company_id string_agg
1 A
2 B
2 BB
3 C
3 CC
3 CCC
4 D
4 DD
4 DDD
4 DDDD

query IT colnames
SELECT company_id, string_agg(NULL::STRING, employee)
OVER (PARTITION BY company_id ORDER BY id)
FROM string_agg_test
ORDER BY company_id, id;
----
company_id string_agg
1 NULL
2 NULL
2 NULL
3 NULL
3 NULL
3 NULL
4 NULL
4 NULL
4 NULL
4 NULL

query IT colnames
SELECT company_id, string_agg(NULL::BYTES, employee::BYTES)
OVER (PARTITION BY company_id ORDER BY id)
FROM string_agg_test
ORDER BY company_id, id;
----
company_id string_agg
1 NULL
2 NULL
2 NULL
3 NULL
3 NULL
3 NULL
4 NULL
4 NULL
4 NULL
4 NULL

query IT colnames
SELECT company_id, string_agg(NULL::STRING, NULL)
OVER (PARTITION BY company_id ORDER BY id)
FROM string_agg_test
ORDER BY company_id, id;
----
company_id string_agg
1 NULL
2 NULL
2 NULL
3 NULL
3 NULL
3 NULL
4 NULL
4 NULL
4 NULL
4 NULL

query IT colnames
SELECT company_id, string_agg(NULL::BYTES, NULL)
OVER (PARTITION BY company_id ORDER BY id)
FROM string_agg_test
ORDER BY company_id, id;
----
company_id string_agg
1 NULL
2 NULL
2 NULL
3 NULL
3 NULL
3 NULL
4 NULL
4 NULL
4 NULL
4 NULL

query IT colnames
SELECT company_id, string_agg(NULL, NULL::STRING)
OVER (PARTITION BY company_id ORDER BY id)
FROM string_agg_test
ORDER BY company_id, id;
----
company_id string_agg
1 NULL
2 NULL
2 NULL
3 NULL
3 NULL
3 NULL
4 NULL
4 NULL
4 NULL
4 NULL

query IT colnames
SELECT company_id, string_agg(NULL, NULL::BYTES)
OVER (PARTITION BY company_id ORDER BY id)
FROM string_agg_test
ORDER BY company_id, id;
----
company_id string_agg
1 NULL
2 NULL
2 NULL
3 NULL
3 NULL
3 NULL
4 NULL
4 NULL
4 NULL
4 NULL

query error pq: ambiguous call: string_agg\(unknown, unknown\)
SELECT company_id, string_agg(NULL, NULL)
OVER (PARTITION BY company_id ORDER BY id)
FROM string_agg_test
ORDER BY company_id, id;

query IT colnames
SELECT company_id, string_agg(employee, lower(employee))
OVER (PARTITION BY company_id)
Expand Down Expand Up @@ -1443,6 +1689,32 @@ ORDER BY e.company_id;
company_id string_agg
1 D, C, B, A

query IT colnames
SELECT e.company_id, string_agg(e.employee, NULL)
FROM (
SELECT employee, company_id
FROM string_agg_test
ORDER BY employee DESC
) AS e
GROUP BY e.company_id
ORDER BY e.company_id;
----
company_id string_agg
1 DCBA

query IT colnames
SELECT e.company_id, string_agg(e.employee, NULL)
FROM (
SELECT employee::BYTES, company_id
FROM string_agg_test
ORDER BY employee DESC
) AS e
GROUP BY e.company_id
ORDER BY e.company_id;
----
company_id string_agg
1 DCBA

statement OK
DROP TABLE string_agg_test

Expand Down
Loading