Skip to content

Commit be1712a

Browse files
committed
Reapply "Merge pull request #904 from dolthub/jennifer/type"
This reverts commit acfeca5.
1 parent acfeca5 commit be1712a

File tree

196 files changed

+8106
-10730
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+8106
-10730
lines changed

core/dataloader/csvdataloader.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/dolthub/go-mysql-server/sql"
2525
"github.com/sirupsen/logrus"
2626

27+
"github.com/dolthub/doltgresql/server/functions/framework"
2728
"github.com/dolthub/doltgresql/server/types"
2829
)
2930

@@ -69,7 +70,7 @@ func NewCsvDataLoader(ctx *sql.Context, table sql.InsertableTable, delimiter str
6970

7071
// LoadChunk implements the DataLoader interface
7172
func (cdl *CsvDataLoader) LoadChunk(ctx *sql.Context, data *bufio.Reader) error {
72-
combinedReader := newStringPrefixReader(cdl.partialRecord, data)
73+
combinedReader := NewStringPrefixReader(cdl.partialRecord, data)
7374
cdl.partialRecord = ""
7475

7576
reader, err := newCsvReaderWithDelimiter(combinedReader, cdl.delimiter)
@@ -134,7 +135,7 @@ func (cdl *CsvDataLoader) LoadChunk(ctx *sql.Context, data *bufio.Reader) error
134135
if record[i] == nil {
135136
row[i] = nil
136137
} else {
137-
row[i], err = cdl.colTypes[i].IoInput(ctx, fmt.Sprintf("%v", record[i]))
138+
row[i], err = framework.IoInput(ctx, cdl.colTypes[i], fmt.Sprintf("%v", record[i]))
138139
if err != nil {
139140
return err
140141
}

core/dataloader/csvreader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ type csvReader struct {
6767
fieldsPerRecord int
6868
}
6969

70-
// newCsvReader creates a csvReader from a given ReadCloser.
70+
// NewCsvReader creates a csvReader from a given ReadCloser.
7171
//
7272
// The interpretation of the bytes of the supplied reader is a little murky. If
7373
// there is a UTF8, UTF16LE or UTF16BE BOM as the first bytes read, then the
7474
// BOM is stripped and the remaining contents of the reader are treated as that
7575
// encoding. If we are not in any of those marked encodings, then some of the
7676
// bytes go uninterpreted until we get to the SQL layer. It is currently the
7777
// case that newlines must be encoded as a '0xa' byte.
78-
func newCsvReader(r io.ReadCloser) (*csvReader, error) {
78+
func NewCsvReader(r io.ReadCloser) (*csvReader, error) {
7979
return newCsvReaderWithDelimiter(r, ",")
8080
}
8181

core/dataloader/string_prefix_reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ type stringPrefixReader struct {
2727

2828
var _ io.ReadCloser = (*stringPrefixReader)(nil)
2929

30-
// newStringPrefixReader creates a new stringPrefixReader that first returns the data in |prefix| and
30+
// NewStringPrefixReader creates a new stringPrefixReader that first returns the data in |prefix| and
3131
// then returns data from |reader|.
32-
func newStringPrefixReader(prefix string, reader io.Reader) *stringPrefixReader {
32+
func NewStringPrefixReader(prefix string, reader io.Reader) *stringPrefixReader {
3333
return &stringPrefixReader{
3434
prefix: prefix,
3535
reader: reader,

core/dataloader/tabdataloader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/dolthub/go-mysql-server/sql"
2424
"github.com/sirupsen/logrus"
2525

26+
"github.com/dolthub/doltgresql/server/functions/framework"
2627
"github.com/dolthub/doltgresql/server/types"
2728
)
2829

@@ -132,7 +133,7 @@ func (tdl *TabularDataLoader) LoadChunk(ctx *sql.Context, data *bufio.Reader) er
132133
if values[i] == tdl.nullChar {
133134
row[i] = nil
134135
} else {
135-
row[i], err = tdl.colTypes[i].IoInput(ctx, values[i])
136+
row[i], err = framework.IoInput(ctx, tdl.colTypes[i], values[i])
136137
if err != nil {
137138
return err
138139
}

core/typecollection/merge.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ import (
2121
"github.com/dolthub/doltgresql/server/types"
2222
)
2323

24-
// Merge handles merging sequences on our root and their root.
24+
// Merge handles merging types on our root and their root.
2525
func Merge(ctx context.Context, ourCollection, theirCollection, ancCollection *TypeCollection) (*TypeCollection, error) {
2626
mergedCollection := ourCollection.Clone()
27-
err := theirCollection.IterateTypes(func(schema string, theirType *types.Type) error {
27+
err := theirCollection.IterateTypes(func(schema string, theirType types.DoltgresType) error {
2828
// If we don't have the type, then we simply add it
2929
mergedType, exists := mergedCollection.GetType(schema, theirType.Name)
3030
if !exists {
31-
newSeq := *theirType
32-
return mergedCollection.CreateType(schema, &newSeq)
31+
return mergedCollection.CreateType(schema, theirType)
3332
}
3433

3534
// Different types with the same name cannot be merged. (e.g.: 'domain' type and 'base' type with the same name)

core/typecollection/serialization.go

Lines changed: 12 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"fmt"
2020
"sync"
2121

22-
"github.com/dolthub/go-mysql-server/sql"
23-
2422
"github.com/dolthub/doltgresql/server/types"
2523
"github.com/dolthub/doltgresql/utils"
2624
)
@@ -34,7 +32,7 @@ func (pgs *TypeCollection) Serialize(ctx context.Context) ([]byte, error) {
3432
pgs.mutex.Lock()
3533
defer pgs.mutex.Unlock()
3634

37-
// Write all the Types to the writer
35+
// Write all the types to the writer
3836
writer := utils.NewWriter(256)
3937
writer.VariableUint(0) // Version
4038
schemaMapKeys := utils.GetMapKeysSorted(pgs.schemaMap)
@@ -46,42 +44,8 @@ func (pgs *TypeCollection) Serialize(ctx context.Context) ([]byte, error) {
4644
writer.VariableUint(uint64(len(nameMapKeys)))
4745
for _, nameMapKey := range nameMapKeys {
4846
typ := nameMap[nameMapKey]
49-
writer.Uint32(typ.Oid)
50-
writer.String(typ.Name)
51-
writer.String(typ.Owner)
52-
writer.Int16(typ.Length)
53-
writer.Bool(typ.PassedByVal)
54-
writer.String(string(typ.TypType))
55-
writer.String(string(typ.TypCategory))
56-
writer.Bool(typ.IsPreferred)
57-
writer.Bool(typ.IsDefined)
58-
writer.String(typ.Delimiter)
59-
writer.Uint32(typ.RelID)
60-
writer.String(typ.SubscriptFunc)
61-
writer.Uint32(typ.Elem)
62-
writer.Uint32(typ.Array)
63-
writer.String(typ.InputFunc)
64-
writer.String(typ.OutputFunc)
65-
writer.String(typ.ReceiveFunc)
66-
writer.String(typ.SendFunc)
67-
writer.String(typ.ModInFunc)
68-
writer.String(typ.ModOutFunc)
69-
writer.String(typ.AnalyzeFunc)
70-
writer.String(string(typ.Align))
71-
writer.String(string(typ.Storage))
72-
writer.Bool(typ.NotNull)
73-
writer.Uint32(typ.BaseTypeOID)
74-
writer.Int32(typ.TypMod)
75-
writer.Int32(typ.NDims)
76-
writer.Uint32(typ.Collation)
77-
writer.String(typ.DefaulBin)
78-
writer.String(typ.Default)
79-
writer.String(typ.Acl)
80-
writer.VariableUint(uint64(len(typ.Checks)))
81-
for _, check := range typ.Checks {
82-
writer.String(check.Name)
83-
writer.String(check.CheckExpression)
84-
}
47+
data := typ.Serialize()
48+
writer.ByteSlice(data)
8549
}
8650
}
8751

@@ -93,11 +57,11 @@ func (pgs *TypeCollection) Serialize(ctx context.Context) ([]byte, error) {
9357
func Deserialize(ctx context.Context, data []byte) (*TypeCollection, error) {
9458
if len(data) == 0 {
9559
return &TypeCollection{
96-
schemaMap: make(map[string]map[string]*types.Type),
60+
schemaMap: make(map[string]map[string]types.DoltgresType),
9761
mutex: &sync.RWMutex{},
9862
}, nil
9963
}
100-
schemaMap := make(map[string]map[string]*types.Type)
64+
schemaMap := make(map[string]map[string]types.DoltgresType)
10165
reader := utils.NewReader(data)
10266
version := reader.VariableUint()
10367
if version != 0 {
@@ -109,51 +73,15 @@ func Deserialize(ctx context.Context, data []byte) (*TypeCollection, error) {
10973
for i := uint64(0); i < numOfSchemas; i++ {
11074
schemaName := reader.String()
11175
numOfTypes := reader.VariableUint()
112-
nameMap := make(map[string]*types.Type)
76+
nameMap := make(map[string]types.DoltgresType)
11377
for j := uint64(0); j < numOfTypes; j++ {
114-
typ := &types.Type{Schema: schemaName}
115-
typ.Oid = reader.Uint32()
116-
typ.Name = reader.String()
117-
typ.Owner = reader.String()
118-
typ.Length = reader.Int16()
119-
typ.PassedByVal = reader.Bool()
120-
typ.TypType = types.TypeType(reader.String())
121-
typ.TypCategory = types.TypeCategory(reader.String())
122-
typ.IsPreferred = reader.Bool()
123-
typ.IsDefined = reader.Bool()
124-
typ.Delimiter = reader.String()
125-
typ.RelID = reader.Uint32()
126-
typ.SubscriptFunc = reader.String()
127-
typ.Elem = reader.Uint32()
128-
typ.Array = reader.Uint32()
129-
typ.InputFunc = reader.String()
130-
typ.OutputFunc = reader.String()
131-
typ.ReceiveFunc = reader.String()
132-
typ.SendFunc = reader.String()
133-
typ.ModInFunc = reader.String()
134-
typ.ModOutFunc = reader.String()
135-
typ.AnalyzeFunc = reader.String()
136-
typ.Align = types.TypeAlignment(reader.String())
137-
typ.Storage = types.TypeStorage(reader.String())
138-
typ.NotNull = reader.Bool()
139-
typ.BaseTypeOID = reader.Uint32()
140-
typ.TypMod = reader.Int32()
141-
typ.NDims = reader.Int32()
142-
typ.Collation = reader.Uint32()
143-
typ.DefaulBin = reader.String()
144-
typ.Default = reader.String()
145-
typ.Acl = reader.String()
146-
numOfChecks := reader.VariableUint()
147-
for k := uint64(0); k < numOfChecks; k++ {
148-
checkName := reader.String()
149-
checkExpr := reader.String()
150-
typ.Checks = append(typ.Checks, &sql.CheckDefinition{
151-
Name: checkName,
152-
CheckExpression: checkExpr,
153-
Enforced: true,
154-
})
78+
typData := reader.ByteSlice()
79+
typ, err := types.DeserializeType(typData)
80+
if err != nil {
81+
return nil, err
15582
}
156-
nameMap[typ.Name] = typ
83+
dt := typ.(types.DoltgresType)
84+
nameMap[dt.Name] = dt
15785
}
15886
schemaMap[schemaName] = nameMap
15987
}

core/typecollection/typecollection.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ import (
2121
"github.com/dolthub/doltgresql/server/types"
2222
)
2323

24-
// TypeCollection contains a collection of Types.
24+
// TypeCollection contains a collection of types.
2525
type TypeCollection struct {
26-
schemaMap map[string]map[string]*types.Type
26+
schemaMap map[string]map[string]types.DoltgresType
2727
mutex *sync.RWMutex
2828
}
2929

30-
// GetType returns the Type with the given schema and name.
31-
// Returns nil if the Type cannot be found.
32-
func (pgs *TypeCollection) GetType(schName, typName string) (*types.Type, bool) {
30+
// GetType returns the type with the given schema and name.
31+
// Returns nil if the type cannot be found.
32+
func (pgs *TypeCollection) GetType(schName, typName string) (types.DoltgresType, bool) {
3333
pgs.mutex.RLock()
3434
defer pgs.mutex.RUnlock()
3535

@@ -38,12 +38,12 @@ func (pgs *TypeCollection) GetType(schName, typName string) (*types.Type, bool)
3838
return typ, true
3939
}
4040
}
41-
return nil, false
41+
return types.DoltgresType{}, false
4242
}
4343

44-
// GetDomainType returns a domain Type with the given schema and name.
45-
// Returns nil if the Type cannot be found. It checks for type of Type for domain type.
46-
func (pgs *TypeCollection) GetDomainType(schName, typName string) (*types.Type, bool) {
44+
// GetDomainType returns a domain type with the given schema and name.
45+
// Returns nil if the type cannot be found. It checks for domain type.
46+
func (pgs *TypeCollection) GetDomainType(schName, typName string) (types.DoltgresType, bool) {
4747
pgs.mutex.RLock()
4848
defer pgs.mutex.RUnlock()
4949

@@ -52,19 +52,19 @@ func (pgs *TypeCollection) GetDomainType(schName, typName string) (*types.Type,
5252
return typ, true
5353
}
5454
}
55-
return nil, false
55+
return types.DoltgresType{}, false
5656
}
5757

5858
// GetAllTypes returns a map containing all types in the collection, grouped by the schema they're contained in.
5959
// Each type array is also sorted by the type name.
60-
func (pgs *TypeCollection) GetAllTypes() (typesMap map[string][]*types.Type, schemaNames []string, totalCount int) {
60+
func (pgs *TypeCollection) GetAllTypes() (typesMap map[string][]types.DoltgresType, schemaNames []string, totalCount int) {
6161
pgs.mutex.RLock()
6262
defer pgs.mutex.RUnlock()
6363

64-
typesMap = make(map[string][]*types.Type)
64+
typesMap = make(map[string][]types.DoltgresType)
6565
for schemaName, nameMap := range pgs.schemaMap {
6666
schemaNames = append(schemaNames, schemaName)
67-
typs := make([]*types.Type, 0, len(nameMap))
67+
typs := make([]types.DoltgresType, 0, len(nameMap))
6868
for _, typ := range nameMap {
6969
typs = append(typs, typ)
7070
}
@@ -74,20 +74,22 @@ func (pgs *TypeCollection) GetAllTypes() (typesMap map[string][]*types.Type, sch
7474
})
7575
typesMap[schemaName] = typs
7676
}
77+
78+
// TODO: add built-in types
7779
sort.Slice(schemaNames, func(i, j int) bool {
7880
return schemaNames[i] < schemaNames[j]
7981
})
8082
return
8183
}
8284

83-
// CreateType creates a new Type.
84-
func (pgs *TypeCollection) CreateType(schema string, typ *types.Type) error {
85+
// CreateType creates a new type.
86+
func (pgs *TypeCollection) CreateType(schema string, typ types.DoltgresType) error {
8587
pgs.mutex.Lock()
8688
defer pgs.mutex.Unlock()
8789

8890
nameMap, ok := pgs.schemaMap[schema]
8991
if !ok {
90-
nameMap = make(map[string]*types.Type)
92+
nameMap = make(map[string]types.DoltgresType)
9193
pgs.schemaMap[schema] = nameMap
9294
}
9395
if _, ok = nameMap[typ.Name]; ok {
@@ -97,7 +99,7 @@ func (pgs *TypeCollection) CreateType(schema string, typ *types.Type) error {
9799
return nil
98100
}
99101

100-
// DropType drops an existing Type.
102+
// DropType drops an existing type.
101103
func (pgs *TypeCollection) DropType(schName, typName string) error {
102104
pgs.mutex.Lock()
103105
defer pgs.mutex.Unlock()
@@ -111,8 +113,8 @@ func (pgs *TypeCollection) DropType(schName, typName string) error {
111113
return types.ErrTypeDoesNotExist.New(typName)
112114
}
113115

114-
// IterateTypes iterates over all Types in the collection.
115-
func (pgs *TypeCollection) IterateTypes(f func(schema string, typ *types.Type) error) error {
116+
// IterateTypes iterates over all types in the collection.
117+
func (pgs *TypeCollection) IterateTypes(f func(schema string, typ types.DoltgresType) error) error {
116118
pgs.mutex.Lock()
117119
defer pgs.mutex.Unlock()
118120

@@ -132,17 +134,16 @@ func (pgs *TypeCollection) Clone() *TypeCollection {
132134
defer pgs.mutex.Unlock()
133135

134136
newCollection := &TypeCollection{
135-
schemaMap: make(map[string]map[string]*types.Type),
137+
schemaMap: make(map[string]map[string]types.DoltgresType),
136138
mutex: &sync.RWMutex{},
137139
}
138140
for schema, nameMap := range pgs.schemaMap {
139141
if len(nameMap) == 0 {
140142
continue
141143
}
142-
clonedNameMap := make(map[string]*types.Type)
144+
clonedNameMap := make(map[string]types.DoltgresType)
143145
for key, typ := range nameMap {
144-
newType := *typ
145-
clonedNameMap[key] = &newType
146+
clonedNameMap[key] = typ
146147
}
147148
newCollection.schemaMap[schema] = clonedNameMap
148149
}

server/analyzer/add_implicit_prefix_lengths.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/dolthub/go-mysql-server/sql/analyzer"
2323
"github.com/dolthub/go-mysql-server/sql/plan"
2424
"github.com/dolthub/go-mysql-server/sql/transform"
25+
"github.com/lib/pq/oid"
2526

2627
pgtypes "github.com/dolthub/doltgresql/server/types"
2728
)
@@ -72,7 +73,7 @@ func AddImplicitPrefixLengths(_ *sql.Context, _ *analyzer.Analyzer, node sql.Nod
7273
if !ok {
7374
return nil, false, fmt.Errorf("indexed column %s not found in schema", index.Columns[i].Name)
7475
}
75-
if _, ok := col.Type.(pgtypes.TextType); ok && index.Columns[i].Length == 0 {
76+
if dt, ok := col.Type.(pgtypes.DoltgresType); ok && dt.OID == uint32(oid.T_text) && index.Columns[i].Length == 0 {
7677
index.Columns[i].Length = defaultIndexPrefixLength
7778
indexModified = true
7879
}
@@ -97,7 +98,7 @@ func AddImplicitPrefixLengths(_ *sql.Context, _ *analyzer.Analyzer, node sql.Nod
9798
if !ok {
9899
return nil, false, fmt.Errorf("indexed column %s not found in schema", newColumns[i].Name)
99100
}
100-
if _, ok := col.Type.(pgtypes.TextType); ok && newColumns[i].Length == 0 {
101+
if dt, ok := col.Type.(pgtypes.DoltgresType); ok && dt.OID == uint32(oid.T_text) && newColumns[i].Length == 0 {
101102
newColumns[i].Length = defaultIndexPrefixLength
102103
indexModified = true
103104
}

server/analyzer/assign_insert_casts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func AssignInsertCasts(ctx *sql.Context, a *analyzer.Analyzer, node sql.Node, sc
6464
// Null ColumnDefaultValues or empty DefaultValues are not properly typed in TypeSanitizer, so we must handle them here
6565
colExprType := colExpr.Type()
6666
if colExprType == nil || colExprType == types.Null {
67-
colExprType = pgtypes.UnknownType{}
67+
colExprType = pgtypes.Unknown
6868
}
6969
fromColType, ok := colExprType.(pgtypes.DoltgresType)
7070
if !ok {

0 commit comments

Comments
 (0)