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

Redo columnDef.UnmarshalJSON #360

Merged
merged 4 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
19 changes: 15 additions & 4 deletions cmd/gemini/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
{
"name": "col0",
"type": {
"complex_type": "udt",
"type_name": "udt_672245080",
"frozen": true,
"coltypes": {
"value_types": {
"udt_672245080_0": "ascii",
"udt_672245080_1": "boolean",
"udt_672245080_2": "bigint",
Expand All @@ -54,6 +55,7 @@
{
"name": "col4",
"type": {
"complex_type": "map",
"key_type": "boolean",
"value_type": "duration",
"frozen": false
Expand All @@ -62,7 +64,8 @@
{
"name": "col5",
"type": {
"types": [
"complex_type": "tuple",
"value_types": [
"varchar",
"smallint"
],
Expand All @@ -72,8 +75,16 @@
{
"name": "col6",
"type": {
"kind": "list",
"type": "int",
"complex_type": "list",
"value_type": "int",
"frozen": true
}
},
{
"name": "col7",
"type": {
"complex_type": "set",
"value_type": "int",
"frozen": true
}
}
Expand Down
29 changes: 16 additions & 13 deletions pkg/generators/column_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ func GenTupleType(sc *typedef.SchemaConfig) typedef.Type {
typeList[i] = GenSimpleType(sc)
}
return &typedef.TupleType{
Types: typeList,
Frozen: rand.Uint32()%2 == 0,
ComplexType: typedef.TYPE_TUPLE,
ValueTypes: typeList,
Frozen: rand.Uint32()%2 == 0,
}
}

Expand All @@ -73,18 +74,19 @@ func GenUDTType(sc *typedef.SchemaConfig) *typedef.UDTType {
}

return &typedef.UDTType{
Types: ts,
TypeName: typeName,
Frozen: true,
ComplexType: typedef.TYPE_UDT,
ValueTypes: ts,
TypeName: typeName,
Frozen: true,
}
}

func GenSetType(sc *typedef.SchemaConfig) *typedef.BagType {
return genBagType("set", sc)
return genBagType(typedef.TYPE_SET, sc)
}

func GenListType(sc *typedef.SchemaConfig) *typedef.BagType {
return genBagType("list", sc)
return genBagType(typedef.TYPE_LIST, sc)
}

func genBagType(kind string, sc *typedef.SchemaConfig) *typedef.BagType {
Expand All @@ -96,9 +98,9 @@ func genBagType(kind string, sc *typedef.SchemaConfig) *typedef.BagType {
}
}
return &typedef.BagType{
Kind: kind,
Type: t,
Frozen: rand.Uint32()%2 == 0,
ComplexType: kind,
ValueType: t,
Frozen: rand.Uint32()%2 == 0,
}
}

Expand All @@ -111,9 +113,10 @@ func GenMapType(sc *typedef.SchemaConfig) *typedef.MapType {
t = GenSimpleType(sc)
}
return &typedef.MapType{
KeyType: t,
ValueType: GenSimpleType(sc),
Frozen: rand.Uint32()%2 == 0,
ComplexType: typedef.TYPE_MAP,
KeyType: t,
ValueType: GenSimpleType(sc),
Frozen: rand.Uint32()%2 == 0,
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GetCreateTypes(t *typedef.Table, keyspace typedef.Keyspace) []string {
}
createType := "CREATE TYPE IF NOT EXISTS %s.%s (%s)"
var typs []string
for name, typ := range c.Types {
for name, typ := range c.ValueTypes {
typs = append(typs, name+" "+typ.CQLDef())
}
stmts = append(stmts, fmt.Sprintf(createType, keyspace.Name, c.TypeName, strings.Join(typs, ",")))
Expand Down
2 changes: 1 addition & 1 deletion pkg/jobs/gen_ddl_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func genAddColumnStmt(t *typedef.Table, keyspace string, column *typedef.ColumnD
if c, ok := column.Type.(*typedef.UDTType); ok {
createType := "CREATE TYPE IF NOT EXISTS %s.%s (%s);"
var typs []string
for name, typ := range c.Types {
for name, typ := range c.ValueTypes {
typs = append(typs, name+" "+typ.CQLDef())
}
stmt := fmt.Sprintf(createType, keyspace, c.TypeName, strings.Join(typs, ","))
Expand Down
6 changes: 3 additions & 3 deletions pkg/jobs/gen_mutate_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ func genInsertJSONStmt(
values[pk.Name] = "0x" + v
}
case *typedef.TupleType:
tupVals := make([]interface{}, len(t.Types))
for j := 0; j < len(t.Types); j++ {
if t.Types[j] == typedef.TYPE_BLOB {
tupVals := make([]interface{}, len(t.ValueTypes))
for j := 0; j < len(t.ValueTypes); j++ {
if t.ValueTypes[j] == typedef.TYPE_BLOB {
v, ok = vs[i+j].(string)
if ok {
v = "0x" + v
Expand Down
2 changes: 1 addition & 1 deletion pkg/jobs/gen_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (r *Result) Diff(t *testing.T, received *Result) {
getErrorMsgIfDifferent(t, r.Query, received.Query, " error: value stmt.Query.ToCql().stmt expected and received are different:")
getErrorMsgIfDifferent(t, r.Names, received.Names, " error: value stmt.Query.ToCql().Names expected and received are different:")
getErrorMsgIfDifferent(t, r.Values, received.Values, " error: value stmt.Values expected and received are different:")
getErrorMsgIfDifferent(t, r.Types, received.Types, " error: value stmt.Types expected and received are different:")
getErrorMsgIfDifferent(t, r.Types, received.Types, " error: value stmt.ValueTypes expected and received are different:")
illia-li marked this conversation as resolved.
Show resolved Hide resolved
getErrorMsgIfDifferent(t, r.Values, received.Values, " error: value stmt.Values expected and received are different:")
getErrorMsgIfDifferent(t, r.QueryType, received.QueryType, " error: value stmt.QueryType expected and received are different:")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/querycache/querycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func genInsertStmtCache(
for _, col := range t.Columns {
switch colType := col.Type.(type) {
case *typedef.TupleType:
builder = builder.TupleColumn(col.Name, len(colType.Types))
builder = builder.TupleColumn(col.Name, len(colType.ValueTypes))
default:
builder = builder.Columns(col.Name)
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func genUpdateStmtCache(s *typedef.Schema, t *typedef.Table) *typedef.StmtCache
for _, cdef := range t.Columns {
switch t := cdef.Type.(type) {
case *typedef.TupleType:
builder = builder.SetTuple(cdef.Name, len(t.Types))
builder = builder.SetTuple(cdef.Name, len(t.ValueTypes))
case *typedef.CounterType:
builder = builder.SetLit(cdef.Name, cdef.Name+"+1")
continue
Expand Down
26 changes: 13 additions & 13 deletions pkg/typedef/bag.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
)

type BagType struct {
Kind string `json:"kind"` // We need to differentiate between sets and lists
Type SimpleType `json:"type"`
Frozen bool `json:"frozen"`
ComplexType string `json:"complex_type"` // We need to differentiate between sets and lists
ValueType SimpleType `json:"value_type"`
Frozen bool `json:"frozen"`
}

func (ct *BagType) CQLType() gocql.TypeInfo {
switch ct.Kind {
case "set":
switch ct.ComplexType {
case TYPE_SET:
return goCQLTypeMap[gocql.TypeSet]
default:
return goCQLTypeMap[gocql.TypeList]
Expand All @@ -40,16 +40,16 @@ func (ct *BagType) CQLType() gocql.TypeInfo {

func (ct *BagType) Name() string {
if ct.Frozen {
return "frozen<" + ct.Kind + "<" + ct.Type.Name() + ">>"
return "frozen<" + ct.ComplexType + "<" + ct.ValueType.Name() + ">>"
}
return ct.Kind + "<" + ct.Type.Name() + ">"
return ct.ComplexType + "<" + ct.ValueType.Name() + ">"
}

func (ct *BagType) CQLDef() string {
if ct.Frozen {
return "frozen<" + ct.Kind + "<" + ct.Type.Name() + ">>"
return "frozen<" + ct.ComplexType + "<" + ct.ValueType.Name() + ">>"
}
return ct.Kind + "<" + ct.Type.Name() + ">"
return ct.ComplexType + "<" + ct.ValueType.Name() + ">"
}

func (ct *BagType) CQLHolder() string {
Expand All @@ -65,15 +65,15 @@ func (ct *BagType) CQLPretty(query string, value []interface{}) (string, int) {
}
s := reflect.ValueOf(value[0])
op, cl := "[", "]"
if ct.Kind == "set" {
if ct.ComplexType == TYPE_SET {
op, cl = "{", "}"
}
vv := op
vv += strings.Repeat("?,", s.Len())
vv = strings.TrimRight(vv, ",")
vv += cl
for i := 0; i < s.Len(); i++ {
vv, _ = ct.Type.CQLPretty(vv, []interface{}{s.Index(i).Interface()})
vv, _ = ct.ValueType.CQLPretty(vv, []interface{}{s.Index(i).Interface()})
}
return strings.Replace(query, "?", vv, 1), 1
}
Expand All @@ -82,7 +82,7 @@ func (ct *BagType) GenValue(r *rand.Rand, p *PartitionRangeConfig) []interface{}
count := r.Intn(9) + 1
out := make([]interface{}, count)
for i := 0; i < count; i++ {
out[i] = ct.Type.GenValue(r, p)[0]
out[i] = ct.ValueType.GenValue(r, p)[0]
}
return []interface{}{out}
}
Expand All @@ -91,7 +91,7 @@ func (ct *BagType) GenJSONValue(r *rand.Rand, p *PartitionRangeConfig) interface
count := r.Intn(9) + 1
out := make([]interface{}, count)
for i := 0; i < count; i++ {
out[i] = ct.Type.GenJSONValue(r, p)
out[i] = ct.ValueType.GenJSONValue(r, p)
}
return out
}
Expand Down
Loading