Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to support nested all queries #4360

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 57 additions & 25 deletions mqlc/mqlc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ func (c *compiler) compileExpressions(expressions []*parser.Expression) error {

func (c *compiler) postCompile() {
code := c.Result.CodeV2
for i := range code.Blocks {
for i := len(code.Blocks) - 1; i >= 0; i-- {
block := code.Blocks[i]
eps := block.Entrypoints

Expand Down Expand Up @@ -1718,7 +1718,16 @@ func (c *compiler) addValueFieldChunks(ref uint64) {
id string
chunk *llx.Chunk
chunkIdx int
children map[string]*fieldTreeNode
children []*fieldTreeNode
}

fieldTreeNodeChild := func(f *fieldTreeNode, id string) *fieldTreeNode {
for _, c := range f.children {
if c.id == id {
return c
}
}
return nil
}

type fieldTree struct {
Expand All @@ -1738,14 +1747,15 @@ func (c *compiler) addValueFieldChunks(ref uint64) {
id: chunk.Id,
chunk: chunk,
chunkIdx: i + 1,
children: map[string]*fieldTreeNode{},
children: []*fieldTreeNode{},
}

if chunk.Function != nil && chunk.Function.Binding != 0 {
chunkIdx := llx.ChunkIndex(chunk.Function.Binding)
parent := nodes[chunkIdx-1]
if parent != nil {
nodes[chunkIdx-1].children[chunk.Id] = nodes[i]
n := nodes[chunkIdx-1]
n.children = append(n.children, nodes[i])
}
}
}
Expand All @@ -1755,7 +1765,7 @@ func (c *compiler) addValueFieldChunks(ref uint64) {
}
}

addToTree := func(tree *fieldTree, parentPath []string, blockRef uint64, block *llx.Block, chunk *llx.Chunk) bool {
addToTree := func(tree *fieldTree, parentPath []string, blockRef uint64, block *llx.Block, chunk *llx.Chunk, remaps map[uint32]uint32) (uint32, bool) {
// add a chunk to the tree. If the path already exists, do nothing
// return true if the chunk was added, false if it already existed
if len(tree.nodes) != len(block.Chunks) {
Expand All @@ -1764,37 +1774,57 @@ func (c *compiler) addValueFieldChunks(ref uint64) {

parent := tree.nodes[0]
for _, id := range parentPath[1:] {
child := parent.children[id]
child := fieldTreeNodeChild(parent, id)
parent = child
}

if parent.children[chunk.Id] != nil {
return false
if c := fieldTreeNodeChild(parent, chunk.Id); c != nil {
return 0, false
}

newChunk := chunk
if chunk.Function != nil {
var args []*llx.Primitive
if len(chunk.Function.Args) > 0 {
args = make([]*llx.Primitive, len(chunk.Function.Args))
copy(args, chunk.Function.Args)
}
newChunk = &llx.Chunk{
Call: chunk.Call,
Id: chunk.Id,
Function: &llx.Function{
Binding: (blockRef & 0xFFFFFFFF00000000) | uint64(parent.chunkIdx),
Type: chunk.Function.Type,
Args: chunk.Function.Args,
Args: args,
},
}
if chunk.Id == "$whereNot" || chunk.Id == "where" {
argRef := chunk.Function.Args[0].RawData().Value.(uint64)
if (chunk.Function.Binding & 0xFFFFFFFF00000000) != (argRef & 0xFFFFFFFF00000000) {
// If they're in different blocks, we don't try to do anything
return 0, false
}
oldChunkIdx := llx.ChunkIndex(argRef)
newChunkIdx, ok := remaps[oldChunkIdx]
if !ok {
return 0, false
}
newChunk.Function.Args[0] = llx.RefPrimitiveV2((blockRef & 0xFFFFFFFF00000000) | uint64(newChunkIdx))
}
}

parent.children[chunk.Id] = &fieldTreeNode{
chunkIdx := len(tree.nodes) + 1
n := &fieldTreeNode{
id: chunk.Id,
chunk: newChunk,
chunkIdx: len(tree.nodes) + 1,
children: map[string]*fieldTreeNode{},
chunkIdx: chunkIdx,
children: []*fieldTreeNode{},
}
tree.nodes = append(tree.nodes, parent.children[chunk.Id])
parent.children = append(parent.children, n)
tree.nodes = append(tree.nodes, n)
block.AddChunk(c.Result.CodeV2, blockRef, newChunk)

return true
return uint32(chunkIdx), true
}

var visitTreeNodes func(tree *fieldTree, node *fieldTreeNode, path []string, visit func(tree *fieldTree, node *fieldTreeNode, path []string))
Expand All @@ -1803,13 +1833,7 @@ func (c *compiler) addValueFieldChunks(ref uint64) {
return
}
path = append(path, node.id)
keys := []string{}
for k := range node.children {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
child := node.children[k]
for _, child := range node.children {
visit(tree, child, path)
visitTreeNodes(tree, child, path, visit)
}
Expand All @@ -1829,8 +1853,11 @@ func (c *compiler) addValueFieldChunks(ref uint64) {
}
}
assessmentBlockTree := blockToFieldTree(assessmentBlock, func(chunkIdx int, chunk *llx.Chunk) bool {
if chunk.Id == "$whereNot" || chunk.Id == "where" {
if chunk.Id == "$all" || chunk.Id == "$one" || chunk.Id == "$none" || chunk.Id == "$any" {
return false
} else if chunk.Id == "$whereNot" || chunk.Id == "where" {
// There is some bug with list that makes this not work. This check filters those out
return chunk.Function.Binding == chunk.Function.Args[0].RawData().Value.(uint64)
} else if _, compareable := llx.ComparableLabel(chunk.Id); compareable {
return false
} else if chunk.Function != nil && len(chunk.Function.Args) > 0 {
Expand All @@ -1852,11 +1879,16 @@ func (c *compiler) addValueFieldChunks(ref uint64) {
return true
})

remaps := map[uint32]uint32{}
visitTreeNodes(&assessmentBlockTree, assessmentBlockTree.nodes[0], make([]string, 0, 16), func(tree *fieldTree, node *fieldTreeNode, path []string) {
// add the node to the assessment block tree
chunkAdded := addToTree(&defaultFieldsBlockTree, path, defaultFieldsRef, defaultFieldsBlock, node.chunk)
if chunkAdded && node.chunk.Function != nil {
defaultFieldsBlock.Entrypoints = append(defaultFieldsBlock.Entrypoints, (defaultFieldsRef&0xFFFFFFFF00000000)|uint64(len(defaultFieldsBlock.Chunks)))
chunkIdx, chunkAdded := addToTree(&defaultFieldsBlockTree, path, defaultFieldsRef, defaultFieldsBlock, node.chunk, remaps)

if chunkAdded {
remaps[uint32(node.chunkIdx)] = chunkIdx
if node.chunk.Function != nil && len(node.children) == 0 {
defaultFieldsBlock.Entrypoints = append(defaultFieldsBlock.Entrypoints, (defaultFieldsRef&0xFFFFFFFF00000000)|uint64(len(defaultFieldsBlock.Chunks)))
}
}
})
}
Expand Down
33 changes: 32 additions & 1 deletion mqlc/mqlc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func TestCompiler_DeterministicChecksum(t *testing.T) {
azureConf := mqlc.NewConfig(azure_schema, features)
res, err := mqlc.Compile(mql, map[string]*llx.Primitive{}, azureConf)
require.Nil(t, err)
require.Equal(t, res.CodeV2.Id, "h81H/YfoIRI=")
require.Equal(t, res.CodeV2.Id, "myGJk5hUrsM=")
}
}

Expand Down Expand Up @@ -1023,6 +1023,37 @@ func TestCompiler_ArrayAll(t *testing.T) {
})
}

func TestCompiler_All_Issue919(t *testing.T) {
// https://github.com/mondoohq/cnquery/issues/919
compileT(t, `files.find(from: ".", type: "file").all( permissions.other_readable == false )`, func(res *llx.CodeBundle) {
require.Equal(t, 3, len(res.CodeV2.Blocks))
require.Equal(t, 6, len(res.CodeV2.Blocks[2].Chunks))
assertPrimitive(t, &llx.Primitive{
Type: string(types.Resource("file")),
}, res.CodeV2.Blocks[2].Chunks[0])
assertFunction(t, "path", &llx.Function{
Type: string(types.String),
Binding: (3 << 32) | 1,
}, res.CodeV2.Blocks[2].Chunks[1])
assertFunction(t, "size", &llx.Function{
Type: string(types.Int),
Binding: (3 << 32) | 1,
}, res.CodeV2.Blocks[2].Chunks[2])
assertFunction(t, "permissions", &llx.Function{
Type: string(types.Resource("file.permissions")),
Binding: (3 << 32) | 1,
}, res.CodeV2.Blocks[2].Chunks[3])
assertFunction(t, "string", &llx.Function{
Type: string(types.String),
Binding: (3 << 32) | 4,
}, res.CodeV2.Blocks[2].Chunks[4])
assertFunction(t, "other_readable", &llx.Function{
Type: string(types.Bool),
Binding: (3 << 32) | 4,
}, res.CodeV2.Blocks[2].Chunks[5])
})
}

func TestCompiler_All_Issue1316(t *testing.T) {
// https://github.com/mondoohq/cnquery/issues/1316
compileT(t, `files.find(from: ".", type: "file").all( permissions.other_readable == false )`, func(res *llx.CodeBundle) {
Expand Down
Loading