Skip to content

Commit e1597d0

Browse files
committed
Merge 'schema: User Defined Types' from Henrik
"This brings in frozen UDT's to be used as simple columns. Fixes: #46" * origin/udt_support: schema: User Defined Types
2 parents da7640b + 440353b commit e1597d0

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

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

Diff for: schema.go

+11
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ func (s *Schema) GetCreateSchema() []string {
170170
for _, cdef := range t.Columns {
171171
columns = append(columns, fmt.Sprintf("%s %s", cdef.Name, cdef.Type.CQLDef()))
172172
}
173+
for _, column := range t.Columns {
174+
switch c := column.Type.(type) {
175+
case UDTType:
176+
createType := "CREATE TYPE %s.%s (%s)"
177+
var typs []string
178+
for name, typ := range c.Types {
179+
typs = append(typs, name+" "+typ.CQLDef())
180+
}
181+
stmts = append(stmts, fmt.Sprintf(createType, s.Keyspace.Name, c.TypeName, strings.Join(typs, ",")))
182+
}
183+
}
173184
var createTable string
174185
if len(clusteringKeys) == 0 {
175186
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, ","))

Diff for: types.go

+72-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const (
3434
TYPE_UUID = SimpleType("uuid")
3535
TYPE_VARCHAR = SimpleType("varchar")
3636
TYPE_VARINT = SimpleType("varint")
37+
38+
MaxUDTParts = 10
3739
)
3840

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

254+
type UDTType struct {
255+
Types map[string]SimpleType
256+
TypeName string
257+
Frozen bool
258+
}
259+
260+
func (tt UDTType) Name() string {
261+
return tt.TypeName
262+
}
263+
264+
func (tt UDTType) CQLDef() string {
265+
if tt.Frozen {
266+
return "frozen<" + tt.TypeName + ">"
267+
}
268+
return tt.TypeName
269+
}
270+
271+
func (tt UDTType) CQLHolder() string {
272+
return "?"
273+
}
274+
275+
func (tt UDTType) Indexable() bool {
276+
for _, t := range tt.Types {
277+
if t == TYPE_DURATION {
278+
return false
279+
}
280+
}
281+
return true
282+
}
283+
284+
func (tt UDTType) GenValue(p *PartitionRange) []interface{} {
285+
vals := make(map[string]interface{})
286+
for name, typ := range tt.Types {
287+
vals[name] = typ.GenValue(p)[0]
288+
}
289+
return []interface{}{vals}
290+
}
291+
292+
func (tt UDTType) GenValueRange(p *PartitionRange) ([]interface{}, []interface{}) {
293+
left := make(map[string]interface{})
294+
right := make(map[string]interface{})
295+
for name, t := range tt.Types {
296+
ttLeft, ttRight := t.GenValueRange(p)
297+
left[name] = ttLeft[0]
298+
right[name] = ttRight[0]
299+
}
300+
return []interface{}{left}, []interface{}{right}
301+
}
302+
252303
type SetType struct {
253304
Type SimpleType
254305
Frozen bool
@@ -370,15 +421,17 @@ func genColumnName(prefix string, idx int) string {
370421
}
371422

372423
func genColumnType(numColumns int) Type {
373-
n := rand.Intn(numColumns + 4)
424+
n := rand.Intn(numColumns + 5)
374425
switch n {
375426
case numColumns:
376427
return genTupleType()
377428
case numColumns + 1:
378-
return genSetType()
429+
return genUDTType()
379430
case numColumns + 2:
380-
return genListType()
431+
return genSetType()
381432
case numColumns + 3:
433+
return genListType()
434+
case numColumns + 4:
382435
return genMapType()
383436
default:
384437
return genSimpleType()
@@ -404,6 +457,22 @@ func genTupleType() Type {
404457
}
405458
}
406459

460+
func genUDTType() UDTType {
461+
udtNum := rand.Uint32()
462+
typeName := fmt.Sprintf("udt_%d", udtNum)
463+
ts := make(map[string]SimpleType)
464+
465+
for i := 0; i < rand.Intn(MaxUDTParts)+1; i++ {
466+
ts[typeName+fmt.Sprintf("_%d", i)] = genSimpleType()
467+
}
468+
469+
return UDTType{
470+
Types: ts,
471+
TypeName: typeName,
472+
Frozen: true,
473+
}
474+
}
475+
407476
func genSetType() SetType {
408477
var t SimpleType
409478
for {

0 commit comments

Comments
 (0)