Skip to content

Commit

Permalink
*added ColumnName in IndexDef
Browse files Browse the repository at this point in the history
*added function that`s adding referenses in 'IndexDef' on 'ColumnDef' for loaded schema from file
*some fixes
  • Loading branch information
illia-li committed Jun 18, 2023
1 parent 4679e78 commit 218c059
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 53 deletions.
3 changes: 2 additions & 1 deletion cmd/gemini/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ func readSchema(confFile string) (*typedef.Schema, error) {

schemaBuilder := builders.NewSchemaBuilder()
schemaBuilder.Keyspace(shm.Keyspace)
for _, tbl := range shm.Tables {
for t, tbl := range shm.Tables {
schemaBuilder.Table(tbl)
generators.AddReferencesForIndexes(shm.Tables[t])
}
return schemaBuilder.Build(), nil
}
Expand Down
120 changes: 96 additions & 24 deletions cmd/gemini/schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"keyspace": {
"name": "ks1"
"name": "ks1",
"replication": {
"class": "SimpleStrategy",
"replication_factor": 1
},
"oracle_replication": {
"class": "SimpleStrategy",
"replication_factor": 1
}
},
"tables": [
{
Expand Down Expand Up @@ -42,18 +50,6 @@
},
{
"name": "col1",
"type": "timestamp"
},
{
"name": "col2",
"type": "decimal"
},
{
"name": "col3",
"type": "uuid"
},
{
"name": "col4",
"type": {
"complex_type": "map",
"key_type": "boolean",
Expand All @@ -62,7 +58,7 @@
}
},
{
"name": "col5",
"name": "col2",
"type": {
"complex_type": "tuple",
"value_types": [
Expand All @@ -73,38 +69,114 @@
}
},
{
"name": "col6",
"name": "col3",
"type": {
"complex_type": "list",
"value_type": "int",
"frozen": true
}
},
{
"name": "col7",
"name": "col4",
"type": {
"complex_type": "set",
"value_type": "int",
"frozen": true
}
},
{
"name": "col5",
"type": "ascii"
},
{
"name": "col6",
"type": "bigint"
},
{
"name": "col7",
"type": "blob"
},
{
"name": "col8",
"type": "boolean"
},
{
"name": "col9",
"type": "date"
},
{
"name": "col10",
"type": "decimal"
},
{
"name": "col11",
"type": "double"
},
{
"name": "col12",
"type": "duration"
},
{
"name": "col13",
"type": "float"
},
{
"name": "col14",
"type": "inet"
},
{
"name": "col15",
"type": "int"
},
{
"name": "col16",
"type": "smallint"
},
{
"name": "col17",
"type": "text"
},
{
"name": "col19",
"type": "timestamp"
},
{
"name": "col20",
"type": "timeuuid"
},
{
"name": "col21",
"type": "tinyint"
},
{
"name": "col22",
"type": "uuid"
},
{
"name": "col23",
"type": "varchar"
},
{
"name": "col24",
"type": "varint"
}
],
"indexes": [
{
"name": "col0_idx",
"column": "col0"
"index_name": "col0_idx",
"column_name": "col56"
},
{
"name": "col1_idx",
"column": "col1"
"index_name": "col1_idx",
"column_name": "col6"
},
{
"name": "col2_idx",
"column": "col2"
"index_name": "col2_idx",
"column_name": "col7"
},
{
"name": "col3_idx",
"column": "col3"
"index_name": "col3_idx",
"column_name": "col8"
}
],
"known_issues": {
Expand Down
25 changes: 24 additions & 1 deletion pkg/generators/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package generators

import (
"fmt"

"github.com/scylladb/gemini/pkg/typedef"
)

Expand All @@ -23,7 +25,11 @@ func CreateIndexesForColumn(table *typedef.Table, maxIndexes int) []typedef.Inde
indexes := make([]typedef.IndexDef, 0, maxIndexes)
for i, col := range table.Columns {
if col.Type.Indexable() && typedef.TypesForIndex.Contains(col.Type) {
indexes = append(indexes, typedef.IndexDef{Name: GenIndexName(table.Name+"_col", i), Column: table.Columns[i]})
indexes = append(indexes, typedef.IndexDef{
IndexName: GenIndexName(table.Name+"_col", i),
ColumnName: table.Columns[i].Name,
Column: table.Columns[i],
})
createdCount++
}
if createdCount == maxIndexes {
Expand All @@ -32,3 +38,20 @@ func CreateIndexesForColumn(table *typedef.Table, maxIndexes int) []typedef.Inde
}
return indexes
}

func AddReferencesForIndexes(table *typedef.Table) {
wrongIndex := -1
for i, index := range table.Indexes {
for c, column := range table.Columns {
if index.ColumnName == column.Name {
table.Indexes[i].Column = table.Columns[c]
break
} else if len(table.Columns) == c {
wrongIndex = i
}
}
}
if wrongIndex != -1 {
panic(fmt.Sprintf("wrong column_name in index defenition:%+v", table.Indexes[wrongIndex]))
}
}
2 changes: 1 addition & 1 deletion pkg/generators/statement_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func GetCreateSchema(s *typedef.Schema) []string {
createTable := GetCreateTable(t, s.Keyspace)
stmts = append(stmts, createTable)
for _, idef := range t.Indexes {
stmts = append(stmts, fmt.Sprintf("CREATE INDEX IF NOT EXISTS %s ON %s.%s (%s)", idef.Name, s.Keyspace.Name, t.Name, idef.Column.Name))
stmts = append(stmts, fmt.Sprintf("CREATE INDEX IF NOT EXISTS %s ON %s.%s (%s)", idef.IndexName, s.Keyspace.Name, t.Name, idef.ColumnName))
}
for _, mv := range t.MaterializedViews {
var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/jobs/gen_check_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ func genSingleIndexQuery(
builder := qb.Select(s.Keyspace.Name + "." + t.Name)
builder.AllowFiltering()
for i := 0; i < idxCount; i++ {
builder = builder.Where(qb.Eq(t.Indexes[i].Column.Name))
builder = builder.Where(qb.Eq(t.Indexes[i].ColumnName))
values = append(values, t.Indexes[i].Column.Type.GenValue(r, p)...)
typs = append(typs, t.Indexes[i].Column.Type)
}
Expand Down
16 changes: 10 additions & 6 deletions pkg/jobs/gen_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,19 @@ func createIdxFromColumns(t testInterface, table *typedef.Table, all bool) (inde
switch all {
case true:
for i := range table.Columns {
var index typedef.IndexDef
index.Name = table.Columns[i].Name + "_idx"
index.Column = table.Columns[i]
index := typedef.IndexDef{
IndexName: table.Columns[i].Name + "_idx",
ColumnName: table.Columns[i].Name,
Column: table.Columns[i],
}
indexes = append(indexes, index)
}
default:
var index typedef.IndexDef
index.Name = table.Columns[0].Name + "_idx"
index.Column = table.Columns[0]
index := typedef.IndexDef{
IndexName: table.Columns[0].Name + "_idx",
ColumnName: table.Columns[0].Name,
Column: table.Columns[0],
}
indexes = append(indexes, index)

}
Expand Down
2 changes: 1 addition & 1 deletion pkg/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func ddl(
defer table.Unlock()
ddlStmts, err := GenDDLStmt(schema, table, r, p, sc)
if err != nil {
logger.Error("Failed! Mutation statement generation failed", zap.Error(err))
logger.Error("Failed! DDl Mutation statement generation failed", zap.Error(err))
globalStatus.WriteErrors.Add(1)
return err
}
Expand Down
28 changes: 21 additions & 7 deletions pkg/typedef/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ package typedef

import (
"encoding/json"
"fmt"

"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"

"golang.org/x/exp/rand"
)

Expand All @@ -29,7 +27,7 @@ type ColumnDef struct {
Name string `json:"name"`
}

var ErrorWrongColTypeDefinition = errors.New("wrong column type definition")
var ErrSchemaValidation = errors.New("validation failed")

func (cd *ColumnDef) IsValidForPrimaryKey() bool {
for _, pkType := range PkTypes {
Expand All @@ -49,15 +47,15 @@ func (cd *ColumnDef) UnmarshalJSON(data []byte) error {
if err != nil {
typeMap, typeOk := dataMap["type"]
if !typeOk {
return errors.Wrap(ErrorWrongColTypeDefinition, fmt.Sprintf("input: %s\n", dataMap))
return errors.Wrapf(ErrSchemaValidation, "missing definition of column 'type': [%T]%+[1]v", dataMap)
}
complexTypeMap, typeMapOk := typeMap.(map[string]interface{})
if !typeMapOk {
return errors.Wrap(ErrorWrongColTypeDefinition, fmt.Sprintf("input: %s\n", typeMap))
return errors.Wrapf(ErrSchemaValidation, "unknown definition column 'type': [%T]%+[1]v", typeMap)
}
complexType, complexTypeOk := complexTypeMap["complex_type"]
if !complexTypeOk {
return errors.Wrap(ErrorWrongColTypeDefinition, fmt.Sprintf("input: %s\n", complexTypeMap))
return errors.Wrapf(ErrSchemaValidation, "missing definition of column 'complex_type': [%T]%+[1]v", complexTypeMap)
}
switch complexType {
case TYPE_LIST, TYPE_SET:
Expand All @@ -69,7 +67,7 @@ func (cd *ColumnDef) UnmarshalJSON(data []byte) error {
case TYPE_UDT:
t, err = GetUDTTypeColumn(dataMap)
default:
return errors.Wrap(ErrorWrongColTypeDefinition, fmt.Sprintf("input: %s\n", complexType))
return errors.Wrapf(ErrSchemaValidation, "unknown 'complex_type': [%T]%+[1]v", complexType)
}
if err != nil {
return err
Expand Down Expand Up @@ -305,6 +303,22 @@ func GetSimpleTypeColumn(data map[string]interface{}) (*ColumnDef, error) {
if err != nil {
return nil, err
}
if st.Name == "" {
return nil, errors.Wrapf(ErrSchemaValidation, "wrong definition of column 'name' [%T]%+[1]v", data)
}
if st.Type == "" {
return nil, errors.Wrapf(ErrSchemaValidation, "empty definition of column 'type' [%T]%+[1]v", data)
}

knownType := false
for _, sType := range AllTypes {
if sType == st.Type {
knownType = true
}
}
if !knownType {
return nil, errors.Wrapf(ErrSchemaValidation, "not simple type in column 'type' [%T]%+[1]v", data)
}
return &ColumnDef{
Name: st.Name,
Type: st.Type,
Expand Down
Loading

0 comments on commit 218c059

Please sign in to comment.