Skip to content

Commit

Permalink
Merge 'schema: index definition JSON cleanup' from Henrik
Browse files Browse the repository at this point in the history
"Instead of reproducing the entire types for the index in question
 we simply store a reference to the column name.

 Fixes: #76"

* origin/simpler_schema_json_definition:
  schema: index definition JSON cleanup
  • Loading branch information
penberg committed Apr 23, 2019
2 parents 68ee63f + 0f381b1 commit 4ebf960
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 94 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- JSON schema definition file has simpler index definition.

## [0.9.2] - 2019-04-18

- Support for User Defined Types (UDT) added for simple columns.
Expand Down
159 changes: 89 additions & 70 deletions cmd/gemini/schema.json
Original file line number Diff line number Diff line change
@@ -1,77 +1,96 @@
{
"keyspace": {
"name": "gemini"
},
"tables": [
"keyspace": {
"name": "ks1"
},
"tables": [
{
"name": "table1",
"partition_keys": [
{
"name": "data1",
"partition_keys": [
{
"name": "pk",
"type": "int"
}
],
"clustering_keys": [
{
"name": "ck",
"type": "int"
}
],
"columns": [
{
"name": "n",
"type": "uuid"
},
{
"name": "t",
"type": "timestamp"
}
]
"name": "pk0",
"type": "int"
}
],
"clustering_keys": [
{
"name": "ck0",
"type": "date"
},
{
"name": "data2",
"partition_keys": [
{
"name": "pk1",
"type": "int"
},
{
"name": "pk2",
"type": "int"
}
],
"clustering_keys": [
{
"name": "ck1",
"type": "int"
},
{
"name": "ck2",
"type": "int"
}
"name": "ck1",
"type": "varint"
},
{
"name": "ck2",
"type": "varchar"
}
],
"columns": [
{
"name": "col0",
"type": {
"types": {
"udt_672245080_0": "ascii",
"udt_672245080_1": "boolean",
"udt_672245080_2": "bigint",
"udt_672245080_3": "blob"
},
"type_name": "udt_672245080",
"frozen": true
}
},
{
"name": "col1",
"type": "timestamp"
},
{
"name": "col2",
"type": "decimal"
},
{
"name": "col3",
"type": "uuid"
},
{
"name": "col4",
"type": {
"key_type": "boolean",
"value_type": "duration",
"frozen": false
}
},
{
"name": "col5",
"type": {
"types": [
"varchar",
"smallint"
],
"columns": [
{
"name": "a",
"type": "bigint"
},
{
"name": "b",
"type": "blob"
},
{
"name": "c",
"type": "text"
},
{
"name": "d",
"type": "date"
},
{
"name": "e",
"type": "varchar"
}
]
"frozen": false
}
}
],
"indexes": [
{
"name": "col0_idx",
"column": "col0"
},
{
"name": "col1_idx",
"column": "col1"
},
{
"name": "col2_idx",
"column": "col2"
},
{
"name": "col3_idx",
"column": "col3"
}
]
],
"known_issues": {
"https://github.com/scylladb/scylla/issues/3708": true
}
}
]
}
28 changes: 14 additions & 14 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Keyspace struct {
}

type ColumnDef struct {
Name string
Type Type
Name string `json:"name"`
Type Type `json:"type"`
}

type Type interface {
Expand All @@ -30,8 +30,8 @@ type Type interface {
}

type IndexDef struct {
Name string
Column ColumnDef
Name string `json:"name"`
Column string `json:"column"`
}

type Columns []ColumnDef
Expand Down Expand Up @@ -68,12 +68,12 @@ func (cs Columns) ToJSONMap(values map[string]interface{}, p *PartitionRange) ma
}

type Table struct {
Name string `json:"name"`
PartitionKeys Columns `json:"partition_keys"`
ClusteringKeys Columns `json:"clustering_keys"`
Columns Columns `json:"columns"`
Indexes []IndexDef `json:"indexes"`
KnownIssues map[string]bool
Name string `json:"name"`
PartitionKeys Columns `json:"partition_keys"`
ClusteringKeys Columns `json:"clustering_keys"`
Columns Columns `json:"columns"`
Indexes []IndexDef `json:"indexes"`
KnownIssues map[string]bool `json:"known_issues"`
}

type Stmt struct {
Expand Down Expand Up @@ -130,7 +130,7 @@ func GenSchema() *Schema {
numIndexes := rand.Intn(numColumns)
for i := 0; i < numIndexes; i++ {
if columns[i].Type.Indexable() {
indexes = append(indexes, IndexDef{Name: genIndexName("col", i), Column: columns[i]})
indexes = append(indexes, IndexDef{Name: genIndexName("col", i), Column: columns[i].Name})
}
}
}
Expand Down Expand Up @@ -190,7 +190,7 @@ func (s *Schema) GetCreateSchema() []string {
}
stmts = append(stmts, createTable)
for _, idef := range t.Indexes {
stmts = append(stmts, fmt.Sprintf("CREATE INDEX %s ON %s.%s (%s)", idef.Name, s.Keyspace.Name, t.Name, idef.Column.Name))
stmts = append(stmts, fmt.Sprintf("CREATE INDEX %s ON %s.%s (%s)", idef.Name, s.Keyspace.Name, t.Name, idef.Column))
}
}
return stmts
Expand Down Expand Up @@ -430,8 +430,8 @@ func (s *Schema) genSingleIndexQuery(t Table, p *PartitionRange) *Stmt {
}
}
idx := p.Rand.Intn(len(t.Indexes))
query := fmt.Sprintf("SELECT * FROM %s.%s WHERE %s AND %s=?", s.Keyspace.Name, t.Name, strings.Join(relations, " AND "), t.Indexes[idx].Column.Name)
values = appendValue(t.Indexes[idx].Column.Type, p, nil)
query := fmt.Sprintf("SELECT * FROM %s.%s WHERE %s AND %s=?", s.Keyspace.Name, t.Name, strings.Join(relations, " AND "), t.Indexes[idx].Column)
values = appendValue(t.Columns[idx].Type, p, nil)
return &Stmt{
Query: query,
Values: func() []interface{} {
Expand Down
20 changes: 10 additions & 10 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ func (st SimpleType) GenValueRange(p *PartitionRange) ([]interface{}, []interfac
}

type TupleType struct {
Types []SimpleType
Frozen bool
Types []SimpleType `json:"types"`
Frozen bool `json:"frozen"`
}

func (tt TupleType) Name() string {
Expand Down Expand Up @@ -252,9 +252,9 @@ func (tt TupleType) GenValueRange(p *PartitionRange) ([]interface{}, []interface
}

type UDTType struct {
Types map[string]SimpleType
TypeName string
Frozen bool
Types map[string]SimpleType `json:"types"`
TypeName string `json:"type_name"`
Frozen bool `json:"frozen"`
}

func (tt UDTType) Name() string {
Expand Down Expand Up @@ -301,8 +301,8 @@ func (tt UDTType) GenValueRange(p *PartitionRange) ([]interface{}, []interface{}
}

type SetType struct {
Type SimpleType
Frozen bool
Type SimpleType `json:"type"`
Frozen bool `json:"frozen"`
}

func (ct SetType) Name() string {
Expand Down Expand Up @@ -367,9 +367,9 @@ func (lt ListType) CQLDef() string {
}

type MapType struct {
KeyType SimpleType
ValueType SimpleType
Frozen bool
KeyType SimpleType `json:"key_type"`
ValueType SimpleType `json:"value_type"`
Frozen bool `json:"frozen"`
}

func (mt MapType) Name() string {
Expand Down

0 comments on commit 4ebf960

Please sign in to comment.