Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve UDF handling in delegation strategy #2699

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System.Collections.Generic;
using System.Globalization;
using System.Security.Authentication.ExtendedProtection;
using Microsoft.PowerFx.Core.Binding;
using Microsoft.PowerFx.Core.Binding.BindInfo;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.Errors;
using Microsoft.PowerFx.Core.Functions.Delegation.DelegationMetadata;
using Microsoft.PowerFx.Core.Localization;
using Microsoft.PowerFx.Core.Logging.Trackers;
using Microsoft.PowerFx.Core.Texl.Builtins;
Expand All @@ -18,17 +20,17 @@ namespace Microsoft.PowerFx.Core.Functions.Delegation.DelegationStrategies
{
internal interface ICallNodeDelegatableNodeValidationStrategy
{
bool IsValidCallNode(CallNode node, TexlBinding binding, OperationCapabilityMetadata metadata, TexlFunction trackingFunction = null);
bool IsValidCallNode(CallNode node, TexlBinding binding, OperationCapabilityMetadata metadata, TexlFunction trackingFunction = null, bool nodeInheritsRowScope = false);
}

internal interface IDottedNameNodeDelegatableNodeValidationStrategy
{
bool IsValidDottedNameNode(DottedNameNode node, TexlBinding binding, OperationCapabilityMetadata metadata, IOpDelegationStrategy opDelStrategy);
bool IsValidDottedNameNode(DottedNameNode node, TexlBinding binding, OperationCapabilityMetadata metadata, IOpDelegationStrategy opDelStrategy, bool nodeInheritsRowScope = false);
rick-nguyen marked this conversation as resolved.
Show resolved Hide resolved
}

internal interface IFirstNameNodeDelegatableNodeValidationStrategy
{
bool IsValidFirstNameNode(FirstNameNode node, TexlBinding binding, IOpDelegationStrategy opDelStrategy);
bool IsValidFirstNameNode(FirstNameNode node, TexlBinding binding, IOpDelegationStrategy opDelStrategy, bool nodeInheritsRowScope = false);
}

internal class DelegationValidationStrategy
Expand Down Expand Up @@ -175,13 +177,13 @@ private OperationCapabilityMetadata GetScopedOperationCapabilityMetadata(IDelega
return delegationMetadata.FilterDelegationMetadata;
}

public bool IsValidDottedNameNode(DottedNameNode node, TexlBinding binding, OperationCapabilityMetadata metadata, IOpDelegationStrategy opDelStrategy)
public bool IsValidDottedNameNode(DottedNameNode node, TexlBinding binding, OperationCapabilityMetadata metadata, IOpDelegationStrategy opDelStrategy, bool nodeInheritsRowScope = false)
{
Contracts.AssertValue(node);
Contracts.AssertValue(binding);
Contracts.AssertValueOrNull(opDelStrategy);

var isRowScoped = binding.IsRowScope(node);
var isRowScoped = binding.IsRowScope(node) || nodeInheritsRowScope;
if (!isRowScoped)
{
return IsValidNonRowScopedDottedNameNode(node, binding, metadata, opDelStrategy);
Expand Down Expand Up @@ -262,13 +264,13 @@ public bool IsValidDottedNameNode(DottedNameNode node, TexlBinding binding, Oper
return opDelStrategy?.IsOpSupportedByColumn(entityCapabilityMetadata, node, entityColumnPath, binding) ?? true;
}

public bool IsValidFirstNameNode(FirstNameNode node, TexlBinding binding, IOpDelegationStrategy opDelStrategy)
public bool IsValidFirstNameNode(FirstNameNode node, TexlBinding binding, IOpDelegationStrategy opDelStrategy, bool nodeInheritsRowScope = false)
{
Contracts.AssertValue(node);
Contracts.AssertValue(binding);
Contracts.AssertValueOrNull(opDelStrategy);

var isRowScoped = binding.IsRowScope(node);
var isRowScoped = binding.IsRowScope(node) || nodeInheritsRowScope;
var isValid = IsValidAsyncOrImpureNode(node, binding);
if (isValid && !isRowScoped)
{
Expand Down Expand Up @@ -312,12 +314,12 @@ private IDelegationMetadata GetCapabilityMetadata(FirstNameInfo info)
}

// Verifies if provided column node supports delegation.
protected bool IsDelegatableColumnNode(FirstNameNode node, TexlBinding binding, IOpDelegationStrategy opDelStrategy, DelegationCapability capability)
protected bool IsDelegatableColumnNode(FirstNameNode node, TexlBinding binding, IOpDelegationStrategy opDelStrategy, DelegationCapability capability, bool nodeInheritsRowScope = false)
{
Contracts.AssertValue(node);
Contracts.AssertValue(binding);
Contracts.AssertValueOrNull(opDelStrategy);
Contracts.Assert(binding.IsRowScope(node));
Contracts.Assert(binding.IsRowScope(node) || nodeInheritsRowScope);

var firstNameInfo = binding.GetInfo(node.AsFirstName());
if (firstNameInfo == null)
Expand Down Expand Up @@ -359,7 +361,7 @@ protected bool IsDelegatableColumnNode(FirstNameNode node, TexlBinding binding,
return true;
}

public virtual bool IsValidCallNode(CallNode node, TexlBinding binding, OperationCapabilityMetadata metadata, TexlFunction trackingFunction = null)
public virtual bool IsValidCallNode(CallNode node, TexlBinding binding, OperationCapabilityMetadata metadata, TexlFunction trackingFunction = null, bool nodeInheritsRowScope = false)
{
// Functions may have their specific CallNodeDelegationStrategies (i.e. AsType, User)
// so, if available, we need to ensure we use their specific delegation strategy.
Expand All @@ -370,13 +372,13 @@ public virtual bool IsValidCallNode(CallNode node, TexlBinding binding, Operatio
if (function != null && function != Function)
{
// We need to keep track of the tracking function for delegation tracking telemetry to be consistent.
return function.GetCallNodeDelegationStrategy().IsValidCallNode(node, binding, metadata, trackingFunction ?? Function);
return function.GetCallNodeDelegationStrategy().IsValidCallNode(node, binding, metadata, trackingFunction ?? Function, nodeInheritsRowScope: nodeInheritsRowScope);
}

return IsValidCallNodeInternal(node, binding, metadata, trackingFunction ?? Function);
return IsValidCallNodeInternal(node, binding, metadata, trackingFunction ?? Function, nodeInheritsRowScope: nodeInheritsRowScope);
}

protected bool IsValidCallNodeInternal(CallNode node, TexlBinding binding, OperationCapabilityMetadata metadata, TexlFunction trackingFunction = null)
protected bool IsValidCallNodeInternal(CallNode node, TexlBinding binding, OperationCapabilityMetadata metadata, TexlFunction trackingFunction = null, bool nodeInheritsRowScope = false)
{
Contracts.AssertValue(node);
Contracts.AssertValue(binding);
Expand All @@ -393,7 +395,7 @@ protected bool IsValidCallNodeInternal(CallNode node, TexlBinding binding, Opera
}

// If the node is not row scoped and it's valid then it can be delegated.
var isRowScoped = binding.IsRowScope(node);
var isRowScoped = binding.IsRowScope(node) || nodeInheritsRowScope;
if (!isRowScoped)
{
return true;
Expand All @@ -405,9 +407,16 @@ protected bool IsValidCallNodeInternal(CallNode node, TexlBinding binding, Opera
SuggestDelegationHint(node, binding, warning, new object[] { callInfo?.Function.Name });
}

if (callInfo?.Function != null && ((TexlFunction)callInfo.Function).IsRowScopedServerDelegatable(node, binding, metadata))
if (callInfo?.Function != null && ((TexlFunction)callInfo.Function).IsRowScopedServerDelegatable(node, binding, metadata, nodeInheritsRowScope: nodeInheritsRowScope))
{
return true;
}

if (callInfo?.Function is UserDefinedFunction udf &&
((FilterFunction)trackingFunction).IsValidDelegatableFilterPredicateNode(udf.Binding.Top, udf.Binding, metadata as FilterOpMetadata, nodeInheritsRowScope: isRowScoped))
rick-nguyen marked this conversation as resolved.
Show resolved Hide resolved
{
udf.SetSupportRowScopedServerDelegation(true);
return true;
}

var telemetryMessage = string.Format(CultureInfo.InvariantCulture, "Kind:{0}, isRowScoped:{1}", node.Kind, isRowScoped);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ internal interface IOpDelegationStrategy

bool IsOpSupportedByTable(OperationCapabilityMetadata metadata, TexlNode node, TexlBinding binder);

bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding);
bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding, bool nodeInheritsRowScope = false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public InOpDelegationStrategy(BinaryOpNode node, TexlFunction function)
_binaryOpNode = node;
}

public override bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding)
public override bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding, bool nodeInheritsRowScope = false)
{
Contracts.AssertValue(node);
Contracts.AssertValue(metadata);
Expand All @@ -52,7 +52,7 @@ public override bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadat
return false;
}

var isRowScopedOrLambda = IsRowScopedOrLambda(binding, node, info, columnName, metadata);
var isRowScopedOrLambda = IsRowScopedOrLambda(binding, node, info, columnName, metadata) || nodeInheritsRowScope;
if (!isRowScopedOrLambda)
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public virtual bool IsOpSupportedByTable(OperationCapabilityMetadata metadata, T
}

// Verifies if given kind of node is supported by function delegation.
private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding, IOpDelegationStrategy opDelStrategy, bool isRHSNode)
private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding, IOpDelegationStrategy opDelStrategy, bool isRHSNode, bool nodeInheritsRowScope = false)
{
Contracts.AssertValue(node);
Contracts.AssertValue(metadata);
Expand Down Expand Up @@ -112,7 +112,7 @@ private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata
}

// Non row-scope, non async, pure nodes should always be valid because we can calculate value in runtime before delegation.
if (!binding.IsRowScope(node) && !binding.IsAsync(node) && binding.IsPure(node))
if (!binding.IsRowScope(node) && !binding.IsAsync(node) && binding.IsPure(node) && !nodeInheritsRowScope)
{
return true;
}
Expand All @@ -138,7 +138,7 @@ private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata
}

var cNodeValStrategy = _function.GetCallNodeDelegationStrategy();
return cNodeValStrategy.IsValidCallNode(node.AsCall(), binding, metadata);
return cNodeValStrategy.IsValidCallNode(node.AsCall(), binding, metadata, nodeInheritsRowScope: nodeInheritsRowScope);
}

case NodeKind.FirstName:
Expand All @@ -156,7 +156,7 @@ private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata

var unaryopNode = node.AsUnaryOpLit();
var unaryOpNodeDelegationStrategy = _function.GetOpDelegationStrategy(unaryopNode.Op);
return unaryOpNodeDelegationStrategy.IsSupportedOpNode(unaryopNode, metadata, binding);
return unaryOpNodeDelegationStrategy.IsSupportedOpNode(unaryopNode, metadata, binding, nodeInheritsRowScope: nodeInheritsRowScope);
}

case NodeKind.BinaryOp:
Expand All @@ -172,7 +172,7 @@ private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata
var binaryOpDelStrategy = (opDelStrategy as BinaryOpDelegationStrategy).VerifyValue();
Contracts.Assert(binaryOpNode.Op == binaryOpDelStrategy.Op);

if (!opDelStrategy.IsSupportedOpNode(node, metadata, binding))
if (!opDelStrategy.IsSupportedOpNode(node, metadata, binding, nodeInheritsRowScope: nodeInheritsRowScope))
{
SuggestDelegationHint(binaryOpNode, binding);
return false;
Expand Down Expand Up @@ -263,7 +263,7 @@ private bool DoCoercionCheck(BinaryOpNode binaryOpNode, OperationCapabilityMetad
return true;
}

public virtual bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding)
public virtual bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding, bool nodeInheritsRowScope = false)
{
Contracts.AssertValue(node);
Contracts.AssertValue(metadata);
Expand Down Expand Up @@ -298,13 +298,13 @@ public virtual bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata
return false;
}

if (!IsSupportedNode(binaryOpNode.Left, metadata, binding, opDelStrategy, false))
if (!IsSupportedNode(binaryOpNode.Left, metadata, binding, opDelStrategy, false, nodeInheritsRowScope))
{
SuggestDelegationHint(node, binding);
return false;
}

if (!IsSupportedNode(binaryOpNode.Right, metadata, binding, opDelStrategy, true))
if (!IsSupportedNode(binaryOpNode.Right, metadata, binding, opDelStrategy, true, nodeInheritsRowScope))
{
SuggestDelegationHint(node, binding);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ public virtual bool IsOpSupportedByTable(OperationCapabilityMetadata metadata, T
return true;
}

private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding, IOpDelegationStrategy opDelStrategy)
private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding, IOpDelegationStrategy opDelStrategy, bool nodeInheritsRowScope = false)
{
Contracts.AssertValue(node);
Contracts.AssertValue(metadata);
Contracts.AssertValue(binding);
Contracts.AssertValue(opDelStrategy);

if (!binding.IsRowScope(node))
if (!binding.IsRowScope(node) && nodeInheritsRowScope)
{
return true;
}
Expand Down Expand Up @@ -150,7 +150,7 @@ private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata
return false;
}

public virtual bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding)
public virtual bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding, bool nodeInheritsRowScope = false)
{
Contracts.AssertValue(node);
Contracts.AssertValue(metadata);
Expand Down Expand Up @@ -179,7 +179,7 @@ public virtual bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata
return false;
}

return IsSupportedNode(unaryOpNode.Child, metadata, binding, opDelStrategy);
return IsSupportedNode(unaryOpNode.Child, metadata, binding, opDelStrategy, nodeInheritsRowScope);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -945,12 +945,12 @@ public virtual bool SupportsPaging(CallNode callNode, TexlBinding binding)

// Returns true if function is row scoped and supports delegation.
// Needs to be overriden by functions (For example, IsBlank) which are not server delegatable themselves but can become one when scoped inside a delegatable function.
public virtual bool IsRowScopedServerDelegatable(CallNode callNode, TexlBinding binding, OperationCapabilityMetadata metadata)
public virtual bool IsRowScopedServerDelegatable(CallNode callNode, TexlBinding binding, OperationCapabilityMetadata metadata, bool nodeInheritsRowScope = false)
rick-nguyen marked this conversation as resolved.
Show resolved Hide resolved
{
Contracts.AssertValue(callNode);
Contracts.AssertValue(binding);

if (!binding.IsRowScope(callNode))
if (!binding.IsRowScope(callNode) && !nodeInheritsRowScope)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.PowerFx.Core.Binding.BindInfo;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.Errors;
using Microsoft.PowerFx.Core.Functions.Delegation;
using Microsoft.PowerFx.Core.Functions.FunctionArgValidators;
using Microsoft.PowerFx.Core.Glue;
using Microsoft.PowerFx.Core.IR;
Expand Down Expand Up @@ -44,7 +45,9 @@ internal class UserDefinedFunction : TexlFunction, IExternalPageableSymbol, IExt

public bool IsPageable => _binding.IsPageable(_binding.Top);

public bool IsDelegatable => _binding.IsDelegatable(_binding.Top);
public bool IsDelegatable => _binding.IsDelegatable(_binding.Top);

public bool SupportsRowScopedServerDelegation;
rick-nguyen marked this conversation as resolved.
Show resolved Hide resolved

public override bool IsServerDelegatable(CallNode callNode, TexlBinding binding)
{
Expand All @@ -53,7 +56,12 @@ public override bool IsServerDelegatable(CallNode callNode, TexlBinding binding)
Contracts.Assert(binding.GetInfo(callNode).Function is UserDefinedFunction udf && udf.Binding != null);

return base.IsServerDelegatable(callNode, binding) || IsDelegatable;
}
}

public override bool IsRowScopedServerDelegatable(CallNode callNode, TexlBinding binding, OperationCapabilityMetadata metadata, bool nodeInheritsRowScope = false)
{
return SupportsRowScopedServerDelegation;
}

public override bool SupportsParamCoercion => true;

Expand Down Expand Up @@ -92,7 +100,8 @@ public UserDefinedFunction(string functionName, DType returnType, TexlNode body,
this._args = args;
this._isImperative = isImperative;

this.UdfBody = body;
this.UdfBody = body;
this.SupportsRowScopedServerDelegation = false;
}

/// <summary>
Expand Down Expand Up @@ -370,8 +379,13 @@ internal static bool IsRestrictedType(FormulaType ft)
}

return false;
}

}

internal void SetSupportRowScopedServerDelegation(bool value)
{
SupportsRowScopedServerDelegation = value;
}

/// <summary>
/// NameResolver that combines global named resolver and params for user defined function.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static bool IsExternalSource(object externalDataSource)
tDsInfo is IExternalTabularDataSource;
}

public override bool IsRowScopedServerDelegatable(CallNode callNode, TexlBinding binding, OperationCapabilityMetadata metadata)
public override bool IsRowScopedServerDelegatable(CallNode callNode, TexlBinding binding, OperationCapabilityMetadata metadata, bool nodeInheritsRowScope = false)
{
return metadata.IsDelegationSupportedByTable(DelegationCapability.AsType);
}
Expand Down
Loading