forked from turbot/steampipe-postgres-fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quals.go
539 lines (471 loc) · 17.6 KB
/
quals.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package main
/*
#cgo linux LDFLAGS: -Wl,-unresolved-symbols=ignore-all
#cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup
#include "fdw_helpers.h"
*/
import "C"
import (
"fmt"
"github.com/gertd/go-pluralize"
"log"
"net"
"unsafe"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/quals"
)
func restrictionsToQuals(node *C.ForeignScanState, cinfos *conversionInfos) (qualsList *proto.Quals, unhandledRestrictions int) {
defer func() {
if r := recover(); r != nil {
log.Printf("[WARN] restrictionsToQuals recovered from panic: %v", r)
}
}()
plan := (*C.ForeignScan)(unsafe.Pointer(node.ss.ps.plan))
restrictions := plan.fdw_exprs
qualsList = &proto.Quals{}
if restrictions == nil {
return qualsList, 0
}
for it := C.list_head(restrictions); it != nil; it = C.lnext(restrictions, it) {
restriction := C.cellGetExpr(it)
log.Printf("[TRACE] RestrictionsToQuals: restriction %s", C.GoString(C.tagTypeToString(C.fdw_nodeTag(restriction))))
var q *proto.Qual
switch C.fdw_nodeTag(restriction) {
case C.T_OpExpr:
q = qualFromOpExpr(C.cellGetOpExpr(it), node, cinfos)
case C.T_Var:
q = qualFromVar(C.cellGetVar(it), node, cinfos)
case C.T_ScalarArrayOpExpr:
q = qualFromScalarOpExpr(C.cellGetScalarArrayOpExpr(it), node, cinfos)
case C.T_NullTest:
q = qualFromNullTest(C.cellGetNullTest(it), node, cinfos)
//extractClauseFromNullTest(base_relids, (NullTest *) node, qualsList);
case C.T_BooleanTest:
q = qualFromBooleanTest((*C.BooleanTest)(unsafe.Pointer(restriction)), node, cinfos)
case C.T_BoolExpr:
q = qualFromBoolExpr((*C.BoolExpr)(unsafe.Pointer(restriction)), node, cinfos)
}
if q != nil {
qualsList.Append(q)
} else {
// we failed to handle this restriction
unhandledRestrictions++
}
}
log.Printf("[TRACE] RestrictionsToQuals: converted postgres restrictions protobuf quals")
for _, q := range qualsList.Quals {
log.Printf("[TRACE] %s", grpc.QualToString(q))
}
if unhandledRestrictions > 0 {
log.Printf("[WARN] RestrictionsToQuals: failed to convert %d %s to quals",
unhandledRestrictions,
pluralize.NewClient().Pluralize("restriction", unhandledRestrictions, false))
}
return qualsList, unhandledRestrictions
}
// build a protobuf qual from an OpExpr
func qualFromOpExpr(restriction *C.OpExpr, node *C.ForeignScanState, cinfos *conversionInfos) *proto.Qual {
log.Printf("[TRACE] qualFromOpExpr")
plan := (*C.ForeignScan)(unsafe.Pointer(node.ss.ps.plan))
relids := C.bms_make_singleton(C.int(plan.scan.scanrelid))
restriction = C.canonicalOpExpr(restriction, relids)
if restriction == nil {
log.Printf("[INFO] could not convert OpExpr to canonical form - NOT adding qual for OpExpr")
return nil
}
left := (*C.Var)(C.list_nth(restriction.args, 0))
right := C.list_nth(restriction.args, 1)
// Do not add it if it either contains a mutable function, or makes self references in the right hand side.
if C.contain_volatile_functions((*C.Node)(right)) || C.bms_is_subset(relids, C.pull_varnos(nil, (*C.Node)(right))) {
log.Printf("[TRACE] restriction either contains a mutable function, or makes self references in the right hand side - NOT adding qual for OpExpr")
return nil
}
var arrayIndex = int(left.varattno - 1)
ci := cinfos.get(arrayIndex)
if ci == nil {
log.Printf("[WARN] failed to convert qual value - could not get conversion info for attribute %d", arrayIndex)
return nil
}
qualValue, err := getQualValue(right, node, ci)
if err != nil {
log.Printf("[INFO] failed to convert qual value; %v", err)
return nil
}
column := C.GoString(ci.attrname)
operatorName := C.GoString(C.getOperatorString(restriction.opno))
qual := &proto.Qual{
FieldName: column,
Operator: &proto.Qual_StringValue{StringValue: operatorName},
Value: qualValue,
}
log.Printf("[TRACE] qualFromOpExpr returning %v", qual)
return qual
}
// build a protobuf qual from a Var - this converts to a simple boolean qual where column=true
func qualFromVar(arg *C.Var, node *C.ForeignScanState, cinfos *conversionInfos) *proto.Qual {
column := columnFromVar(arg, cinfos)
// if we failed to get a column we cannot create a qual
if column == "" {
log.Printf("[WARN] qualFromVar failed to get column from variable %v", arg)
return nil
}
return &proto.Qual{
FieldName: column,
Operator: &proto.Qual_StringValue{StringValue: "="},
Value: &proto.QualValue{Value: &proto.QualValue_BoolValue{BoolValue: true}},
}
}
func qualFromScalarOpExpr(restriction *C.ScalarArrayOpExpr, node *C.ForeignScanState, cinfos *conversionInfos) *proto.Qual {
plan := (*C.ForeignScan)(unsafe.Pointer(node.ss.ps.plan))
relids := C.bms_make_singleton(C.int(plan.scan.scanrelid))
restriction = C.canonicalScalarArrayOpExpr(restriction, relids)
if restriction == nil {
log.Printf("[WARN] could not convert OpExpr to canonical form - NOT adding qual for OpExpr")
return nil
}
left := (*C.Var)(C.list_nth(restriction.args, 0))
right := C.list_nth(restriction.args, 1)
// Do not add it if it either contains a mutable function, or makes self references in the right hand side.
if C.contain_volatile_functions((*C.Node)(right)) || C.bms_is_subset(relids, C.pull_varnos(nil, (*C.Node)(right))) {
log.Printf("[TRACE] restriction either contains a mutable function, or makes self references in the right hand side - NOT adding qual for OpExpr")
return nil
}
var arrayIndex = int(left.varattno - 1)
ci := cinfos.get(arrayIndex)
if ci == nil {
log.Printf("[WARN]] failed to convert qual value - could not get conversion info for attribute %d", arrayIndex)
return nil
}
qualValue, err := getQualValue(right, node, ci)
if err != nil {
log.Printf("[WARN] failed to convert qual value; %v", err)
return nil
}
column := C.GoString(ci.attrname)
operatorName := C.GoString(C.getOperatorString(restriction.opno))
qual := &proto.Qual{
FieldName: column,
Operator: &proto.Qual_StringValue{StringValue: operatorName},
Value: qualValue,
}
return qual
}
// build a protobuf qual from a NullTest
func qualFromNullTest(restriction *C.NullTest, node *C.ForeignScanState, cinfos *conversionInfos) *proto.Qual {
if C.fdw_nodeTag(restriction.arg) != C.T_Var {
return nil
}
arg := (*C.Var)(unsafe.Pointer(restriction.arg))
if arg.varattno < 1 {
return nil
}
var operatorName string
if restriction.nulltesttype == C.IS_NULL {
operatorName = quals.QualOperatorIsNull
} else {
operatorName = quals.QualOperatorIsNotNull
}
// try to get the column
column := columnFromVar(arg, cinfos)
// if we failed to get a column we cannot create a qual
if column == "" {
log.Printf("[WARN] qualFromNullTest failed to get column from variable %v", arg)
return nil
}
qual := &proto.Qual{
FieldName: column,
Operator: &proto.Qual_StringValue{StringValue: operatorName},
Value: nil,
}
return qual
}
// build a protobuf qual from a BoolTest
func qualFromBooleanTest(restriction *C.BooleanTest, node *C.ForeignScanState, cinfos *conversionInfos) *proto.Qual {
arg := restriction.arg
if C.fdw_nodeTag(arg) != C.T_Var {
return nil
}
// try to get the column
variable := (*C.Var)(unsafe.Pointer(arg))
column := columnFromVar(variable, cinfos)
// if we failed to get a column we cannot create a qual
if column == "" {
log.Printf("[WARN] qualFromBooleanTest failed to get column from variable %v", variable)
return nil
}
// now populate the operator
operatorName := ""
switch restriction.booltesttype {
case C.IS_TRUE:
operatorName = "="
case C.IS_NOT_TRUE, C.IS_FALSE:
operatorName = "<>"
default:
return nil
}
qual := &proto.Qual{
FieldName: column,
Operator: &proto.Qual_StringValue{StringValue: operatorName},
Value: &proto.QualValue{Value: &proto.QualValue_BoolValue{BoolValue: true}},
}
return qual
}
// convert a boolean expression into a qual
// currently we only support simple expressions like column=true
func qualFromBoolExpr(restriction *C.BoolExpr, node *C.ForeignScanState, cinfos *conversionInfos) *proto.Qual {
arg := C.cellGetExpr(C.list_head(restriction.args))
// NOTE currently we only handle boolean expression with a single argument and a NOT operato
if restriction.args.length == 1 || restriction.boolop == C.NOT_EXPR && C.fdw_nodeTag(arg) == C.T_Var {
// try to get the column from the variable
variable := C.cellGetVar(C.list_head(restriction.args))
column := columnFromVar(variable, cinfos)
// if we failed to get a column we cannot create a qual
if column == "" {
log.Printf("[WARN] qualFromBoolExpr failed to get column from variable %v", arg)
return nil
}
return &proto.Qual{
FieldName: column,
Operator: &proto.Qual_StringValue{StringValue: "<>"},
Value: &proto.QualValue{Value: &proto.QualValue_BoolValue{BoolValue: true}},
}
}
return nil
}
func columnFromVar(variable *C.Var, cinfos *conversionInfos) string {
var arrayIndex = int(variable.varattno - 1)
if arrayIndex < 0 {
log.Printf("[WARN] columnFromVar failed - index %d, returning empty string", arrayIndex)
return ""
}
ci := cinfos.get(arrayIndex)
if ci == nil {
log.Printf("[WARN] columnFromVar failed - could not get conversion info for index %d, returning empty string", arrayIndex)
return ""
}
if ci.attrname == nil {
log.Printf("[WARN] columnFromVar failed - conversion info for index %d has no attrname, returning empty string", arrayIndex)
return ""
}
return C.GoString(ci.attrname)
}
func getQualValue(right unsafe.Pointer, node *C.ForeignScanState, ci *C.ConversionInfo) (*proto.QualValue, error) {
log.Printf("[TRACE] getQualValue")
var isNull C.bool
var typeOid C.Oid
var value C.Datum
valueExpression := (*C.Expr)(right)
switch C.fdw_nodeTag(valueExpression) {
case C.T_Const:
constQual := (*C.Const)(right)
typeOid = constQual.consttype
value = constQual.constvalue
isNull = constQual.constisnull
log.Printf("[TRACE] getQualValue T_Const qual, value %v", value)
case C.T_Param:
paramQual := (*C.Param)(right)
typeOid = paramQual.paramtype
exprState := C.ExecInitExpr(valueExpression, (*C.PlanState)(unsafe.Pointer(node)))
econtext := node.ss.ps.ps_ExprContext
value = C.ExecEvalExpr(exprState, econtext, &isNull)
log.Printf("[TRACE] getQualValue T_Param qual, value %v", value)
case C.T_OpExpr:
/* T_OpExpr may be something like
where date_time_column > current_timestamp - interval '1 hr'
or
select * from
aws_ec2_instance as i,
jsonb_array_elements(i.block_device_mappings) as b,
aws_ebs_volume as v
where
v.volume_id = b -> 'Ebs' ->> 'VolumeId';
evaluating jsonb_array_elements causes a crash, so exclude these quals from the evaluation
*/
opExprQual := (*C.OpExpr)(right)
log.Printf("[TRACE] getQualValue T_OpExpr qual opno %d, opfuncid %d", opExprQual.opno, opExprQual.opfuncid)
// TACTICAL we know that trying to evaluate a qual deriving from a jsonb_array_elements function call causes
// ExecEvalExpr to crash - so skip evaluation in that case
if opExprQual.opfuncid == 3214 {
log.Printf("[TRACE] skipping OpExpr evaluation as opfuncid 3214 (jsonb_array_elements) is not supported)")
return nil, fmt.Errorf("QualDefsToQuals: unsupported qual value (type %s, opfuncid %d), skipping\n", C.GoString(C.tagTypeToString(C.fdw_nodeTag(valueExpression))), opExprQual.opfuncid)
}
typeOid = opExprQual.opresulttype
exprState := C.ExecInitExpr(valueExpression, (*C.PlanState)(unsafe.Pointer(node)))
econtext := node.ss.ps.ps_ExprContext
value = C.ExecEvalExpr(exprState, econtext, &isNull)
log.Printf("[TRACE] getQualValue T_OpExpr qual, value %v, isNull %v", value, isNull)
default:
return nil, fmt.Errorf("QualDefsToQuals: unsupported qual value (type %s), skipping\n", C.GoString(C.tagTypeToString(C.fdw_nodeTag(valueExpression))))
}
var qualValue *proto.QualValue
if isNull {
log.Printf("[TRACE] qualDef.isnull=true - returning qual with nil value")
qualValue = nil
} else {
if typeOid == C.InvalidOid {
typeOid = ci.atttypoid
}
var err error
if qualValue, err = datumToQualValue(value, typeOid, ci); err != nil {
return nil, err
}
}
return qualValue, nil
}
func datumToQualValue(datum C.Datum, typeOid C.Oid, cinfo *C.ConversionInfo) (result *proto.QualValue, err error) {
/* we support these postgres column types (see sqlTypeForColumnType):
bool
bigint
double precision
text
inet
cidr
jsonb
timestamp
so we must handle quals of all these types
*/
result = &proto.QualValue{}
switch typeOid {
case C.TEXTOID, C.VARCHAROID:
result.Value = &proto.QualValue_StringValue{StringValue: C.GoString(C.datumString(datum, cinfo))}
case C.INETOID, C.CIDROID:
// handle zero value - return nil
if datum == 0 {
break
}
inet := C.datumInet(datum, cinfo)
ipAddrBytes := C.GoBytes(unsafe.Pointer(C.ipAddr(inet)), 16)
netmaskBits := int32(C.netmaskBits(inet))
var ipAddrString string
var protocolVersion string
if C.isIpV6(inet) {
ipAddrString = net.IP(ipAddrBytes).String()
protocolVersion = grpc.IPv6
log.Printf("[TRACE] ipv6 qual: %s/%d", ipAddrString, netmaskBits)
} else {
ipAddrString = net.IPv4(ipAddrBytes[0], ipAddrBytes[1], ipAddrBytes[2], ipAddrBytes[3]).String()
protocolVersion = grpc.IPv4
log.Printf("[TRACE] ipv4 qual: %s/%d", ipAddrString, netmaskBits)
}
result.Value = &proto.QualValue_InetValue{
InetValue: &proto.Inet{
Mask: netmaskBits,
Addr: ipAddrString,
Cidr: fmt.Sprintf("%s/%d", ipAddrString, netmaskBits),
ProtocolVersion: protocolVersion,
},
}
case C.DATEOID:
pgts := int64(C.datumDate(datum, cinfo))
var timestamp *timestamp.Timestamp
timestamp, err := PgTimeToTimestamp(pgts)
if err != nil {
break
}
result.Value = &proto.QualValue_TimestampValue{TimestampValue: timestamp}
case C.TIMESTAMPOID, C.TIMESTAMPTZOID:
pgts := int64(C.datumTimestamp(datum, cinfo))
var timestamp *timestamp.Timestamp
timestamp, err := PgTimeToTimestamp(pgts)
if err != nil {
break
}
result.Value = &proto.QualValue_TimestampValue{TimestampValue: timestamp}
case C.INT2OID:
result.Value = &proto.QualValue_Int64Value{Int64Value: int64(C.datumInt16(datum, cinfo))}
case C.INT4OID:
result.Value = &proto.QualValue_Int64Value{Int64Value: int64(C.datumInt32(datum, cinfo))}
case C.INT8OID:
result.Value = &proto.QualValue_Int64Value{Int64Value: int64(C.datumInt64(datum, cinfo))}
case C.FLOAT4OID:
result.Value = &proto.QualValue_DoubleValue{DoubleValue: float64(C.datumFloat4(datum, cinfo))}
case C.FLOAT8OID:
result.Value = &proto.QualValue_DoubleValue{DoubleValue: float64(C.datumFloat8(datum, cinfo))}
case C.BOOLOID:
result.Value = &proto.QualValue_BoolValue{BoolValue: bool(C.datumBool(datum, cinfo))}
case C.JSONBOID:
var jsonbQualStr string
// handle zero value
if datum == 0 {
jsonbQualStr = "0"
} else {
jsonbQual := C.datumJsonb(datum, cinfo)
jsonbQualStr = C.GoString(C.JsonbToCStringIndent(nil, &(jsonbQual.root), -1))
}
result.Value = &proto.QualValue_JsonbValue{JsonbValue: jsonbQualStr}
default:
log.Printf("[INFO] datumToQualValue unknown typeoid %v ", typeOid)
result, err = convertUnknown(datum, typeOid, cinfo)
}
return
}
func convertUnknown(datum C.Datum, typeOid C.Oid, cinfo *C.ConversionInfo) (*proto.QualValue, error) {
tuple := C.fdw_searchSysCache1(C.TYPEOID, C.fdw_objectIdGetDatum(typeOid))
if !C.fdw_heapTupleIsValid(tuple) {
return nil, fmt.Errorf("lookup failed for type %v", typeOid)
}
typeStruct := (C.Form_pg_type)(unsafe.Pointer(C.fdw_getStruct(tuple)))
C.ReleaseSysCache(tuple)
if (typeStruct.typelem != 0) && (typeStruct.typlen == -1) {
log.Printf("[TRACE] datum is an array")
return datumArrayToQualValue(datum, typeOid, cinfo)
}
return nil, fmt.Errorf("Unknown qual type %v", typeOid)
}
func datumArrayToQualValue(datum C.Datum, typeOid C.Oid, cinfo *C.ConversionInfo) (*proto.QualValue, error) {
var result = &proto.QualValue{
Value: &proto.QualValue_ListValue{
ListValue: &proto.QualValueList{},
},
}
// NOTE: we have seen the occurrence of a zero datum with an array typeoid - explicitly check for this
if datum == 0 {
return result, nil
}
iterator := C.array_create_iterator(C.fdw_datumGetArrayTypeP(datum), 0, nil)
var qualValues []*proto.QualValue
var elem C.Datum
var isNull C.bool
for C.array_iterate(iterator, &elem, &isNull) {
if isNull == C.bool(true) {
log.Printf("[TRACE] datumArrayToQualValue: null qual value: %v", isNull)
log.Println(isNull)
qualValues = append(qualValues, nil)
continue
}
tuple := C.fdw_searchSysCache1(C.TYPEOID, C.fdw_objectIdGetDatum(typeOid))
if !C.fdw_heapTupleIsValid(tuple) {
return nil, fmt.Errorf("lookup failed for type %v", typeOid)
}
typeStruct := (C.Form_pg_type)(unsafe.Pointer(C.fdw_getStruct(tuple)))
C.ReleaseSysCache(tuple)
if qualValue, err := datumToQualValue(elem, typeStruct.typelem, cinfo); err != nil {
return nil, err
} else {
log.Printf("[INFO] datumArrayToQualValue: successfully converted qual value %v", qualValue)
if !qualValueArrayContainsValue(qualValues, qualValue) {
log.Printf("[INFO] adding qual value %v", qualValue)
qualValues = append(qualValues, qualValue)
} else {
log.Printf("[INFO] qual value array already contains an identical qual value %v", qualValue)
}
}
}
result.Value = &proto.QualValue_ListValue{
ListValue: &proto.QualValueList{
Values: qualValues,
},
}
log.Printf("[TRACE] datumArrayToQualValue complete, returning array of %d quals values \n", len(qualValues))
return result, nil
}
func qualValueArrayContainsValue(values []*proto.QualValue, newVal *proto.QualValue) bool {
for _, existingVal := range values {
if existingVal.Equals(newVal) {
return true
}
}
return false
}