Skip to content

Commit

Permalink
Sf/add filtermap cases (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-lfallasavendano committed Jan 8, 2024
1 parent 0d2e165 commit 84c5294
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
19 changes: 17 additions & 2 deletions src/Morphir/Snowpark/Backend.elm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import Morphir.Snowpark.MappingContext as MappingContext exposing (
import Morphir.Snowpark.MappingContext exposing (isRecordWithComplexTypes)
import Morphir.Snowpark.ReferenceUtils exposing (scalaPathToModule)
import Morphir.Snowpark.Utils exposing (collectMaybeList)
import Morphir.Snowpark.MappingContext exposing (isRecordWithSimpleTypes)
import Morphir.Snowpark.MappingContext exposing (isAnonymousRecordWithSimpleTypes)

type alias Options =
{}
Expand Down Expand Up @@ -279,8 +281,21 @@ mapRecordCreation : Type () -> Dict.Dict (Name.Name) (Value ta (Type ())) -> Val
mapRecordCreation tpe fields ctx =
if isTypeRefToRecordWithComplexTypes tpe ctx.typesContextInfo then
mapRecordCreationToCaseClassCreation tpe fields ctx
else
Scala.Literal (Scala.StringLit ("Record creation not converted"))
else
if (isTypeRefToRecordWithSimpleTypes tpe ctx.typesContextInfo ||
isAnonymousRecordWithSimpleTypes tpe ctx.typesContextInfo) &&
isFunctionClassificationReturningDataFrameExpressions ctx.currentFunctionClassification then
MappingContext.getFieldInfoIfRecordType tpe ctx.typesContextInfo
|> Maybe.map (\fieldInfo -> collectMaybeList
((\(fieldName, _) ->
(Dict.get fieldName fields)
|> Maybe.map (\argExpr -> mapValue argExpr ctx)))
fieldInfo )
|> Maybe.withDefault Nothing
|> Maybe.map (applySnowparkFunc "array_construct")
|> Maybe.withDefault (Scala.Literal (Scala.StringLit ("Record creation not converted1")))
else
Scala.Literal (Scala.StringLit ("Record creation not converted2"))


mapRecordCreationToCaseClassCreation : Type () -> Dict.Dict (Name.Name) (Value ta (Type ())) -> ValueMappingContext -> Scala.Value
Expand Down
28 changes: 26 additions & 2 deletions src/Morphir/Snowpark/MapFunctionsMapping.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import Morphir.IR.Value as Value
import Morphir.Snowpark.AggregateMapping as AggregateMapping
import Morphir.Snowpark.Constants as Constants exposing (applySnowparkFunc)
import Morphir.Snowpark.Operatorsmaps exposing (mapOperator)
import Morphir.Snowpark.ReferenceUtils exposing (isTypeReferenceToSimpleTypesRecord, scalaPathToModule, getCustomTypeParameterFieldAccess)
import Morphir.Snowpark.ReferenceUtils exposing (isTypeReferenceToSimpleTypesRecord
, scalaPathToModule
, getCustomTypeParameterFieldAccess)
import Morphir.Snowpark.TypeRefMapping exposing (generateRecordTypeWrapperExpression, generateCastIfPossible)
import Morphir.Snowpark.MappingContext exposing (
ValueMappingContext
Expand All @@ -23,9 +25,11 @@ import Morphir.Snowpark.MappingContext exposing (
, isLocalFunctionName
, getFieldInfoIfRecordType
, addReplacementForIdentifier
, getLocalVariableIfDataFrameReference
, isFunctionReturningDataFrameExpressions)
import Morphir.Snowpark.Utils exposing (collectMaybeList)
import Morphir.Snowpark.MappingContext exposing (isRecordWithSimpleTypes)
import Morphir.Snowpark.ReferenceUtils exposing (getListTypeParameter)

mapFunctionsMapping : ValueIR.Value ta (TypeIR.Type ()) -> Constants.MapValueType ta -> ValueMappingContext -> Scala.Value
mapFunctionsMapping value mapValue ctx =
Expand Down Expand Up @@ -365,7 +369,27 @@ generateForListFilterMap predicate sourceRelation ctx mapValue =
in
Maybe.withDefault filterCall (generateProjectionForArrayColumnIfRequired tpe ctx filterCall "result")
_ ->
Scala.Literal (Scala.StringLit "Unsupported filterMap scenario")
let
tpe = Value.valueAttribute predicate
recordReference = getListTypeParameter (valueAttribute sourceRelation)
|> Maybe.map (\t -> (getLocalVariableIfDataFrameReference t ctx))
|> Maybe.withDefault Nothing
|> Maybe.map Scala.Variable
|> Maybe.withDefault (Scala.Literal Scala.NullLit)
predicateApplication =
Scala.Apply (mapValue predicate ctx) [Scala.ArgValue Nothing recordReference]
selectColumnAlias =
Scala.Apply (Scala.Select predicateApplication "as ") [ Scala.ArgValue Nothing resultId ]
selectCall =
Scala.Apply (Scala.Select (mapValue sourceRelation ctx) "select") [Scala.ArgValue Nothing <| selectColumnAlias]
resultId =
Scala.Literal <| Scala.StringLit "result"
isNotNullCall =
Scala.Select (Constants.applySnowparkFunc "col" [ resultId ]) "is_not_null"
filterCall =
Scala.Apply (Scala.Select selectCall "filter") [Scala.ArgValue Nothing isNotNullCall]
in
Maybe.withDefault filterCall (generateProjectionForArrayColumnIfRequired tpe ctx filterCall "result")
else
Scala.Literal (Scala.StringLit "Unsupported filterMap scenario")

Expand Down
7 changes: 5 additions & 2 deletions src/Morphir/Snowpark/MappingContext.elm
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ getFieldInfoIfRecordType tpe ctx =
Just fieldInfo
_ ->
Nothing
Record _ fields ->
Just <| List.map (\field -> (field.name, field.tpe) ) fields
_ ->
Nothing

Expand Down Expand Up @@ -400,10 +402,11 @@ processFunctionDefinition definition ctx =
, (\_ ->
if List.all (\tpe -> (isDataFrameFriendlyType tpe ctx) ||
(isTypeRefToRecordWithSimpleTypes tpe ctx) ||
(typeRefIsMaybeOf tpe (\t -> isTypeRefToRecordWithSimpleTypes t ctx)))
(isTypeRefToRecordWithSimpleTypes tpe ctx) ||
(typeRefIsMaybeOf tpe (\t -> (isTypeRefToRecordWithSimpleTypes t ctx) )))
inputTypes &&
((isDataFrameFriendlyType definition.outputType ctx) ||
(typeRefIsMaybeOf definition.outputType (\t -> isTypeRefToRecordWithSimpleTypes t ctx)))
(typeRefIsMaybeOf definition.outputType (\t -> (isTypeRefToRecordWithSimpleTypes t ctx) || (isAnonymousRecordWithSimpleTypes t ctx))))
then
Just FromDfValuesToDfValues
else Nothing)
Expand Down
11 changes: 10 additions & 1 deletion src/Morphir/Snowpark/ReferenceUtils.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Morphir.Snowpark.ReferenceUtils exposing (
, isValueReferenceToSimpleTypesRecord
, mapLiteral
, scalaReferenceToUnionTypeCase
, getCustomTypeParameterFieldAccess)
, getCustomTypeParameterFieldAccess
, getListTypeParameter)

import Morphir.IR.Name as Name
import Morphir.IR.Type as IrType
Expand Down Expand Up @@ -70,3 +71,11 @@ scalaReferenceToUnionTypeCase typeName constructorName =
getCustomTypeParameterFieldAccess : Int -> String
getCustomTypeParameterFieldAccess paramIndex =
"field" ++ (String.fromInt paramIndex)

getListTypeParameter : IrType.Type () -> Maybe (IrType.Type ())
getListTypeParameter tpe =
case tpe of
IrType.Reference _ ([ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "list" ] ], [ "list" ]) [innertype] ->
Just innertype
_ ->
Nothing

0 comments on commit 84c5294

Please sign in to comment.