Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Carry over hash value for all literal types in remote caching (#378)
Browse files Browse the repository at this point in the history
Previously, the hash set on an input literal would be ignored if
the literal was of either the `Collection` or `Map` type. This is
fixed here by moving the hash-checking branch up in front of the
special type logic, so that an empty literal with hash is constructed
in either case if a hash is found.

Adds a regression test to verify the hashes are carried over into the
representer literals for both map and collection types.

Signed-off-by: Nicholas Junge <[email protected]>
  • Loading branch information
nicholasjng committed Jul 18, 2023
1 parent 4e5ff42 commit 13ef7d6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
15 changes: 8 additions & 7 deletions go/tasks/pluginmachinery/catalog/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ var emptyLiteralMap = core.LiteralMap{Literals: map[string]*core.Literal{}}
// Hashify a literal, in other words, produce a new literal where the corresponding value is removed in case
// the literal hash is set.
func hashify(literal *core.Literal) *core.Literal {
// If the hash is set, return an empty literal with the same hash,
// regardless of type (scalar/collection/map).
if literal.GetHash() != "" {
return &core.Literal{
Hash: literal.GetHash(),
}
}

// Two recursive cases:
// 1. A collection of literals or
// 2. A map of literals

if literal.GetCollection() != nil {
literals := literal.GetCollection().Literals
literalsHash := make([]*core.Literal, 0)
Expand Down Expand Up @@ -45,12 +52,6 @@ func hashify(literal *core.Literal) *core.Literal {
}
}

// And a base case that consists of a scalar, where the hash might be set
if literal.GetHash() != "" {
return &core.Literal{
Hash: literal.GetHash(),
}
}
return literal
}

Expand Down
58 changes: 58 additions & 0 deletions go/tasks/pluginmachinery/catalog/hashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,64 @@ func TestHashLiteralMap_LiteralsWithHashSet(t *testing.T) {
},
},
},
{
name: "literal map containing hash",
literal: &core.Literal{
Value: &core.Literal_Map{
Map: &core.LiteralMap{
Literals: map[string]*core.Literal{
"hello": {
Value: &core.Literal_Scalar{
Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{
Primitive: &core.Primitive{
Value: &core.Primitive_StringValue{
StringValue: "world",
},
},
},
},
},
},
},
},
},
Hash: "0xffff",
},
expectedLiteral: &core.Literal{
Value: nil,
Hash: "0xffff",
},
},
{
name: "literal collection containing hash",
literal: &core.Literal{
Value: &core.Literal_Collection{
Collection: &core.LiteralCollection{
Literals: []*core.Literal{
{
Value: &core.Literal_Scalar{
Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{
Primitive: &core.Primitive{
Value: &core.Primitive_Integer{
Integer: 42,
},
},
},
},
},
},
},
},
},
Hash: "0xabcdef",
},
expectedLiteral: &core.Literal{
Value: nil,
Hash: "0xabcdef",
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 13ef7d6

Please sign in to comment.