Skip to content

Commit

Permalink
sem/tree: check oid and version in tree.(*DEnum).CompareError
Browse files Browse the repository at this point in the history
Make sure when we're comparing two enum datums that they are, in fact,
the same enum type.

Informs: #124181

Release note: None
  • Loading branch information
michae2 committed May 23, 2024
1 parent 7b13432 commit 0881755
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/sql/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -5463,10 +5463,26 @@ func (d *DEnum) CompareError(ctx CompareContext, other Datum) (int, error) {
if !ok {
return 0, makeUnsupportedComparisonMessage(d, other)
}

if v.EnumTyp.Oid() != d.EnumTyp.Oid() {
return 0, makeUnsupportedEnumComparisonMessage(d, v)
}
if v.EnumTyp.TypeMeta.Version != d.EnumTyp.TypeMeta.Version {
return 0, makeUnsupportedEnumComparisonMessage(d, v)
}

res := bytes.Compare(d.PhysicalRep, v.PhysicalRep)
return res, nil
}

func makeUnsupportedEnumComparisonMessage(e1, e2 *DEnum) error {
return pgerror.Newf(pgcode.DatatypeMismatch,
"unsupported comparison: %s (oid %d version %d) to %s (oid %d version %d)",
errors.Safe(e1.EnumTyp), e1.EnumTyp.Oid(), e1.EnumTyp.TypeMeta.Version,
errors.Safe(e2.EnumTyp), e2.EnumTyp.Oid(), e2.EnumTyp.TypeMeta.Version,
)
}

// Prev implements the Datum interface.
func (d *DEnum) Prev(ctx CompareContext) (Datum, bool) {
idx, err := d.EnumTyp.EnumGetIdxOfPhysical(d.PhysicalRep)
Expand Down

0 comments on commit 0881755

Please sign in to comment.