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 all 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, bool nodeInheritsRowScope, TexlFunction trackingFunction = null);
}

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);
}

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

internal class DelegationValidationStrategy
Expand Down Expand Up @@ -91,7 +93,7 @@ protected void SuggestDelegationHint(TexlNode node, TexlBinding binding)
SuggestDelegationHint(node, binding, null);
}

private bool IsValidRowScopedDottedNameNode(DottedNameNode node, TexlBinding binding, OperationCapabilityMetadata metadata, out bool isRowScopedDelegationExempted)
private bool IsValidRowScopedDottedNameNode(DottedNameNode node, TexlBinding binding, OperationCapabilityMetadata metadata, bool nodeInheritsRowScope, out bool isRowScopedDelegationExempted)
{
Contracts.AssertValue(node);
Contracts.AssertValue(binding);
Expand All @@ -108,31 +110,31 @@ private bool IsValidRowScopedDottedNameNode(DottedNameNode node, TexlBinding bin

if (node.Left.Kind == NodeKind.Call)
{
return IsValidCallNode(node.Left as CallNode, binding, metadata);
return IsValidCallNode(node.Left as CallNode, binding, metadata, nodeInheritsRowScope);
}

if (node.Left.Kind == NodeKind.DottedName)
{
return IsValidRowScopedDottedNameNode(node.Left.AsDottedName(), binding, metadata, out isRowScopedDelegationExempted);
return IsValidRowScopedDottedNameNode(node.Left.AsDottedName(), binding, metadata, nodeInheritsRowScope, out isRowScopedDelegationExempted);
}

return node.Left.Kind == NodeKind.FirstName;
}

private bool IsValidNonRowScopedDottedNameNode(DottedNameNode node, TexlBinding binding, OperationCapabilityMetadata metadata, IOpDelegationStrategy opDelStrategy)
private bool IsValidNonRowScopedDottedNameNode(DottedNameNode node, TexlBinding binding, OperationCapabilityMetadata metadata, IOpDelegationStrategy opDelStrategy, bool nodeInheritsRowScope)
{
Contracts.AssertValue(node);
Contracts.AssertValue(binding);
Contracts.AssertValueOrNull(opDelStrategy);

if (node.Left.Kind == NodeKind.Call)
{
return IsValidCallNode(node.Left as CallNode, binding, metadata);
return IsValidCallNode(node.Left as CallNode, binding, metadata, nodeInheritsRowScope);
}

if (node.Left.Kind == NodeKind.DottedName)
{
return IsValidDottedNameNode(node.Left.AsDottedName(), binding, metadata, opDelStrategy);
return IsValidDottedNameNode(node.Left.AsDottedName(), binding, metadata, opDelStrategy, nodeInheritsRowScope);
}

if (node.Left.Kind == NodeKind.FirstName)
Expand Down Expand Up @@ -175,19 +177,19 @@ 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)
{
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);
return IsValidNonRowScopedDottedNameNode(node, binding, metadata, opDelStrategy, nodeInheritsRowScope);
}

if (!IsValidRowScopedDottedNameNode(node, binding, metadata, out var isRowScopedDelegationExempted))
if (!IsValidRowScopedDottedNameNode(node, binding, metadata, nodeInheritsRowScope, out var isRowScopedDelegationExempted))
{
var telemetryMessage = string.Format(CultureInfo.InvariantCulture, "Kind:{0}, isRowScoped:{1}", node.Kind, isRowScoped);

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)
{
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 All @@ -281,7 +283,7 @@ public bool IsValidFirstNameNode(FirstNameNode node, TexlBinding binding, IOpDel
return false;
}

return IsDelegatableColumnNode(node, binding, opDelStrategy, Function.FunctionDelegationCapability);
return IsDelegatableColumnNode(node, binding, opDelStrategy, Function.FunctionDelegationCapability, nodeInheritsRowScope);
}

private IDelegationMetadata GetCapabilityMetadata(FirstNameInfo info)
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)
{
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, bool nodeInheritsRowScope, TexlFunction trackingFunction = null)
{
// 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, nodeInheritsRowScope: nodeInheritsRowScope, trackingFunction ?? Function);
}

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

protected bool IsValidCallNodeInternal(CallNode node, TexlBinding binding, OperationCapabilityMetadata metadata, TexlFunction trackingFunction = null)
protected bool IsValidCallNodeInternal(CallNode node, TexlBinding binding, OperationCapabilityMetadata metadata, bool nodeInheritsRowScope, TexlFunction trackingFunction = null)
{
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,7 +407,17 @@ 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 is UserDefinedFunction udf &&
node.Parent?.Parent is CallNode parentNode && binding.GetInfo(parentNode).Function is FilterFunctionBase filterFunc)
{
if (filterFunc.IsValidDelegatableFilterPredicateNode(udf.Binding.Top, udf.Binding, metadata as FilterOpMetadata, generateHints: false, nodeInheritsRowScope: isRowScoped))
{
udf.SetSupportRowScopedServerDelegation(true);
return true;
}
}

if (callInfo?.Function != null && ((TexlFunction)callInfo.Function).IsRowScopedServerDelegatable(node, binding, metadata, nodeInheritsRowScope: nodeInheritsRowScope))
{
return true;
}
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)
{
Contracts.AssertValue(node);
Contracts.AssertValue(metadata);
Expand All @@ -40,7 +40,7 @@ public override bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadat
var isRHSDelegableTable = IsRHSDelegableTable(binding, binaryOpNode, metadata);
if (isRHSDelegableTable && binaryOpNode.Left is DottedNameNode dottedField && binding.GetType(dottedField.Left).HasExpandInfo)
{
return base.IsSupportedOpNode(node, metadata, binding);
return base.IsSupportedOpNode(node, metadata, binding, nodeInheritsRowScope);
}

DName columnName = default;
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 All @@ -68,7 +68,7 @@ public override bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadat
return false;
}

return base.IsSupportedOpNode(node, metadata, binding);
return base.IsSupportedOpNode(node, metadata, binding, nodeInheritsRowScope);
}

public bool IsRHSDelegableTable(TexlBinding binding, BinaryOpNode binaryOpNode, OperationCapabilityMetadata metadata)
Expand Down
Loading