-
Notifications
You must be signed in to change notification settings - Fork 59
[flytepropeller] Support attribute access on promises #615
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,26 @@ func validateBinding(w c.WorkflowBuilder, nodeID c.NodeID, nodeParam string, bin | |
} | ||
} | ||
|
||
// If the type is a struct (e.g. dataclass) and the attribute path is longer than 0, | ||
// We skip the type check and let it fail at runtime because we don't know the type of struct field | ||
if sourceType.GetSimple() == flyte.SimpleType_STRUCT && len(val.Promise.AttrPath) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wild-endeavor is this necessarily true. if we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ByronHsu would you mind just trying to fill this in a bit? If it's really not doable, or if it has a bunch of edge cases, I think it's okay. Walking through some examples. Let's say the simple case of @task
def t1() -> List[str]:
...
@task
def t2(needs_str: str):
...
@workflow
def wf():
res = t1()
t2(needs_str=res[5]) In this case
and because it's a string it will type check against the string in The dataclass case is more complicated @dataclass_json
@dataclass
class MyDC(object):
snapshotDate: datetime
region: str
@task
def t1(needs_dt: datetime):
...
@task
def t2(needs_str: str):
...
@workflow
def wf(a: MyDC):
t1(needs_dt=a.snapshotDate)
t2(needs_str=a.region) The reason it's more complicated is because the dataclass types are completely obscured (esp. since flyte idl currently doesn't support multi-variate map types). I assume this is why you're skipping checking in the simple/struct case. Could you see if it's possible though to capture it? Can we
What do you think? It will add to the correctness of this new feature. And it will make Dan happy. And in the end, that's what we're all really about. |
||
return param.GetType(), []c.NodeID{val.Promise.NodeId}, true | ||
} | ||
|
||
// If the variable has an attribute path. Extract the type of the last attribute. | ||
for range val.Promise.AttrPath { | ||
if sourceType.GetCollectionType() != nil { | ||
sourceType = sourceType.GetCollectionType() | ||
} | ||
if sourceType.GetMapValueType() != nil { | ||
sourceType = sourceType.GetMapValueType() | ||
} | ||
// If the current type is struct, skip the type check because we don't know the type of struct field | ||
if sourceType.GetSimple() == flyte.SimpleType_STRUCT { | ||
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 | ||
|
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 | ||
} |
There was a problem hiding this comment.
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?