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

create DoltgresType struct used for all types #904

Merged
merged 41 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
fcf7e05
create DoltgresType struct used for all types
jennifersp Oct 30, 2024
cf923bb
clean up
jennifersp Nov 1, 2024
2feab21
serialize and deserialize values using recv and send functions
jennifersp Nov 4, 2024
747c9da
wip
jennifersp Nov 5, 2024
0c71dfc
Merge branch 'main' into jennifer/type
jennifersp Nov 5, 2024
a532319
tests passes, need clean up
jennifersp Nov 7, 2024
77ea706
format
jennifersp Nov 7, 2024
c7a819a
merge main
jennifersp Nov 8, 2024
3e86459
clean up
jennifersp Nov 8, 2024
a9e8b0d
fix
jennifersp Nov 8, 2024
a3ba58b
skip a test that's hanging
jennifersp Nov 8, 2024
5892510
update dataTypeSize in client test
jennifersp Nov 8, 2024
b893c2f
update it in more places
jennifersp Nov 8, 2024
8c13835
fix and add test for function with cast as prepared stmt
jennifersp Nov 9, 2024
e4c119e
some fixes for feedback
jennifersp Nov 12, 2024
765a57d
add owner to types
jennifersp Nov 12, 2024
19b2110
move dataloader tests
jennifersp Nov 12, 2024
086cd97
merge main
jennifersp Nov 12, 2024
c4249df
update gms
jennifersp Nov 12, 2024
eb2388e
add godocs
jennifersp Nov 13, 2024
aa043da
try setting regression test timeout to 40 min
jennifersp Nov 13, 2024
7403429
merge main
jennifersp Nov 13, 2024
1d44f98
undo timeout for regression test
jennifersp Nov 13, 2024
3894493
skip
jennifersp Nov 13, 2024
7d8d06e
use nil context for IoCompare
jennifersp Nov 13, 2024
9b42444
use nil ctx
jennifersp Nov 13, 2024
a4016fb
not use IoCompare function for DoltgresType.Compare
jennifersp Nov 14, 2024
d1e2d5a
add array compare
jennifersp Nov 14, 2024
0db08c8
format
jennifersp Nov 14, 2024
f6ce06b
fix regressed tests
jennifersp Nov 14, 2024
10bf24b
format
jennifersp Nov 14, 2024
44f6fba
try not posting Progression
jennifersp Nov 14, 2024
802ef0e
bump gms
jennifersp Nov 14, 2024
98bc4a1
merge main
jennifersp Nov 14, 2024
36a5f97
merge main
jennifersp Nov 15, 2024
ef1c53e
undo and show progression
jennifersp Nov 15, 2024
17ad531
try checkout pr ref and run
jennifersp Nov 15, 2024
31f1251
try
jennifersp Nov 15, 2024
db376ba
test the regression test display
jennifersp Nov 18, 2024
06d105d
Merge branch 'main' into jennifer/type
jennifersp Nov 18, 2024
5931ec2
undo regression-tests.yaml
jennifersp Nov 18, 2024
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
3 changes: 2 additions & 1 deletion core/dataloader/csvdataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/sirupsen/logrus"

"github.com/dolthub/doltgresql/server/functions/framework"
"github.com/dolthub/doltgresql/server/types"
)

Expand Down Expand Up @@ -134,7 +135,7 @@ func (cdl *CsvDataLoader) LoadChunk(ctx *sql.Context, data *bufio.Reader) error
if record[i] == nil {
row[i] = nil
} else {
row[i], err = cdl.colTypes[i].IoInput(ctx, fmt.Sprintf("%v", record[i]))
row[i], err = framework.IoInput(ctx, cdl.colTypes[i], fmt.Sprintf("%v", record[i]))
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions core/dataloader/csvdataloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/stretchr/testify/require"

"github.com/dolthub/doltgresql/server/expression"
"github.com/dolthub/doltgresql/server/functions"
"github.com/dolthub/doltgresql/server/functions/framework"
"github.com/dolthub/doltgresql/server/types"
)

// TestCsvDataLoader tests the CsvDataLoader implementation.
func TestCsvDataLoader(t *testing.T) {
db := memory.NewDatabase("mydb")
provider := memory.NewDBProvider(db)
// cannot call initialize.Initialize(), so call necessary Init() functions.
jennifersp marked this conversation as resolved.
Show resolved Hide resolved
framework.Init()
expression.Init()
functions.Init()
framework.Initialize()

ctx := &sql.Context{
Context: context.Background(),
Expand Down
3 changes: 2 additions & 1 deletion core/dataloader/tabdataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/sirupsen/logrus"

"github.com/dolthub/doltgresql/server/functions/framework"
"github.com/dolthub/doltgresql/server/types"
)

Expand Down Expand Up @@ -132,7 +133,7 @@ func (tdl *TabularDataLoader) LoadChunk(ctx *sql.Context, data *bufio.Reader) er
if values[i] == tdl.nullChar {
row[i] = nil
} else {
row[i], err = tdl.colTypes[i].IoInput(ctx, values[i])
row[i], err = framework.IoInput(ctx, tdl.colTypes[i], values[i])
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions core/typecollection/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ import (
"github.com/dolthub/doltgresql/server/types"
)

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

// Different types with the same name cannot be merged. (e.g.: 'domain' type and 'base' type with the same name)
Expand Down
93 changes: 10 additions & 83 deletions core/typecollection/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"fmt"
"sync"

"github.com/dolthub/go-mysql-server/sql"

"github.com/dolthub/doltgresql/server/types"
"github.com/dolthub/doltgresql/utils"
)
Expand All @@ -34,7 +32,7 @@ func (pgs *TypeCollection) Serialize(ctx context.Context) ([]byte, error) {
pgs.mutex.Lock()
defer pgs.mutex.Unlock()

// Write all the Types to the writer
// Write all the types to the writer
writer := utils.NewWriter(256)
writer.VariableUint(0) // Version
schemaMapKeys := utils.GetMapKeysSorted(pgs.schemaMap)
Expand All @@ -46,42 +44,8 @@ func (pgs *TypeCollection) Serialize(ctx context.Context) ([]byte, error) {
writer.VariableUint(uint64(len(nameMapKeys)))
for _, nameMapKey := range nameMapKeys {
typ := nameMap[nameMapKey]
writer.Uint32(typ.Oid)
writer.String(typ.Name)
writer.String(typ.Owner)
writer.Int16(typ.Length)
writer.Bool(typ.PassedByVal)
writer.String(string(typ.TypType))
writer.String(string(typ.TypCategory))
writer.Bool(typ.IsPreferred)
writer.Bool(typ.IsDefined)
writer.String(typ.Delimiter)
writer.Uint32(typ.RelID)
writer.String(typ.SubscriptFunc)
writer.Uint32(typ.Elem)
writer.Uint32(typ.Array)
writer.String(typ.InputFunc)
writer.String(typ.OutputFunc)
writer.String(typ.ReceiveFunc)
writer.String(typ.SendFunc)
writer.String(typ.ModInFunc)
writer.String(typ.ModOutFunc)
writer.String(typ.AnalyzeFunc)
writer.String(string(typ.Align))
writer.String(string(typ.Storage))
writer.Bool(typ.NotNull)
writer.Uint32(typ.BaseTypeOID)
writer.Int32(typ.TypMod)
writer.Int32(typ.NDims)
writer.Uint32(typ.Collation)
writer.String(typ.DefaulBin)
writer.String(typ.Default)
writer.String(typ.Acl)
writer.VariableUint(uint64(len(typ.Checks)))
for _, check := range typ.Checks {
writer.String(check.Name)
writer.String(check.CheckExpression)
}
data := typ.Serialize()
writer.ByteSlice(data)
}
}

Expand All @@ -93,11 +57,11 @@ func (pgs *TypeCollection) Serialize(ctx context.Context) ([]byte, error) {
func Deserialize(ctx context.Context, data []byte) (*TypeCollection, error) {
if len(data) == 0 {
return &TypeCollection{
schemaMap: make(map[string]map[string]*types.Type),
schemaMap: make(map[string]map[string]types.DoltgresType),
mutex: &sync.RWMutex{},
}, nil
}
schemaMap := make(map[string]map[string]*types.Type)
schemaMap := make(map[string]map[string]types.DoltgresType)
reader := utils.NewReader(data)
version := reader.VariableUint()
if version != 0 {
Expand All @@ -109,49 +73,12 @@ func Deserialize(ctx context.Context, data []byte) (*TypeCollection, error) {
for i := uint64(0); i < numOfSchemas; i++ {
schemaName := reader.String()
numOfTypes := reader.VariableUint()
nameMap := make(map[string]*types.Type)
nameMap := make(map[string]types.DoltgresType)
for j := uint64(0); j < numOfTypes; j++ {
typ := &types.Type{Schema: schemaName}
typ.Oid = reader.Uint32()
typ.Name = reader.String()
typ.Owner = reader.String()
typ.Length = reader.Int16()
typ.PassedByVal = reader.Bool()
typ.TypType = types.TypeType(reader.String())
typ.TypCategory = types.TypeCategory(reader.String())
typ.IsPreferred = reader.Bool()
typ.IsDefined = reader.Bool()
typ.Delimiter = reader.String()
typ.RelID = reader.Uint32()
typ.SubscriptFunc = reader.String()
typ.Elem = reader.Uint32()
typ.Array = reader.Uint32()
typ.InputFunc = reader.String()
typ.OutputFunc = reader.String()
typ.ReceiveFunc = reader.String()
typ.SendFunc = reader.String()
typ.ModInFunc = reader.String()
typ.ModOutFunc = reader.String()
typ.AnalyzeFunc = reader.String()
typ.Align = types.TypeAlignment(reader.String())
typ.Storage = types.TypeStorage(reader.String())
typ.NotNull = reader.Bool()
typ.BaseTypeOID = reader.Uint32()
typ.TypMod = reader.Int32()
typ.NDims = reader.Int32()
typ.Collation = reader.Uint32()
typ.DefaulBin = reader.String()
typ.Default = reader.String()
typ.Acl = reader.String()
numOfChecks := reader.VariableUint()
for k := uint64(0); k < numOfChecks; k++ {
checkName := reader.String()
checkExpr := reader.String()
typ.Checks = append(typ.Checks, &sql.CheckDefinition{
Name: checkName,
CheckExpression: checkExpr,
Enforced: true,
})
typData := reader.ByteSlice()
typ, err := types.Deserialize(typData)
if err != nil {
return nil, err
}
nameMap[typ.Name] = typ
}
Expand Down
47 changes: 24 additions & 23 deletions core/typecollection/typecollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (
"github.com/dolthub/doltgresql/server/types"
)

// TypeCollection contains a collection of Types.
// TypeCollection contains a collection of types.
type TypeCollection struct {
schemaMap map[string]map[string]*types.Type
schemaMap map[string]map[string]types.DoltgresType
mutex *sync.RWMutex
}

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

Expand All @@ -38,12 +38,12 @@ func (pgs *TypeCollection) GetType(schName, typName string) (*types.Type, bool)
return typ, true
}
}
return nil, false
return types.DoltgresType{}, false
}

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

Expand All @@ -52,19 +52,19 @@ func (pgs *TypeCollection) GetDomainType(schName, typName string) (*types.Type,
return typ, true
}
}
return nil, false
return types.DoltgresType{}, false
}

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

typesMap = make(map[string][]*types.Type)
typesMap = make(map[string][]types.DoltgresType)
for schemaName, nameMap := range pgs.schemaMap {
schemaNames = append(schemaNames, schemaName)
typs := make([]*types.Type, 0, len(nameMap))
typs := make([]types.DoltgresType, 0, len(nameMap))
for _, typ := range nameMap {
typs = append(typs, typ)
}
Expand All @@ -74,20 +74,22 @@ func (pgs *TypeCollection) GetAllTypes() (typesMap map[string][]*types.Type, sch
})
typesMap[schemaName] = typs
}

// TODO: add built-in types
sort.Slice(schemaNames, func(i, j int) bool {
return schemaNames[i] < schemaNames[j]
})
return
}

// CreateType creates a new Type.
func (pgs *TypeCollection) CreateType(schema string, typ *types.Type) error {
// CreateType creates a new type.
func (pgs *TypeCollection) CreateType(schema string, typ types.DoltgresType) error {
pgs.mutex.Lock()
defer pgs.mutex.Unlock()

nameMap, ok := pgs.schemaMap[schema]
if !ok {
nameMap = make(map[string]*types.Type)
nameMap = make(map[string]types.DoltgresType)
pgs.schemaMap[schema] = nameMap
}
if _, ok = nameMap[typ.Name]; ok {
Expand All @@ -97,7 +99,7 @@ func (pgs *TypeCollection) CreateType(schema string, typ *types.Type) error {
return nil
}

// DropType drops an existing Type.
// DropType drops an existing type.
func (pgs *TypeCollection) DropType(schName, typName string) error {
pgs.mutex.Lock()
defer pgs.mutex.Unlock()
Expand All @@ -111,8 +113,8 @@ func (pgs *TypeCollection) DropType(schName, typName string) error {
return types.ErrTypeDoesNotExist.New(typName)
}

// IterateTypes iterates over all Types in the collection.
func (pgs *TypeCollection) IterateTypes(f func(schema string, typ *types.Type) error) error {
// IterateTypes iterates over all types in the collection.
func (pgs *TypeCollection) IterateTypes(f func(schema string, typ types.DoltgresType) error) error {
pgs.mutex.Lock()
defer pgs.mutex.Unlock()

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

newCollection := &TypeCollection{
schemaMap: make(map[string]map[string]*types.Type),
schemaMap: make(map[string]map[string]types.DoltgresType),
mutex: &sync.RWMutex{},
}
for schema, nameMap := range pgs.schemaMap {
if len(nameMap) == 0 {
continue
}
clonedNameMap := make(map[string]*types.Type)
clonedNameMap := make(map[string]types.DoltgresType)
for key, typ := range nameMap {
newType := *typ
clonedNameMap[key] = &newType
clonedNameMap[key] = typ
}
newCollection.schemaMap[schema] = clonedNameMap
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241104143128-c2bb78c109df
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662
github.com/dolthub/go-mysql-server v0.18.2-0.20241106010546-3281d09c1f15
github.com/dolthub/go-mysql-server v0.18.2-0.20241107001811-260794c0ad7f
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216
github.com/dolthub/vitess v0.0.0-20241104125316-860772ba6683
github.com/fatih/color v1.13.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662 h1:aC17hZD6iwzBwwfO5M+3oBT5E5gGRiQPdn+vzpDXqIA=
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168=
github.com/dolthub/go-mysql-server v0.18.2-0.20241106010546-3281d09c1f15 h1:VGjaqZKfys8GeaI5uIaKr9y4XZbqMWlA5DJcgfiMHl8=
github.com/dolthub/go-mysql-server v0.18.2-0.20241106010546-3281d09c1f15/go.mod h1:0xWs/FBE4xlhlOsAWoGh24SDRHemT7/U1nApu7SNRXg=
github.com/dolthub/go-mysql-server v0.18.2-0.20241107001811-260794c0ad7f h1:tNQuFYTBfywE+/L7LdjQEsHj4JZBcFyZ9eM7vS2SAlU=
github.com/dolthub/go-mysql-server v0.18.2-0.20241107001811-260794c0ad7f/go.mod h1:0xWs/FBE4xlhlOsAWoGh24SDRHemT7/U1nApu7SNRXg=
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI=
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q=
github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE=
Expand Down
5 changes: 3 additions & 2 deletions server/analyzer/add_implicit_prefix_lengths.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/dolthub/go-mysql-server/sql/analyzer"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/go-mysql-server/sql/transform"
"github.com/lib/pq/oid"

pgtypes "github.com/dolthub/doltgresql/server/types"
)
Expand Down Expand Up @@ -72,7 +73,7 @@ func AddImplicitPrefixLengths(_ *sql.Context, _ *analyzer.Analyzer, node sql.Nod
if !ok {
return nil, false, fmt.Errorf("indexed column %s not found in schema", index.Columns[i].Name)
}
if _, ok := col.Type.(pgtypes.TextType); ok && index.Columns[i].Length == 0 {
if dt, ok := col.Type.(pgtypes.DoltgresType); ok && dt.OID == uint32(oid.T_text) && index.Columns[i].Length == 0 {
index.Columns[i].Length = defaultIndexPrefixLength
indexModified = true
}
Expand All @@ -97,7 +98,7 @@ func AddImplicitPrefixLengths(_ *sql.Context, _ *analyzer.Analyzer, node sql.Nod
if !ok {
return nil, false, fmt.Errorf("indexed column %s not found in schema", newColumns[i].Name)
}
if _, ok := col.Type.(pgtypes.TextType); ok && newColumns[i].Length == 0 {
if dt, ok := col.Type.(pgtypes.DoltgresType); ok && dt.OID == uint32(oid.T_text) && newColumns[i].Length == 0 {
newColumns[i].Length = defaultIndexPrefixLength
indexModified = true
}
Expand Down
Loading
Loading