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

sql: allow refcursor comparison with NULL values #112070

Merged
merged 1 commit into from
Oct 10, 2023
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
1 change: 1 addition & 0 deletions docs/generated/sql/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@
<tr><td>oid <code>IS NOT DISTINCT FROM</code> <a href="int.html">int</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>oid <code>IS NOT DISTINCT FROM</code> oid</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>pg_lsn <code>IS NOT DISTINCT FROM</code> pg_lsn</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>refcursor <code>IS NOT DISTINCT FROM</code> unknown</td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="string.html">string</a> <code>IS NOT DISTINCT FROM</code> <a href="string.html">string</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="string.html">string[]</a> <code>IS NOT DISTINCT FROM</code> <a href="string.html">string[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="time.html">time</a> <code>IS NOT DISTINCT FROM</code> <a href="time.html">time</a></td><td><a href="bool.html">bool</a></td></tr>
Expand Down
106 changes: 97 additions & 9 deletions pkg/sql/logictest/testdata/logic_test/refcursor
Original file line number Diff line number Diff line change
Expand Up @@ -440,29 +440,117 @@ SELECT 'foo'::REFCURSOR >= 'foo'::TEXT;
statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR >= NULL;

# TODO(drewk): Postgres allows this case.
statement error pgcode 42883 pq: unsupported comparison operator
statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS DISTINCT FROM 'foo'::REFCURSOR;

statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS DISTINCT FROM 'foo'::TEXT;

statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM 'foo'::REFCURSOR;

statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM 'foo'::TEXT;

# Regression test for #112010 - REFCURSOR should allow IS NULL and
# IS NOT NULL comparisons.
subtest is_null

query B
SELECT 'foo'::REFCURSOR IS NULL;
----
false

statement error pgcode 42883 pq: unsupported comparison operator
query B
SELECT 'foo'::REFCURSOR IS NOT NULL;
----
true

query B
SELECT 'foo'::REFCURSOR IS DISTINCT FROM NULL;
----
true

query B
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM NULL;
----
false

# REFCURSOR columns are allowed.
query B rowsort
SELECT l IS NULL FROM implicit_types;
----
true
true
true
true
true

query B rowsort
SELECT l IS NOT NULL FROM implicit_types;
----
false
false
false
false
false

query B rowsort
SELECT l IS DISTINCT FROM NULL FROM implicit_types;
----
false
false
false
false
false

query B rowsort
SELECT l IS NOT DISTINCT FROM NULL FROM implicit_types;
----
true
true
true
true
true

# Comparison with column is not allowed.
statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS DISTINCT FROM 'foo'::REFCURSOR;
SELECT 'foo'::REFCURSOR IS DISTINCT FROM l FROM implicit_types;

statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS DISTINCT FROM 'foo'::TEXT;
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM l FROM implicit_types;

# Comparison with typed NULL is not allowed.
statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS DISTINCT FROM NULL;
SELECT 'foo'::REFCURSOR IS DISTINCT FROM (NULL::REFCURSOR);

statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM 'foo'::REFCURSOR;
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM (NULL::REFCURSOR);

# Comparison with CASE expression is not allowed.
statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM 'foo'::TEXT;
SELECT 'foo'::REFCURSOR IS DISTINCT FROM (CASE WHEN true THEN NULL ELSE 1 END);

statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM NULL;
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM (CASE WHEN true THEN NULL ELSE 1 END);

# The CASE operator is folded into an untyped NULL, so it's allowed.
# TODO(drewk): Postgres doesn't allow this case.
query B
SELECT 'foo'::REFCURSOR IS DISTINCT FROM (CASE WHEN true THEN NULL ELSE NULL END);
----
true

query B
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM (CASE WHEN true THEN NULL ELSE NULL END);
----
false

# The CASE operator is folded into a typed NULL, so it's not allowed.
statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS DISTINCT FROM (CASE WHEN true THEN NULL ELSE NULL::REFCURSOR END);

statement error pgcode 22023 pq: unsupported comparison operator
SELECT 'foo'::REFCURSOR IS NOT DISTINCT FROM (CASE WHEN true THEN NULL ELSE NULL::REFCURSOR END);

subtest end
4 changes: 4 additions & 0 deletions pkg/sql/sem/tree/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,10 @@ var CmpOps = cmpOpFixups(map[treecmp.ComparisonOperatorSymbol]*CmpOpOverloads{
makeIsFn(types.Void, types.Unknown, volatility.Leakproof),
makeIsFn(types.Unknown, types.Void, volatility.Leakproof),

// REFCURSOR cannot be compared with itself, but as a special case, it can
// be compared with NULL using IS NOT DISTINCT FROM.
makeIsFn(types.RefCursor, types.Unknown, volatility.Leakproof),

// Tuple comparison.
{
LeftType: types.AnyTuple,
Expand Down
20 changes: 0 additions & 20 deletions pkg/sql/sem/tree/type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,14 +941,6 @@ func (expr *ComparisonExpr) TypeCheck(
return nil, err
}

// REFCURSOR does not support comparisons.
leftType, rightType := leftTyped.ResolvedType(), rightTyped.ResolvedType()
if leftType.Family() == types.RefCursorFamily || rightType.Family() == types.RefCursorFamily {
return nil, pgerror.Newf(pgcode.UndefinedFunction,
"unsupported comparison operator: <%s> %s <%s>", leftType, cmpOp, rightType,
)
}

if alwaysNull {
return DNull, nil
}
Expand Down Expand Up @@ -1471,12 +1463,6 @@ func (expr *IsNullExpr) TypeCheck(
if err != nil {
return nil, err
}
// REFCURSOR does not support comparisons.
if exprTyped.ResolvedType().Family() == types.RefCursorFamily {
return nil, pgerror.New(pgcode.UndefinedFunction,
"unsupported comparison operator: refcursor IS unknown",
)
}
expr.Expr = exprTyped
expr.typ = types.Bool
return expr, nil
Expand All @@ -1490,12 +1476,6 @@ func (expr *IsNotNullExpr) TypeCheck(
if err != nil {
return nil, err
}
// REFCURSOR does not support comparisons.
if exprTyped.ResolvedType().Family() == types.RefCursorFamily {
return nil, pgerror.New(pgcode.UndefinedFunction,
"unsupported comparison operator: refcursor IS NOT unknown",
)
}
expr.Expr = exprTyped
expr.typ = types.Bool
return expr, nil
Expand Down