Skip to content

Commit

Permalink
add columns support to materialized view creation using Schema dump (#…
Browse files Browse the repository at this point in the history
…379)

* update create view statement using schema dump in cql

* update CHANGELOG, adopt better golang strings usage
  • Loading branch information
skywhat authored May 30, 2019
1 parent 51c4058 commit 564468a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## v3.4.3 (unreleased)
- Add optional 'columns' tag to Index definitions
- update dosa-idl into v3.2.1
- add columns support to materialized view creation using Schema dump

## v3.4.2 (2019-03-26)
- Update version.VERSION
Expand Down
11 changes: 10 additions & 1 deletion schema/cql/cql.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cql

import (
"bytes"
"strings"
"text/template"

"github.com/uber-go/dosa"
Expand Down Expand Up @@ -55,15 +56,23 @@ func typeMap(t dosa.Type) string {
return "unknown"
}

func selectFieldsInCreatingView(columns []string) string {
if len(columns) == 0 {
return "*"
}
return `"` + strings.Join(columns, `", "`) + `"`
}

// precompile the template for create table
var cqlCreateTableTemplate = template.Must(template.
New("cqlCreateTable").
Funcs(map[string]interface{}{"typeMap": typeMap}).
Funcs(map[string]interface{}{"uniqueKey": uniqueKey}).
Funcs(map[string]interface{}{"selectFieldsInCreatingView": selectFieldsInCreatingView}).
Parse(`create table "{{.Name}}" ({{range .Columns}}"{{- .Name -}}" {{ typeMap .Type -}}, {{end}}primary key {{ .Key }});
{{- range $name, $indexdef := .Indexes }}
create materialized view "{{- $name -}}" as
select * from "{{- $.Name -}}"
select {{selectFieldsInCreatingView $indexdef.Columns}} from "{{- $.Name -}}"
where{{range $keynum, $key := $indexdef.Key.PartitionKeys }}{{if $keynum}} AND {{end}} "{{ $key }}" is not null {{- end}}
primary key {{ uniqueKey $ $indexdef.Key }};
{{- end -}}`))
Expand Down
24 changes: 24 additions & 0 deletions schema/cql/cql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ create materialized view "i2" as
}
}

func TestSelectFieldsInCreatingView(t *testing.T) {
data := []struct {
Columns []string
Statement string
}{
{
Columns: []string{"foo", "bar", "hello"},
Statement: `"foo", "bar", "hello"`,
},
{
Columns: []string{"foo"},
Statement: `"foo"`,
},
{
Columns: []string{},
Statement: "*",
},
}
for _, d := range data {
statement := selectFieldsInCreatingView(d.Columns)
assert.Equal(t, d.Statement, statement)
}
}

func BenchmarkCQL(b *testing.B) {
table, _ := dosa.TableFromInstance(&AllTypes{})
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 564468a

Please sign in to comment.