-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks_cfg.go
More file actions
171 lines (146 loc) · 4.63 KB
/
checks_cfg.go
File metadata and controls
171 lines (146 loc) · 4.63 KB
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
// Copyright 2026 The DBQ Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dbqcore
import (
"os"
"gopkg.in/yaml.v3"
)
type OnFailAction string
const (
OnFailActionWarn OnFailAction = "warn"
OnFailActionError OnFailAction = "error"
)
type ChecksFileConfig struct {
Version string `yaml:"version"`
Rules []ValidationRule `yaml:"rules"`
}
type ValidationRule struct {
Dataset string `yaml:"dataset"`
Where string `yaml:"where,omitempty"`
Checks []DataQualityCheck `yaml:"checks"`
}
type DataQualityCheck struct {
Expression string `yaml:"-"`
Description string `yaml:"desc,omitempty"`
OnFail OnFailAction `yaml:"on_fail,omitempty"`
Query string `yaml:"query,omitempty"`
// Schema check fields
SchemaCheck *SchemaCheckConfig `yaml:"schema_check,omitempty"`
ParsedCheck *CheckExpression `yaml:"-"`
}
type SchemaCheckConfig struct {
ExpectColumnsOrdered *ExpectColumnsOrderedConfig `yaml:"expect_columns_ordered,omitempty"`
ExpectColumns *ExpectColumnsConfig `yaml:"expect_columns,omitempty"`
ColumnsNotPresent *ColumnsNotPresentConfig `yaml:"columns_not_present,omitempty"`
}
type ExpectColumnsOrderedConfig struct {
ColumnsOrder []string `yaml:"columns_order"`
}
type ExpectColumnsConfig struct {
Columns []string `yaml:"columns"`
}
type ColumnsNotPresentConfig struct {
Columns []string `yaml:"columns,omitempty"`
Pattern string `yaml:"pattern,omitempty"`
}
func (c *DataQualityCheck) UnmarshalYAML(node *yaml.Node) error {
if node.Kind == yaml.MappingNode && len(node.Content) >= 2 {
key := node.Content[0].Value
value := node.Content[1]
if key == CheckTypeSchemaCheck {
// Handle schema_check format
type tempCheck struct {
SchemaCheck *SchemaCheckConfig `yaml:"schema_check"`
Desc string `yaml:"desc,omitempty"`
OnFail OnFailAction `yaml:"on_fail,omitempty"`
}
// Decode the entire node to get all fields
var temp tempCheck
if err := node.Decode(&temp); err != nil {
return err
}
c.SchemaCheck = temp.SchemaCheck
c.Description = temp.Desc
c.OnFail = temp.OnFail
// For schema checks, we set the Expression but don't create ParsedCheck
if c.SchemaCheck != nil {
if c.SchemaCheck.ExpectColumnsOrdered != nil {
c.Expression = "expect_columns_ordered"
} else if c.SchemaCheck.ExpectColumns != nil {
c.Expression = "expect_columns"
} else if c.SchemaCheck.ColumnsNotPresent != nil {
c.Expression = "columns_not_present"
}
// ParsedCheck should be nil - the SchemaCheck field contains all needed info
}
} else if key == CheckTypeRawQuery {
c.Expression = key
var rawQueryCheck struct {
Desc string `yaml:"desc,omitempty"`
Query string `yaml:"query"`
OnFail OnFailAction `yaml:"on_fail,omitempty"`
}
if err := value.Decode(&rawQueryCheck); err != nil {
return err
}
c.Description = rawQueryCheck.Desc
c.Query = rawQueryCheck.Query
c.OnFail = rawQueryCheck.OnFail
parsedCheck, err := ParseCheckExpression(CheckTypeRawQuery)
if err != nil {
return err
}
c.ParsedCheck = parsedCheck
} else {
c.Expression = key
if value.Kind == yaml.MappingNode {
var checkDetails struct {
Desc string `yaml:"desc,omitempty"`
OnFail OnFailAction `yaml:"on_fail,omitempty"`
}
if err := value.Decode(&checkDetails); err != nil {
return err
}
c.Description = checkDetails.Desc
c.OnFail = checkDetails.OnFail
}
parsedCheck, err := ParseCheckExpression(key)
if err != nil {
return err
}
c.ParsedCheck = parsedCheck
}
} else if node.Kind == yaml.ScalarNode {
c.Expression = node.Value
parsedCheck, err := ParseCheckExpression(node.Value)
if err != nil {
return err
}
c.ParsedCheck = parsedCheck
}
return nil
}
func LoadChecksFileConfig(fileName string) (*ChecksFileConfig, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
var cfg ChecksFileConfig
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&cfg); err != nil {
return nil, err
}
return &cfg, nil
}