-
Notifications
You must be signed in to change notification settings - Fork 111
/
model_manager.go
138 lines (113 loc) · 3.79 KB
/
model_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package clickhouse
import (
"context"
"fmt"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/rilldata/rill/runtime/drivers"
)
type ModelInputProperties struct {
SQL string `mapstructure:"sql"`
}
func (p *ModelInputProperties) Validate() error {
if p.SQL == "" {
return fmt.Errorf("missing property 'sql'")
}
return nil
}
type ModelOutputProperties struct {
Table string `mapstructure:"table"`
Materialize *bool `mapstructure:"materialize"`
}
func (p *ModelOutputProperties) Validate(opts *drivers.ModelExecutorOptions) error {
return nil
}
type ModelResultProperties struct {
Table string `mapstructure:"table"`
View bool `mapstructure:"view"`
UsedModelName bool `mapstructure:"used_model_name"`
}
func (c *connection) Rename(ctx context.Context, res *drivers.ModelResult, newName string, env *drivers.ModelEnv) (*drivers.ModelResult, error) {
olap, ok := c.AsOLAP(c.instanceID)
if !ok {
return nil, fmt.Errorf("connector is not an OLAP")
}
resProps := &ModelResultProperties{}
if err := mapstructure.WeakDecode(res.Properties, resProps); err != nil {
return nil, fmt.Errorf("failed to parse previous result properties: %w", err)
}
if !resProps.UsedModelName {
return res, nil
}
err := olapForceRenameTable(ctx, olap, resProps.Table, resProps.View, newName)
if err != nil {
return nil, fmt.Errorf("failed to rename model: %w", err)
}
resProps.Table = newName
resPropsMap := map[string]interface{}{}
err = mapstructure.WeakDecode(resProps, &resPropsMap)
if err != nil {
return nil, fmt.Errorf("failed to encode result properties: %w", err)
}
return &drivers.ModelResult{
Connector: res.Connector,
Properties: resPropsMap,
Table: newName,
}, nil
}
func (c *connection) Exists(ctx context.Context, res *drivers.ModelResult) (bool, error) {
olap, ok := c.AsOLAP(c.instanceID)
if !ok {
return false, fmt.Errorf("connector is not an OLAP")
}
_, err := olap.InformationSchema().Lookup(ctx, "", "", res.Table)
return err == nil, nil
}
func (c *connection) Delete(ctx context.Context, res *drivers.ModelResult) error {
olap, ok := c.AsOLAP(c.instanceID)
if !ok {
return fmt.Errorf("connector is not an OLAP")
}
stagingTable, err := olap.InformationSchema().Lookup(ctx, "", "", stagingTableNameFor(res.Table))
if err == nil {
_ = olap.DropTable(ctx, stagingTable.Name, stagingTable.View)
}
table, err := olap.InformationSchema().Lookup(ctx, "", "", res.Table)
if err != nil {
return err
}
return olap.DropTable(ctx, table.Name, table.View)
}
// stagingTableName returns a stable temporary table name for a destination table.
// By using a stable temporary table name, we can ensure proper garbage collection without managing additional state.
func stagingTableNameFor(table string) string {
return "__rill_tmp_model_" + table
}
// olapForceRenameTable renames a table or view from fromName to toName in the OLAP connector.
// If a view or table already exists with toName, it is overwritten.
func olapForceRenameTable(ctx context.Context, olap drivers.OLAPStore, fromName string, fromIsView bool, toName string) error {
if fromName == "" || toName == "" {
return fmt.Errorf("cannot rename empty table name: fromName=%q, toName=%q", fromName, toName)
}
if fromName == toName {
return nil
}
// Infer SQL keyword for the table type
var typ string
if fromIsView {
typ = "VIEW"
} else {
typ = "TABLE"
}
// Renaming a table to the same name with different casing is not supported. Workaround by renaming to a temporary name first.
if strings.EqualFold(fromName, toName) {
tmpName := fmt.Sprintf("__rill_tmp_rename_%s_%s", typ, toName)
err := olap.RenameTable(ctx, fromName, tmpName, fromIsView)
if err != nil {
return err
}
fromName = tmpName
}
// Do the rename
return olap.RenameTable(ctx, fromName, toName, fromIsView)
}