Skip to content

Commit

Permalink
🐛 Fix tree nodes ordering, makes query checksums deterministic.
Browse files Browse the repository at this point in the history
  • Loading branch information
preslavgerchev committed Sep 19, 2023
1 parent 36c54c1 commit fcee83d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 7 additions & 1 deletion mqlc/mqlc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,13 @@ func (c *compiler) addValueFieldChunks(ref uint64) {
return
}
path = append(path, node.id)
for _, child := range node.children {
keys := []string{}
for k := range node.children {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
child := node.children[k]
visit(tree, child, path)
visitTreeNodes(tree, child, path, visit)
}
Expand Down
22 changes: 17 additions & 5 deletions mqlc/mqlc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
)

var (
features = cnquery.Features{}
os_schema = providers.MustLoadSchemaFromFile("os", "../providers/os/resources/os.resources.json")
core_schema = providers.MustLoadSchemaFromFile("core", "../providers/core/resources/core.resources.json")
conf = mqlc.NewConfig(
core_schema.Add(os_schema),
features = cnquery.Features{}
os_schema = providers.MustLoadSchemaFromFile("os", "../providers/os/resources/os.resources.json")
core_schema = providers.MustLoadSchemaFromFile("core", "../providers/core/resources/core.resources.json")
azure_schema = providers.MustLoadSchemaFromFile("azure", "../providers/azure/resources/azure.resources.json")
conf = mqlc.NewConfig(
core_schema.Add(os_schema).Add(azure_schema),
features,
)
)
Expand Down Expand Up @@ -235,6 +236,17 @@ func TestCompiler_Semicolon(t *testing.T) {
})
}

func TestCompiler_DeterministicChecksum(t *testing.T) {
for i := 0; i < 10_000; i++ {
// this is a query that in the past used to produce different checksum every now and then
// this test ensures that the checksum is always deterministic now
m := `azure.subscription.sql.servers.all(databases.one (transparentDataEncryption["state"] == "Enabled") && encryptionProtector["serverKeyType"] == "AzureKeyVault" )`
compileT(t, m, func(res *llx.CodeBundle) {
require.Equal(t, res.CodeV2.Id, "LkB8PP3xB2Q=", i)
})
}
}

func TestCompiler_Simple(t *testing.T) {
data := []struct {
code string
Expand Down

0 comments on commit fcee83d

Please sign in to comment.