Skip to content

Commit

Permalink
sql: fix serialization of pg_index.indkey
Browse files Browse the repository at this point in the history
`indkey` is an `int2vector`, which is serialized as a space-delimited
string containing the elements of the vector. Sequelize relies on this
representation, splitting `indkey` by spaces. This fixes compatibility
with Sequelize and has been tested with Hibernate, GORM, and SQLAlchemy.

This change adds an INT2VECTOR column type that maps to a new
TypeIntVector/DIntVector that uses DOidWrapper to wrap DArray with the
int2vector OID. The new column type allows the serialization of `indkey`
to match what Postgres emits.
  • Loading branch information
Cuong Do committed Feb 9, 2017
1 parent ad21cfa commit 66a4d30
Show file tree
Hide file tree
Showing 17 changed files with 4,567 additions and 4,473 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ func MakeTableDesc(
if d, ok := def.(*parser.ColumnTableDef); ok {
if !desc.IsVirtualTable() {
if _, ok := d.Type.(*parser.ArrayColType); ok {
return desc, util.UnimplementedWithIssueErrorf(2115, "ARRAY column types are unsupported")
return desc, util.UnimplementedWithIssueErrorf(2115, "ARRAY and INT2VECTOR column types are unsupported")
}
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/parser/col_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ func arrayOf(colType ColumnType, boundsExprs Exprs) (ColumnType, error) {
}
}

var int2vectorColType = &ArrayColType{
Name: "INT2VECTOR",
ParamType: intColTypeInt,
BoundsExprs: Exprs{NewDInt(DInt(-1))},
}

// Pre-allocated immutable postgres oid column type.
var oidColTypeOid = &OidColType{}

Expand Down Expand Up @@ -440,6 +446,9 @@ func CastTargetToDatumType(t CastTargetType) Type {
case *CollatedStringColType:
return TCollatedString{Locale: ct.Locale}
case *ArrayColType:
if ct.Name == "INT2VECTOR" {
return TypeIntVector
}
return tArray{CastTargetToDatumType(ct.ParamType)}
case *OidColType:
return TypeOid
Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/parser/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,7 @@ func wrapWithOid(d Datum, oid oid.Oid) Datum {
return nil
case *DInt:
case *DString:
case *DArray:
case dNull, *DOidWrapper:
panic(fmt.Errorf("cannot wrap %T with an Oid", v))
default:
Expand Down Expand Up @@ -2154,3 +2155,9 @@ func mixedTypeCompare(l, r Datum) (int, bool) {
}
return 1, true
}

// NewDIntVectorFromDArray is a helper routine to create a *DIntVector
// (implemented as a *DOidWrapper) initialized from an existing *DArray.
func NewDIntVectorFromDArray(d *DArray) Datum {
return wrapWithOid(d, oid.T_int2vector)
}
1 change: 1 addition & 0 deletions pkg/sql/parser/keywords.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 66a4d30

Please sign in to comment.