-
Notifications
You must be signed in to change notification settings - Fork 1
/
operator.go
176 lines (141 loc) · 4.49 KB
/
operator.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
package bigquery
import (
"context"
"io"
"github.com/bruin-data/bruin/pkg/executor"
"github.com/bruin-data/bruin/pkg/pipeline"
"github.com/bruin-data/bruin/pkg/query"
"github.com/bruin-data/bruin/pkg/scheduler"
"github.com/pkg/errors"
)
type materializer interface {
Render(task *pipeline.Asset, query string) (string, error)
}
type queryExtractor interface {
ExtractQueriesFromString(filepath string) ([]*query.Query, error)
}
type connectionFetcher interface {
GetBqConnection(name string) (DB, error)
}
type BasicOperator struct {
connection connectionFetcher
extractor queryExtractor
materializer materializer
}
func NewBasicOperator(conn connectionFetcher, extractor queryExtractor, materializer materializer) *BasicOperator {
return &BasicOperator{
connection: conn,
extractor: extractor,
materializer: materializer,
}
}
func (o BasicOperator) Run(ctx context.Context, ti scheduler.TaskInstance) error {
return o.RunTask(ctx, ti.GetPipeline(), ti.GetAsset())
}
func (o BasicOperator) RunTask(ctx context.Context, p *pipeline.Pipeline, t *pipeline.Asset) error {
queries, err := o.extractor.ExtractQueriesFromString(t.ExecutableFile.Content)
if err != nil {
return errors.Wrap(err, "cannot extract queries from the task file")
}
if len(queries) == 0 {
return nil
}
if len(queries) > 1 && t.Materialization.Type != pipeline.MaterializationTypeNone {
return errors.New("cannot enable materialization for tasks with multiple queries")
}
q := queries[0]
materialized, err := o.materializer.Render(t, q.String())
if err != nil {
return err
}
q.Query = materialized
connName, err := p.GetConnectionNameForAsset(t)
if err != nil {
return err
}
conn, err := o.connection.GetBqConnection(connName)
if err != nil {
return err
}
return conn.RunQueryWithoutResult(ctx, q)
}
type checkRunner interface {
Check(ctx context.Context, ti *scheduler.ColumnCheckInstance) error
}
type ColumnCheckOperator struct {
checkRunners map[string]checkRunner
}
func NewColumnCheckOperator(manager connectionFetcher) (*ColumnCheckOperator, error) {
return &ColumnCheckOperator{
checkRunners: map[string]checkRunner{
"not_null": &NotNullCheck{conn: manager},
"unique": &UniqueCheck{conn: manager},
"positive": &PositiveCheck{conn: manager},
"non_negative": &NonNegativeCheck{conn: manager},
"negative": &NegativeCheck{conn: manager},
"accepted_values": &AcceptedValuesCheck{conn: manager},
"pattern": &PatternCheck{conn: manager},
},
}, nil
}
func (o ColumnCheckOperator) Run(ctx context.Context, ti scheduler.TaskInstance) error {
test, ok := ti.(*scheduler.ColumnCheckInstance)
if !ok {
return errors.New("cannot run a non-column check instance")
}
executor, ok := o.checkRunners[test.Check.Name]
if !ok {
return errors.New("there is no executor configured for the check type, check cannot be run: " + test.Check.Name)
}
return executor.Check(ctx, test)
}
type customCheckRunner interface {
Check(ctx context.Context, ti *scheduler.CustomCheckInstance) error
}
func NewCustomCheckOperator(manager connectionFetcher) (*CustomCheckOperator, error) {
return &CustomCheckOperator{
checkRunner: &CustomCheck{conn: manager},
}, nil
}
type CustomCheckOperator struct {
checkRunner customCheckRunner
}
func (o *CustomCheckOperator) Run(ctx context.Context, ti scheduler.TaskInstance) error {
instance, ok := ti.(*scheduler.CustomCheckInstance)
if !ok {
return errors.New("cannot run a non-custom check instance")
}
return o.checkRunner.Check(ctx, instance)
}
type MetadataPushOperator struct {
connection connectionFetcher
}
func NewMetadataPushOperator(conn connectionFetcher) *MetadataPushOperator {
return &MetadataPushOperator{
connection: conn,
}
}
func (o *MetadataPushOperator) Run(ctx context.Context, ti scheduler.TaskInstance) error {
conn, err := ti.GetPipeline().GetConnectionNameForAsset(ti.GetAsset())
if err != nil {
return err
}
client, err := o.connection.GetBqConnection(conn)
if err != nil {
return err
}
writer := ctx.Value(executor.KeyPrinter).(io.Writer)
if writer == nil {
return errors.New("no writer found in context, please create an issue for this: https://github.com/bruin-data/bruin/issues")
}
err = client.UpdateTableMetadataIfNotExist(ctx, ti.GetAsset())
if err != nil {
var noMetadata NoMetadataUpdatedError
if errors.As(err, &noMetadata) {
_, _ = writer.Write([]byte("No metadata found to be pushed to BigQuery, skipping...\n"))
return nil
}
return err
}
return nil
}