Skip to content

Commit cf253ab

Browse files
authored
Bugfix: Adding stored query to array should expand the stored query. (#122)
1 parent 648369d commit cf253ab

3 files changed

Lines changed: 104 additions & 4 deletions

File tree

fixtures/multi_vql_queries.golden

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,5 +1338,83 @@
13381338
"StructValue.src_ip": "127.0.0.1",
13391339
"StructValue.SrcIp": null
13401340
}
1341+
],
1342+
"088/000 Adding Stored Query: LET X = SELECT value AS Foo FROM range(start=1, end=4)": null,
1343+
"088/001 Adding Stored Query: SELECT (dict(Foo=12), ) + X, X + (dict(Foo=12), ) FROM scope()": [
1344+
{
1345+
"(dict(Foo=12), ) + X": [
1346+
{
1347+
"Foo": 12
1348+
},
1349+
{
1350+
"Foo": 1
1351+
},
1352+
{
1353+
"Foo": 2
1354+
},
1355+
{
1356+
"Foo": 3
1357+
},
1358+
{
1359+
"Foo": 4
1360+
}
1361+
],
1362+
"X + (dict(Foo=12), )": [
1363+
{
1364+
"Foo": 1
1365+
},
1366+
{
1367+
"Foo": 2
1368+
},
1369+
{
1370+
"Foo": 3
1371+
},
1372+
{
1373+
"Foo": 4
1374+
},
1375+
{
1376+
"Foo": 12
1377+
}
1378+
]
1379+
}
1380+
],
1381+
"089/000 Adding Materialized Stored Query: LET X \u003c= SELECT value AS Foo FROM range(start=1, end=4)": null,
1382+
"089/001 Adding Materialized Stored Query: SELECT (dict(Foo=12), ) + X, X + (dict(Foo=12), ) FROM scope()": [
1383+
{
1384+
"(dict(Foo=12), ) + X": [
1385+
{
1386+
"Foo": 12
1387+
},
1388+
{
1389+
"Foo": 1
1390+
},
1391+
{
1392+
"Foo": 2
1393+
},
1394+
{
1395+
"Foo": 3
1396+
},
1397+
{
1398+
"Foo": 4
1399+
}
1400+
],
1401+
"X + (dict(Foo=12), )": [
1402+
{
1403+
"Foo": 1
1404+
},
1405+
{
1406+
"Foo": 2
1407+
},
1408+
{
1409+
"Foo": 3
1410+
},
1411+
{
1412+
"Foo": 4
1413+
},
1414+
{
1415+
"Foo": 12
1416+
}
1417+
]
1418+
}
13411419
]
13421420
}

protocols/protocol_add.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package protocols
22

33
import (
4+
"context"
45
"reflect"
56

67
"www.velocidex.com/golang/vfilter/types"
@@ -94,8 +95,8 @@ func (self AddDispatcher) Add(scope types.Scope, a types.Any, b types.Any) types
9495

9596
// Handle array concatenation
9697
if is_array(a) || is_array(b) {
97-
a_slice := convertToSlice(a)
98-
b_slice := convertToSlice(b)
98+
a_slice := convertToSlice(scope, a)
99+
b_slice := convertToSlice(scope, b)
99100

100101
return append(a_slice, b_slice...)
101102
}
@@ -111,7 +112,7 @@ func (self *AddDispatcher) AddImpl(elements ...AddProtocol) {
111112
}
112113
}
113114

114-
func convertToSlice(a types.Any) []types.Any {
115+
func convertToSlice(scope types.Scope, a types.Any) []types.Any {
115116
if is_array(a) {
116117
a_slice := reflect.ValueOf(a)
117118
result := make([]types.Any, 0, a_slice.Len())
@@ -120,5 +121,15 @@ func convertToSlice(a types.Any) []types.Any {
120121
}
121122
return result
122123
}
124+
125+
stored_query, ok := a.(types.StoredQuery)
126+
if ok {
127+
var res []types.Any
128+
for _, x := range MaterializeToArray(context.Background(), scope, stored_query) {
129+
res = append(res, x)
130+
}
131+
return res
132+
}
133+
123134
return []types.Any{a}
124135
}

vfilter_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,17 @@ FROM scope()
13341334
{"Test struct associative", `
13351335
SELECT StructValue.SrcIP, StructValue.src_ip, StructValue.SrcIp
13361336
FROM scope()`},
1337+
1338+
{"Adding Stored Query", `
1339+
LET X = SELECT value AS Foo FROM range(start=1, end=4)
1340+
1341+
SELECT (dict(Foo=12), ) + X, X + (dict(Foo=12),) FROM scope()
1342+
`},
1343+
{"Adding Materialized Stored Query", `
1344+
LET X <= SELECT value AS Foo FROM range(start=1, end=4)
1345+
1346+
SELECT (dict(Foo=12), ) + X, X + (dict(Foo=12),) FROM scope()
1347+
`},
13371348
}
13381349

13391350
type _RangeArgs struct {
@@ -1533,7 +1544,7 @@ func TestMultiVQLQueries(t *testing.T) {
15331544
// Store the result in ordered dict so we have a consistent golden file.
15341545
result := ordereddict.NewDict()
15351546
for i, testCase := range multiVQLTest {
1536-
if false && i != 85 {
1547+
if false && i != 88 {
15371548
continue
15381549
}
15391550
scope := makeTestScope()

0 commit comments

Comments
 (0)