Skip to content

Commit 413391f

Browse files
committed
fixup: Move to storing record field values in RecordValue instances
1 parent 3ca4392 commit 413391f

File tree

12 files changed

+142
-98
lines changed

12 files changed

+142
-98
lines changed

server/expression/record.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ func (t *RecordExpr) IsNullable() bool {
6868

6969
// Eval implements the sql.Expression interface.
7070
func (t *RecordExpr) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
71-
vals := make([]interface{}, len(t.exprs))
71+
vals := make([]pgtypes.RecordValue, len(t.exprs))
7272
for i, expr := range t.exprs {
7373
val, err := expr.Eval(ctx, row)
7474
if err != nil {
7575
return nil, err
7676
}
77-
vals[i] = val
77+
vals[i] = pgtypes.RecordValue{
78+
Value: val,
79+
Type: expr.Type(),
80+
}
7881
}
7982

8083
return vals, nil
@@ -112,6 +115,6 @@ func (t *RecordExpr) WithResolvedChildren(children []any) (any, error) {
112115
fieldTypes[i] = expr.Type()
113116
}
114117

115-
newExpr.(*RecordExpr).typ = pgtypes.NewAnonymousRecordType(fieldTypes)
118+
newExpr.(*RecordExpr).typ = pgtypes.Record
116119
return newExpr, err
117120
}

server/functions/binary/equal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ var record_eq = framework.Function2{
438438
if pgtypes.RecordValueHasNull(val1) || pgtypes.RecordValueHasNull(val2) {
439439
return nil, nil
440440
}
441-
res, err := pgtypes.Record.Compare(ctx, val1, val2)
441+
res, err := pgtypes.CompareRecords(ctx, val1, val2)
442442
return res == 0, err
443443
},
444444
}

server/functions/binary/greater.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ var record_gt = framework.Function2{
531531
if !pgtypes.CanCompareRecordValues(val1, val2) {
532532
return nil, nil
533533
}
534-
res, err := pgtypes.Record.Compare(ctx, val1, val2)
534+
res, err := pgtypes.CompareRecords(ctx, val1, val2)
535535
return res == 1, err
536536
},
537537
}

server/functions/binary/greater_equal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ var record_ge = framework.Function2{
531531
if !pgtypes.CanCompareRecordValues(val1, val2) {
532532
return nil, nil
533533
}
534-
res, err := pgtypes.Record.Compare(ctx, val1, val2)
534+
res, err := pgtypes.CompareRecords(ctx, val1, val2)
535535
return res >= 0, err
536536
},
537537
}

server/functions/binary/less.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ var record_lt = framework.Function2{
531531
if !pgtypes.CanCompareRecordValues(val1, val2) {
532532
return nil, nil
533533
}
534-
res, err := pgtypes.Record.Compare(ctx, val1, val2)
534+
res, err := pgtypes.CompareRecords(ctx, val1, val2)
535535
return res == -1, err
536536
},
537537
}

server/functions/binary/less_equal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ var record_le = framework.Function2{
531531
if !pgtypes.CanCompareRecordValues(val1, val2) {
532532
return nil, nil
533533
}
534-
res, err := pgtypes.Record.Compare(ctx, val1, val2)
534+
res, err := pgtypes.CompareRecords(ctx, val1, val2)
535535
return res <= 0, err
536536
},
537537
}

server/functions/binary/not_equal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ var record_ne = framework.Function2{
532532
if !pgtypes.CanCompareRecordValuesForNotEquals(val1, val2) {
533533
return nil, nil
534534
}
535-
res, err := pgtypes.Record.Compare(ctx, val1, val2)
535+
res, err := pgtypes.CompareRecords(ctx, val1, val2)
536536
return res != 0, err
537537
},
538538
}

server/functions/record.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/dolthub/doltgresql/server/functions/framework"
2323
pgtypes "github.com/dolthub/doltgresql/server/types"
24-
"github.com/dolthub/doltgresql/utils"
2524
)
2625

2726
// initRecord registers the functions to the catalog.
@@ -41,8 +40,7 @@ var record_in = framework.Function3{
4140
Parameters: [3]*pgtypes.DoltgresType{pgtypes.Cstring, pgtypes.Oid, pgtypes.Int32},
4241
Strict: true,
4342
Callable: func(ctx *sql.Context, _ [4]*pgtypes.DoltgresType, val1, val2, val3 any) (any, error) {
44-
//typOid := val2.(id.Id)
45-
return val1.(string), nil
43+
return nil, fmt.Errorf("record_in not implemented")
4644
},
4745
}
4846

@@ -53,11 +51,11 @@ var record_out = framework.Function1{
5351
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Record},
5452
Strict: true,
5553
Callable: func(ctx *sql.Context, t [2]*pgtypes.DoltgresType, val any) (any, error) {
56-
values, ok := val.([]any)
54+
values, ok := val.([]pgtypes.RecordValue)
5755
if !ok {
5856
return nil, fmt.Errorf("expected []any, but got %T", val)
5957
}
60-
return pgtypes.RecordToString(ctx, values, t[0].FieldTypes)
58+
return pgtypes.RecordToString(ctx, values)
6159
},
6260
}
6361

@@ -68,14 +66,7 @@ var record_recv = framework.Function3{
6866
Parameters: [3]*pgtypes.DoltgresType{pgtypes.Internal, pgtypes.Oid, pgtypes.Int32},
6967
Strict: true,
7068
Callable: func(ctx *sql.Context, _ [4]*pgtypes.DoltgresType, val1, val2, val3 any) (any, error) {
71-
// TODO
72-
// typOid := val2.(id.Id)
73-
data := val1.([]byte)
74-
if len(data) == 0 {
75-
return nil, nil
76-
}
77-
reader := utils.NewReader(data)
78-
return reader.String(), nil
69+
return nil, fmt.Errorf("record_recv not implemented")
7970
},
8071
}
8172

@@ -86,11 +77,7 @@ var record_send = framework.Function1{
8677
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Record},
8778
Strict: true,
8879
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
89-
// TODO
90-
str := val.(string)
91-
writer := utils.NewWriter(uint64(len(str) + 4))
92-
writer.String(str)
93-
return writer.Data(), nil
80+
return nil, fmt.Errorf("record_send not implemented")
9481
},
9582
}
9683

server/types/record.go

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,46 @@ import (
2020
"github.com/dolthub/doltgresql/core/id"
2121
)
2222

23-
// NewAnonymousRecordType creates a new type for an anonymous record, whose fields have the types
24-
// specified in |fieldTypes|.
25-
func NewAnonymousRecordType(fieldTypes []sql.Type) *DoltgresType {
26-
return &DoltgresType{
27-
ID: toInternal("record"),
28-
TypLength: -1,
29-
PassedByVal: false,
30-
FieldTypes: fieldTypes,
31-
TypType: TypeType_Pseudo,
32-
TypCategory: TypeCategory_PseudoTypes,
33-
IsPreferred: false,
34-
IsDefined: true,
35-
Delimiter: ",",
36-
RelID: id.Null,
37-
SubscriptFunc: toFuncID("-"),
38-
Elem: id.NullType,
39-
Array: toInternal("_record"),
40-
InputFunc: toFuncID("record_in", toInternal("cstring"), toInternal("oid"), toInternal("int4")),
41-
OutputFunc: toFuncID("record_out", toInternal("record")),
42-
ReceiveFunc: toFuncID("record_recv", toInternal("internal"), toInternal("oid"), toInternal("int4")),
43-
SendFunc: toFuncID("record_send", toInternal("record")),
44-
ModInFunc: toFuncID("-"),
45-
ModOutFunc: toFuncID("-"),
46-
AnalyzeFunc: toFuncID("-"),
47-
Align: TypeAlignment_Double,
48-
Storage: TypeStorage_Extended,
49-
NotNull: false,
50-
BaseTypeID: id.NullType,
51-
TypMod: -1,
52-
NDims: 0,
53-
TypCollation: id.NullCollation,
54-
DefaulBin: "",
55-
Default: "",
56-
Acl: nil,
57-
Checks: nil,
58-
attTypMod: -1,
59-
CompareFunc: toFuncID("btrecordcmp", toInternal("record"), toInternal("record")),
60-
}
61-
}
62-
6323
// Record is a generic, anonymous record type, without field type information supplied yet. When used with RecordExpr,
6424
// the field type information will be created once the field expressions are analyzed and type information is available,
6525
// and a new DoltgresType instance will be created with the field type information populated.
66-
var Record = NewAnonymousRecordType(nil)
26+
var Record = &DoltgresType{
27+
ID: toInternal("record"),
28+
TypLength: -1,
29+
PassedByVal: false,
30+
TypType: TypeType_Pseudo,
31+
TypCategory: TypeCategory_PseudoTypes,
32+
IsPreferred: false,
33+
IsDefined: true,
34+
Delimiter: ",",
35+
RelID: id.Null,
36+
SubscriptFunc: toFuncID("-"),
37+
Elem: id.NullType,
38+
Array: toInternal("_record"),
39+
InputFunc: toFuncID("record_in", toInternal("cstring"), toInternal("oid"), toInternal("int4")),
40+
OutputFunc: toFuncID("record_out", toInternal("record")),
41+
ReceiveFunc: toFuncID("record_recv", toInternal("internal"), toInternal("oid"), toInternal("int4")),
42+
SendFunc: toFuncID("record_send", toInternal("record")),
43+
ModInFunc: toFuncID("-"),
44+
ModOutFunc: toFuncID("-"),
45+
AnalyzeFunc: toFuncID("-"),
46+
Align: TypeAlignment_Double,
47+
Storage: TypeStorage_Extended,
48+
NotNull: false,
49+
BaseTypeID: id.NullType,
50+
TypMod: -1,
51+
NDims: 0,
52+
TypCollation: id.NullCollation,
53+
DefaulBin: "",
54+
Default: "",
55+
Acl: nil,
56+
Checks: nil,
57+
attTypMod: -1,
58+
}
59+
60+
// RecordValue holds the value of a single field in a record, including type information for the
61+
// field value.
62+
type RecordValue struct {
63+
Value any
64+
Type sql.Type
65+
}

server/types/type.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ type DoltgresType struct {
6666
Align TypeAlignment
6767
Storage TypeStorage
6868

69-
// FieldTypes tracks record field types, not used by other types.
70-
// TODO: We could almost use CompositeAttrs for this, but CompositeAttribute limits us
71-
// to DoltgresTypes, and we still see some GMS types used in records, such as Null.
72-
FieldTypes []sql.Type
73-
7469
NotNull bool // for Domain types
7570
BaseTypeID id.Type // for Domain types
7671
TypMod int32 // for Domain types
@@ -286,7 +281,7 @@ func (t *DoltgresType) Compare(ctx context.Context, v1 interface{}, v2 interface
286281
case id.Id:
287282
return cmp.Compare(id.Cache().ToOID(ab), id.Cache().ToOID(v2.(id.Id))), nil
288283
case []any:
289-
if !t.IsArrayType() && !t.IsCompositeType() {
284+
if !t.IsArrayType() {
290285
return 0, errors.Errorf("array value received in Compare for non array or composite type")
291286
}
292287
bb := v2.([]any)

0 commit comments

Comments
 (0)