-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquals.go
More file actions
305 lines (269 loc) · 8.51 KB
/
quals.go
File metadata and controls
305 lines (269 loc) · 8.51 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"fmt"
"golang.org/x/exp/maps"
"log"
"slices"
"strconv"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/turbot/pipe-fittings/v2/sperr"
sdkfilter "github.com/turbot/steampipe-plugin-sdk/v5/filter"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
)
// buildQuals builds a map of quals from the provided where clauses and table schema.
func buildQuals(whereClauses []string, schema *proto.TableSchema) (map[string]*proto.Quals, error) {
var quals = make(map[string]*proto.Quals)
if len(whereClauses) > 0 {
for _, whereFlag := range whereClauses {
qual, err := filterStringToQuals(whereFlag, schema)
if err != nil {
return nil, err
}
for columnName, q := range qual {
if zQual, found := quals[columnName]; found {
zQual.Quals = append(zQual.Quals, q.Quals...)
} else {
quals[columnName] = q
}
}
}
}
return quals, nil
}
// filterStringToQuals converts a filter string into a map of quals based on the provided table schema.
func filterStringToQuals(raw string, tableSchema *proto.TableSchema) (map[string]*proto.Quals, error) {
columnMap := tableSchema.GetColumnMap()
keyColumns := tableSchema.GetAllKeyColumns()
parsed, err := sdkfilter.Parse("", []byte(raw))
if err != nil {
log.Printf("err %v", err)
return nil, sperr.New("failed to parse 'where' property: %s", err.Error())
}
// convert table schema into a column map
filter := parsed.(sdkfilter.ComparisonNode)
log.Println(filter)
var qual *proto.Qual
var column string
switch filter.Type {
case "compare", "like":
codeNodes, ok := filter.Values.([]sdkfilter.CodeNode)
if !ok {
return nil, fmt.Errorf("failed to parse filter")
}
if len(codeNodes) != 2 {
return nil, fmt.Errorf("failed to parse filter")
}
column = codeNodes[0].Value
value := codeNodes[1].Value
operator := filter.Operator.Value
// map the operator
mappedOperator := mapOperator(operator)
// validate this qual
// - the column exists in the table
// - the column is a key column
// - the operator is supported
if err := validateQual(column, mappedOperator, columnMap, keyColumns); err != nil {
return nil, err
}
// convert the value string into a qual
columnType := columnMap[column].Type
qualValue, err := stringToQualValue(value, columnType)
if err != nil {
return nil, err
}
qual = &proto.Qual{
FieldName: column,
Operator: &proto.Qual_StringValue{mappedOperator},
Value: qualValue,
}
case "in":
if filter.Operator.Value == "not in" {
return nil, fmt.Errorf("failed to convert 'where' arg to qual - 'not in' is not supported")
}
codeNodes, ok := filter.Values.([]sdkfilter.CodeNode)
if !ok || len(codeNodes) < 2 {
return nil, fmt.Errorf("failed to parse filter")
}
column = codeNodes[0].Value
operator := "="
// map the operator
mappedOperator := mapOperator(operator)
// validate this qual
// - the column exists in the table
// - the colummn is a key column
// - the operator is supported
if err := validateQual(column, mappedOperator, columnMap, keyColumns); err != nil {
return nil, err
}
// Build look up of values
values := make(map[string]struct{}, len(codeNodes)-1)
for _, c := range codeNodes[1:] {
values[c.Value] = struct{}{}
}
// Convert these raw values into a qual
columnType := columnMap[column].Type
qualValue, err := stringToQualListValue(maps.Keys(values), columnType)
if err != nil {
return nil, err
}
// Create a Qual slice for the field and add the Qual to it
qual = &proto.Qual{
FieldName: column,
Operator: &proto.Qual_StringValue{mappedOperator},
Value: qualValue,
}
default:
return nil, fmt.Errorf("failed to convert 'where' arg to qual")
}
if qual == nil {
// unexpected
return nil, fmt.Errorf("failed to convert 'where' arg to qual")
}
qualmap := make(map[string]*proto.Quals)
qualmap[column] = &proto.Quals{Quals: []*proto.Qual{qual}}
return qualmap, nil
}
// validate this qual
// - the column exists in the table
// - the colummn is a key column
// - the operator is supported
func validateQual(column, operator string, columnMap map[string]*proto.ColumnDefinition, quals []*proto.KeyColumn) error {
// does the column exists in the table
_, ok := columnMap[column]
if !ok {
return fmt.Errorf("column %s does not exist", column)
}
unsupportedOperator := false
// is the column is a key column
for _, keyColumn := range quals {
// is this key column for the target column
if keyColumn.Name == column {
// check the operator is supported
if isOperatorSupported(keyColumn.Operators, operator) {
// ok this qual is valid
return nil
} else {
unsupportedOperator = true
}
}
}
if unsupportedOperator {
return fmt.Errorf("key column for '%s' does not support operator '%s'", column, operator)
}
return fmt.Errorf("there is no key column defined for column '%s'", column)
}
// stringToQualValue converts a string value to a QualValue based on the column type.
func stringToQualValue(valueString string, columnType proto.ColumnType) (*proto.QualValue, error) {
result := &proto.QualValue{}
switch columnType {
case proto.ColumnType_BOOL:
b, err := strconv.ParseBool(valueString)
if err != nil {
return nil, err
}
result.Value = &proto.QualValue_BoolValue{BoolValue: b}
case proto.ColumnType_INT:
i, err := strconv.ParseInt(valueString, 10, 64)
if err != nil {
return nil, err
}
result.Value = &proto.QualValue_Int64Value{Int64Value: i}
case proto.ColumnType_DOUBLE:
f, err := strconv.ParseFloat(valueString, 64)
if err != nil {
return nil, err
}
result.Value = &proto.QualValue_DoubleValue{DoubleValue: f}
case proto.ColumnType_STRING:
result.Value = &proto.QualValue_StringValue{StringValue: valueString}
case proto.ColumnType_JSON:
result.Value = &proto.QualValue_JsonbValue{JsonbValue: valueString}
case proto.ColumnType_IPADDR:
// todo parse
case proto.ColumnType_CIDR:
// todo parse
case proto.ColumnType_INET:
// todo parse
case proto.ColumnType_DATETIME, proto.ColumnType_TIMESTAMP:
var t time.Time
var err error
// Try parsing as Unix timestamp (seconds since epoch)
if unixTime, err := strconv.ParseInt(valueString, 10, 64); err == nil {
t = time.Unix(unixTime, 0)
ts, err := ptypes.TimestampProto(t)
if err != nil {
return nil, fmt.Errorf("failed to convert Unix time to timestamp: %v", err)
}
result.Value = &proto.QualValue_TimestampValue{TimestampValue: ts}
return result, nil
}
// Try parsing with multiple common time formats
formats := []string{
time.RFC3339,
time.RFC3339Nano,
"2006-01-02T15:04:05",
"2006-01-02 15:04:05",
"2006-01-02",
time.RFC1123,
time.RFC1123Z,
time.RFC822,
time.RFC822Z,
}
for _, format := range formats {
t, err = time.Parse(format, valueString)
if err == nil {
break
}
}
if err != nil {
return nil, fmt.Errorf("could not parse time value '%s' with any supported format", valueString)
}
ts, err := ptypes.TimestampProto(t)
if err != nil {
return nil, fmt.Errorf("failed to convert time to timestamp: %v", err)
}
result.Value = &proto.QualValue_TimestampValue{TimestampValue: ts}
case proto.ColumnType_LTREE:
result.Value = &proto.QualValue_LtreeValue{LtreeValue: valueString}
}
if result.Value == nil {
return nil, fmt.Errorf("faile to convert value string")
}
return result, nil
}
// stringToQualListValue converts a slice of strings into a QualValue with a ListValue type.
func stringToQualListValue(values []string, columnType proto.ColumnType) (*proto.QualValue, error) {
res := &proto.QualValue{
Value: &proto.QualValue_ListValue{
ListValue: &proto.QualValueList{
Values: make([]*proto.QualValue, len(values)),
},
},
}
for i, v := range values {
qv, err := stringToQualValue(v, columnType)
if err != nil {
return nil, err
}
res.Value.(*proto.QualValue_ListValue).ListValue.Values[i] = qv
}
return res, nil
}
// mapOperator translates equivalent operator representations to a standard form.
func mapOperator(operator string) string {
operatorMappings := map[string]string{
"like": "~~", // Map "like" to "~~"
// TODO PSKR: Add more mappings here as needed.
}
// Check if the operator is in the mapping, if so, return the mapped value.
if mappedOperator, ok := operatorMappings[operator]; ok {
return mappedOperator
}
// If no mapping is found, return the original operator.
return operator
}
func isOperatorSupported(keyColumns []string, mappedOperator string) bool {
// Check if the mapped operator is supported.
return slices.Contains(keyColumns, mappedOperator)
}