-
Notifications
You must be signed in to change notification settings - Fork 108
/
olap.go
253 lines (220 loc) · 8.25 KB
/
olap.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package drivers
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
)
// ErrUnsupportedConnector is returned from Ingest for unsupported connectors.
var ErrUnsupportedConnector = errors.New("drivers: connector not supported")
// WithConnectionFunc is a callback function that provides a context to be used in further OLAP store calls to enforce affinity to a single connection.
// It also provides pointers to the actual database/sql and database/sql/driver connections.
// It's called with two contexts: wrappedCtx wraps the input context (including cancellation),
// and ensuredCtx wraps a background context (ensuring it can never be cancelled).
type WithConnectionFunc func(wrappedCtx context.Context, ensuredCtx context.Context, conn *sql.Conn) error
// OLAPStore is implemented by drivers that are capable of storing, transforming and serving analytical queries.
// NOTE crud APIs are not safe to be called with `WithConnection`
type OLAPStore interface {
Dialect() Dialect
WithConnection(ctx context.Context, priority int, longRunning, tx bool, fn WithConnectionFunc) error
Exec(ctx context.Context, stmt *Statement) error
Execute(ctx context.Context, stmt *Statement) (*Result, error)
InformationSchema() InformationSchema
EstimateSize() (int64, bool)
CreateTableAsSelect(ctx context.Context, name string, view bool, sql string) error
InsertTableAsSelect(ctx context.Context, name, sql string, byName, inPlace bool, strategy IncrementalStrategy, uniqueKey []string) error
DropTable(ctx context.Context, name string, view bool) error
RenameTable(ctx context.Context, name, newName string, view bool) error
AddTableColumn(ctx context.Context, tableName, columnName string, typ string) error
AlterTableColumn(ctx context.Context, tableName, columnName string, newType string) error
}
// Statement wraps a query to execute against an OLAP driver.
type Statement struct {
Query string
Args []any
DryRun bool
Priority int
LongRunning bool
ExecutionTimeout time.Duration
}
// Result wraps the results of query.
type Result struct {
*sqlx.Rows
Schema *runtimev1.StructType
cleanupFn func() error
}
// SetCleanupFunc sets a function, which will be called when the Result is closed.
func (r *Result) SetCleanupFunc(fn func() error) {
if r.cleanupFn != nil {
panic("cleanup function already set")
}
r.cleanupFn = fn
}
// Close wraps rows.Close and calls the Result's cleanup function (if it is set).
// Close should be idempotent.
func (r *Result) Close() error {
firstErr := r.Rows.Close()
if r.cleanupFn != nil {
err := r.cleanupFn()
if firstErr == nil {
firstErr = err
}
// Prevent cleanupFn from being called multiple times.
// NOTE: Not idempotent for error returned from cleanupFn.
r.cleanupFn = nil
}
return firstErr
}
// InformationSchema contains information about existing tables in an OLAP driver.
// Table lookups should be case insensitive.
type InformationSchema interface {
All(ctx context.Context) ([]*Table, error)
Lookup(ctx context.Context, db, schema, name string) (*Table, error)
}
// Table represents a table in an information schema.
type Table struct {
Database string
DatabaseSchema string
IsDefaultDatabase bool
IsDefaultDatabaseSchema bool
Name string
View bool
Schema *runtimev1.StructType
UnsupportedCols map[string]string
}
// IngestionSummary is details about ingestion
type IngestionSummary struct {
BytesIngested int64
}
// IncrementalStrategy is a strategy to use for incrementally inserting data into a SQL table.
type IncrementalStrategy string
const (
IncrementalStrategyUnspecified IncrementalStrategy = ""
IncrementalStrategyAppend IncrementalStrategy = "append"
IncrementalStrategyMerge IncrementalStrategy = "merge"
)
// Dialect enumerates OLAP query languages.
type Dialect int
const (
DialectUnspecified Dialect = iota
DialectDuckDB
DialectDruid
DialectClickHouse
DialectPinot
)
func (d Dialect) String() string {
switch d {
case DialectUnspecified:
return ""
case DialectDuckDB:
return "duckdb"
case DialectDruid:
return "druid"
case DialectClickHouse:
return "clickhouse"
case DialectPinot:
return "pinot"
default:
panic("not implemented")
}
}
// EscapeIdentifier returns an escaped SQL identifier in the dialect.
func (d Dialect) EscapeIdentifier(ident string) string {
if ident == "" {
return ident
}
return fmt.Sprintf("\"%s\"", strings.ReplaceAll(ident, "\"", "\"\""))
}
func (d Dialect) ConvertToDateTruncSpecifier(specifier runtimev1.TimeGrain) string {
var str string
switch specifier {
case runtimev1.TimeGrain_TIME_GRAIN_MILLISECOND:
str = "MILLISECOND"
case runtimev1.TimeGrain_TIME_GRAIN_SECOND:
str = "SECOND"
case runtimev1.TimeGrain_TIME_GRAIN_MINUTE:
str = "MINUTE"
case runtimev1.TimeGrain_TIME_GRAIN_HOUR:
str = "HOUR"
case runtimev1.TimeGrain_TIME_GRAIN_DAY:
str = "DAY"
case runtimev1.TimeGrain_TIME_GRAIN_WEEK:
str = "WEEK"
case runtimev1.TimeGrain_TIME_GRAIN_MONTH:
str = "MONTH"
case runtimev1.TimeGrain_TIME_GRAIN_QUARTER:
str = "QUARTER"
case runtimev1.TimeGrain_TIME_GRAIN_YEAR:
str = "YEAR"
}
if d == DialectClickHouse {
return strings.ToLower(str)
}
return str
}
// EscapeTable returns an esacped fully qualified table name
func (d Dialect) EscapeTable(db, schema, table string) string {
var sb strings.Builder
if db != "" {
sb.WriteString(d.EscapeIdentifier(db))
sb.WriteString(".")
}
if schema != "" {
sb.WriteString(d.EscapeIdentifier(schema))
sb.WriteString(".")
}
sb.WriteString(d.EscapeIdentifier(table))
return sb.String()
}
func (d Dialect) DimensionSelect(db, dbSchema, table string, dim *runtimev1.MetricsViewSpec_DimensionV2) (dimSelect, unnestClause string) {
colName := d.EscapeIdentifier(dim.Name)
if !dim.Unnest || d == DialectDruid {
return fmt.Sprintf(`(%s) as %s`, d.MetricsViewDimensionExpression(dim), colName), ""
}
unnestColName := d.EscapeIdentifier(tempName(fmt.Sprintf("%s_%s_", "unnested", dim.Name)))
unnestTableName := tempName("tbl")
sel := fmt.Sprintf(`%s as %s`, unnestColName, colName)
if dim.Expression == "" {
// select "unnested_colName" as "colName" ... FROM "mv_table", LATERAL UNNEST("mv_table"."colName") tbl_name("unnested_colName") ...
return sel, fmt.Sprintf(`, LATERAL UNNEST(%s.%s) %s(%s)`, d.EscapeTable(db, dbSchema, table), colName, unnestTableName, unnestColName)
}
return sel, fmt.Sprintf(`, LATERAL UNNEST(%s) %s(%s)`, dim.Expression, unnestTableName, unnestColName)
}
func (d Dialect) DimensionSelectPair(db, dbSchema, table string, dim *runtimev1.MetricsViewSpec_DimensionV2) (expr, alias, unnestClause string) {
colName := d.EscapeIdentifier(dim.Name)
if !dim.Unnest || d == DialectDruid {
return d.MetricsViewDimensionExpression(dim), colName, ""
}
unnestColName := d.EscapeIdentifier(tempName(fmt.Sprintf("%s_%s_", "unnested", dim.Name)))
unnestTableName := tempName("tbl")
if dim.Expression == "" {
// select "unnested_colName" as "colName" ... FROM "mv_table", LATERAL UNNEST("mv_table"."colName") tbl_name("unnested_colName") ...
return unnestColName, colName, fmt.Sprintf(`, LATERAL UNNEST(%s.%s) %s(%s)`, d.EscapeTable(db, dbSchema, table), colName, unnestTableName, unnestColName)
}
return unnestColName, colName, fmt.Sprintf(`, LATERAL UNNEST(%s) %s(%s)`, dim.Expression, unnestTableName, unnestColName)
}
func (d Dialect) LateralUnnest(expr, tableAlias, colName string) (tbl string, auto bool, err error) {
if d == DialectDruid || d == DialectPinot {
return "", true, nil
}
return fmt.Sprintf(`LATERAL UNNEST(%s) %s(%s)`, expr, tableAlias, d.EscapeIdentifier(colName)), false, nil
}
func (d Dialect) MetricsViewDimensionExpression(dimension *runtimev1.MetricsViewSpec_DimensionV2) string {
if dimension.Expression != "" {
return dimension.Expression
}
if dimension.Column != "" {
return d.EscapeIdentifier(dimension.Column)
}
// backwards compatibility for older projects that have not run reconcile on this dashboard
// in that case `column` will not be present
return d.EscapeIdentifier(dimension.Name)
}
func tempName(prefix string) string {
return prefix + strings.ReplaceAll(uuid.New().String(), "-", "")
}