Skip to content

Commit

Permalink
*changed varillbe names inside complex types defenitions
Browse files Browse the repository at this point in the history
*some fixes
  • Loading branch information
illia-li committed Jun 16, 2023
1 parent 9bb49d1 commit 19b3fb0
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 71 deletions.
8 changes: 4 additions & 4 deletions cmd/gemini/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"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 Down Expand Up @@ -65,7 +65,7 @@
"name": "col5",
"type": {
"complex_type": "tuple",
"coltypes": [
"value_types": [
"varchar",
"smallint"
],
Expand All @@ -76,15 +76,15 @@
"name": "col6",
"type": {
"complex_type": "list",
"type": "int",
"value_type": "int",
"frozen": true
}
},
{
"name": "col7",
"type": {
"complex_type": "set",
"type": "int",
"value_type": "int",
"frozen": true
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/generators/column_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func GenTupleType(sc *typedef.SchemaConfig) typedef.Type {
}
return &typedef.TupleType{
ComplexType: typedef.TYPE_TUPLE,
Types: typeList,
ValueTypes: typeList,
Frozen: rand.Uint32()%2 == 0,
}
}
Expand All @@ -75,7 +75,7 @@ func GenUDTType(sc *typedef.SchemaConfig) *typedef.UDTType {

return &typedef.UDTType{
ComplexType: typedef.TYPE_UDT,
Types: ts,
ValueTypes: ts,
TypeName: typeName,
Frozen: true,
}
Expand All @@ -99,7 +99,7 @@ func genBagType(kind string, sc *typedef.SchemaConfig) *typedef.BagType {
}
return &typedef.BagType{
ComplexType: kind,
Type: t,
ValueType: t,
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:")
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
16 changes: 8 additions & 8 deletions pkg/typedef/bag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

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

Expand All @@ -40,16 +40,16 @@ func (ct *BagType) CQLType() gocql.TypeInfo {

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

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

func (ct *BagType) CQLHolder() string {
Expand All @@ -73,7 +73,7 @@ func (ct *BagType) CQLPretty(query string, value []interface{}) (string, int) {
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
22 changes: 11 additions & 11 deletions pkg/typedef/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@ func GetBagTypeColumn(data map[string]interface{}) (out *ColumnDef, err error) {
return nil, errors.Wrapf(err, "can't decode bool value for BagType::Frozen, value=%v", st)
}
var typ SimpleType
if err = mapstructure.Decode(st.Type["type"], &typ); err != nil {
if err = mapstructure.Decode(st.Type["value_type"], &typ); err != nil {
return nil, errors.Wrapf(err, "can't decode SimpleType value for BagType::ValueType, value=%v", st)
}
return &ColumnDef{
Name: st.Name,
Type: &BagType{
ComplexType: complexType,
Frozen: frozen,
Type: typ,
ValueType: typ,
},
}, err
}
Expand All @@ -234,23 +234,23 @@ func GetTupleTypeColumn(data map[string]interface{}) (out *ColumnDef, err error)
return nil, errors.Wrapf(err, "can't decode []SimpleType value, value=%+v", data)
}

if _, ok := st.Type["coltypes"]; !ok {
if _, ok := st.Type["value_types"]; !ok {
return nil, errors.Errorf("not a tuple type, value=%v", st)
}

var dbTypes []SimpleType
if err = mapstructure.Decode(st.Type["coltypes"], &dbTypes); err != nil {
return nil, errors.Wrapf(err, "can't decode []SimpleType value for TupleType::Types, value=%v", st)
if err = mapstructure.Decode(st.Type["value_types"], &dbTypes); err != nil {
return nil, errors.Wrapf(err, "can't decode []SimpleType value for TupleType::ValueTypes, value=%v", st)
}
var frozen bool
if err = mapstructure.Decode(st.Type["frozen"], &frozen); err != nil {
return nil, errors.Wrapf(err, "can't decode bool value for TupleType::Types, value=%v", st)
return nil, errors.Wrapf(err, "can't decode bool value for TupleType::ValueTypes, value=%v", st)
}
return &ColumnDef{
Name: st.Name,
Type: &TupleType{
ComplexType: TYPE_TUPLE,
Types: dbTypes,
ValueTypes: dbTypes,
Frozen: frozen,
},
}, nil
Expand All @@ -266,16 +266,16 @@ func GetUDTTypeColumn(data map[string]interface{}) (out *ColumnDef, err error) {
return nil, errors.Wrapf(err, "can't decode []SimpleType , value=%+v", data)
}

if _, ok := st.Type["coltypes"]; !ok {
if _, ok := st.Type["value_types"]; !ok {
return nil, errors.Errorf("not a UDT type, value=%v", st)
}
if _, ok := st.Type["type_name"]; !ok {
return nil, errors.Errorf("not a UDT type, value=%v", st)
}

var dbTypes map[string]SimpleType
if err = mapstructure.Decode(st.Type["coltypes"], &dbTypes); err != nil {
return nil, errors.Wrapf(err, "can't decode []SimpleType value for UDTType::Types, value=%v", st)
if err = mapstructure.Decode(st.Type["value_types"], &dbTypes); err != nil {
return nil, errors.Wrapf(err, "can't decode []SimpleType value for UDTType::ValueTypes, value=%v", st)
}
var frozen bool
if err = mapstructure.Decode(st.Type["frozen"], &frozen); err != nil {
Expand All @@ -289,7 +289,7 @@ func GetUDTTypeColumn(data map[string]interface{}) (out *ColumnDef, err error) {
Name: st.Name,
Type: &UDTType{
ComplexType: TYPE_UDT,
Types: dbTypes,
ValueTypes: dbTypes,
TypeName: typeName,
Frozen: frozen,
},
Expand Down
7 changes: 4 additions & 3 deletions pkg/typedef/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func TestColumnMarshalUnmarshal(t *testing.T) {
Type: &typedef.UDTType{
ComplexType: typedef.TYPE_UDT,
TypeName: "udt1",
Types: udtTypes,
ValueTypes: udtTypes,
},
Name: "udt1",
},
//nolint:lll
expected: "{\"type\":{\"complex_type\":\"udt\",\"coltypes\":{\"col_ascii\":\"ascii\",\"col_bigint\":\"bigint\",\"col_blob\":\"blob\",\"col_boolean\":\"boolean\",\"col_date\":\"date\",\"col_decimal\":\"decimal\",\"col_double\":\"double\",\"col_duration\":\"duration\",\"col_float\":\"float\",\"col_inet\":\"inet\",\"col_int\":\"int\",\"col_smallint\":\"smallint\",\"col_text\":\"text\",\"col_time\":\"time\",\"col_timestamp\":\"timestamp\",\"col_timeuuid\":\"timeuuid\",\"col_tinyint\":\"tinyint\",\"col_uuid\":\"uuid\",\"col_varchar\":\"varchar\",\"col_varint\":\"varint\"},\"type_name\":\"udt1\",\"frozen\":false},\"name\":\"udt1\"}",
expected: "{\"type\":{\"complex_type\":\"udt\",\"value_types\":{\"col_ascii\":\"ascii\",\"col_bigint\":\"bigint\",\"col_blob\":\"blob\",\"col_boolean\":\"boolean\",\"col_date\":\"date\",\"col_decimal\":\"decimal\",\"col_double\":\"double\",\"col_duration\":\"duration\",\"col_float\":\"float\",\"col_inet\":\"inet\",\"col_int\":\"int\",\"col_smallint\":\"smallint\",\"col_text\":\"text\",\"col_time\":\"time\",\"col_timestamp\":\"timestamp\",\"col_timeuuid\":\"timeuuid\",\"col_tinyint\":\"tinyint\",\"col_uuid\":\"uuid\",\"col_varchar\":\"varchar\",\"col_varint\":\"varint\"},\"type_name\":\"udt1\",\"frozen\":false},\"name\":\"udt1\"}",
})

for id := range testCases {
Expand All @@ -96,9 +96,10 @@ func TestColumnMarshalUnmarshal(t *testing.T) {
t.Errorf(diff)
}
var unmarshaledDef typedef.ColumnDef

err = json.Unmarshal(marshaledData, &unmarshaledDef)
if err != nil {
fmt.Printf("_______%s", marshaledData)
fmt.Printf("_______%s", unmarshaledDef)
t.Fatal(err.Error())
}

Expand Down
26 changes: 13 additions & 13 deletions pkg/typedef/tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

type TupleType struct {
ComplexType string `json:"complex_type"`
Types []SimpleType `json:"coltypes"`
ValueTypes []SimpleType `json:"value_types"`
Frozen bool `json:"frozen"`
}

Expand All @@ -32,16 +32,16 @@ func (t *TupleType) CQLType() gocql.TypeInfo {
}

func (t *TupleType) Name() string {
names := make([]string, len(t.Types))
for i, tp := range t.Types {
names := make([]string, len(t.ValueTypes))
for i, tp := range t.ValueTypes {
names[i] = tp.Name()
}
return "Type: " + strings.Join(names, ",")
}

func (t *TupleType) CQLDef() string {
names := make([]string, len(t.Types))
for i, tp := range t.Types {
names := make([]string, len(t.ValueTypes))
for i, tp := range t.ValueTypes {
names[i] = tp.CQLDef()
}
if t.Frozen {
Expand All @@ -51,23 +51,23 @@ func (t *TupleType) CQLDef() string {
}

func (t *TupleType) CQLHolder() string {
return "(" + strings.TrimRight(strings.Repeat("?,", len(t.Types)), ",") + ")"
return "(" + strings.TrimRight(strings.Repeat("?,", len(t.ValueTypes)), ",") + ")"
}

func (t *TupleType) CQLPretty(query string, value []interface{}) (string, int) {
if len(value) == 0 {
return query, 0
}
var cnt, tmp int
for i, tp := range t.Types {
for i, tp := range t.ValueTypes {
query, tmp = tp.CQLPretty(query, value[i:])
cnt += tmp
}
return query, cnt
}

func (t *TupleType) Indexable() bool {
for _, t := range t.Types {
for _, t := range t.ValueTypes {
if t == TYPE_DURATION {
return false
}
Expand All @@ -76,24 +76,24 @@ func (t *TupleType) Indexable() bool {
}

func (t *TupleType) GenJSONValue(r *rand.Rand, p *PartitionRangeConfig) interface{} {
out := make([]interface{}, 0, len(t.Types))
for _, tp := range t.Types {
out := make([]interface{}, 0, len(t.ValueTypes))
for _, tp := range t.ValueTypes {
out = append(out, tp.GenJSONValue(r, p))
}
return out
}

func (t *TupleType) GenValue(r *rand.Rand, p *PartitionRangeConfig) []interface{} {
out := make([]interface{}, 0, len(t.Types))
for _, tp := range t.Types {
out := make([]interface{}, 0, len(t.ValueTypes))
for _, tp := range t.ValueTypes {
out = append(out, tp.GenValue(r, p)...)
}
return out
}

func (t *TupleType) LenValue() int {
out := 0
for _, tp := range t.Types {
for _, tp := range t.ValueTypes {
out += tp.LenValue()
}
return out
Expand Down
9 changes: 9 additions & 0 deletions pkg/typedef/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ import (
"github.com/scylladb/gemini/pkg/utils"
)

// nolint:revive
const (
TYPE_UDT = "udt"
TYPE_MAP = "map"
TYPE_LIST = "list"
TYPE_SET = "set"
TYPE_TUPLE = "tuple"
)

// nolint:revive
const (
TYPE_ASCII = SimpleType("ascii")
Expand Down
Loading

0 comments on commit 19b3fb0

Please sign in to comment.