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

[flytepropeller] Support attribute access on promises #615

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,4 @@ require (
)

replace github.com/aws/amazon-sagemaker-operator-for-k8s => github.com/aws/amazon-sagemaker-operator-for-k8s v1.0.1-0.20210303003444-0fb33b1fd49d
replace github.com/flyteorg/flyteidl => ../flyteidl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ByronHsu can you update this?

6 changes: 6 additions & 0 deletions pkg/compiler/validators/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func validateBinding(w c.WorkflowBuilder, nodeID c.NodeID, nodeParam string, bin
}
}

// Skip the validation if the promise has attribute paths
// because we don't know the type of the resolved attribute
if len(val.Promise.AttrPath) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should be able to know the attribute type if the promise type is list or dict, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the list contains nested union type?

For example, in type Dict[str, Union[str, Union[str, int]]], we pass x["a"], but we don't know if "a" mapped to str or Union[str, int]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to use the AreTypesCastable function here right? It should handle Union as either a source or destination type. IMO this feature needs type validation, it's something Flyte is very opinionated about and seems very sloppy to leave out.

return param.GetType(), []c.NodeID{val.Promise.NodeId}, true
}

if !validateParamTypes || AreTypesCastable(sourceType, expectedType) {
val.Promise.NodeId = upNode.GetId()
return param.GetType(), []c.NodeID{val.Promise.NodeId}, true
Expand Down
156 changes: 156 additions & 0 deletions pkg/controller/nodes/attr_path_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package nodes

import (
"context"

"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/errors"
"google.golang.org/protobuf/types/known/structpb"
)

// resolveAttrPathInPromise resolves the literal with attribute path
// If the promise is chained with attributes (e.g. promise.a["b"][0]), then we need to resolve the promise
func resolveAttrPathInPromise(ctx context.Context, nodeID string, literal *core.Literal, bindAttrPath []*core.PromiseAttribute) (*core.Literal, error) {
var currVal *core.Literal = literal
var tmpVal *core.Literal
var err error
var exist bool
count := 0

for _, attr := range bindAttrPath {
switch currVal.GetValue().(type) {
case *core.Literal_Map:
tmpVal, exist = currVal.GetMap().GetLiterals()[attr.GetStringValue()]
if exist == false {
return nil, errors.Errorf(errors.PromiseAttributeResolveError, nodeID, "key [%v] does not exist in literal %v", attr.GetStringValue(), currVal.GetMap().GetLiterals())
}
currVal = tmpVal
count += 1
case *core.Literal_Collection:
if int(attr.GetIntValue()) >= len(currVal.GetCollection().GetLiterals()) {
return nil, errors.Errorf(errors.PromiseAttributeResolveError, nodeID, "index [%v] is out of range of %v", attr.GetIntValue(), currVal.GetCollection().GetLiterals())
}
currVal = currVal.GetCollection().GetLiterals()[attr.GetIntValue()]
count += 1
// scalar is always the leaf, so we can break here
case *core.Literal_Scalar:
break
}
}

// resolve dataclass
if currVal.GetScalar() != nil && currVal.GetScalar().GetGeneric() != nil {
st := currVal.GetScalar().GetGeneric()
// start from index "count"
currVal, err = resolveAttrPathInPbStruct(ctx, nodeID, st, bindAttrPath[count:])
if err != nil {
return nil, err
}
}

return currVal, nil
}

// resolveAttrPathInPbStruct resolves the protobuf struct (e.g. dataclass) with attribute path
func resolveAttrPathInPbStruct(ctx context.Context, nodeID string, st *structpb.Struct, bindAttrPath []*core.PromiseAttribute) (*core.Literal, error) {
ByronHsu marked this conversation as resolved.
Show resolved Hide resolved

var currVal interface{}
var tmpVal interface{}
var exist bool

currVal = st.AsMap()

// Turn the current value to a map so it can be resolved more easily
for _, attr := range bindAttrPath {
switch currVal.(type) {
ByronHsu marked this conversation as resolved.
Show resolved Hide resolved
// map
case map[string]interface{}:
tmpVal, exist = currVal.(map[string]interface{})[attr.GetStringValue()]
if exist == false {
return nil, errors.Errorf(errors.PromiseAttributeResolveError, nodeID, "key [%v] does not exist in literal %v", attr.GetStringValue(), currVal)
}
currVal = tmpVal
// list
case []interface{}:
if int(attr.GetIntValue()) >= len(currVal.([]interface{})) {
return nil, errors.Errorf(errors.PromiseAttributeResolveError, nodeID, "index [%v] is out of range of %v", attr.GetIntValue(), currVal)
}
currVal = currVal.([]interface{})[attr.GetIntValue()]
}
}

// After resolve, convert the interface to literal
literal, err := convertInterfaceToLiteral(ctx, nodeID, currVal)

return literal, err
}

// convertInterfaceToLiteral converts the protobuf struct (e.g. dataclass) to literal
func convertInterfaceToLiteral(ctx context.Context, nodeID string, obj interface{}) (*core.Literal, error) {

literal := &core.Literal{}

switch obj.(type) {
case map[string]interface{}:
new_st, err := structpb.NewStruct(obj.(map[string]interface{}))
if err != nil {
return nil, err
}
literal.Value = &core.Literal_Scalar{
Scalar: &core.Scalar{
Value: &core.Scalar_Generic{
Generic: new_st,
},
},
}
case []interface{}:
literals := []*core.Literal{}
for _, v := range obj.([]interface{}) {
// recursively convert the interface to literal
literal, err := convertInterfaceToLiteral(ctx, nodeID, v)
if err != nil {
return nil, err
}
literals = append(literals, literal)
}
literal.Value = &core.Literal_Collection{
Collection: &core.LiteralCollection{
Literals: literals,
},
}
case interface{}:
scalar, err := convertInterfaceToLiteralScalar(ctx, nodeID, obj)
if err != nil {
return nil, err
}
literal.Value = scalar
}

return literal, nil
}

// convertInterfaceToLiteralScalar converts the a single value to a literal scalar
func convertInterfaceToLiteralScalar(ctx context.Context, nodeID string, obj interface{}) (*core.Literal_Scalar, error) {
value := &core.Primitive{}

switch obj.(type) {
case string:
value.Value = &core.Primitive_StringValue{StringValue: obj.(string)}
case int:
value.Value = &core.Primitive_Integer{Integer: int64(obj.(int))}
case float64:
value.Value = &core.Primitive_FloatValue{FloatValue: obj.(float64)}
case bool:
ByronHsu marked this conversation as resolved.
Show resolved Hide resolved
value.Value = &core.Primitive_Boolean{Boolean: obj.(bool)}
default:
return nil, errors.Errorf(errors.PromiseAttributeResolveError, nodeID, "Failed to resolve interface to literal scalar")
}

return &core.Literal_Scalar{
Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{
Primitive: value,
},
},
}, nil
}
Loading
Loading