-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathvalidators.go
More file actions
95 lines (87 loc) · 2.22 KB
/
validators.go
File metadata and controls
95 lines (87 loc) · 2.22 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
package schema
import (
"encoding/json"
"slices"
"strings"
"github.com/apache/arrow-go/v18/arrow"
"github.com/cloudquery/plugin-sdk/v4/types"
)
func isValueValid(i int, arr arrow.Array) bool {
if arr.IsValid(i) {
if arrow.TypeEqual(arr.DataType(), types.ExtensionTypes.JSON) {
// JSON column shouldn't be empty
val := arr.GetOneForMarshal(i).(json.RawMessage)
if isEmptyJSON(val) {
return false
}
}
return true
}
return false
}
func FindEmptyColumns(table *Table, records []arrow.RecordBatch) []string {
columnsWithValues := make([]bool, len(table.Columns))
emptyColumns := make([]string, 0)
for _, resource := range records {
for colIndex, arr := range resource.Columns() {
allValuesValid := true
for i := 0; i < arr.Len(); i++ {
if !isValueValid(i, arr) {
allValuesValid = false
break
}
}
if allValuesValid {
columnsWithValues[colIndex] = true
}
}
}
// Make sure every column has at least one value.
for i, hasValue := range columnsWithValues {
col := table.Columns[i]
emptyExpected := col.Name == "_cq_parent_id" && table.Parent == nil
if !hasValue && !emptyExpected && !col.IgnoreInTests {
emptyColumns = append(emptyColumns, col.Name)
}
}
return emptyColumns
}
func FindNotMatchingSensitiveColumns(table *Table) (nonMatchingColumns []string, nonMatchingJSONColumns []string) {
if len(table.SensitiveColumns) == 0 {
return []string{}, []string{}
}
nonMatchingColumns = make([]string, 0)
nonMatchingJSONColumns = make([]string, 0)
tableColumns := table.Columns.Names()
for _, c := range table.SensitiveColumns {
isJSONPath := false
if strings.Contains(c, ".") {
c = strings.Split(c, ".")[0]
isJSONPath = true
}
if !slices.Contains(tableColumns, c) {
nonMatchingColumns = append(nonMatchingColumns, c)
continue
}
if !isJSONPath {
continue
}
col := table.Columns.Get(c)
if !arrow.TypeEqual(col.Type, types.ExtensionTypes.JSON) {
nonMatchingJSONColumns = append(nonMatchingJSONColumns, c)
continue
}
}
return nonMatchingColumns, nonMatchingJSONColumns
}
func isEmptyJSON(msg json.RawMessage) bool {
if len(msg) == 0 {
return true
}
switch string(msg) {
case "null", "{}", "[]":
return true
default:
return false
}
}