Skip to content

Commit

Permalink
Merge 'schema: User Defined Types' from Henrik
Browse files Browse the repository at this point in the history
"This brings in frozen UDT's to be used as simple columns.

 Fixes: #46"

* origin/udt_support:
  schema: User Defined Types
  • Loading branch information
penberg committed Apr 18, 2019
2 parents da7640b + 440353b commit e1597d0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Support for User Defined Types (UDT) added for simple columns.
- Support for collections such as sets, lists and maps added for simple columns.
- Support for writing the result to a file in JSON format.

Expand Down
11 changes: 11 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ func (s *Schema) GetCreateSchema() []string {
for _, cdef := range t.Columns {
columns = append(columns, fmt.Sprintf("%s %s", cdef.Name, cdef.Type.CQLDef()))
}
for _, column := range t.Columns {
switch c := column.Type.(type) {
case UDTType:
createType := "CREATE TYPE %s.%s (%s)"
var typs []string
for name, typ := range c.Types {
typs = append(typs, name+" "+typ.CQLDef())
}
stmts = append(stmts, fmt.Sprintf(createType, s.Keyspace.Name, c.TypeName, strings.Join(typs, ",")))
}
}
var createTable string
if len(clusteringKeys) == 0 {
createTable = fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (%s, PRIMARY KEY (%s))", s.Keyspace.Name, t.Name, strings.Join(columns, ","), strings.Join(partitionKeys, ","))
Expand Down
75 changes: 72 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (
TYPE_UUID = SimpleType("uuid")
TYPE_VARCHAR = SimpleType("varchar")
TYPE_VARINT = SimpleType("varint")

MaxUDTParts = 10
)

// TODO: Add support for time when gocql bug is fixed.
Expand Down Expand Up @@ -249,6 +251,55 @@ func (tt TupleType) GenValueRange(p *PartitionRange) ([]interface{}, []interface
return left, right
}

type UDTType struct {
Types map[string]SimpleType
TypeName string
Frozen bool
}

func (tt UDTType) Name() string {
return tt.TypeName
}

func (tt UDTType) CQLDef() string {
if tt.Frozen {
return "frozen<" + tt.TypeName + ">"
}
return tt.TypeName
}

func (tt UDTType) CQLHolder() string {
return "?"
}

func (tt UDTType) Indexable() bool {
for _, t := range tt.Types {
if t == TYPE_DURATION {
return false
}
}
return true
}

func (tt UDTType) GenValue(p *PartitionRange) []interface{} {
vals := make(map[string]interface{})
for name, typ := range tt.Types {
vals[name] = typ.GenValue(p)[0]
}
return []interface{}{vals}
}

func (tt UDTType) GenValueRange(p *PartitionRange) ([]interface{}, []interface{}) {
left := make(map[string]interface{})
right := make(map[string]interface{})
for name, t := range tt.Types {
ttLeft, ttRight := t.GenValueRange(p)
left[name] = ttLeft[0]
right[name] = ttRight[0]
}
return []interface{}{left}, []interface{}{right}
}

type SetType struct {
Type SimpleType
Frozen bool
Expand Down Expand Up @@ -370,15 +421,17 @@ func genColumnName(prefix string, idx int) string {
}

func genColumnType(numColumns int) Type {
n := rand.Intn(numColumns + 4)
n := rand.Intn(numColumns + 5)
switch n {
case numColumns:
return genTupleType()
case numColumns + 1:
return genSetType()
return genUDTType()
case numColumns + 2:
return genListType()
return genSetType()
case numColumns + 3:
return genListType()
case numColumns + 4:
return genMapType()
default:
return genSimpleType()
Expand All @@ -404,6 +457,22 @@ func genTupleType() Type {
}
}

func genUDTType() UDTType {
udtNum := rand.Uint32()
typeName := fmt.Sprintf("udt_%d", udtNum)
ts := make(map[string]SimpleType)

for i := 0; i < rand.Intn(MaxUDTParts)+1; i++ {
ts[typeName+fmt.Sprintf("_%d", i)] = genSimpleType()
}

return UDTType{
Types: ts,
TypeName: typeName,
Frozen: true,
}
}

func genSetType() SetType {
var t SimpleType
for {
Expand Down

0 comments on commit e1597d0

Please sign in to comment.