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: use uint32 for DOid #82430

Merged
merged 3 commits into from
Jun 7, 2022
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
2 changes: 1 addition & 1 deletion pkg/server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3326,7 +3326,7 @@ func (s *adminServer) queryTableID(
if row == nil {
return descpb.InvalidID, errors.Newf("failed to resolve %q as a table name", tableName)
}
return descpb.ID(tree.MustBeDOid(row[0]).DInt), nil
return descpb.ID(tree.MustBeDOid(row[0]).Oid), nil
}

// Note that the function returns plain errors, and it is the caller's
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/schemaexpr/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func GetSeqIDFromExpr(expr tree.Expr) (int64, bool) {
}
return id, true
case *tree.DOid:
return int64(n.DInt), true
return int64(n.Oid), true
default:
return 0, false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/seqexpr/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func getSequenceIdentifier(expr tree.Expr) *SeqIdentifier {
SeqName: seqName,
}
case *tree.DOid:
id := int64(a.DInt)
id := int64(a.Oid)
return &SeqIdentifier{
SeqID: id,
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/sql/evalcatalog/pg_updatable.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ var (
)

// PGRelationIsUpdatable is part of the eval.CatalogBuiltins interface.
func (b *Builtins) PGRelationIsUpdatable(ctx context.Context, oid *tree.DOid) (*tree.DInt, error) {
func (b *Builtins) PGRelationIsUpdatable(
ctx context.Context, oidArg *tree.DOid,
) (*tree.DInt, error) {
tableDesc, err := b.dc.GetImmutableTableByID(
ctx, b.txn, descpb.ID(oid.DInt), tree.ObjectLookupFlagsWithRequired(),
ctx, b.txn, descpb.ID(oidArg.Oid), tree.ObjectLookupFlagsWithRequired(),
)
if err != nil {
// For postgres compatibility, it is expected that rather returning
Expand All @@ -62,13 +64,12 @@ func (b *Builtins) PGRelationIsUpdatable(ctx context.Context, oid *tree.DOid) (*
func (b *Builtins) PGColumnIsUpdatable(
ctx context.Context, oidArg *tree.DOid, attNumArg tree.DInt,
) (*tree.DBool, error) {
oid := descpb.ID(oidArg.DInt)
if attNumArg < 0 {
// System columns are not updatable.
return tree.DBoolFalse, nil
}
attNum := descpb.PGAttributeNum(attNumArg)
tableDesc, err := b.dc.GetImmutableTableByID(ctx, b.txn, oid, tree.ObjectLookupFlagsWithRequired())
tableDesc, err := b.dc.GetImmutableTableByID(ctx, b.txn, descpb.ID(oidArg.Oid), tree.ObjectLookupFlagsWithRequired())
if err != nil {
if sqlerrors.IsUndefinedRelationError(err) {
// For postgres compatibility, it is expected that rather returning
Expand Down
16 changes: 13 additions & 3 deletions pkg/sql/logictest/testdata/logic_test/builtin_function
Original file line number Diff line number Diff line change
Expand Up @@ -2365,10 +2365,20 @@ SELECT pg_catalog.length('hello')
----
5

query OOO
SELECT oid(3), oid(0), oid(12023948723)
# -2147483648 is MinInt32.
# 4294967295 is MaxUint32.
query OOOOO
SELECT oid(3), oid(0), (-1)::oid, (-2147483648)::oid, (4294967295)::oid
----
3 0 12023948723
3 0 4294967295 2147483648 4294967295

# -2147483649 is (MinInt32 - 1).
query error OID out of range: -2147483649
SELECT oid(-2147483649)

# 4294967296 is (MaxUint32 + 1).
query error OID out of range: 4294967296
SELECT oid(4294967296)

query T
SELECT to_english(i) FROM (VALUES (1), (13), (617), (-2), (-9223372036854775808)) AS a(i)
Expand Down
39 changes: 38 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/pgoidtype
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ SELECT proargtypes::REGTYPE[] FROM pg_proc WHERE proname = 'obj_description'
query I
SELECT 'trigger'::REGTYPE::INT
----
-1
0

# Regression test for #41708.

Expand Down Expand Up @@ -525,3 +525,40 @@ regression_62205
# Check we error as appropriate if the OID type is not legit.
statement error pgcode 22P02 invalid input syntax for type oid: "regression_69907"
SELECT 'regression_69907'::oid

# 4294967295 is MaxUint32.
# -2147483648 is MinInt32.
query OIBB
SELECT o, i, o > i, i > o FROM (VALUES
(1::oid, 4294967295::int8),
(1::oid, -2147483648::int8),
((-1)::oid, 4294967295::int8),
((-1)::oid, -2147483648::int8),
((-2147483648)::oid, 4294967295::int8),
((-2147483648)::oid, -2147483648::int8),
(4294967295::oid, 4294967295::int8),
(4294967295::oid, -2147483648::int8)
) tbl(o, i)
----
1 4294967295 false true
1 -2147483648 false true
4294967295 4294967295 false false
4294967295 -2147483648 true false
2147483648 4294967295 false true
2147483648 -2147483648 false false
4294967295 4294967295 false false
4294967295 -2147483648 true false

# 4294967296 is (MaxUint32 + 1).
query error OID out of range: 4294967296
SELECT 1:::OID >= 4294967296:::INT8

query error OID out of range: 4294967296
SELECT 4294967296:::INT8 >= 1:::OID

# -2147483649 is (MinInt32 - 1).
query error OID out of range: -2147483649
SELECT 1:::OID >= -2147483649:::INT8

query error OID out of range: -2147483649
SELECT -2147483649:::INT8 >= 1:::OID
2 changes: 1 addition & 1 deletion pkg/sql/opt/constraint/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ func TestSpan_KeyCount(t *testing.T) {
// Multiple key span with DOid datum type.
keyCtx: kcAscAsc,
length: 1,
span: ParseSpan(&evalCtx, "[/-5 - /5]", types.OidFamily),
span: ParseSpan(&evalCtx, "[/0 - /10]", types.OidFamily),
expected: "11",
},
{ // 3
Expand Down
10 changes: 5 additions & 5 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ func makeAllRelationsVirtualTableWithDescriptorIDIndex(
var id descpb.ID
switch t := unwrappedConstraint.(type) {
case *tree.DOid:
id = descpb.ID(t.DInt)
id = descpb.ID(t.Oid)
case *tree.DInt:
id = descpb.ID(*t)
default:
Expand Down Expand Up @@ -3021,7 +3021,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-type.html`,
h := makeOidHasher()
nspOid := h.NamespaceOid(db.GetID(), pgCatalogName)
coid := tree.MustBeDOid(unwrappedConstraint)
ooid := oid.Oid(int(coid.DInt))
ooid := coid.Oid

// Check if it is a predefined type.
typ, ok := types.OidToType[ooid]
Expand Down Expand Up @@ -4167,7 +4167,7 @@ https://www.postgresql.org/docs/9.6/catalog-pg-aggregate.html`,
}
}
}
regprocForZeroOid := tree.NewDOidWithName(tree.DInt(0), types.RegProc, "-")
regprocForZeroOid := tree.NewDOidWithName(0, types.RegProc, "-")
err := addRow(
h.BuiltinOid(name, &overload).AsRegProc(name), // aggfnoid
aggregateKind, // aggkind
Expand Down Expand Up @@ -4213,7 +4213,7 @@ func init() {
for _, o := range def.Definition {
if overload, ok := o.(*tree.Overload); ok {
builtinOid := h.BuiltinOid(name, overload)
id := oid.Oid(builtinOid.DInt)
id := builtinOid.Oid
tree.OidToBuiltinName[id] = name
overload.Oid = id
}
Expand Down Expand Up @@ -4283,7 +4283,7 @@ func (h oidHasher) writeUInt64(i uint64) {
}

func (h oidHasher) writeOID(oid *tree.DOid) {
h.writeUInt64(uint64(oid.DInt))
h.writeUInt64(uint64(oid.Oid))
}

type oidTypeTag uint8
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/pg_oid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestDefaultOid(t *testing.T) {

for _, tc := range testCases {
oid := tableOid(tc.id)
if tc.oid.DInt != oid.DInt {
t.Fatalf("expected oid %d(%32b), got %d(%32b)", tc.oid.DInt, tc.oid.DInt, oid.DInt, oid.DInt)
if tc.oid.Oid != oid.Oid {
t.Fatalf("expected oid %d(%32b), got %d(%32b)", tc.oid.Oid, tc.oid.Oid, oid.Oid, oid.Oid)
}
}
}
2 changes: 1 addition & 1 deletion pkg/sql/pgwire/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func writeBinaryDatumNotNull(

case *tree.DOid:
b.putInt32(4)
b.putInt32(int32(v.DInt))
b.putInt32(int32(v.Oid))
default:
b.setError(errors.AssertionFailedf("unsupported type %T", d))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/resolve_oid.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func resolveOID(
"%s %s does not exist", info.objName, toResolve)
}
return tree.NewDOidWithName(
results[0].(*tree.DOid).DInt,
tree.DInt(results[0].(*tree.DOid).Oid),
resultType,
tree.AsStringWithFlags(results[1], tree.FmtBareStrings),
), nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/row/expr_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func importNextVal(evalCtx *eval.Context, args tree.Datums) (tree.Datum, error)
func importNextValByID(evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
c := getCellInfoAnnotation(evalCtx.Annotations)
oid := tree.MustBeDOid(args[0])
seqMetadata, ok := c.seqIDToMetadata[descpb.ID(oid.DInt)]
seqMetadata, ok := c.seqIDToMetadata[descpb.ID(oid.Oid)]
if !ok {
return nil, errors.Newf("sequence with ID %v not found in annotation", oid)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/rowenc/keyside/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ func Encode(b []byte, val tree.Datum, dir encoding.Direction) ([]byte, error) {
return encoding.EncodeBitArrayDescending(b, t.BitArray), nil
case *tree.DOid:
if dir == encoding.Ascending {
return encoding.EncodeVarintAscending(b, int64(t.DInt)), nil
return encoding.EncodeVarintAscending(b, int64(t.Oid)), nil
}
return encoding.EncodeVarintDescending(b, int64(t.DInt)), nil
return encoding.EncodeVarintDescending(b, int64(t.Oid)), nil
case *tree.DEnum:
if dir == encoding.Ascending {
return encoding.EncodeBytesAscending(b, t.PhysicalRep), nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/rowenc/valueside/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func encodeArrayElement(b []byte, d tree.Datum) ([]byte, error) {
case *tree.DIPAddr:
return encoding.EncodeUntaggedIPAddrValue(b, t.IPAddr), nil
case *tree.DOid:
return encoding.EncodeUntaggedIntValue(b, int64(t.DInt)), nil
return encoding.EncodeUntaggedIntValue(b, int64(t.Oid)), nil
case *tree.DCollatedString:
return encoding.EncodeUntaggedBytesValue(b, []byte(t.Contents)), nil
case *tree.DOidWrapper:
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/rowenc/valueside/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func Encode(appendTo []byte, colID ColumnIDDelta, val tree.Datum, scratch []byte
case *tree.DCollatedString:
return encoding.EncodeBytesValue(appendTo, uint32(colID), []byte(t.Contents)), nil
case *tree.DOid:
return encoding.EncodeIntValue(appendTo, uint32(colID), int64(t.DInt)), nil
return encoding.EncodeIntValue(appendTo, uint32(colID), int64(t.Oid)), nil
case *tree.DEnum:
return encoding.EncodeBytesValue(appendTo, uint32(colID), t.PhysicalRep), nil
case *tree.DVoid:
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/rowenc/valueside/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func MarshalLegacy(colType *types.T, val tree.Datum) (roachpb.Value, error) {
}
case types.OidFamily:
if v, ok := val.(*tree.DOid); ok {
r.SetInt(int64(v.DInt))
r.SetInt(int64(v.Oid))
return r, nil
}
case types.EnumFamily:
Expand Down
22 changes: 11 additions & 11 deletions pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,7 @@ var builtins = map[string]builtinDefinition{
if err != nil {
return nil, err
}
res, err := evalCtx.Sequence.IncrementSequenceByID(evalCtx.Ctx(), int64(dOid.DInt))
res, err := evalCtx.Sequence.IncrementSequenceByID(evalCtx.Ctx(), int64(dOid.Oid))
if err != nil {
return nil, err
}
Expand All @@ -2221,7 +2221,7 @@ var builtins = map[string]builtinDefinition{
ReturnType: tree.FixedReturnType(types.Int),
Fn: func(evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
oid := tree.MustBeDOid(args[0])
res, err := evalCtx.Sequence.IncrementSequenceByID(evalCtx.Ctx(), int64(oid.DInt))
res, err := evalCtx.Sequence.IncrementSequenceByID(evalCtx.Ctx(), int64(oid.Oid))
if err != nil {
return nil, err
}
Expand All @@ -2247,7 +2247,7 @@ var builtins = map[string]builtinDefinition{
if err != nil {
return nil, err
}
res, err := evalCtx.Sequence.GetLatestValueInSessionForSequenceByID(evalCtx.Ctx(), int64(dOid.DInt))
res, err := evalCtx.Sequence.GetLatestValueInSessionForSequenceByID(evalCtx.Ctx(), int64(dOid.Oid))
if err != nil {
return nil, err
}
Expand All @@ -2261,7 +2261,7 @@ var builtins = map[string]builtinDefinition{
ReturnType: tree.FixedReturnType(types.Int),
Fn: func(evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
oid := tree.MustBeDOid(args[0])
res, err := evalCtx.Sequence.GetLatestValueInSessionForSequenceByID(evalCtx.Ctx(), int64(oid.DInt))
res, err := evalCtx.Sequence.GetLatestValueInSessionForSequenceByID(evalCtx.Ctx(), int64(oid.Oid))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2311,7 +2311,7 @@ var builtins = map[string]builtinDefinition{

newVal := tree.MustBeDInt(args[1])
if err := evalCtx.Sequence.SetSequenceValueByID(
evalCtx.Ctx(), uint32(dOid.DInt), int64(newVal), true /* isCalled */); err != nil {
evalCtx.Ctx(), uint32(dOid.Oid), int64(newVal), true /* isCalled */); err != nil {
return nil, err
}
return args[1], nil
Expand All @@ -2327,7 +2327,7 @@ var builtins = map[string]builtinDefinition{
oid := tree.MustBeDOid(args[0])
newVal := tree.MustBeDInt(args[1])
if err := evalCtx.Sequence.SetSequenceValueByID(
evalCtx.Ctx(), uint32(oid.DInt), int64(newVal), true /* isCalled */); err != nil {
evalCtx.Ctx(), uint32(oid.Oid), int64(newVal), true /* isCalled */); err != nil {
return nil, err
}
return args[1], nil
Expand All @@ -2351,7 +2351,7 @@ var builtins = map[string]builtinDefinition{

newVal := tree.MustBeDInt(args[1])
if err := evalCtx.Sequence.SetSequenceValueByID(
evalCtx.Ctx(), uint32(dOid.DInt), int64(newVal), isCalled); err != nil {
evalCtx.Ctx(), uint32(dOid.Oid), int64(newVal), isCalled); err != nil {
return nil, err
}
return args[1], nil
Expand All @@ -2371,7 +2371,7 @@ var builtins = map[string]builtinDefinition{

newVal := tree.MustBeDInt(args[1])
if err := evalCtx.Sequence.SetSequenceValueByID(
evalCtx.Ctx(), uint32(oid.DInt), int64(newVal), isCalled); err != nil {
evalCtx.Ctx(), uint32(oid.Oid), int64(newVal), isCalled); err != nil {
return nil, err
}
return args[1], nil
Expand Down Expand Up @@ -6498,7 +6498,7 @@ table's zone configuration this will return NULL.`,
ReturnType: tree.FixedReturnType(types.Void),
Fn: func(evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
oid := tree.MustBeDOid(args[0])
if err := evalCtx.Planner.RepairTTLScheduledJobForTable(evalCtx.Ctx(), int64(oid.DInt)); err != nil {
if err := evalCtx.Planner.RepairTTLScheduledJobForTable(evalCtx.Ctx(), int64(oid.Oid)); err != nil {
return nil, err
}
return tree.DVoidDatum, nil
Expand Down Expand Up @@ -6587,7 +6587,7 @@ in the current database. Returns an error if validation fails.`,
if err != nil {
return nil, err
}
if err := evalCtx.Planner.RevalidateUniqueConstraintsInTable(evalCtx.Ctx(), int(dOid.DInt)); err != nil {
if err := evalCtx.Planner.RevalidateUniqueConstraintsInTable(evalCtx.Ctx(), int(dOid.Oid)); err != nil {
return nil, err
}
return tree.DVoidDatum, nil
Expand All @@ -6613,7 +6613,7 @@ table. Returns an error if validation fails.`,
return nil, err
}
if err = evalCtx.Planner.RevalidateUniqueConstraint(
evalCtx.Ctx(), int(dOid.DInt), string(constraintName),
evalCtx.Ctx(), int(dOid.Oid), string(constraintName),
); err != nil {
return nil, err
}
Expand Down
Loading