Skip to content

Commit

Permalink
sql: allow INT to compare to OID
Browse files Browse the repository at this point in the history
An OID is just an INT, so they should be comparable.

Postgres allows this, and certain drivers create queries that attempt to
compare INTs to OIDs, so this improves compatibility.

Release justification: low-risk, high benefit change that improves
compatibility with PGJDBC.
Release note (sql change): A value of type OID can now be compared to a
value of type INT.
  • Loading branch information
rafiss committed Aug 26, 2020
1 parent eaa939c commit 6517f55
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
8 changes: 8 additions & 0 deletions docs/generated/sql/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,12 @@
<tr><td><a href="int.html">int</a> <code><</code> <a href="decimal.html">decimal</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code><</code> <a href="float.html">float</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code><</code> <a href="int.html">int</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code><</code> oid</td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int[]</a> <code><</code> <a href="int.html">int[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="interval.html">interval</a> <code><</code> <a href="interval.html">interval</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="interval.html">interval[]</a> <code><</code> <a href="interval.html">interval[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>jsonb <code><</code> jsonb</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>oid <code><</code> <a href="int.html">int</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>oid <code><</code> oid</td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="string.html">string</a> <code><</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><</code> <a href="string.html">string[]</a></td><td><a href="bool.html">bool</a></td></tr>
Expand Down Expand Up @@ -237,10 +239,12 @@
<tr><td><a href="int.html">int</a> <code><=</code> <a href="decimal.html">decimal</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code><=</code> <a href="float.html">float</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code><=</code> <a href="int.html">int</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code><=</code> oid</td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int[]</a> <code><=</code> <a href="int.html">int[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="interval.html">interval</a> <code><=</code> <a href="interval.html">interval</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="interval.html">interval[]</a> <code><=</code> <a href="interval.html">interval[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>jsonb <code><=</code> jsonb</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>oid <code><=</code> <a href="int.html">int</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>oid <code><=</code> oid</td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="string.html">string</a> <code><=</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><=</code> <a href="string.html">string[]</a></td><td><a href="bool.html">bool</a></td></tr>
Expand Down Expand Up @@ -297,10 +301,12 @@
<tr><td><a href="int.html">int</a> <code>=</code> <a href="decimal.html">decimal</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code>=</code> <a href="float.html">float</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code>=</code> <a href="int.html">int</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code>=</code> oid</td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int[]</a> <code>=</code> <a href="int.html">int[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="interval.html">interval</a> <code>=</code> <a href="interval.html">interval</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="interval.html">interval[]</a> <code>=</code> <a href="interval.html">interval[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>jsonb <code>=</code> jsonb</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>oid <code>=</code> <a href="int.html">int</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>oid <code>=</code> oid</td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="string.html">string</a> <code>=</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>=</code> <a href="string.html">string[]</a></td><td><a href="bool.html">bool</a></td></tr>
Expand Down Expand Up @@ -411,10 +417,12 @@
<tr><td><a href="int.html">int</a> <code>IS NOT DISTINCT FROM</code> <a href="decimal.html">decimal</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code>IS NOT DISTINCT FROM</code> <a href="float.html">float</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code>IS NOT DISTINCT FROM</code> <a href="int.html">int</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int</a> <code>IS NOT DISTINCT FROM</code> oid</td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="int.html">int[]</a> <code>IS NOT DISTINCT FROM</code> <a href="int.html">int[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="interval.html">interval</a> <code>IS NOT DISTINCT FROM</code> <a href="interval.html">interval</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="interval.html">interval[]</a> <code>IS NOT DISTINCT FROM</code> <a href="interval.html">interval[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>jsonb <code>IS NOT DISTINCT FROM</code> jsonb</td><td><a href="bool.html">bool</a></td></tr>
<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><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>
Expand Down
21 changes: 21 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pgoidtype
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,24 @@ query TT
SELECT 1::OID::TEXT, quote_literal(1::OID)
----
1 '1'

# Allow INT-OID comparison. Regression test for #53143.
statement ok
SELECT
c.oid,
a.attnum,
a.attname,
c.relname,
n.nspname,
a.attnotnull
OR (t.typtype = 'd' AND t.typnotnull),
pg_catalog.pg_get_expr(d.adbin, d.adrelid) LIKE '%nextval(%'
FROM
pg_catalog.pg_class AS c
JOIN pg_catalog.pg_namespace AS n ON (c.relnamespace = n.oid)
JOIN pg_catalog.pg_attribute AS a ON (c.oid = a.attrelid)
JOIN pg_catalog.pg_type AS t ON (a.atttypid = t.oid)
LEFT JOIN pg_catalog.pg_attrdef AS d ON
(d.adrelid = a.attrelid AND d.adnum = a.attnum)
JOIN (SELECT 1 AS oid, 1 AS attnum) AS vals ON
(c.oid = vals.oid AND a.attnum = vals.attnum);
16 changes: 12 additions & 4 deletions pkg/sql/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,8 @@ func (d *DInt) Compare(ctx *EvalContext, other Datum) int {
v = *t
case *DFloat, *DDecimal:
return -t.Compare(ctx, d)
case *DOid:
v = t.DInt
default:
panic(makeUnsupportedComparisonMessage(d, other))
}
Expand Down Expand Up @@ -4235,14 +4237,20 @@ func (d *DOid) Compare(ctx *EvalContext, other Datum) int {
// NULL is less than any non-NULL value.
return 1
}
v, ok := UnwrapDatum(ctx, other).(*DOid)
if !ok {
var v DInt
switch t := UnwrapDatum(ctx, other).(type) {
case *DOid:
v = t.DInt
case *DInt:
v = *t
default:
panic(makeUnsupportedComparisonMessage(d, other))
}
if d.DInt < v.DInt {

if d.DInt < v {
return -1
}
if d.DInt > v.DInt {
if d.DInt > v {
return 1
}
return 0
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/sem/tree/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,8 @@ var CmpOps = cmpOpFixups(map[ComparisonOperator]cmpOpOverload{
makeEqFn(types.Float, types.Int, VolatilityLeakProof),
makeEqFn(types.Int, types.Decimal, VolatilityLeakProof),
makeEqFn(types.Int, types.Float, VolatilityLeakProof),
makeEqFn(types.Int, types.Oid, VolatilityLeakProof),
makeEqFn(types.Oid, types.Int, VolatilityLeakProof),
makeEqFn(types.Timestamp, types.Date, VolatilityImmutable),
makeEqFn(types.Timestamp, types.TimestampTZ, VolatilityStable),
makeEqFn(types.TimestampTZ, types.Date, VolatilityStable),
Expand Down Expand Up @@ -2137,6 +2139,8 @@ var CmpOps = cmpOpFixups(map[ComparisonOperator]cmpOpOverload{
makeLtFn(types.Float, types.Int, VolatilityLeakProof),
makeLtFn(types.Int, types.Decimal, VolatilityLeakProof),
makeLtFn(types.Int, types.Float, VolatilityLeakProof),
makeLtFn(types.Int, types.Oid, VolatilityLeakProof),
makeLtFn(types.Oid, types.Int, VolatilityLeakProof),
makeLtFn(types.Timestamp, types.Date, VolatilityImmutable),
makeLtFn(types.Timestamp, types.TimestampTZ, VolatilityStable),
makeLtFn(types.TimestampTZ, types.Date, VolatilityStable),
Expand Down Expand Up @@ -2191,6 +2195,8 @@ var CmpOps = cmpOpFixups(map[ComparisonOperator]cmpOpOverload{
makeLeFn(types.Float, types.Int, VolatilityLeakProof),
makeLeFn(types.Int, types.Decimal, VolatilityLeakProof),
makeLeFn(types.Int, types.Float, VolatilityLeakProof),
makeLeFn(types.Int, types.Oid, VolatilityLeakProof),
makeLeFn(types.Oid, types.Int, VolatilityLeakProof),
makeLeFn(types.Timestamp, types.Date, VolatilityImmutable),
makeLeFn(types.Timestamp, types.TimestampTZ, VolatilityStable),
makeLeFn(types.TimestampTZ, types.Date, VolatilityStable),
Expand Down Expand Up @@ -2255,6 +2261,8 @@ var CmpOps = cmpOpFixups(map[ComparisonOperator]cmpOpOverload{
makeIsFn(types.Float, types.Int, VolatilityLeakProof),
makeIsFn(types.Int, types.Decimal, VolatilityLeakProof),
makeIsFn(types.Int, types.Float, VolatilityLeakProof),
makeIsFn(types.Int, types.Oid, VolatilityLeakProof),
makeIsFn(types.Oid, types.Int, VolatilityLeakProof),
makeIsFn(types.Timestamp, types.Date, VolatilityImmutable),
makeIsFn(types.Timestamp, types.TimestampTZ, VolatilityStable),
makeIsFn(types.TimestampTZ, types.Date, VolatilityStable),
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql/sem/tree/testdata/eval/oid
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ eval
3:::REGCLASS
----
3

eval
1:::OID = 1:::INT
----
true

eval
1:::OID < 2:::INT2
----
true

eval
1:::OID >= 3:::INT
----
false

0 comments on commit 6517f55

Please sign in to comment.